Commit 45e46f0fb9

Jakub Konka <kubkon@jakubkonka.com>
2024-09-01 15:13:09
elf: allocate atom chunks using allocateChunk mechanics in objects
1 parent 1ef96f0
Changed files (3)
src/link/Elf/Object.zig
@@ -955,27 +955,26 @@ pub fn initOutputSections(self: *Object, elf_file: *Elf) !void {
 }
 
 pub fn allocateAtoms(self: *Object, elf_file: *Elf) !void {
-    _ = elf_file;
     for (self.section_chunks.items) |*chunk| {
         chunk.updateSize(self);
     }
-}
 
-pub fn addAtomsToOutputSections(self: *Object, elf_file: *Elf) !void {
-    for (self.atoms_indexes.items) |atom_index| {
-        const atom_ptr = self.atom(atom_index) orelse continue;
-        if (!atom_ptr.alive) continue;
-        const shdr = atom_ptr.inputShdr(elf_file);
-        atom_ptr.output_section_index = elf_file.initOutputSection(.{
-            .name = self.getString(shdr.sh_name),
-            .flags = shdr.sh_flags,
-            .type = shdr.sh_type,
-        }) catch unreachable;
+    for (self.section_chunks.items) |*chunk| {
+        const alloc_res = try elf_file.allocateChunk(chunk.output_section_index, chunk.size, chunk.alignment);
+        chunk.value = @intCast(alloc_res.value);
 
-        const comp = elf_file.base.comp;
-        const gpa = comp.gpa;
-        const atom_list = &elf_file.sections.items(.atom_list)[atom_ptr.output_section_index];
-        try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
+        const slice = elf_file.sections.slice();
+        const shdr = &slice.items(.shdr)[chunk.output_section_index];
+        const last_atom_ref = &slice.items(.last_atom)[chunk.output_section_index];
+
+        const expand_section = if (elf_file.atom(alloc_res.placement)) |placement_atom|
+            placement_atom.nextAtom(elf_file) == null
+        else
+            true;
+        if (expand_section) last_atom_ref.* = chunk.lastAtom(self).ref();
+        shdr.sh_addralign = @max(shdr.sh_addralign, chunk.alignment.toByteUnits().?);
+
+        // TODO create back and forward links
     }
 }
 
@@ -1599,6 +1598,16 @@ const SectionChunk = struct {
         }
     }
 
+    fn firstAtom(chunk: SectionChunk, object: *Object) *Atom {
+        assert(chunk.atoms.items.len > 0);
+        return object.atom(chunk.atoms.items[0]).?;
+    }
+
+    fn lastAtom(chunk: SectionChunk, object: *Object) *Atom {
+        assert(chunk.atoms.items.len > 0);
+        return object.atom(chunk.atoms.items[chunk.atoms.items.len - 1]).?;
+    }
+
     pub fn format(
         chunk: SectionChunk,
         comptime unused_fmt_string: []const u8,
src/link/Elf/relocatable.zig
@@ -208,7 +208,6 @@ pub fn flushObject(elf_file: *Elf, comp: *Compilation, module_obj_path: ?[]const
     }
     for (elf_file.objects.items) |index| {
         const object = elf_file.file(index).?.object;
-        try object.addAtomsToOutputSections(elf_file);
         try object.addAtomsToRelaSections(elf_file);
     }
     try elf_file.updateMergeSectionSizes();
src/link/Elf.zig
@@ -727,7 +727,10 @@ pub fn allocateChunk(self: *Elf, shndx: u32, size: u64, alignment: Atom.Alignmen
         true;
     if (expand_section) {
         const needed_size = res.value + size;
-        try self.growAllocSection(shndx, needed_size);
+        if (shdr.sh_flags & elf.SHF_ALLOC != 0)
+            try self.growAllocSection(shndx, needed_size)
+        else
+            try self.growNonAllocSection(shndx, needed_size, @intCast(alignment.toByteUnits().?), true);
     }
 
     return res;
@@ -1036,9 +1039,6 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
     try self.setHashSections();
     try self.setVersionSymtab();
 
-    for (self.objects.items) |index| {
-        try self.file(index).?.object.addAtomsToOutputSections(self);
-    }
     try self.sortInitFini();
     try self.updateMergeSectionSizes();
     try self.updateSectionSizes();
@@ -3560,29 +3560,29 @@ fn updateSectionSizes(self: *Elf) !void {
     }
 
     const slice = self.sections.slice();
-    for (slice.items(.shdr), slice.items(.atom_list)) |*shdr, atom_list| {
-        if (atom_list.items.len == 0) continue;
-        if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;
-        for (atom_list.items) |ref| {
-            const atom_ptr = self.atom(ref) orelse continue;
-            if (!atom_ptr.alive) continue;
-            const offset = atom_ptr.alignment.forward(shdr.sh_size);
-            const padding = offset - shdr.sh_size;
-            atom_ptr.value = @intCast(offset);
-            shdr.sh_size += padding + atom_ptr.size;
-            shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);
-        }
-    }
-
-    if (self.requiresThunks()) {
-        for (slice.items(.shdr), slice.items(.atom_list), 0..) |*shdr, atom_list, shndx| {
-            if (shdr.sh_flags & elf.SHF_EXECINSTR == 0) continue;
-            if (atom_list.items.len == 0) continue;
-
-            // Create jump/branch range extenders if needed.
-            try self.createThunks(shdr, @intCast(shndx));
-        }
-    }
+    // for (slice.items(.shdr), slice.items(.atom_list)) |*shdr, atom_list| {
+    //     if (atom_list.items.len == 0) continue;
+    //     if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;
+    //     for (atom_list.items) |ref| {
+    //         const atom_ptr = self.atom(ref) orelse continue;
+    //         if (!atom_ptr.alive) continue;
+    //         const offset = atom_ptr.alignment.forward(shdr.sh_size);
+    //         const padding = offset - shdr.sh_size;
+    //         atom_ptr.value = @intCast(offset);
+    //         shdr.sh_size += padding + atom_ptr.size;
+    //         shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits() orelse 1);
+    //     }
+    // }
+
+    // if (self.requiresThunks()) {
+    //     for (slice.items(.shdr), slice.items(.atom_list), 0..) |*shdr, atom_list, shndx| {
+    //         if (shdr.sh_flags & elf.SHF_EXECINSTR == 0) continue;
+    //         if (atom_list.items.len == 0) continue;
+
+    //         // Create jump/branch range extenders if needed.
+    //         try self.createThunks(shdr, @intCast(shndx));
+    //     }
+    // }
 
     const shdrs = slice.items(.shdr);
     if (self.eh_frame_section_index) |index| {