authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-26 14:49:15+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-30 10:00:50+02:00
loge8d008a8a83f10b8400691bef216147e08ac2daf
treedb9aae30efd8882a1a0890262a2f169d20eb70fd
parent96c20adeee84dbd86a7036cce49ef0c58870bdce

elf: atom is always assigned output section index


5 files changed, 14 insertions(+), 20 deletions(-)

src/link/Elf.zig+1-1
...@@ -1372,7 +1372,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod...@@ -1372,7 +1372,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
1372 for (zo.atoms_indexes.items) |atom_index| {1372 for (zo.atoms_indexes.items) |atom_index| {
1373 const atom_ptr = zo.atom(atom_index) orelse continue;1373 const atom_ptr = zo.atom(atom_index) orelse continue;
1374 if (!atom_ptr.alive) continue;1374 if (!atom_ptr.alive) continue;
1375 const out_shndx = atom_ptr.outputShndx() orelse continue;1375 const out_shndx = atom_ptr.output_section_index;
1376 const shdr = &self.shdrs.items[out_shndx];1376 const shdr = &self.shdrs.items[out_shndx];
1377 if (shdr.sh_type == elf.SHT_NOBITS) continue;1377 if (shdr.sh_type == elf.SHT_NOBITS) continue;
1378 const code = try zo.codeAlloc(self, atom_index);1378 const code = try zo.codeAlloc(self, atom_index);
src/link/Elf/Atom.zig+5-11
...@@ -48,8 +48,7 @@ pub fn name(self: Atom, elf_file: *Elf) []const u8 {...@@ -48,8 +48,7 @@ pub fn name(self: Atom, elf_file: *Elf) []const u8 {
48}48}
4949
50pub fn address(self: Atom, elf_file: *Elf) i64 {50pub fn address(self: Atom, elf_file: *Elf) i64 {
51 const shndx = self.outputShndx() orelse return self.value;51 const shdr = elf_file.shdrs.items[self.output_section_index];
52 const shdr = elf_file.shdrs.items[shndx];
53 return @as(i64, @intCast(shdr.sh_addr)) + self.value;52 return @as(i64, @intCast(shdr.sh_addr)) + self.value;
54}53}
5554
...@@ -87,11 +86,6 @@ pub fn relocsShndx(self: Atom) ?u32 {...@@ -87,11 +86,6 @@ pub fn relocsShndx(self: Atom) ?u32 {
87 return self.relocs_section_index;86 return self.relocs_section_index;
88}87}
8988
90pub fn outputShndx(self: Atom) ?u32 {
91 if (self.output_section_index == 0) return null;
92 return self.output_section_index;
93}
94
95pub fn priority(self: Atom, elf_file: *Elf) u64 {89pub fn priority(self: Atom, elf_file: *Elf) u64 {
96 const index = self.file(elf_file).?.index();90 const index = self.file(elf_file).?.index();
97 return (@as(u64, @intCast(index)) << 32) | @as(u64, @intCast(self.input_section_index));91 return (@as(u64, @intCast(index)) << 32) | @as(u64, @intCast(self.input_section_index));
...@@ -122,8 +116,8 @@ pub fn freeListEligible(self: Atom, elf_file: *Elf) bool {...@@ -122,8 +116,8 @@ pub fn freeListEligible(self: Atom, elf_file: *Elf) bool {
122116
123pub fn allocate(self: *Atom, elf_file: *Elf) !void {117pub fn allocate(self: *Atom, elf_file: *Elf) !void {
124 const zo = elf_file.zigObjectPtr().?;118 const zo = elf_file.zigObjectPtr().?;
125 const shdr = &elf_file.shdrs.items[self.outputShndx().?];119 const shdr = &elf_file.shdrs.items[self.output_section_index];
126 const meta = elf_file.last_atom_and_free_list_table.getPtr(self.outputShndx().?).?;120 const meta = elf_file.last_atom_and_free_list_table.getPtr(self.output_section_index).?;
127 const free_list = &meta.free_list;121 const free_list = &meta.free_list;
128 const last_atom_index = &meta.last_atom_index;122 const last_atom_index = &meta.last_atom_index;
129 const new_atom_ideal_capacity = Elf.padToIdeal(self.size);123 const new_atom_ideal_capacity = Elf.padToIdeal(self.size);
...@@ -199,7 +193,7 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {...@@ -199,7 +193,7 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {
199 true;193 true;
200 if (expand_section) {194 if (expand_section) {
201 const needed_size: u64 = @intCast(self.value + @as(i64, @intCast(self.size)));195 const needed_size: u64 = @intCast(self.value + @as(i64, @intCast(self.size)));
202 try elf_file.growAllocSection(self.outputShndx().?, needed_size);196 try elf_file.growAllocSection(self.output_section_index, needed_size);
203 last_atom_index.* = self.atom_index;197 last_atom_index.* = self.atom_index;
204198
205 const zig_object = elf_file.zigObjectPtr().?;199 const zig_object = elf_file.zigObjectPtr().?;
...@@ -258,7 +252,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void {...@@ -258,7 +252,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
258 const zo = elf_file.zigObjectPtr().?;252 const zo = elf_file.zigObjectPtr().?;
259 const comp = elf_file.base.comp;253 const comp = elf_file.base.comp;
260 const gpa = comp.gpa;254 const gpa = comp.gpa;
261 const shndx = self.outputShndx().?;255 const shndx = self.output_section_index;
262 const meta = elf_file.last_atom_and_free_list_table.getPtr(shndx).?;256 const meta = elf_file.last_atom_and_free_list_table.getPtr(shndx).?;
263 const free_list = &meta.free_list;257 const free_list = &meta.free_list;
264 const last_atom_index = &meta.last_atom_index;258 const last_atom_index = &meta.last_atom_index;
src/link/Elf/Object.zig+2-2
...@@ -1036,12 +1036,12 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {...@@ -1036,12 +1036,12 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {
1036 break :blk self.initOutputSection(elf_file, shdr) catch unreachable;1036 break :blk self.initOutputSection(elf_file, shdr) catch unreachable;
1037 };1037 };
1038 const shdr = &elf_file.shdrs.items[shndx];1038 const shdr = &elf_file.shdrs.items[shndx];
1039 shdr.sh_info = atom_ptr.outputShndx().?;1039 shdr.sh_info = atom_ptr.output_section_index;
1040 shdr.sh_link = elf_file.symtab_section_index.?;1040 shdr.sh_link = elf_file.symtab_section_index.?;
10411041
1042 const comp = elf_file.base.comp;1042 const comp = elf_file.base.comp;
1043 const gpa = comp.gpa;1043 const gpa = comp.gpa;
1044 const gop = try elf_file.output_rela_sections.getOrPut(gpa, atom_ptr.outputShndx().?);1044 const gop = try elf_file.output_rela_sections.getOrPut(gpa, atom_ptr.output_section_index);
1045 if (!gop.found_existing) gop.value_ptr.* = .{ .shndx = shndx };1045 if (!gop.found_existing) gop.value_ptr.* = .{ .shndx = shndx };
1046 try gop.value_ptr.atom_list.append(gpa, .{ .index = atom_index, .file = self.index });1046 try gop.value_ptr.atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
1047 }1047 }
src/link/Elf/ZigObject.zig+4-4
...@@ -311,7 +311,7 @@ pub fn newAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index {...@@ -311,7 +311,7 @@ pub fn newAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index {
311/// TODO actually create fake input shdrs and return that instead.311/// TODO actually create fake input shdrs and return that instead.
312pub fn inputShdr(self: *ZigObject, atom_index: Atom.Index, elf_file: *Elf) elf.Elf64_Shdr {312pub fn inputShdr(self: *ZigObject, atom_index: Atom.Index, elf_file: *Elf) elf.Elf64_Shdr {
313 const atom_ptr = self.atom(atom_index) orelse return Elf.null_shdr;313 const atom_ptr = self.atom(atom_index) orelse return Elf.null_shdr;
314 const shndx = atom_ptr.outputShndx() orelse return Elf.null_shdr;314 const shndx = atom_ptr.output_section_index;
315 var shdr = elf_file.shdrs.items[shndx];315 var shdr = elf_file.shdrs.items[shndx];
316 shdr.sh_addr = 0;316 shdr.sh_addr = 0;
317 shdr.sh_offset = 0;317 shdr.sh_offset = 0;
...@@ -342,7 +342,7 @@ pub fn resolveSymbols(self: *ZigObject, elf_file: *Elf) void {...@@ -342,7 +342,7 @@ pub fn resolveSymbols(self: *ZigObject, elf_file: *Elf) void {
342 else => unreachable,342 else => unreachable,
343 };343 };
344 const output_section_index = if (self.atom(atom_index)) |atom_ptr|344 const output_section_index = if (self.atom(atom_index)) |atom_ptr|
345 atom_ptr.outputShndx().?345 atom_ptr.output_section_index
346 else346 else
347 elf.SHN_UNDEF;347 elf.SHN_UNDEF;
348 global.value = @intCast(esym.st_value);348 global.value = @intCast(esym.st_value);
...@@ -523,7 +523,7 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {...@@ -523,7 +523,7 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
523 const rela_shndx = atom_ptr.relocsShndx() orelse continue;523 const rela_shndx = atom_ptr.relocsShndx() orelse continue;
524 // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level524 // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level
525 if (self.relocs.items[rela_shndx].items.len == 0) continue;525 if (self.relocs.items[rela_shndx].items.len == 0) continue;
526 const out_shndx = atom_ptr.outputShndx().?;526 const out_shndx = atom_ptr.output_section_index;
527 const out_shdr = elf_file.shdrs.items[out_shndx];527 const out_shdr = elf_file.shdrs.items[out_shndx];
528 if (out_shdr.sh_type == elf.SHT_NOBITS) continue;528 if (out_shdr.sh_type == elf.SHT_NOBITS) continue;
529529
...@@ -623,7 +623,7 @@ pub fn asFile(self: *ZigObject) File {...@@ -623,7 +623,7 @@ pub fn asFile(self: *ZigObject) File {
623pub fn codeAlloc(self: *ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8 {623pub fn codeAlloc(self: *ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8 {
624 const gpa = elf_file.base.comp.gpa;624 const gpa = elf_file.base.comp.gpa;
625 const atom_ptr = self.atom(atom_index).?;625 const atom_ptr = self.atom(atom_index).?;
626 const shdr = &elf_file.shdrs.items[atom_ptr.outputShndx().?];626 const shdr = &elf_file.shdrs.items[atom_ptr.output_section_index];
627627
628 if (shdr.sh_flags & elf.SHF_TLS != 0) {628 if (shdr.sh_flags & elf.SHF_TLS != 0) {
629 const tlv = self.tls_variables.get(atom_index).?;629 const tlv = self.tls_variables.get(atom_index).?;
src/link/Elf/synthetic_sections.zig+2-2
...@@ -1701,13 +1701,13 @@ pub const ComdatGroupSection = struct {...@@ -1701,13 +1701,13 @@ pub const ComdatGroupSection = struct {
1701 elf.SHT_RELA => {1701 elf.SHT_RELA => {
1702 const atom_index = object.atoms_indexes.items[shdr.sh_info];1702 const atom_index = object.atoms_indexes.items[shdr.sh_info];
1703 const atom = object.atom(atom_index).?;1703 const atom = object.atom(atom_index).?;
1704 const rela = elf_file.output_rela_sections.get(atom.outputShndx().?).?;1704 const rela = elf_file.output_rela_sections.get(atom.output_section_index).?;
1705 try writer.writeInt(u32, rela.shndx, .little);1705 try writer.writeInt(u32, rela.shndx, .little);
1706 },1706 },
1707 else => {1707 else => {
1708 const atom_index = object.atoms_indexes.items[shndx];1708 const atom_index = object.atoms_indexes.items[shndx];
1709 const atom = object.atom(atom_index).?;1709 const atom = object.atom(atom_index).?;
1710 try writer.writeInt(u32, atom.outputShndx().?, .little);1710 try writer.writeInt(u32, atom.output_section_index, .little);
1711 },1711 },
1712 }1712 }
1713 }1713 }