authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-11 14:17:36+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:05+02:00
logc87e73b19f8aca4a5bcea1548924b44377a5eec6
tree2e38190c0dafb0955b08f09336baa35936ee9eb5
parent2aa6099ad9dc5c45994101c94888deeab4ec2a06

elf: allocate non-alloc sections using incremental mechanism


1 files changed, 8 insertions(+), 17 deletions(-)

src/link/Elf.zig+8-17
...@@ -4623,18 +4623,6 @@ fn allocateAllocSectionsInFile(self: *Elf, base_offset: u64) void {...@@ -4623,18 +4623,6 @@ fn allocateAllocSectionsInFile(self: *Elf, base_offset: u64) void {
4623 }4623 }
4624}4624}
46254625
4626fn allocateNonAllocSectionsInFile(self: *Elf, base_offset: u64) void {
4627 var offset = base_offset;
4628 var i: usize = 0;
4629 while (i < self.shdrs.items.len) : (i += 1) {
4630 const first = &self.shdrs.items[i];
4631 if (shdrIsAlloc(first) or first.sh_type == elf.SHT_NULL) continue;
4632 // Non-alloc sections don't need congruency with their allocated virtual memory addresses
4633 first.sh_offset = mem.alignForward(u64, offset, first.sh_addralign);
4634 offset = first.sh_offset + first.sh_size;
4635 }
4636}
4637
4638fn initAndAllocateSegments(self: *Elf) !void {4626fn initAndAllocateSegments(self: *Elf) !void {
4639 const ehsize: u64 = switch (self.ptr_width) {4627 const ehsize: u64 = switch (self.ptr_width) {
4640 .p32 => @sizeOf(elf.Elf32_Ehdr),4628 .p32 => @sizeOf(elf.Elf32_Ehdr),
...@@ -4658,13 +4646,16 @@ fn initAndAllocateSegments(self: *Elf) !void {...@@ -4658,13 +4646,16 @@ fn initAndAllocateSegments(self: *Elf) !void {
4658}4646}
46594647
4660fn allocateNonAllocSections(self: *Elf) void {4648fn allocateNonAllocSections(self: *Elf) void {
4661 var off: u64 = 0;4649 for (self.shdrs.items) |*shdr| {
4662 for (self.shdrs.items) |shdr| {
4663 if (shdr.sh_type == elf.SHT_NULL) continue;4650 if (shdr.sh_type == elf.SHT_NULL) continue;
4664 if (shdr.sh_flags & elf.SHF_ALLOC == 0) break;4651 if (shdr.sh_flags & elf.SHF_ALLOC != 0) continue;
4665 off = @max(off, shdr.sh_offset + shdr.sh_size);4652 const needed_size = shdr.sh_size;
4653 if (needed_size > self.allocatedSize(shdr.sh_offset)) {
4654 shdr.sh_size = 0;
4655 shdr.sh_offset = self.findFreeSpace(needed_size, shdr.sh_addralign);
4656 shdr.sh_size = needed_size;
4657 }
4666 }4658 }
4667 self.allocateNonAllocSectionsInFile(off);
4668}4659}
46694660
4670fn allocateSpecialPhdrs(self: *Elf) void {4661fn allocateSpecialPhdrs(self: *Elf) void {