authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-02 14:45:21+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:03+02:00
logd06aa21e484e891db208003d9814499586d80006
treef0c81d762fddcb655a7d0eabf1d195ec11499066
parentff1423c4cedda2e35d3ad32c350cc6c97da736b5

elf: for now always update .shstrtab and .strtab sections


1 files changed, 45 insertions(+), 60 deletions(-)

src/link/Elf.zig+45-60
......@@ -111,8 +111,6 @@ symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .{},
111111
112112phdr_table_dirty: bool = false,
113113shdr_table_dirty: bool = false,
114shstrtab_dirty: bool = false,
115strtab_dirty: bool = false,
116114got_dirty: bool = false,
117115
118116debug_strtab_dirty: bool = false,
......@@ -723,24 +721,6 @@ pub fn populateMissingMetadata(self: *Elf) !void {
723721 }
724722 }
725723
726 if (self.shstrtab_section_index == null) {
727 self.shstrtab_section_index = try self.allocateNonAllocSection(.{
728 .name = ".shstrtab",
729 .size = @intCast(self.shstrtab.buffer.items.len),
730 .type = elf.SHT_STRTAB,
731 });
732 self.shstrtab_dirty = true;
733 }
734
735 if (self.strtab_section_index == null) {
736 self.strtab_section_index = try self.allocateNonAllocSection(.{
737 .name = ".strtab",
738 .size = @intCast(self.strtab.buffer.items.len),
739 .type = elf.SHT_STRTAB,
740 });
741 self.strtab_dirty = true;
742 }
743
744724 if (self.text_section_index == null) {
745725 self.text_section_index = try self.allocateAllocSection(.{
746726 .name = ".text",
......@@ -1375,11 +1355,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
13751355 } else null;
13761356 }
13771357
1378 // Generate and emit non-incremental sections.
1379 try self.initSyntheticSections();
1380 try self.updateSyntheticSectionSizes();
1381 try self.writeSyntheticSections();
1382
13831358 if (self.dwarf) |*dw| {
13841359 if (self.debug_abbrev_section_dirty) {
13851360 try dw.writeDbgAbbrev();
......@@ -1416,8 +1391,22 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
14161391 try dw.writeDbgLineHeader();
14171392 self.debug_line_header_dirty = false;
14181393 }
1394
1395 if (self.debug_str_section_index) |index| {
1396 if (self.debug_strtab_dirty or dw.strtab.buffer.items.len != self.shdrs.items[index].sh_size) {
1397 try self.growNonAllocSection(index, dw.strtab.buffer.items.len, 1, false);
1398 const shdr = self.shdrs.items[index];
1399 try self.base.file.?.pwriteAll(dw.strtab.buffer.items, shdr.sh_offset);
1400 self.debug_strtab_dirty = false;
1401 }
1402 }
14191403 }
14201404
1405 // Generate and emit non-incremental sections.
1406 try self.initSyntheticSections();
1407 try self.updateSyntheticSectionSizes();
1408 try self.writeSyntheticSections();
1409
14211410 if (self.phdr_table_dirty) {
14221411 const phsize: u64 = switch (self.ptr_width) {
14231412 .p32 => @sizeOf(elf.Elf32_Phdr),
......@@ -1480,36 +1469,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
14801469 self.phdr_table_dirty = false;
14811470 }
14821471
1483 {
1484 const shdr_index = self.shstrtab_section_index.?;
1485 if (self.shstrtab_dirty or self.shstrtab.buffer.items.len != self.shdrs.items[shdr_index].sh_size) {
1486 try self.growNonAllocSection(shdr_index, self.shstrtab.buffer.items.len, 1, false);
1487 const shstrtab_sect = &self.shdrs.items[shdr_index];
1488 try self.base.file.?.pwriteAll(self.shstrtab.buffer.items, shstrtab_sect.sh_offset);
1489 self.shstrtab_dirty = false;
1490 }
1491 }
1492
1493 {
1494 const shdr_index = self.strtab_section_index.?;
1495 if (self.strtab_dirty or self.strtab.buffer.items.len != self.shdrs.items[shdr_index].sh_size) {
1496 try self.growNonAllocSection(shdr_index, self.strtab.buffer.items.len, 1, false);
1497 const strtab_sect = self.shdrs.items[shdr_index];
1498 try self.base.file.?.pwriteAll(self.strtab.buffer.items, strtab_sect.sh_offset);
1499 self.strtab_dirty = false;
1500 }
1501 }
1502
1503 if (self.dwarf) |dwarf| {
1504 const shdr_index = self.debug_str_section_index.?;
1505 if (self.debug_strtab_dirty or dwarf.strtab.buffer.items.len != self.shdrs.items[shdr_index].sh_size) {
1506 try self.growNonAllocSection(shdr_index, dwarf.strtab.buffer.items.len, 1, false);
1507 const debug_strtab_sect = self.shdrs.items[shdr_index];
1508 try self.base.file.?.pwriteAll(dwarf.strtab.buffer.items, debug_strtab_sect.sh_offset);
1509 self.debug_strtab_dirty = false;
1510 }
1511 }
1512
15131472 if (self.shdr_table_dirty) {
15141473 const shsize: u64 = switch (self.ptr_width) {
15151474 .p32 => @sizeOf(elf.Elf32_Shdr),
......@@ -1580,8 +1539,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
15801539 assert(!self.debug_aranges_section_dirty);
15811540 assert(!self.phdr_table_dirty);
15821541 assert(!self.shdr_table_dirty);
1583 assert(!self.shstrtab_dirty);
1584 assert(!self.strtab_dirty);
15851542 assert(!self.debug_strtab_dirty);
15861543 assert(!self.got.dirty);
15871544}
......@@ -3523,12 +3480,34 @@ fn initSyntheticSections(self: *Elf) !void {
35233480 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),
35243481 });
35253482 }
3483 if (self.strtab_section_index == null) {
3484 self.strtab_section_index = try self.addSection(.{
3485 .name = ".strtab",
3486 .type = elf.SHT_STRTAB,
3487 .entsize = 1,
3488 .addralign = 1,
3489 });
3490 }
3491 if (self.shstrtab_section_index == null) {
3492 self.shstrtab_section_index = try self.addSection(.{
3493 .name = ".shstrtab",
3494 .type = elf.SHT_STRTAB,
3495 .entsize = 1,
3496 .addralign = 1,
3497 });
3498 }
35263499}
35273500
35283501fn updateSyntheticSectionSizes(self: *Elf) !void {
35293502 if (self.symtab_section_index != null) {
35303503 try self.updateSymtabSize();
35313504 }
3505 if (self.strtab_section_index) |index| {
3506 try self.growNonAllocSection(index, self.strtab.buffer.items.len, 1, false);
3507 }
3508 if (self.shstrtab_section_index) |index| {
3509 try self.growNonAllocSection(index, self.shstrtab.buffer.items.len, 1, false);
3510 }
35323511}
35333512
35343513fn updateSymtabSize(self: *Elf) !void {
......@@ -3562,7 +3541,6 @@ fn updateSymtabSize(self: *Elf) !void {
35623541 const shdr = &self.shdrs.items[self.symtab_section_index.?];
35633542 shdr.sh_info = sizes.nlocals + 1;
35643543 shdr.sh_link = self.strtab_section_index.?;
3565 self.markDirty(self.symtab_section_index.?, null);
35663544
35673545 const sym_size: u64 = switch (self.ptr_width) {
35683546 .p32 => @sizeOf(elf.Elf32_Sym),
......@@ -3573,11 +3551,18 @@ fn updateSymtabSize(self: *Elf) !void {
35733551 .p64 => @alignOf(elf.Elf64_Sym),
35743552 };
35753553 const needed_size = (sizes.nlocals + sizes.nglobals + 1) * sym_size;
3576 shdr.sh_size = needed_size;
3577 try self.growNonAllocSection(self.symtab_section_index.?, needed_size, sym_align, true);
3554 try self.growNonAllocSection(self.symtab_section_index.?, needed_size, sym_align, false);
35783555}
35793556
35803557fn writeSyntheticSections(self: *Elf) !void {
3558 if (self.shstrtab_section_index) |index| {
3559 const shdr = self.shdrs.items[index];
3560 try self.base.file.?.pwriteAll(self.shstrtab.buffer.items, shdr.sh_offset);
3561 }
3562 if (self.strtab_section_index) |index| {
3563 const shdr = self.shdrs.items[index];
3564 try self.base.file.?.pwriteAll(self.strtab.buffer.items, shdr.sh_offset);
3565 }
35813566 if (self.symtab_section_index) |_| {
35823567 try self.writeSymtab();
35833568 }