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