Luuk de Gram b9224c172f
wasm-linker: Fix & mangle symbol name of imports
When outputting the names section, we should output the actual symbol
name rather than the import name. This makes sure that symbols with
an explicit name set have the correct name but retain the import name
too.

We also now correctly mangle the name of an extern function with an
explicit library name. This ensures that functions that have a
different library name, but the same import/function name, can be
resolved correctly with other modules and don't resolve to the
same symbol.
2023-01-03 10:02:54 +01:00

40 lines
1.6 KiB
Zig

const std = @import("std");
const Builder = std.build.Builder;
pub fn build(b: *Builder) void {
const test_step = b.step("test", "Test");
test_step.dependOn(b.getInstallStep());
const lib = b.addSharedLibrary("lib", "lib.zig", .unversioned);
lib.setBuildMode(.ReleaseSafe); // to make the output deterministic in address positions
lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
lib.use_lld = false;
lib.export_symbol_names = &.{ "foo", "bar" };
lib.global_base = 0; // put data section at address 0 to make data symbols easier to parse
const check_lib = lib.checkObject(.wasm);
check_lib.checkStart("Section global");
check_lib.checkNext("entries 3");
check_lib.checkNext("type i32"); // stack pointer so skip other fields
check_lib.checkNext("type i32");
check_lib.checkNext("mutable false");
check_lib.checkNext("i32.const {foo_address}");
check_lib.checkNext("type i32");
check_lib.checkNext("mutable false");
check_lib.checkNext("i32.const {bar_address}");
check_lib.checkComputeCompare("foo_address", .{ .op = .eq, .value = .{ .literal = 0 } });
check_lib.checkComputeCompare("bar_address", .{ .op = .eq, .value = .{ .literal = 4 } });
check_lib.checkStart("Section export");
check_lib.checkNext("entries 3");
check_lib.checkNext("name foo");
check_lib.checkNext("kind global");
check_lib.checkNext("index 1");
check_lib.checkNext("name bar");
check_lib.checkNext("kind global");
check_lib.checkNext("index 2");
test_step.dependOn(&check_lib.step);
}