authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-30 00:08:18+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-30 13:56:08+01:00
logb8490c05c10193363107a1fb2e7c4ffab287f5ad
tree00b05680a984e2dfad2f4f1bec815574763bd90f
parenta2ad8517eea597eb9d7215aef831a86ffd16d1b3

macho: improve weak-ref symbols handling


4 files changed, 33 insertions(+), 1 deletions(-)

src/link/MachO/Dylib.zig+2-1
......@@ -520,7 +520,6 @@ pub fn resolveSymbols(self: *Dylib, macho_file: *MachO) void {
520520 global.nlist_idx = 0;
521521 global.file = self.index;
522522 global.flags.weak = flags.weak;
523 global.flags.weak_ref = false;
524523 global.flags.tlv = flags.tlv;
525524 global.flags.dyn_ref = false;
526525 global.flags.tentative = false;
......@@ -534,9 +533,11 @@ pub fn resetGlobals(self: *Dylib, macho_file: *MachO) void {
534533 const sym = macho_file.getSymbol(sym_index);
535534 const name = sym.name;
536535 const global = sym.flags.global;
536 const weak_ref = sym.flags.weak_ref;
537537 sym.* = .{};
538538 sym.name = name;
539539 sym.flags.global = global;
540 sym.flags.weak_ref = weak_ref;
540541 }
541542}
542543
src/link/MachO/Object.zig+5
......@@ -525,6 +525,9 @@ fn initSymbols(self: *Object, macho_file: *MachO) !void {
525525 const off = try macho_file.strings.insert(gpa, name);
526526 const gop = try macho_file.getOrCreateGlobal(off);
527527 self.symbols.addOneAssumeCapacity().* = gop.index;
528 if (nlist.undf() and nlist.weakRef()) {
529 macho_file.getSymbol(gop.index).flags.weak_ref = true;
530 }
528531 continue;
529532 }
530533
......@@ -1099,9 +1102,11 @@ pub fn resetGlobals(self: *Object, macho_file: *MachO) void {
10991102 const sym = macho_file.getSymbol(sym_index);
11001103 const name = sym.name;
11011104 const global = sym.flags.global;
1105 const weak_ref = sym.flags.weak_ref;
11021106 sym.* = .{};
11031107 sym.name = name;
11041108 sym.flags.global = global;
1109 sym.flags.weak_ref = weak_ref;
11051110 }
11061111}
11071112
src/link/MachO/ZigObject.zig+2
......@@ -233,9 +233,11 @@ pub fn resetGlobals(self: *ZigObject, macho_file: *MachO) void {
233233 const sym = macho_file.getSymbol(sym_index);
234234 const name = sym.name;
235235 const global = sym.flags.global;
236 const weak_ref = sym.flags.weak_ref;
236237 sym.* = .{};
237238 sym.name = name;
238239 sym.flags.global = global;
240 sym.flags.weak_ref = weak_ref;
239241 }
240242}
241243
test/link/macho.zig+24
......@@ -43,6 +43,11 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
4343 macho_step.dependOn(testUnwindInfoNoSubsectionsX64(b, .{ .target = x86_64_target }));
4444 macho_step.dependOn(testUnwindInfoNoSubsectionsArm64(b, .{ .target = aarch64_target }));
4545 macho_step.dependOn(testWeakBind(b, .{ .target = x86_64_target }));
46 macho_step.dependOn(testWeakRef(b, .{ .target = b.resolveTargetQuery(.{
47 .cpu_arch = .x86_64,
48 .os_tag = .macos,
49 .os_version_min = .{ .semver = .{ .major = 10, .minor = 13, .patch = 0 } },
50 }) }));
4651
4752 // Tests requiring symlinks when tested on Windows
4853 if (build_opts.has_symlinks_windows) {
......@@ -2223,6 +2228,25 @@ fn testWeakLibrary(b: *Build, opts: Options) *Step {
22232228 return test_step;
22242229}
22252230
2231fn testWeakRef(b: *Build, opts: Options) *Step {
2232 const test_step = addTestStep(b, "macho-weak-ref", opts);
2233
2234 const exe = addExecutable(b, opts, .{ .name = "main", .c_source_bytes =
2235 \\#include <stdio.h>
2236 \\#include <sys/_types/_fd_def.h>
2237 \\int main(int argc, char** argv) {
2238 \\ printf("__darwin_check_fd_set_overflow: %p\n", __darwin_check_fd_set_overflow);
2239 \\}
2240 });
2241
2242 const check = exe.checkObject();
2243 check.checkInSymtab();
2244 check.checkExact("(undefined) weakref external ___darwin_check_fd_set_overflow (from libSystem.B)");
2245 test_step.dependOn(&check.step);
2246
2247 return test_step;
2248}
2249
22262250fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
22272251 return link.addTestStep(b, "macho-" ++ prefix, opts);
22282252}