authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-10 22:46:59+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-11 07:59:54+02:00
logd07edfabd6e45a486d1611e357887d01b38db32e
treef65dfcc372dddb4ccaf649ca4d22f582f7f5bcf2
parent4b082d89c9cd6e40259701f21344d55eef2a8d2e

elf: simplify handling of relocs for atoms


4 files changed, 82 insertions(+), 75 deletions(-)

src/link/Elf.zig+7-49
......@@ -135,10 +135,6 @@ last_atom_and_free_list_table: std.AutoArrayHashMapUnmanaged(u16, LastAtomAndFre
135135/// with `Decl` `main`, and lives as long as that `Decl`.
136136unnamed_consts: UnnamedConstTable = .{},
137137
138/// A table of relocations indexed by the owning them `TextBlock`.
139relocs: RelocTable = .{},
140
141const RelocTable = std.AutoHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Atom.Reloc));
142138const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Symbol.Index));
143139const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, LazySymbolMetadata);
144140
......@@ -293,14 +289,6 @@ pub fn deinit(self: *Elf) void {
293289 self.unnamed_consts.deinit(gpa);
294290 }
295291
296 {
297 var it = self.relocs.valueIterator();
298 while (it.next()) |relocs| {
299 relocs.deinit(gpa);
300 }
301 self.relocs.deinit(gpa);
302 }
303
304292 if (self.dwarf) |*dw| {
305293 dw.deinit();
306294 }
......@@ -312,12 +300,11 @@ pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: link.
312300 const this_sym_index = try self.getOrCreateMetadataForDecl(decl_index);
313301 const this_sym = self.symbol(this_sym_index);
314302 const vaddr = this_sym.value;
315 const parent_atom_index = self.symbol(reloc_info.parent_atom_index).atom_index;
316 try Atom.addRelocation(self, parent_atom_index, .{
303 const parent_atom = self.symbol(reloc_info.parent_atom_index).atom(self).?;
304 try parent_atom.addReloc(self, .{
317305 .target = this_sym_index,
318306 .offset = reloc_info.offset,
319307 .addend = reloc_info.addend,
320 .prev_vaddr = vaddr,
321308 });
322309
323310 return vaddr;
......@@ -1032,38 +1019,9 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10321019
10331020 // Beyond this point, everything has been allocated a virtual address and we can resolve
10341021 // the relocations.
1035 {
1036 var it = self.relocs.iterator();
1037 while (it.next()) |entry| {
1038 const atom_index = entry.key_ptr.*;
1039 const relocs = entry.value_ptr.*;
1040 const atom_ptr = self.atom(atom_index).?;
1041 const source_shdr = &self.shdrs.items[atom_ptr.output_section_index];
1042
1043 log.debug("relocating '{s}'", .{atom_ptr.name(self)});
1044
1045 for (relocs.items) |*reloc| {
1046 const target_sym = self.symbol(reloc.target);
1047 const target_vaddr = target_sym.value + reloc.addend;
1048
1049 if (target_vaddr == reloc.prev_vaddr) continue;
1050
1051 const section_offset = (atom_ptr.value + reloc.offset) - source_shdr.sh_addr;
1052 const file_offset = source_shdr.sh_offset + section_offset;
1053
1054 log.debug(" ({x}: [() => 0x{x}] ({s}))", .{
1055 reloc.offset,
1056 target_vaddr,
1057 target_sym.name(self),
1058 });
1059
1060 switch (self.ptr_width) {
1061 .p32 => try self.base.file.?.pwriteAll(mem.asBytes(&@as(u32, @intCast(target_vaddr))), file_offset),
1062 .p64 => try self.base.file.?.pwriteAll(mem.asBytes(&target_vaddr), file_offset),
1063 }
1064
1065 reloc.prev_vaddr = target_vaddr;
1066 }
1022 if (self.zig_module_index) |index| {
1023 for (self.file(index).?.zig_module.atoms.items) |atom_index| {
1024 try self.atom(atom_index).?.resolveRelocs(self);
10671025 }
10681026 }
10691027
......@@ -2313,7 +2271,7 @@ pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: A
23132271
23142272 const sym_index = try self.getOrCreateMetadataForDecl(decl_index);
23152273 self.freeUnnamedConsts(decl_index);
2316 Atom.freeRelocations(self, self.symbol(sym_index).atom_index);
2274 self.symbol(sym_index).atom(self).?.freeRelocs(self);
23172275
23182276 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
23192277 defer code_buffer.deinit();
......@@ -2378,7 +2336,7 @@ pub fn updateDecl(
23782336 }
23792337
23802338 const sym_index = try self.getOrCreateMetadataForDecl(decl_index);
2381 Atom.freeRelocations(self, self.symbol(sym_index).atom_index);
2339 self.symbol(sym_index).atom(self).?.freeRelocs(self);
23822340
23832341 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
23842342 defer code_buffer.deinit();
src/link/Elf/Atom.zig+56-25
......@@ -14,13 +14,13 @@ size: u64 = 0,
1414alignment: u8 = 0,
1515
1616/// Index of the input section.
17input_section_index: u16 = 0,
17input_section_index: Index = 0,
1818
1919/// Index of the output section.
2020output_section_index: u16 = 0,
2121
2222/// Index of the input section containing this atom's relocs.
23relocs_section_index: u16 = 0,
23relocs_section_index: Index = 0,
2424
2525/// Index of this atom in the linker's atoms table.
2626atom_index: Index = 0,
......@@ -64,20 +64,6 @@ pub fn freeListEligible(self: Atom, elf_file: *Elf) bool {
6464 return surplus >= Elf.min_text_capacity;
6565}
6666
67pub fn addRelocation(elf_file: *Elf, atom_index: Index, reloc: Reloc) !void {
68 const gpa = elf_file.base.allocator;
69 const gop = try elf_file.relocs.getOrPut(gpa, atom_index);
70 if (!gop.found_existing) {
71 gop.value_ptr.* = .{};
72 }
73 try gop.value_ptr.append(gpa, reloc);
74}
75
76pub fn freeRelocations(elf_file: *Elf, atom_index: Index) void {
77 var removed_relocs = elf_file.relocs.fetchRemove(atom_index);
78 if (removed_relocs) |*relocs| relocs.value.deinit(elf_file.base.allocator);
79}
80
8167pub fn allocate(self: *Atom, elf_file: *Elf) !void {
8268 const shdr = &elf_file.shdrs.items[self.output_section_index];
8369 const meta = elf_file.last_atom_and_free_list_table.getPtr(self.output_section_index).?;
......@@ -205,8 +191,6 @@ pub fn grow(self: *Atom, elf_file: *Elf) !void {
205191pub fn free(self: *Atom, elf_file: *Elf) void {
206192 log.debug("freeAtom {d} ({s})", .{ self.atom_index, self.name(elf_file) });
207193
208 Atom.freeRelocations(elf_file, self.atom_index);
209
210194 const gpa = elf_file.base.allocator;
211195 const shndx = self.output_section_index;
212196 const meta = elf_file.last_atom_and_free_list_table.getPtr(shndx).?;
......@@ -256,23 +240,70 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
256240 self.next_index = 0;
257241 }
258242
243 // TODO create relocs free list
244 self.freeRelocs(elf_file);
259245 self.* = .{};
260246}
261247
262pub const Index = u32;
248pub fn relocs(self: Atom, elf_file: *Elf) []const Relocation {
249 const file_ptr = elf_file.file(self.file_index).?;
250 if (file_ptr != .zig_module) @panic("TODO");
251 const zig_module = file_ptr.zig_module;
252 return zig_module.relocs.items[self.relocs_section_index].items;
253}
254
255pub fn addReloc(self: Atom, elf_file: *Elf, reloc: Relocation) !void {
256 const gpa = elf_file.base.allocator;
257 const file_ptr = elf_file.file(self.file_index).?;
258 assert(file_ptr == .zig_module);
259 const zig_module = file_ptr.zig_module;
260 const rels = &zig_module.relocs.items[self.relocs_section_index];
261 try rels.append(gpa, reloc);
262}
263
264pub fn freeRelocs(self: Atom, elf_file: *Elf) void {
265 const file_ptr = elf_file.file(self.file_index).?;
266 assert(file_ptr == .zig_module);
267 const zig_module = file_ptr.zig_module;
268 zig_module.relocs.items[self.relocs_section_index].clearRetainingCapacity();
269}
270
271/// TODO mark relocs dirty
272pub fn resolveRelocs(self: Atom, elf_file: *Elf) !void {
273 relocs_log.debug("0x{x}: {s}", .{ self.value, self.name(elf_file) });
274 const shdr = &elf_file.shdrs.items[self.output_section_index];
275 for (self.relocs(elf_file)) |reloc| {
276 const target_sym = elf_file.symbol(reloc.target);
277 const target_vaddr = target_sym.value + reloc.addend;
278 const section_offset = (self.value + reloc.offset) - shdr.sh_addr;
279 const file_offset = shdr.sh_offset + section_offset;
280
281 relocs_log.debug(" ({x}: [() => 0x{x}] ({s}))", .{
282 reloc.offset,
283 target_vaddr,
284 target_sym.name(elf_file),
285 });
286
287 switch (elf_file.ptr_width) {
288 .p32 => try elf_file.base.file.?.pwriteAll(
289 std.mem.asBytes(&@as(u32, @intCast(target_vaddr))),
290 file_offset,
291 ),
292 .p64 => try elf_file.base.file.?.pwriteAll(std.mem.asBytes(&target_vaddr), file_offset),
293 }
294 }
295}
263296
264pub const Reloc = struct {
265 target: u32,
266 offset: u64,
267 addend: u32,
268 prev_vaddr: u64,
269};
297pub const Index = u32;
270298
271299const std = @import("std");
272300const assert = std.debug.assert;
273301const elf = std.elf;
274302const log = std.log.scoped(.link);
303const relocs_log = std.log.scoped(.link_relocs);
275304
305const Allocator = std.mem.Allocator;
276306const Atom = @This();
277307const Elf = @import("../Elf.zig");
278308const File = @import("file.zig").File;
309const Relocation = @import("Relocation.zig");
src/link/Elf/Relocation.zig created+8
......@@ -0,0 +1,8 @@
1target: Symbol.Index,
2offset: u64,
3addend: u32,
4
5const std = @import("std");
6
7const Symbol = @import("Symbol.zig");
8const Relocation = @This();
src/link/Elf/ZigModule.zig+11-1
......@@ -9,6 +9,7 @@ elf_global_symbols: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
99global_symbols: std.AutoArrayHashMapUnmanaged(Symbol.Index, void) = .{},
1010
1111atoms: std.ArrayListUnmanaged(Atom.Index) = .{},
12relocs: std.ArrayListUnmanaged(std.ArrayListUnmanaged(Relocation)) = .{},
1213
1314alive: bool = true,
1415
......@@ -20,6 +21,10 @@ pub fn deinit(self: *ZigModule, allocator: Allocator) void {
2021 self.elf_global_symbols.deinit(allocator);
2122 self.global_symbols.deinit(allocator);
2223 self.atoms.deinit(allocator);
24 for (self.relocs.items) |*list| {
25 list.deinit(allocator);
26 }
27 self.relocs.deinit(allocator);
2328}
2429
2530pub fn createAtom(self: *ZigModule, output_section_index: u16, elf_file: *Elf) !Symbol.Index {
......@@ -34,6 +39,10 @@ pub fn createAtom(self: *ZigModule, output_section_index: u16, elf_file: *Elf) !
3439 symbol_ptr.output_section_index = output_section_index;
3540 const local_esym = symbol_ptr.sourceSymbol(elf_file);
3641 local_esym.st_shndx = output_section_index;
42 const relocs_index = @as(Atom.Index, @intCast(self.relocs.items.len));
43 const relocs = try self.relocs.addOne(gpa);
44 relocs.* = .{};
45 atom_ptr.relocs_section_index = relocs_index;
3746 try self.atoms.append(gpa, atom_index);
3847 return symbol_index;
3948}
......@@ -184,5 +193,6 @@ const Atom = @import("Atom.zig");
184193const Elf = @import("../Elf.zig");
185194const File = @import("file.zig").File;
186195const Module = @import("../../Module.zig");
187const ZigModule = @This();
196const Relocation = @import("Relocation.zig");
188197const Symbol = @import("Symbol.zig");
198const ZigModule = @This();