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 {...@@ -520,7 +520,6 @@ pub fn resolveSymbols(self: *Dylib, macho_file: *MachO) void {
520 global.nlist_idx = 0;520 global.nlist_idx = 0;
521 global.file = self.index;521 global.file = self.index;
522 global.flags.weak = flags.weak;522 global.flags.weak = flags.weak;
523 global.flags.weak_ref = false;
524 global.flags.tlv = flags.tlv;523 global.flags.tlv = flags.tlv;
525 global.flags.dyn_ref = false;524 global.flags.dyn_ref = false;
526 global.flags.tentative = false;525 global.flags.tentative = false;
...@@ -534,9 +533,11 @@ pub fn resetGlobals(self: *Dylib, macho_file: *MachO) void {...@@ -534,9 +533,11 @@ pub fn resetGlobals(self: *Dylib, macho_file: *MachO) void {
534 const sym = macho_file.getSymbol(sym_index);533 const sym = macho_file.getSymbol(sym_index);
535 const name = sym.name;534 const name = sym.name;
536 const global = sym.flags.global;535 const global = sym.flags.global;
536 const weak_ref = sym.flags.weak_ref;
537 sym.* = .{};537 sym.* = .{};
538 sym.name = name;538 sym.name = name;
539 sym.flags.global = global;539 sym.flags.global = global;
540 sym.flags.weak_ref = weak_ref;
540 }541 }
541}542}
542543
src/link/MachO/Object.zig+5
...@@ -525,6 +525,9 @@ fn initSymbols(self: *Object, macho_file: *MachO) !void {...@@ -525,6 +525,9 @@ fn initSymbols(self: *Object, macho_file: *MachO) !void {
525 const off = try macho_file.strings.insert(gpa, name);525 const off = try macho_file.strings.insert(gpa, name);
526 const gop = try macho_file.getOrCreateGlobal(off);526 const gop = try macho_file.getOrCreateGlobal(off);
527 self.symbols.addOneAssumeCapacity().* = gop.index;527 self.symbols.addOneAssumeCapacity().* = gop.index;
528 if (nlist.undf() and nlist.weakRef()) {
529 macho_file.getSymbol(gop.index).flags.weak_ref = true;
530 }
528 continue;531 continue;
529 }532 }
530533
...@@ -1099,9 +1102,11 @@ pub fn resetGlobals(self: *Object, macho_file: *MachO) void {...@@ -1099,9 +1102,11 @@ pub fn resetGlobals(self: *Object, macho_file: *MachO) void {
1099 const sym = macho_file.getSymbol(sym_index);1102 const sym = macho_file.getSymbol(sym_index);
1100 const name = sym.name;1103 const name = sym.name;
1101 const global = sym.flags.global;1104 const global = sym.flags.global;
1105 const weak_ref = sym.flags.weak_ref;
1102 sym.* = .{};1106 sym.* = .{};
1103 sym.name = name;1107 sym.name = name;
1104 sym.flags.global = global;1108 sym.flags.global = global;
1109 sym.flags.weak_ref = weak_ref;
1105 }1110 }
1106}1111}
11071112
src/link/MachO/ZigObject.zig+2
...@@ -233,9 +233,11 @@ pub fn resetGlobals(self: *ZigObject, macho_file: *MachO) void {...@@ -233,9 +233,11 @@ pub fn resetGlobals(self: *ZigObject, macho_file: *MachO) void {
233 const sym = macho_file.getSymbol(sym_index);233 const sym = macho_file.getSymbol(sym_index);
234 const name = sym.name;234 const name = sym.name;
235 const global = sym.flags.global;235 const global = sym.flags.global;
236 const weak_ref = sym.flags.weak_ref;
236 sym.* = .{};237 sym.* = .{};
237 sym.name = name;238 sym.name = name;
238 sym.flags.global = global;239 sym.flags.global = global;
240 sym.flags.weak_ref = weak_ref;
239 }241 }
240}242}
241243
test/link/macho.zig+24
...@@ -43,6 +43,11 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {...@@ -43,6 +43,11 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
43 macho_step.dependOn(testUnwindInfoNoSubsectionsX64(b, .{ .target = x86_64_target }));43 macho_step.dependOn(testUnwindInfoNoSubsectionsX64(b, .{ .target = x86_64_target }));
44 macho_step.dependOn(testUnwindInfoNoSubsectionsArm64(b, .{ .target = aarch64_target }));44 macho_step.dependOn(testUnwindInfoNoSubsectionsArm64(b, .{ .target = aarch64_target }));
45 macho_step.dependOn(testWeakBind(b, .{ .target = x86_64_target }));45 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
47 // Tests requiring symlinks when tested on Windows52 // Tests requiring symlinks when tested on Windows
48 if (build_opts.has_symlinks_windows) {53 if (build_opts.has_symlinks_windows) {
...@@ -2223,6 +2228,25 @@ fn testWeakLibrary(b: *Build, opts: Options) *Step {...@@ -2223,6 +2228,25 @@ fn testWeakLibrary(b: *Build, opts: Options) *Step {
2223 return test_step;2228 return test_step;
2224}2229}
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
2226fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {2250fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
2227 return link.addTestStep(b, "macho-" ++ prefix, opts);2251 return link.addTestStep(b, "macho-" ++ prefix, opts);
2228}2252}