Skip to content

macho linking uses incorrect value for headerpad_max_install_names #13229

Description

@andrewrk

This value, std.os.PATH_MAX applies to the host target. However the linker is supposed to be linking for the target given to the compiler at runtime.

const name_len = if (assume_max_path_len) std.os.PATH_MAX else std.mem.len(name) + 1;

This bug is revealed clearly when compiling for WASI which has no std.os.PATH_MAX value, yet should still be able to cross-compile macos binaries:

$ stage3/bin/zig build -p wasi -Dskip-install-lib-files -Dtarget=wasm32-wasi
/home/andy/Downloads/zig/lib/std/os.zig:110:28: error: root struct of file 'os.wasi' has no member named 'PATH_MAX'
pub const PATH_MAX = system.PATH_MAX;
                     ~~~~~~^~~~~~~~~
referenced by:
    calcInstallNameLen: /home/andy/Downloads/zig/src/link/MachO.zig:3540:53
    calcLCsSize: /home/andy/Downloads/zig/src/link/MachO.zig:3577:24
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

In theory we should be able to use instead something like std.os.darwin.PATH_MAX however the std lib is organized in a way that only makes that work if the host matches.

A quickfix instead would look something like this:

--- a/src/link/MachO.zig
+++ b/src/link/MachO.zig
@@ -3537,7 +3537,8 @@ pub fn populateMissingMetadata(self: *MachO) !void {
 }
 
 inline fn calcInstallNameLen(cmd_size: u64, name: []const u8, assume_max_path_len: bool) u64 {
-    const name_len = if (assume_max_path_len) std.os.PATH_MAX else std.mem.len(name) + 1;
+    const darwin_path_max = 1024;
+    const name_len = if (assume_max_path_len) darwin_path_max else std.mem.len(name) + 1;
     return mem.alignForwardGeneric(u64, cmd_size + name_len, @alignOf(u64));
 }

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.linkingos-macosmacOS

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions