authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-10-09 17:06:34+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-10-09 21:39:44+02:00
log90aa68cbfc088376a6890402ea5ed5c340c75f7c
tree7d4585748a793e71bac9b3fff0dfb7b1be529a05
parent22661f3d67251688a1fabf9e5fe65210ce284b9f

macho: report special symbols if undefined

Special symbols include explictly force undefined symbols passed via -u flag, missing entry point symbol, missing 'dyld_stub_binder' symbol, or missing '_objc_msgsend' symbol.

3 files changed, 162 insertions(+), 88 deletions(-)

src/link/MachO.zig+118-86
...@@ -27,7 +27,7 @@ sections: std.MultiArrayList(Section) = .{},...@@ -27,7 +27,7 @@ sections: std.MultiArrayList(Section) = .{},
27resolver: SymbolResolver = .{},27resolver: SymbolResolver = .{},
28/// This table will be populated after `scanRelocs` has run.28/// This table will be populated after `scanRelocs` has run.
29/// Key is symbol index.29/// Key is symbol index.
30undefs: std.AutoArrayHashMapUnmanaged(SymbolResolver.Index, std.ArrayListUnmanaged(Ref)) = .empty,30undefs: std.AutoArrayHashMapUnmanaged(SymbolResolver.Index, UndefRefs) = .empty,
31undefs_mutex: std.Thread.Mutex = .{},31undefs_mutex: std.Thread.Mutex = .{},
32dupes: std.AutoArrayHashMapUnmanaged(SymbolResolver.Index, std.ArrayListUnmanaged(File.Index)) = .empty,32dupes: std.AutoArrayHashMapUnmanaged(SymbolResolver.Index, std.ArrayListUnmanaged(File.Index)) = .empty,
33dupes_mutex: std.Thread.Mutex = .{},33dupes_mutex: std.Thread.Mutex = .{},
...@@ -1470,6 +1470,9 @@ fn scanRelocs(self: *MachO) !void {...@@ -1470,6 +1470,9 @@ fn scanRelocs(self: *MachO) !void {
14701470
1471 if (self.has_errors.swap(false, .seq_cst)) return error.FlushFailure;1471 if (self.has_errors.swap(false, .seq_cst)) return error.FlushFailure;
14721472
1473 if (self.getInternalObject()) |obj| {
1474 try obj.checkUndefs(self);
1475 }
1473 try self.reportUndefs();1476 try self.reportUndefs();
14741477
1475 if (self.getZigObject()) |zo| {1478 if (self.getZigObject()) |zo| {
...@@ -1530,29 +1533,43 @@ fn reportUndefs(self: *MachO) !void {...@@ -1530,29 +1533,43 @@ fn reportUndefs(self: *MachO) !void {
1530 }1533 }
1531 }.lessThan;1534 }.lessThan;
15321535
1533 for (self.undefs.values()) |*refs| {1536 for (self.undefs.values()) |*undefs| switch (undefs.*) {
1534 mem.sort(Ref, refs.items, {}, refLessThan);1537 .refs => |refs| mem.sort(Ref, refs.items, {}, refLessThan),
1535 }1538 else => {},
1539 };
15361540
1537 for (keys.items) |key| {1541 for (keys.items) |key| {
1538 const undef_sym = self.resolver.keys.items[key - 1];1542 const undef_sym = self.resolver.keys.items[key - 1];
1539 const notes = self.undefs.get(key).?;1543 const notes = self.undefs.get(key).?;
1540 const nnotes = @min(notes.items.len, max_notes) + @intFromBool(notes.items.len > max_notes);1544 const nnotes = nnotes: {
1545 const nnotes = switch (notes) {
1546 .refs => |refs| refs.items.len,
1547 else => 1,
1548 };
1549 break :nnotes @min(nnotes, max_notes) + @intFromBool(nnotes > max_notes);
1550 };
15411551
1542 var err = try self.base.addErrorWithNotes(nnotes);1552 var err = try self.base.addErrorWithNotes(nnotes);
1543 try err.addMsg("undefined symbol: {s}", .{undef_sym.getName(self)});1553 try err.addMsg("undefined symbol: {s}", .{undef_sym.getName(self)});
15441554
1545 var inote: usize = 0;1555 switch (notes) {
1546 while (inote < @min(notes.items.len, max_notes)) : (inote += 1) {1556 .force_undefined => try err.addNote("referenced with linker flag -u", .{}),
1547 const note = notes.items[inote];1557 .entry => try err.addNote("referenced with linker flag -e", .{}),
1548 const file = self.getFile(note.file).?;1558 .dyld_stub_binder, .objc_msgsend => try err.addNote("referenced implicitly", .{}),
1549 const atom = note.getAtom(self).?;1559 .refs => |refs| {
1550 try err.addNote("referenced by {}:{s}", .{ file.fmtPath(), atom.getName(self) });1560 var inote: usize = 0;
1551 }1561 while (inote < @min(refs.items.len, max_notes)) : (inote += 1) {
1562 const ref = refs.items[inote];
1563 const file = self.getFile(ref.file).?;
1564 const atom = ref.getAtom(self).?;
1565 try err.addNote("referenced by {}:{s}", .{ file.fmtPath(), atom.getName(self) });
1566 }
15521567
1553 if (notes.items.len > max_notes) {1568 if (refs.items.len > max_notes) {
1554 const remaining = notes.items.len - max_notes;1569 const remaining = refs.items.len - max_notes;
1555 try err.addNote("referenced {d} more times", .{remaining});1570 try err.addNote("referenced {d} more times", .{remaining});
1571 }
1572 },
1556 }1573 }
1557 }1574 }
15581575
...@@ -4584,78 +4601,20 @@ pub const String = struct {...@@ -4584,78 +4601,20 @@ pub const String = struct {
4584 len: u32 = 0,4601 len: u32 = 0,
4585};4602};
45864603
4587const MachO = @This();4604pub const UndefRefs = union(enum) {
45884605 force_undefined,
4589const std = @import("std");4606 entry,
4590const build_options = @import("build_options");4607 dyld_stub_binder,
4591const builtin = @import("builtin");4608 objc_msgsend,
4592const assert = std.debug.assert;4609 refs: std.ArrayListUnmanaged(Ref),
4593const fs = std.fs;
4594const log = std.log.scoped(.link);
4595const state_log = std.log.scoped(.link_state);
4596const macho = std.macho;
4597const math = std.math;
4598const mem = std.mem;
4599const meta = std.meta;
4600
4601const aarch64 = @import("../arch/aarch64/bits.zig");
4602const bind = @import("MachO/dyld_info/bind.zig");
4603const calcUuid = @import("MachO/uuid.zig").calcUuid;
4604const codegen = @import("../codegen.zig");
4605const dead_strip = @import("MachO/dead_strip.zig");
4606const eh_frame = @import("MachO/eh_frame.zig");
4607const fat = @import("MachO/fat.zig");
4608const link = @import("../link.zig");
4609const load_commands = @import("MachO/load_commands.zig");
4610const relocatable = @import("MachO/relocatable.zig");
4611const tapi = @import("tapi.zig");
4612const target_util = @import("../target.zig");
4613const trace = @import("../tracy.zig").trace;
4614const synthetic = @import("MachO/synthetic.zig");
46154610
4616const Air = @import("../Air.zig");4611 pub fn deinit(self: *UndefRefs, allocator: Allocator) void {
4617const Alignment = Atom.Alignment;4612 switch (self.*) {
4618const Allocator = mem.Allocator;4613 .refs => |*refs| refs.deinit(allocator),
4619const Archive = @import("MachO/Archive.zig");4614 else => {},
4620pub const Atom = @import("MachO/Atom.zig");4615 }
4621const AtomicBool = std.atomic.Value(bool);4616 }
4622const Bind = bind.Bind;4617};
4623const Cache = std.Build.Cache;
4624const Path = Cache.Path;
4625const CodeSignature = @import("MachO/CodeSignature.zig");
4626const Compilation = @import("../Compilation.zig");
4627const DataInCode = synthetic.DataInCode;
4628pub const DebugSymbols = @import("MachO/DebugSymbols.zig");
4629const Dylib = @import("MachO/Dylib.zig");
4630const ExportTrie = @import("MachO/dyld_info/Trie.zig");
4631const File = @import("MachO/file.zig").File;
4632const GotSection = synthetic.GotSection;
4633const Hash = std.hash.Wyhash;
4634const Indsymtab = synthetic.Indsymtab;
4635const InternalObject = @import("MachO/InternalObject.zig");
4636const ObjcStubsSection = synthetic.ObjcStubsSection;
4637const Object = @import("MachO/Object.zig");
4638const LazyBind = bind.LazyBind;
4639const LaSymbolPtrSection = synthetic.LaSymbolPtrSection;
4640const Liveness = @import("../Liveness.zig");
4641const LlvmObject = @import("../codegen/llvm.zig").Object;
4642const Md5 = std.crypto.hash.Md5;
4643const Zcu = @import("../Zcu.zig");
4644const InternPool = @import("../InternPool.zig");
4645const Rebase = @import("MachO/dyld_info/Rebase.zig");
4646pub const Relocation = @import("MachO/Relocation.zig");
4647const StringTable = @import("StringTable.zig");
4648const StubsSection = synthetic.StubsSection;
4649const StubsHelperSection = synthetic.StubsHelperSection;
4650const Symbol = @import("MachO/Symbol.zig");
4651const Thunk = @import("MachO/Thunk.zig");
4652const TlvPtrSection = synthetic.TlvPtrSection;
4653const Value = @import("../Value.zig");
4654const UnwindInfo = @import("MachO/UnwindInfo.zig");
4655const WaitGroup = std.Thread.WaitGroup;
4656const WeakBind = bind.WeakBind;
4657const ZigObject = @import("MachO/ZigObject.zig");
4658const dev = @import("../dev.zig");
46594618
4660pub const MachError = error{4619pub const MachError = error{
4661 /// Not enough permissions held to perform the requested kernel4620 /// Not enough permissions held to perform the requested kernel
...@@ -5392,3 +5351,76 @@ const max_distance = (1 << (jump_bits - 1));...@@ -5392,3 +5351,76 @@ const max_distance = (1 << (jump_bits - 1));
5392/// mold uses 5MiB margin, while ld64 uses 4MiB margin. We will follow mold5351/// mold uses 5MiB margin, while ld64 uses 4MiB margin. We will follow mold
5393/// and assume margin to be 5MiB.5352/// and assume margin to be 5MiB.
5394const max_allowed_distance = max_distance - 0x500_000;5353const max_allowed_distance = max_distance - 0x500_000;
5354
5355const MachO = @This();
5356
5357const std = @import("std");
5358const build_options = @import("build_options");
5359const builtin = @import("builtin");
5360const assert = std.debug.assert;
5361const fs = std.fs;
5362const log = std.log.scoped(.link);
5363const state_log = std.log.scoped(.link_state);
5364const macho = std.macho;
5365const math = std.math;
5366const mem = std.mem;
5367const meta = std.meta;
5368
5369const aarch64 = @import("../arch/aarch64/bits.zig");
5370const bind = @import("MachO/dyld_info/bind.zig");
5371const calcUuid = @import("MachO/uuid.zig").calcUuid;
5372const codegen = @import("../codegen.zig");
5373const dead_strip = @import("MachO/dead_strip.zig");
5374const eh_frame = @import("MachO/eh_frame.zig");
5375const fat = @import("MachO/fat.zig");
5376const link = @import("../link.zig");
5377const load_commands = @import("MachO/load_commands.zig");
5378const relocatable = @import("MachO/relocatable.zig");
5379const tapi = @import("tapi.zig");
5380const target_util = @import("../target.zig");
5381const trace = @import("../tracy.zig").trace;
5382const synthetic = @import("MachO/synthetic.zig");
5383
5384const Air = @import("../Air.zig");
5385const Alignment = Atom.Alignment;
5386const Allocator = mem.Allocator;
5387const Archive = @import("MachO/Archive.zig");
5388pub const Atom = @import("MachO/Atom.zig");
5389const AtomicBool = std.atomic.Value(bool);
5390const Bind = bind.Bind;
5391const Cache = std.Build.Cache;
5392const Path = Cache.Path;
5393const CodeSignature = @import("MachO/CodeSignature.zig");
5394const Compilation = @import("../Compilation.zig");
5395const DataInCode = synthetic.DataInCode;
5396pub const DebugSymbols = @import("MachO/DebugSymbols.zig");
5397const Dylib = @import("MachO/Dylib.zig");
5398const ExportTrie = @import("MachO/dyld_info/Trie.zig");
5399const File = @import("MachO/file.zig").File;
5400const GotSection = synthetic.GotSection;
5401const Hash = std.hash.Wyhash;
5402const Indsymtab = synthetic.Indsymtab;
5403const InternalObject = @import("MachO/InternalObject.zig");
5404const ObjcStubsSection = synthetic.ObjcStubsSection;
5405const Object = @import("MachO/Object.zig");
5406const LazyBind = bind.LazyBind;
5407const LaSymbolPtrSection = synthetic.LaSymbolPtrSection;
5408const Liveness = @import("../Liveness.zig");
5409const LlvmObject = @import("../codegen/llvm.zig").Object;
5410const Md5 = std.crypto.hash.Md5;
5411const Zcu = @import("../Zcu.zig");
5412const InternPool = @import("../InternPool.zig");
5413const Rebase = @import("MachO/dyld_info/Rebase.zig");
5414pub const Relocation = @import("MachO/Relocation.zig");
5415const StringTable = @import("StringTable.zig");
5416const StubsSection = synthetic.StubsSection;
5417const StubsHelperSection = synthetic.StubsHelperSection;
5418const Symbol = @import("MachO/Symbol.zig");
5419const Thunk = @import("MachO/Thunk.zig");
5420const TlvPtrSection = synthetic.TlvPtrSection;
5421const Value = @import("../Value.zig");
5422const UnwindInfo = @import("MachO/UnwindInfo.zig");
5423const WaitGroup = std.Thread.WaitGroup;
5424const WeakBind = bind.WeakBind;
5425const ZigObject = @import("MachO/ZigObject.zig");
5426const dev = @import("../dev.zig");
src/link/MachO/Atom.zig+2-2
...@@ -560,9 +560,9 @@ fn reportUndefSymbol(self: Atom, rel: Relocation, macho_file: *MachO) !bool {...@@ -560,9 +560,9 @@ fn reportUndefSymbol(self: Atom, rel: Relocation, macho_file: *MachO) !bool {
560 const gpa = macho_file.base.comp.gpa;560 const gpa = macho_file.base.comp.gpa;
561 const gop = try macho_file.undefs.getOrPut(gpa, file.getGlobals()[rel.target]);561 const gop = try macho_file.undefs.getOrPut(gpa, file.getGlobals()[rel.target]);
562 if (!gop.found_existing) {562 if (!gop.found_existing) {
563 gop.value_ptr.* = .{};563 gop.value_ptr.* = .{ .refs = .{} };
564 }564 }
565 try gop.value_ptr.append(gpa, .{ .index = self.atom_index, .file = self.file });565 try gop.value_ptr.refs.append(gpa, .{ .index = self.atom_index, .file = self.file });
566 return true;566 return true;
567 }567 }
568568
src/link/MachO/InternalObject.zig+42
...@@ -507,6 +507,41 @@ pub fn scanRelocs(self: *InternalObject, macho_file: *MachO) void {...@@ -507,6 +507,41 @@ pub fn scanRelocs(self: *InternalObject, macho_file: *MachO) void {
507 }507 }
508}508}
509509
510pub fn checkUndefs(self: InternalObject, macho_file: *MachO) !void {
511 const addUndef = struct {
512 fn addUndef(mf: *MachO, index: MachO.SymbolResolver.Index, tag: anytype) !void {
513 const gpa = mf.base.comp.gpa;
514 mf.undefs_mutex.lock();
515 defer mf.undefs_mutex.unlock();
516 const gop = try mf.undefs.getOrPut(gpa, index);
517 if (!gop.found_existing) {
518 gop.value_ptr.* = tag;
519 }
520 }
521 }.addUndef;
522 for (self.force_undefined.items) |index| {
523 const ref = self.getSymbolRef(index, macho_file);
524 if (ref.getFile(macho_file) == null) {
525 try addUndef(macho_file, self.globals.items[index], .force_undefined);
526 }
527 }
528 if (self.getEntryRef(macho_file)) |ref| {
529 if (ref.getFile(macho_file) == null) {
530 try addUndef(macho_file, self.globals.items[self.entry_index.?], .entry);
531 }
532 }
533 if (self.getDyldStubBinderRef(macho_file)) |ref| {
534 if (ref.getFile(macho_file) == null and macho_file.stubs.symbols.items.len > 0) {
535 try addUndef(macho_file, self.globals.items[self.dyld_stub_binder_index.?], .dyld_stub_binder);
536 }
537 }
538 if (self.getObjcMsgSendRef(macho_file)) |ref| {
539 if (ref.getFile(macho_file) == null and self.needsObjcMsgsendSymbol()) {
540 try addUndef(macho_file, self.globals.items[self.objc_msg_send_index.?], .objc_msgsend);
541 }
542 }
543}
544
510pub fn allocateSyntheticSymbols(self: *InternalObject, macho_file: *MachO) void {545pub fn allocateSyntheticSymbols(self: *InternalObject, macho_file: *MachO) void {
511 const text_seg = macho_file.getTextSegment();546 const text_seg = macho_file.getTextSegment();
512547
...@@ -791,6 +826,13 @@ pub fn setSymbolExtra(self: *InternalObject, index: u32, extra: Symbol.Extra) vo...@@ -791,6 +826,13 @@ pub fn setSymbolExtra(self: *InternalObject, index: u32, extra: Symbol.Extra) vo
791 }826 }
792}827}
793828
829fn needsObjcMsgsendSymbol(self: InternalObject) bool {
830 for (self.sections.items(.extra)) |extra| {
831 if (extra.is_objc_methname or extra.is_objc_selref) return true;
832 }
833 return false;
834}
835
794const FormatContext = struct {836const FormatContext = struct {
795 self: *InternalObject,837 self: *InternalObject,
796 macho_file: *MachO,838 macho_file: *MachO,