From 66e5920dc3411daa4f0c84a8f4c733c1263e8523 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 27 Jul 2021 15:47:25 -0700 Subject: llvm backend: LLVMGetNamedGlobalAlias requires a null terminated string --- src/codegen/llvm.zig | 8 ++++---- src/codegen/llvm/bindings.zig | 5 ++++- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 2468b1dbde..74a51e6634 100644 --- a/src/codegen/llvm.zig +++ b/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; } diff --git a/src/codegen/llvm/bindings.zig b/src/codegen/llvm/bindings.zig index d63a75d8bd..6d09d69447 100644 --- a/src/codegen/llvm/bindings.zig +++ b/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; }; -- cgit v1.2.3