authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-23 07:11:55+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-25 10:20:15+02:00
log95c30fb4864ddde2f8d8da7fc64f6f90baa42325
tree10e8268f34fc0d1ac2240719b52c46768d4cead0
parenta9d3885ac7529e3b1dde3def42ac3da2874dd774

elf: fix shdr-to-phdr links


2 files changed, 16 insertions(+), 10 deletions(-)

src/link/Elf.zig+15-9
......@@ -559,7 +559,7 @@ pub fn growAllocSection(self: *Elf, shdr_index: u32, needed_size: u64) !void {
559559 const shdr = &slice.items(.shdr)[shdr_index];
560560 assert(shdr.sh_flags & elf.SHF_ALLOC != 0);
561561 const phndx = slice.items(.phndx)[shdr_index];
562 const phdr = &self.phdrs.items[phndx];
562 const maybe_phdr = if (phndx) |ndx| &self.phdrs.items[ndx] else null;
563563
564564 log.debug("allocated size {x} of {s}, needed size {x}", .{
565565 self.allocatedSize(shdr.sh_offset),
......@@ -575,7 +575,7 @@ pub fn growAllocSection(self: *Elf, shdr_index: u32, needed_size: u64) !void {
575575 const existing_size = shdr.sh_size;
576576 shdr.sh_size = 0;
577577 // Must move the entire section.
578 const alignment = if (phdr.p_type == elf.PT_NULL) shdr.sh_addralign else phdr.p_align;
578 const alignment = if (maybe_phdr) |phdr| phdr.p_align else shdr.sh_addralign;
579579 const new_offset = try self.findFreeSpace(needed_size, alignment);
580580
581581 log.debug("new '{s}' file offset 0x{x} to 0x{x}", .{
......@@ -589,17 +589,17 @@ pub fn growAllocSection(self: *Elf, shdr_index: u32, needed_size: u64) !void {
589589 if (amt != existing_size) return error.InputOutput;
590590
591591 shdr.sh_offset = new_offset;
592 if (phdr.p_type != elf.PT_NULL) phdr.p_offset = new_offset;
592 if (maybe_phdr) |phdr| phdr.p_offset = new_offset;
593593 }
594 if (phdr.p_type != elf.PT_NULL) phdr.p_filesz = needed_size;
594 if (maybe_phdr) |phdr| phdr.p_filesz = needed_size;
595595 }
596596 shdr.sh_size = needed_size;
597597
598 if (phdr.p_type != elf.PT_NULL) {
598 if (maybe_phdr) |phdr| {
599599 const mem_capacity = self.allocatedVirtualSize(phdr.p_vaddr);
600600 if (needed_size > mem_capacity) {
601601 var err = try self.base.addErrorWithNotes(2);
602 try err.addMsg("fatal linker error: cannot expand load segment phdr({d}) in virtual memory", .{phndx});
602 try err.addMsg("fatal linker error: cannot expand load segment phdr({d}) in virtual memory", .{phndx.?});
603603 try err.addNote("TODO: emit relocations to memory locations in self-hosted backends", .{});
604604 try err.addNote("as a workaround, try increasing pre-allocated virtual memory of each segment", .{});
605605 }
......@@ -3235,6 +3235,12 @@ fn sortPhdrs(self: *Elf) error{OutOfMemory}!void {
32353235 index.* = backlinks[index.*];
32363236 }
32373237 }
3238
3239 for (self.sections.items(.phndx)) |*maybe_phndx| {
3240 if (maybe_phndx.*) |*index| {
3241 index.* = backlinks[index.*];
3242 }
3243 }
32383244}
32393245
32403246fn shdrRank(self: *Elf, shndx: u32) u8 {
......@@ -3763,6 +3769,7 @@ pub fn allocateAllocSections(self: *Elf) !void {
37633769
37643770 for (cover.items) |shndx| {
37653771 const shdr = &slice.items(.shdr)[shndx];
3772 slice.items(.phndx)[shndx] = phndx;
37663773 if (shdr.sh_type == elf.SHT_NOBITS) {
37673774 shdr.sh_offset = 0;
37683775 continue;
......@@ -3770,7 +3777,6 @@ pub fn allocateAllocSections(self: *Elf) !void {
37703777 off = alignment.@"align"(shndx, shdr.sh_addralign, off);
37713778 shdr.sh_offset = off;
37723779 off += shdr.sh_size;
3773 slice.items(.phndx)[shndx] = phndx;
37743780 }
37753781
37763782 addr = mem.alignForward(u64, addr, self.page_size);
......@@ -5282,7 +5288,7 @@ fn fmtDumpState(
52825288
52835289 try writer.writeAll("\nOutput shdrs\n");
52845290 for (self.sections.items(.shdr), self.sections.items(.phndx), 0..) |shdr, phndx, shndx| {
5285 try writer.print(" shdr({d}) : phdr({d}) : {}\n", .{
5291 try writer.print(" shdr({d}) : phdr({?d}) : {}\n", .{
52865292 shndx,
52875293 phndx,
52885294 self.fmtShdr(shdr),
......@@ -5530,7 +5536,7 @@ pub const SymbolResolver = struct {
55305536
55315537const Section = struct {
55325538 shdr: elf.Elf64_Shdr,
5533 phndx: u32 = 0,
5539 phndx: ?u32 = null,
55345540 atom_list: std.ArrayListUnmanaged(Ref) = .{},
55355541
55365542 /// Index of the last allocated atom in this section.
src/link/Elf/ZigObject.zig+1-1
......@@ -239,7 +239,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
239239 shdr.sh_size = 1024;
240240 } else {
241241 phndx.* = elf_file.phdr_zig_load_zerofill_index.?;
242 const phdr = elf_file.phdrs.items[phndx.*];
242 const phdr = elf_file.phdrs.items[phndx.*.?];
243243 shdr.sh_addr = phdr.p_vaddr;
244244 shdr.sh_size = phdr.p_memsz;
245245 }