authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-28 14:46:50+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-30 10:00:50+02:00
logef7bbcd80fec680b527795dd85bc294b3bf6cbbd
treee943de4e790be9494767f8bb43c7097defd64000
parent801c37209889883ad2d7b9cfb232fd8d9e0ed66c

elf: do not store merge section output section name in strings buffer


6 files changed, 53 insertions(+), 50 deletions(-)

src/link/Elf.zig+42-42
......@@ -371,7 +371,7 @@ pub fn createEmpty(
371371 try self.shstrtab.append(gpa, 0);
372372 try self.strtab.append(gpa, 0);
373373 // There must always be a null shdr in index 0
374 _ = try self.addSection(.{ .name = "" });
374 _ = try self.addSection(.{});
375375 // Append null symbol in output symtab
376376 try self.symtab.append(gpa, null_sym);
377377
......@@ -716,7 +716,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
716716
717717 if (self.zig_text_section_index == null) {
718718 self.zig_text_section_index = try self.addSection(.{
719 .name = ".text.zig",
719 .name = try self.insertShString(".text.zig"),
720720 .type = elf.SHT_PROGBITS,
721721 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
722722 .addralign = 1,
......@@ -725,7 +725,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
725725 const shdr = &self.shdrs.items[self.zig_text_section_index.?];
726726 fillSection(self, shdr, options.program_code_size_hint, self.phdr_zig_load_re_index);
727727 if (self.base.isRelocatable()) {
728 const rela_shndx = try self.addRelaShdr(".rela.text.zig", self.zig_text_section_index.?);
728 const rela_shndx = try self.addRelaShdr(try self.insertShString(".rela.text.zig"), self.zig_text_section_index.?);
729729 try self.output_rela_sections.putNoClobber(gpa, self.zig_text_section_index.?, .{
730730 .shndx = rela_shndx,
731731 });
......@@ -742,7 +742,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
742742
743743 if (self.zig_got_section_index == null and !self.base.isRelocatable()) {
744744 self.zig_got_section_index = try self.addSection(.{
745 .name = ".got.zig",
745 .name = try self.insertShString(".got.zig"),
746746 .type = elf.SHT_PROGBITS,
747747 .addralign = ptr_size,
748748 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
......@@ -763,7 +763,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
763763
764764 if (self.zig_data_rel_ro_section_index == null) {
765765 self.zig_data_rel_ro_section_index = try self.addSection(.{
766 .name = ".data.rel.ro.zig",
766 .name = try self.insertShString(".data.rel.ro.zig"),
767767 .type = elf.SHT_PROGBITS,
768768 .addralign = 1,
769769 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
......@@ -773,7 +773,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
773773 fillSection(self, shdr, 1024, self.phdr_zig_load_ro_index);
774774 if (self.base.isRelocatable()) {
775775 const rela_shndx = try self.addRelaShdr(
776 ".rela.data.rel.ro.zig",
776 try self.insertShString(".rela.data.rel.ro.zig"),
777777 self.zig_data_rel_ro_section_index.?,
778778 );
779779 try self.output_rela_sections.putNoClobber(gpa, self.zig_data_rel_ro_section_index.?, .{
......@@ -792,7 +792,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
792792
793793 if (self.zig_data_section_index == null) {
794794 self.zig_data_section_index = try self.addSection(.{
795 .name = ".data.zig",
795 .name = try self.insertShString(".data.zig"),
796796 .type = elf.SHT_PROGBITS,
797797 .addralign = ptr_size,
798798 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
......@@ -802,7 +802,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
802802 fillSection(self, shdr, 1024, self.phdr_zig_load_rw_index);
803803 if (self.base.isRelocatable()) {
804804 const rela_shndx = try self.addRelaShdr(
805 ".rela.data.zig",
805 try self.insertShString(".rela.data.zig"),
806806 self.zig_data_section_index.?,
807807 );
808808 try self.output_rela_sections.putNoClobber(gpa, self.zig_data_section_index.?, .{
......@@ -821,7 +821,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
821821
822822 if (self.zig_bss_section_index == null) {
823823 self.zig_bss_section_index = try self.addSection(.{
824 .name = ".bss.zig",
824 .name = try self.insertShString(".bss.zig"),
825825 .type = elf.SHT_NOBITS,
826826 .addralign = ptr_size,
827827 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
......@@ -845,7 +845,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
845845 assert(dw.strtab.buffer.items.len == 0);
846846 try dw.strtab.buffer.append(gpa, 0);
847847 self.debug_str_section_index = try self.addSection(.{
848 .name = ".debug_str",
848 .name = try self.insertShString(".debug_str"),
849849 .flags = elf.SHF_MERGE | elf.SHF_STRINGS,
850850 .entsize = 1,
851851 .type = elf.SHT_PROGBITS,
......@@ -863,7 +863,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
863863
864864 if (self.debug_info_section_index == null) {
865865 self.debug_info_section_index = try self.addSection(.{
866 .name = ".debug_info",
866 .name = try self.insertShString(".debug_info"),
867867 .type = elf.SHT_PROGBITS,
868868 .addralign = 1,
869869 .offset = std.math.maxInt(u64),
......@@ -879,7 +879,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
879879
880880 if (self.debug_abbrev_section_index == null) {
881881 self.debug_abbrev_section_index = try self.addSection(.{
882 .name = ".debug_abbrev",
882 .name = try self.insertShString(".debug_abbrev"),
883883 .type = elf.SHT_PROGBITS,
884884 .addralign = 1,
885885 .offset = std.math.maxInt(u64),
......@@ -895,7 +895,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
895895
896896 if (self.debug_aranges_section_index == null) {
897897 self.debug_aranges_section_index = try self.addSection(.{
898 .name = ".debug_aranges",
898 .name = try self.insertShString(".debug_aranges"),
899899 .type = elf.SHT_PROGBITS,
900900 .addralign = 16,
901901 .offset = std.math.maxInt(u64),
......@@ -911,7 +911,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
911911
912912 if (self.debug_line_section_index == null) {
913913 self.debug_line_section_index = try self.addSection(.{
914 .name = ".debug_line",
914 .name = try self.insertShString(".debug_line"),
915915 .type = elf.SHT_PROGBITS,
916916 .addralign = 1,
917917 .offset = std.math.maxInt(u64),
......@@ -3390,7 +3390,7 @@ pub fn initMergeSections(self: *Elf) !void {
33903390 if (msec.finalized_subsections.items.len == 0) continue;
33913391 const name = msec.name(self);
33923392 const shndx = self.sectionByName(name) orelse try self.addSection(.{
3393 .name = name,
3393 .name = msec.name_offset,
33943394 .type = msec.type,
33953395 .flags = msec.flags,
33963396 });
......@@ -3416,7 +3416,7 @@ fn initSyntheticSections(self: *Elf) !void {
34163416 } else false;
34173417 if (needs_eh_frame) {
34183418 self.eh_frame_section_index = try self.addSection(.{
3419 .name = ".eh_frame",
3419 .name = try self.insertShString(".eh_frame"),
34203420 .type = elf.SHT_PROGBITS,
34213421 .flags = elf.SHF_ALLOC,
34223422 .addralign = ptr_size,
......@@ -3425,7 +3425,7 @@ fn initSyntheticSections(self: *Elf) !void {
34253425
34263426 if (comp.link_eh_frame_hdr) {
34273427 self.eh_frame_hdr_section_index = try self.addSection(.{
3428 .name = ".eh_frame_hdr",
3428 .name = try self.insertShString(".eh_frame_hdr"),
34293429 .type = elf.SHT_PROGBITS,
34303430 .flags = elf.SHF_ALLOC,
34313431 .addralign = 4,
......@@ -3436,7 +3436,7 @@ fn initSyntheticSections(self: *Elf) !void {
34363436
34373437 if (self.got.entries.items.len > 0) {
34383438 self.got_section_index = try self.addSection(.{
3439 .name = ".got",
3439 .name = try self.insertShString(".got"),
34403440 .type = elf.SHT_PROGBITS,
34413441 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
34423442 .addralign = ptr_size,
......@@ -3445,7 +3445,7 @@ fn initSyntheticSections(self: *Elf) !void {
34453445 }
34463446
34473447 self.got_plt_section_index = try self.addSection(.{
3448 .name = ".got.plt",
3448 .name = try self.insertShString(".got.plt"),
34493449 .type = elf.SHT_PROGBITS,
34503450 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
34513451 .addralign = @alignOf(u64),
......@@ -3465,7 +3465,7 @@ fn initSyntheticSections(self: *Elf) !void {
34653465 };
34663466 if (needs_rela_dyn) {
34673467 self.rela_dyn_section_index = try self.addSection(.{
3468 .name = ".rela.dyn",
3468 .name = try self.insertShString(".rela.dyn"),
34693469 .type = elf.SHT_RELA,
34703470 .flags = elf.SHF_ALLOC,
34713471 .addralign = @alignOf(elf.Elf64_Rela),
......@@ -3476,14 +3476,14 @@ fn initSyntheticSections(self: *Elf) !void {
34763476
34773477 if (self.plt.symbols.items.len > 0) {
34783478 self.plt_section_index = try self.addSection(.{
3479 .name = ".plt",
3479 .name = try self.insertShString(".plt"),
34803480 .type = elf.SHT_PROGBITS,
34813481 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
34823482 .addralign = 16,
34833483 .offset = std.math.maxInt(u64),
34843484 });
34853485 self.rela_plt_section_index = try self.addSection(.{
3486 .name = ".rela.plt",
3486 .name = try self.insertShString(".rela.plt"),
34873487 .type = elf.SHT_RELA,
34883488 .flags = elf.SHF_ALLOC,
34893489 .addralign = @alignOf(elf.Elf64_Rela),
......@@ -3494,7 +3494,7 @@ fn initSyntheticSections(self: *Elf) !void {
34943494
34953495 if (self.plt_got.symbols.items.len > 0) {
34963496 self.plt_got_section_index = try self.addSection(.{
3497 .name = ".plt.got",
3497 .name = try self.insertShString(".plt.got"),
34983498 .type = elf.SHT_PROGBITS,
34993499 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
35003500 .addralign = 16,
......@@ -3504,7 +3504,7 @@ fn initSyntheticSections(self: *Elf) !void {
35043504
35053505 if (self.copy_rel.symbols.items.len > 0) {
35063506 self.copy_rel_section_index = try self.addSection(.{
3507 .name = ".copyrel",
3507 .name = try self.insertShString(".copyrel"),
35083508 .type = elf.SHT_NOBITS,
35093509 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
35103510 .offset = std.math.maxInt(u64),
......@@ -3522,7 +3522,7 @@ fn initSyntheticSections(self: *Elf) !void {
35223522 };
35233523 if (needs_interp) {
35243524 self.interp_section_index = try self.addSection(.{
3525 .name = ".interp",
3525 .name = try self.insertShString(".interp"),
35263526 .type = elf.SHT_PROGBITS,
35273527 .flags = elf.SHF_ALLOC,
35283528 .addralign = 1,
......@@ -3532,7 +3532,7 @@ fn initSyntheticSections(self: *Elf) !void {
35323532
35333533 if (self.isEffectivelyDynLib() or self.shared_objects.items.len > 0 or comp.config.pie) {
35343534 self.dynstrtab_section_index = try self.addSection(.{
3535 .name = ".dynstr",
3535 .name = try self.insertShString(".dynstr"),
35363536 .flags = elf.SHF_ALLOC,
35373537 .type = elf.SHT_STRTAB,
35383538 .entsize = 1,
......@@ -3540,7 +3540,7 @@ fn initSyntheticSections(self: *Elf) !void {
35403540 .offset = std.math.maxInt(u64),
35413541 });
35423542 self.dynamic_section_index = try self.addSection(.{
3543 .name = ".dynamic",
3543 .name = try self.insertShString(".dynamic"),
35443544 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
35453545 .type = elf.SHT_DYNAMIC,
35463546 .entsize = @sizeOf(elf.Elf64_Dyn),
......@@ -3548,7 +3548,7 @@ fn initSyntheticSections(self: *Elf) !void {
35483548 .offset = std.math.maxInt(u64),
35493549 });
35503550 self.dynsymtab_section_index = try self.addSection(.{
3551 .name = ".dynsym",
3551 .name = try self.insertShString(".dynsym"),
35523552 .flags = elf.SHF_ALLOC,
35533553 .type = elf.SHT_DYNSYM,
35543554 .addralign = @alignOf(elf.Elf64_Sym),
......@@ -3557,7 +3557,7 @@ fn initSyntheticSections(self: *Elf) !void {
35573557 .offset = std.math.maxInt(u64),
35583558 });
35593559 self.hash_section_index = try self.addSection(.{
3560 .name = ".hash",
3560 .name = try self.insertShString(".hash"),
35613561 .flags = elf.SHF_ALLOC,
35623562 .type = elf.SHT_HASH,
35633563 .addralign = 4,
......@@ -3565,7 +3565,7 @@ fn initSyntheticSections(self: *Elf) !void {
35653565 .offset = std.math.maxInt(u64),
35663566 });
35673567 self.gnu_hash_section_index = try self.addSection(.{
3568 .name = ".gnu.hash",
3568 .name = try self.insertShString(".gnu.hash"),
35693569 .flags = elf.SHF_ALLOC,
35703570 .type = elf.SHT_GNU_HASH,
35713571 .addralign = 8,
......@@ -3578,7 +3578,7 @@ fn initSyntheticSections(self: *Elf) !void {
35783578 } else false;
35793579 if (needs_versions) {
35803580 self.versym_section_index = try self.addSection(.{
3581 .name = ".gnu.version",
3581 .name = try self.insertShString(".gnu.version"),
35823582 .flags = elf.SHF_ALLOC,
35833583 .type = elf.SHT_GNU_VERSYM,
35843584 .addralign = @alignOf(elf.Elf64_Versym),
......@@ -3586,7 +3586,7 @@ fn initSyntheticSections(self: *Elf) !void {
35863586 .offset = std.math.maxInt(u64),
35873587 });
35883588 self.verneed_section_index = try self.addSection(.{
3589 .name = ".gnu.version_r",
3589 .name = try self.insertShString(".gnu.version_r"),
35903590 .flags = elf.SHF_ALLOC,
35913591 .type = elf.SHT_GNU_VERNEED,
35923592 .addralign = @alignOf(elf.Elf64_Verneed),
......@@ -3606,7 +3606,7 @@ pub fn initSymtab(self: *Elf) !void {
36063606 };
36073607 if (self.symtab_section_index == null) {
36083608 self.symtab_section_index = try self.addSection(.{
3609 .name = ".symtab",
3609 .name = try self.insertShString(".symtab"),
36103610 .type = elf.SHT_SYMTAB,
36113611 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),
36123612 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),
......@@ -3615,7 +3615,7 @@ pub fn initSymtab(self: *Elf) !void {
36153615 }
36163616 if (self.strtab_section_index == null) {
36173617 self.strtab_section_index = try self.addSection(.{
3618 .name = ".strtab",
3618 .name = try self.insertShString(".strtab"),
36193619 .type = elf.SHT_STRTAB,
36203620 .entsize = 1,
36213621 .addralign = 1,
......@@ -3627,7 +3627,7 @@ pub fn initSymtab(self: *Elf) !void {
36273627pub fn initShStrtab(self: *Elf) !void {
36283628 if (self.shstrtab_section_index == null) {
36293629 self.shstrtab_section_index = try self.addSection(.{
3630 .name = ".shstrtab",
3630 .name = try self.insertShString(".shstrtab"),
36313631 .type = elf.SHT_STRTAB,
36323632 .entsize = 1,
36333633 .addralign = 1,
......@@ -5428,7 +5428,7 @@ fn addPhdr(self: *Elf, opts: struct {
54285428 return index;
54295429}
54305430
5431pub fn addRelaShdr(self: *Elf, name: [:0]const u8, shndx: u32) !u32 {
5431pub fn addRelaShdr(self: *Elf, name: u32, shndx: u32) !u32 {
54325432 const entsize: u64 = switch (self.ptr_width) {
54335433 .p32 => @sizeOf(elf.Elf32_Rela),
54345434 .p64 => @sizeOf(elf.Elf64_Rela),
......@@ -5449,7 +5449,7 @@ pub fn addRelaShdr(self: *Elf, name: [:0]const u8, shndx: u32) !u32 {
54495449}
54505450
54515451pub const AddSectionOpts = struct {
5452 name: [:0]const u8,
5452 name: u32 = 0,
54535453 type: u32 = elf.SHT_NULL,
54545454 flags: u64 = 0,
54555455 link: u32 = 0,
......@@ -5464,7 +5464,7 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u32 {
54645464 const index = @as(u32, @intCast(self.shdrs.items.len));
54655465 const shdr = try self.shdrs.addOne(gpa);
54665466 shdr.* = .{
5467 .sh_name = try self.insertShString(opts.name),
5467 .sh_name = opts.name,
54685468 .sh_type = opts.type,
54695469 .sh_flags = opts.flags,
54705470 .sh_addr = 0,
......@@ -5709,7 +5709,7 @@ pub fn zigObjectPtr(self: *Elf) ?*ZigObject {
57095709 return self.file(index).?.zig_object;
57105710}
57115711
5712pub fn getOrCreateMergeSection(self: *Elf, name: []const u8, flags: u64, @"type": u32) !MergeSection.Index {
5712pub fn getOrCreateMergeSection(self: *Elf, name: [:0]const u8, flags: u64, @"type": u32) !MergeSection.Index {
57135713 const gpa = self.base.comp.gpa;
57145714 const out_name = name: {
57155715 if (self.base.isRelocatable()) break :name name;
......@@ -5717,11 +5717,11 @@ pub fn getOrCreateMergeSection(self: *Elf, name: []const u8, flags: u64, @"type"
57175717 break :name if (flags & elf.SHF_STRINGS != 0) ".rodata.str" else ".rodata.cst";
57185718 break :name name;
57195719 };
5720 const out_off = try self.strings.insert(gpa, out_name);
5721 const out_flags = flags & ~@as(u64, elf.SHF_COMPRESSED | elf.SHF_GROUP);
57225720 for (self.merge_sections.items, 0..) |msec, index| {
5723 if (msec.name_offset == out_off) return @intCast(index);
5721 if (mem.eql(u8, msec.name(self), out_name)) return @intCast(index);
57245722 }
5723 const out_off = try self.insertShString(out_name);
5724 const out_flags = flags & ~@as(u64, elf.SHF_COMPRESSED | elf.SHF_GROUP);
57255725 const index = @as(MergeSection.Index, @intCast(self.merge_sections.items.len));
57265726 const msec = try self.merge_sections.addOne(gpa);
57275727 msec.* = .{
src/link/Elf/Atom.zig+1-1
......@@ -40,7 +40,7 @@ extra_index: u32 = 0,
4040
4141pub const Alignment = @import("../../InternPool.zig").Alignment;
4242
43pub fn name(self: Atom, elf_file: *Elf) []const u8 {
43pub fn name(self: Atom, elf_file: *Elf) [:0]const u8 {
4444 const file_ptr = self.file(elf_file).?;
4545 return switch (file_ptr) {
4646 inline else => |x| x.getString(self.name_offset),
src/link/Elf/Object.zig+1-1
......@@ -354,7 +354,7 @@ fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) error{O
354354 const out_shndx = elf_file.sectionByName(name) orelse try elf_file.addSection(.{
355355 .type = @"type",
356356 .flags = flags,
357 .name = name,
357 .name = try elf_file.insertShString(name),
358358 });
359359 return out_shndx;
360360}
src/link/Elf/ZigObject.zig+2-2
......@@ -854,14 +854,14 @@ fn getDeclShdrIndex(
854854 if (is_all_zeroes) break :blk elf_file.sectionByName(".tbss") orelse try elf_file.addSection(.{
855855 .type = elf.SHT_NOBITS,
856856 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,
857 .name = ".tbss",
857 .name = try elf_file.insertShString(".tbss"),
858858 .offset = std.math.maxInt(u64),
859859 });
860860
861861 break :blk elf_file.sectionByName(".tdata") orelse try elf_file.addSection(.{
862862 .type = elf.SHT_PROGBITS,
863863 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,
864 .name = ".tdata",
864 .name = try elf_file.insertShString(".tdata"),
865865 .offset = std.math.maxInt(u64),
866866 });
867867 }
src/link/Elf/merge_section.zig+1-1
......@@ -21,7 +21,7 @@ pub const MergeSection = struct {
2121 }
2222
2323 pub fn name(msec: MergeSection, elf_file: *Elf) [:0]const u8 {
24 return elf_file.strings.getAssumeExists(msec.name_offset);
24 return elf_file.getShString(msec.name_offset);
2525 }
2626
2727 pub fn address(msec: MergeSection, elf_file: *Elf) i64 {
src/link/Elf/relocatable.zig+6-3
......@@ -299,13 +299,16 @@ fn initSections(elf_file: *Elf) !void {
299299 } else false;
300300 if (needs_eh_frame) {
301301 elf_file.eh_frame_section_index = try elf_file.addSection(.{
302 .name = ".eh_frame",
302 .name = try elf_file.insertShString(".eh_frame"),
303303 .type = elf.SHT_PROGBITS,
304304 .flags = elf.SHF_ALLOC,
305305 .addralign = ptr_size,
306306 .offset = std.math.maxInt(u64),
307307 });
308 elf_file.eh_frame_rela_section_index = try elf_file.addRelaShdr(".rela.eh_frame", elf_file.eh_frame_section_index.?);
308 elf_file.eh_frame_rela_section_index = try elf_file.addRelaShdr(
309 try elf_file.insertShString(".rela.eh_frame"),
310 elf_file.eh_frame_section_index.?,
311 );
309312 }
310313
311314 try initComdatGroups(elf_file);
......@@ -323,7 +326,7 @@ fn initComdatGroups(elf_file: *Elf) !void {
323326 const cg_sec = try elf_file.comdat_group_sections.addOne(gpa);
324327 cg_sec.* = .{
325328 .shndx = try elf_file.addSection(.{
326 .name = ".group",
329 .name = try elf_file.insertShString(".group"),
327330 .type = elf.SHT_GROUP,
328331 .entsize = @sizeOf(u32),
329332 .addralign = @alignOf(u32),