Skip to content

Commit

Permalink
link/MachO: Avoid depending on host PATH_MAX
Browse files Browse the repository at this point in the history
Repeat of a4eb221 for the newly-synchronized zld code.

Restores ability to compile Zig for WASI.
  • Loading branch information
topolarity authored and andrewrk committed Oct 31, 2022
1 parent d022426 commit 6f2408a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/link/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2982,7 +2982,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
}
}

inline fn calcInstallNameLen(cmd_size: u64, name: []const u8, assume_max_path_len: bool) u64 {
pub inline fn calcInstallNameLen(cmd_size: u64, name: []const u8, assume_max_path_len: bool) u64 {
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));
Expand Down
13 changes: 4 additions & 9 deletions src/link/MachO/zld.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1516,11 +1516,6 @@ pub const Zld = struct {
}
}

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;
return mem.alignForwardGeneric(u64, cmd_size + name_len, @alignOf(u64));
}

fn calcLCsSize(self: *Zld, assume_max_path_len: bool) !u32 {
const gpa = self.gpa;

Expand All @@ -1542,7 +1537,7 @@ pub const Zld = struct {
// LC_DYSYMTAB
sizeofcmds += @sizeOf(macho.dysymtab_command);
// LC_LOAD_DYLINKER
sizeofcmds += calcInstallNameLen(
sizeofcmds += MachO.calcInstallNameLen(
@sizeOf(macho.dylinker_command),
mem.sliceTo(MachO.default_dyld_path, 0),
false,
Expand All @@ -1555,7 +1550,7 @@ pub const Zld = struct {
if (self.options.output_mode == .Lib) {
sizeofcmds += blk: {
const install_name = self.options.install_name orelse self.options.emit.?.sub_path;
break :blk calcInstallNameLen(
break :blk MachO.calcInstallNameLen(
@sizeOf(macho.dylib_command),
install_name,
assume_max_path_len,
Expand All @@ -1567,7 +1562,7 @@ pub const Zld = struct {
var it = RpathIterator.init(gpa, self.options.rpath_list);
defer it.deinit();
while (try it.next()) |rpath| {
sizeofcmds += calcInstallNameLen(
sizeofcmds += MachO.calcInstallNameLen(
@sizeOf(macho.rpath_command),
rpath,
assume_max_path_len,
Expand All @@ -1584,7 +1579,7 @@ pub const Zld = struct {
for (self.referenced_dylibs.keys()) |id| {
const dylib = self.dylibs.items[id];
const dylib_id = dylib.id orelse unreachable;
sizeofcmds += calcInstallNameLen(
sizeofcmds += MachO.calcInstallNameLen(
@sizeOf(macho.dylib_command),
dylib_id.name,
assume_max_path_len,
Expand Down

0 comments on commit 6f2408a

Please # to comment.