@@ -90,24 +90,26 @@ pub fn getFirst(atom: *Atom) *Atom {
9090 return tmp ;
9191}
9292
93- /// Unlike `getFirst` this returns the first `*Atom` that was
94- /// produced from Zig code, rather than an object file.
95- /// This is useful for debug sections where we want to extend
96- /// the bytes, and don't want to overwrite existing Atoms.
97- pub fn getFirstZigAtom (atom : * Atom ) * Atom {
98- if (atom .file == null ) return atom ;
99- var tmp = atom ;
100- return while (tmp .prev ) | prev | {
101- if (prev .file == null ) break prev ;
102- tmp = prev ;
103- } else unreachable ; // must allocate an Atom first!
104- }
105-
10693/// Returns the location of the symbol that represents this `Atom`
10794pub fn symbolLoc (atom : Atom ) Wasm.SymbolLoc {
10895 return .{ .file = atom .file , .index = atom .sym_index };
10996}
11097
98+ /// Returns the virtual address of the `Atom`. This is the address starting
99+ /// from the first entry within a section.
100+ pub fn getVA (atom : Atom , wasm : * const Wasm , symbol : * const Symbol ) u32 {
101+ if (symbol .tag == .function ) return atom .offset ;
102+ std .debug .assert (symbol .tag == .data );
103+ const merge_segment = wasm .base .options .output_mode != .Obj ;
104+ const segment_info = if (atom .file ) | object_index | blk : {
105+ break :blk wasm .objects .items [object_index ].segment_info ;
106+ } else wasm .segment_info .values ();
107+ const segment_name = segment_info [symbol .index ].outputName (merge_segment );
108+ const segment_index = wasm .data_segments .get (segment_name ).? ;
109+ const segment = wasm .segments .items [segment_index ];
110+ return segment .offset + atom .offset ;
111+ }
112+
111113/// Resolves the relocations within the atom, writing the new value
112114/// at the calculated offset.
113115pub fn resolveRelocs (atom : * Atom , wasm_bin : * const Wasm ) void {
@@ -159,7 +161,7 @@ pub fn resolveRelocs(atom: *Atom, wasm_bin: *const Wasm) void {
159161/// The final value must be casted to the correct size.
160162fn relocationValue (atom : Atom , relocation : types.Relocation , wasm_bin : * const Wasm ) u64 {
161163 const target_loc = (Wasm.SymbolLoc { .file = atom .file , .index = relocation .index }).finalLoc (wasm_bin );
162- const symbol = target_loc .getSymbol (wasm_bin ).* ;
164+ const symbol = target_loc .getSymbol (wasm_bin );
163165 switch (relocation .relocation_type ) {
164166 .R_WASM_FUNCTION_INDEX_LEB = > return symbol .index ,
165167 .R_WASM_TABLE_NUMBER_LEB = > return symbol .index ,
@@ -190,17 +192,9 @@ fn relocationValue(atom: Atom, relocation: types.Relocation, wasm_bin: *const Wa
190192 if (symbol .isUndefined ()) {
191193 return 0 ;
192194 }
193-
194- const merge_segment = wasm_bin .base .options .output_mode != .Obj ;
195195 const target_atom = wasm_bin .symbol_atom .get (target_loc ).? ;
196- const segment_info = if (target_atom .file ) | object_index | blk : {
197- break :blk wasm_bin .objects .items [object_index ].segment_info ;
198- } else wasm_bin .segment_info .values ();
199- const segment_name = segment_info [symbol .index ].outputName (merge_segment );
200- const segment_index = wasm_bin .data_segments .get (segment_name ).? ;
201- const segment = wasm_bin .segments .items [segment_index ];
202- const rel_value = @intCast (i32 , target_atom .offset + segment .offset ) + relocation .addend ;
203- return @intCast (u32 , rel_value );
196+ const va = @intCast (i32 , target_atom .getVA (wasm_bin , symbol ));
197+ return @intCast (u32 , va + relocation .addend );
204198 },
205199 .R_WASM_EVENT_INDEX_LEB = > return symbol .index ,
206200 .R_WASM_SECTION_OFFSET_I32 = > {
0 commit comments