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 {...@@ -2153,7 +2153,14 @@ fn allocateVirtualAddresses(wasm: *Wasm) void {
2153 const segment_name = segment_info[symbol.index].outputName(merge_segment);2153 const segment_name = segment_info[symbol.index].outputName(merge_segment);
2154 const segment_index = wasm.data_segments.get(segment_name).?;2154 const segment_index = wasm.data_segments.get(segment_name).?;
2155 const segment = wasm.segments.items[segment_index];2155 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 }
2157 }2164 }
2158}2165}
21592166
src/link/Wasm/Atom.zig+7-6
...@@ -174,14 +174,14 @@ fn relocationValue(atom: Atom, relocation: types.Relocation, wasm_bin: *const Wa...@@ -174,14 +174,14 @@ fn relocationValue(atom: Atom, relocation: types.Relocation, wasm_bin: *const Wa
174 return 0;174 return 0;
175 }175 }
176 const va = @as(i64, @intCast(symbol.virtual_address));176 const va = @as(i64, @intCast(symbol.virtual_address));
177 return @as(u32, @intCast(va + relocation.addend));177 return @intCast(va + relocation.addend);
178 },178 },
179 .R_WASM_EVENT_INDEX_LEB => return symbol.index,179 .R_WASM_EVENT_INDEX_LEB => return symbol.index,
180 .R_WASM_SECTION_OFFSET_I32 => {180 .R_WASM_SECTION_OFFSET_I32 => {
181 const target_atom_index = wasm_bin.symbol_atom.get(target_loc).?;181 const target_atom_index = wasm_bin.symbol_atom.get(target_loc).?;
182 const target_atom = wasm_bin.getAtom(target_atom_index);182 const target_atom = wasm_bin.getAtom(target_atom_index);
183 const rel_value = @as(i32, @intCast(target_atom.offset)) + relocation.addend;183 const rel_value: i32 = @intCast(target_atom.offset);
184 return @as(u32, @intCast(rel_value));184 return @intCast(rel_value + relocation.addend);
185 },185 },
186 .R_WASM_FUNCTION_OFFSET_I32 => {186 .R_WASM_FUNCTION_OFFSET_I32 => {
187 const target_atom_index = wasm_bin.symbol_atom.get(target_loc) orelse {187 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...@@ -189,13 +189,14 @@ fn relocationValue(atom: Atom, relocation: types.Relocation, wasm_bin: *const Wa
189 };189 };
190 const target_atom = wasm_bin.getAtom(target_atom_index);190 const target_atom = wasm_bin.getAtom(target_atom_index);
191 const offset: u32 = 11 + Wasm.getULEB128Size(target_atom.size); // Header (11 bytes fixed-size) + body size (leb-encoded)191 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;192 const rel_value: i32 = @intCast(target_atom.offset + offset);
193 return @as(u32, @intCast(rel_value));193 return @intCast(rel_value + relocation.addend);
194 },194 },
195 .R_WASM_MEMORY_ADDR_TLS_SLEB,195 .R_WASM_MEMORY_ADDR_TLS_SLEB,
196 .R_WASM_MEMORY_ADDR_TLS_SLEB64,196 .R_WASM_MEMORY_ADDR_TLS_SLEB64,
197 => {197 => {
198 @panic("TODO: Implement TLS relocations");198 const va: i32 = @intCast(symbol.virtual_address);
199 return @intCast(va + relocation.addend);
199 },200 },
200 }201 }
201}202}