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:
Alexandre Janon 2024-04-28 11:45:50 +02:00 committed by Andrew Kelley
parent ef9fb428b7
commit 704f8f4013

View file

@ -648,7 +648,6 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
const ptr_size = self.ptrWidthBytes(); const ptr_size = self.ptrWidthBytes();
const target = self.base.comp.root_mod.resolved_target.result; const target = self.base.comp.root_mod.resolved_target.result;
const ptr_bit_width = target.ptrBitWidth(); const ptr_bit_width = target.ptrBitWidth();
const has_os = target.os.tag != .freestanding;
const zig_object = self.zigObjectPtr().?; const zig_object = self.zigObjectPtr().?;
const fillSection = struct { const fillSection = struct {
@ -684,9 +683,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
} }
if (self.phdr_zig_got_index == null) { if (self.phdr_zig_got_index == null) {
// We really only need ptr alignment but since we are using PROGBITS, linux requires const alignment = self.page_size;
// page align.
const alignment = if (has_os) self.page_size else @as(u16, ptr_size);
const filesz = @as(u64, ptr_size) * options.symbol_count_hint; const filesz = @as(u64, ptr_size) * options.symbol_count_hint;
const off = self.findFreeSpace(filesz, alignment); const off = self.findFreeSpace(filesz, alignment);
self.phdr_zig_got_index = try self.addPhdr(.{ 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) { 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 filesz: u64 = 1024;
const off = self.findFreeSpace(filesz, alignment); const off = self.findFreeSpace(filesz, alignment);
self.phdr_zig_load_ro_index = try self.addPhdr(.{ 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) { 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 filesz: u64 = 1024;
const off = self.findFreeSpace(filesz, alignment); const off = self.findFreeSpace(filesz, alignment);
self.phdr_zig_load_rw_index = try self.addPhdr(.{ 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) { 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(.{ self.phdr_zig_load_zerofill_index = try self.addPhdr(.{
.type = elf.PT_LOAD, .type = elf.PT_LOAD,
.addr = if (ptr_bit_width >= 32) 0x14000000 else 0xf000, .addr = if (ptr_bit_width >= 32) 0x14000000 else 0xf000,