authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-16 01:18:22+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-11-16 01:18:22+01:00
log359842f8d5a0ee2641c07c1e659d06553d6269fc
tree5041d6b759152e2021cd4bedeaa1af4a123d4dfa
parent0c6cb8d8c80f0a316a77eddfebaa42cb77c8bf7d
parent6e4d7362cedd9570bb32868d868877d6ba39c156
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #18010 from ziglang/elf-symtab-fixes

elf: actually write synthetic globals to output symtab and other misc fixes

7 files changed, 222 insertions(+), 103 deletions(-)

src/link/Elf.zig+40-43
...@@ -1208,14 +1208,15 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1208,14 +1208,15 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1208 }1208 }
1209 }1209 }
12101210
1211 try self.initOutputSections();
1211 try self.addLinkerDefinedSymbols();1212 try self.addLinkerDefinedSymbols();
1212 self.claimUnresolved();1213 self.claimUnresolved();
12131214
1214 // Scan and create missing synthetic entries such as GOT indirection.1215 // Scan and create missing synthetic entries such as GOT indirection.
1215 try self.scanRelocs();1216 try self.scanRelocs();
12161217
1217 // Generate and emit non-incremental sections.1218 // Generate and emit synthetic sections.
1218 try self.initSections();1219 try self.initSyntheticSections();
1219 try self.initSpecialPhdrs();1220 try self.initSpecialPhdrs();
1220 try self.sortShdrs();1221 try self.sortShdrs();
1221 for (self.objects.items) |index| {1222 for (self.objects.items) |index| {
...@@ -3210,21 +3211,18 @@ fn addLinkerDefinedSymbols(self: *Elf) !void {...@@ -3210,21 +3211,18 @@ fn addLinkerDefinedSymbols(self: *Elf) !void {
3210 self.rela_iplt_start_index = try linker_defined.addGlobal("__rela_iplt_start", self);3211 self.rela_iplt_start_index = try linker_defined.addGlobal("__rela_iplt_start", self);
3211 self.rela_iplt_end_index = try linker_defined.addGlobal("__rela_iplt_end", self);3212 self.rela_iplt_end_index = try linker_defined.addGlobal("__rela_iplt_end", self);
32123213
3213 for (self.objects.items) |index| {3214 for (self.shdrs.items) |shdr| {
3214 const object = self.file(index).?.object;3215 if (self.getStartStopBasename(shdr)) |name| {
3215 for (object.atoms.items) |atom_index| {3216 const gpa = self.base.allocator;
3216 if (self.getStartStopBasename(atom_index)) |name| {3217 try self.start_stop_indexes.ensureUnusedCapacity(gpa, 2);
3217 const gpa = self.base.allocator;3218
3218 try self.start_stop_indexes.ensureUnusedCapacity(gpa, 2);3219 const start = try std.fmt.allocPrintZ(gpa, "__start_{s}", .{name});
32193220 defer gpa.free(start);
3220 const start = try std.fmt.allocPrintZ(gpa, "__start_{s}", .{name});3221 const stop = try std.fmt.allocPrintZ(gpa, "__stop_{s}", .{name});
3221 defer gpa.free(start);3222 defer gpa.free(stop);
3222 const stop = try std.fmt.allocPrintZ(gpa, "__stop_{s}", .{name});3223
3223 defer gpa.free(stop);3224 self.start_stop_indexes.appendAssumeCapacity(try linker_defined.addGlobal(start, self));
32243225 self.start_stop_indexes.appendAssumeCapacity(try linker_defined.addGlobal(stop, self));
3225 self.start_stop_indexes.appendAssumeCapacity(try linker_defined.addGlobal(start, self));
3226 self.start_stop_indexes.appendAssumeCapacity(try linker_defined.addGlobal(stop, self));
3227 }
3228 }3226 }
3229 }3227 }
32303228
...@@ -3354,12 +3352,14 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {...@@ -3354,12 +3352,14 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
3354 }3352 }
3355}3353}
33563354
3357fn initSections(self: *Elf) !void {3355fn initOutputSections(self: *Elf) !void {
3358 const ptr_size = self.ptrWidthBytes();
3359
3360 for (self.objects.items) |index| {3356 for (self.objects.items) |index| {
3361 try self.file(index).?.object.initOutputSections(self);3357 try self.file(index).?.object.initOutputSections(self);
3362 }3358 }
3359}
3360
3361fn initSyntheticSections(self: *Elf) !void {
3362 const ptr_size = self.ptrWidthBytes();
33633363
3364 const needs_eh_frame = for (self.objects.items) |index| {3364 const needs_eh_frame = for (self.objects.items) |index| {
3365 if (self.file(index).?.object.cies.items.len > 0) break true;3365 if (self.file(index).?.object.cies.items.len > 0) break true;
...@@ -3394,6 +3394,14 @@ fn initSections(self: *Elf) !void {...@@ -3394,6 +3394,14 @@ fn initSections(self: *Elf) !void {
3394 });3394 });
3395 }3395 }
33963396
3397 self.got_plt_section_index = try self.addSection(.{
3398 .name = ".got.plt",
3399 .type = elf.SHT_PROGBITS,
3400 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
3401 .addralign = @alignOf(u64),
3402 .offset = std.math.maxInt(u64),
3403 });
3404
3397 const needs_rela_dyn = blk: {3405 const needs_rela_dyn = blk: {
3398 if (self.got.flags.needs_rela or self.got.flags.needs_tlsld or3406 if (self.got.flags.needs_rela or self.got.flags.needs_tlsld or
3399 self.zig_got.flags.needs_rela or self.copy_rel.symbols.items.len > 0) break :blk true;3407 self.zig_got.flags.needs_rela or self.copy_rel.symbols.items.len > 0) break :blk true;
...@@ -3424,13 +3432,6 @@ fn initSections(self: *Elf) !void {...@@ -3424,13 +3432,6 @@ fn initSections(self: *Elf) !void {
3424 .addralign = 16,3432 .addralign = 16,
3425 .offset = std.math.maxInt(u64),3433 .offset = std.math.maxInt(u64),
3426 });3434 });
3427 self.got_plt_section_index = try self.addSection(.{
3428 .name = ".got.plt",
3429 .type = elf.SHT_PROGBITS,
3430 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
3431 .addralign = @alignOf(u64),
3432 .offset = std.math.maxInt(u64),
3433 });
3434 self.rela_plt_section_index = try self.addSection(.{3435 self.rela_plt_section_index = try self.addSection(.{
3435 .name = ".rela.plt",3436 .name = ".rela.plt",
3436 .type = elf.SHT_RELA,3437 .type = elf.SHT_RELA,
...@@ -4819,15 +4820,12 @@ fn updateSymtabSize(self: *Elf) !void {...@@ -4819,15 +4820,12 @@ fn updateSymtabSize(self: *Elf) !void {
4819 const gpa = self.base.allocator;4820 const gpa = self.base.allocator;
4820 var files = std.ArrayList(File.Index).init(gpa);4821 var files = std.ArrayList(File.Index).init(gpa);
4821 defer files.deinit();4822 defer files.deinit();
4822 try files.ensureTotalCapacityPrecise(self.objects.items.len + self.shared_objects.items.len + 1);4823 try files.ensureTotalCapacityPrecise(self.objects.items.len + self.shared_objects.items.len + 2);
48234824
4824 if (self.zig_object_index) |index| files.appendAssumeCapacity(index);4825 if (self.zig_object_index) |index| files.appendAssumeCapacity(index);
4825 for (self.objects.items) |index| {4826 for (self.objects.items) |index| files.appendAssumeCapacity(index);
4826 files.appendAssumeCapacity(index);4827 for (self.shared_objects.items) |index| files.appendAssumeCapacity(index);
4827 }4828 if (self.linker_defined_index) |index| files.appendAssumeCapacity(index);
4828 for (self.shared_objects.items) |index| {
4829 files.appendAssumeCapacity(index);
4830 }
48314829
4832 // Section symbols4830 // Section symbols
4833 for (self.output_sections.keys()) |_| {4831 for (self.output_sections.keys()) |_| {
...@@ -5166,6 +5164,11 @@ fn writeSymtab(self: *Elf) !void {...@@ -5166,6 +5164,11 @@ fn writeSymtab(self: *Elf) !void {
5166 file_ptr.writeSymtab(self);5164 file_ptr.writeSymtab(self);
5167 }5165 }
51685166
5167 if (self.linker_defined_index) |index| {
5168 const file_ptr = self.file(index).?;
5169 file_ptr.writeSymtab(self);
5170 }
5171
5169 if (self.zig_got_section_index) |_| {5172 if (self.zig_got_section_index) |_| {
5170 self.zig_got.writeSymtab(self);5173 self.zig_got.writeSymtab(self);
5171 }5174 }
...@@ -5182,11 +5185,6 @@ fn writeSymtab(self: *Elf) !void {...@@ -5182,11 +5185,6 @@ fn writeSymtab(self: *Elf) !void {
5182 self.plt_got.writeSymtab(self);5185 self.plt_got.writeSymtab(self);
5183 }5186 }
51845187
5185 if (self.linker_defined_index) |index| {
5186 const file_ptr = self.file(index).?;
5187 file_ptr.writeSymtab(self);
5188 }
5189
5190 const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian();5188 const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian();
5191 switch (self.ptr_width) {5189 switch (self.ptr_width) {
5192 .p32 => {5190 .p32 => {
...@@ -5750,10 +5748,9 @@ pub fn isCIdentifier(name: []const u8) bool {...@@ -5750,10 +5748,9 @@ pub fn isCIdentifier(name: []const u8) bool {
5750 return true;5748 return true;
5751}5749}
57525750
5753fn getStartStopBasename(self: *Elf, atom_index: Atom.Index) ?[]const u8 {5751fn getStartStopBasename(self: *Elf, shdr: elf.Elf64_Shdr) ?[]const u8 {
5754 const atom_ptr = self.atom(atom_index) orelse return null;5752 const name = self.getShString(shdr.sh_name);
5755 const name = atom_ptr.name(self);5753 if (shdr.sh_flags & elf.SHF_ALLOC != 0 and name.len > 0) {
5756 if (atom_ptr.inputShdr(self).sh_flags & elf.SHF_ALLOC != 0 and name.len > 0) {
5757 if (isCIdentifier(name)) return name;5754 if (isCIdentifier(name)) return name;
5758 }5755 }
5759 return null;5756 return null;
src/link/Elf/LinkerDefined.zig+33-1
...@@ -48,10 +48,42 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {...@@ -48,10 +48,42 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {
48 }48 }
49}49}
5050
51pub fn globals(self: *LinkerDefined) []const Symbol.Index {51pub fn globals(self: LinkerDefined) []const Symbol.Index {
52 return self.symbols.items;52 return self.symbols.items;
53}53}
5454
55pub fn updateSymtabSize(self: *LinkerDefined, elf_file: *Elf) !void {
56 for (self.globals()) |global_index| {
57 const global = elf_file.symbol(global_index);
58 const file_ptr = global.file(elf_file) orelse continue;
59 if (file_ptr.index() != self.index) continue;
60 global.flags.output_symtab = true;
61 if (global.isLocal(elf_file)) {
62 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
63 self.output_symtab_ctx.nlocals += 1;
64 } else {
65 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
66 self.output_symtab_ctx.nglobals += 1;
67 }
68 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
69 }
70}
71
72pub fn writeSymtab(self: LinkerDefined, elf_file: *Elf) void {
73 for (self.globals()) |global_index| {
74 const global = elf_file.symbol(global_index);
75 const file_ptr = global.file(elf_file) orelse continue;
76 if (file_ptr.index() != self.index) continue;
77 const idx = global.outputSymtabIndex(elf_file) orelse continue;
78 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
79 elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
80 elf_file.strtab.appendAssumeCapacity(0);
81 const out_sym = &elf_file.symtab.items[idx];
82 out_sym.st_name = st_name;
83 global.setOutputSym(elf_file, out_sym);
84 }
85}
86
55pub fn asFile(self: *LinkerDefined) File {87pub fn asFile(self: *LinkerDefined) File {
56 return .{ .linker_defined = self };88 return .{ .linker_defined = self };
57}89}
src/link/Elf/Object.zig+57
...@@ -741,6 +741,63 @@ pub fn writeAr(self: Object, writer: anytype) !void {...@@ -741,6 +741,63 @@ pub fn writeAr(self: Object, writer: anytype) !void {
741 try writer.writeAll(self.data);741 try writer.writeAll(self.data);
742}742}
743743
744pub fn updateSymtabSize(self: *Object, elf_file: *Elf) !void {
745 for (self.locals()) |local_index| {
746 const local = elf_file.symbol(local_index);
747 if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
748 const esym = local.elfSym(elf_file);
749 switch (esym.st_type()) {
750 elf.STT_SECTION, elf.STT_NOTYPE => continue,
751 else => {},
752 }
753 local.flags.output_symtab = true;
754 try local.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
755 self.output_symtab_ctx.nlocals += 1;
756 self.output_symtab_ctx.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1;
757 }
758
759 for (self.globals()) |global_index| {
760 const global = elf_file.symbol(global_index);
761 const file_ptr = global.file(elf_file) orelse continue;
762 if (file_ptr.index() != self.index) continue;
763 if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
764 global.flags.output_symtab = true;
765 if (global.isLocal(elf_file)) {
766 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
767 self.output_symtab_ctx.nlocals += 1;
768 } else {
769 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
770 self.output_symtab_ctx.nglobals += 1;
771 }
772 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
773 }
774}
775
776pub fn writeSymtab(self: Object, elf_file: *Elf) void {
777 for (self.locals()) |local_index| {
778 const local = elf_file.symbol(local_index);
779 const idx = local.outputSymtabIndex(elf_file) orelse continue;
780 const out_sym = &elf_file.symtab.items[idx];
781 out_sym.st_name = @intCast(elf_file.strtab.items.len);
782 elf_file.strtab.appendSliceAssumeCapacity(local.name(elf_file));
783 elf_file.strtab.appendAssumeCapacity(0);
784 local.setOutputSym(elf_file, out_sym);
785 }
786
787 for (self.globals()) |global_index| {
788 const global = elf_file.symbol(global_index);
789 const file_ptr = global.file(elf_file) orelse continue;
790 if (file_ptr.index() != self.index) continue;
791 const idx = global.outputSymtabIndex(elf_file) orelse continue;
792 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
793 elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
794 elf_file.strtab.appendAssumeCapacity(0);
795 const out_sym = &elf_file.symtab.items[idx];
796 out_sym.st_name = st_name;
797 global.setOutputSym(elf_file, out_sym);
798 }
799}
800
744pub fn locals(self: Object) []const Symbol.Index {801pub fn locals(self: Object) []const Symbol.Index {
745 if (self.symbols.items.len == 0) return &[0]Symbol.Index{};802 if (self.symbols.items.len == 0) return &[0]Symbol.Index{};
746 const end = self.first_global orelse self.symbols.items.len;803 const end = self.first_global orelse self.symbols.items.len;
src/link/Elf/SharedObject.zig+28
...@@ -191,6 +191,34 @@ pub fn globals(self: SharedObject) []const Symbol.Index {...@@ -191,6 +191,34 @@ pub fn globals(self: SharedObject) []const Symbol.Index {
191 return self.symbols.items;191 return self.symbols.items;
192}192}
193193
194pub fn updateSymtabSize(self: *SharedObject, elf_file: *Elf) !void {
195 for (self.globals()) |global_index| {
196 const global = elf_file.symbol(global_index);
197 const file_ptr = global.file(elf_file) orelse continue;
198 if (file_ptr.index() != self.index) continue;
199 if (global.isLocal(elf_file)) continue;
200 global.flags.output_symtab = true;
201 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
202 self.output_symtab_ctx.nglobals += 1;
203 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
204 }
205}
206
207pub fn writeSymtab(self: SharedObject, elf_file: *Elf) void {
208 for (self.globals()) |global_index| {
209 const global = elf_file.symbol(global_index);
210 const file_ptr = global.file(elf_file) orelse continue;
211 if (file_ptr.index() != self.index) continue;
212 const idx = global.outputSymtabIndex(elf_file) orelse continue;
213 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
214 elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
215 elf_file.strtab.appendAssumeCapacity(0);
216 const out_sym = &elf_file.symtab.items[idx];
217 out_sym.st_name = st_name;
218 global.setOutputSym(elf_file, out_sym);
219 }
220}
221
194pub fn shdrContents(self: SharedObject, index: u16) []const u8 {222pub fn shdrContents(self: SharedObject, index: u16) []const u8 {
195 const shdr = self.shdrs.items[index];223 const shdr = self.shdrs.items[index];
196 return self.data[shdr.sh_offset..][0..shdr.sh_size];224 return self.data[shdr.sh_offset..][0..shdr.sh_size];
src/link/Elf/ZigObject.zig+57
...@@ -528,6 +528,63 @@ pub fn globals(self: ZigObject) []const Symbol.Index {...@@ -528,6 +528,63 @@ pub fn globals(self: ZigObject) []const Symbol.Index {
528 return self.global_symbols.items;528 return self.global_symbols.items;
529}529}
530530
531pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {
532 for (self.locals()) |local_index| {
533 const local = elf_file.symbol(local_index);
534 if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
535 const esym = local.elfSym(elf_file);
536 switch (esym.st_type()) {
537 elf.STT_SECTION, elf.STT_NOTYPE => continue,
538 else => {},
539 }
540 local.flags.output_symtab = true;
541 try local.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
542 self.output_symtab_ctx.nlocals += 1;
543 self.output_symtab_ctx.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1;
544 }
545
546 for (self.globals()) |global_index| {
547 const global = elf_file.symbol(global_index);
548 const file_ptr = global.file(elf_file) orelse continue;
549 if (file_ptr.index() != self.index) continue;
550 if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
551 global.flags.output_symtab = true;
552 if (global.isLocal(elf_file)) {
553 try global.setOutputSymtabIndex(self.output_symtab_ctx.nlocals, elf_file);
554 self.output_symtab_ctx.nlocals += 1;
555 } else {
556 try global.setOutputSymtabIndex(self.output_symtab_ctx.nglobals, elf_file);
557 self.output_symtab_ctx.nglobals += 1;
558 }
559 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
560 }
561}
562
563pub fn writeSymtab(self: ZigObject, elf_file: *Elf) void {
564 for (self.locals()) |local_index| {
565 const local = elf_file.symbol(local_index);
566 const idx = local.outputSymtabIndex(elf_file) orelse continue;
567 const out_sym = &elf_file.symtab.items[idx];
568 out_sym.st_name = @intCast(elf_file.strtab.items.len);
569 elf_file.strtab.appendSliceAssumeCapacity(local.name(elf_file));
570 elf_file.strtab.appendAssumeCapacity(0);
571 local.setOutputSym(elf_file, out_sym);
572 }
573
574 for (self.globals()) |global_index| {
575 const global = elf_file.symbol(global_index);
576 const file_ptr = global.file(elf_file) orelse continue;
577 if (file_ptr.index() != self.index) continue;
578 const idx = global.outputSymtabIndex(elf_file) orelse continue;
579 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
580 elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
581 elf_file.strtab.appendAssumeCapacity(0);
582 const out_sym = &elf_file.symtab.items[idx];
583 out_sym.st_name = st_name;
584 global.setOutputSym(elf_file, out_sym);
585 }
586}
587
531pub fn asFile(self: *ZigObject) File {588pub fn asFile(self: *ZigObject) File {
532 return .{ .zig_object = self };589 return .{ .zig_object = self };
533}590}
src/link/Elf/file.zig+5-53
...@@ -128,63 +128,15 @@ pub const File = union(enum) {...@@ -128,63 +128,15 @@ pub const File = union(enum) {
128 }128 }
129129
130 pub fn updateSymtabSize(file: File, elf_file: *Elf) !void {130 pub fn updateSymtabSize(file: File, elf_file: *Elf) !void {
131 const output_symtab_ctx = switch (file) {131 return switch (file) {
132 inline else => |x| &x.output_symtab_ctx,132 inline else => |x| x.updateSymtabSize(elf_file),
133 };133 };
134 for (file.locals()) |local_index| {
135 const local = elf_file.symbol(local_index);
136 if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
137 const esym = local.elfSym(elf_file);
138 switch (esym.st_type()) {
139 elf.STT_SECTION, elf.STT_NOTYPE => continue,
140 else => {},
141 }
142 local.flags.output_symtab = true;
143 try local.setOutputSymtabIndex(output_symtab_ctx.nlocals, elf_file);
144 output_symtab_ctx.nlocals += 1;
145 output_symtab_ctx.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1;
146 }
147
148 for (file.globals()) |global_index| {
149 const global = elf_file.symbol(global_index);
150 const file_ptr = global.file(elf_file) orelse continue;
151 if (file_ptr.index() != file.index()) continue;
152 if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
153 global.flags.output_symtab = true;
154 if (global.isLocal(elf_file)) {
155 try global.setOutputSymtabIndex(output_symtab_ctx.nlocals, elf_file);
156 output_symtab_ctx.nlocals += 1;
157 } else {
158 try global.setOutputSymtabIndex(output_symtab_ctx.nglobals, elf_file);
159 output_symtab_ctx.nglobals += 1;
160 }
161 output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
162 }
163 }134 }
164135
165 pub fn writeSymtab(file: File, elf_file: *Elf) void {136 pub fn writeSymtab(file: File, elf_file: *Elf) void {
166 for (file.locals()) |local_index| {137 return switch (file) {
167 const local = elf_file.symbol(local_index);138 inline else => |x| x.writeSymtab(elf_file),
168 const idx = local.outputSymtabIndex(elf_file) orelse continue;139 };
169 const out_sym = &elf_file.symtab.items[idx];
170 out_sym.st_name = @intCast(elf_file.strtab.items.len);
171 elf_file.strtab.appendSliceAssumeCapacity(local.name(elf_file));
172 elf_file.strtab.appendAssumeCapacity(0);
173 local.setOutputSym(elf_file, out_sym);
174 }
175
176 for (file.globals()) |global_index| {
177 const global = elf_file.symbol(global_index);
178 const file_ptr = global.file(elf_file) orelse continue;
179 if (file_ptr.index() != file.index()) continue;
180 const idx = global.outputSymtabIndex(elf_file) orelse continue;
181 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
182 elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
183 elf_file.strtab.appendAssumeCapacity(0);
184 const out_sym = &elf_file.symtab.items[idx];
185 out_sym.st_name = st_name;
186 global.setOutputSym(elf_file, out_sym);
187 }
188 }140 }
189141
190 pub fn updateArSymtab(file: File, ar_symtab: *Archive.ArSymtab, elf_file: *Elf) !void {142 pub fn updateArSymtab(file: File, ar_symtab: *Archive.ArSymtab, elf_file: *Elf) !void {
src/link/Elf/synthetic_sections.zig+2-6
...@@ -917,8 +917,7 @@ pub const PltSection = struct {...@@ -917,8 +917,7 @@ pub const PltSection = struct {
917 }917 }
918918
919 pub fn writeSymtab(plt: PltSection, elf_file: *Elf) void {919 pub fn writeSymtab(plt: PltSection, elf_file: *Elf) void {
920 var ilocal = plt.output_symtab_ctx.ilocal;920 for (plt.symbols.items, plt.output_symtab_ctx.ilocal..) |sym_index, ilocal| {
921 for (plt.symbols.items) |sym_index| {
922 const sym = elf_file.symbol(sym_index);921 const sym = elf_file.symbol(sym_index);
923 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));922 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
924 elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file));923 elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file));
...@@ -932,7 +931,6 @@ pub const PltSection = struct {...@@ -932,7 +931,6 @@ pub const PltSection = struct {
932 .st_value = sym.pltAddress(elf_file),931 .st_value = sym.pltAddress(elf_file),
933 .st_size = 16,932 .st_size = 16,
934 };933 };
935 ilocal += 1;
936 }934 }
937 }935 }
938936
...@@ -1046,8 +1044,7 @@ pub const PltGotSection = struct {...@@ -1046,8 +1044,7 @@ pub const PltGotSection = struct {
1046 }1044 }
10471045
1048 pub fn writeSymtab(plt_got: PltGotSection, elf_file: *Elf) void {1046 pub fn writeSymtab(plt_got: PltGotSection, elf_file: *Elf) void {
1049 var ilocal = plt_got.output_symtab_ctx.ilocal;1047 for (plt_got.symbols.items, plt_got.output_symtab_ctx.ilocal..) |sym_index, ilocal| {
1050 for (plt_got.symbols.items) |sym_index| {
1051 const sym = elf_file.symbol(sym_index);1048 const sym = elf_file.symbol(sym_index);
1052 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));1049 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
1053 elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file));1050 elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file));
...@@ -1061,7 +1058,6 @@ pub const PltGotSection = struct {...@@ -1061,7 +1058,6 @@ pub const PltGotSection = struct {
1061 .st_value = sym.pltGotAddress(elf_file),1058 .st_value = sym.pltGotAddress(elf_file),
1062 .st_size = 16,1059 .st_size = 16,
1063 };1060 };
1064 ilocal += 1;
1065 }1061 }
1066 }1062 }
1067};1063};