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(...@@ -371,7 +371,7 @@ pub fn createEmpty(
371 try self.shstrtab.append(gpa, 0);371 try self.shstrtab.append(gpa, 0);
372 try self.strtab.append(gpa, 0);372 try self.strtab.append(gpa, 0);
373 // There must always be a null shdr in index 0373 // There must always be a null shdr in index 0
374 _ = try self.addSection(.{ .name = "" });374 _ = try self.addSection(.{});
375 // Append null symbol in output symtab375 // Append null symbol in output symtab
376 try self.symtab.append(gpa, null_sym);376 try self.symtab.append(gpa, null_sym);
377377
...@@ -716,7 +716,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -716,7 +716,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
716716
717 if (self.zig_text_section_index == null) {717 if (self.zig_text_section_index == null) {
718 self.zig_text_section_index = try self.addSection(.{718 self.zig_text_section_index = try self.addSection(.{
719 .name = ".text.zig",719 .name = try self.insertShString(".text.zig"),
720 .type = elf.SHT_PROGBITS,720 .type = elf.SHT_PROGBITS,
721 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,721 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
722 .addralign = 1,722 .addralign = 1,
...@@ -725,7 +725,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -725,7 +725,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
725 const shdr = &self.shdrs.items[self.zig_text_section_index.?];725 const shdr = &self.shdrs.items[self.zig_text_section_index.?];
726 fillSection(self, shdr, options.program_code_size_hint, self.phdr_zig_load_re_index);726 fillSection(self, shdr, options.program_code_size_hint, self.phdr_zig_load_re_index);
727 if (self.base.isRelocatable()) {727 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.?);
729 try self.output_rela_sections.putNoClobber(gpa, self.zig_text_section_index.?, .{729 try self.output_rela_sections.putNoClobber(gpa, self.zig_text_section_index.?, .{
730 .shndx = rela_shndx,730 .shndx = rela_shndx,
731 });731 });
...@@ -742,7 +742,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -742,7 +742,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
742742
743 if (self.zig_got_section_index == null and !self.base.isRelocatable()) {743 if (self.zig_got_section_index == null and !self.base.isRelocatable()) {
744 self.zig_got_section_index = try self.addSection(.{744 self.zig_got_section_index = try self.addSection(.{
745 .name = ".got.zig",745 .name = try self.insertShString(".got.zig"),
746 .type = elf.SHT_PROGBITS,746 .type = elf.SHT_PROGBITS,
747 .addralign = ptr_size,747 .addralign = ptr_size,
748 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,748 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
...@@ -763,7 +763,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -763,7 +763,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
763763
764 if (self.zig_data_rel_ro_section_index == null) {764 if (self.zig_data_rel_ro_section_index == null) {
765 self.zig_data_rel_ro_section_index = try self.addSection(.{765 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"),
767 .type = elf.SHT_PROGBITS,767 .type = elf.SHT_PROGBITS,
768 .addralign = 1,768 .addralign = 1,
769 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,769 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
...@@ -773,7 +773,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -773,7 +773,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
773 fillSection(self, shdr, 1024, self.phdr_zig_load_ro_index);773 fillSection(self, shdr, 1024, self.phdr_zig_load_ro_index);
774 if (self.base.isRelocatable()) {774 if (self.base.isRelocatable()) {
775 const rela_shndx = try self.addRelaShdr(775 const rela_shndx = try self.addRelaShdr(
776 ".rela.data.rel.ro.zig",776 try self.insertShString(".rela.data.rel.ro.zig"),
777 self.zig_data_rel_ro_section_index.?,777 self.zig_data_rel_ro_section_index.?,
778 );778 );
779 try self.output_rela_sections.putNoClobber(gpa, self.zig_data_rel_ro_section_index.?, .{779 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 {...@@ -792,7 +792,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
792792
793 if (self.zig_data_section_index == null) {793 if (self.zig_data_section_index == null) {
794 self.zig_data_section_index = try self.addSection(.{794 self.zig_data_section_index = try self.addSection(.{
795 .name = ".data.zig",795 .name = try self.insertShString(".data.zig"),
796 .type = elf.SHT_PROGBITS,796 .type = elf.SHT_PROGBITS,
797 .addralign = ptr_size,797 .addralign = ptr_size,
798 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,798 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
...@@ -802,7 +802,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -802,7 +802,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
802 fillSection(self, shdr, 1024, self.phdr_zig_load_rw_index);802 fillSection(self, shdr, 1024, self.phdr_zig_load_rw_index);
803 if (self.base.isRelocatable()) {803 if (self.base.isRelocatable()) {
804 const rela_shndx = try self.addRelaShdr(804 const rela_shndx = try self.addRelaShdr(
805 ".rela.data.zig",805 try self.insertShString(".rela.data.zig"),
806 self.zig_data_section_index.?,806 self.zig_data_section_index.?,
807 );807 );
808 try self.output_rela_sections.putNoClobber(gpa, self.zig_data_section_index.?, .{808 try self.output_rela_sections.putNoClobber(gpa, self.zig_data_section_index.?, .{
...@@ -821,7 +821,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -821,7 +821,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
821821
822 if (self.zig_bss_section_index == null) {822 if (self.zig_bss_section_index == null) {
823 self.zig_bss_section_index = try self.addSection(.{823 self.zig_bss_section_index = try self.addSection(.{
824 .name = ".bss.zig",824 .name = try self.insertShString(".bss.zig"),
825 .type = elf.SHT_NOBITS,825 .type = elf.SHT_NOBITS,
826 .addralign = ptr_size,826 .addralign = ptr_size,
827 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,827 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
...@@ -845,7 +845,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -845,7 +845,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
845 assert(dw.strtab.buffer.items.len == 0);845 assert(dw.strtab.buffer.items.len == 0);
846 try dw.strtab.buffer.append(gpa, 0);846 try dw.strtab.buffer.append(gpa, 0);
847 self.debug_str_section_index = try self.addSection(.{847 self.debug_str_section_index = try self.addSection(.{
848 .name = ".debug_str",848 .name = try self.insertShString(".debug_str"),
849 .flags = elf.SHF_MERGE | elf.SHF_STRINGS,849 .flags = elf.SHF_MERGE | elf.SHF_STRINGS,
850 .entsize = 1,850 .entsize = 1,
851 .type = elf.SHT_PROGBITS,851 .type = elf.SHT_PROGBITS,
...@@ -863,7 +863,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -863,7 +863,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
863863
864 if (self.debug_info_section_index == null) {864 if (self.debug_info_section_index == null) {
865 self.debug_info_section_index = try self.addSection(.{865 self.debug_info_section_index = try self.addSection(.{
866 .name = ".debug_info",866 .name = try self.insertShString(".debug_info"),
867 .type = elf.SHT_PROGBITS,867 .type = elf.SHT_PROGBITS,
868 .addralign = 1,868 .addralign = 1,
869 .offset = std.math.maxInt(u64),869 .offset = std.math.maxInt(u64),
...@@ -879,7 +879,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -879,7 +879,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
879879
880 if (self.debug_abbrev_section_index == null) {880 if (self.debug_abbrev_section_index == null) {
881 self.debug_abbrev_section_index = try self.addSection(.{881 self.debug_abbrev_section_index = try self.addSection(.{
882 .name = ".debug_abbrev",882 .name = try self.insertShString(".debug_abbrev"),
883 .type = elf.SHT_PROGBITS,883 .type = elf.SHT_PROGBITS,
884 .addralign = 1,884 .addralign = 1,
885 .offset = std.math.maxInt(u64),885 .offset = std.math.maxInt(u64),
...@@ -895,7 +895,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -895,7 +895,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
895895
896 if (self.debug_aranges_section_index == null) {896 if (self.debug_aranges_section_index == null) {
897 self.debug_aranges_section_index = try self.addSection(.{897 self.debug_aranges_section_index = try self.addSection(.{
898 .name = ".debug_aranges",898 .name = try self.insertShString(".debug_aranges"),
899 .type = elf.SHT_PROGBITS,899 .type = elf.SHT_PROGBITS,
900 .addralign = 16,900 .addralign = 16,
901 .offset = std.math.maxInt(u64),901 .offset = std.math.maxInt(u64),
...@@ -911,7 +911,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -911,7 +911,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
911911
912 if (self.debug_line_section_index == null) {912 if (self.debug_line_section_index == null) {
913 self.debug_line_section_index = try self.addSection(.{913 self.debug_line_section_index = try self.addSection(.{
914 .name = ".debug_line",914 .name = try self.insertShString(".debug_line"),
915 .type = elf.SHT_PROGBITS,915 .type = elf.SHT_PROGBITS,
916 .addralign = 1,916 .addralign = 1,
917 .offset = std.math.maxInt(u64),917 .offset = std.math.maxInt(u64),
...@@ -3390,7 +3390,7 @@ pub fn initMergeSections(self: *Elf) !void {...@@ -3390,7 +3390,7 @@ pub fn initMergeSections(self: *Elf) !void {
3390 if (msec.finalized_subsections.items.len == 0) continue;3390 if (msec.finalized_subsections.items.len == 0) continue;
3391 const name = msec.name(self);3391 const name = msec.name(self);
3392 const shndx = self.sectionByName(name) orelse try self.addSection(.{3392 const shndx = self.sectionByName(name) orelse try self.addSection(.{
3393 .name = name,3393 .name = msec.name_offset,
3394 .type = msec.type,3394 .type = msec.type,
3395 .flags = msec.flags,3395 .flags = msec.flags,
3396 });3396 });
...@@ -3416,7 +3416,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3416,7 +3416,7 @@ fn initSyntheticSections(self: *Elf) !void {
3416 } else false;3416 } else false;
3417 if (needs_eh_frame) {3417 if (needs_eh_frame) {
3418 self.eh_frame_section_index = try self.addSection(.{3418 self.eh_frame_section_index = try self.addSection(.{
3419 .name = ".eh_frame",3419 .name = try self.insertShString(".eh_frame"),
3420 .type = elf.SHT_PROGBITS,3420 .type = elf.SHT_PROGBITS,
3421 .flags = elf.SHF_ALLOC,3421 .flags = elf.SHF_ALLOC,
3422 .addralign = ptr_size,3422 .addralign = ptr_size,
...@@ -3425,7 +3425,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3425,7 +3425,7 @@ fn initSyntheticSections(self: *Elf) !void {
34253425
3426 if (comp.link_eh_frame_hdr) {3426 if (comp.link_eh_frame_hdr) {
3427 self.eh_frame_hdr_section_index = try self.addSection(.{3427 self.eh_frame_hdr_section_index = try self.addSection(.{
3428 .name = ".eh_frame_hdr",3428 .name = try self.insertShString(".eh_frame_hdr"),
3429 .type = elf.SHT_PROGBITS,3429 .type = elf.SHT_PROGBITS,
3430 .flags = elf.SHF_ALLOC,3430 .flags = elf.SHF_ALLOC,
3431 .addralign = 4,3431 .addralign = 4,
...@@ -3436,7 +3436,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3436,7 +3436,7 @@ fn initSyntheticSections(self: *Elf) !void {
34363436
3437 if (self.got.entries.items.len > 0) {3437 if (self.got.entries.items.len > 0) {
3438 self.got_section_index = try self.addSection(.{3438 self.got_section_index = try self.addSection(.{
3439 .name = ".got",3439 .name = try self.insertShString(".got"),
3440 .type = elf.SHT_PROGBITS,3440 .type = elf.SHT_PROGBITS,
3441 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,3441 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
3442 .addralign = ptr_size,3442 .addralign = ptr_size,
...@@ -3445,7 +3445,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3445,7 +3445,7 @@ fn initSyntheticSections(self: *Elf) !void {
3445 }3445 }
34463446
3447 self.got_plt_section_index = try self.addSection(.{3447 self.got_plt_section_index = try self.addSection(.{
3448 .name = ".got.plt",3448 .name = try self.insertShString(".got.plt"),
3449 .type = elf.SHT_PROGBITS,3449 .type = elf.SHT_PROGBITS,
3450 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,3450 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
3451 .addralign = @alignOf(u64),3451 .addralign = @alignOf(u64),
...@@ -3465,7 +3465,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3465,7 +3465,7 @@ fn initSyntheticSections(self: *Elf) !void {
3465 };3465 };
3466 if (needs_rela_dyn) {3466 if (needs_rela_dyn) {
3467 self.rela_dyn_section_index = try self.addSection(.{3467 self.rela_dyn_section_index = try self.addSection(.{
3468 .name = ".rela.dyn",3468 .name = try self.insertShString(".rela.dyn"),
3469 .type = elf.SHT_RELA,3469 .type = elf.SHT_RELA,
3470 .flags = elf.SHF_ALLOC,3470 .flags = elf.SHF_ALLOC,
3471 .addralign = @alignOf(elf.Elf64_Rela),3471 .addralign = @alignOf(elf.Elf64_Rela),
...@@ -3476,14 +3476,14 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3476,14 +3476,14 @@ fn initSyntheticSections(self: *Elf) !void {
34763476
3477 if (self.plt.symbols.items.len > 0) {3477 if (self.plt.symbols.items.len > 0) {
3478 self.plt_section_index = try self.addSection(.{3478 self.plt_section_index = try self.addSection(.{
3479 .name = ".plt",3479 .name = try self.insertShString(".plt"),
3480 .type = elf.SHT_PROGBITS,3480 .type = elf.SHT_PROGBITS,
3481 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,3481 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
3482 .addralign = 16,3482 .addralign = 16,
3483 .offset = std.math.maxInt(u64),3483 .offset = std.math.maxInt(u64),
3484 });3484 });
3485 self.rela_plt_section_index = try self.addSection(.{3485 self.rela_plt_section_index = try self.addSection(.{
3486 .name = ".rela.plt",3486 .name = try self.insertShString(".rela.plt"),
3487 .type = elf.SHT_RELA,3487 .type = elf.SHT_RELA,
3488 .flags = elf.SHF_ALLOC,3488 .flags = elf.SHF_ALLOC,
3489 .addralign = @alignOf(elf.Elf64_Rela),3489 .addralign = @alignOf(elf.Elf64_Rela),
...@@ -3494,7 +3494,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3494,7 +3494,7 @@ fn initSyntheticSections(self: *Elf) !void {
34943494
3495 if (self.plt_got.symbols.items.len > 0) {3495 if (self.plt_got.symbols.items.len > 0) {
3496 self.plt_got_section_index = try self.addSection(.{3496 self.plt_got_section_index = try self.addSection(.{
3497 .name = ".plt.got",3497 .name = try self.insertShString(".plt.got"),
3498 .type = elf.SHT_PROGBITS,3498 .type = elf.SHT_PROGBITS,
3499 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,3499 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
3500 .addralign = 16,3500 .addralign = 16,
...@@ -3504,7 +3504,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3504,7 +3504,7 @@ fn initSyntheticSections(self: *Elf) !void {
35043504
3505 if (self.copy_rel.symbols.items.len > 0) {3505 if (self.copy_rel.symbols.items.len > 0) {
3506 self.copy_rel_section_index = try self.addSection(.{3506 self.copy_rel_section_index = try self.addSection(.{
3507 .name = ".copyrel",3507 .name = try self.insertShString(".copyrel"),
3508 .type = elf.SHT_NOBITS,3508 .type = elf.SHT_NOBITS,
3509 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,3509 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
3510 .offset = std.math.maxInt(u64),3510 .offset = std.math.maxInt(u64),
...@@ -3522,7 +3522,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3522,7 +3522,7 @@ fn initSyntheticSections(self: *Elf) !void {
3522 };3522 };
3523 if (needs_interp) {3523 if (needs_interp) {
3524 self.interp_section_index = try self.addSection(.{3524 self.interp_section_index = try self.addSection(.{
3525 .name = ".interp",3525 .name = try self.insertShString(".interp"),
3526 .type = elf.SHT_PROGBITS,3526 .type = elf.SHT_PROGBITS,
3527 .flags = elf.SHF_ALLOC,3527 .flags = elf.SHF_ALLOC,
3528 .addralign = 1,3528 .addralign = 1,
...@@ -3532,7 +3532,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3532,7 +3532,7 @@ fn initSyntheticSections(self: *Elf) !void {
35323532
3533 if (self.isEffectivelyDynLib() or self.shared_objects.items.len > 0 or comp.config.pie) {3533 if (self.isEffectivelyDynLib() or self.shared_objects.items.len > 0 or comp.config.pie) {
3534 self.dynstrtab_section_index = try self.addSection(.{3534 self.dynstrtab_section_index = try self.addSection(.{
3535 .name = ".dynstr",3535 .name = try self.insertShString(".dynstr"),
3536 .flags = elf.SHF_ALLOC,3536 .flags = elf.SHF_ALLOC,
3537 .type = elf.SHT_STRTAB,3537 .type = elf.SHT_STRTAB,
3538 .entsize = 1,3538 .entsize = 1,
...@@ -3540,7 +3540,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3540,7 +3540,7 @@ fn initSyntheticSections(self: *Elf) !void {
3540 .offset = std.math.maxInt(u64),3540 .offset = std.math.maxInt(u64),
3541 });3541 });
3542 self.dynamic_section_index = try self.addSection(.{3542 self.dynamic_section_index = try self.addSection(.{
3543 .name = ".dynamic",3543 .name = try self.insertShString(".dynamic"),
3544 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,3544 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
3545 .type = elf.SHT_DYNAMIC,3545 .type = elf.SHT_DYNAMIC,
3546 .entsize = @sizeOf(elf.Elf64_Dyn),3546 .entsize = @sizeOf(elf.Elf64_Dyn),
...@@ -3548,7 +3548,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3548,7 +3548,7 @@ fn initSyntheticSections(self: *Elf) !void {
3548 .offset = std.math.maxInt(u64),3548 .offset = std.math.maxInt(u64),
3549 });3549 });
3550 self.dynsymtab_section_index = try self.addSection(.{3550 self.dynsymtab_section_index = try self.addSection(.{
3551 .name = ".dynsym",3551 .name = try self.insertShString(".dynsym"),
3552 .flags = elf.SHF_ALLOC,3552 .flags = elf.SHF_ALLOC,
3553 .type = elf.SHT_DYNSYM,3553 .type = elf.SHT_DYNSYM,
3554 .addralign = @alignOf(elf.Elf64_Sym),3554 .addralign = @alignOf(elf.Elf64_Sym),
...@@ -3557,7 +3557,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3557,7 +3557,7 @@ fn initSyntheticSections(self: *Elf) !void {
3557 .offset = std.math.maxInt(u64),3557 .offset = std.math.maxInt(u64),
3558 });3558 });
3559 self.hash_section_index = try self.addSection(.{3559 self.hash_section_index = try self.addSection(.{
3560 .name = ".hash",3560 .name = try self.insertShString(".hash"),
3561 .flags = elf.SHF_ALLOC,3561 .flags = elf.SHF_ALLOC,
3562 .type = elf.SHT_HASH,3562 .type = elf.SHT_HASH,
3563 .addralign = 4,3563 .addralign = 4,
...@@ -3565,7 +3565,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3565,7 +3565,7 @@ fn initSyntheticSections(self: *Elf) !void {
3565 .offset = std.math.maxInt(u64),3565 .offset = std.math.maxInt(u64),
3566 });3566 });
3567 self.gnu_hash_section_index = try self.addSection(.{3567 self.gnu_hash_section_index = try self.addSection(.{
3568 .name = ".gnu.hash",3568 .name = try self.insertShString(".gnu.hash"),
3569 .flags = elf.SHF_ALLOC,3569 .flags = elf.SHF_ALLOC,
3570 .type = elf.SHT_GNU_HASH,3570 .type = elf.SHT_GNU_HASH,
3571 .addralign = 8,3571 .addralign = 8,
...@@ -3578,7 +3578,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3578,7 +3578,7 @@ fn initSyntheticSections(self: *Elf) !void {
3578 } else false;3578 } else false;
3579 if (needs_versions) {3579 if (needs_versions) {
3580 self.versym_section_index = try self.addSection(.{3580 self.versym_section_index = try self.addSection(.{
3581 .name = ".gnu.version",3581 .name = try self.insertShString(".gnu.version"),
3582 .flags = elf.SHF_ALLOC,3582 .flags = elf.SHF_ALLOC,
3583 .type = elf.SHT_GNU_VERSYM,3583 .type = elf.SHT_GNU_VERSYM,
3584 .addralign = @alignOf(elf.Elf64_Versym),3584 .addralign = @alignOf(elf.Elf64_Versym),
...@@ -3586,7 +3586,7 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3586,7 +3586,7 @@ fn initSyntheticSections(self: *Elf) !void {
3586 .offset = std.math.maxInt(u64),3586 .offset = std.math.maxInt(u64),
3587 });3587 });
3588 self.verneed_section_index = try self.addSection(.{3588 self.verneed_section_index = try self.addSection(.{
3589 .name = ".gnu.version_r",3589 .name = try self.insertShString(".gnu.version_r"),
3590 .flags = elf.SHF_ALLOC,3590 .flags = elf.SHF_ALLOC,
3591 .type = elf.SHT_GNU_VERNEED,3591 .type = elf.SHT_GNU_VERNEED,
3592 .addralign = @alignOf(elf.Elf64_Verneed),3592 .addralign = @alignOf(elf.Elf64_Verneed),
...@@ -3606,7 +3606,7 @@ pub fn initSymtab(self: *Elf) !void {...@@ -3606,7 +3606,7 @@ pub fn initSymtab(self: *Elf) !void {
3606 };3606 };
3607 if (self.symtab_section_index == null) {3607 if (self.symtab_section_index == null) {
3608 self.symtab_section_index = try self.addSection(.{3608 self.symtab_section_index = try self.addSection(.{
3609 .name = ".symtab",3609 .name = try self.insertShString(".symtab"),
3610 .type = elf.SHT_SYMTAB,3610 .type = elf.SHT_SYMTAB,
3611 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),3611 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),
3612 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),3612 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),
...@@ -3615,7 +3615,7 @@ pub fn initSymtab(self: *Elf) !void {...@@ -3615,7 +3615,7 @@ pub fn initSymtab(self: *Elf) !void {
3615 }3615 }
3616 if (self.strtab_section_index == null) {3616 if (self.strtab_section_index == null) {
3617 self.strtab_section_index = try self.addSection(.{3617 self.strtab_section_index = try self.addSection(.{
3618 .name = ".strtab",3618 .name = try self.insertShString(".strtab"),
3619 .type = elf.SHT_STRTAB,3619 .type = elf.SHT_STRTAB,
3620 .entsize = 1,3620 .entsize = 1,
3621 .addralign = 1,3621 .addralign = 1,
...@@ -3627,7 +3627,7 @@ pub fn initSymtab(self: *Elf) !void {...@@ -3627,7 +3627,7 @@ pub fn initSymtab(self: *Elf) !void {
3627pub fn initShStrtab(self: *Elf) !void {3627pub fn initShStrtab(self: *Elf) !void {
3628 if (self.shstrtab_section_index == null) {3628 if (self.shstrtab_section_index == null) {
3629 self.shstrtab_section_index = try self.addSection(.{3629 self.shstrtab_section_index = try self.addSection(.{
3630 .name = ".shstrtab",3630 .name = try self.insertShString(".shstrtab"),
3631 .type = elf.SHT_STRTAB,3631 .type = elf.SHT_STRTAB,
3632 .entsize = 1,3632 .entsize = 1,
3633 .addralign = 1,3633 .addralign = 1,
...@@ -5428,7 +5428,7 @@ fn addPhdr(self: *Elf, opts: struct {...@@ -5428,7 +5428,7 @@ fn addPhdr(self: *Elf, opts: struct {
5428 return index;5428 return index;
5429}5429}
54305430
5431pub fn addRelaShdr(self: *Elf, name: [:0]const u8, shndx: u32) !u32 {5431pub fn addRelaShdr(self: *Elf, name: u32, shndx: u32) !u32 {
5432 const entsize: u64 = switch (self.ptr_width) {5432 const entsize: u64 = switch (self.ptr_width) {
5433 .p32 => @sizeOf(elf.Elf32_Rela),5433 .p32 => @sizeOf(elf.Elf32_Rela),
5434 .p64 => @sizeOf(elf.Elf64_Rela),5434 .p64 => @sizeOf(elf.Elf64_Rela),
...@@ -5449,7 +5449,7 @@ pub fn addRelaShdr(self: *Elf, name: [:0]const u8, shndx: u32) !u32 {...@@ -5449,7 +5449,7 @@ pub fn addRelaShdr(self: *Elf, name: [:0]const u8, shndx: u32) !u32 {
5449}5449}
54505450
5451pub const AddSectionOpts = struct {5451pub const AddSectionOpts = struct {
5452 name: [:0]const u8,5452 name: u32 = 0,
5453 type: u32 = elf.SHT_NULL,5453 type: u32 = elf.SHT_NULL,
5454 flags: u64 = 0,5454 flags: u64 = 0,
5455 link: u32 = 0,5455 link: u32 = 0,
...@@ -5464,7 +5464,7 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u32 {...@@ -5464,7 +5464,7 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u32 {
5464 const index = @as(u32, @intCast(self.shdrs.items.len));5464 const index = @as(u32, @intCast(self.shdrs.items.len));
5465 const shdr = try self.shdrs.addOne(gpa);5465 const shdr = try self.shdrs.addOne(gpa);
5466 shdr.* = .{5466 shdr.* = .{
5467 .sh_name = try self.insertShString(opts.name),5467 .sh_name = opts.name,
5468 .sh_type = opts.type,5468 .sh_type = opts.type,
5469 .sh_flags = opts.flags,5469 .sh_flags = opts.flags,
5470 .sh_addr = 0,5470 .sh_addr = 0,
...@@ -5709,7 +5709,7 @@ pub fn zigObjectPtr(self: *Elf) ?*ZigObject {...@@ -5709,7 +5709,7 @@ pub fn zigObjectPtr(self: *Elf) ?*ZigObject {
5709 return self.file(index).?.zig_object;5709 return self.file(index).?.zig_object;
5710}5710}
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 {
5713 const gpa = self.base.comp.gpa;5713 const gpa = self.base.comp.gpa;
5714 const out_name = name: {5714 const out_name = name: {
5715 if (self.base.isRelocatable()) break :name name;5715 if (self.base.isRelocatable()) break :name name;
...@@ -5717,11 +5717,11 @@ pub fn getOrCreateMergeSection(self: *Elf, name: []const u8, flags: u64, @"type"...@@ -5717,11 +5717,11 @@ pub fn getOrCreateMergeSection(self: *Elf, name: []const u8, flags: u64, @"type"
5717 break :name if (flags & elf.SHF_STRINGS != 0) ".rodata.str" else ".rodata.cst";5717 break :name if (flags & elf.SHF_STRINGS != 0) ".rodata.str" else ".rodata.cst";
5718 break :name name;5718 break :name name;
5719 };5719 };
5720 const out_off = try self.strings.insert(gpa, out_name);
5721 const out_flags = flags & ~@as(u64, elf.SHF_COMPRESSED | elf.SHF_GROUP);
5722 for (self.merge_sections.items, 0..) |msec, index| {5720 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);
5724 }5722 }
5723 const out_off = try self.insertShString(out_name);
5724 const out_flags = flags & ~@as(u64, elf.SHF_COMPRESSED | elf.SHF_GROUP);
5725 const index = @as(MergeSection.Index, @intCast(self.merge_sections.items.len));5725 const index = @as(MergeSection.Index, @intCast(self.merge_sections.items.len));
5726 const msec = try self.merge_sections.addOne(gpa);5726 const msec = try self.merge_sections.addOne(gpa);
5727 msec.* = .{5727 msec.* = .{
src/link/Elf/Atom.zig+1-1
...@@ -40,7 +40,7 @@ extra_index: u32 = 0,...@@ -40,7 +40,7 @@ extra_index: u32 = 0,
4040
41pub const Alignment = @import("../../InternPool.zig").Alignment;41pub 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 {
44 const file_ptr = self.file(elf_file).?;44 const file_ptr = self.file(elf_file).?;
45 return switch (file_ptr) {45 return switch (file_ptr) {
46 inline else => |x| x.getString(self.name_offset),46 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...@@ -354,7 +354,7 @@ fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) error{O
354 const out_shndx = elf_file.sectionByName(name) orelse try elf_file.addSection(.{354 const out_shndx = elf_file.sectionByName(name) orelse try elf_file.addSection(.{
355 .type = @"type",355 .type = @"type",
356 .flags = flags,356 .flags = flags,
357 .name = name,357 .name = try elf_file.insertShString(name),
358 });358 });
359 return out_shndx;359 return out_shndx;
360}360}
src/link/Elf/ZigObject.zig+2-2
...@@ -854,14 +854,14 @@ fn getDeclShdrIndex(...@@ -854,14 +854,14 @@ fn getDeclShdrIndex(
854 if (is_all_zeroes) break :blk elf_file.sectionByName(".tbss") orelse try elf_file.addSection(.{854 if (is_all_zeroes) break :blk elf_file.sectionByName(".tbss") orelse try elf_file.addSection(.{
855 .type = elf.SHT_NOBITS,855 .type = elf.SHT_NOBITS,
856 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,856 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,
857 .name = ".tbss",857 .name = try elf_file.insertShString(".tbss"),
858 .offset = std.math.maxInt(u64),858 .offset = std.math.maxInt(u64),
859 });859 });
860860
861 break :blk elf_file.sectionByName(".tdata") orelse try elf_file.addSection(.{861 break :blk elf_file.sectionByName(".tdata") orelse try elf_file.addSection(.{
862 .type = elf.SHT_PROGBITS,862 .type = elf.SHT_PROGBITS,
863 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,863 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,
864 .name = ".tdata",864 .name = try elf_file.insertShString(".tdata"),
865 .offset = std.math.maxInt(u64),865 .offset = std.math.maxInt(u64),
866 });866 });
867 }867 }
src/link/Elf/merge_section.zig+1-1
...@@ -21,7 +21,7 @@ pub const MergeSection = struct {...@@ -21,7 +21,7 @@ pub const MergeSection = struct {
21 }21 }
2222
23 pub fn name(msec: MergeSection, elf_file: *Elf) [:0]const u8 {23 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);
25 }25 }
2626
27 pub fn address(msec: MergeSection, elf_file: *Elf) i64 {27 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 {...@@ -299,13 +299,16 @@ fn initSections(elf_file: *Elf) !void {
299 } else false;299 } else false;
300 if (needs_eh_frame) {300 if (needs_eh_frame) {
301 elf_file.eh_frame_section_index = try elf_file.addSection(.{301 elf_file.eh_frame_section_index = try elf_file.addSection(.{
302 .name = ".eh_frame",302 .name = try elf_file.insertShString(".eh_frame"),
303 .type = elf.SHT_PROGBITS,303 .type = elf.SHT_PROGBITS,
304 .flags = elf.SHF_ALLOC,304 .flags = elf.SHF_ALLOC,
305 .addralign = ptr_size,305 .addralign = ptr_size,
306 .offset = std.math.maxInt(u64),306 .offset = std.math.maxInt(u64),
307 });307 });
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 );
309 }312 }
310313
311 try initComdatGroups(elf_file);314 try initComdatGroups(elf_file);
...@@ -323,7 +326,7 @@ fn initComdatGroups(elf_file: *Elf) !void {...@@ -323,7 +326,7 @@ fn initComdatGroups(elf_file: *Elf) !void {
323 const cg_sec = try elf_file.comdat_group_sections.addOne(gpa);326 const cg_sec = try elf_file.comdat_group_sections.addOne(gpa);
324 cg_sec.* = .{327 cg_sec.* = .{
325 .shndx = try elf_file.addSection(.{328 .shndx = try elf_file.addSection(.{
326 .name = ".group",329 .name = try elf_file.insertShString(".group"),
327 .type = elf.SHT_GROUP,330 .type = elf.SHT_GROUP,
328 .entsize = @sizeOf(u32),331 .entsize = @sizeOf(u32),
329 .addralign = @alignOf(u32),332 .addralign = @alignOf(u32),