authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-21 09:56:26+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-04-21 09:56:26+02:00
loge0d7a98b72f54aa502ed43cda7c712962489ba54
treecc1d0bf5af98cac7024d22cbb1de9d44efd2ae07
parentc352845e888870172c88900144e243b3c1eed010
parent4daeffab4a2cb7a5fa7165370f9a26b1dc219fab
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #19710 from jacobly0/elf-segment-align

Elf: fix unaligned segments on non-linux

2 files changed, 9 insertions(+), 6 deletions(-)

src/link/Elf.zig+5-5
......@@ -623,7 +623,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
623623 const ptr_size = self.ptrWidthBytes();
624624 const target = self.base.comp.root_mod.resolved_target.result;
625625 const ptr_bit_width = target.ptrBitWidth();
626 const is_linux = target.os.tag == .linux;
626 const has_os = target.os.tag != .freestanding;
627627 const zig_object = self.zigObjectPtr().?;
628628
629629 const fillSection = struct {
......@@ -661,7 +661,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
661661 if (self.phdr_zig_got_index == null) {
662662 // We really only need ptr alignment but since we are using PROGBITS, linux requires
663663 // page align.
664 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
664 const alignment = if (has_os) self.page_size else @as(u16, ptr_size);
665665 const filesz = @as(u64, ptr_size) * options.symbol_count_hint;
666666 const off = self.findFreeSpace(filesz, alignment);
667667 self.phdr_zig_got_index = try self.addPhdr(.{
......@@ -676,7 +676,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
676676 }
677677
678678 if (self.phdr_zig_load_ro_index == null) {
679 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
679 const alignment = if (has_os) self.page_size else @as(u16, ptr_size);
680680 const filesz: u64 = 1024;
681681 const off = self.findFreeSpace(filesz, alignment);
682682 self.phdr_zig_load_ro_index = try self.addPhdr(.{
......@@ -691,7 +691,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
691691 }
692692
693693 if (self.phdr_zig_load_rw_index == null) {
694 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
694 const alignment = if (has_os) self.page_size else @as(u16, ptr_size);
695695 const filesz: u64 = 1024;
696696 const off = self.findFreeSpace(filesz, alignment);
697697 self.phdr_zig_load_rw_index = try self.addPhdr(.{
......@@ -706,7 +706,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
706706 }
707707
708708 if (self.phdr_zig_load_zerofill_index == null) {
709 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
709 const alignment = if (has_os) self.page_size else @as(u16, ptr_size);
710710 self.phdr_zig_load_zerofill_index = try self.addPhdr(.{
711711 .type = elf.PT_LOAD,
712712 .addr = if (ptr_bit_width >= 32) 0x14000000 else 0xf000,
src/link/Elf/Atom.zig+4-1
......@@ -601,7 +601,10 @@ fn outputType(elf_file: *Elf) u2 {
601601 return switch (elf_file.base.comp.config.output_mode) {
602602 .Obj => unreachable,
603603 .Lib => 0,
604 .Exe => if (comp.config.pie) 1 else 2,
604 .Exe => switch (elf_file.getTarget().os.tag) {
605 .haiku => 0,
606 else => if (comp.config.pie) 1 else 2,
607 },
605608 };
606609}
607610