mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
Fix ELF alignment for freestanding targets (#19766)
* Fix the ELF binaries for freestanding target created with the self-hosted linker.
The ELF specification (generic ABI) states that ``loadable process segments must have congruent
values for p_vaddr and p_offset, modulo the page size''. Linux refuses to load binaries that
don't meet this requirement (execve() fails with EINVAL).
This commit is contained in:
parent
ef9fb428b7
commit
704f8f4013
1 changed files with 4 additions and 7 deletions
|
|
@ -648,7 +648,6 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
|
|||
const ptr_size = self.ptrWidthBytes();
|
||||
const target = self.base.comp.root_mod.resolved_target.result;
|
||||
const ptr_bit_width = target.ptrBitWidth();
|
||||
const has_os = target.os.tag != .freestanding;
|
||||
const zig_object = self.zigObjectPtr().?;
|
||||
|
||||
const fillSection = struct {
|
||||
|
|
@ -684,9 +683,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
|
|||
}
|
||||
|
||||
if (self.phdr_zig_got_index == null) {
|
||||
// We really only need ptr alignment but since we are using PROGBITS, linux requires
|
||||
// page align.
|
||||
const alignment = if (has_os) self.page_size else @as(u16, ptr_size);
|
||||
const alignment = self.page_size;
|
||||
const filesz = @as(u64, ptr_size) * options.symbol_count_hint;
|
||||
const off = self.findFreeSpace(filesz, alignment);
|
||||
self.phdr_zig_got_index = try self.addPhdr(.{
|
||||
|
|
@ -701,7 +698,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
|
|||
}
|
||||
|
||||
if (self.phdr_zig_load_ro_index == null) {
|
||||
const alignment = if (has_os) self.page_size else @as(u16, ptr_size);
|
||||
const alignment = self.page_size;
|
||||
const filesz: u64 = 1024;
|
||||
const off = self.findFreeSpace(filesz, alignment);
|
||||
self.phdr_zig_load_ro_index = try self.addPhdr(.{
|
||||
|
|
@ -716,7 +713,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
|
|||
}
|
||||
|
||||
if (self.phdr_zig_load_rw_index == null) {
|
||||
const alignment = if (has_os) self.page_size else @as(u16, ptr_size);
|
||||
const alignment = self.page_size;
|
||||
const filesz: u64 = 1024;
|
||||
const off = self.findFreeSpace(filesz, alignment);
|
||||
self.phdr_zig_load_rw_index = try self.addPhdr(.{
|
||||
|
|
@ -731,7 +728,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
|
|||
}
|
||||
|
||||
if (self.phdr_zig_load_zerofill_index == null) {
|
||||
const alignment = if (has_os) self.page_size else @as(u16, ptr_size);
|
||||
const alignment = self.page_size;
|
||||
self.phdr_zig_load_zerofill_index = try self.addPhdr(.{
|
||||
.type = elf.PT_LOAD,
|
||||
.addr = if (ptr_bit_width >= 32) 0x14000000 else 0xf000,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue