authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-12-17 17:17:34+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-12-17 17:17:34+01:00
log2a62dbda0bb5e5c8a1c92a058b684309bd7efeeb
tree923fba4c3fff74adf5e5e7f2ec921b6c7825ba46
parent476202eec03a2196daab7f9998c556796cc42eca
signature Commit is signed but in an unrecognized format.

wasm-linker: fix type index relocations

Previously we used the relocation index to find the corresponding symbol that represents the type. However, the index actually represents the index into the list of types. We solved this by first retrieving the original type, and then finding its location in the new list of types. When the atom file is 'null', it means the type originates from a Zig function pointer or a synthetic function. In both cases, the final type index was already resolved and therefore equals to relocation's index value.

1 files changed, 7 insertions(+), 6 deletions(-)

src/link/Wasm/Atom.zig+7-6
......@@ -168,12 +168,13 @@ fn relocationValue(atom: Atom, relocation: types.Relocation, wasm_bin: *const Wa
168168 .R_WASM_TABLE_INDEX_SLEB,
169169 .R_WASM_TABLE_INDEX_SLEB64,
170170 => return wasm_bin.function_table.get(target_loc) orelse 0,
171 .R_WASM_TYPE_INDEX_LEB => return blk: {
172 if (symbol.isUndefined()) {
173 const imp = wasm_bin.imports.get(target_loc).?;
174 break :blk imp.kind.function;
175 }
176 break :blk wasm_bin.functions.values()[symbol.index - wasm_bin.imported_functions_count].type_index;
171 .R_WASM_TYPE_INDEX_LEB => {
172 const file_index = atom.file orelse {
173 return relocation.index;
174 };
175
176 const original_type = wasm_bin.objects.items[file_index].func_types[relocation.index];
177 return wasm_bin.getTypeIndex(original_type).?;
177178 },
178179 .R_WASM_GLOBAL_INDEX_I32,
179180 .R_WASM_GLOBAL_INDEX_LEB,