Commit 760ce69734

Jakub Konka <kubkon@jakubkonka.com>
2023-11-15 15:07:09
elf: actually write synthetic globals to output symtab
1 parent 0c6cb8d
Changed files (2)
src/link/Elf/synthetic_sections.zig
@@ -917,8 +917,7 @@ pub const PltSection = struct {
     }
 
     pub fn writeSymtab(plt: PltSection, elf_file: *Elf) void {
-        var ilocal = plt.output_symtab_ctx.ilocal;
-        for (plt.symbols.items) |sym_index| {
+        for (plt.symbols.items, plt.output_symtab_ctx.ilocal..) |sym_index, ilocal| {
             const sym = elf_file.symbol(sym_index);
             const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
             elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file));
@@ -932,7 +931,6 @@ pub const PltSection = struct {
                 .st_value = sym.pltAddress(elf_file),
                 .st_size = 16,
             };
-            ilocal += 1;
         }
     }
 
@@ -1046,8 +1044,7 @@ pub const PltGotSection = struct {
     }
 
     pub fn writeSymtab(plt_got: PltGotSection, elf_file: *Elf) void {
-        var ilocal = plt_got.output_symtab_ctx.ilocal;
-        for (plt_got.symbols.items) |sym_index| {
+        for (plt_got.symbols.items, plt_got.output_symtab_ctx.ilocal..) |sym_index, ilocal| {
             const sym = elf_file.symbol(sym_index);
             const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
             elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file));
@@ -1061,7 +1058,6 @@ pub const PltGotSection = struct {
                 .st_value = sym.pltGotAddress(elf_file),
                 .st_size = 16,
             };
-            ilocal += 1;
         }
     }
 };
src/link/Elf.zig
@@ -4819,15 +4819,12 @@ fn updateSymtabSize(self: *Elf) !void {
     const gpa = self.base.allocator;
     var files = std.ArrayList(File.Index).init(gpa);
     defer files.deinit();
-    try files.ensureTotalCapacityPrecise(self.objects.items.len + self.shared_objects.items.len + 1);
+    try files.ensureTotalCapacityPrecise(self.objects.items.len + self.shared_objects.items.len + 2);
 
     if (self.zig_object_index) |index| files.appendAssumeCapacity(index);
-    for (self.objects.items) |index| {
-        files.appendAssumeCapacity(index);
-    }
-    for (self.shared_objects.items) |index| {
-        files.appendAssumeCapacity(index);
-    }
+    for (self.objects.items) |index| files.appendAssumeCapacity(index);
+    for (self.shared_objects.items) |index| files.appendAssumeCapacity(index);
+    if (self.linker_defined_index) |index| files.appendAssumeCapacity(index);
 
     // Section symbols
     for (self.output_sections.keys()) |_| {
@@ -5166,6 +5163,11 @@ fn writeSymtab(self: *Elf) !void {
         file_ptr.writeSymtab(self);
     }
 
+    if (self.linker_defined_index) |index| {
+        const file_ptr = self.file(index).?;
+        file_ptr.writeSymtab(self);
+    }
+
     if (self.zig_got_section_index) |_| {
         self.zig_got.writeSymtab(self);
     }
@@ -5182,11 +5184,6 @@ fn writeSymtab(self: *Elf) !void {
         self.plt_got.writeSymtab(self);
     }
 
-    if (self.linker_defined_index) |index| {
-        const file_ptr = self.file(index).?;
-        file_ptr.writeSymtab(self);
-    }
-
     const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian();
     switch (self.ptr_width) {
         .p32 => {