Skip to content

Commit

Permalink
macho: allow undefined symbols in dylibs
Browse files Browse the repository at this point in the history
We now respect both `-fallow-shlib-undefined` and
`-Wl,"-undefined=dynamic_lookup"` flags. This is the first step
towards solving issues #8180 and #3000. We currently do not expose
any other ld64 equivalent flag for `-undefined` flag - we basically
throw an error should the user specify a different flag. Support for
those is conditional on closing #8180. As a result of this change,
it is now possible to generate a valid native Node.js addon with Zig
for macOS.
  • Loading branch information
kubkon committed Dec 11, 2021
1 parent 75f3e7a commit 411723f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/link/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
const is_dyn_lib = self.base.options.link_mode == .Dynamic and is_lib;
const is_exe_or_dyn_lib = is_dyn_lib or self.base.options.output_mode == .Exe;
const stack_size = self.base.options.stack_size_override orelse 0;
const allow_undef = is_dyn_lib and (self.base.options.allow_shlib_undefined orelse false);

const id_symlink_basename = "zld.id";

Expand Down Expand Up @@ -847,6 +848,11 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
try argv.append(try std.fmt.allocPrint(arena, "-F{s}", .{framework_dir}));
}

if (allow_undef) {
try argv.append("-undefined");
try argv.append("dynamic_lookup");
}

Compilation.dump_argv(argv.items);
}

Expand Down Expand Up @@ -899,6 +905,16 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
};
_ = self.unresolved.swapRemove(resolv.where_index);
continue;
} else if (allow_undef) {
const n_desc = @bitCast(
u16,
macho.BIND_SPECIAL_DYLIB_FLAT_LOOKUP * @intCast(i16, macho.N_SYMBOL_RESOLVER),
);
// TODO allow_shlib_undefined is an ELF flag so figure out macOS specific flags too.
sym.n_type = macho.N_EXT;
sym.n_desc = n_desc;
_ = self.unresolved.swapRemove(resolv.where_index);
continue;
}

log.err("undefined reference to symbol '{s}'", .{sym_name});
Expand Down Expand Up @@ -5111,7 +5127,7 @@ fn writeDyldInfoData(self: *MachO) !void {
try bind_pointers.append(.{
.offset = binding.offset + base_offset,
.segment_id = match.seg,
.dylib_ordinal = @divExact(bind_sym.n_desc, macho.N_SYMBOL_RESOLVER),
.dylib_ordinal = @divExact(@bitCast(i16, bind_sym.n_desc), macho.N_SYMBOL_RESOLVER),
.name = self.getString(bind_sym.n_strx),
});
},
Expand All @@ -5133,7 +5149,7 @@ fn writeDyldInfoData(self: *MachO) !void {
try lazy_bind_pointers.append(.{
.offset = binding.offset + base_offset,
.segment_id = match.seg,
.dylib_ordinal = @divExact(bind_sym.n_desc, macho.N_SYMBOL_RESOLVER),
.dylib_ordinal = @divExact(@bitCast(i16, bind_sym.n_desc), macho.N_SYMBOL_RESOLVER),
.name = self.getString(bind_sym.n_strx),
});
},
Expand Down
10 changes: 10 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,16 @@ fn buildOutputType(
}
emit_implib = .{ .yes = linker_args.items[i] };
emit_implib_arg_provided = true;
} else if (mem.eql(u8, arg, "-undefined")) {
i += 1;
if (i >= linker_args.items.len) {
fatal("expected linker arg after '{s}'", .{arg});
}
if (mem.eql(u8, "dynamic_lookup", linker_args.items[i])) {
linker_allow_shlib_undefined = true;
} else {
fatal("unsupported -undefined option '{s}'", .{linker_args.items[i]});
}
} else {
warn("unsupported linker arg: {s}", .{arg});
}
Expand Down

0 comments on commit 411723f

Please sign in to comment.