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) = .{},...@@ -111,8 +111,6 @@ symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .{},
111111
112phdr_table_dirty: bool = false,112phdr_table_dirty: bool = false,
113shdr_table_dirty: bool = false,113shdr_table_dirty: bool = false,
114shstrtab_dirty: bool = false,
115strtab_dirty: bool = false,
116got_dirty: bool = false,114got_dirty: bool = false,
117115
118debug_strtab_dirty: bool = false,116debug_strtab_dirty: bool = false,
...@@ -723,24 +721,6 @@ pub fn populateMissingMetadata(self: *Elf) !void {...@@ -723,24 +721,6 @@ pub fn populateMissingMetadata(self: *Elf) !void {
723 }721 }
724 }722 }
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
744 if (self.text_section_index == null) {724 if (self.text_section_index == null) {
745 self.text_section_index = try self.allocateAllocSection(.{725 self.text_section_index = try self.allocateAllocSection(.{
746 .name = ".text",726 .name = ".text",
...@@ -1375,11 +1355,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1375,11 +1355,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1375 } else null;1355 } else null;
1376 }1356 }
13771357
1378 // Generate and emit non-incremental sections.
1379 try self.initSyntheticSections();
1380 try self.updateSyntheticSectionSizes();
1381 try self.writeSyntheticSections();
1382
1383 if (self.dwarf) |*dw| {1358 if (self.dwarf) |*dw| {
1384 if (self.debug_abbrev_section_dirty) {1359 if (self.debug_abbrev_section_dirty) {
1385 try dw.writeDbgAbbrev();1360 try dw.writeDbgAbbrev();
...@@ -1416,8 +1391,22 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1416,8 +1391,22 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1416 try dw.writeDbgLineHeader();1391 try dw.writeDbgLineHeader();
1417 self.debug_line_header_dirty = false;1392 self.debug_line_header_dirty = false;
1418 }1393 }
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 }
1419 }1403 }
14201404
1405 // Generate and emit non-incremental sections.
1406 try self.initSyntheticSections();
1407 try self.updateSyntheticSectionSizes();
1408 try self.writeSyntheticSections();
1409
1421 if (self.phdr_table_dirty) {1410 if (self.phdr_table_dirty) {
1422 const phsize: u64 = switch (self.ptr_width) {1411 const phsize: u64 = switch (self.ptr_width) {
1423 .p32 => @sizeOf(elf.Elf32_Phdr),1412 .p32 => @sizeOf(elf.Elf32_Phdr),
...@@ -1480,36 +1469,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1480,36 +1469,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1480 self.phdr_table_dirty = false;1469 self.phdr_table_dirty = false;
1481 }1470 }
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
1513 if (self.shdr_table_dirty) {1472 if (self.shdr_table_dirty) {
1514 const shsize: u64 = switch (self.ptr_width) {1473 const shsize: u64 = switch (self.ptr_width) {
1515 .p32 => @sizeOf(elf.Elf32_Shdr),1474 .p32 => @sizeOf(elf.Elf32_Shdr),
...@@ -1580,8 +1539,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1580,8 +1539,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1580 assert(!self.debug_aranges_section_dirty);1539 assert(!self.debug_aranges_section_dirty);
1581 assert(!self.phdr_table_dirty);1540 assert(!self.phdr_table_dirty);
1582 assert(!self.shdr_table_dirty);1541 assert(!self.shdr_table_dirty);
1583 assert(!self.shstrtab_dirty);
1584 assert(!self.strtab_dirty);
1585 assert(!self.debug_strtab_dirty);1542 assert(!self.debug_strtab_dirty);
1586 assert(!self.got.dirty);1543 assert(!self.got.dirty);
1587}1544}
...@@ -3523,12 +3480,34 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3523,12 +3480,34 @@ fn initSyntheticSections(self: *Elf) !void {
3523 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),3480 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),
3524 });3481 });
3525 }3482 }
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 }
3526}3499}
35273500
3528fn updateSyntheticSectionSizes(self: *Elf) !void {3501fn updateSyntheticSectionSizes(self: *Elf) !void {
3529 if (self.symtab_section_index != null) {3502 if (self.symtab_section_index != null) {
3530 try self.updateSymtabSize();3503 try self.updateSymtabSize();
3531 }3504 }
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 }
3532}3511}
35333512
3534fn updateSymtabSize(self: *Elf) !void {3513fn updateSymtabSize(self: *Elf) !void {
...@@ -3562,7 +3541,6 @@ fn updateSymtabSize(self: *Elf) !void {...@@ -3562,7 +3541,6 @@ fn updateSymtabSize(self: *Elf) !void {
3562 const shdr = &self.shdrs.items[self.symtab_section_index.?];3541 const shdr = &self.shdrs.items[self.symtab_section_index.?];
3563 shdr.sh_info = sizes.nlocals + 1;3542 shdr.sh_info = sizes.nlocals + 1;
3564 shdr.sh_link = self.strtab_section_index.?;3543 shdr.sh_link = self.strtab_section_index.?;
3565 self.markDirty(self.symtab_section_index.?, null);
35663544
3567 const sym_size: u64 = switch (self.ptr_width) {3545 const sym_size: u64 = switch (self.ptr_width) {
3568 .p32 => @sizeOf(elf.Elf32_Sym),3546 .p32 => @sizeOf(elf.Elf32_Sym),
...@@ -3573,11 +3551,18 @@ fn updateSymtabSize(self: *Elf) !void {...@@ -3573,11 +3551,18 @@ fn updateSymtabSize(self: *Elf) !void {
3573 .p64 => @alignOf(elf.Elf64_Sym),3551 .p64 => @alignOf(elf.Elf64_Sym),
3574 };3552 };
3575 const needed_size = (sizes.nlocals + sizes.nglobals + 1) * sym_size;3553 const needed_size = (sizes.nlocals + sizes.nglobals + 1) * sym_size;
3576 shdr.sh_size = needed_size;3554 try self.growNonAllocSection(self.symtab_section_index.?, needed_size, sym_align, false);
3577 try self.growNonAllocSection(self.symtab_section_index.?, needed_size, sym_align, true);
3578}3555}
35793556
3580fn writeSyntheticSections(self: *Elf) !void {3557fn 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 }
3581 if (self.symtab_section_index) |_| {3566 if (self.symtab_section_index) |_| {
3582 try self.writeSymtab();3567 try self.writeSymtab();
3583 }3568 }