authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-15 22:46:52+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-20 23:36:41+02:00
log09820a96b691f79403291d9b2179adbae50fc33b
treeaac8065372bc1e5eb0e786e5e0f15e5a14a8a73d
parent13b403cbf728454ba4d57565485051a89bb70230

link/elf: move relocs indexes into Atom extras


2 files changed, 19 insertions(+), 13 deletions(-)

src/link/Elf/Atom.zig+15-10
...@@ -22,12 +22,6 @@ output_section_index: u32 = 0,...@@ -22,12 +22,6 @@ output_section_index: u32 = 0,
22/// Index of the input section containing this atom's relocs.22/// Index of the input section containing this atom's relocs.
23relocs_section_index: u32 = 0,23relocs_section_index: u32 = 0,
2424
25/// Start index of the relocations belonging to this atom.
26rel_index: u32 = 0,
27
28/// Number of relocations belonging to this atom.
29rel_num: u32 = 0,
30
31/// Index of this atom in the linker's atoms table.25/// Index of this atom in the linker's atoms table.
32atom_index: Index = 0,26atom_index: Index = 0,
3327
...@@ -304,11 +298,14 @@ pub fn free(self: *Atom, elf_file: *Elf) void {...@@ -304,11 +298,14 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
304298
305pub fn relocs(self: Atom, elf_file: *Elf) []const elf.Elf64_Rela {299pub fn relocs(self: Atom, elf_file: *Elf) []const elf.Elf64_Rela {
306 const shndx = self.relocsShndx() orelse return &[0]elf.Elf64_Rela{};300 const shndx = self.relocsShndx() orelse return &[0]elf.Elf64_Rela{};
307 return switch (self.file(elf_file).?) {301 switch (self.file(elf_file).?) {
308 .zig_object => |x| x.relocs.items[shndx].items,302 .zig_object => |x| return x.relocs.items[shndx].items,
309 .object => |x| x.relocs.items[self.rel_index..][0..self.rel_num],303 .object => |x| {
304 const extras = self.extra(elf_file).?;
305 return x.relocs.items[extras.rel_index..][0..extras.rel_count];
306 },
310 else => unreachable,307 else => unreachable,
311 };308 }
312}309}
313310
314pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.Elf64_Rela)) !void {311pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.Elf64_Rela)) !void {
...@@ -981,6 +978,8 @@ const AddExtraOpts = struct {...@@ -981,6 +978,8 @@ const AddExtraOpts = struct {
981 thunk: ?u32 = null,978 thunk: ?u32 = null,
982 fde_start: ?u32 = null,979 fde_start: ?u32 = null,
983 fde_count: ?u32 = null,980 fde_count: ?u32 = null,
981 rel_index: ?u32 = null,
982 rel_count: ?u32 = null,
984};983};
985984
986pub fn addExtra(atom: *Atom, opts: AddExtraOpts, elf_file: *Elf) !void {985pub fn addExtra(atom: *Atom, opts: AddExtraOpts, elf_file: *Elf) !void {
...@@ -2206,6 +2205,12 @@ pub const Extra = struct {...@@ -2206,6 +2205,12 @@ pub const Extra = struct {
22062205
2207 /// Count of FDEs referencing this atom.2206 /// Count of FDEs referencing this atom.
2208 fde_count: u32 = 0,2207 fde_count: u32 = 0,
2208
2209 /// Start index of relocations belonging to this atom.
2210 rel_index: u32 = 0,
2211
2212 /// Count of relocations belonging to this atom.
2213 rel_count: u32 = 0,
2209};2214};
22102215
2211const std = @import("std");2216const std = @import("std");
src/link/Elf/Object.zig+4-3
...@@ -242,11 +242,12 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file:...@@ -242,11 +242,12 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file:
242 const relocs = try self.preadRelocsAlloc(allocator, handle, @intCast(i));242 const relocs = try self.preadRelocsAlloc(allocator, handle, @intCast(i));
243 defer allocator.free(relocs);243 defer allocator.free(relocs);
244 atom.relocs_section_index = @intCast(i);244 atom.relocs_section_index = @intCast(i);
245 atom.rel_index = @intCast(self.relocs.items.len);245 const rel_index: u32 = @intCast(self.relocs.items.len);
246 atom.rel_num = @intCast(relocs.len);246 const rel_count: u32 = @intCast(relocs.len);
247 try atom.addExtra(.{ .rel_index = rel_index, .rel_count = rel_count }, elf_file);
247 try self.relocs.appendUnalignedSlice(allocator, relocs);248 try self.relocs.appendUnalignedSlice(allocator, relocs);
248 if (elf_file.getTarget().cpu.arch == .riscv64) {249 if (elf_file.getTarget().cpu.arch == .riscv64) {
249 sortRelocs(self.relocs.items[atom.rel_index..][0..atom.rel_num]);250 sortRelocs(self.relocs.items[rel_index..][0..rel_count]);
250 }251 }
251 }252 }
252 },253 },