authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-02 01:33:06+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-04 09:10:30+01:00
logdbe13200f1b5a20367b9e5ec288a8ec27fa2d505
tree3cb3e4f6b460bb2616134a3f577d30374cc0a3f8
parent8055f687659de87ce0101cb3e459699b4541a8b7

elf: emit STT_SECTION symbols


4 files changed, 54 insertions(+), 21 deletions(-)

src/link/Elf.zig+8-8
......@@ -494,6 +494,7 @@ pub fn initMetadata(self: *Elf) !void {
494494 const ptr_size = self.ptrWidthBytes();
495495 const ptr_bit_width = self.base.options.target.ptrBitWidth();
496496 const is_linux = self.base.options.target.os.tag == .linux;
497 const zig_object = self.zigObjectPtr().?;
497498
498499 const fillSection = struct {
499500 fn fillSection(elf_file: *Elf, shdr: *elf.Elf64_Shdr, size: u64, phndx: ?u16) void {
......@@ -597,6 +598,7 @@ pub fn initMetadata(self: *Elf) !void {
597598 const shdr = &self.shdrs.items[self.zig_text_section_index.?];
598599 fillSection(self, shdr, self.base.options.program_code_size_hint, self.phdr_zig_load_re_index);
599600 if (self.isObject()) {
601 try zig_object.addSectionSymbol(self.zig_text_section_index.?, self);
600602 self.zig_text_rela_section_index = try self.addRelaShdr(
601603 ".rela.text.zig",
602604 self.zig_text_section_index.?,
......@@ -643,6 +645,7 @@ pub fn initMetadata(self: *Elf) !void {
643645 const shdr = &self.shdrs.items[self.zig_data_rel_ro_section_index.?];
644646 fillSection(self, shdr, 1024, self.phdr_zig_load_ro_index);
645647 if (self.isObject()) {
648 try zig_object.addSectionSymbol(self.zig_data_rel_ro_section_index.?, self);
646649 self.zig_data_rel_ro_rela_section_index = try self.addRelaShdr(
647650 ".rela.data.rel.ro.zig",
648651 self.zig_data_rel_ro_section_index.?,
......@@ -668,6 +671,7 @@ pub fn initMetadata(self: *Elf) !void {
668671 const shdr = &self.shdrs.items[self.zig_data_section_index.?];
669672 fillSection(self, shdr, 1024, self.phdr_zig_load_rw_index);
670673 if (self.isObject()) {
674 try zig_object.addSectionSymbol(self.zig_data_section_index.?, self);
671675 self.zig_data_rela_section_index = try self.addRelaShdr(
672676 ".rela.data.zig",
673677 self.zig_data_section_index.?,
......@@ -697,12 +701,12 @@ pub fn initMetadata(self: *Elf) !void {
697701 shdr.sh_size = phdr.p_memsz;
698702 try self.phdr_to_shdr_table.putNoClobber(gpa, self.zig_bss_section_index.?, phndx);
699703 } else {
704 try zig_object.addSectionSymbol(self.zig_bss_section_index.?, self);
700705 shdr.sh_size = 1024;
701706 }
702707 try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_bss_section_index.?, .{});
703708 }
704709
705 const zig_object = self.zigObjectPtr().?;
706710 if (zig_object.dwarf) |*dw| {
707711 if (self.debug_str_section_index == null) {
708712 assert(dw.strtab.buffer.items.len == 0);
......@@ -3801,23 +3805,19 @@ fn sortShdrs(self: *Elf) !void {
38013805 if (self.zigObjectPtr()) |zig_object| {
38023806 for (zig_object.atoms.items) |atom_index| {
38033807 const atom_ptr = self.atom(atom_index) orelse continue;
3804 if (!atom_ptr.flags.alive) continue;
3805 const out_shndx = atom_ptr.outputShndx() orelse continue;
3806 atom_ptr.output_section_index = backlinks[out_shndx];
3808 atom_ptr.output_section_index = backlinks[atom_ptr.output_section_index];
38073809 }
38083810
38093811 for (zig_object.locals()) |local_index| {
38103812 const local = self.symbol(local_index);
3811 const atom_ptr = local.atom(self) orelse continue;
3812 if (!atom_ptr.flags.alive) continue;
3813 const out_shndx = local.outputShndx() orelse continue;
3814 local.output_section_index = backlinks[out_shndx];
3813 local.output_section_index = backlinks[local.output_section_index];
38153814 }
38163815
38173816 for (zig_object.globals()) |global_index| {
38183817 const global = self.symbol(global_index);
38193818 const atom_ptr = global.atom(self) orelse continue;
38203819 if (!atom_ptr.flags.alive) continue;
3820 // TODO claim unresolved for objects
38213821 if (global.file(self).?.index() != zig_object.index) continue;
38223822 const out_shndx = global.outputShndx() orelse continue;
38233823 global.output_section_index = backlinks[out_shndx];
src/link/Elf/Symbol.zig+2
......@@ -223,6 +223,8 @@ pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void {
223223 const st_shndx = blk: {
224224 if (symbol.flags.has_copy_rel) break :blk elf_file.copy_rel_section_index.?;
225225 if (file_ptr == .shared_object or esym.st_shndx == elf.SHN_UNDEF) break :blk elf.SHN_UNDEF;
226 // TODO I think this is wrong and obsolete
227 if (elf_file.isObject() and st_type == elf.STT_SECTION) break :blk symbol.outputShndx().?;
226228 if (symbol.atom(elf_file) == null and file_ptr != .linker_defined)
227229 break :blk elf.SHN_ABS;
228230 break :blk symbol.outputShndx() orelse elf.SHN_UNDEF;
src/link/Elf/ZigObject.zig+42-12
......@@ -87,7 +87,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf) !void {
8787 const esym_index = try self.addLocalEsym(gpa);
8888 const esym = &self.local_esyms.items(.elf_sym)[esym_index];
8989 esym.st_name = name_off;
90 esym.st_info |= elf.STT_FILE;
90 esym.st_info = elf.STT_FILE;
9191 esym.st_shndx = elf.SHN_ABS;
9292 symbol_ptr.esym_index = esym_index;
9393
......@@ -284,6 +284,22 @@ pub fn addAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index {
284284 return symbol_index;
285285}
286286
287pub fn addSectionSymbol(self: *ZigObject, shndx: u16, elf_file: *Elf) !void {
288 assert(elf_file.isObject());
289 const gpa = elf_file.base.allocator;
290 const symbol_index = try elf_file.addSymbol();
291 try self.local_symbols.append(gpa, symbol_index);
292 const symbol_ptr = elf_file.symbol(symbol_index);
293 symbol_ptr.file_index = self.index;
294 symbol_ptr.output_section_index = shndx;
295
296 const esym_index = try self.addLocalEsym(gpa);
297 const esym = &self.local_esyms.items(.elf_sym)[esym_index];
298 esym.st_info = elf.STT_SECTION;
299 esym.st_shndx = shndx;
300 symbol_ptr.esym_index = esym_index;
301}
302
287303/// TODO actually create fake input shdrs and return that instead.
288304pub fn inputShdr(self: ZigObject, atom_index: Atom.Index, elf_file: *Elf) Object.ElfShdr {
289305 _ = self;
......@@ -435,6 +451,16 @@ pub fn updateRelaSectionSizes(self: ZigObject, elf_file: *Elf) void {
435451pub fn writeRelaSections(self: ZigObject, elf_file: *Elf) !void {
436452 const gpa = elf_file.base.allocator;
437453
454 // const getSectionSymbol = struct {
455 // fn getSectionSymbol(zig_object: ZigObject, shndx: u16, ctx: *Elf) Symbol.Index {
456 // for (zig_object.locals()) |local_index| {
457 // const local = ctx.symbol(local_index);
458 // if (local.type(ctx) == elf.STT_SECTION and local.output_section_index == shndx)
459 // return local.esym_index;
460 // } else unreachable;
461 // }
462 // }.getSectionSymbol;
463
438464 for (&[_]?u16{
439465 elf_file.zig_text_rela_section_index,
440466 elf_file.zig_data_rel_ro_rela_section_index,
......@@ -453,8 +479,13 @@ pub fn writeRelaSections(self: ZigObject, elf_file: *Elf) !void {
453479
454480 while (true) {
455481 for (atom.relocs(elf_file)) |rel| {
456 var r_sym = rel.r_sym() & symbol_mask;
457 if (self.isGlobal(rel.r_sym())) r_sym += @intCast(self.local_esyms.slice().len + 1);
482 const target = elf_file.symbol(self.symbol(rel.r_sym()));
483 const r_offset = target.value + rel.r_offset;
484 const r_sym: u32 = if (target.flags.global)
485 (target.esym_index & symbol_mask) + @as(u32, @intCast(self.local_esyms.slice().len))
486 else
487 target.esym_index;
488 // getSectionSymbol(self, target.outputShndx().?, elf_file);
458489 const r_type = switch (rel.r_type()) {
459490 Elf.R_X86_64_ZIG_GOT32,
460491 Elf.R_X86_64_ZIG_GOTPCREL,
......@@ -462,9 +493,9 @@ pub fn writeRelaSections(self: ZigObject, elf_file: *Elf) !void {
462493 else => |r_type| r_type,
463494 };
464495 relocs.appendAssumeCapacity(.{
465 .r_offset = rel.r_offset,
496 .r_offset = r_offset,
466497 .r_addend = rel.r_addend,
467 .r_info = (@as(u64, @intCast(r_sym)) << 32) | r_type,
498 .r_info = (@as(u64, @intCast(r_sym + 1)) << 32) | r_type,
468499 });
469500 }
470501 if (elf_file.atom(atom.prev_index)) |prev| {
......@@ -485,28 +516,27 @@ pub fn writeRelaSections(self: ZigObject, elf_file: *Elf) !void {
485516 }
486517}
487518
488pub fn isGlobal(self: ZigObject, index: Symbol.Index) bool {
489 _ = self;
519inline fn isGlobal(index: Symbol.Index) bool {
490520 return index & global_symbol_bit != 0;
491521}
492522
493pub fn symbol(self: *ZigObject, index: Symbol.Index) Symbol.Index {
523pub fn symbol(self: ZigObject, index: Symbol.Index) Symbol.Index {
494524 const actual_index = index & symbol_mask;
495 if (self.isGlobal(index)) return self.global_symbols.items[actual_index];
525 if (isGlobal(index)) return self.global_symbols.items[actual_index];
496526 return self.local_symbols.items[actual_index];
497527}
498528
499529pub fn elfSym(self: *ZigObject, index: Symbol.Index) *elf.Elf64_Sym {
500530 const actual_index = index & symbol_mask;
501 if (self.isGlobal(index)) return &self.global_esyms.items(.elf_sym)[actual_index];
531 if (isGlobal(index)) return &self.global_esyms.items(.elf_sym)[actual_index];
502532 return &self.local_esyms.items(.elf_sym)[actual_index];
503533}
504534
505pub fn locals(self: *ZigObject) []const Symbol.Index {
535pub fn locals(self: ZigObject) []const Symbol.Index {
506536 return self.local_symbols.items;
507537}
508538
509pub fn globals(self: *ZigObject) []const Symbol.Index {
539pub fn globals(self: ZigObject) []const Symbol.Index {
510540 return self.global_symbols.items;
511541}
512542
src/link/Elf/file.zig+2-1
......@@ -136,7 +136,8 @@ pub const File = union(enum) {
136136 if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
137137 const esym = local.elfSym(elf_file);
138138 switch (esym.st_type()) {
139 elf.STT_SECTION, elf.STT_NOTYPE => continue,
139 elf.STT_SECTION => if (!elf_file.isObject()) continue,
140 elf.STT_NOTYPE => continue,
140141 else => {},
141142 }
142143 local.flags.output_symtab = true;