aboutsummaryrefslogtreecommitdiff
path: root/src/link/MachO
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2022-11-06 15:19:14 +0100
committerJakub Konka <kubkon@jakubkonka.com>2022-11-06 15:19:17 +0100
commit351031b6c7b01f45a4ee366eb1c0b28bce89145c (patch)
tree747aa84686966ca730dd314336afcffbfef9c2d0 /src/link/MachO
parent1aeef297337985c5fd8647462cc7c78ea1a4df43 (diff)
downloadzig-351031b6c7b01f45a4ee366eb1c0b28bce89145c.tar.gz
zig-351031b6c7b01f45a4ee366eb1c0b28bce89145c.zip
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.
Diffstat (limited to 'src/link/MachO')
-rw-r--r--src/link/MachO/Dylib.zig42
1 files changed, 36 insertions, 6 deletions
diff --git a/src/link/MachO/Dylib.zig b/src/link/MachO/Dylib.zig
index 0f16eada61..1c1842aaed 100644
--- a/src/link/MachO/Dylib.zig
+++ b/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);