diff options
| author | Alexandre Janon <alex14fr@gmail.com> | 2024-04-28 11:45:50 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-28 11:45:50 +0200 |
| commit | 25f1526fe6424cef156724977b75a5b80a3d5833 (patch) | |
| tree | 5caffcd72f5baac64fe99482fc511db79525d77e /src | |
| parent | aecd9cc6d152443dc7c02dfe373be654d8adae64 (diff) | |
| download | zig-25f1526fe6424cef156724977b75a5b80a3d5833.tar.gz zig-25f1526fe6424cef156724977b75a5b80a3d5833.zip | |
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).
Diffstat (limited to 'src')
| -rw-r--r-- | src/link/Elf.zig | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 529aad8dcb..86b8def937 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -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, |
