diff options
| author | Luuk de Gram <luuk@degram.dev> | 2022-09-01 22:02:24 +0200 |
|---|---|---|
| committer | Luuk de Gram <luuk@degram.dev> | 2022-09-07 18:53:16 +0200 |
| commit | 46c932a2c9650f14ae8035d7382d825bfabdc0a5 (patch) | |
| tree | e819d6b20acb736af12a5b7f35ce4c34e17f3600 /src | |
| parent | c347751338a4a1e2874207674fb47908fd601484 (diff) | |
| download | zig-46c932a2c9650f14ae8035d7382d825bfabdc0a5.tar.gz zig-46c932a2c9650f14ae8035d7382d825bfabdc0a5.zip | |
wasm-linker: perform debug relocations
This correctly performs a relocation for debug sections.
The result is that the wasm-linker can now correctly create
a binary from object files while preserving all debug information.
Diffstat (limited to 'src')
| -rw-r--r-- | src/link/Wasm.zig | 12 | ||||
| -rw-r--r-- | src/link/Wasm/Atom.zig | 7 | ||||
| -rw-r--r-- | src/link/Wasm/Object.zig | 3 |
3 files changed, 16 insertions, 6 deletions
diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index b17eeb57e9..2ff631d9ba 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -221,6 +221,18 @@ pub const SymbolLoc = struct { } return wasm_bin.string_table.get(wasm_bin.symbols.items[self.index].name); } + + /// From a given symbol location, returns the final location. + /// e.g. when a symbol was resolved and replaced by the symbol + /// in a different file, this will return said location. + /// If the symbol wasn't replaced by another, this will return + /// the given location itself. + pub fn finalLoc(self: SymbolLoc, wasm_bin: *const Wasm) SymbolLoc { + if (wasm_bin.discarded.get(self)) |new_loc| { + return new_loc.finalLoc(wasm_bin); + } + return self; + } }; /// Generic string table that duplicates strings diff --git a/src/link/Wasm/Atom.zig b/src/link/Wasm/Atom.zig index 9e7f7a5a76..64efa8320f 100644 --- a/src/link/Wasm/Atom.zig +++ b/src/link/Wasm/Atom.zig @@ -145,7 +145,7 @@ pub fn resolveRelocs(self: *Atom, wasm_bin: *const Wasm) void { /// All values will be represented as a `u64` as all values can fit within it. /// The final value must be casted to the correct size. fn relocationValue(self: Atom, relocation: types.Relocation, wasm_bin: *const Wasm) u64 { - const target_loc: Wasm.SymbolLoc = .{ .file = self.file, .index = relocation.index }; + const target_loc = (Wasm.SymbolLoc{ .file = self.file, .index = relocation.index }).finalLoc(wasm_bin); const symbol = target_loc.getSymbol(wasm_bin).*; switch (relocation.relocation_type) { .R_WASM_FUNCTION_INDEX_LEB => return symbol.index, @@ -174,8 +174,7 @@ fn relocationValue(self: Atom, relocation: types.Relocation, wasm_bin: *const Wa => { std.debug.assert(symbol.tag == .data and !symbol.isUndefined()); const merge_segment = wasm_bin.base.options.output_mode != .Obj; - const target_atom_loc = wasm_bin.discarded.get(target_loc) orelse target_loc; - const target_atom = wasm_bin.symbol_atom.get(target_atom_loc).?; + const target_atom = wasm_bin.symbol_atom.get(target_loc).?; const segment_info = if (target_atom.file) |object_index| blk: { break :blk wasm_bin.objects.items[object_index].segment_info; } else wasm_bin.segment_info.items; @@ -187,6 +186,6 @@ fn relocationValue(self: Atom, relocation: types.Relocation, wasm_bin: *const Wa .R_WASM_EVENT_INDEX_LEB => return symbol.index, .R_WASM_SECTION_OFFSET_I32, .R_WASM_FUNCTION_OFFSET_I32, - => return relocation.offset, + => return relocation.addend orelse 0, } } diff --git a/src/link/Wasm/Object.zig b/src/link/Wasm/Object.zig index fac5ce3aa8..86c9cdc023 100644 --- a/src/link/Wasm/Object.zig +++ b/src/link/Wasm/Object.zig @@ -387,11 +387,10 @@ fn Parser(comptime ReaderType: type) type { .data = debug_content.ptr, .size = debug_size, .index = try self.object.string_table.put(gpa, name), - .offset = len - debug_size, + .offset = 0, // debug sections only contain 1 entry, so no need to calculate offset .section_index = section_index, }); } else { - log.info("found unknown custom section '{s}' - skipping parsing", .{name}); try reader.skipBytes(reader.context.bytes_left, .{}); } }, |
