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 {...@@ -623,7 +623,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
623 const ptr_size = self.ptrWidthBytes();623 const ptr_size = self.ptrWidthBytes();
624 const target = self.base.comp.root_mod.resolved_target.result;624 const target = self.base.comp.root_mod.resolved_target.result;
625 const ptr_bit_width = target.ptrBitWidth();625 const ptr_bit_width = target.ptrBitWidth();
626 const is_linux = target.os.tag == .linux;626 const has_os = target.os.tag != .freestanding;
627 const zig_object = self.zigObjectPtr().?;627 const zig_object = self.zigObjectPtr().?;
628628
629 const fillSection = struct {629 const fillSection = struct {
...@@ -661,7 +661,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -661,7 +661,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
661 if (self.phdr_zig_got_index == null) {661 if (self.phdr_zig_got_index == null) {
662 // We really only need ptr alignment but since we are using PROGBITS, linux requires662 // We really only need ptr alignment but since we are using PROGBITS, linux requires
663 // page align.663 // 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);
665 const filesz = @as(u64, ptr_size) * options.symbol_count_hint;665 const filesz = @as(u64, ptr_size) * options.symbol_count_hint;
666 const off = self.findFreeSpace(filesz, alignment);666 const off = self.findFreeSpace(filesz, alignment);
667 self.phdr_zig_got_index = try self.addPhdr(.{667 self.phdr_zig_got_index = try self.addPhdr(.{
...@@ -676,7 +676,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -676,7 +676,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
676 }676 }
677677
678 if (self.phdr_zig_load_ro_index == null) {678 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);
680 const filesz: u64 = 1024;680 const filesz: u64 = 1024;
681 const off = self.findFreeSpace(filesz, alignment);681 const off = self.findFreeSpace(filesz, alignment);
682 self.phdr_zig_load_ro_index = try self.addPhdr(.{682 self.phdr_zig_load_ro_index = try self.addPhdr(.{
...@@ -691,7 +691,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -691,7 +691,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
691 }691 }
692692
693 if (self.phdr_zig_load_rw_index == null) {693 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);
695 const filesz: u64 = 1024;695 const filesz: u64 = 1024;
696 const off = self.findFreeSpace(filesz, alignment);696 const off = self.findFreeSpace(filesz, alignment);
697 self.phdr_zig_load_rw_index = try self.addPhdr(.{697 self.phdr_zig_load_rw_index = try self.addPhdr(.{
...@@ -706,7 +706,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {...@@ -706,7 +706,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
706 }706 }
707707
708 if (self.phdr_zig_load_zerofill_index == null) {708 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);
710 self.phdr_zig_load_zerofill_index = try self.addPhdr(.{710 self.phdr_zig_load_zerofill_index = try self.addPhdr(.{
711 .type = elf.PT_LOAD,711 .type = elf.PT_LOAD,
712 .addr = if (ptr_bit_width >= 32) 0x14000000 else 0xf000,712 .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 {...@@ -601,7 +601,10 @@ fn outputType(elf_file: *Elf) u2 {
601 return switch (elf_file.base.comp.config.output_mode) {601 return switch (elf_file.base.comp.config.output_mode) {
602 .Obj => unreachable,602 .Obj => unreachable,
603 .Lib => 0,603 .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 },
605 };608 };
606}609}
607610