1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
pub const File = union(enum) {
zig_object: *ZigObject,
internal: *InternalObject,
object: *Object,
dylib: *Dylib,
pub fn getIndex(file: File) Index {
return switch (file) {
inline else => |x| x.index,
};
}
pub fn fmtPath(file: File) std.fmt.Alt(File, formatPath) {
return .{ .data = file };
}
fn formatPath(file: File, w: *Writer) Writer.Error!void {
switch (file) {
.zig_object => |zo| try w.writeAll(zo.basename),
.internal => try w.writeAll("internal"),
.object => |x| try w.print("{f}", .{x.fmtPath()}),
.dylib => |dl| try w.print("{f}", .{@as(Path, dl.path)}),
}
}
pub fn resolveSymbols(file: File, macho_file: *MachO) !void {
return switch (file) {
inline else => |x| x.resolveSymbols(macho_file),
};
}
pub fn scanRelocs(file: File, macho_file: *MachO) !void {
return switch (file) {
.dylib => unreachable,
inline else => |x| x.scanRelocs(macho_file),
};
}
/// Encodes symbol rank so that the following ordering applies:
/// * strong in object
/// * strong in archive/dylib
/// * weak in object
/// * weak in archive/dylib
/// * tentative in object
/// * tentative in archive
/// * unclaimed
/// Ties are broken by file priority.
pub fn getSymbolRank(file: File, args: struct {
archive: bool = false,
weak: bool = false,
tentative: bool = false,
}) u32 {
const archive_or_dylib = @as(u32, @intFromBool(file == .dylib or args.archive)) << 29;
const strength: u32 = if (args.tentative) 0b10 << 30 else if (args.weak) 0b01 << 30 else 0b00 << 30;
return strength | archive_or_dylib | file.getIndex();
}
pub fn getAtom(file: File, atom_index: Atom.Index) ?*Atom {
return switch (file) {
.dylib => unreachable,
inline else => |x| x.getAtom(atom_index),
};
}
pub fn getAtoms(file: File) []const Atom.Index {
return switch (file) {
.dylib => unreachable,
inline else => |x| x.getAtoms(),
};
}
pub fn addAtomExtra(file: File, allocator: Allocator, extra: Atom.Extra) !u32 {
return switch (file) {
.dylib => unreachable,
inline else => |x| x.addAtomExtra(allocator, extra),
};
}
pub fn getAtomExtra(file: File, index: u32) Atom.Extra {
return switch (file) {
.dylib => unreachable,
inline else => |x| x.getAtomExtra(index),
};
}
pub fn setAtomExtra(file: File, index: u32, extra: Atom.Extra) void {
return switch (file) {
.dylib => unreachable,
inline else => |x| x.setAtomExtra(index, extra),
};
}
pub fn getSymbols(file: File) []Symbol {
return switch (file) {
inline else => |x| x.symbols.items,
};
}
pub fn getSymbolRef(file: File, sym_index: Symbol.Index, macho_file: *MachO) MachO.Ref {
return switch (file) {
inline else => |x| x.getSymbolRef(sym_index, macho_file),
};
}
pub fn getNlists(file: File) []macho.nlist_64 {
return switch (file) {
.dylib => unreachable,
.internal => |x| x.symtab.items,
inline else => |x| x.symtab.items(.nlist),
};
}
pub fn getGlobals(file: File) []MachO.SymbolResolver.Index {
return switch (file) {
inline else => |x| x.globals.items,
};
}
pub fn markImportsExports(file: File, macho_file: *MachO) void {
const tracy = trace(@src());
defer tracy.end();
const nsyms = switch (file) {
.dylib => unreachable,
inline else => |x| x.symbols.items.len,
};
for (0..nsyms) |i| {
const ref = file.getSymbolRef(@intCast(i), macho_file);
if (ref.getFile(macho_file) == null) continue;
const sym = ref.getSymbol(macho_file).?;
if (sym.visibility != .global) continue;
if (sym.getFile(macho_file).? == .dylib and !sym.flags.abs) {
sym.flags.import = true;
continue;
}
if (file.getIndex() == ref.file) {
sym.flags.@"export" = true;
}
}
}
pub fn markExportsRelocatable(file: File, macho_file: *MachO) void {
const tracy = trace(@src());
defer tracy.end();
assert(file == .object or file == .zig_object);
for (file.getSymbols(), 0..) |*sym, i| {
const ref = file.getSymbolRef(@intCast(i), macho_file);
const other_file = ref.getFile(macho_file) orelse continue;
if (other_file.getIndex() != file.getIndex()) continue;
if (sym.visibility != .global) continue;
sym.flags.@"export" = true;
}
}
pub fn createSymbolIndirection(file: File, macho_file: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();
const nsyms = switch (file) {
inline else => |x| x.symbols.items.len,
};
for (0..nsyms) |i| {
const ref = file.getSymbolRef(@intCast(i), macho_file);
if (ref.getFile(macho_file) == null) continue;
if (ref.file != file.getIndex()) continue;
const sym = ref.getSymbol(macho_file).?;
if (sym.getSectionFlags().needs_got) {
log.debug("'{s}' needs GOT", .{sym.getName(macho_file)});
try macho_file.got.addSymbol(ref, macho_file);
}
if (sym.getSectionFlags().stubs) {
log.debug("'{s}' needs STUBS", .{sym.getName(macho_file)});
try macho_file.stubs.addSymbol(ref, macho_file);
}
if (sym.getSectionFlags().tlv_ptr) {
log.debug("'{s}' needs TLV pointer", .{sym.getName(macho_file)});
try macho_file.tlv_ptr.addSymbol(ref, macho_file);
}
if (sym.getSectionFlags().objc_stubs) {
log.debug("'{s}' needs OBJC STUBS", .{sym.getName(macho_file)});
try macho_file.objc_stubs.addSymbol(ref, macho_file);
}
}
}
pub fn claimUnresolved(file: File, macho_file: *MachO) void {
const tracy = trace(@src());
defer tracy.end();
assert(file == .object or file == .zig_object);
for (file.getSymbols(), file.getNlists(), 0..) |*sym, nlist, i| {
if (!nlist.n_type.bits.ext) continue;
if (nlist.n_type.bits.type != .undf) continue;
if (file.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue;
const is_import = switch (macho_file.undefined_treatment) {
.@"error" => false,
.warn, .suppress => nlist.n_desc.weak_ref,
.dynamic_lookup => true,
};
if (is_import) {
sym.value = 0;
sym.atom_ref = .{ .index = 0, .file = 0 };
sym.flags.weak = false;
sym.flags.weak_ref = nlist.n_desc.weak_ref;
sym.flags.import = is_import;
sym.visibility = .global;
const idx = file.getGlobals()[i];
macho_file.resolver.values.items[idx - 1] = .{ .index = @intCast(i), .file = file.getIndex() };
}
}
}
pub fn claimUnresolvedRelocatable(file: File, macho_file: *MachO) void {
const tracy = trace(@src());
defer tracy.end();
assert(file == .object or file == .zig_object);
for (file.getSymbols(), file.getNlists(), 0..) |*sym, nlist, i| {
if (!nlist.n_type.bits.ext) continue;
if (nlist.n_type.bits.type != .undf) continue;
if (file.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue;
sym.value = 0;
sym.atom_ref = .{ .index = 0, .file = 0 };
sym.flags.weak_ref = nlist.n_desc.weak_ref;
sym.flags.import = true;
sym.visibility = .global;
const idx = file.getGlobals()[i];
macho_file.resolver.values.items[idx - 1] = .{ .index = @intCast(i), .file = file.getIndex() };
}
}
pub fn checkDuplicates(file: File, macho_file: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();
const gpa = macho_file.base.comp.gpa;
for (file.getSymbols(), file.getNlists(), 0..) |sym, nlist, i| {
if (sym.visibility != .global) continue;
if (sym.flags.weak) continue;
if (nlist.n_type.bits.type == .undf) continue;
const ref = file.getSymbolRef(@intCast(i), macho_file);
const ref_file = ref.getFile(macho_file) orelse continue;
if (ref_file.getIndex() == file.getIndex()) continue;
macho_file.dupes_mutex.lock();
defer macho_file.dupes_mutex.unlock();
const gop = try macho_file.dupes.getOrPut(gpa, file.getGlobals()[i]);
if (!gop.found_existing) {
gop.value_ptr.* = .{};
}
try gop.value_ptr.append(gpa, file.getIndex());
}
}
pub fn initOutputSections(file: File, macho_file: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();
for (file.getAtoms()) |atom_index| {
const atom = file.getAtom(atom_index) orelse continue;
if (!atom.isAlive()) continue;
atom.out_n_sect = try Atom.initOutputSection(atom.getInputSection(macho_file), macho_file);
}
}
pub fn dedupLiterals(file: File, lp: MachO.LiteralPool, macho_file: *MachO) void {
return switch (file) {
.dylib => unreachable,
inline else => |x| x.dedupLiterals(lp, macho_file),
};
}
pub fn writeAtoms(file: File, macho_file: *MachO) !void {
return switch (file) {
.dylib => unreachable,
inline else => |x| x.writeAtoms(macho_file),
};
}
pub fn writeAtomsRelocatable(file: File, macho_file: *MachO) !void {
return switch (file) {
.dylib, .internal => unreachable,
inline else => |x| x.writeAtomsRelocatable(macho_file),
};
}
pub fn calcSymtabSize(file: File, macho_file: *MachO) void {
return switch (file) {
inline else => |x| x.calcSymtabSize(macho_file),
};
}
pub fn writeSymtab(file: File, macho_file: *MachO, ctx: anytype) void {
return switch (file) {
inline else => |x| x.writeSymtab(macho_file, ctx),
};
}
pub fn updateArSymtab(file: File, ar_symtab: *Archive.ArSymtab, macho_file: *MachO) error{OutOfMemory}!void {
return switch (file) {
.dylib, .internal => unreachable,
inline else => |x| x.updateArSymtab(ar_symtab, macho_file),
};
}
pub fn updateArSize(file: File, macho_file: *MachO) !void {
return switch (file) {
.dylib, .internal => unreachable,
.zig_object => |x| x.updateArSize(),
.object => |x| x.updateArSize(macho_file),
};
}
pub fn writeAr(file: File, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void {
return switch (file) {
.dylib, .internal => unreachable,
.zig_object => |x| x.writeAr(ar_format, writer),
.object => |x| x.writeAr(ar_format, macho_file, writer),
};
}
pub fn parse(file: File, macho_file: *MachO) !void {
return switch (file) {
.internal, .zig_object => unreachable,
.object => |x| x.parse(macho_file),
.dylib => |x| x.parse(macho_file),
};
}
pub fn parseAr(file: File, macho_file: *MachO) !void {
return switch (file) {
.internal, .zig_object, .dylib => unreachable,
.object => |x| x.parseAr(macho_file),
};
}
pub const Index = u32;
pub const Entry = union(enum) {
null: void,
zig_object: ZigObject,
internal: InternalObject,
object: Object,
dylib: Dylib,
};
pub const Handle = std.fs.File;
pub const HandleIndex = Index;
};
const std = @import("std");
const assert = std.debug.assert;
const log = std.log.scoped(.link);
const macho = std.macho;
const Allocator = std.mem.Allocator;
const Path = std.Build.Cache.Path;
const Writer = std.Io.Writer;
const trace = @import("../../tracy.zig").trace;
const Archive = @import("Archive.zig");
const Atom = @import("Atom.zig");
const InternalObject = @import("InternalObject.zig");
const MachO = @import("../MachO.zig");
const Object = @import("Object.zig");
const Dylib = @import("Dylib.zig");
const Symbol = @import("Symbol.zig");
const ZigObject = @import("ZigObject.zig");
|