authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-27 19:41:59+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-27 19:41:59+02:00
log8b7255c22a9dcdd9396bbd1f7c212a5c2be71dd0
tree1701fc448ca07573e888fc7ee8521302db02bda2
parent111349f83c456200c131c222d06cdf83fa62ac54

elf: hint allocateSegment where to put the segment at


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

src/link/Elf.zig+25-8
......@@ -413,6 +413,7 @@ fn findFreeSpace(self: *Elf, object_size: u64, min_alignment: u64) u64 {
413413const AllocateSegmentOpts = struct {
414414 size: u64,
415415 alignment: u64,
416 addr: ?u64 = null,
416417 flags: u32 = elf.PF_R,
417418};
418419
......@@ -423,8 +424,8 @@ pub fn allocateSegment(self: *Elf, opts: AllocateSegmentOpts) error{OutOfMemory}
423424 // Memory is always allocated in sequence.
424425 // TODO is this correct? Or should we implement something similar to `findFreeSpace`?
425426 // How would that impact HCS?
426 const addr = blk: {
427 assert(self.phdr_table_load_index != null);
427 const addr = opts.addr orelse blk: {
428 assert(index >= 1);
428429 const phdr = &self.phdrs.items[index - 1];
429430 const increased_size = padToIdeal(phdr.p_vaddr + phdr.p_memsz);
430431 break :blk mem.alignForward(u64, increased_size, opts.alignment);
......@@ -532,6 +533,7 @@ pub fn populateMissingMetadata(self: *Elf) !void {
532533 .p64 => false,
533534 };
534535 const ptr_size: u8 = self.ptrWidthBytes();
536 const is_linux = self.base.options.target.os.tag == .linux;
535537 const image_base = self.calcImageBase();
536538
537539 if (self.phdr_table_index == null) {
......@@ -570,6 +572,7 @@ pub fn populateMissingMetadata(self: *Elf) !void {
570572
571573 if (self.phdr_load_re_index == null) {
572574 self.phdr_load_re_index = try self.allocateSegment(.{
575 .addr = self.defaultEntryAddress(),
573576 .size = self.base.options.program_code_size_hint,
574577 .alignment = self.page_size,
575578 .flags = elf.PF_X | elf.PF_R | elf.PF_W,
......@@ -578,10 +581,12 @@ pub fn populateMissingMetadata(self: *Elf) !void {
578581 }
579582
580583 if (self.phdr_got_index == null) {
584 const addr: u64 = if (ptr_size >= 4) 0x4000000 else 0x8000;
581585 // We really only need ptr alignment but since we are using PROGBITS, linux requires
582586 // page align.
583 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);
587 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
584588 self.phdr_got_index = try self.allocateSegment(.{
589 .addr = addr,
585590 .size = @as(u64, ptr_size) * self.base.options.symbol_count_hint,
586591 .alignment = alignment,
587592 .flags = elf.PF_R | elf.PF_W,
......@@ -589,9 +594,10 @@ pub fn populateMissingMetadata(self: *Elf) !void {
589594 }
590595
591596 if (self.phdr_load_ro_index == null) {
592 // Same reason as for GOT
593 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);
597 const addr: u64 = if (ptr_size >= 4) 0xc000000 else 0xa000;
598 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
594599 self.phdr_load_ro_index = try self.allocateSegment(.{
600 .addr = addr,
595601 .size = 1024,
596602 .alignment = alignment,
597603 .flags = elf.PF_R | elf.PF_W,
......@@ -599,9 +605,10 @@ pub fn populateMissingMetadata(self: *Elf) !void {
599605 }
600606
601607 if (self.phdr_load_rw_index == null) {
602 // Same reason as for GOT
603 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);
608 const addr: u64 = if (ptr_size >= 4) 0x10000000 else 0xc000;
609 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
604610 self.phdr_load_rw_index = try self.allocateSegment(.{
611 .addr = addr,
605612 .size = 1024,
606613 .alignment = alignment,
607614 .flags = elf.PF_R | elf.PF_W,
......@@ -609,8 +616,10 @@ pub fn populateMissingMetadata(self: *Elf) !void {
609616 }
610617
611618 if (self.phdr_load_zerofill_index == null) {
612 const alignment = if (self.base.options.target.os.tag == .linux) self.page_size else @as(u16, ptr_size);
619 const addr: u64 = if (ptr_size >= 4) 0x14000000 else 0xf000;
620 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
613621 self.phdr_load_zerofill_index = try self.allocateSegment(.{
622 .addr = addr,
614623 .size = 0,
615624 .alignment = alignment,
616625 .flags = elf.PF_R | elf.PF_W,
......@@ -3801,6 +3810,14 @@ pub fn calcImageBase(self: Elf) u64 {
38013810 };
38023811}
38033812
3813pub fn defaultEntryAddress(self: Elf) u64 {
3814 if (self.entry_addr) |addr| return addr;
3815 return switch (self.base.options.target.cpu.arch) {
3816 .spu_2 => 0,
3817 else => default_entry_addr,
3818 };
3819}
3820
38043821pub fn isDynLib(self: Elf) bool {
38053822 return self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic;
38063823}