Commit e8d008a8a8

Jakub Konka <kubkon@jakubkonka.com>
2024-07-26 14:49:15
elf: atom is always assigned output section index
1 parent 96c20ad
src/link/Elf/Atom.zig
@@ -48,8 +48,7 @@ pub fn name(self: Atom, elf_file: *Elf) []const u8 {
 }
 
 pub fn address(self: Atom, elf_file: *Elf) i64 {
-    const shndx = self.outputShndx() orelse return self.value;
-    const shdr = elf_file.shdrs.items[shndx];
+    const shdr = elf_file.shdrs.items[self.output_section_index];
     return @as(i64, @intCast(shdr.sh_addr)) + self.value;
 }
 
@@ -87,11 +86,6 @@ pub fn relocsShndx(self: Atom) ?u32 {
     return self.relocs_section_index;
 }
 
-pub fn outputShndx(self: Atom) ?u32 {
-    if (self.output_section_index == 0) return null;
-    return self.output_section_index;
-}
-
 pub fn priority(self: Atom, elf_file: *Elf) u64 {
     const index = self.file(elf_file).?.index();
     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 {
 
 pub fn allocate(self: *Atom, elf_file: *Elf) !void {
     const zo = elf_file.zigObjectPtr().?;
-    const shdr = &elf_file.shdrs.items[self.outputShndx().?];
-    const meta = elf_file.last_atom_and_free_list_table.getPtr(self.outputShndx().?).?;
+    const shdr = &elf_file.shdrs.items[self.output_section_index];
+    const meta = elf_file.last_atom_and_free_list_table.getPtr(self.output_section_index).?;
     const free_list = &meta.free_list;
     const last_atom_index = &meta.last_atom_index;
     const new_atom_ideal_capacity = Elf.padToIdeal(self.size);
@@ -199,7 +193,7 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {
         true;
     if (expand_section) {
         const needed_size: u64 = @intCast(self.value + @as(i64, @intCast(self.size)));
-        try elf_file.growAllocSection(self.outputShndx().?, needed_size);
+        try elf_file.growAllocSection(self.output_section_index, needed_size);
         last_atom_index.* = self.atom_index;
 
         const zig_object = elf_file.zigObjectPtr().?;
@@ -258,7 +252,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
     const zo = elf_file.zigObjectPtr().?;
     const comp = elf_file.base.comp;
     const gpa = comp.gpa;
-    const shndx = self.outputShndx().?;
+    const shndx = self.output_section_index;
     const meta = elf_file.last_atom_and_free_list_table.getPtr(shndx).?;
     const free_list = &meta.free_list;
     const last_atom_index = &meta.last_atom_index;
src/link/Elf/Object.zig
@@ -1036,12 +1036,12 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {
             break :blk self.initOutputSection(elf_file, shdr) catch unreachable;
         };
         const shdr = &elf_file.shdrs.items[shndx];
-        shdr.sh_info = atom_ptr.outputShndx().?;
+        shdr.sh_info = atom_ptr.output_section_index;
         shdr.sh_link = elf_file.symtab_section_index.?;
 
         const comp = elf_file.base.comp;
         const gpa = comp.gpa;
-        const gop = try elf_file.output_rela_sections.getOrPut(gpa, atom_ptr.outputShndx().?);
+        const gop = try elf_file.output_rela_sections.getOrPut(gpa, atom_ptr.output_section_index);
         if (!gop.found_existing) gop.value_ptr.* = .{ .shndx = shndx };
         try gop.value_ptr.atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
     }
src/link/Elf/synthetic_sections.zig
@@ -1701,13 +1701,13 @@ pub const ComdatGroupSection = struct {
                 elf.SHT_RELA => {
                     const atom_index = object.atoms_indexes.items[shdr.sh_info];
                     const atom = object.atom(atom_index).?;
-                    const rela = elf_file.output_rela_sections.get(atom.outputShndx().?).?;
+                    const rela = elf_file.output_rela_sections.get(atom.output_section_index).?;
                     try writer.writeInt(u32, rela.shndx, .little);
                 },
                 else => {
                     const atom_index = object.atoms_indexes.items[shndx];
                     const atom = object.atom(atom_index).?;
-                    try writer.writeInt(u32, atom.outputShndx().?, .little);
+                    try writer.writeInt(u32, atom.output_section_index, .little);
                 },
             }
         }
src/link/Elf/ZigObject.zig
@@ -311,7 +311,7 @@ pub fn newAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index {
 /// TODO actually create fake input shdrs and return that instead.
 pub fn inputShdr(self: *ZigObject, atom_index: Atom.Index, elf_file: *Elf) elf.Elf64_Shdr {
     const atom_ptr = self.atom(atom_index) orelse return Elf.null_shdr;
-    const shndx = atom_ptr.outputShndx() orelse return Elf.null_shdr;
+    const shndx = atom_ptr.output_section_index;
     var shdr = elf_file.shdrs.items[shndx];
     shdr.sh_addr = 0;
     shdr.sh_offset = 0;
@@ -342,7 +342,7 @@ pub fn resolveSymbols(self: *ZigObject, elf_file: *Elf) void {
                 else => unreachable,
             };
             const output_section_index = if (self.atom(atom_index)) |atom_ptr|
-                atom_ptr.outputShndx().?
+                atom_ptr.output_section_index
             else
                 elf.SHN_UNDEF;
             global.value = @intCast(esym.st_value);
@@ -523,7 +523,7 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
         const rela_shndx = atom_ptr.relocsShndx() orelse continue;
         // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level
         if (self.relocs.items[rela_shndx].items.len == 0) continue;
-        const out_shndx = atom_ptr.outputShndx().?;
+        const out_shndx = atom_ptr.output_section_index;
         const out_shdr = elf_file.shdrs.items[out_shndx];
         if (out_shdr.sh_type == elf.SHT_NOBITS) continue;
 
@@ -623,7 +623,7 @@ pub fn asFile(self: *ZigObject) File {
 pub fn codeAlloc(self: *ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8 {
     const gpa = elf_file.base.comp.gpa;
     const atom_ptr = self.atom(atom_index).?;
-    const shdr = &elf_file.shdrs.items[atom_ptr.outputShndx().?];
+    const shdr = &elf_file.shdrs.items[atom_ptr.output_section_index];
 
     if (shdr.sh_flags & elf.SHF_TLS != 0) {
         const tlv = self.tls_variables.get(atom_index).?;
src/link/Elf.zig
@@ -1372,7 +1372,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
         for (zo.atoms_indexes.items) |atom_index| {
             const atom_ptr = zo.atom(atom_index) orelse continue;
             if (!atom_ptr.alive) continue;
-            const out_shndx = atom_ptr.outputShndx() orelse continue;
+            const out_shndx = atom_ptr.output_section_index;
             const shdr = &self.shdrs.items[out_shndx];
             if (shdr.sh_type == elf.SHT_NOBITS) continue;
             const code = try zo.codeAlloc(self, atom_index);