| author | |
| committer | |
| log | 5f11e341d8fee91acdb153c6e29ee8f484130f2d |
| tree | 475c89e07d91ae251707d18e9b2bdf7e04945b25 |
| parent | f354ad638aa7baf74a246723b7fcb01164cabbbd |
llvm: mangle extern function names for Wasm target6 files changed, 49 insertions(+), 2 deletions(-)
src/codegen/llvm.zig+12-2| ... | ... | @@ -1279,8 +1279,18 @@ pub const Object = struct { |
| 1279 | 1279 | const llvm_global = self.decl_map.get(decl_index) orelse return; |
| 1280 | 1280 | const decl = module.declPtr(decl_index); |
| 1281 | 1281 | if (decl.isExtern()) { |
| 1282 | llvm_global.setValueName(decl.name); | |
| 1283 | if (self.getLlvmGlobal(decl.name)) |other_global| { | |
| 1282 | const is_wasm_fn = module.getTarget().isWasm() and try decl.isFunction(); | |
| 1283 | const mangle_name = is_wasm_fn and | |
| 1284 | decl.getExternFn().?.lib_name != null and | |
| 1285 | !std.mem.eql(u8, std.mem.sliceTo(decl.getExternFn().?.lib_name.?, 0), "c"); | |
| 1286 | const decl_name = if (mangle_name) name: { | |
| 1287 | const tmp = try std.fmt.allocPrintZ(module.gpa, "{s}|{s}", .{ decl.name, decl.getExternFn().?.lib_name.? }); | |
| 1288 | break :name tmp.ptr; | |
| 1289 | } else decl.name; | |
| 1290 | defer if (mangle_name) module.gpa.free(std.mem.sliceTo(decl_name, 0)); | |
| 1291 | ||
| 1292 | llvm_global.setValueName(decl_name); | |
| 1293 | if (self.getLlvmGlobal(decl_name)) |other_global| { | |
| 1284 | 1294 | if (other_global != llvm_global) { |
| 1285 | 1295 | log.debug("updateDeclExports isExtern()=true setValueName({s}) conflict", .{decl.name}); |
| 1286 | 1296 | try self.extern_collisions.put(module.gpa, decl_index, {}); |
test/link.zig+5| ... | ... | @@ -48,6 +48,11 @@ fn addWasmCases(cases: *tests.StandaloneContext) void { |
| 48 | 48 | .use_emulation = true, |
| 49 | 49 | }); |
| 50 | 50 | |
| 51 | cases.addBuildFile("test/link/wasm/extern-mangle/build.zig", .{ | |
| 52 | .build_modes = true, | |
| 53 | .requires_stage2 = true, | |
| 54 | }); | |
| 55 | ||
| 51 | 56 | cases.addBuildFile("test/link/wasm/infer-features/build.zig", .{ |
| 52 | 57 | .requires_stage2 = true, |
| 53 | 58 | }); |
test/link/wasm/extern-mangle/a.zig created+1| ... | ... | @@ -0,0 +1 @@ |
| 1 | pub extern "a" fn hello() i32; |
test/link/wasm/extern-mangle/b.zig created+1| ... | ... | @@ -0,0 +1 @@ |
| 1 | pub extern "b" fn hello() i32; |
test/link/wasm/extern-mangle/build.zig created+24| ... | ... | @@ -0,0 +1,24 @@ |
| 1 | const std = @import("std"); | |
| 2 | const Builder = std.build.Builder; | |
| 3 | ||
| 4 | pub fn build(b: *Builder) void { | |
| 5 | const mode = b.standardReleaseOptions(); | |
| 6 | ||
| 7 | const test_step = b.step("test", "Test"); | |
| 8 | test_step.dependOn(b.getInstallStep()); | |
| 9 | ||
| 10 | const lib = b.addSharedLibrary("lib", "lib.zig", .unversioned); | |
| 11 | lib.setBuildMode(mode); | |
| 12 | lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); | |
| 13 | lib.install(); | |
| 14 | ||
| 15 | const check_lib = lib.checkObject(.wasm); | |
| 16 | check_lib.checkStart("Section import"); | |
| 17 | check_lib.checkNext("entries 2"); // a.hello & b.hello | |
| 18 | check_lib.checkNext("module a"); | |
| 19 | check_lib.checkNext("name hello"); | |
| 20 | check_lib.checkNext("module b"); | |
| 21 | check_lib.checkNext("name hello"); | |
| 22 | ||
| 23 | test_step.dependOn(&check_lib.step); | |
| 24 | } |
test/link/wasm/extern-mangle/lib.zig created+6| ... | ... | @@ -0,0 +1,6 @@ |
| 1 | const a = @import("a.zig").hello; | |
| 2 | const b = @import("b.zig").hello; | |
| 3 | export fn foo() void { | |
| 4 | _ = a(); | |
| 5 | _ = b(); | |
| 6 | } |