| ... | ... | @@ -429,15 +429,15 @@ pub fn allocateSegment(self: *Elf, opts: AllocateSegmentOpts) error{OutOfMemory} |
| 429 | 429 | const addr = opts.addr orelse blk: { |
| 430 | 430 | const reserved_capacity = self.calcImageBase() * 4; |
| 431 | 431 | // Calculate largest VM address |
| 432 | | const count = self.phdrs.items.len; |
| 433 | 432 | var addresses = std.ArrayList(u64).init(gpa); |
| 434 | 433 | defer addresses.deinit(); |
| 435 | | try addresses.ensureTotalCapacityPrecise(count); |
| 434 | try addresses.ensureTotalCapacityPrecise(self.phdrs.items.len); |
| 436 | 435 | for (self.phdrs.items) |phdr| { |
| 436 | if (phdr.p_type != elf.PT_LOAD) continue; |
| 437 | 437 | addresses.appendAssumeCapacity(phdr.p_vaddr + reserved_capacity); |
| 438 | 438 | } |
| 439 | 439 | mem.sort(u64, addresses.items, {}, std.sort.asc(u64)); |
| 440 | | break :blk mem.alignForward(u64, addresses.items[count - 1], opts.alignment); |
| 440 | break :blk mem.alignForward(u64, addresses.pop(), opts.alignment); |
| 441 | 441 | }; |
| 442 | 442 | log.debug("allocating phdr({d})({c}{c}{c}) from 0x{x} to 0x{x} (0x{x} - 0x{x})", .{ |
| 443 | 443 | index, |
| ... | ... | @@ -543,7 +543,6 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 543 | 543 | }; |
| 544 | 544 | const ptr_size: u8 = self.ptrWidthBytes(); |
| 545 | 545 | const is_linux = self.base.options.target.os.tag == .linux; |
| 546 | | const large_addrspace = self.base.options.target.ptrBitWidth() >= 32; |
| 547 | 546 | const image_base = self.calcImageBase(); |
| 548 | 547 | |
| 549 | 548 | if (self.phdr_table_index == null) { |
| ... | ... | @@ -566,23 +565,16 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 566 | 565 | } |
| 567 | 566 | |
| 568 | 567 | if (self.phdr_table_load_index == null) { |
| 569 | | self.phdr_table_load_index = @intCast(self.phdrs.items.len); |
| 570 | | try self.phdrs.append(gpa, .{ |
| 571 | | .p_type = elf.PT_LOAD, |
| 572 | | .p_offset = 0, |
| 573 | | .p_filesz = 0, |
| 574 | | .p_vaddr = image_base, |
| 575 | | .p_paddr = image_base, |
| 576 | | .p_memsz = 0, |
| 577 | | .p_align = self.page_size, |
| 578 | | .p_flags = elf.PF_R, |
| 568 | self.phdr_table_load_index = try self.allocateSegment(.{ |
| 569 | .addr = image_base, |
| 570 | .size = 0, |
| 571 | .alignment = self.page_size, |
| 579 | 572 | }); |
| 580 | 573 | self.phdr_table_dirty = true; |
| 581 | 574 | } |
| 582 | 575 | |
| 583 | 576 | if (self.phdr_load_re_index == null) { |
| 584 | 577 | self.phdr_load_re_index = try self.allocateSegment(.{ |
| 585 | | .addr = self.defaultEntryAddress(), |
| 586 | 578 | .size = self.base.options.program_code_size_hint, |
| 587 | 579 | .alignment = self.page_size, |
| 588 | 580 | .flags = elf.PF_X | elf.PF_R | elf.PF_W, |
| ... | ... | @@ -591,12 +583,10 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 591 | 583 | } |
| 592 | 584 | |
| 593 | 585 | if (self.phdr_got_index == null) { |
| 594 | | const addr: u64 = if (large_addrspace) 0x4000000 else 0x8000; |
| 595 | 586 | // We really only need ptr alignment but since we are using PROGBITS, linux requires |
| 596 | 587 | // page align. |
| 597 | 588 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); |
| 598 | 589 | self.phdr_got_index = try self.allocateSegment(.{ |
| 599 | | .addr = addr, |
| 600 | 590 | .size = @as(u64, ptr_size) * self.base.options.symbol_count_hint, |
| 601 | 591 | .alignment = alignment, |
| 602 | 592 | .flags = elf.PF_R | elf.PF_W, |
| ... | ... | @@ -604,10 +594,8 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 604 | 594 | } |
| 605 | 595 | |
| 606 | 596 | if (self.phdr_load_ro_index == null) { |
| 607 | | const addr: u64 = if (large_addrspace) 0xc000000 else 0xa000; |
| 608 | 597 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); |
| 609 | 598 | self.phdr_load_ro_index = try self.allocateSegment(.{ |
| 610 | | .addr = addr, |
| 611 | 599 | .size = 1024, |
| 612 | 600 | .alignment = alignment, |
| 613 | 601 | .flags = elf.PF_R | elf.PF_W, |
| ... | ... | @@ -615,10 +603,8 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 615 | 603 | } |
| 616 | 604 | |
| 617 | 605 | if (self.phdr_load_rw_index == null) { |
| 618 | | const addr: u64 = if (large_addrspace) 0x10000000 else 0xc000; |
| 619 | 606 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); |
| 620 | 607 | self.phdr_load_rw_index = try self.allocateSegment(.{ |
| 621 | | .addr = addr, |
| 622 | 608 | .size = 1024, |
| 623 | 609 | .alignment = alignment, |
| 624 | 610 | .flags = elf.PF_R | elf.PF_W, |
| ... | ... | @@ -626,10 +612,8 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 626 | 612 | } |
| 627 | 613 | |
| 628 | 614 | if (self.phdr_load_zerofill_index == null) { |
| 629 | | const addr: u64 = if (large_addrspace) 0x14000000 else 0xf000; |
| 630 | 615 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); |
| 631 | 616 | self.phdr_load_zerofill_index = try self.allocateSegment(.{ |
| 632 | | .addr = addr, |
| 633 | 617 | .size = 0, |
| 634 | 618 | .alignment = alignment, |
| 635 | 619 | .flags = elf.PF_R | elf.PF_W, |
| ... | ... | @@ -3820,14 +3804,6 @@ pub fn calcImageBase(self: Elf) u64 { |
| 3820 | 3804 | }; |
| 3821 | 3805 | } |
| 3822 | 3806 | |
| 3823 | | pub fn defaultEntryAddress(self: Elf) u64 { |
| 3824 | | if (self.entry_addr) |addr| return addr; |
| 3825 | | return switch (self.base.options.target.cpu.arch) { |
| 3826 | | .spu_2 => 0, |
| 3827 | | else => default_entry_addr, |
| 3828 | | }; |
| 3829 | | } |
| 3830 | | |
| 3831 | 3807 | pub fn isDynLib(self: Elf) bool { |
| 3832 | 3808 | return self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic; |
| 3833 | 3809 | } |