authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-07-16 16:31:07+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-07-19 17:22:46+02:00
log376e1b4603e70f1407862fde0180cfe45ca56e73
treea10cd86492b027721f23c1d27d13d432dde11fe5
parent388589987c104354c072d04128327e9823c87497
signaturelock-open Commit is signed but in an unrecognized format.

wasm-linker: implement TLS relocations


2 files changed, 15 insertions(+), 7 deletions(-)

src/link/Wasm.zig+8-1
......@@ -2153,7 +2153,14 @@ fn allocateVirtualAddresses(wasm: *Wasm) void {
21532153 const segment_name = segment_info[symbol.index].outputName(merge_segment);
21542154 const segment_index = wasm.data_segments.get(segment_name).?;
21552155 const segment = wasm.segments.items[segment_index];
2156 symbol.virtual_address = atom.offset + segment.offset;
2156
2157 // TLS symbols have their virtual address set relative to their own TLS segment,
2158 // rather than the entire Data section.
2159 if (symbol.hasFlag(.WASM_SYM_TLS)) {
2160 symbol.virtual_address = atom.offset;
2161 } else {
2162 symbol.virtual_address = atom.offset + segment.offset;
2163 }
21572164 }
21582165}
21592166
src/link/Wasm/Atom.zig+7-6
......@@ -174,14 +174,14 @@ fn relocationValue(atom: Atom, relocation: types.Relocation, wasm_bin: *const Wa
174174 return 0;
175175 }
176176 const va = @as(i64, @intCast(symbol.virtual_address));
177 return @as(u32, @intCast(va + relocation.addend));
177 return @intCast(va + relocation.addend);
178178 },
179179 .R_WASM_EVENT_INDEX_LEB => return symbol.index,
180180 .R_WASM_SECTION_OFFSET_I32 => {
181181 const target_atom_index = wasm_bin.symbol_atom.get(target_loc).?;
182182 const target_atom = wasm_bin.getAtom(target_atom_index);
183 const rel_value = @as(i32, @intCast(target_atom.offset)) + relocation.addend;
184 return @as(u32, @intCast(rel_value));
183 const rel_value: i32 = @intCast(target_atom.offset);
184 return @intCast(rel_value + relocation.addend);
185185 },
186186 .R_WASM_FUNCTION_OFFSET_I32 => {
187187 const target_atom_index = wasm_bin.symbol_atom.get(target_loc) orelse {
......@@ -189,13 +189,14 @@ fn relocationValue(atom: Atom, relocation: types.Relocation, wasm_bin: *const Wa
189189 };
190190 const target_atom = wasm_bin.getAtom(target_atom_index);
191191 const offset: u32 = 11 + Wasm.getULEB128Size(target_atom.size); // Header (11 bytes fixed-size) + body size (leb-encoded)
192 const rel_value = @as(i32, @intCast(target_atom.offset + offset)) + relocation.addend;
193 return @as(u32, @intCast(rel_value));
192 const rel_value: i32 = @intCast(target_atom.offset + offset);
193 return @intCast(rel_value + relocation.addend);
194194 },
195195 .R_WASM_MEMORY_ADDR_TLS_SLEB,
196196 .R_WASM_MEMORY_ADDR_TLS_SLEB64,
197197 => {
198 @panic("TODO: Implement TLS relocations");
198 const va: i32 = @intCast(symbol.virtual_address);
199 return @intCast(va + relocation.addend);
199200 },
200201 }
201202}