Commit 66e5920dc3

Andrew Kelley <andrew@ziglang.org>
2021-07-28 00:47:25
llvm backend: LLVMGetNamedGlobalAlias requires a null terminated string
1 parent a2eb91c
Changed files (2)
src
src/codegen/llvm/bindings.zig
@@ -193,7 +193,10 @@ pub const Module = opaque {
     pub const getNamedGlobalAlias = LLVMGetNamedGlobalAlias;
     extern fn LLVMGetNamedGlobalAlias(
         M: *const Module,
-        Name: [*]const u8,
+        /// Empirically, LLVM will call strlen() on `Name` and so it
+        /// must be both null terminated and also have `NameLen` set
+        /// to the size.
+        Name: [*:0]const u8,
         NameLen: usize,
     ) ?*const Value;
 };
src/codegen/llvm.zig
@@ -454,12 +454,12 @@ pub const Object = struct {
         // Until then we iterate over existing aliases and make them point
         // to the correct decl, or otherwise add a new alias. Old aliases are leaked.
         for (exports) |exp| {
-            if (self.llvm_module.getNamedGlobalAlias(exp.options.name.ptr, exp.options.name.len)) |alias| {
+            const exp_name_z = try module.gpa.dupeZ(u8, exp.options.name);
+            defer module.gpa.free(exp_name_z);
+
+            if (self.llvm_module.getNamedGlobalAlias(exp_name_z.ptr, exp_name_z.len)) |alias| {
                 alias.setAliasee(llvm_fn);
             } else {
-                const exp_name_z = try module.gpa.dupeZ(u8, exp.options.name);
-                defer module.gpa.free(exp_name_z);
-
                 const alias = self.llvm_module.addAlias(llvm_fn.typeOf(), llvm_fn, exp_name_z);
                 _ = alias;
             }