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
12081208 }
12091209 }
12101210
1211 try self.initOutputSections();
12111212 try self.addLinkerDefinedSymbols();
12121213 self.claimUnresolved();
12131214
12141215 // Scan and create missing synthetic entries such as GOT indirection.
12151216 try self.scanRelocs();
12161217
1217 // Generate and emit non-incremental sections.
1218 try self.initSections();
1218 // Generate and emit synthetic sections.
1219 try self.initSyntheticSections();
12191220 try self.initSpecialPhdrs();
12201221 try self.sortShdrs();
12211222 for (self.objects.items) |index| {
......@@ -3210,21 +3211,18 @@ fn addLinkerDefinedSymbols(self: *Elf) !void {
32103211 self.rela_iplt_start_index = try linker_defined.addGlobal("__rela_iplt_start", self);
32113212 self.rela_iplt_end_index = try linker_defined.addGlobal("__rela_iplt_end", self);
32123213
3213 for (self.objects.items) |index| {
3214 const object = self.file(index).?.object;
3215 for (object.atoms.items) |atom_index| {
3216 if (self.getStartStopBasename(atom_index)) |name| {
3217 const gpa = self.base.allocator;
3218 try self.start_stop_indexes.ensureUnusedCapacity(gpa, 2);
3219
3220 const start = try std.fmt.allocPrintZ(gpa, "__start_{s}", .{name});
3221 defer gpa.free(start);
3222 const stop = try std.fmt.allocPrintZ(gpa, "__stop_{s}", .{name});
3223 defer gpa.free(stop);
3224
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 }
3214 for (self.shdrs.items) |shdr| {
3215 if (self.getStartStopBasename(shdr)) |name| {
3216 const gpa = self.base.allocator;
3217 try self.start_stop_indexes.ensureUnusedCapacity(gpa, 2);
3218
3219 const start = try std.fmt.allocPrintZ(gpa, "__start_{s}", .{name});
3220 defer gpa.free(start);
3221 const stop = try std.fmt.allocPrintZ(gpa, "__stop_{s}", .{name});
3222 defer gpa.free(stop);
3223
3224 self.start_stop_indexes.appendAssumeCapacity(try linker_defined.addGlobal(start, self));
3225 self.start_stop_indexes.appendAssumeCapacity(try linker_defined.addGlobal(stop, self));
32283226 }
32293227 }
32303228
......@@ -3354,12 +3352,14 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
33543352 }
33553353}
33563354
3357fn initSections(self: *Elf) !void {
3358 const ptr_size = self.ptrWidthBytes();
3359
3355fn initOutputSections(self: *Elf) !void {
33603356 for (self.objects.items) |index| {
33613357 try self.file(index).?.object.initOutputSections(self);
33623358 }
3359}
3360
3361fn initSyntheticSections(self: *Elf) !void {
3362 const ptr_size = self.ptrWidthBytes();
33633363
33643364 const needs_eh_frame = for (self.objects.items) |index| {
33653365 if (self.file(index).?.object.cies.items.len > 0) break true;
......@@ -3394,6 +3394,14 @@ fn initSections(self: *Elf) !void {
33943394 });
33953395 }
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
33973405 const needs_rela_dyn = blk: {
33983406 if (self.got.flags.needs_rela or self.got.flags.needs_tlsld or
33993407 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 {
34243432 .addralign = 16,
34253433 .offset = std.math.maxInt(u64),
34263434 });
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 });
34343435 self.rela_plt_section_index = try self.addSection(.{
34353436 .name = ".rela.plt",
34363437 .type = elf.SHT_RELA,
......@@ -4819,15 +4820,12 @@ fn updateSymtabSize(self: *Elf) !void {
48194820 const gpa = self.base.allocator;
48204821 var files = std.ArrayList(File.Index).init(gpa);
48214822 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
48244825 if (self.zig_object_index) |index| files.appendAssumeCapacity(index);
4825 for (self.objects.items) |index| {
4826 files.appendAssumeCapacity(index);
4827 }
4828 for (self.shared_objects.items) |index| {
4829 files.appendAssumeCapacity(index);
4830 }
4826 for (self.objects.items) |index| files.appendAssumeCapacity(index);
4827 for (self.shared_objects.items) |index| files.appendAssumeCapacity(index);
4828 if (self.linker_defined_index) |index| files.appendAssumeCapacity(index);
48314829
48324830 // Section symbols
48334831 for (self.output_sections.keys()) |_| {
......@@ -5166,6 +5164,11 @@ fn writeSymtab(self: *Elf) !void {
51665164 file_ptr.writeSymtab(self);
51675165 }
51685166
5167 if (self.linker_defined_index) |index| {
5168 const file_ptr = self.file(index).?;
5169 file_ptr.writeSymtab(self);
5170 }
5171
51695172 if (self.zig_got_section_index) |_| {
51705173 self.zig_got.writeSymtab(self);
51715174 }
......@@ -5182,11 +5185,6 @@ fn writeSymtab(self: *Elf) !void {
51825185 self.plt_got.writeSymtab(self);
51835186 }
51845187
5185 if (self.linker_defined_index) |index| {
5186 const file_ptr = self.file(index).?;
5187 file_ptr.writeSymtab(self);
5188 }
5189
51905188 const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian();
51915189 switch (self.ptr_width) {
51925190 .p32 => {
......@@ -5750,10 +5748,9 @@ pub fn isCIdentifier(name: []const u8) bool {
57505748 return true;
57515749}
57525750
5753fn getStartStopBasename(self: *Elf, atom_index: Atom.Index) ?[]const u8 {
5754 const atom_ptr = self.atom(atom_index) orelse return null;
5755 const name = atom_ptr.name(self);
5756 if (atom_ptr.inputShdr(self).sh_flags & elf.SHF_ALLOC != 0 and name.len > 0) {
5751fn getStartStopBasename(self: *Elf, shdr: elf.Elf64_Shdr) ?[]const u8 {
5752 const name = self.getShString(shdr.sh_name);
5753 if (shdr.sh_flags & elf.SHF_ALLOC != 0 and name.len > 0) {
57575754 if (isCIdentifier(name)) return name;
57585755 }
57595756 return null;
src/link/Elf/LinkerDefined.zig+33-1
......@@ -48,10 +48,42 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {
4848 }
4949}
5050
51pub fn globals(self: *LinkerDefined) []const Symbol.Index {
51pub fn globals(self: LinkerDefined) []const Symbol.Index {
5252 return self.symbols.items;
5353}
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
5587pub fn asFile(self: *LinkerDefined) File {
5688 return .{ .linker_defined = self };
5789}
src/link/Elf/Object.zig+57
......@@ -741,6 +741,63 @@ pub fn writeAr(self: Object, writer: anytype) !void {
741741 try writer.writeAll(self.data);
742742}
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
744801pub fn locals(self: Object) []const Symbol.Index {
745802 if (self.symbols.items.len == 0) return &[0]Symbol.Index{};
746803 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 {
191191 return self.symbols.items;
192192}
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
194222pub fn shdrContents(self: SharedObject, index: u16) []const u8 {
195223 const shdr = self.shdrs.items[index];
196224 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 {
528528 return self.global_symbols.items;
529529}
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
531588pub fn asFile(self: *ZigObject) File {
532589 return .{ .zig_object = self };
533590}
src/link/Elf/file.zig+5-53
......@@ -128,63 +128,15 @@ pub const File = union(enum) {
128128 }
129129
130130 pub fn updateSymtabSize(file: File, elf_file: *Elf) !void {
131 const output_symtab_ctx = switch (file) {
132 inline else => |x| &x.output_symtab_ctx,
131 return switch (file) {
132 inline else => |x| x.updateSymtabSize(elf_file),
133133 };
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 }
163134 }
164135
165136 pub fn writeSymtab(file: File, elf_file: *Elf) void {
166 for (file.locals()) |local_index| {
167 const local = elf_file.symbol(local_index);
168 const idx = local.outputSymtabIndex(elf_file) orelse continue;
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 }
137 return switch (file) {
138 inline else => |x| x.writeSymtab(elf_file),
139 };
188140 }
189141
190142 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 {
917917 }
918918
919919 pub fn writeSymtab(plt: PltSection, elf_file: *Elf) void {
920 var ilocal = plt.output_symtab_ctx.ilocal;
921 for (plt.symbols.items) |sym_index| {
920 for (plt.symbols.items, plt.output_symtab_ctx.ilocal..) |sym_index, ilocal| {
922921 const sym = elf_file.symbol(sym_index);
923922 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
924923 elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file));
......@@ -932,7 +931,6 @@ pub const PltSection = struct {
932931 .st_value = sym.pltAddress(elf_file),
933932 .st_size = 16,
934933 };
935 ilocal += 1;
936934 }
937935 }
938936
......@@ -1046,8 +1044,7 @@ pub const PltGotSection = struct {
10461044 }
10471045
10481046 pub fn writeSymtab(plt_got: PltGotSection, elf_file: *Elf) void {
1049 var ilocal = plt_got.output_symtab_ctx.ilocal;
1050 for (plt_got.symbols.items) |sym_index| {
1047 for (plt_got.symbols.items, plt_got.output_symtab_ctx.ilocal..) |sym_index, ilocal| {
10511048 const sym = elf_file.symbol(sym_index);
10521049 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
10531050 elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file));
......@@ -1061,7 +1058,6 @@ pub const PltGotSection = struct {
10611058 .st_value = sym.pltGotAddress(elf_file),
10621059 .st_size = 16,
10631060 };
1064 ilocal += 1;
10651061 }
10661062 }
10671063};