| ... | ... | @@ -559,7 +559,7 @@ pub fn growAllocSection(self: *Elf, shdr_index: u32, needed_size: u64) !void { |
| 559 | 559 | const shdr = &slice.items(.shdr)[shdr_index]; |
| 560 | 560 | assert(shdr.sh_flags & elf.SHF_ALLOC != 0); |
| 561 | 561 | 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; |
| 563 | 563 | |
| 564 | 564 | log.debug("allocated size {x} of {s}, needed size {x}", .{ |
| 565 | 565 | self.allocatedSize(shdr.sh_offset), |
| ... | ... | @@ -575,7 +575,7 @@ pub fn growAllocSection(self: *Elf, shdr_index: u32, needed_size: u64) !void { |
| 575 | 575 | const existing_size = shdr.sh_size; |
| 576 | 576 | shdr.sh_size = 0; |
| 577 | 577 | // 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; |
| 579 | 579 | const new_offset = try self.findFreeSpace(needed_size, alignment); |
| 580 | 580 | |
| 581 | 581 | 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 { |
| 589 | 589 | if (amt != existing_size) return error.InputOutput; |
| 590 | 590 | |
| 591 | 591 | 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; |
| 593 | 593 | } |
| 594 | | if (phdr.p_type != elf.PT_NULL) phdr.p_filesz = needed_size; |
| 594 | if (maybe_phdr) |phdr| phdr.p_filesz = needed_size; |
| 595 | 595 | } |
| 596 | 596 | shdr.sh_size = needed_size; |
| 597 | 597 | |
| 598 | | if (phdr.p_type != elf.PT_NULL) { |
| 598 | if (maybe_phdr) |phdr| { |
| 599 | 599 | const mem_capacity = self.allocatedVirtualSize(phdr.p_vaddr); |
| 600 | 600 | if (needed_size > mem_capacity) { |
| 601 | 601 | 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.?}); |
| 603 | 603 | try err.addNote("TODO: emit relocations to memory locations in self-hosted backends", .{}); |
| 604 | 604 | try err.addNote("as a workaround, try increasing pre-allocated virtual memory of each segment", .{}); |
| 605 | 605 | } |
| ... | ... | @@ -3235,6 +3235,12 @@ fn sortPhdrs(self: *Elf) error{OutOfMemory}!void { |
| 3235 | 3235 | index.* = backlinks[index.*]; |
| 3236 | 3236 | } |
| 3237 | 3237 | } |
| 3238 | |
| 3239 | for (self.sections.items(.phndx)) |*maybe_phndx| { |
| 3240 | if (maybe_phndx.*) |*index| { |
| 3241 | index.* = backlinks[index.*]; |
| 3242 | } |
| 3243 | } |
| 3238 | 3244 | } |
| 3239 | 3245 | |
| 3240 | 3246 | fn shdrRank(self: *Elf, shndx: u32) u8 { |
| ... | ... | @@ -3763,6 +3769,7 @@ pub fn allocateAllocSections(self: *Elf) !void { |
| 3763 | 3769 | |
| 3764 | 3770 | for (cover.items) |shndx| { |
| 3765 | 3771 | const shdr = &slice.items(.shdr)[shndx]; |
| 3772 | slice.items(.phndx)[shndx] = phndx; |
| 3766 | 3773 | if (shdr.sh_type == elf.SHT_NOBITS) { |
| 3767 | 3774 | shdr.sh_offset = 0; |
| 3768 | 3775 | continue; |
| ... | ... | @@ -3770,7 +3777,6 @@ pub fn allocateAllocSections(self: *Elf) !void { |
| 3770 | 3777 | off = alignment.@"align"(shndx, shdr.sh_addralign, off); |
| 3771 | 3778 | shdr.sh_offset = off; |
| 3772 | 3779 | off += shdr.sh_size; |
| 3773 | | slice.items(.phndx)[shndx] = phndx; |
| 3774 | 3780 | } |
| 3775 | 3781 | |
| 3776 | 3782 | addr = mem.alignForward(u64, addr, self.page_size); |
| ... | ... | @@ -5282,7 +5288,7 @@ fn fmtDumpState( |
| 5282 | 5288 | |
| 5283 | 5289 | try writer.writeAll("\nOutput shdrs\n"); |
| 5284 | 5290 | 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", .{ |
| 5286 | 5292 | shndx, |
| 5287 | 5293 | phndx, |
| 5288 | 5294 | self.fmtShdr(shdr), |
| ... | ... | @@ -5530,7 +5536,7 @@ pub const SymbolResolver = struct { |
| 5530 | 5536 | |
| 5531 | 5537 | const Section = struct { |
| 5532 | 5538 | shdr: elf.Elf64_Shdr, |
| 5533 | | phndx: u32 = 0, |
| 5539 | phndx: ?u32 = null, |
| 5534 | 5540 | atom_list: std.ArrayListUnmanaged(Ref) = .{}, |
| 5535 | 5541 | |
| 5536 | 5542 | /// Index of the last allocated atom in this section. |