Commit 351031b6c7

Jakub Konka <kubkon@jakubkonka.com>
2022-11-06 15:19:14
macho: parse weak symbols in tbds
However, we will treat them as standard imports rather than refs to weak imports until I investigate more how it actually works underneath.
1 parent 1aeef29
Changed files (2)
src
src/link/MachO/Dylib.zig
@@ -22,7 +22,14 @@ weak: bool = false,
 /// Parsed symbol table represented as hash map of symbols'
 /// names. We can and should defer creating *Symbols until
 /// a symbol is referenced by an object file.
-symbols: std.StringArrayHashMapUnmanaged(void) = .{},
+///
+/// The value for each parsed symbol represents whether the
+/// symbol is defined as a weak symbol or strong.
+/// TODO when the referenced symbol is weak, ld64 marks it as
+/// N_REF_TO_WEAK but need to investigate if there's more to it
+/// such as weak binding entry or simply weak. For now, we generate
+/// standard bind or lazy bind.
+symbols: std.StringArrayHashMapUnmanaged(bool) = .{},
 
 pub const Id = struct {
     name: []const u8,
@@ -168,7 +175,7 @@ pub fn parseFromBinary(
                     if (!add_to_symtab) continue;
 
                     const sym_name = mem.sliceTo(@ptrCast([*:0]const u8, strtab.ptr + sym.n_strx), 0);
-                    try self.symbols.putNoClobber(allocator, try allocator.dupe(u8, sym_name), {});
+                    try self.symbols.putNoClobber(allocator, try allocator.dupe(u8, sym_name), false);
                 }
             },
             .ID_DYLIB => {
@@ -202,25 +209,30 @@ fn addObjCClassSymbol(self: *Dylib, allocator: Allocator, sym_name: []const u8)
 
     for (expanded) |sym| {
         if (self.symbols.contains(sym)) continue;
-        try self.symbols.putNoClobber(allocator, sym, {});
+        try self.symbols.putNoClobber(allocator, sym, false);
     }
 }
 
 fn addObjCIVarSymbol(self: *Dylib, allocator: Allocator, sym_name: []const u8) !void {
     const expanded = try std.fmt.allocPrint(allocator, "_OBJC_IVAR_$_{s}", .{sym_name});
     if (self.symbols.contains(expanded)) return;
-    try self.symbols.putNoClobber(allocator, expanded, {});
+    try self.symbols.putNoClobber(allocator, expanded, false);
 }
 
 fn addObjCEhTypeSymbol(self: *Dylib, allocator: Allocator, sym_name: []const u8) !void {
     const expanded = try std.fmt.allocPrint(allocator, "_OBJC_EHTYPE_$_{s}", .{sym_name});
     if (self.symbols.contains(expanded)) return;
-    try self.symbols.putNoClobber(allocator, expanded, {});
+    try self.symbols.putNoClobber(allocator, expanded, false);
 }
 
 fn addSymbol(self: *Dylib, allocator: Allocator, sym_name: []const u8) !void {
     if (self.symbols.contains(sym_name)) return;
-    try self.symbols.putNoClobber(allocator, try allocator.dupe(u8, sym_name), {});
+    try self.symbols.putNoClobber(allocator, try allocator.dupe(u8, sym_name), false);
+}
+
+fn addWeakSymbol(self: *Dylib, allocator: Allocator, sym_name: []const u8) !void {
+    if (self.symbols.contains(sym_name)) return;
+    try self.symbols.putNoClobber(allocator, try allocator.dupe(u8, sym_name), true);
 }
 
 const TargetMatcher = struct {
@@ -359,6 +371,12 @@ pub fn parseFromStub(
                             }
                         }
 
+                        if (exp.weak_symbols) |symbols| {
+                            for (symbols) |sym_name| {
+                                try self.addWeakSymbol(allocator, sym_name);
+                            }
+                        }
+
                         if (exp.objc_classes) |objc_classes| {
                             for (objc_classes) |class_name| {
                                 try self.addObjCClassSymbol(allocator, class_name);
@@ -402,6 +420,12 @@ pub fn parseFromStub(
                             }
                         }
 
+                        if (exp.weak_symbols) |symbols| {
+                            for (symbols) |sym_name| {
+                                try self.addWeakSymbol(allocator, sym_name);
+                            }
+                        }
+
                         if (exp.objc_classes) |classes| {
                             for (classes) |sym_name| {
                                 try self.addObjCClassSymbol(allocator, sym_name);
@@ -432,6 +456,12 @@ pub fn parseFromStub(
                             }
                         }
 
+                        if (reexp.weak_symbols) |symbols| {
+                            for (symbols) |sym_name| {
+                                try self.addWeakSymbol(allocator, sym_name);
+                            }
+                        }
+
                         if (reexp.objc_classes) |classes| {
                             for (classes) |sym_name| {
                                 try self.addObjCClassSymbol(allocator, sym_name);
src/link/tapi.zig
@@ -26,6 +26,7 @@ pub const TbdV3 = struct {
         allowable_clients: ?[]const []const u8,
         re_exports: ?[]const []const u8,
         symbols: ?[]const []const u8,
+        weak_symbols: ?[]const []const u8,
         objc_classes: ?[]const []const u8,
         objc_ivars: ?[]const []const u8,
         objc_eh_types: ?[]const []const u8,
@@ -53,6 +54,7 @@ pub const TbdV4 = struct {
     exports: ?[]const struct {
         targets: []const []const u8,
         symbols: ?[]const []const u8,
+        weak_symbols: ?[]const []const u8,
         objc_classes: ?[]const []const u8,
         objc_ivars: ?[]const []const u8,
         objc_eh_types: ?[]const []const u8,
@@ -60,6 +62,7 @@ pub const TbdV4 = struct {
     reexports: ?[]const struct {
         targets: []const []const u8,
         symbols: ?[]const []const u8,
+        weak_symbols: ?[]const []const u8,
         objc_classes: ?[]const []const u8,
         objc_ivars: ?[]const []const u8,
         objc_eh_types: ?[]const []const u8,