Skip to content

Conversation

@kubkon
Copy link
Member

@kubkon kubkon commented Aug 10, 2022

When parsing dylibs (binary files as opposed to text-based stubs), every __LINKEDIT data section has to be aligned to a minimum of @alignOf(u64) as otherwise dyld will refuse to load it, which means we don't have to relax the requirements for dylib parsing.

Closes #12387

@kubkon
Copy link
Member Author

kubkon commented Aug 10, 2022

@andrewrk could you locally rebase #12368 on top of this branch, and verify this fixes #12387 for you? I'll also double check on my 10.15.7 macOS in the meantime.

@andrewrk
Copy link
Member

@andrewrk could you locally rebase #12368 on top of this branch, and verify this fixes #12387 for you? I'll also double check on my 10.15.7 macOS in the meantime.

Counter-proposal, let's fix it in master branch and then I will rebase #12368 when it is landed.

@kubkon
Copy link
Member Author

kubkon commented Aug 10, 2022

@andrewrk could you locally rebase #12368 on top of this branch, and verify this fixes #12387 for you? I'll also double check on my 10.15.7 macOS in the meantime.

Counter-proposal, let's fix it in master branch and then I will rebase #12368 when it is landed.

That's what I meant actually. Just wanted for you to locally test it with stage3-default branch on your x86 macOS host that it actually did fix the issue :-)

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed that it solves the issue for me locally 👍

@kubkon
Copy link
Member Author

kubkon commented Aug 10, 2022

Confirmed that it solves the issue for me locally 👍

Excellent!

@andrewrk andrewrk merged commit 45c444f into master Aug 10, 2022
@andrewrk andrewrk deleted the fix-12387 branch August 10, 2022 22:45
@alignCast(@alignOf(macho.nlist_64), &self.contents[symtab.symoff]),
)[0..symtab.nsyms];
// Sadly, SYMTAB may be at an unaligned offset within the object file.
self.in_symtab = @alignCast(@alignOf(macho.nlist_64), @ptrCast(
Copy link
Member

@andrewrk andrewrk Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't know how I missed this before but these @alignCast are doing the exact same assertion they were before. This patch effectively does nothing. I have no idea why the stage3 build completed successfully for me, but anyway now it is panicking again. I'll push the fix soon 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm surprised as this fixed it for me locally. I'll re-do the tests then.

@andrewrk
Copy link
Member

andrewrk commented Aug 11, 2022

btw could it be perhaps that the objects in question are aligned properly, however, the readToEndAlloc call is only using 8 bytes alignment instead of page alignment?

edit: nope, I tested this hypothesis:

--- a/src/link/MachO.zig
+++ b/src/link/MachO.zig
@@ -56,6 +56,10 @@ pub const SearchStrategy = enum {

 pub const N_DESC_GCED: u16 = @bitCast(u16, @as(i16, -1));

+// x86_64 is 4 KB
+// aarch64 is 16 KB
+const max_page_size = 16 * 1024;
+
 const SystemLib = struct {
     needed: bool = false,
     weak: bool = false,
@@ -1384,7 +1388,7 @@ fn parseObject(self: *MachO, path: []const u8) !bool {
     };
     const file_stat = try file.stat();
     const file_size = math.cast(usize, file_stat.size) orelse return error.Overflow;
-    const contents = try file.readToEndAllocOptions(gpa, file_size, file_size, @alignOf(u64), null);
+    const contents = try file.readToEndAllocOptions(gpa, file_size, file_size, max_page_size, null);

     var object = Object{
         .name = name,
@@ -1495,7 +1499,7 @@ pub fn parseDylib(
     try file.seekTo(fat_offset);
     file_size -= fat_offset;

-    const contents = try file.readToEndAllocOptions(gpa, file_size, file_size, @alignOf(u64), null);
+    const contents = try file.readToEndAllocOptions(gpa, file_size, file_size, max_page_size, null);
     defer gpa.free(contents);

     const dylib_id = @intCast(u16, self.dylibs.items.len);

same issue:

MachO Flush... thread 7608896 panic: incorrect alignment
/Users/andy/dev/zig/src/link/MachO/Object.zig:104:72: 0x10d2811ec in link.MachO.Object.parse (zig1)
                    @alignCast(@alignOf(macho.nlist_64), &self.contents[symtab.symoff]),
                                                                       ^

@kubkon
Copy link
Member Author

kubkon commented Aug 11, 2022

As you discovered, it's not the problem with alignment of the entire object file, but with misaligned symtab that is not always aligned to 8 bytes for some reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stage2 unable to build stage3 on x86_64 macos: panic: incorrect alignment

2 participants