Commit 6f2408aab8

Cody Tapscott <topolarity@tapscott.me>
2022-10-31 16:28:16
link/MachO: Avoid depending on host PATH_MAX
Repeat of a4eb221b9 for the newly-synchronized zld code. Restores ability to compile Zig for WASI.
1 parent d022426
Changed files (2)
src
link
src/link/MachO/zld.zig
@@ -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;
 
@@ -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,
@@ -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,
@@ -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,
@@ -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,
src/link/MachO.zig
@@ -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));