authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-08-26 08:11:17+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-08-30 16:38:55+02:00
log4f72ac265acac682541f170a1189a06350009431
tree4724681222846d5303f56e63886e4d4ba4fe7e09
parent414fcea162a751435f0194ed4a01785b3a0913a0
signaturelock-open Commit is signed but in an unrecognized format.

wasm: create relocations for extern decls

This also fixes performing relocations for data symbols of which the target symbol exists in an external object file. We do this by checking if the target symbol was discarded, and if so: get the new location so that we can find the corresponding atom that belongs to said new location. Previously it would always assume the symbol would live in the same file as the atom/symbol that is doing the relocation.

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

src/arch/wasm/CodeGen.zig+1-3
......@@ -2355,7 +2355,7 @@ fn lowerDeclRefValue(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index)
23552355
23562356 const module = self.bin_file.base.options.module.?;
23572357 const decl = module.declPtr(decl_index);
2358 if (decl.ty.zigTypeTag() != .Fn and !decl.ty.hasRuntimeBitsIgnoreComptime()) {
2358 if (!decl.ty.hasRuntimeBitsIgnoreComptime()) {
23592359 return WValue{ .imm32 = 0xaaaaaaaa };
23602360 }
23612361
......@@ -2394,9 +2394,7 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue {
23942394 const decl_index = decl_ref_mut.data.decl_index;
23952395 return self.lowerDeclRefValue(.{ .ty = ty, .val = val }, decl_index);
23962396 }
2397
23982397 const target = self.target;
2399
24002398 switch (ty.zigTypeTag()) {
24012399 .Void => return WValue{ .none = {} },
24022400 .Int => {
src/link/Wasm/Atom.zig+5-4
......@@ -174,13 +174,14 @@ fn relocationValue(self: Atom, relocation: types.Relocation, wasm_bin: *const Wa
174174 => {
175175 std.debug.assert(symbol.tag == .data and !symbol.isUndefined());
176176 const merge_segment = wasm_bin.base.options.output_mode != .Obj;
177 const segment_info = if (self.file) |object_index| blk: {
177 const target_atom_loc = wasm_bin.discarded.get(target_loc) orelse target_loc;
178 const target_atom = wasm_bin.symbol_atom.get(target_atom_loc).?;
179 const segment_info = if (target_atom.file) |object_index| blk: {
178180 break :blk wasm_bin.objects.items[object_index].segment_info;
179181 } else wasm_bin.segment_info.items;
180182 const segment_name = segment_info[symbol.index].outputName(merge_segment);
181 const atom_index = wasm_bin.data_segments.get(segment_name).?;
182 const target_atom = wasm_bin.symbol_atom.get(target_loc).?;
183 const segment = wasm_bin.segments.items[atom_index];
183 const segment_index = wasm_bin.data_segments.get(segment_name).?;
184 const segment = wasm_bin.segments.items[segment_index];
184185 return target_atom.offset + segment.offset + (relocation.addend orelse 0);
185186 },
186187 .R_WASM_EVENT_INDEX_LEB => return symbol.index,