authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-19 16:15:19+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-29 11:39:34+02:00
log7b282dffe68a7187a4fa4b5c11c82f1f67248a96
treeae1592746bfa08e3882358d7108c296f95dee003
parent702bcfecf5732eceada9bfbca0804c706c238d49

macho: unify concept of SymbolWithLoc across drivers


12 files changed, 88 insertions(+), 127 deletions(-)

src/arch/aarch64/Emit.zig+4-4
......@@ -670,7 +670,7 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
670670
671671 if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
672672 // Add relocation to the decl.
673 const atom_index = macho_file.getAtomIndexForSymbol(.{ .sym_index = relocation.atom_index, .file = null }).?;
673 const atom_index = macho_file.getAtomIndexForSymbol(.{ .sym_index = relocation.atom_index }).?;
674674 const target = macho_file.getGlobalByIndex(relocation.sym_index);
675675 try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
676676 .type = .branch,
......@@ -885,9 +885,9 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
885885 if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
886886 const Atom = link.File.MachO.Atom;
887887 const Relocation = Atom.Relocation;
888 const atom_index = macho_file.getAtomIndexForSymbol(.{ .sym_index = data.atom_index, .file = null }).?;
888 const atom_index = macho_file.getAtomIndexForSymbol(.{ .sym_index = data.atom_index }).?;
889889 try Atom.addRelocations(macho_file, atom_index, &[_]Relocation{ .{
890 .target = .{ .sym_index = data.sym_index, .file = null },
890 .target = .{ .sym_index = data.sym_index },
891891 .offset = offset,
892892 .addend = 0,
893893 .pcrel = true,
......@@ -898,7 +898,7 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
898898 else => unreachable,
899899 },
900900 }, .{
901 .target = .{ .sym_index = data.sym_index, .file = null },
901 .target = .{ .sym_index = data.sym_index },
902902 .offset = offset + 4,
903903 .addend = 0,
904904 .pcrel = false,
src/arch/x86_64/Emit.zig+3-8
......@@ -43,9 +43,7 @@ pub fn emitMir(emit: *Emit) Error!void {
4343 }),
4444 .linker_extern_fn => |symbol| if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
4545 // Add relocation to the decl.
46 const atom_index = macho_file.getAtomIndexForSymbol(
47 .{ .sym_index = symbol.atom_index, .file = null },
48 ).?;
46 const atom_index = macho_file.getAtomIndexForSymbol(.{ .sym_index = symbol.atom_index }).?;
4947 const target = macho_file.getGlobalByIndex(symbol.sym_index);
5048 try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
5149 .type = .branch,
......@@ -77,10 +75,7 @@ pub fn emitMir(emit: *Emit) Error!void {
7775 .linker_import,
7876 .linker_tlv,
7977 => |symbol| if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
80 const atom_index = macho_file.getAtomIndexForSymbol(.{
81 .sym_index = symbol.atom_index,
82 .file = null,
83 }).?;
78 const atom_index = macho_file.getAtomIndexForSymbol(.{ .sym_index = symbol.atom_index }).?;
8479 try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
8580 .type = switch (lowered_relocs[0].target) {
8681 .linker_got => .got,
......@@ -88,7 +83,7 @@ pub fn emitMir(emit: *Emit) Error!void {
8883 .linker_tlv => .tlv,
8984 else => unreachable,
9085 },
91 .target = .{ .sym_index = symbol.sym_index, .file = null },
86 .target = .{ .sym_index = symbol.sym_index },
9287 .offset = @as(u32, @intCast(end_offset - 4)),
9388 .addend = 0,
9489 .pcrel = true,
src/link/MachO.zig+48-43
......@@ -249,20 +249,14 @@ const DeclMetadata = struct {
249249
250250 fn getExport(m: DeclMetadata, macho_file: *const MachO, name: []const u8) ?u32 {
251251 for (m.exports.items) |exp| {
252 if (mem.eql(u8, name, macho_file.getSymbolName(.{
253 .sym_index = exp,
254 .file = null,
255 }))) return exp;
252 if (mem.eql(u8, name, macho_file.getSymbolName(.{ .sym_index = exp }))) return exp;
256253 }
257254 return null;
258255 }
259256
260257 fn getExportPtr(m: *DeclMetadata, macho_file: *MachO, name: []const u8) ?*u32 {
261258 for (m.exports.items) |*exp| {
262 if (mem.eql(u8, name, macho_file.getSymbolName(.{
263 .sym_index = exp.*,
264 .file = null,
265 }))) return exp;
259 if (mem.eql(u8, name, macho_file.getSymbolName(.{ .sym_index = exp.* }))) return exp;
266260 }
267261 return null;
268262 }
......@@ -284,21 +278,20 @@ const ResolveAction = struct {
284278 };
285279};
286280
287pub const SymbolWithLoc = struct {
281pub const SymbolWithLoc = extern struct {
288282 // Index into the respective symbol table.
289283 sym_index: u32,
290284
291 // null means it's a synthetic global.
292 file: ?u32 = null,
285 // 0 means it's a synthetic global.
286 file: u32 = 0,
293287
294 pub fn eql(this: SymbolWithLoc, other: SymbolWithLoc) bool {
295 if (this.file == null and other.file == null) {
296 return this.sym_index == other.sym_index;
297 }
298 if (this.file != null and other.file != null) {
299 return this.sym_index == other.sym_index and this.file.? == other.file.?;
300 }
301 return false;
288 pub fn getFile(self: SymbolWithLoc) ?u32 {
289 if (self.file == 0) return null;
290 return self.file - 1;
291 }
292
293 pub fn eql(self: SymbolWithLoc, other: SymbolWithLoc) bool {
294 return self.file == other.file and self.sym_index == other.sym_index;
302295 }
303296};
304297
......@@ -1576,7 +1569,7 @@ pub fn allocateSpecialSymbols(self: *MachO) !void {
15761569 "__mh_execute_header",
15771570 }) |name| {
15781571 const global = self.getGlobal(name) orelse continue;
1579 if (global.file != null) continue;
1572 if (global.getFile() != null) continue;
15801573 const sym = self.getSymbolPtr(global);
15811574 const seg = self.getSegment(self.text_section_index.?);
15821575 sym.n_sect = 1;
......@@ -1597,7 +1590,7 @@ pub fn createAtom(self: *MachO) !Atom.Index {
15971590 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom_index);
15981591 atom.* = .{
15991592 .sym_index = sym_index,
1600 .file = null,
1593 .file = 0,
16011594 .size = 0,
16021595 .prev_index = null,
16031596 .next_index = null,
......@@ -1664,7 +1657,7 @@ fn createMhExecuteHeaderSymbol(self: *MachO) !void {
16641657
16651658 const gpa = self.base.allocator;
16661659 const sym_index = try self.allocateSymbol();
1667 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
1660 const sym_loc = SymbolWithLoc{ .sym_index = sym_index };
16681661 const sym = self.getSymbolPtr(sym_loc);
16691662 sym.* = .{
16701663 .n_strx = try self.strtab.insert(gpa, "__mh_execute_header"),
......@@ -1684,7 +1677,7 @@ fn createDsoHandleSymbol(self: *MachO) !void {
16841677
16851678 const gpa = self.base.allocator;
16861679 const sym_index = try self.allocateSymbol();
1687 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
1680 const sym_loc = SymbolWithLoc{ .sym_index = sym_index };
16881681 const sym = self.getSymbolPtr(sym_loc);
16891682 sym.* = .{
16901683 .n_strx = try self.strtab.insert(gpa, "___dso_handle"),
......@@ -1998,10 +1991,7 @@ fn allocateGlobal(self: *MachO) !u32 {
19981991 }
19991992 };
20001993
2001 self.globals.items[index] = .{
2002 .sym_index = 0,
2003 .file = null,
2004 };
1994 self.globals.items[index] = .{ .sym_index = 0 };
20051995
20061996 return index;
20071997}
......@@ -2613,7 +2603,7 @@ pub fn updateDeclExports(
26132603 try decl_metadata.exports.append(gpa, sym_index);
26142604 break :blk sym_index;
26152605 };
2616 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
2606 const sym_loc = SymbolWithLoc{ .sym_index = sym_index };
26172607 const sym = self.getSymbolPtr(sym_loc);
26182608 sym.* = .{
26192609 .n_strx = try self.strtab.insert(gpa, exp_name),
......@@ -2643,7 +2633,7 @@ pub fn updateDeclExports(
26432633 error.MultipleSymbolDefinitions => {
26442634 // TODO: this needs rethinking
26452635 const global = self.getGlobal(exp_name).?;
2646 if (sym_loc.sym_index != global.sym_index and global.file != null) {
2636 if (sym_loc.sym_index != global.sym_index and global.getFile() != null) {
26472637 _ = try mod.failed_exports.put(mod.gpa, exp, try Module.ErrorMsg.create(
26482638 gpa,
26492639 decl.srcLoc(mod),
......@@ -2672,7 +2662,7 @@ pub fn deleteDeclExport(
26722662 defer gpa.free(exp_name);
26732663 const sym_index = metadata.getExportPtr(self, exp_name) orelse return;
26742664
2675 const sym_loc = SymbolWithLoc{ .sym_index = sym_index.*, .file = null };
2665 const sym_loc = SymbolWithLoc{ .sym_index = sym_index.* };
26762666 const sym = self.getSymbolPtr(sym_loc);
26772667 log.debug("deleting export '{s}'", .{exp_name});
26782668 assert(sym.sect() and sym.ext());
......@@ -2688,10 +2678,7 @@ pub fn deleteDeclExport(
26882678 if (self.resolver.fetchRemove(exp_name)) |entry| {
26892679 defer gpa.free(entry.key);
26902680 self.globals_free_list.append(gpa, entry.value) catch {};
2691 self.globals.items[entry.value] = .{
2692 .sym_index = 0,
2693 .file = null,
2694 };
2681 self.globals.items[entry.value] = .{ .sym_index = 0 };
26952682 }
26962683
26972684 sym_index.* = 0;
......@@ -2730,10 +2717,10 @@ pub fn getDeclVAddr(self: *MachO, decl_index: Module.Decl.Index, reloc_info: Fil
27302717
27312718 const this_atom_index = try self.getOrCreateAtomForDecl(decl_index);
27322719 const sym_index = self.getAtom(this_atom_index).getSymbolIndex().?;
2733 const atom_index = self.getAtomIndexForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;
2720 const atom_index = self.getAtomIndexForSymbol(.{ .sym_index = reloc_info.parent_atom_index }).?;
27342721 try Atom.addRelocation(self, atom_index, .{
27352722 .type = .unsigned,
2736 .target = .{ .sym_index = sym_index, .file = null },
2723 .target = .{ .sym_index = sym_index },
27372724 .offset = @as(u32, @intCast(reloc_info.offset)),
27382725 .addend = reloc_info.addend,
27392726 .pcrel = false,
......@@ -3514,7 +3501,7 @@ fn writeSymtab(self: *MachO) !SymtabCtx {
35143501
35153502 for (self.locals.items, 0..) |sym, sym_id| {
35163503 if (sym.n_strx == 0) continue; // no name, skip
3517 const sym_loc = SymbolWithLoc{ .sym_index = @as(u32, @intCast(sym_id)), .file = null };
3504 const sym_loc = SymbolWithLoc{ .sym_index = @as(u32, @intCast(sym_id)) };
35183505 if (self.symbolIsTemp(sym_loc)) continue; // local temp symbol, skip
35193506 if (self.getGlobal(self.getSymbolName(sym_loc)) != null) continue; // global symbol is either an export or import, skip
35203507 try locals.append(sym);
......@@ -3934,13 +3921,13 @@ pub fn symbolIsTemp(self: *MachO, sym_with_loc: SymbolWithLoc) bool {
39343921
39353922/// Returns pointer-to-symbol described by `sym_with_loc` descriptor.
39363923pub fn getSymbolPtr(self: *MachO, sym_with_loc: SymbolWithLoc) *macho.nlist_64 {
3937 assert(sym_with_loc.file == null);
3924 assert(sym_with_loc.getFile() == null);
39383925 return &self.locals.items[sym_with_loc.sym_index];
39393926}
39403927
39413928/// Returns symbol described by `sym_with_loc` descriptor.
39423929pub fn getSymbol(self: *const MachO, sym_with_loc: SymbolWithLoc) macho.nlist_64 {
3943 assert(sym_with_loc.file == null);
3930 assert(sym_with_loc.getFile() == null);
39443931 return self.locals.items[sym_with_loc.sym_index];
39453932}
39463933
......@@ -4006,7 +3993,7 @@ pub fn getAtomPtr(self: *MachO, atom_index: Atom.Index) *Atom {
40063993/// Returns atom if there is an atom referenced by the symbol described by `sym_with_loc` descriptor.
40073994/// Returns null on failure.
40083995pub fn getAtomIndexForSymbol(self: *MachO, sym_with_loc: SymbolWithLoc) ?Atom.Index {
4009 assert(sym_with_loc.file == null);
3996 assert(sym_with_loc.getFile() == null);
40103997 return self.atom_by_index_table.get(sym_with_loc.sym_index);
40113998}
40123999
......@@ -4034,13 +4021,31 @@ pub inline fn getPageSize(cpu_arch: std.Target.Cpu.Arch) u16 {
40344021 };
40354022}
40364023
4037pub fn findFirst(comptime T: type, haystack: []align(1) const T, start: usize, predicate: anytype) usize {
4024/// Binary search
4025pub fn bsearch(comptime T: type, haystack: []align(1) const T, predicate: anytype) usize {
40384026 if (!@hasDecl(@TypeOf(predicate), "predicate"))
40394027 @compileError("Predicate is required to define fn predicate(@This(), T) bool");
40404028
4041 if (start == haystack.len) return start;
4029 var min: usize = 0;
4030 var max: usize = haystack.len;
4031 while (min < max) {
4032 const index = (min + max) / 2;
4033 const curr = haystack[index];
4034 if (predicate.predicate(curr)) {
4035 min = index + 1;
4036 } else {
4037 max = index;
4038 }
4039 }
4040 return min;
4041}
4042
4043/// Linear search
4044pub fn lsearch(comptime T: type, haystack: []align(1) const T, predicate: anytype) usize {
4045 if (!@hasDecl(@TypeOf(predicate), "predicate"))
4046 @compileError("Predicate is required to define fn predicate(@This(), T) bool");
40424047
4043 var i = start;
4048 var i: usize = 0;
40444049 while (i < haystack.len) : (i += 1) {
40454050 if (predicate.predicate(haystack[i])) break;
40464051 }
src/link/MachO/Atom.zig+9-6
......@@ -17,16 +17,19 @@ const MachO = @import("../MachO.zig");
1717pub const Relocation = @import("Relocation.zig");
1818const SymbolWithLoc = MachO.SymbolWithLoc;
1919
20/// Each decl always gets a local symbol with the fully qualified name.
21/// The vaddr and size are found here directly.
22/// The file offset is found by computing the vaddr offset from the section vaddr
23/// the symbol references, and adding that to the file offset of the section.
20/// Each Atom always gets a symbol with the fully qualified name.
21/// The symbol can reside in any object file context structure in `symtab` array
22/// (see `Object`), or if the symbol is a synthetic symbol such as a GOT cell or
23/// a stub trampoline, it can be found in the linkers `locals` arraylist.
2424/// If this field is 0, it means the codegen size = 0 and there is no symbol or
2525/// offset table entry.
2626sym_index: u32,
2727
28/// null means symbol defined by Zig source.
29file: ?u32,
28/// 0 means an Atom is a synthetic Atom such as a GOT cell defined by the linker.
29/// Otherwise, it is the index into appropriate object file (indexing from 1).
30/// Prefer using `getFile()` helper to get the file index out rather than using
31/// the field directly.
32file: u32,
3033
3134/// Size and alignment of this atom
3235/// Unlike in Elf, we need to store the size of this symbol as part of
src/link/MachO/DebugSymbols.zig+1-1
......@@ -475,7 +475,7 @@ fn writeSymtab(self: *DebugSymbols, macho_file: *MachO) !void {
475475
476476 for (macho_file.locals.items, 0..) |sym, sym_id| {
477477 if (sym.n_strx == 0) continue; // no name, skip
478 const sym_loc = MachO.SymbolWithLoc{ .sym_index = @as(u32, @intCast(sym_id)), .file = null };
478 const sym_loc = MachO.SymbolWithLoc{ .sym_index = @as(u32, @intCast(sym_id)) };
479479 if (macho_file.symbolIsTemp(sym_loc)) continue; // local temp symbol, skip
480480 if (macho_file.getGlobal(macho_file.getSymbolName(sym_loc)) != null) continue; // global symbol is either an export or import, skip
481481 var out_sym = sym;
src/link/MachO/Object.zig+11-10
......@@ -23,9 +23,10 @@ const Atom = @import("ZldAtom.zig");
2323const AtomIndex = @import("zld.zig").AtomIndex;
2424const DwarfInfo = @import("DwarfInfo.zig");
2525const LoadCommandIterator = macho.LoadCommandIterator;
26const Zld = @import("zld.zig").Zld;
27const SymbolWithLoc = @import("zld.zig").SymbolWithLoc;
26const MachO = @import("../MachO.zig");
27const SymbolWithLoc = MachO.SymbolWithLoc;
2828const UnwindInfo = @import("UnwindInfo.zig");
29const Zld = @import("zld.zig").Zld;
2930
3031name: []const u8,
3132mtime: u64,
......@@ -314,10 +315,10 @@ fn filterSymbolsBySection(symbols: []macho.nlist_64, n_sect: u8) struct {
314315 }
315316 };
316317
317 const index = @import("zld.zig").lsearch(macho.nlist_64, symbols, FirstMatch{
318 const index = MachO.lsearch(macho.nlist_64, symbols, FirstMatch{
318319 .n_sect = n_sect,
319320 });
320 const len = @import("zld.zig").lsearch(macho.nlist_64, symbols[index..], FirstNonMatch{
321 const len = MachO.lsearch(macho.nlist_64, symbols[index..], FirstNonMatch{
321322 .n_sect = n_sect,
322323 });
323324
......@@ -336,10 +337,10 @@ fn filterSymbolsByAddress(symbols: []macho.nlist_64, start_addr: u64, end_addr:
336337 }
337338 };
338339
339 const index = @import("zld.zig").lsearch(macho.nlist_64, symbols, Predicate{
340 const index = MachO.lsearch(macho.nlist_64, symbols, Predicate{
340341 .addr = start_addr,
341342 });
342 const len = @import("zld.zig").lsearch(macho.nlist_64, symbols[index..], Predicate{
343 const len = MachO.lsearch(macho.nlist_64, symbols[index..], Predicate{
343344 .addr = end_addr,
344345 });
345346
......@@ -631,8 +632,8 @@ fn filterRelocs(
631632 }
632633 };
633634
634 const start = @import("zld.zig").bsearch(macho.relocation_info, relocs, Predicate{ .addr = end_addr });
635 const len = @import("zld.zig").lsearch(macho.relocation_info, relocs[start..], LPredicate{ .addr = start_addr });
635 const start = MachO.bsearch(macho.relocation_info, relocs, Predicate{ .addr = end_addr });
636 const len = MachO.lsearch(macho.relocation_info, relocs[start..], LPredicate{ .addr = start_addr });
636637
637638 return .{ .start = @as(u32, @intCast(start)), .len = @as(u32, @intCast(len)) };
638639}
......@@ -1031,7 +1032,7 @@ pub fn getSymbolByAddress(self: Object, addr: u64, sect_hint: ?u8) u32 {
10311032 if (sect_hint) |sect_id| {
10321033 if (self.source_section_index_lookup[sect_id].len > 0) {
10331034 const lookup = self.source_section_index_lookup[sect_id];
1034 const target_sym_index = @import("zld.zig").lsearch(
1035 const target_sym_index = MachO.lsearch(
10351036 i64,
10361037 self.source_address_lookup[lookup.start..][0..lookup.len],
10371038 Predicate{ .addr = @as(i64, @intCast(addr)) },
......@@ -1046,7 +1047,7 @@ pub fn getSymbolByAddress(self: Object, addr: u64, sect_hint: ?u8) u32 {
10461047 return self.getSectionAliasSymbolIndex(sect_id);
10471048 }
10481049
1049 const target_sym_index = @import("zld.zig").lsearch(i64, self.source_address_lookup, Predicate{
1050 const target_sym_index = MachO.lsearch(i64, self.source_address_lookup, Predicate{
10501051 .addr = @as(i64, @intCast(addr)),
10511052 });
10521053 assert(target_sym_index > 0);
src/link/MachO/UnwindInfo.zig+2-1
......@@ -15,8 +15,9 @@ const Allocator = mem.Allocator;
1515const Atom = @import("ZldAtom.zig");
1616const AtomIndex = @import("zld.zig").AtomIndex;
1717const EhFrameRecord = eh_frame.EhFrameRecord;
18const MachO = @import("../MachO.zig");
1819const Object = @import("Object.zig");
19const SymbolWithLoc = @import("zld.zig").SymbolWithLoc;
20const SymbolWithLoc = MachO.SymbolWithLoc;
2021const Zld = @import("zld.zig").Zld;
2122
2223const N_DEAD = @import("zld.zig").N_DEAD;
src/link/MachO/ZldAtom.zig+1-1
......@@ -22,7 +22,7 @@ const Arch = std.Target.Cpu.Arch;
2222const AtomIndex = @import("zld.zig").AtomIndex;
2323const Object = @import("Object.zig");
2424const Relocation = @import("Relocation.zig");
25const SymbolWithLoc = @import("zld.zig").SymbolWithLoc;
25const SymbolWithLoc = @import("../MachO.zig").SymbolWithLoc;
2626const Zld = @import("zld.zig").Zld;
2727
2828/// Each Atom always gets a symbol with the fully qualified name.
src/link/MachO/dead_strip.zig+2-1
......@@ -11,7 +11,8 @@ const mem = std.mem;
1111const Allocator = mem.Allocator;
1212const AtomIndex = @import("zld.zig").AtomIndex;
1313const Atom = @import("ZldAtom.zig");
14const SymbolWithLoc = @import("zld.zig").SymbolWithLoc;
14const MachO = @import("../MachO.zig");
15const SymbolWithLoc = MachO.SymbolWithLoc;
1516const SymbolResolver = @import("zld.zig").SymbolResolver;
1617const UnwindInfo = @import("UnwindInfo.zig");
1718const Zld = @import("zld.zig").Zld;
src/link/MachO/eh_frame.zig+2-1
......@@ -9,8 +9,9 @@ const log = std.log.scoped(.eh_frame);
99const Allocator = mem.Allocator;
1010const AtomIndex = @import("zld.zig").AtomIndex;
1111const Atom = @import("ZldAtom.zig");
12const MachO = @import("../MachO.zig");
1213const Relocation = @import("Relocation.zig");
13const SymbolWithLoc = @import("zld.zig").SymbolWithLoc;
14const SymbolWithLoc = MachO.SymbolWithLoc;
1415const UnwindInfo = @import("UnwindInfo.zig");
1516const Zld = @import("zld.zig").Zld;
1617
src/link/MachO/thunks.zig+2-1
......@@ -17,8 +17,9 @@ const aarch64 = @import("../../arch/aarch64/bits.zig");
1717const Allocator = mem.Allocator;
1818const Atom = @import("ZldAtom.zig");
1919const AtomIndex = @import("zld.zig").AtomIndex;
20const MachO = @import("../MachO.zig");
2021const Relocation = @import("Relocation.zig");
21const SymbolWithLoc = @import("zld.zig").SymbolWithLoc;
22const SymbolWithLoc = MachO.SymbolWithLoc;
2223const Zld = @import("zld.zig").Zld;
2324
2425pub const ThunkIndex = u32;
src/link/MachO/zld.zig+3-50
......@@ -32,6 +32,7 @@ const Md5 = std.crypto.hash.Md5;
3232const LibStub = @import("../tapi.zig").LibStub;
3333const Object = @import("Object.zig");
3434const StringTable = @import("../strtab.zig").StringTable;
35const SymbolWithLoc = MachO.SymbolWithLoc;
3536const Trie = @import("Trie.zig");
3637const UnwindInfo = @import("UnwindInfo.zig");
3738
......@@ -2038,8 +2039,8 @@ pub const Zld = struct {
20382039 }
20392040 };
20402041
2041 const start = lsearch(macho.data_in_code_entry, dices, Predicate{ .addr = start_addr });
2042 const end = lsearch(macho.data_in_code_entry, dices[start..], Predicate{ .addr = end_addr }) + start;
2042 const start = MachO.lsearch(macho.data_in_code_entry, dices, Predicate{ .addr = start_addr });
2043 const end = MachO.lsearch(macho.data_in_code_entry, dices[start..], Predicate{ .addr = end_addr }) + start;
20432044
20442045 return dices[start..end];
20452046 }
......@@ -3021,23 +3022,6 @@ const IndirectPointer = struct {
30213022 }
30223023};
30233024
3024pub const SymbolWithLoc = extern struct {
3025 // Index into the respective symbol table.
3026 sym_index: u32,
3027
3028 // 0 means it's a synthetic global.
3029 file: u32 = 0,
3030
3031 pub fn getFile(self: SymbolWithLoc) ?u32 {
3032 if (self.file == 0) return null;
3033 return self.file - 1;
3034 }
3035
3036 pub fn eql(self: SymbolWithLoc, other: SymbolWithLoc) bool {
3037 return self.file == other.file and self.sym_index == other.sym_index;
3038 }
3039};
3040
30413025pub const SymbolResolver = struct {
30423026 arena: Allocator,
30433027 table: std.StringHashMap(u32),
......@@ -3636,34 +3620,3 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
36363620 macho_file.base.lock = man.toOwnedLock();
36373621 }
36383622}
3639
3640/// Binary search
3641pub fn bsearch(comptime T: type, haystack: []align(1) const T, predicate: anytype) usize {
3642 if (!@hasDecl(@TypeOf(predicate), "predicate"))
3643 @compileError("Predicate is required to define fn predicate(@This(), T) bool");
3644
3645 var min: usize = 0;
3646 var max: usize = haystack.len;
3647 while (min < max) {
3648 const index = (min + max) / 2;
3649 const curr = haystack[index];
3650 if (predicate.predicate(curr)) {
3651 min = index + 1;
3652 } else {
3653 max = index;
3654 }
3655 }
3656 return min;
3657}
3658
3659/// Linear search
3660pub fn lsearch(comptime T: type, haystack: []align(1) const T, predicate: anytype) usize {
3661 if (!@hasDecl(@TypeOf(predicate), "predicate"))
3662 @compileError("Predicate is required to define fn predicate(@This(), T) bool");
3663
3664 var i: usize = 0;
3665 while (i < haystack.len) : (i += 1) {
3666 if (predicate.predicate(haystack[i])) break;
3667 }
3668 return i;
3669}