authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-07-23 00:01:09-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-07-23 00:01:09-07:00
loga8bfddfaeae4f48c044fd134aac1e977e6a161f8
tree4b1b000767ba641f5ca7f7c40aa17e29991e9114
parenta035d75a1750e59e43bb9122f33d8586ed1ee385
parentcf6cfc830db89e0031200d1a16c93eb7801cb911
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12140 from ziglang/macho-gc-sections

macho: add support for `-dead_strip` (GC sections) and simplify symbol resolution

22 files changed, 3507 insertions(+), 2868 deletions(-)

CMakeLists.txt+2
......@@ -757,10 +757,12 @@ set(ZIG_STAGE2_SOURCES
757757 "${CMAKE_SOURCE_DIR}/src/link/MachO/Object.zig"
758758 "${CMAKE_SOURCE_DIR}/src/link/MachO/Trie.zig"
759759 "${CMAKE_SOURCE_DIR}/src/link/MachO/bind.zig"
760 "${CMAKE_SOURCE_DIR}/src/link/MachO/dead_strip.zig"
760761 "${CMAKE_SOURCE_DIR}/src/link/Plan9.zig"
761762 "${CMAKE_SOURCE_DIR}/src/link/Plan9/aout.zig"
762763 "${CMAKE_SOURCE_DIR}/src/link/Wasm.zig"
763764 "${CMAKE_SOURCE_DIR}/src/link/msdos-stub.bin"
765 "${CMAKE_SOURCE_DIR}/src/link/strtab.zig"
764766 "${CMAKE_SOURCE_DIR}/src/link/tapi.zig"
765767 "${CMAKE_SOURCE_DIR}/src/link/tapi/Tokenizer.zig"
766768 "${CMAKE_SOURCE_DIR}/src/link/tapi/parse.zig"
lib/std/build.zig+7
......@@ -1561,6 +1561,10 @@ pub const LibExeObjStep = struct {
15611561 /// safely garbage-collected during the linking phase.
15621562 link_function_sections: bool = false,
15631563
1564 /// Remove functions and data that are unreachable by the entry point or
1565 /// exported symbols.
1566 link_gc_sections: ?bool = null,
1567
15641568 linker_allow_shlib_undefined: ?bool = null,
15651569
15661570 /// Permit read-only relocations in read-only segments. Disallowed by default.
......@@ -2705,6 +2709,9 @@ pub const LibExeObjStep = struct {
27052709 if (self.link_function_sections) {
27062710 try zig_args.append("-ffunction-sections");
27072711 }
2712 if (self.link_gc_sections) |x| {
2713 try zig_args.append(if (x) "--gc-sections" else "--no-gc-sections");
2714 }
27082715 if (self.linker_allow_shlib_undefined) |x| {
27092716 try zig_args.append(if (x) "-fallow-shlib-undefined" else "-fno-allow-shlib-undefined");
27102717 }
lib/std/build/CheckObjectStep.zig+33-2
......@@ -50,7 +50,7 @@ pub fn create(builder: *Builder, source: build.FileSource, obj_format: std.Targe
5050/// For example, if the two extracted values were saved as `vmaddr` and `entryoff` respectively
5151/// they could then be added with this simple program `vmaddr entryoff +`.
5252const Action = struct {
53 tag: enum { match, compute_cmp },
53 tag: enum { match, not_present, compute_cmp },
5454 phrase: []const u8,
5555 expected: ?ComputeCompareExpected = null,
5656
......@@ -63,7 +63,7 @@ const Action = struct {
6363 /// name {*}libobjc{*}.dylib => will match `name` followed by a token which contains `libobjc` and `.dylib`
6464 /// in that order with other letters in between
6565 fn match(act: Action, haystack: []const u8, global_vars: anytype) !bool {
66 assert(act.tag == .match);
66 assert(act.tag == .match or act.tag == .not_present);
6767
6868 var candidate_var: ?struct { name: []const u8, value: u64 } = null;
6969 var hay_it = mem.tokenize(u8, mem.trim(u8, haystack, " "), " ");
......@@ -202,6 +202,13 @@ const Check = struct {
202202 }) catch unreachable;
203203 }
204204
205 fn notPresent(self: *Check, phrase: []const u8) void {
206 self.actions.append(.{
207 .tag = .not_present,
208 .phrase = self.builder.dupe(phrase),
209 }) catch unreachable;
210 }
211
205212 fn computeCmp(self: *Check, phrase: []const u8, expected: ComputeCompareExpected) void {
206213 self.actions.append(.{
207214 .tag = .compute_cmp,
......@@ -226,6 +233,15 @@ pub fn checkNext(self: *CheckObjectStep, phrase: []const u8) void {
226233 last.match(phrase);
227234}
228235
236/// Adds another searched phrase to the latest created Check with `CheckObjectStep.checkStart(...)`
237/// however ensures there is no matching phrase in the output.
238/// Asserts at least one check already exists.
239pub fn checkNotPresent(self: *CheckObjectStep, phrase: []const u8) void {
240 assert(self.checks.items.len > 0);
241 const last = &self.checks.items[self.checks.items.len - 1];
242 last.notPresent(phrase);
243}
244
229245/// Creates a new check checking specifically symbol table parsed and dumped from the object
230246/// file.
231247/// Issuing this check will force parsing and dumping of the symbol table.
......@@ -293,6 +309,21 @@ fn make(step: *Step) !void {
293309 return error.TestFailed;
294310 }
295311 },
312 .not_present => {
313 while (it.next()) |line| {
314 if (try act.match(line, &vars)) {
315 std.debug.print(
316 \\
317 \\========= Expected not to find: ===================
318 \\{s}
319 \\========= But parsed file does contain it: ========
320 \\{s}
321 \\
322 , .{ act.phrase, output });
323 return error.TestFailed;
324 }
325 }
326 },
296327 .compute_cmp => {
297328 const res = act.computeCmp(gpa, vars) catch |err| switch (err) {
298329 error.UnknownVariable => {
src/arch/aarch64/CodeGen.zig+9-9
......@@ -3174,7 +3174,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
31743174 const func = func_payload.data;
31753175 const fn_owner_decl = mod.declPtr(func.owner_decl);
31763176 try self.genSetReg(Type.initTag(.u64), .x30, .{
3177 .got_load = fn_owner_decl.link.macho.local_sym_index,
3177 .got_load = fn_owner_decl.link.macho.sym_index,
31783178 });
31793179 // blr x30
31803180 _ = try self.addInst(.{
......@@ -3190,14 +3190,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
31903190 lib_name,
31913191 });
31923192 }
3193 const n_strx = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
3193 const sym_index = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
31943194
31953195 _ = try self.addInst(.{
31963196 .tag = .call_extern,
31973197 .data = .{
3198 .extern_fn = .{
3199 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.local_sym_index,
3200 .sym_name = n_strx,
3198 .relocation = .{
3199 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
3200 .sym_index = sym_index,
32013201 },
32023202 },
32033203 });
......@@ -4157,7 +4157,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
41574157 .data = .{
41584158 .payload = try self.addExtra(Mir.LoadMemoryPie{
41594159 .register = @enumToInt(src_reg),
4160 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.local_sym_index,
4160 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
41614161 .sym_index = sym_index,
41624162 }),
41634163 },
......@@ -4270,7 +4270,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
42704270 .data = .{
42714271 .payload = try self.addExtra(Mir.LoadMemoryPie{
42724272 .register = @enumToInt(reg),
4273 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.local_sym_index,
4273 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
42744274 .sym_index = sym_index,
42754275 }),
42764276 },
......@@ -4578,8 +4578,8 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
45784578 } else if (self.bin_file.cast(link.File.MachO)) |_| {
45794579 // Because MachO is PIE-always-on, we defer memory address resolution until
45804580 // the linker has enough info to perform relocations.
4581 assert(decl.link.macho.local_sym_index != 0);
4582 return MCValue{ .got_load = decl.link.macho.local_sym_index };
4581 assert(decl.link.macho.sym_index != 0);
4582 return MCValue{ .got_load = decl.link.macho.sym_index };
45834583 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
45844584 const got_addr = coff_file.offset_table_virtual_address + decl.link.coff.offset_table_index * ptr_bytes;
45854585 return MCValue{ .memory = got_addr };
src/arch/aarch64/Emit.zig+8-5
......@@ -649,7 +649,7 @@ fn mirDebugEpilogueBegin(self: *Emit) !void {
649649
650650fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
651651 assert(emit.mir.instructions.items(.tag)[inst] == .call_extern);
652 const extern_fn = emit.mir.instructions.items(.data)[inst].extern_fn;
652 const relocation = emit.mir.instructions.items(.data)[inst].relocation;
653653
654654 if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
655655 const offset = blk: {
......@@ -659,10 +659,13 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
659659 break :blk offset;
660660 };
661661 // Add relocation to the decl.
662 const atom = macho_file.atom_by_index_table.get(extern_fn.atom_index).?;
662 const atom = macho_file.atom_by_index_table.get(relocation.atom_index).?;
663663 try atom.relocs.append(emit.bin_file.allocator, .{
664664 .offset = offset,
665 .target = .{ .global = extern_fn.sym_name },
665 .target = .{
666 .sym_index = relocation.sym_index,
667 .file = null,
668 },
666669 .addend = 0,
667670 .subtractor = null,
668671 .pcrel = true,
......@@ -864,7 +867,7 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
864867 // Page reloc for adrp instruction.
865868 try atom.relocs.append(emit.bin_file.allocator, .{
866869 .offset = offset,
867 .target = .{ .local = data.sym_index },
870 .target = .{ .sym_index = data.sym_index, .file = null },
868871 .addend = 0,
869872 .subtractor = null,
870873 .pcrel = true,
......@@ -882,7 +885,7 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
882885 // Pageoff reloc for adrp instruction.
883886 try atom.relocs.append(emit.bin_file.allocator, .{
884887 .offset = offset + 4,
885 .target = .{ .local = data.sym_index },
888 .target = .{ .sym_index = data.sym_index, .file = null },
886889 .addend = 0,
887890 .subtractor = null,
888891 .pcrel = false,
src/arch/aarch64/Mir.zig+5-3
......@@ -225,14 +225,16 @@ pub const Inst = struct {
225225 ///
226226 /// Used by e.g. b
227227 inst: Index,
228 /// An extern function
228 /// Relocation for the linker where:
229 /// * `atom_index` is the index of the source
230 /// * `sym_index` is the index of the target
229231 ///
230232 /// Used by e.g. call_extern
231 extern_fn: struct {
233 relocation: struct {
232234 /// Index of the containing atom.
233235 atom_index: u32,
234236 /// Index into the linker's string table.
235 sym_name: u32,
237 sym_index: u32,
236238 },
237239 /// A 16-bit immediate value.
238240 ///
src/arch/riscv64/CodeGen.zig+1-1
......@@ -2563,7 +2563,7 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
25632563 } else if (self.bin_file.cast(link.File.MachO)) |_| {
25642564 // TODO I'm hacking my way through here by repurposing .memory for storing
25652565 // index to the GOT target symbol index.
2566 return MCValue{ .memory = decl.link.macho.local_sym_index };
2566 return MCValue{ .memory = decl.link.macho.sym_index };
25672567 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
25682568 const got_addr = coff_file.offset_table_virtual_address + decl.link.coff.offset_table_index * ptr_bytes;
25692569 return MCValue{ .memory = got_addr };
src/arch/x86_64/CodeGen.zig+9-9
......@@ -2644,8 +2644,8 @@ fn loadMemPtrIntoRegister(self: *Self, reg: Register, ptr_ty: Type, ptr: MCValue
26442644 .flags = flags,
26452645 }),
26462646 .data = .{
2647 .load_reloc = .{
2648 .atom_index = fn_owner_decl.link.macho.local_sym_index,
2647 .relocation = .{
2648 .atom_index = fn_owner_decl.link.macho.sym_index,
26492649 .sym_index = sym_index,
26502650 },
26512651 },
......@@ -3977,7 +3977,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
39773977 const func = func_payload.data;
39783978 const fn_owner_decl = mod.declPtr(func.owner_decl);
39793979 try self.genSetReg(Type.initTag(.usize), .rax, .{
3980 .got_load = fn_owner_decl.link.macho.local_sym_index,
3980 .got_load = fn_owner_decl.link.macho.sym_index,
39813981 });
39823982 // callq *%rax
39833983 _ = try self.addInst(.{
......@@ -3997,14 +3997,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
39973997 lib_name,
39983998 });
39993999 }
4000 const n_strx = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
4000 const sym_index = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
40014001 _ = try self.addInst(.{
40024002 .tag = .call_extern,
40034003 .ops = undefined,
40044004 .data = .{
4005 .extern_fn = .{
4006 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.local_sym_index,
4007 .sym_name = n_strx,
4005 .relocation = .{
4006 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
4007 .sym_index = sym_index,
40084008 },
40094009 },
40104010 });
......@@ -6771,8 +6771,8 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
67716771 } else if (self.bin_file.cast(link.File.MachO)) |_| {
67726772 // Because MachO is PIE-always-on, we defer memory address resolution until
67736773 // the linker has enough info to perform relocations.
6774 assert(decl.link.macho.local_sym_index != 0);
6775 return MCValue{ .got_load = decl.link.macho.local_sym_index };
6774 assert(decl.link.macho.sym_index != 0);
6775 return MCValue{ .got_load = decl.link.macho.sym_index };
67766776 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
67776777 const got_addr = coff_file.offset_table_virtual_address + decl.link.coff.offset_table_index * ptr_bytes;
67786778 return MCValue{ .memory = got_addr };
src/arch/x86_64/Emit.zig+10-7
......@@ -982,7 +982,7 @@ fn mirLeaPie(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
982982 const tag = emit.mir.instructions.items(.tag)[inst];
983983 assert(tag == .lea_pie);
984984 const ops = emit.mir.instructions.items(.ops)[inst].decode();
985 const load_reloc = emit.mir.instructions.items(.data)[inst].load_reloc;
985 const relocation = emit.mir.instructions.items(.data)[inst].relocation;
986986
987987 // lea reg1, [rip + reloc]
988988 // RM
......@@ -1001,11 +1001,11 @@ fn mirLeaPie(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
10011001 0b01 => @enumToInt(std.macho.reloc_type_x86_64.X86_64_RELOC_SIGNED),
10021002 else => return emit.fail("TODO unused LEA PIE variants 0b10 and 0b11", .{}),
10031003 };
1004 const atom = macho_file.atom_by_index_table.get(load_reloc.atom_index).?;
1005 log.debug("adding reloc of type {} to local @{d}", .{ reloc_type, load_reloc.sym_index });
1004 const atom = macho_file.atom_by_index_table.get(relocation.atom_index).?;
1005 log.debug("adding reloc of type {} to local @{d}", .{ reloc_type, relocation.sym_index });
10061006 try atom.relocs.append(emit.bin_file.allocator, .{
10071007 .offset = @intCast(u32, end_offset - 4),
1008 .target = .{ .local = load_reloc.sym_index },
1008 .target = .{ .sym_index = relocation.sym_index, .file = null },
10091009 .addend = 0,
10101010 .subtractor = null,
10111011 .pcrel = true,
......@@ -1116,7 +1116,7 @@ fn mirCmpFloatAvx(emit: *Emit, tag: Tag, inst: Mir.Inst.Index) InnerError!void {
11161116fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
11171117 const tag = emit.mir.instructions.items(.tag)[inst];
11181118 assert(tag == .call_extern);
1119 const extern_fn = emit.mir.instructions.items(.data)[inst].extern_fn;
1119 const relocation = emit.mir.instructions.items(.data)[inst].relocation;
11201120
11211121 const offset = blk: {
11221122 // callq
......@@ -1126,10 +1126,13 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
11261126
11271127 if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
11281128 // Add relocation to the decl.
1129 const atom = macho_file.atom_by_index_table.get(extern_fn.atom_index).?;
1129 const atom = macho_file.atom_by_index_table.get(relocation.atom_index).?;
11301130 try atom.relocs.append(emit.bin_file.allocator, .{
11311131 .offset = offset,
1132 .target = .{ .global = extern_fn.sym_name },
1132 .target = .{
1133 .sym_index = relocation.sym_index,
1134 .file = null,
1135 },
11331136 .addend = 0,
11341137 .subtractor = null,
11351138 .pcrel = true,
src/arch/x86_64/Mir.zig+6-11
......@@ -181,7 +181,7 @@ pub const Inst = struct {
181181 /// 0b00 reg1, [rip + reloc] // via GOT emits X86_64_RELOC_GOT relocation
182182 /// 0b01 reg1, [rip + reloc] // direct load emits X86_64_RELOC_SIGNED relocation
183183 /// Notes:
184 /// * `Data` contains `load_reloc`
184 /// * `Data` contains `relocation`
185185 lea_pie,
186186
187187 /// ops flags: form:
......@@ -368,7 +368,7 @@ pub const Inst = struct {
368368 /// Pseudo-instructions
369369 /// call extern function
370370 /// Notes:
371 /// * target of the call is stored as `extern_fn` in `Data` union.
371 /// * target of the call is stored as `relocation` in `Data` union.
372372 call_extern,
373373
374374 /// end of prologue
......@@ -439,15 +439,10 @@ pub const Inst = struct {
439439 /// A condition code for use with EFLAGS register.
440440 cc: bits.Condition,
441441 },
442 /// An extern function.
443 extern_fn: struct {
444 /// Index of the containing atom.
445 atom_index: u32,
446 /// Index into the linker's string table.
447 sym_name: u32,
448 },
449 /// PIE load relocation.
450 load_reloc: struct {
442 /// Relocation for the linker where:
443 /// * `atom_index` is the index of the source
444 /// * `sym_index` is the index of the target
445 relocation: struct {
451446 /// Index of the containing atom.
452447 atom_index: u32,
453448 /// Index into the linker's symbol table.
src/link.zig+1-6
......@@ -544,12 +544,7 @@ pub const File = struct {
544544 switch (base.tag) {
545545 .coff => return @fieldParentPtr(Coff, "base", base).allocateDeclIndexes(decl_index),
546546 .elf => return @fieldParentPtr(Elf, "base", base).allocateDeclIndexes(decl_index),
547 .macho => return @fieldParentPtr(MachO, "base", base).allocateDeclIndexes(decl_index) catch |err| switch (err) {
548 // remap this error code because we are transitioning away from
549 // `allocateDeclIndexes`.
550 error.Overflow => return error.OutOfMemory,
551 error.OutOfMemory => return error.OutOfMemory,
552 },
547 .macho => return @fieldParentPtr(MachO, "base", base).allocateDeclIndexes(decl_index),
553548 .wasm => return @fieldParentPtr(Wasm, "base", base).allocateDeclIndexes(decl_index),
554549 .plan9 => return @fieldParentPtr(Plan9, "base", base).allocateDeclIndexes(decl_index),
555550 .c, .spirv, .nvptx => {},
src/link/MachO.zig+2181-1981
......@@ -4,6 +4,7 @@ const std = @import("std");
44const build_options = @import("build_options");
55const builtin = @import("builtin");
66const assert = std.debug.assert;
7const dwarf = std.dwarf;
78const fmt = std.fmt;
89const fs = std.fs;
910const log = std.log.scoped(.link);
......@@ -15,6 +16,7 @@ const meta = std.meta;
1516const aarch64 = @import("../arch/aarch64/bits.zig");
1617const bind = @import("MachO/bind.zig");
1718const codegen = @import("../codegen.zig");
19const dead_strip = @import("MachO/dead_strip.zig");
1820const link = @import("../link.zig");
1921const llvm_backend = @import("../codegen/llvm.zig");
2022const target_util = @import("../target.zig");
......@@ -35,8 +37,7 @@ const LibStub = @import("tapi.zig").LibStub;
3537const Liveness = @import("../Liveness.zig");
3638const LlvmObject = @import("../codegen/llvm.zig").Object;
3739const Module = @import("../Module.zig");
38const StringIndexAdapter = std.hash_map.StringIndexAdapter;
39const StringIndexContext = std.hash_map.StringIndexContext;
40const StringTable = @import("strtab.zig").StringTable;
4041const Trie = @import("MachO/Trie.zig");
4142const Type = @import("../type.zig").Type;
4243const TypedValue = @import("../TypedValue.zig");
......@@ -52,6 +53,8 @@ pub const SearchStrategy = enum {
5253 dylibs_first,
5354};
5455
56pub const N_DESC_GCED: u16 = @bitCast(u16, @as(i16, -1));
57
5558const SystemLib = struct {
5659 needed: bool = false,
5760 weak: bool = false,
......@@ -69,10 +72,10 @@ d_sym: ?DebugSymbols = null,
6972/// For x86_64 that's 4KB, whereas for aarch64, that's 16KB.
7073page_size: u16,
7174
72/// If true, the linker will preallocate several sections and segments before starting the linking
73/// process. This is for example true for stage2 debug builds, however, this is false for stage1
74/// and potentially stage2 release builds in the future.
75needs_prealloc: bool = true,
75/// Mode of operation: incremental - will preallocate segments/sections and is compatible with
76/// watch and HCS modes of operation; one_shot - will link relocatables in a traditional, one-shot
77/// fashion (default for LLVM backend).
78mode: enum { incremental, one_shot },
7679
7780/// The absolute address of the entry point.
7881entry_addr: ?u64 = null,
......@@ -151,53 +154,48 @@ rustc_section_index: ?u16 = null,
151154rustc_section_size: u64 = 0,
152155
153156locals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
154globals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
155undefs: std.ArrayListUnmanaged(macho.nlist_64) = .{},
156symbol_resolver: std.AutoHashMapUnmanaged(u32, SymbolWithLoc) = .{},
157unresolved: std.AutoArrayHashMapUnmanaged(u32, enum {
158 none,
159 stub,
160 got,
161}) = .{},
162tentatives: std.AutoArrayHashMapUnmanaged(u32, void) = .{},
157globals: std.StringArrayHashMapUnmanaged(SymbolWithLoc) = .{},
158// FIXME Jakub
159// TODO storing index into globals might be dangerous if we delete a global
160// while not having everything resolved. Actually, perhaps `unresolved`
161// should not be stored at the global scope? Is this possible?
162// Otherwise, audit if this can be a problem.
163// An alternative, which I still need to investigate for perf reasons is to
164// store all global names in an adapted with context strtab.
165unresolved: std.AutoArrayHashMapUnmanaged(u32, bool) = .{},
163166
164167locals_free_list: std.ArrayListUnmanaged(u32) = .{},
165globals_free_list: std.ArrayListUnmanaged(u32) = .{},
166168
167169dyld_stub_binder_index: ?u32 = null,
168170dyld_private_atom: ?*Atom = null,
169171stub_helper_preamble_atom: ?*Atom = null,
170172
171mh_execute_header_sym_index: ?u32 = null,
172dso_handle_sym_index: ?u32 = null,
173
174strtab: std.ArrayListUnmanaged(u8) = .{},
175strtab_dir: std.HashMapUnmanaged(u32, void, StringIndexContext, std.hash_map.default_max_load_percentage) = .{},
173strtab: StringTable(.strtab) = .{},
176174
175// TODO I think synthetic tables are a perfect match for some generic refactoring,
176// and probably reusable between linker backends too.
177177tlv_ptr_entries: std.ArrayListUnmanaged(Entry) = .{},
178178tlv_ptr_entries_free_list: std.ArrayListUnmanaged(u32) = .{},
179tlv_ptr_entries_table: std.AutoArrayHashMapUnmanaged(Atom.Relocation.Target, u32) = .{},
179tlv_ptr_entries_table: std.AutoHashMapUnmanaged(SymbolWithLoc, u32) = .{},
180180
181181got_entries: std.ArrayListUnmanaged(Entry) = .{},
182182got_entries_free_list: std.ArrayListUnmanaged(u32) = .{},
183got_entries_table: std.AutoArrayHashMapUnmanaged(Atom.Relocation.Target, u32) = .{},
183got_entries_table: std.AutoHashMapUnmanaged(SymbolWithLoc, u32) = .{},
184184
185stubs: std.ArrayListUnmanaged(*Atom) = .{},
185stubs: std.ArrayListUnmanaged(Entry) = .{},
186186stubs_free_list: std.ArrayListUnmanaged(u32) = .{},
187stubs_table: std.AutoArrayHashMapUnmanaged(u32, u32) = .{},
187stubs_table: std.AutoHashMapUnmanaged(SymbolWithLoc, u32) = .{},
188188
189189error_flags: File.ErrorFlags = File.ErrorFlags{},
190190
191191load_commands_dirty: bool = false,
192192sections_order_dirty: bool = false,
193has_dices: bool = false,
194has_stabs: bool = false,
193
195194/// A helper var to indicate if we are at the start of the incremental updates, or
196195/// already somewhere further along the update-and-run chain.
197196/// TODO once we add opening a prelinked output binary from file, this will become
198197/// obsolete as we will carry on where we left off.
199cold_start: bool = false,
200invalidate_relocs: bool = false,
198cold_start: bool = true,
201199
202200section_ordinals: std.AutoArrayHashMapUnmanaged(MatchingSection, void) = .{},
203201
......@@ -221,12 +219,10 @@ atom_free_lists: std.AutoHashMapUnmanaged(MatchingSection, std.ArrayListUnmanage
221219/// Pointer to the last allocated atom
222220atoms: std.AutoHashMapUnmanaged(MatchingSection, *Atom) = .{},
223221
224/// List of atoms that are owned directly by the linker.
225/// Currently these are only atoms that are the result of linking
226/// object files. Atoms which take part in incremental linking are
227/// at present owned by Module.Decl.
228/// TODO consolidate this.
222/// List of atoms that are either synthetic or map directly to the Zig source program.
229223managed_atoms: std.ArrayListUnmanaged(*Atom) = .{},
224
225/// Table of atoms indexed by the symbol index.
230226atom_by_index_table: std.AutoHashMapUnmanaged(u32, *Atom) = .{},
231227
232228/// Table of unnamed constants associated with a parent `Decl`.
......@@ -257,8 +253,25 @@ unnamed_const_atoms: UnnamedConstTable = .{},
257253decls: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, ?MatchingSection) = .{},
258254
259255const Entry = struct {
260 target: Atom.Relocation.Target,
261 atom: *Atom,
256 target: SymbolWithLoc,
257 // Index into the synthetic symbol table (i.e., file == null).
258 sym_index: u32,
259
260 pub fn getSymbol(entry: Entry, macho_file: *MachO) macho.nlist_64 {
261 return macho_file.getSymbol(.{ .sym_index = entry.sym_index, .file = null });
262 }
263
264 pub fn getSymbolPtr(entry: Entry, macho_file: *MachO) *macho.nlist_64 {
265 return macho_file.getSymbolPtr(.{ .sym_index = entry.sym_index, .file = null });
266 }
267
268 pub fn getAtom(entry: Entry, macho_file: *MachO) *Atom {
269 return macho_file.getAtomForSymbol(.{ .sym_index = entry.sym_index, .file = null }).?;
270 }
271
272 pub fn getName(entry: Entry, macho_file: *MachO) []const u8 {
273 return macho_file.getSymbolName(.{ .sym_index = entry.sym_index, .file = null });
274 }
262275};
263276
264277const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(*Atom));
......@@ -269,15 +282,12 @@ const PendingUpdate = union(enum) {
269282 add_got_entry: u32,
270283};
271284
272const SymbolWithLoc = struct {
273 // Table where the symbol can be found.
274 where: enum {
275 global,
276 undef,
277 },
278 where_index: u32,
279 local_sym_index: u32 = 0,
280 file: ?u16 = null, // null means Zig module
285pub const SymbolWithLoc = struct {
286 // Index into the respective symbol table.
287 sym_index: u32,
288
289 // null means it's a synthetic global.
290 file: ?u32 = null,
281291};
282292
283293/// When allocating, the ideal_capacity is calculated by
......@@ -385,7 +395,7 @@ pub fn openPath(allocator: Allocator, options: link.Options) !*MachO {
385395 .n_desc = 0,
386396 .n_value = 0,
387397 });
388 try self.strtab.append(allocator, 0);
398 try self.strtab.buffer.append(allocator, 0);
389399
390400 try self.populateMissingMetadata();
391401
......@@ -406,7 +416,6 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*MachO {
406416 const requires_adhoc_codesig = cpu_arch == .aarch64 and (os_tag == .macos or abi == .simulator);
407417 const use_llvm = build_options.have_llvm and options.use_llvm;
408418 const use_stage1 = build_options.is_stage1 and options.use_stage1;
409 const needs_prealloc = !(use_stage1 or use_llvm or options.cache_mode == .whole);
410419
411420 const self = try gpa.create(MachO);
412421 errdefer gpa.destroy(self);
......@@ -419,14 +428,22 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*MachO {
419428 .file = null,
420429 },
421430 .page_size = page_size,
422 .code_signature = if (requires_adhoc_codesig) CodeSignature.init(page_size) else null,
423 .needs_prealloc = needs_prealloc,
431 .code_signature = if (requires_adhoc_codesig)
432 CodeSignature.init(page_size)
433 else
434 null,
435 .mode = if (use_stage1 or use_llvm or options.module == null or options.cache_mode == .whole)
436 .one_shot
437 else
438 .incremental,
424439 };
425440
426441 if (use_llvm and !use_stage1) {
427442 self.llvm_object = try LlvmObject.create(gpa, options);
428443 }
429444
445 log.debug("selected linker mode '{s}'", .{@tagName(self.mode)});
446
430447 return self;
431448}
432449
......@@ -448,33 +465,209 @@ pub fn flush(self: *MachO, comp: *Compilation, prog_node: *std.Progress.Node) !v
448465 return error.TODOImplementWritingStaticLibFiles;
449466 }
450467 }
451 return self.flushModule(comp, prog_node);
468
469 switch (self.mode) {
470 .one_shot => return self.linkOneShot(comp, prog_node),
471 .incremental => return self.flushModule(comp, prog_node),
472 }
452473}
453474
454475pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.Node) !void {
455476 const tracy = trace(@src());
456477 defer tracy.end();
457478
458 const use_stage1 = build_options.is_stage1 and self.base.options.use_stage1;
459
460 if (build_options.have_llvm and !use_stage1) {
479 if (build_options.have_llvm) {
461480 if (self.llvm_object) |llvm_object| {
462 try llvm_object.flushModule(comp, prog_node);
463
464 llvm_object.destroy(self.base.allocator);
465 self.llvm_object = null;
466
467 if (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Static) {
468 return;
469 }
481 return try llvm_object.flushModule(comp, prog_node);
470482 }
471483 }
472484
485 var arena_allocator = std.heap.ArenaAllocator.init(self.base.allocator);
486 defer arena_allocator.deinit();
487 const arena = arena_allocator.allocator();
488
473489 var sub_prog_node = prog_node.start("MachO Flush", 0);
474490 sub_prog_node.activate();
475491 defer sub_prog_node.end();
476492
477 var arena_allocator = std.heap.ArenaAllocator.init(self.base.allocator);
493 const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented;
494
495 if (self.d_sym) |*d_sym| {
496 try d_sym.dwarf.flushModule(&self.base, module);
497 }
498
499 var libs = std.StringArrayHashMap(SystemLib).init(arena);
500 try self.resolveLibSystem(arena, comp, &.{}, &libs);
501
502 const id_symlink_basename = "zld.id";
503
504 const cache_dir_handle = module.zig_cache_artifact_directory.handle;
505 var man: Cache.Manifest = undefined;
506 defer if (!self.base.options.disable_lld_caching) man.deinit();
507
508 var digest: [Cache.hex_digest_len]u8 = undefined;
509 man = comp.cache_parent.obtain();
510 self.base.releaseLock();
511
512 man.hash.addListOfBytes(libs.keys());
513
514 _ = try man.hit();
515 digest = man.final();
516
517 var prev_digest_buf: [digest.len]u8 = undefined;
518 const prev_digest: []u8 = Cache.readSmallFile(
519 cache_dir_handle,
520 id_symlink_basename,
521 &prev_digest_buf,
522 ) catch |err| blk: {
523 log.debug("MachO Zld new_digest={s} error: {s}", .{
524 std.fmt.fmtSliceHexLower(&digest),
525 @errorName(err),
526 });
527 // Handle this as a cache miss.
528 break :blk prev_digest_buf[0..0];
529 };
530 const cache_miss: bool = cache_miss: {
531 if (mem.eql(u8, prev_digest, &digest)) {
532 log.debug("MachO Zld digest={s} match", .{
533 std.fmt.fmtSliceHexLower(&digest),
534 });
535 if (!self.cold_start) {
536 log.debug(" skipping parsing linker line objects", .{});
537 break :cache_miss false;
538 } else {
539 log.debug(" TODO parse prelinked binary and continue linking where we left off", .{});
540 }
541 }
542 log.debug("MachO Zld prev_digest={s} new_digest={s}", .{
543 std.fmt.fmtSliceHexLower(prev_digest),
544 std.fmt.fmtSliceHexLower(&digest),
545 });
546 // We are about to change the output file to be different, so we invalidate the build hash now.
547 cache_dir_handle.deleteFile(id_symlink_basename) catch |err| switch (err) {
548 error.FileNotFound => {},
549 else => |e| return e,
550 };
551 break :cache_miss true;
552 };
553
554 if (cache_miss) {
555 for (self.dylibs.items) |*dylib| {
556 dylib.deinit(self.base.allocator);
557 }
558 self.dylibs.clearRetainingCapacity();
559 self.dylibs_map.clearRetainingCapacity();
560 self.referenced_dylibs.clearRetainingCapacity();
561
562 var dependent_libs = std.fifo.LinearFifo(struct {
563 id: Dylib.Id,
564 parent: u16,
565 }, .Dynamic).init(self.base.allocator);
566 defer dependent_libs.deinit();
567 try self.parseLibs(libs.keys(), libs.values(), self.base.options.sysroot, &dependent_libs);
568 try self.parseDependentLibs(self.base.options.sysroot, &dependent_libs);
569 }
570
571 try self.createMhExecuteHeaderSymbol();
572 try self.resolveDyldStubBinder();
573 try self.createDyldPrivateAtom();
574 try self.createStubHelperPreambleAtom();
575 try self.resolveSymbolsInDylibs();
576 try self.addCodeSignatureLC();
577
578 if (self.unresolved.count() > 0) {
579 return error.UndefinedSymbolReference;
580 }
581
582 try self.allocateSpecialSymbols();
583
584 if (build_options.enable_logging) {
585 self.logSymtab();
586 self.logSectionOrdinals();
587 self.logAtoms();
588 }
589
590 try self.writeAtomsIncremental();
591
592 try self.setEntryPoint();
593 try self.updateSectionOrdinals();
594 try self.writeLinkeditSegment();
595
596 if (self.d_sym) |*d_sym| {
597 // Flush debug symbols bundle.
598 try d_sym.flushModule(self.base.allocator, self.base.options);
599 }
600
601 // code signature and entitlements
602 if (self.base.options.entitlements) |path| {
603 if (self.code_signature) |*csig| {
604 try csig.addEntitlements(self.base.allocator, path);
605 csig.code_directory.ident = self.base.options.emit.?.sub_path;
606 } else {
607 var csig = CodeSignature.init(self.page_size);
608 try csig.addEntitlements(self.base.allocator, path);
609 csig.code_directory.ident = self.base.options.emit.?.sub_path;
610 self.code_signature = csig;
611 }
612 }
613
614 if (self.code_signature) |*csig| {
615 csig.clear(self.base.allocator);
616 csig.code_directory.ident = self.base.options.emit.?.sub_path;
617 // Preallocate space for the code signature.
618 // We need to do this at this stage so that we have the load commands with proper values
619 // written out to the file.
620 // The most important here is to have the correct vm and filesize of the __LINKEDIT segment
621 // where the code signature goes into.
622 try self.writeCodeSignaturePadding(csig);
623 }
624
625 try self.writeLoadCommands();
626 try self.writeHeader();
627
628 if (self.entry_addr == null and self.base.options.output_mode == .Exe) {
629 log.debug("flushing. no_entry_point_found = true", .{});
630 self.error_flags.no_entry_point_found = true;
631 } else {
632 log.debug("flushing. no_entry_point_found = false", .{});
633 self.error_flags.no_entry_point_found = false;
634 }
635
636 assert(!self.load_commands_dirty);
637
638 if (self.code_signature) |*csig| {
639 try self.writeCodeSignature(csig); // code signing always comes last
640 }
641
642 if (build_options.enable_link_snapshots) {
643 if (self.base.options.enable_link_snapshots)
644 try self.snapshotState();
645 }
646
647 if (cache_miss) {
648 // Update the file with the digest. If it fails we can continue; it only
649 // means that the next invocation will have an unnecessary cache miss.
650 Cache.writeSmallFile(cache_dir_handle, id_symlink_basename, &digest) catch |err| {
651 log.debug("failed to save linking hash digest file: {s}", .{@errorName(err)});
652 };
653 // Again failure here only means an unnecessary cache miss.
654 man.writeManifest() catch |err| {
655 log.debug("failed to write cache manifest when linking: {s}", .{@errorName(err)});
656 };
657 // We hang on to this lock so that the output file path can be used without
658 // other processes clobbering it.
659 self.base.lock = man.toOwnedLock();
660 }
661
662 self.cold_start = false;
663}
664
665fn linkOneShot(self: *MachO, comp: *Compilation, prog_node: *std.Progress.Node) !void {
666 const tracy = trace(@src());
667 defer tracy.end();
668
669 const gpa = self.base.allocator;
670 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
478671 defer arena_allocator.deinit();
479672 const arena = arena_allocator.allocator();
480673
......@@ -484,7 +677,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
484677 // If there is no Zig code to compile, then we should skip flushing the output file because it
485678 // will not be part of the linker line anyway.
486679 const module_obj_path: ?[]const u8 = if (self.base.options.module) |module| blk: {
487 if (use_stage1) {
680 if (self.base.options.use_stage1) {
488681 const obj_basename = try std.zig.binNameAlloc(arena, .{
489682 .root_name = self.base.options.root_name,
490683 .target = self.base.options.target,
......@@ -501,48 +694,35 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
501694 }
502695 }
503696
504 const obj_basename = self.base.intermediary_basename orelse break :blk null;
697 try self.flushModule(comp, prog_node);
505698
506699 if (fs.path.dirname(full_out_path)) |dirname| {
507 break :blk try fs.path.join(arena, &.{ dirname, obj_basename });
700 break :blk try fs.path.join(arena, &.{ dirname, self.base.intermediary_basename.? });
508701 } else {
509 break :blk obj_basename;
702 break :blk self.base.intermediary_basename.?;
510703 }
511704 } else null;
512705
513 if (self.d_sym) |*d_sym| {
514 if (self.base.options.module) |module| {
515 try d_sym.dwarf.flushModule(&self.base, module);
516 }
517 }
706 var sub_prog_node = prog_node.start("MachO Flush", 0);
707 sub_prog_node.activate();
708 sub_prog_node.context.refresh();
709 defer sub_prog_node.end();
518710
519711 const is_lib = self.base.options.output_mode == .Lib;
520712 const is_dyn_lib = self.base.options.link_mode == .Dynamic and is_lib;
521713 const is_exe_or_dyn_lib = is_dyn_lib or self.base.options.output_mode == .Exe;
522714 const stack_size = self.base.options.stack_size_override orelse 0;
523 const allow_undef = is_dyn_lib and (self.base.options.allow_shlib_undefined orelse false);
715 const is_debug_build = self.base.options.optimize_mode == .Debug;
716 const gc_sections = self.base.options.gc_sections orelse !is_debug_build;
524717
525718 const id_symlink_basename = "zld.id";
526 const cache_dir_handle = blk: {
527 if (use_stage1) {
528 break :blk directory.handle;
529 }
530 if (self.base.options.module) |module| {
531 break :blk module.zig_cache_artifact_directory.handle;
532 }
533 break :blk directory.handle;
534 };
535719
536720 var man: Cache.Manifest = undefined;
537721 defer if (!self.base.options.disable_lld_caching) man.deinit();
538722
539723 var digest: [Cache.hex_digest_len]u8 = undefined;
540 var needs_full_relink = true;
541
542 cache: {
543 if ((use_stage1 and self.base.options.disable_lld_caching) or self.base.options.cache_mode == .whole)
544 break :cache;
545724
725 if (!self.base.options.disable_lld_caching) {
546726 man = comp.cache_parent.obtain();
547727
548728 // We are about to obtain this lock, so here we give other processes a chance first.
......@@ -565,7 +745,9 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
565745 man.hash.addOptional(self.base.options.search_strategy);
566746 man.hash.addOptional(self.base.options.headerpad_size);
567747 man.hash.add(self.base.options.headerpad_max_install_names);
748 man.hash.add(gc_sections);
568749 man.hash.add(self.base.options.dead_strip_dylibs);
750 man.hash.add(self.base.options.strip);
569751 man.hash.addListOfBytes(self.base.options.lib_dirs);
570752 man.hash.addListOfBytes(self.base.options.framework_dirs);
571753 link.hashAddSystemLibs(&man.hash, self.base.options.frameworks);
......@@ -584,7 +766,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
584766
585767 var prev_digest_buf: [digest.len]u8 = undefined;
586768 const prev_digest: []u8 = Cache.readSmallFile(
587 cache_dir_handle,
769 directory.handle,
588770 id_symlink_basename,
589771 &prev_digest_buf,
590772 ) catch |err| blk: {
......@@ -597,23 +779,11 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
597779 };
598780 if (mem.eql(u8, prev_digest, &digest)) {
599781 // Hot diggity dog! The output binary is already there.
600
601 const use_llvm = build_options.have_llvm and self.base.options.use_llvm;
602 if (use_llvm or use_stage1) {
603 log.debug("MachO Zld digest={s} match - skipping invocation", .{std.fmt.fmtSliceHexLower(&digest)});
604 self.base.lock = man.toOwnedLock();
605 return;
606 } else {
607 log.debug("MachO Zld digest={s} match", .{std.fmt.fmtSliceHexLower(&digest)});
608 if (!self.cold_start) {
609 log.debug(" no need to relink objects", .{});
610 needs_full_relink = false;
611 } else {
612 log.debug(" TODO parse prelinked binary and continue linking where we left off", .{});
613 // TODO until such time however, perform a full relink of objects.
614 needs_full_relink = true;
615 }
616 }
782 log.debug("MachO Zld digest={s} match - skipping invocation", .{
783 std.fmt.fmtSliceHexLower(&digest),
784 });
785 self.base.lock = man.toOwnedLock();
786 return;
617787 }
618788 log.debug("MachO Zld prev_digest={s} new_digest={s}", .{
619789 std.fmt.fmtSliceHexLower(prev_digest),
......@@ -621,7 +791,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
621791 });
622792
623793 // We are about to change the output file to be different, so we invalidate the build hash now.
624 cache_dir_handle.deleteFile(id_symlink_basename) catch |err| switch (err) {
794 directory.handle.deleteFile(id_symlink_basename) catch |err| switch (err) {
625795 error.FileNotFound => {},
626796 else => |e| return e,
627797 };
......@@ -652,450 +822,350 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
652822 try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{});
653823 }
654824 } else {
655 if (use_stage1) {
656 const sub_path = self.base.options.emit.?.sub_path;
657 self.base.file = try cache_dir_handle.createFile(sub_path, .{
658 .truncate = true,
659 .read = true,
660 .mode = link.determineMode(self.base.options),
661 });
662 // Index 0 is always a null symbol.
663 try self.locals.append(self.base.allocator, .{
664 .n_strx = 0,
665 .n_type = 0,
666 .n_sect = 0,
667 .n_desc = 0,
668 .n_value = 0,
669 });
670 try self.strtab.append(self.base.allocator, 0);
671 try self.populateMissingMetadata();
672 }
825 const sub_path = self.base.options.emit.?.sub_path;
826 self.base.file = try directory.handle.createFile(sub_path, .{
827 .truncate = true,
828 .read = true,
829 .mode = link.determineMode(self.base.options),
830 });
831 // Index 0 is always a null symbol.
832 try self.locals.append(gpa, .{
833 .n_strx = 0,
834 .n_type = 0,
835 .n_sect = 0,
836 .n_desc = 0,
837 .n_value = 0,
838 });
839 try self.strtab.buffer.append(gpa, 0);
840 try self.populateMissingMetadata();
673841
674842 var lib_not_found = false;
675843 var framework_not_found = false;
676844
677 if (needs_full_relink) {
678 for (self.objects.items) |*object| {
679 object.free(self.base.allocator, self);
680 object.deinit(self.base.allocator);
681 }
682 self.objects.clearRetainingCapacity();
683
684 for (self.archives.items) |*archive| {
685 archive.deinit(self.base.allocator);
686 }
687 self.archives.clearRetainingCapacity();
845 // Positional arguments to the linker such as object files and static archives.
846 var positionals = std.ArrayList([]const u8).init(arena);
847 try positionals.ensureUnusedCapacity(self.base.options.objects.len);
688848
689 for (self.dylibs.items) |*dylib| {
690 dylib.deinit(self.base.allocator);
691 }
692 self.dylibs.clearRetainingCapacity();
693 self.dylibs_map.clearRetainingCapacity();
694 self.referenced_dylibs.clearRetainingCapacity();
695
696 {
697 var to_remove = std.ArrayList(u32).init(self.base.allocator);
698 defer to_remove.deinit();
699 var it = self.symbol_resolver.iterator();
700 while (it.next()) |entry| {
701 const key = entry.key_ptr.*;
702 const value = entry.value_ptr.*;
703 if (value.file != null) {
704 try to_remove.append(key);
705 }
706 }
849 var must_link_archives = std.StringArrayHashMap(void).init(arena);
850 try must_link_archives.ensureUnusedCapacity(self.base.options.objects.len);
707851
708 for (to_remove.items) |key| {
709 if (self.symbol_resolver.fetchRemove(key)) |entry| {
710 const resolv = entry.value;
711 switch (resolv.where) {
712 .global => {
713 self.globals_free_list.append(self.base.allocator, resolv.where_index) catch {};
714 const sym = &self.globals.items[resolv.where_index];
715 sym.n_strx = 0;
716 sym.n_type = 0;
717 sym.n_value = 0;
718 },
719 .undef => {
720 const sym = &self.undefs.items[resolv.where_index];
721 sym.n_strx = 0;
722 sym.n_desc = 0;
723 },
724 }
725 if (self.got_entries_table.get(.{ .global = entry.key })) |i| {
726 self.got_entries_free_list.append(self.base.allocator, @intCast(u32, i)) catch {};
727 self.got_entries.items[i] = .{ .target = .{ .local = 0 }, .atom = undefined };
728 _ = self.got_entries_table.swapRemove(.{ .global = entry.key });
729 }
730 if (self.stubs_table.get(entry.key)) |i| {
731 self.stubs_free_list.append(self.base.allocator, @intCast(u32, i)) catch {};
732 self.stubs.items[i] = undefined;
733 _ = self.stubs_table.swapRemove(entry.key);
734 }
735 }
736 }
852 for (self.base.options.objects) |obj| {
853 if (must_link_archives.contains(obj.path)) continue;
854 if (obj.must_link) {
855 _ = must_link_archives.getOrPutAssumeCapacity(obj.path);
856 } else {
857 _ = positionals.appendAssumeCapacity(obj.path);
737858 }
738 // Invalidate all relocs
739 // TODO we only need to invalidate the backlinks to the relinked atoms from
740 // the relocatable object files.
741 self.invalidate_relocs = true;
742
743 // Positional arguments to the linker such as object files and static archives.
744 var positionals = std.ArrayList([]const u8).init(arena);
745 try positionals.ensureUnusedCapacity(self.base.options.objects.len);
859 }
746860
747 var must_link_archives = std.StringArrayHashMap(void).init(arena);
748 try must_link_archives.ensureUnusedCapacity(self.base.options.objects.len);
861 for (comp.c_object_table.keys()) |key| {
862 try positionals.append(key.status.success.object_path);
863 }
749864
750 for (self.base.options.objects) |obj| {
751 if (must_link_archives.contains(obj.path)) continue;
752 if (obj.must_link) {
753 _ = must_link_archives.getOrPutAssumeCapacity(obj.path);
754 } else {
755 _ = positionals.appendAssumeCapacity(obj.path);
756 }
757 }
865 if (module_obj_path) |p| {
866 try positionals.append(p);
867 }
758868
759 for (comp.c_object_table.keys()) |key| {
760 try positionals.append(key.status.success.object_path);
761 }
869 if (comp.compiler_rt_lib) |lib| {
870 try positionals.append(lib.full_object_path);
871 }
762872
763 if (module_obj_path) |p| {
764 try positionals.append(p);
765 }
873 // libc++ dep
874 if (self.base.options.link_libcpp) {
875 try positionals.append(comp.libcxxabi_static_lib.?.full_object_path);
876 try positionals.append(comp.libcxx_static_lib.?.full_object_path);
877 }
766878
767 if (comp.compiler_rt_lib) |lib| {
768 try positionals.append(lib.full_object_path);
769 }
879 // Shared and static libraries passed via `-l` flag.
880 var candidate_libs = std.StringArrayHashMap(SystemLib).init(arena);
770881
771 // libc++ dep
772 if (self.base.options.link_libcpp) {
773 try positionals.append(comp.libcxxabi_static_lib.?.full_object_path);
774 try positionals.append(comp.libcxx_static_lib.?.full_object_path);
882 const system_lib_names = self.base.options.system_libs.keys();
883 for (system_lib_names) |system_lib_name| {
884 // By this time, we depend on these libs being dynamically linked libraries and not static libraries
885 // (the check for that needs to be earlier), but they could be full paths to .dylib files, in which
886 // case we want to avoid prepending "-l".
887 if (Compilation.classifyFileExt(system_lib_name) == .shared_library) {
888 try positionals.append(system_lib_name);
889 continue;
775890 }
776891
777 // Shared and static libraries passed via `-l` flag.
778 var candidate_libs = std.StringArrayHashMap(SystemLib).init(arena);
779
780 const system_lib_names = self.base.options.system_libs.keys();
781 for (system_lib_names) |system_lib_name| {
782 // By this time, we depend on these libs being dynamically linked libraries and not static libraries
783 // (the check for that needs to be earlier), but they could be full paths to .dylib files, in which
784 // case we want to avoid prepending "-l".
785 if (Compilation.classifyFileExt(system_lib_name) == .shared_library) {
786 try positionals.append(system_lib_name);
787 continue;
788 }
789
790 const system_lib_info = self.base.options.system_libs.get(system_lib_name).?;
791 try candidate_libs.put(system_lib_name, .{
792 .needed = system_lib_info.needed,
793 .weak = system_lib_info.weak,
794 });
795 }
892 const system_lib_info = self.base.options.system_libs.get(system_lib_name).?;
893 try candidate_libs.put(system_lib_name, .{
894 .needed = system_lib_info.needed,
895 .weak = system_lib_info.weak,
896 });
897 }
796898
797 var lib_dirs = std.ArrayList([]const u8).init(arena);
798 for (self.base.options.lib_dirs) |dir| {
799 if (try resolveSearchDir(arena, dir, self.base.options.sysroot)) |search_dir| {
800 try lib_dirs.append(search_dir);
801 } else {
802 log.warn("directory not found for '-L{s}'", .{dir});
803 }
899 var lib_dirs = std.ArrayList([]const u8).init(arena);
900 for (self.base.options.lib_dirs) |dir| {
901 if (try resolveSearchDir(arena, dir, self.base.options.sysroot)) |search_dir| {
902 try lib_dirs.append(search_dir);
903 } else {
904 log.warn("directory not found for '-L{s}'", .{dir});
804905 }
906 }
805907
806 var libs = std.StringArrayHashMap(SystemLib).init(arena);
807
808 // Assume ld64 default -search_paths_first if no strategy specified.
809 const search_strategy = self.base.options.search_strategy orelse .paths_first;
810 outer: for (candidate_libs.keys()) |lib_name| {
811 switch (search_strategy) {
812 .paths_first => {
813 // Look in each directory for a dylib (stub first), and then for archive
814 for (lib_dirs.items) |dir| {
815 for (&[_][]const u8{ ".tbd", ".dylib", ".a" }) |ext| {
816 if (try resolveLib(arena, dir, lib_name, ext)) |full_path| {
817 try libs.put(full_path, candidate_libs.get(lib_name).?);
818 continue :outer;
819 }
908 var libs = std.StringArrayHashMap(SystemLib).init(arena);
909
910 // Assume ld64 default -search_paths_first if no strategy specified.
911 const search_strategy = self.base.options.search_strategy orelse .paths_first;
912 outer: for (candidate_libs.keys()) |lib_name| {
913 switch (search_strategy) {
914 .paths_first => {
915 // Look in each directory for a dylib (stub first), and then for archive
916 for (lib_dirs.items) |dir| {
917 for (&[_][]const u8{ ".tbd", ".dylib", ".a" }) |ext| {
918 if (try resolveLib(arena, dir, lib_name, ext)) |full_path| {
919 try libs.put(full_path, candidate_libs.get(lib_name).?);
920 continue :outer;
820921 }
821 } else {
822 log.warn("library not found for '-l{s}'", .{lib_name});
823 lib_not_found = true;
824922 }
825 },
826 .dylibs_first => {
827 // First, look for a dylib in each search dir
828 for (lib_dirs.items) |dir| {
829 for (&[_][]const u8{ ".tbd", ".dylib" }) |ext| {
830 if (try resolveLib(arena, dir, lib_name, ext)) |full_path| {
831 try libs.put(full_path, candidate_libs.get(lib_name).?);
832 continue :outer;
833 }
834 }
835 } else for (lib_dirs.items) |dir| {
836 if (try resolveLib(arena, dir, lib_name, ".a")) |full_path| {
923 } else {
924 log.warn("library not found for '-l{s}'", .{lib_name});
925 lib_not_found = true;
926 }
927 },
928 .dylibs_first => {
929 // First, look for a dylib in each search dir
930 for (lib_dirs.items) |dir| {
931 for (&[_][]const u8{ ".tbd", ".dylib" }) |ext| {
932 if (try resolveLib(arena, dir, lib_name, ext)) |full_path| {
837933 try libs.put(full_path, candidate_libs.get(lib_name).?);
838 } else {
839 log.warn("library not found for '-l{s}'", .{lib_name});
840 lib_not_found = true;
934 continue :outer;
841935 }
842936 }
843 },
844 }
845 }
846
847 if (lib_not_found) {
848 log.warn("Library search paths:", .{});
849 for (lib_dirs.items) |dir| {
850 log.warn(" {s}", .{dir});
851 }
852 }
853
854 // If we were given the sysroot, try to look there first for libSystem.B.{dylib, tbd}.
855 var libsystem_available = false;
856 if (self.base.options.sysroot != null) blk: {
857 // Try stub file first. If we hit it, then we're done as the stub file
858 // re-exports every single symbol definition.
859 for (lib_dirs.items) |dir| {
860 if (try resolveLib(arena, dir, "System", ".tbd")) |full_path| {
861 try libs.put(full_path, .{ .needed = true });
862 libsystem_available = true;
863 break :blk;
864 }
865 }
866 // If we didn't hit the stub file, try .dylib next. However, libSystem.dylib
867 // doesn't export libc.dylib which we'll need to resolve subsequently also.
868 for (lib_dirs.items) |dir| {
869 if (try resolveLib(arena, dir, "System", ".dylib")) |libsystem_path| {
870 if (try resolveLib(arena, dir, "c", ".dylib")) |libc_path| {
871 try libs.put(libsystem_path, .{ .needed = true });
872 try libs.put(libc_path, .{ .needed = true });
873 libsystem_available = true;
874 break :blk;
937 } else for (lib_dirs.items) |dir| {
938 if (try resolveLib(arena, dir, lib_name, ".a")) |full_path| {
939 try libs.put(full_path, candidate_libs.get(lib_name).?);
940 } else {
941 log.warn("library not found for '-l{s}'", .{lib_name});
942 lib_not_found = true;
875943 }
876944 }
877 }
945 },
878946 }
879 if (!libsystem_available) {
880 const libsystem_name = try std.fmt.allocPrint(arena, "libSystem.{d}.tbd", .{
881 self.base.options.target.os.version_range.semver.min.major,
882 });
883 const full_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
884 "libc", "darwin", libsystem_name,
885 });
886 try libs.put(full_path, .{ .needed = true });
947 }
948
949 if (lib_not_found) {
950 log.warn("Library search paths:", .{});
951 for (lib_dirs.items) |dir| {
952 log.warn(" {s}", .{dir});
887953 }
954 }
888955
889 // frameworks
890 var framework_dirs = std.ArrayList([]const u8).init(arena);
891 for (self.base.options.framework_dirs) |dir| {
892 if (try resolveSearchDir(arena, dir, self.base.options.sysroot)) |search_dir| {
893 try framework_dirs.append(search_dir);
894 } else {
895 log.warn("directory not found for '-F{s}'", .{dir});
896 }
956 try self.resolveLibSystem(arena, comp, lib_dirs.items, &libs);
957
958 // frameworks
959 var framework_dirs = std.ArrayList([]const u8).init(arena);
960 for (self.base.options.framework_dirs) |dir| {
961 if (try resolveSearchDir(arena, dir, self.base.options.sysroot)) |search_dir| {
962 try framework_dirs.append(search_dir);
963 } else {
964 log.warn("directory not found for '-F{s}'", .{dir});
897965 }
966 }
898967
899 outer: for (self.base.options.frameworks.keys()) |f_name| {
900 for (framework_dirs.items) |dir| {
901 for (&[_][]const u8{ ".tbd", ".dylib", "" }) |ext| {
902 if (try resolveFramework(arena, dir, f_name, ext)) |full_path| {
903 const info = self.base.options.frameworks.get(f_name).?;
904 try libs.put(full_path, .{
905 .needed = info.needed,
906 .weak = info.weak,
907 });
908 continue :outer;
909 }
968 outer: for (self.base.options.frameworks.keys()) |f_name| {
969 for (framework_dirs.items) |dir| {
970 for (&[_][]const u8{ ".tbd", ".dylib", "" }) |ext| {
971 if (try resolveFramework(arena, dir, f_name, ext)) |full_path| {
972 const info = self.base.options.frameworks.get(f_name).?;
973 try libs.put(full_path, .{
974 .needed = info.needed,
975 .weak = info.weak,
976 });
977 continue :outer;
910978 }
911 } else {
912 log.warn("framework not found for '-framework {s}'", .{f_name});
913 framework_not_found = true;
914979 }
980 } else {
981 log.warn("framework not found for '-framework {s}'", .{f_name});
982 framework_not_found = true;
915983 }
984 }
916985
917 if (framework_not_found) {
918 log.warn("Framework search paths:", .{});
919 for (framework_dirs.items) |dir| {
920 log.warn(" {s}", .{dir});
921 }
986 if (framework_not_found) {
987 log.warn("Framework search paths:", .{});
988 for (framework_dirs.items) |dir| {
989 log.warn(" {s}", .{dir});
922990 }
991 }
923992
924 // rpaths
925 var rpath_table = std.StringArrayHashMap(void).init(arena);
926 for (self.base.options.rpath_list) |rpath| {
927 if (rpath_table.contains(rpath)) continue;
928 const cmdsize = @intCast(u32, mem.alignForwardGeneric(
929 u64,
930 @sizeOf(macho.rpath_command) + rpath.len + 1,
931 @sizeOf(u64),
932 ));
933 var rpath_cmd = macho.emptyGenericCommandWithData(macho.rpath_command{
934 .cmdsize = cmdsize,
935 .path = @sizeOf(macho.rpath_command),
936 });
937 rpath_cmd.data = try self.base.allocator.alloc(u8, cmdsize - rpath_cmd.inner.path);
938 mem.set(u8, rpath_cmd.data, 0);
939 mem.copy(u8, rpath_cmd.data, rpath);
940 try self.load_commands.append(self.base.allocator, .{ .rpath = rpath_cmd });
941 try rpath_table.putNoClobber(rpath, {});
942 self.load_commands_dirty = true;
943 }
993 // rpaths
994 var rpath_table = std.StringArrayHashMap(void).init(arena);
995 for (self.base.options.rpath_list) |rpath| {
996 if (rpath_table.contains(rpath)) continue;
997 const cmdsize = @intCast(u32, mem.alignForwardGeneric(
998 u64,
999 @sizeOf(macho.rpath_command) + rpath.len + 1,
1000 @sizeOf(u64),
1001 ));
1002 var rpath_cmd = macho.emptyGenericCommandWithData(macho.rpath_command{
1003 .cmdsize = cmdsize,
1004 .path = @sizeOf(macho.rpath_command),
1005 });
1006 rpath_cmd.data = try gpa.alloc(u8, cmdsize - rpath_cmd.inner.path);
1007 mem.set(u8, rpath_cmd.data, 0);
1008 mem.copy(u8, rpath_cmd.data, rpath);
1009 try self.load_commands.append(gpa, .{ .rpath = rpath_cmd });
1010 try rpath_table.putNoClobber(rpath, {});
1011 self.load_commands_dirty = true;
1012 }
9441013
945 // code signature and entitlements
946 if (self.base.options.entitlements) |path| {
947 if (self.code_signature) |*csig| {
948 try csig.addEntitlements(self.base.allocator, path);
949 csig.code_directory.ident = self.base.options.emit.?.sub_path;
950 } else {
951 var csig = CodeSignature.init(self.page_size);
952 try csig.addEntitlements(self.base.allocator, path);
953 csig.code_directory.ident = self.base.options.emit.?.sub_path;
954 self.code_signature = csig;
955 }
1014 // code signature and entitlements
1015 if (self.base.options.entitlements) |path| {
1016 if (self.code_signature) |*csig| {
1017 try csig.addEntitlements(gpa, path);
1018 csig.code_directory.ident = self.base.options.emit.?.sub_path;
1019 } else {
1020 var csig = CodeSignature.init(self.page_size);
1021 try csig.addEntitlements(gpa, path);
1022 csig.code_directory.ident = self.base.options.emit.?.sub_path;
1023 self.code_signature = csig;
9561024 }
1025 }
9571026
958 if (self.base.options.verbose_link) {
959 var argv = std.ArrayList([]const u8).init(arena);
960
961 try argv.append("zig");
962 try argv.append("ld");
963
964 if (is_exe_or_dyn_lib) {
965 try argv.append("-dynamic");
966 }
1027 if (self.base.options.verbose_link) {
1028 var argv = std.ArrayList([]const u8).init(arena);
9671029
968 if (is_dyn_lib) {
969 try argv.append("-dylib");
1030 try argv.append("zig");
1031 try argv.append("ld");
9701032
971 if (self.base.options.install_name) |install_name| {
972 try argv.append("-install_name");
973 try argv.append(install_name);
974 }
975 }
1033 if (is_exe_or_dyn_lib) {
1034 try argv.append("-dynamic");
1035 }
9761036
977 if (self.base.options.sysroot) |syslibroot| {
978 try argv.append("-syslibroot");
979 try argv.append(syslibroot);
980 }
1037 if (is_dyn_lib) {
1038 try argv.append("-dylib");
9811039
982 for (rpath_table.keys()) |rpath| {
983 try argv.append("-rpath");
984 try argv.append(rpath);
1040 if (self.base.options.install_name) |install_name| {
1041 try argv.append("-install_name");
1042 try argv.append(install_name);
9851043 }
1044 }
9861045
987 if (self.base.options.pagezero_size) |pagezero_size| {
988 try argv.append("-pagezero_size");
989 try argv.append(try std.fmt.allocPrint(arena, "0x{x}", .{pagezero_size}));
990 }
1046 if (self.base.options.sysroot) |syslibroot| {
1047 try argv.append("-syslibroot");
1048 try argv.append(syslibroot);
1049 }
9911050
992 if (self.base.options.search_strategy) |strat| switch (strat) {
993 .paths_first => try argv.append("-search_paths_first"),
994 .dylibs_first => try argv.append("-search_dylibs_first"),
995 };
1051 for (rpath_table.keys()) |rpath| {
1052 try argv.append("-rpath");
1053 try argv.append(rpath);
1054 }
9961055
997 if (self.base.options.headerpad_size) |headerpad_size| {
998 try argv.append("-headerpad_size");
999 try argv.append(try std.fmt.allocPrint(arena, "0x{x}", .{headerpad_size}));
1000 }
1056 if (self.base.options.pagezero_size) |pagezero_size| {
1057 try argv.append("-pagezero_size");
1058 try argv.append(try std.fmt.allocPrint(arena, "0x{x}", .{pagezero_size}));
1059 }
10011060
1002 if (self.base.options.headerpad_max_install_names) {
1003 try argv.append("-headerpad_max_install_names");
1004 }
1061 if (self.base.options.search_strategy) |strat| switch (strat) {
1062 .paths_first => try argv.append("-search_paths_first"),
1063 .dylibs_first => try argv.append("-search_dylibs_first"),
1064 };
10051065
1006 if (self.base.options.dead_strip_dylibs) {
1007 try argv.append("-dead_strip_dylibs");
1008 }
1066 if (self.base.options.headerpad_size) |headerpad_size| {
1067 try argv.append("-headerpad_size");
1068 try argv.append(try std.fmt.allocPrint(arena, "0x{x}", .{headerpad_size}));
1069 }
10091070
1010 if (self.base.options.entry) |entry| {
1011 try argv.append("-e");
1012 try argv.append(entry);
1013 }
1071 if (self.base.options.headerpad_max_install_names) {
1072 try argv.append("-headerpad_max_install_names");
1073 }
10141074
1015 for (self.base.options.objects) |obj| {
1016 try argv.append(obj.path);
1017 }
1075 if (gc_sections) {
1076 try argv.append("-dead_strip");
1077 }
10181078
1019 for (comp.c_object_table.keys()) |key| {
1020 try argv.append(key.status.success.object_path);
1021 }
1079 if (self.base.options.dead_strip_dylibs) {
1080 try argv.append("-dead_strip_dylibs");
1081 }
10221082
1023 if (module_obj_path) |p| {
1024 try argv.append(p);
1025 }
1083 if (self.base.options.entry) |entry| {
1084 try argv.append("-e");
1085 try argv.append(entry);
1086 }
10261087
1027 if (comp.compiler_rt_lib) |lib| {
1028 try argv.append(lib.full_object_path);
1029 }
1088 for (self.base.options.objects) |obj| {
1089 try argv.append(obj.path);
1090 }
10301091
1031 if (self.base.options.link_libcpp) {
1032 try argv.append(comp.libcxxabi_static_lib.?.full_object_path);
1033 try argv.append(comp.libcxx_static_lib.?.full_object_path);
1034 }
1092 for (comp.c_object_table.keys()) |key| {
1093 try argv.append(key.status.success.object_path);
1094 }
10351095
1036 try argv.append("-o");
1037 try argv.append(full_out_path);
1096 if (module_obj_path) |p| {
1097 try argv.append(p);
1098 }
10381099
1039 try argv.append("-lSystem");
1040 try argv.append("-lc");
1100 if (comp.compiler_rt_lib) |lib| {
1101 try argv.append(lib.full_object_path);
1102 }
10411103
1042 for (self.base.options.system_libs.keys()) |l_name| {
1043 const info = self.base.options.system_libs.get(l_name).?;
1044 const arg = if (info.needed)
1045 try std.fmt.allocPrint(arena, "-needed-l{s}", .{l_name})
1046 else if (info.weak)
1047 try std.fmt.allocPrint(arena, "-weak-l{s}", .{l_name})
1048 else
1049 try std.fmt.allocPrint(arena, "-l{s}", .{l_name});
1050 try argv.append(arg);
1051 }
1104 if (self.base.options.link_libcpp) {
1105 try argv.append(comp.libcxxabi_static_lib.?.full_object_path);
1106 try argv.append(comp.libcxx_static_lib.?.full_object_path);
1107 }
10521108
1053 for (self.base.options.lib_dirs) |lib_dir| {
1054 try argv.append(try std.fmt.allocPrint(arena, "-L{s}", .{lib_dir}));
1055 }
1109 try argv.append("-o");
1110 try argv.append(full_out_path);
1111
1112 try argv.append("-lSystem");
1113 try argv.append("-lc");
1114
1115 for (self.base.options.system_libs.keys()) |l_name| {
1116 const info = self.base.options.system_libs.get(l_name).?;
1117 const arg = if (info.needed)
1118 try std.fmt.allocPrint(arena, "-needed-l{s}", .{l_name})
1119 else if (info.weak)
1120 try std.fmt.allocPrint(arena, "-weak-l{s}", .{l_name})
1121 else
1122 try std.fmt.allocPrint(arena, "-l{s}", .{l_name});
1123 try argv.append(arg);
1124 }
10561125
1057 for (self.base.options.frameworks.keys()) |framework| {
1058 const info = self.base.options.frameworks.get(framework).?;
1059 const arg = if (info.needed)
1060 try std.fmt.allocPrint(arena, "-needed_framework {s}", .{framework})
1061 else if (info.weak)
1062 try std.fmt.allocPrint(arena, "-weak_framework {s}", .{framework})
1063 else
1064 try std.fmt.allocPrint(arena, "-framework {s}", .{framework});
1065 try argv.append(arg);
1066 }
1126 for (self.base.options.lib_dirs) |lib_dir| {
1127 try argv.append(try std.fmt.allocPrint(arena, "-L{s}", .{lib_dir}));
1128 }
10671129
1068 for (self.base.options.framework_dirs) |framework_dir| {
1069 try argv.append(try std.fmt.allocPrint(arena, "-F{s}", .{framework_dir}));
1070 }
1130 for (self.base.options.frameworks.keys()) |framework| {
1131 const info = self.base.options.frameworks.get(framework).?;
1132 const arg = if (info.needed)
1133 try std.fmt.allocPrint(arena, "-needed_framework {s}", .{framework})
1134 else if (info.weak)
1135 try std.fmt.allocPrint(arena, "-weak_framework {s}", .{framework})
1136 else
1137 try std.fmt.allocPrint(arena, "-framework {s}", .{framework});
1138 try argv.append(arg);
1139 }
10711140
1072 if (allow_undef) {
1073 try argv.append("-undefined");
1074 try argv.append("dynamic_lookup");
1075 }
1141 for (self.base.options.framework_dirs) |framework_dir| {
1142 try argv.append(try std.fmt.allocPrint(arena, "-F{s}", .{framework_dir}));
1143 }
10761144
1077 for (must_link_archives.keys()) |lib| {
1078 try argv.append(try std.fmt.allocPrint(arena, "-force_load {s}", .{lib}));
1079 }
1145 if (is_dyn_lib and (self.base.options.allow_shlib_undefined orelse false)) {
1146 try argv.append("-undefined");
1147 try argv.append("dynamic_lookup");
1148 }
10801149
1081 Compilation.dump_argv(argv.items);
1150 for (must_link_archives.keys()) |lib| {
1151 try argv.append(try std.fmt.allocPrint(arena, "-force_load {s}", .{lib}));
10821152 }
10831153
1084 var dependent_libs = std.fifo.LinearFifo(struct {
1085 id: Dylib.Id,
1086 parent: u16,
1087 }, .Dynamic).init(self.base.allocator);
1088 defer dependent_libs.deinit();
1089 try self.parseInputFiles(positionals.items, self.base.options.sysroot, &dependent_libs);
1090 try self.parseAndForceLoadStaticArchives(must_link_archives.keys());
1091 try self.parseLibs(libs.keys(), libs.values(), self.base.options.sysroot, &dependent_libs);
1092 try self.parseDependentLibs(self.base.options.sysroot, &dependent_libs);
1154 Compilation.dump_argv(argv.items);
10931155 }
10941156
1095 try self.createMhExecuteHeaderSymbol();
1157 var dependent_libs = std.fifo.LinearFifo(struct {
1158 id: Dylib.Id,
1159 parent: u16,
1160 }, .Dynamic).init(gpa);
1161 defer dependent_libs.deinit();
1162 try self.parseInputFiles(positionals.items, self.base.options.sysroot, &dependent_libs);
1163 try self.parseAndForceLoadStaticArchives(must_link_archives.keys());
1164 try self.parseLibs(libs.keys(), libs.values(), self.base.options.sysroot, &dependent_libs);
1165 try self.parseDependentLibs(self.base.options.sysroot, &dependent_libs);
1166
10961167 for (self.objects.items) |*object, object_id| {
1097 if (object.analyzed) continue;
1098 try self.resolveSymbolsInObject(@intCast(u16, object_id));
1168 try self.resolveSymbolsInObject(object, @intCast(u16, object_id));
10991169 }
11001170
11011171 try self.resolveSymbolsInArchives();
......@@ -1103,46 +1173,11 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
11031173 try self.createDyldPrivateAtom();
11041174 try self.createStubHelperPreambleAtom();
11051175 try self.resolveSymbolsInDylibs();
1176 try self.createMhExecuteHeaderSymbol();
11061177 try self.createDsoHandleSymbol();
11071178 try self.addCodeSignatureLC();
1179 try self.resolveSymbolsAtLoading();
11081180
1109 {
1110 var next_sym: usize = 0;
1111 while (next_sym < self.unresolved.count()) {
1112 const sym = &self.undefs.items[self.unresolved.keys()[next_sym]];
1113 const sym_name = self.getString(sym.n_strx);
1114 const resolv = self.symbol_resolver.get(sym.n_strx) orelse unreachable;
1115
1116 if (sym.discarded()) {
1117 sym.* = .{
1118 .n_strx = 0,
1119 .n_type = macho.N_UNDF,
1120 .n_sect = 0,
1121 .n_desc = 0,
1122 .n_value = 0,
1123 };
1124 _ = self.unresolved.swapRemove(resolv.where_index);
1125 continue;
1126 } else if (allow_undef) {
1127 const n_desc = @bitCast(
1128 u16,
1129 macho.BIND_SPECIAL_DYLIB_FLAT_LOOKUP * @intCast(i16, macho.N_SYMBOL_RESOLVER),
1130 );
1131 // TODO allow_shlib_undefined is an ELF flag so figure out macOS specific flags too.
1132 sym.n_type = macho.N_EXT;
1133 sym.n_desc = n_desc;
1134 _ = self.unresolved.swapRemove(resolv.where_index);
1135 continue;
1136 }
1137
1138 log.err("undefined reference to symbol '{s}'", .{sym_name});
1139 if (resolv.file) |file| {
1140 log.err(" first referenced in '{s}'", .{self.objects.items[file].name});
1141 }
1142
1143 next_sym += 1;
1144 }
1145 }
11461181 if (self.unresolved.count() > 0) {
11471182 return error.UndefinedSymbolReference;
11481183 }
......@@ -1154,46 +1189,42 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
11541189 }
11551190
11561191 try self.createTentativeDefAtoms();
1157 try self.parseObjectsIntoAtoms();
11581192
1159 const use_llvm = build_options.have_llvm and self.base.options.use_llvm;
1160 if (use_llvm or use_stage1) {
1161 try self.pruneAndSortSections();
1162 try self.allocateSegments();
1163 try self.allocateLocals();
1193 for (self.objects.items) |*object, object_id| {
1194 try object.splitIntoAtomsOneShot(self, @intCast(u32, object_id));
11641195 }
11651196
1197 if (gc_sections) {
1198 try dead_strip.gcAtoms(self);
1199 }
1200
1201 try self.pruneAndSortSections();
1202 try self.allocateSegments();
1203 try self.allocateSymbols();
1204
11661205 try self.allocateSpecialSymbols();
1167 try self.allocateGlobals();
11681206
11691207 if (build_options.enable_logging) {
11701208 self.logSymtab();
11711209 self.logSectionOrdinals();
1210 self.logAtoms();
11721211 }
11731212
1174 if (use_llvm or use_stage1) {
1175 try self.writeAllAtoms();
1176 } else {
1177 try self.writeAtoms();
1178 }
1213 try self.writeAtomsOneShot();
11791214
11801215 if (self.rustc_section_index) |id| {
1181 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].segment;
1182 const sect = &seg.sections.items[id];
1216 const sect = self.getSectionPtr(.{
1217 .seg = self.data_segment_cmd_index.?,
1218 .sect = id,
1219 });
11831220 sect.size = self.rustc_section_size;
11841221 }
11851222
11861223 try self.setEntryPoint();
1187 try self.updateSectionOrdinals();
11881224 try self.writeLinkeditSegment();
11891225
1190 if (self.d_sym) |*d_sym| {
1191 // Flush debug symbols bundle.
1192 try d_sym.flushModule(self.base.allocator, self.base.options);
1193 }
1194
11951226 if (self.code_signature) |*csig| {
1196 csig.clear(self.base.allocator);
1227 csig.clear(gpa);
11971228 csig.code_directory.ident = self.base.options.emit.?.sub_path;
11981229 // Preallocate space for the code signature.
11991230 // We need to do this at this stage so that we have the load commands with proper values
......@@ -1206,32 +1237,17 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
12061237 try self.writeLoadCommands();
12071238 try self.writeHeader();
12081239
1209 if (self.entry_addr == null and self.base.options.output_mode == .Exe) {
1210 log.debug("flushing. no_entry_point_found = true", .{});
1211 self.error_flags.no_entry_point_found = true;
1212 } else {
1213 log.debug("flushing. no_entry_point_found = false", .{});
1214 self.error_flags.no_entry_point_found = false;
1215 }
1216
12171240 assert(!self.load_commands_dirty);
12181241
12191242 if (self.code_signature) |*csig| {
12201243 try self.writeCodeSignature(csig); // code signing always comes last
12211244 }
1222
1223 if (build_options.enable_link_snapshots) {
1224 if (self.base.options.enable_link_snapshots)
1225 try self.snapshotState();
1226 }
12271245 }
12281246
1229 cache: {
1230 if ((use_stage1 and self.base.options.disable_lld_caching) or self.base.options.cache_mode == .whole)
1231 break :cache;
1247 if (!self.base.options.disable_lld_caching) {
12321248 // Update the file with the digest. If it fails we can continue; it only
12331249 // means that the next invocation will have an unnecessary cache miss.
1234 Cache.writeSmallFile(cache_dir_handle, id_symlink_basename, &digest) catch |err| {
1250 Cache.writeSmallFile(directory.handle, id_symlink_basename, &digest) catch |err| {
12351251 log.debug("failed to save linking hash digest file: {s}", .{@errorName(err)});
12361252 };
12371253 // Again failure here only means an unnecessary cache miss.
......@@ -1242,8 +1258,49 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
12421258 // other processes clobbering it.
12431259 self.base.lock = man.toOwnedLock();
12441260 }
1261}
12451262
1246 self.cold_start = false;
1263fn resolveLibSystem(
1264 self: *MachO,
1265 arena: Allocator,
1266 comp: *Compilation,
1267 search_dirs: []const []const u8,
1268 out_libs: anytype,
1269) !void {
1270 // If we were given the sysroot, try to look there first for libSystem.B.{dylib, tbd}.
1271 var libsystem_available = false;
1272 if (self.base.options.sysroot != null) blk: {
1273 // Try stub file first. If we hit it, then we're done as the stub file
1274 // re-exports every single symbol definition.
1275 for (search_dirs) |dir| {
1276 if (try resolveLib(arena, dir, "System", ".tbd")) |full_path| {
1277 try out_libs.put(full_path, .{ .needed = true });
1278 libsystem_available = true;
1279 break :blk;
1280 }
1281 }
1282 // If we didn't hit the stub file, try .dylib next. However, libSystem.dylib
1283 // doesn't export libc.dylib which we'll need to resolve subsequently also.
1284 for (search_dirs) |dir| {
1285 if (try resolveLib(arena, dir, "System", ".dylib")) |libsystem_path| {
1286 if (try resolveLib(arena, dir, "c", ".dylib")) |libc_path| {
1287 try out_libs.put(libsystem_path, .{ .needed = true });
1288 try out_libs.put(libc_path, .{ .needed = true });
1289 libsystem_available = true;
1290 break :blk;
1291 }
1292 }
1293 }
1294 }
1295 if (!libsystem_available) {
1296 const libsystem_name = try std.fmt.allocPrint(arena, "libSystem.{d}.tbd", .{
1297 self.base.options.target.os.version_range.semver.min.major,
1298 });
1299 const full_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
1300 "libc", "darwin", libsystem_name,
1301 });
1302 try out_libs.put(full_path, .{ .needed = true });
1303 }
12471304}
12481305
12491306fn resolveSearchDir(
......@@ -1288,6 +1345,16 @@ fn resolveSearchDir(
12881345 return null;
12891346}
12901347
1348fn resolveSearchDirs(arena: Allocator, dirs: []const []const u8, syslibroot: ?[]const u8, out_dirs: anytype) !void {
1349 for (dirs) |dir| {
1350 if (try resolveSearchDir(arena, dir, syslibroot)) |search_dir| {
1351 try out_dirs.append(search_dir);
1352 } else {
1353 log.warn("directory not found for '-L{s}'", .{dir});
1354 }
1355 }
1356}
1357
12911358fn resolveLib(
12921359 arena: Allocator,
12931360 search_dir: []const u8,
......@@ -1337,9 +1404,15 @@ fn parseObject(self: *MachO, path: []const u8) !bool {
13371404 const name = try self.base.allocator.dupe(u8, path);
13381405 errdefer self.base.allocator.free(name);
13391406
1407 const mtime: u64 = mtime: {
1408 const stat = file.stat() catch break :mtime 0;
1409 break :mtime @intCast(u64, @divFloor(stat.mtime, 1_000_000_000));
1410 };
1411
13401412 var object = Object{
13411413 .name = name,
13421414 .file = file,
1415 .mtime = mtime,
13431416 };
13441417
13451418 object.parse(self.base.allocator, self.base.options.target) catch |err| switch (err) {
......@@ -1507,7 +1580,7 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const
15071580 .syslibroot = syslibroot,
15081581 })) continue;
15091582
1510 log.warn("unknown filetype for positional input file: '{s}'", .{file_name});
1583 log.debug("unknown filetype for positional input file: '{s}'", .{file_name});
15111584 }
15121585}
15131586
......@@ -1522,7 +1595,7 @@ fn parseAndForceLoadStaticArchives(self: *MachO, files: []const []const u8) !voi
15221595 log.debug("parsing and force loading static archive '{s}'", .{full_path});
15231596
15241597 if (try self.parseArchive(full_path, true)) continue;
1525 log.warn("unknown filetype: expected static archive: '{s}'", .{file_name});
1598 log.debug("unknown filetype: expected static archive: '{s}'", .{file_name});
15261599 }
15271600}
15281601
......@@ -1543,7 +1616,7 @@ fn parseLibs(
15431616 })) continue;
15441617 if (try self.parseArchive(lib, false)) continue;
15451618
1546 log.warn("unknown filetype for a library: '{s}'", .{lib});
1619 log.debug("unknown filetype for a library: '{s}'", .{lib});
15471620 }
15481621}
15491622
......@@ -1587,7 +1660,7 @@ fn parseDependentLibs(self: *MachO, syslibroot: ?[]const u8, dependent_libs: any
15871660 });
15881661 if (did_parse_successfully) break;
15891662 } else {
1590 log.warn("unable to resolve dependency {s}", .{dep_id.id.name});
1663 log.debug("unable to resolve dependency {s}", .{dep_id.id.name});
15911664 }
15921665 }
15931666}
......@@ -1595,6 +1668,15 @@ fn parseDependentLibs(self: *MachO, syslibroot: ?[]const u8, dependent_libs: any
15951668pub const MatchingSection = struct {
15961669 seg: u16,
15971670 sect: u16,
1671
1672 pub fn eql(this: MatchingSection, other: struct {
1673 seg: ?u16,
1674 sect: ?u16,
1675 }) bool {
1676 const seg = other.seg orelse return false;
1677 const sect = other.sect orelse return false;
1678 return this.seg == seg and this.sect == sect;
1679 }
15981680};
15991681
16001682pub fn getMatchingSection(self: *MachO, sect: macho.section_64) !?MatchingSection {
......@@ -2158,33 +2240,31 @@ pub fn getMatchingSection(self: *MachO, sect: macho.section_64) !?MatchingSectio
21582240 return res;
21592241}
21602242
2161pub fn createEmptyAtom(self: *MachO, local_sym_index: u32, size: u64, alignment: u32) !*Atom {
2243pub fn createEmptyAtom(gpa: Allocator, sym_index: u32, size: u64, alignment: u32) !*Atom {
21622244 const size_usize = math.cast(usize, size) orelse return error.Overflow;
2163 const atom = try self.base.allocator.create(Atom);
2164 errdefer self.base.allocator.destroy(atom);
2245 const atom = try gpa.create(Atom);
2246 errdefer gpa.destroy(atom);
21652247 atom.* = Atom.empty;
2166 atom.local_sym_index = local_sym_index;
2248 atom.sym_index = sym_index;
21672249 atom.size = size;
21682250 atom.alignment = alignment;
21692251
2170 try atom.code.resize(self.base.allocator, size_usize);
2252 try atom.code.resize(gpa, size_usize);
21712253 mem.set(u8, atom.code.items, 0);
21722254
2173 try self.managed_atoms.append(self.base.allocator, atom);
21742255 return atom;
21752256}
21762257
21772258pub fn writeAtom(self: *MachO, atom: *Atom, match: MatchingSection) !void {
2178 const seg = self.load_commands.items[match.seg].segment;
2179 const sect = seg.sections.items[match.sect];
2180 const sym = self.locals.items[atom.local_sym_index];
2259 const sect = self.getSection(match);
2260 const sym = atom.getSymbol(self);
21812261 const file_offset = sect.offset + sym.n_value - sect.addr;
21822262 try atom.resolveRelocs(self);
2183 log.debug("writing atom for symbol {s} at file offset 0x{x}", .{ self.getString(sym.n_strx), file_offset });
2263 log.debug("writing atom for symbol {s} at file offset 0x{x}", .{ atom.getName(self), file_offset });
21842264 try self.base.file.?.pwriteAll(atom.code.items, file_offset);
21852265}
21862266
2187fn allocateLocals(self: *MachO) !void {
2267fn allocateSymbols(self: *MachO) !void {
21882268 var it = self.atoms.iterator();
21892269 while (it.next()) |entry| {
21902270 const match = entry.key_ptr.*;
......@@ -2194,37 +2274,25 @@ fn allocateLocals(self: *MachO) !void {
21942274 atom = prev;
21952275 }
21962276
2197 const n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1);
2198 const seg = self.load_commands.items[match.seg].segment;
2199 const sect = seg.sections.items[match.sect];
2277 const n_sect = self.getSectionOrdinal(match);
2278 const sect = self.getSection(match);
22002279 var base_vaddr = sect.addr;
22012280
2202 log.debug("allocating local symbols in {s},{s}", .{ sect.segName(), sect.sectName() });
2281 log.debug("allocating local symbols in sect({d}, '{s},{s}')", .{ n_sect, sect.segName(), sect.sectName() });
22032282
22042283 while (true) {
22052284 const alignment = try math.powi(u32, 2, atom.alignment);
22062285 base_vaddr = mem.alignForwardGeneric(u64, base_vaddr, alignment);
22072286
2208 const sym = &self.locals.items[atom.local_sym_index];
2287 const sym = atom.getSymbolPtr(self);
22092288 sym.n_value = base_vaddr;
22102289 sym.n_sect = n_sect;
22112290
2212 log.debug(" {d}: {s} allocated at 0x{x}", .{
2213 atom.local_sym_index,
2214 self.getString(sym.n_strx),
2215 base_vaddr,
2216 });
2217
2218 // Update each alias (if any)
2219 for (atom.aliases.items) |index| {
2220 const alias_sym = &self.locals.items[index];
2221 alias_sym.n_value = base_vaddr;
2222 alias_sym.n_sect = n_sect;
2223 }
2291 log.debug(" ATOM(%{d}, '{s}') @{x}", .{ atom.sym_index, atom.getName(self), base_vaddr });
22242292
22252293 // Update each symbol contained within the atom
22262294 for (atom.contained.items) |sym_at_off| {
2227 const contained_sym = &self.locals.items[sym_at_off.local_sym_index];
2295 const contained_sym = self.getSymbolPtr(.{ .sym_index = sym_at_off.sym_index, .file = atom.file });
22282296 contained_sym.n_value = base_vaddr + sym_at_off.offset;
22292297 contained_sym.n_sect = n_sect;
22302298 }
......@@ -2242,16 +2310,11 @@ fn shiftLocalsByOffset(self: *MachO, match: MatchingSection, offset: i64) !void
22422310 var atom = self.atoms.get(match) orelse return;
22432311
22442312 while (true) {
2245 const atom_sym = &self.locals.items[atom.local_sym_index];
2313 const atom_sym = atom.getSymbolPtr(self);
22462314 atom_sym.n_value = @intCast(u64, @intCast(i64, atom_sym.n_value) + offset);
22472315
2248 for (atom.aliases.items) |index| {
2249 const alias_sym = &self.locals.items[index];
2250 alias_sym.n_value = @intCast(u64, @intCast(i64, alias_sym.n_value) + offset);
2251 }
2252
22532316 for (atom.contained.items) |sym_at_off| {
2254 const contained_sym = &self.locals.items[sym_at_off.local_sym_index];
2317 const contained_sym = self.getSymbolPtr(.{ .sym_index = sym_at_off.sym_index, .file = atom.file });
22552318 contained_sym.n_value = @intCast(u64, @intCast(i64, contained_sym.n_value) + offset);
22562319 }
22572320
......@@ -2262,53 +2325,33 @@ fn shiftLocalsByOffset(self: *MachO, match: MatchingSection, offset: i64) !void
22622325}
22632326
22642327fn allocateSpecialSymbols(self: *MachO) !void {
2265 for (&[_]?u32{
2266 self.mh_execute_header_sym_index,
2267 self.dso_handle_sym_index,
2268 }) |maybe_sym_index| {
2269 const sym_index = maybe_sym_index orelse continue;
2270 const sym = &self.locals.items[sym_index];
2328 for (&[_][]const u8{
2329 "___dso_handle",
2330 "__mh_execute_header",
2331 }) |name| {
2332 const global = self.globals.get(name) orelse continue;
2333 if (global.file != null) continue;
2334 const sym = self.getSymbolPtr(global);
22712335 const seg = self.load_commands.items[self.text_segment_cmd_index.?].segment;
2272 sym.n_sect = @intCast(u8, self.section_ordinals.getIndex(.{
2336 sym.n_sect = self.getSectionOrdinal(.{
22732337 .seg = self.text_segment_cmd_index.?,
22742338 .sect = 0,
2275 }).? + 1);
2339 });
22762340 sym.n_value = seg.inner.vmaddr;
22772341
22782342 log.debug("allocating {s} at the start of {s}", .{
2279 self.getString(sym.n_strx),
2343 name,
22802344 seg.inner.segName(),
22812345 });
22822346 }
22832347}
22842348
2285fn allocateGlobals(self: *MachO) !void {
2286 log.debug("allocating global symbols", .{});
2349fn writeAtomsOneShot(self: *MachO) !void {
2350 assert(self.mode == .one_shot);
22872351
2288 var sym_it = self.symbol_resolver.valueIterator();
2289 while (sym_it.next()) |resolv| {
2290 if (resolv.where != .global) continue;
2291
2292 assert(resolv.local_sym_index != 0);
2293 const local_sym = self.locals.items[resolv.local_sym_index];
2294 const sym = &self.globals.items[resolv.where_index];
2295 sym.n_value = local_sym.n_value;
2296 sym.n_sect = local_sym.n_sect;
2297
2298 log.debug(" {d}: {s} allocated at 0x{x}", .{
2299 resolv.where_index,
2300 self.getString(sym.n_strx),
2301 local_sym.n_value,
2302 });
2303 }
2304}
2305
2306fn writeAllAtoms(self: *MachO) !void {
23072352 var it = self.atoms.iterator();
23082353 while (it.next()) |entry| {
2309 const match = entry.key_ptr.*;
2310 const seg = self.load_commands.items[match.seg].segment;
2311 const sect = seg.sections.items[match.sect];
2354 const sect = self.getSection(entry.key_ptr.*);
23122355 var atom: *Atom = entry.value_ptr.*;
23132356
23142357 if (sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL) continue;
......@@ -2324,20 +2367,28 @@ fn writeAllAtoms(self: *MachO) !void {
23242367 }
23252368
23262369 while (true) {
2327 const atom_sym = self.locals.items[atom.local_sym_index];
2370 const this_sym = atom.getSymbol(self);
23282371 const padding_size: usize = if (atom.next) |next| blk: {
2329 const next_sym = self.locals.items[next.local_sym_index];
2330 const size = next_sym.n_value - (atom_sym.n_value + atom.size);
2372 const next_sym = next.getSymbol(self);
2373 const size = next_sym.n_value - (this_sym.n_value + atom.size);
23312374 break :blk math.cast(usize, size) orelse return error.Overflow;
23322375 } else 0;
23332376
2334 log.debug(" (adding atom {s} to buffer: {})", .{ self.getString(atom_sym.n_strx), atom_sym });
2377 log.debug(" (adding ATOM(%{d}, '{s}') from object({d}) to buffer)", .{
2378 atom.sym_index,
2379 atom.getName(self),
2380 atom.file,
2381 });
2382 if (padding_size > 0) {
2383 log.debug(" (with padding {x})", .{padding_size});
2384 }
23352385
23362386 try atom.resolveRelocs(self);
23372387 buffer.appendSliceAssumeCapacity(atom.code.items);
23382388
23392389 var i: usize = 0;
23402390 while (i < padding_size) : (i += 1) {
2391 // TODO with NOPs
23412392 buffer.appendAssumeCapacity(0);
23422393 }
23432394
......@@ -2381,12 +2432,13 @@ fn writePadding(self: *MachO, match: MatchingSection, size: usize, writer: anyty
23812432 }
23822433}
23832434
2384fn writeAtoms(self: *MachO) !void {
2435fn writeAtomsIncremental(self: *MachO) !void {
2436 assert(self.mode == .incremental);
2437
23852438 var it = self.atoms.iterator();
23862439 while (it.next()) |entry| {
23872440 const match = entry.key_ptr.*;
2388 const seg = self.load_commands.items[match.seg].segment;
2389 const sect = seg.sections.items[match.sect];
2441 const sect = self.getSection(match);
23902442 var atom: *Atom = entry.value_ptr.*;
23912443
23922444 // TODO handle zerofill in stage2
......@@ -2395,7 +2447,7 @@ fn writeAtoms(self: *MachO) !void {
23952447 log.debug("writing atoms in {s},{s}", .{ sect.segName(), sect.sectName() });
23962448
23972449 while (true) {
2398 if (atom.dirty or self.invalidate_relocs) {
2450 if (atom.dirty) {
23992451 try self.writeAtom(atom, match);
24002452 atom.dirty = false;
24012453 }
......@@ -2407,17 +2459,19 @@ fn writeAtoms(self: *MachO) !void {
24072459 }
24082460}
24092461
2410pub fn createGotAtom(self: *MachO, target: Atom.Relocation.Target) !*Atom {
2411 const local_sym_index = @intCast(u32, self.locals.items.len);
2412 try self.locals.append(self.base.allocator, .{
2462pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
2463 const gpa = self.base.allocator;
2464 const sym_index = @intCast(u32, self.locals.items.len);
2465 try self.locals.append(gpa, .{
24132466 .n_strx = 0,
24142467 .n_type = macho.N_SECT,
24152468 .n_sect = 0,
24162469 .n_desc = 0,
24172470 .n_value = 0,
24182471 });
2419 const atom = try self.createEmptyAtom(local_sym_index, @sizeOf(u64), 3);
2420 try atom.relocs.append(self.base.allocator, .{
2472
2473 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
2474 try atom.relocs.append(gpa, .{
24212475 .offset = 0,
24222476 .target = target,
24232477 .addend = 0,
......@@ -2430,35 +2484,60 @@ pub fn createGotAtom(self: *MachO, target: Atom.Relocation.Target) !*Atom {
24302484 else => unreachable,
24312485 },
24322486 });
2433 switch (target) {
2434 .local => {
2435 try atom.rebases.append(self.base.allocator, 0);
2436 },
2437 .global => |n_strx| {
2438 try atom.bindings.append(self.base.allocator, .{
2439 .n_strx = n_strx,
2440 .offset = 0,
2441 });
2442 },
2487
2488 const target_sym = self.getSymbol(target);
2489 if (target_sym.undf()) {
2490 const global = self.globals.get(self.getSymbolName(target)).?;
2491 try atom.bindings.append(gpa, .{
2492 .target = global,
2493 .offset = 0,
2494 });
2495 } else {
2496 try atom.rebases.append(gpa, 0);
24432497 }
2498
2499 try self.managed_atoms.append(gpa, atom);
2500 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
2501
2502 try self.allocateAtomCommon(atom, .{
2503 .seg = self.data_const_segment_cmd_index.?,
2504 .sect = self.got_section_index.?,
2505 });
2506
24442507 return atom;
24452508}
24462509
2447pub fn createTlvPtrAtom(self: *MachO, target: Atom.Relocation.Target) !*Atom {
2448 const local_sym_index = @intCast(u32, self.locals.items.len);
2449 try self.locals.append(self.base.allocator, .{
2510pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
2511 const gpa = self.base.allocator;
2512 const sym_index = @intCast(u32, self.locals.items.len);
2513 try self.locals.append(gpa, .{
24502514 .n_strx = 0,
24512515 .n_type = macho.N_SECT,
24522516 .n_sect = 0,
24532517 .n_desc = 0,
24542518 .n_value = 0,
24552519 });
2456 const atom = try self.createEmptyAtom(local_sym_index, @sizeOf(u64), 3);
2457 assert(target == .global);
2458 try atom.bindings.append(self.base.allocator, .{
2459 .n_strx = target.global,
2520
2521 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
2522 const target_sym = self.getSymbol(target);
2523 assert(target_sym.undf());
2524
2525 const global = self.globals.get(self.getSymbolName(target)).?;
2526 try atom.bindings.append(gpa, .{
2527 .target = global,
24602528 .offset = 0,
24612529 });
2530
2531 try self.managed_atoms.append(gpa, atom);
2532 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
2533
2534 const match = (try self.getMatchingSection(.{
2535 .segname = makeStaticString("__DATA"),
2536 .sectname = makeStaticString("__thread_ptrs"),
2537 .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS,
2538 })).?;
2539 try self.allocateAtomCommon(atom, match);
2540
24622541 return atom;
24632542}
24642543
......@@ -2466,34 +2545,32 @@ fn createDyldPrivateAtom(self: *MachO) !void {
24662545 if (self.dyld_stub_binder_index == null) return;
24672546 if (self.dyld_private_atom != null) return;
24682547
2469 const local_sym_index = @intCast(u32, self.locals.items.len);
2470 const sym = try self.locals.addOne(self.base.allocator);
2471 sym.* = .{
2548 const gpa = self.base.allocator;
2549 const sym_index = @intCast(u32, self.locals.items.len);
2550 try self.locals.append(gpa, .{
24722551 .n_strx = 0,
24732552 .n_type = macho.N_SECT,
24742553 .n_sect = 0,
24752554 .n_desc = 0,
24762555 .n_value = 0,
2477 };
2478 const atom = try self.createEmptyAtom(local_sym_index, @sizeOf(u64), 3);
2556 });
2557 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
24792558 self.dyld_private_atom = atom;
2480 const match = MatchingSection{
2559
2560 try self.allocateAtomCommon(atom, .{
24812561 .seg = self.data_segment_cmd_index.?,
24822562 .sect = self.data_section_index.?,
2483 };
2484 if (self.needs_prealloc) {
2485 const vaddr = try self.allocateAtom(atom, @sizeOf(u64), 8, match);
2486 log.debug("allocated {s} atom at 0x{x}", .{ self.getString(sym.n_strx), vaddr });
2487 sym.n_value = vaddr;
2488 } else try self.addAtomToSection(atom, match);
2563 });
24892564
2490 sym.n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1);
2565 try self.managed_atoms.append(gpa, atom);
2566 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
24912567}
24922568
24932569fn createStubHelperPreambleAtom(self: *MachO) !void {
24942570 if (self.dyld_stub_binder_index == null) return;
24952571 if (self.stub_helper_preamble_atom != null) return;
24962572
2573 const gpa = self.base.allocator;
24972574 const arch = self.base.options.target.cpu.arch;
24982575 const size: u64 = switch (arch) {
24992576 .x86_64 => 15,
......@@ -2505,17 +2582,16 @@ fn createStubHelperPreambleAtom(self: *MachO) !void {
25052582 .aarch64 => 2,
25062583 else => unreachable,
25072584 };
2508 const local_sym_index = @intCast(u32, self.locals.items.len);
2509 const sym = try self.locals.addOne(self.base.allocator);
2510 sym.* = .{
2585 const sym_index = @intCast(u32, self.locals.items.len);
2586 try self.locals.append(gpa, .{
25112587 .n_strx = 0,
25122588 .n_type = macho.N_SECT,
25132589 .n_sect = 0,
25142590 .n_desc = 0,
25152591 .n_value = 0,
2516 };
2517 const atom = try self.createEmptyAtom(local_sym_index, size, alignment);
2518 const dyld_private_sym_index = self.dyld_private_atom.?.local_sym_index;
2592 });
2593 const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment);
2594 const dyld_private_sym_index = self.dyld_private_atom.?.sym_index;
25192595 switch (arch) {
25202596 .x86_64 => {
25212597 try atom.relocs.ensureUnusedCapacity(self.base.allocator, 2);
......@@ -2525,7 +2601,7 @@ fn createStubHelperPreambleAtom(self: *MachO) !void {
25252601 atom.code.items[2] = 0x1d;
25262602 atom.relocs.appendAssumeCapacity(.{
25272603 .offset = 3,
2528 .target = .{ .local = dyld_private_sym_index },
2604 .target = .{ .sym_index = dyld_private_sym_index, .file = null },
25292605 .addend = 0,
25302606 .subtractor = null,
25312607 .pcrel = true,
......@@ -2540,7 +2616,7 @@ fn createStubHelperPreambleAtom(self: *MachO) !void {
25402616 atom.code.items[10] = 0x25;
25412617 atom.relocs.appendAssumeCapacity(.{
25422618 .offset = 11,
2543 .target = .{ .global = self.undefs.items[self.dyld_stub_binder_index.?].n_strx },
2619 .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null },
25442620 .addend = 0,
25452621 .subtractor = null,
25462622 .pcrel = true,
......@@ -2554,7 +2630,7 @@ fn createStubHelperPreambleAtom(self: *MachO) !void {
25542630 mem.writeIntLittle(u32, atom.code.items[0..][0..4], aarch64.Instruction.adrp(.x17, 0).toU32());
25552631 atom.relocs.appendAssumeCapacity(.{
25562632 .offset = 0,
2557 .target = .{ .local = dyld_private_sym_index },
2633 .target = .{ .sym_index = dyld_private_sym_index, .file = null },
25582634 .addend = 0,
25592635 .subtractor = null,
25602636 .pcrel = true,
......@@ -2565,7 +2641,7 @@ fn createStubHelperPreambleAtom(self: *MachO) !void {
25652641 mem.writeIntLittle(u32, atom.code.items[4..][0..4], aarch64.Instruction.add(.x17, .x17, 0, false).toU32());
25662642 atom.relocs.appendAssumeCapacity(.{
25672643 .offset = 4,
2568 .target = .{ .local = dyld_private_sym_index },
2644 .target = .{ .sym_index = dyld_private_sym_index, .file = null },
25692645 .addend = 0,
25702646 .subtractor = null,
25712647 .pcrel = false,
......@@ -2583,7 +2659,7 @@ fn createStubHelperPreambleAtom(self: *MachO) !void {
25832659 mem.writeIntLittle(u32, atom.code.items[12..][0..4], aarch64.Instruction.adrp(.x16, 0).toU32());
25842660 atom.relocs.appendAssumeCapacity(.{
25852661 .offset = 12,
2586 .target = .{ .global = self.undefs.items[self.dyld_stub_binder_index.?].n_strx },
2662 .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null },
25872663 .addend = 0,
25882664 .subtractor = null,
25892665 .pcrel = true,
......@@ -2598,7 +2674,7 @@ fn createStubHelperPreambleAtom(self: *MachO) !void {
25982674 ).toU32());
25992675 atom.relocs.appendAssumeCapacity(.{
26002676 .offset = 16,
2601 .target = .{ .global = self.undefs.items[self.dyld_stub_binder_index.?].n_strx },
2677 .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null },
26022678 .addend = 0,
26032679 .subtractor = null,
26042680 .pcrel = false,
......@@ -2611,22 +2687,18 @@ fn createStubHelperPreambleAtom(self: *MachO) !void {
26112687 else => unreachable,
26122688 }
26132689 self.stub_helper_preamble_atom = atom;
2614 const match = MatchingSection{
2690
2691 try self.allocateAtomCommon(atom, .{
26152692 .seg = self.text_segment_cmd_index.?,
26162693 .sect = self.stub_helper_section_index.?,
2617 };
2618
2619 if (self.needs_prealloc) {
2620 const alignment_pow_2 = try math.powi(u32, 2, atom.alignment);
2621 const vaddr = try self.allocateAtom(atom, atom.size, alignment_pow_2, match);
2622 log.debug("allocated {s} atom at 0x{x}", .{ self.getString(sym.n_strx), vaddr });
2623 sym.n_value = vaddr;
2624 } else try self.addAtomToSection(atom, match);
2694 });
26252695
2626 sym.n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1);
2696 try self.managed_atoms.append(gpa, atom);
2697 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
26272698}
26282699
26292700pub fn createStubHelperAtom(self: *MachO) !*Atom {
2701 const gpa = self.base.allocator;
26302702 const arch = self.base.options.target.cpu.arch;
26312703 const stub_size: u4 = switch (arch) {
26322704 .x86_64 => 10,
......@@ -2638,16 +2710,16 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
26382710 .aarch64 => 2,
26392711 else => unreachable,
26402712 };
2641 const local_sym_index = @intCast(u32, self.locals.items.len);
2642 try self.locals.append(self.base.allocator, .{
2713 const sym_index = @intCast(u32, self.locals.items.len);
2714 try self.locals.append(gpa, .{
26432715 .n_strx = 0,
26442716 .n_type = macho.N_SECT,
26452717 .n_sect = 0,
26462718 .n_desc = 0,
26472719 .n_value = 0,
26482720 });
2649 const atom = try self.createEmptyAtom(local_sym_index, stub_size, alignment);
2650 try atom.relocs.ensureTotalCapacity(self.base.allocator, 1);
2721 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);
2722 try atom.relocs.ensureTotalCapacity(gpa, 1);
26512723
26522724 switch (arch) {
26532725 .x86_64 => {
......@@ -2658,7 +2730,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
26582730 atom.code.items[5] = 0xe9;
26592731 atom.relocs.appendAssumeCapacity(.{
26602732 .offset = 6,
2661 .target = .{ .local = self.stub_helper_preamble_atom.?.local_sym_index },
2733 .target = .{ .sym_index = self.stub_helper_preamble_atom.?.sym_index, .file = null },
26622734 .addend = 0,
26632735 .subtractor = null,
26642736 .pcrel = true,
......@@ -2680,7 +2752,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
26802752 mem.writeIntLittle(u32, atom.code.items[4..8], aarch64.Instruction.b(0).toU32());
26812753 atom.relocs.appendAssumeCapacity(.{
26822754 .offset = 4,
2683 .target = .{ .local = self.stub_helper_preamble_atom.?.local_sym_index },
2755 .target = .{ .sym_index = self.stub_helper_preamble_atom.?.sym_index, .file = null },
26842756 .addend = 0,
26852757 .subtractor = null,
26862758 .pcrel = true,
......@@ -2692,22 +2764,31 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
26922764 else => unreachable,
26932765 }
26942766
2767 try self.managed_atoms.append(gpa, atom);
2768 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
2769
2770 try self.allocateAtomCommon(atom, .{
2771 .seg = self.text_segment_cmd_index.?,
2772 .sect = self.stub_helper_section_index.?,
2773 });
2774
26952775 return atom;
26962776}
26972777
2698pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, n_strx: u32) !*Atom {
2699 const local_sym_index = @intCast(u32, self.locals.items.len);
2700 try self.locals.append(self.base.allocator, .{
2778pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWithLoc) !*Atom {
2779 const gpa = self.base.allocator;
2780 const sym_index = @intCast(u32, self.locals.items.len);
2781 try self.locals.append(gpa, .{
27012782 .n_strx = 0,
27022783 .n_type = macho.N_SECT,
27032784 .n_sect = 0,
27042785 .n_desc = 0,
27052786 .n_value = 0,
27062787 });
2707 const atom = try self.createEmptyAtom(local_sym_index, @sizeOf(u64), 3);
2708 try atom.relocs.append(self.base.allocator, .{
2788 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
2789 try atom.relocs.append(gpa, .{
27092790 .offset = 0,
2710 .target = .{ .local = stub_sym_index },
2791 .target = .{ .sym_index = stub_sym_index, .file = null },
27112792 .addend = 0,
27122793 .subtractor = null,
27132794 .pcrel = false,
......@@ -2718,15 +2799,27 @@ pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, n_strx: u32) !*A
27182799 else => unreachable,
27192800 },
27202801 });
2721 try atom.rebases.append(self.base.allocator, 0);
2722 try atom.lazy_bindings.append(self.base.allocator, .{
2723 .n_strx = n_strx,
2802 try atom.rebases.append(gpa, 0);
2803
2804 const global = self.globals.get(self.getSymbolName(target)).?;
2805 try atom.lazy_bindings.append(gpa, .{
2806 .target = global,
27242807 .offset = 0,
27252808 });
2809
2810 try self.managed_atoms.append(gpa, atom);
2811 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
2812
2813 try self.allocateAtomCommon(atom, .{
2814 .seg = self.data_segment_cmd_index.?,
2815 .sect = self.la_symbol_ptr_section_index.?,
2816 });
2817
27262818 return atom;
27272819}
27282820
27292821pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
2822 const gpa = self.base.allocator;
27302823 const arch = self.base.options.target.cpu.arch;
27312824 const alignment: u2 = switch (arch) {
27322825 .x86_64 => 0,
......@@ -2738,23 +2831,23 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
27382831 .aarch64 => 3 * @sizeOf(u32),
27392832 else => unreachable, // unhandled architecture type
27402833 };
2741 const local_sym_index = @intCast(u32, self.locals.items.len);
2742 try self.locals.append(self.base.allocator, .{
2834 const sym_index = @intCast(u32, self.locals.items.len);
2835 try self.locals.append(gpa, .{
27432836 .n_strx = 0,
27442837 .n_type = macho.N_SECT,
27452838 .n_sect = 0,
27462839 .n_desc = 0,
27472840 .n_value = 0,
27482841 });
2749 const atom = try self.createEmptyAtom(local_sym_index, stub_size, alignment);
2842 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);
27502843 switch (arch) {
27512844 .x86_64 => {
27522845 // jmp
27532846 atom.code.items[0] = 0xff;
27542847 atom.code.items[1] = 0x25;
2755 try atom.relocs.append(self.base.allocator, .{
2848 try atom.relocs.append(gpa, .{
27562849 .offset = 2,
2757 .target = .{ .local = laptr_sym_index },
2850 .target = .{ .sym_index = laptr_sym_index, .file = null },
27582851 .addend = 0,
27592852 .subtractor = null,
27602853 .pcrel = true,
......@@ -2763,12 +2856,12 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
27632856 });
27642857 },
27652858 .aarch64 => {
2766 try atom.relocs.ensureTotalCapacity(self.base.allocator, 2);
2859 try atom.relocs.ensureTotalCapacity(gpa, 2);
27672860 // adrp x16, pages
27682861 mem.writeIntLittle(u32, atom.code.items[0..4], aarch64.Instruction.adrp(.x16, 0).toU32());
27692862 atom.relocs.appendAssumeCapacity(.{
27702863 .offset = 0,
2771 .target = .{ .local = laptr_sym_index },
2864 .target = .{ .sym_index = laptr_sym_index, .file = null },
27722865 .addend = 0,
27732866 .subtractor = null,
27742867 .pcrel = true,
......@@ -2783,7 +2876,7 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
27832876 ).toU32());
27842877 atom.relocs.appendAssumeCapacity(.{
27852878 .offset = 4,
2786 .target = .{ .local = laptr_sym_index },
2879 .target = .{ .sym_index = laptr_sym_index, .file = null },
27872880 .addend = 0,
27882881 .subtractor = null,
27892882 .pcrel = false,
......@@ -2795,101 +2888,179 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
27952888 },
27962889 else => unreachable,
27972890 }
2891
2892 try self.managed_atoms.append(gpa, atom);
2893 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
2894
2895 try self.allocateAtomCommon(atom, .{
2896 .seg = self.text_segment_cmd_index.?,
2897 .sect = self.stubs_section_index.?,
2898 });
2899
27982900 return atom;
27992901}
28002902
28012903fn createTentativeDefAtoms(self: *MachO) !void {
2802 if (self.tentatives.count() == 0) return;
2803 // Convert any tentative definition into a regular symbol and allocate
2804 // text blocks for each tentative definition.
2805 while (self.tentatives.popOrNull()) |entry| {
2904 const gpa = self.base.allocator;
2905
2906 for (self.globals.values()) |global| {
2907 const sym = self.getSymbolPtr(global);
2908 if (!sym.tentative()) continue;
2909
2910 log.debug("creating tentative definition for ATOM(%{d}, '{s}') in object({d})", .{
2911 global.sym_index, self.getSymbolName(global), global.file,
2912 });
2913
2914 // Convert any tentative definition into a regular symbol and allocate
2915 // text blocks for each tentative definition.
28062916 const match = MatchingSection{
28072917 .seg = self.data_segment_cmd_index.?,
28082918 .sect = self.bss_section_index.?,
28092919 };
2810 _ = try self.section_ordinals.getOrPut(self.base.allocator, match);
2920 _ = try self.section_ordinals.getOrPut(gpa, match);
28112921
2812 const global_sym = &self.globals.items[entry.key];
2813 const size = global_sym.n_value;
2814 const alignment = (global_sym.n_desc >> 8) & 0x0f;
2922 const size = sym.n_value;
2923 const alignment = (sym.n_desc >> 8) & 0x0f;
28152924
2816 global_sym.n_value = 0;
2817 global_sym.n_desc = 0;
2818 global_sym.n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1);
2819
2820 const local_sym_index = @intCast(u32, self.locals.items.len);
2821 const local_sym = try self.locals.addOne(self.base.allocator);
2822 local_sym.* = .{
2823 .n_strx = global_sym.n_strx,
2824 .n_type = macho.N_SECT,
2825 .n_sect = global_sym.n_sect,
2925 sym.* = .{
2926 .n_strx = sym.n_strx,
2927 .n_type = macho.N_SECT | macho.N_EXT,
2928 .n_sect = 0,
28262929 .n_desc = 0,
28272930 .n_value = 0,
28282931 };
28292932
2830 const resolv = self.symbol_resolver.getPtr(local_sym.n_strx) orelse unreachable;
2831 resolv.local_sym_index = local_sym_index;
2933 const atom = try MachO.createEmptyAtom(gpa, global.sym_index, size, alignment);
2934 atom.file = global.file;
28322935
2833 const atom = try self.createEmptyAtom(local_sym_index, size, alignment);
2936 try self.allocateAtomCommon(atom, match);
28342937
2835 if (self.needs_prealloc) {
2836 const alignment_pow_2 = try math.powi(u32, 2, alignment);
2837 const vaddr = try self.allocateAtom(atom, size, alignment_pow_2, match);
2838 local_sym.n_value = vaddr;
2839 global_sym.n_value = vaddr;
2840 } else try self.addAtomToSection(atom, match);
2938 if (global.file) |file| {
2939 const object = &self.objects.items[file];
2940 try object.managed_atoms.append(gpa, atom);
2941 try object.atom_by_index_table.putNoClobber(gpa, global.sym_index, atom);
2942 } else {
2943 try self.managed_atoms.append(gpa, atom);
2944 try self.atom_by_index_table.putNoClobber(gpa, global.sym_index, atom);
2945 }
28412946 }
28422947}
28432948
2844fn createDsoHandleSymbol(self: *MachO) !void {
2845 if (self.dso_handle_sym_index != null) return;
2846
2847 const n_strx = self.strtab_dir.getKeyAdapted(@as([]const u8, "___dso_handle"), StringIndexAdapter{
2848 .bytes = &self.strtab,
2849 }) orelse return;
2850
2851 const resolv = self.symbol_resolver.getPtr(n_strx) orelse return;
2852 if (resolv.where != .undef) return;
2949fn createMhExecuteHeaderSymbol(self: *MachO) !void {
2950 if (self.base.options.output_mode != .Exe) return;
2951 if (self.globals.get("__mh_execute_header")) |global| {
2952 const sym = self.getSymbol(global);
2953 if (!sym.undf() and !(sym.pext() or sym.weakDef())) return;
2954 }
28532955
2854 const undef = &self.undefs.items[resolv.where_index];
2855 const local_sym_index = @intCast(u32, self.locals.items.len);
2856 var nlist = macho.nlist_64{
2857 .n_strx = undef.n_strx,
2858 .n_type = macho.N_SECT,
2956 const gpa = self.base.allocator;
2957 const n_strx = try self.strtab.insert(gpa, "__mh_execute_header");
2958 const sym_index = @intCast(u32, self.locals.items.len);
2959 try self.locals.append(gpa, .{
2960 .n_strx = n_strx,
2961 .n_type = macho.N_SECT | macho.N_EXT,
28592962 .n_sect = 0,
2860 .n_desc = 0,
2963 .n_desc = macho.REFERENCED_DYNAMICALLY,
28612964 .n_value = 0,
2862 };
2863 try self.locals.append(self.base.allocator, nlist);
2864 const global_sym_index = @intCast(u32, self.globals.items.len);
2865 nlist.n_type |= macho.N_EXT;
2866 nlist.n_desc = macho.N_WEAK_DEF;
2867 try self.globals.append(self.base.allocator, nlist);
2868 self.dso_handle_sym_index = local_sym_index;
2965 });
28692966
2870 assert(self.unresolved.swapRemove(resolv.where_index));
2967 const name = try gpa.dupe(u8, "__mh_execute_header");
2968 const gop = try self.globals.getOrPut(gpa, name);
2969 defer if (gop.found_existing) gpa.free(name);
2970 gop.value_ptr.* = .{
2971 .sym_index = sym_index,
2972 .file = null,
2973 };
2974}
28712975
2872 undef.* = .{
2873 .n_strx = 0,
2874 .n_type = macho.N_UNDF,
2976fn createDsoHandleSymbol(self: *MachO) !void {
2977 const global = self.globals.getPtr("___dso_handle") orelse return;
2978 const sym = self.getSymbolPtr(global.*);
2979 if (!sym.undf()) return;
2980
2981 const gpa = self.base.allocator;
2982 const n_strx = try self.strtab.insert(gpa, "___dso_handle");
2983 const sym_index = @intCast(u32, self.locals.items.len);
2984 try self.locals.append(gpa, .{
2985 .n_strx = n_strx,
2986 .n_type = macho.N_SECT | macho.N_EXT,
28752987 .n_sect = 0,
2876 .n_desc = 0,
2988 .n_desc = macho.N_WEAK_DEF,
28772989 .n_value = 0,
2990 });
2991 global.* = .{
2992 .sym_index = sym_index,
2993 .file = null,
28782994 };
2879 resolv.* = .{
2880 .where = .global,
2881 .where_index = global_sym_index,
2882 .local_sym_index = local_sym_index,
2883 };
2995 _ = self.unresolved.swapRemove(@intCast(u32, self.globals.getIndex("___dso_handle").?));
28842996}
28852997
2886fn resolveSymbolsInObject(self: *MachO, object_id: u16) !void {
2887 const object = &self.objects.items[object_id];
2998fn resolveGlobalSymbol(self: *MachO, current: SymbolWithLoc) !void {
2999 const gpa = self.base.allocator;
3000 const sym = self.getSymbol(current);
3001 const sym_name = self.getSymbolName(current);
3002
3003 const name = try gpa.dupe(u8, sym_name);
3004 const global_index = @intCast(u32, self.globals.values().len);
3005 const gop = try self.globals.getOrPut(gpa, name);
3006 defer if (gop.found_existing) gpa.free(name);
3007
3008 if (!gop.found_existing) {
3009 gop.value_ptr.* = current;
3010 if (sym.undf() and !sym.tentative()) {
3011 try self.unresolved.putNoClobber(gpa, global_index, false);
3012 }
3013 return;
3014 }
3015
3016 const global = gop.value_ptr.*;
3017 const global_sym = self.getSymbol(global);
3018
3019 // Cases to consider: sym vs global_sym
3020 // 1. strong(sym) and strong(global_sym) => error
3021 // 2. strong(sym) and weak(global_sym) => sym
3022 // 3. strong(sym) and tentative(global_sym) => sym
3023 // 4. strong(sym) and undf(global_sym) => sym
3024 // 5. weak(sym) and strong(global_sym) => global_sym
3025 // 6. weak(sym) and tentative(global_sym) => sym
3026 // 7. weak(sym) and undf(global_sym) => sym
3027 // 8. tentative(sym) and strong(global_sym) => global_sym
3028 // 9. tentative(sym) and weak(global_sym) => global_sym
3029 // 10. tentative(sym) and tentative(global_sym) => pick larger
3030 // 11. tentative(sym) and undf(global_sym) => sym
3031 // 12. undf(sym) and * => global_sym
3032 //
3033 // Reduces to:
3034 // 1. strong(sym) and strong(global_sym) => error
3035 // 2. * and strong(global_sym) => global_sym
3036 // 3. weak(sym) and weak(global_sym) => global_sym
3037 // 4. tentative(sym) and tentative(global_sym) => pick larger
3038 // 5. undf(sym) and * => global_sym
3039 // 6. else => sym
3040
3041 const sym_is_strong = sym.sect() and !(sym.weakDef() or sym.pext());
3042 const global_is_strong = global_sym.sect() and !(global_sym.weakDef() or global_sym.pext());
3043 const sym_is_weak = sym.sect() and (sym.weakDef() or sym.pext());
3044 const global_is_weak = global_sym.sect() and (global_sym.weakDef() or global_sym.pext());
3045
3046 if (sym_is_strong and global_is_strong) return error.MultipleSymbolDefinitions;
3047 if (global_is_strong) return;
3048 if (sym_is_weak and global_is_weak) return;
3049 if (sym.tentative() and global_sym.tentative()) {
3050 if (global_sym.n_value >= sym.n_value) return;
3051 }
3052 if (sym.undf() and !sym.tentative()) return;
3053
3054 _ = self.unresolved.swapRemove(@intCast(u32, self.globals.getIndex(name).?));
3055
3056 gop.value_ptr.* = current;
3057}
28883058
3059fn resolveSymbolsInObject(self: *MachO, object: *Object, object_id: u16) !void {
28893060 log.debug("resolving symbols in '{s}'", .{object.name});
28903061
2891 for (object.symtab.items) |sym, id| {
2892 const sym_id = @intCast(u32, id);
3062 for (object.symtab.items) |sym, index| {
3063 const sym_index = @intCast(u32, index);
28933064 const sym_name = object.getString(sym.n_strx);
28943065
28953066 if (sym.stab()) {
......@@ -2913,170 +3084,27 @@ fn resolveSymbolsInObject(self: *MachO, object_id: u16) !void {
29133084 return error.UnhandledSymbolType;
29143085 }
29153086
2916 if (sym.sect()) {
2917 // Defined symbol regardless of scope lands in the locals symbol table.
2918 const local_sym_index = @intCast(u32, self.locals.items.len);
2919 try self.locals.append(self.base.allocator, .{
2920 .n_strx = if (symbolIsTemp(sym, sym_name)) 0 else try self.makeString(sym_name),
2921 .n_type = macho.N_SECT,
2922 .n_sect = 0,
2923 .n_desc = 0,
2924 .n_value = sym.n_value,
2925 });
2926 try object.symbol_mapping.putNoClobber(self.base.allocator, sym_id, local_sym_index);
2927 try object.reverse_symbol_mapping.putNoClobber(self.base.allocator, local_sym_index, sym_id);
2928
2929 // If the symbol's scope is not local aka translation unit, then we need work out
2930 // if we should save the symbol as a global, or potentially flag the error.
2931 if (!sym.ext()) continue;
2932
2933 const n_strx = try self.makeString(sym_name);
2934 const local = self.locals.items[local_sym_index];
2935 const resolv = self.symbol_resolver.getPtr(n_strx) orelse {
2936 const global_sym_index = @intCast(u32, self.globals.items.len);
2937 try self.globals.append(self.base.allocator, .{
2938 .n_strx = n_strx,
2939 .n_type = sym.n_type,
2940 .n_sect = 0,
2941 .n_desc = sym.n_desc,
2942 .n_value = sym.n_value,
2943 });
2944 try self.symbol_resolver.putNoClobber(self.base.allocator, n_strx, .{
2945 .where = .global,
2946 .where_index = global_sym_index,
2947 .local_sym_index = local_sym_index,
2948 .file = object_id,
2949 });
2950 continue;
2951 };
2952
2953 switch (resolv.where) {
2954 .global => {
2955 const global = &self.globals.items[resolv.where_index];
2956
2957 if (global.tentative()) {
2958 assert(self.tentatives.swapRemove(resolv.where_index));
2959 } else if (!(sym.weakDef() or sym.pext()) and !(global.weakDef() or global.pext())) {
2960 log.err("symbol '{s}' defined multiple times", .{sym_name});
2961 if (resolv.file) |file| {
2962 log.err(" first definition in '{s}'", .{self.objects.items[file].name});
2963 }
2964 log.err(" next definition in '{s}'", .{object.name});
2965 return error.MultipleSymbolDefinitions;
2966 } else if (sym.weakDef() or sym.pext()) continue; // Current symbol is weak, so skip it.
2967
2968 // Otherwise, update the resolver and the global symbol.
2969 global.n_type = sym.n_type;
2970 resolv.local_sym_index = local_sym_index;
2971 resolv.file = object_id;
2972
2973 continue;
2974 },
2975 .undef => {
2976 const undef = &self.undefs.items[resolv.where_index];
2977 undef.* = .{
2978 .n_strx = 0,
2979 .n_type = macho.N_UNDF,
2980 .n_sect = 0,
2981 .n_desc = 0,
2982 .n_value = 0,
2983 };
2984 assert(self.unresolved.swapRemove(resolv.where_index));
2985 },
2986 }
2987
2988 const global_sym_index = @intCast(u32, self.globals.items.len);
2989 try self.globals.append(self.base.allocator, .{
2990 .n_strx = local.n_strx,
2991 .n_type = sym.n_type,
2992 .n_sect = 0,
2993 .n_desc = sym.n_desc,
2994 .n_value = sym.n_value,
3087 if (sym.sect() and !sym.ext()) {
3088 log.debug("symbol '{s}' local to object {s}; skipping...", .{
3089 sym_name,
3090 object.name,
29953091 });
2996 resolv.* = .{
2997 .where = .global,
2998 .where_index = global_sym_index,
2999 .local_sym_index = local_sym_index,
3000 .file = object_id,
3001 };
3002 } else if (sym.tentative()) {
3003 // Symbol is a tentative definition.
3004 const n_strx = try self.makeString(sym_name);
3005 const resolv = self.symbol_resolver.getPtr(n_strx) orelse {
3006 const global_sym_index = @intCast(u32, self.globals.items.len);
3007 try self.globals.append(self.base.allocator, .{
3008 .n_strx = try self.makeString(sym_name),
3009 .n_type = sym.n_type,
3010 .n_sect = 0,
3011 .n_desc = sym.n_desc,
3012 .n_value = sym.n_value,
3013 });
3014 try self.symbol_resolver.putNoClobber(self.base.allocator, n_strx, .{
3015 .where = .global,
3016 .where_index = global_sym_index,
3017 .file = object_id,
3018 });
3019 _ = try self.tentatives.getOrPut(self.base.allocator, global_sym_index);
3020 continue;
3021 };
3022
3023 switch (resolv.where) {
3024 .global => {
3025 const global = &self.globals.items[resolv.where_index];
3026 if (!global.tentative()) continue;
3027 if (global.n_value >= sym.n_value) continue;
3028
3029 global.n_desc = sym.n_desc;
3030 global.n_value = sym.n_value;
3031 resolv.file = object_id;
3032 },
3033 .undef => {
3034 const undef = &self.undefs.items[resolv.where_index];
3035 const global_sym_index = @intCast(u32, self.globals.items.len);
3036 try self.globals.append(self.base.allocator, .{
3037 .n_strx = undef.n_strx,
3038 .n_type = sym.n_type,
3039 .n_sect = 0,
3040 .n_desc = sym.n_desc,
3041 .n_value = sym.n_value,
3042 });
3043 _ = try self.tentatives.getOrPut(self.base.allocator, global_sym_index);
3044 assert(self.unresolved.swapRemove(resolv.where_index));
3045
3046 resolv.* = .{
3047 .where = .global,
3048 .where_index = global_sym_index,
3049 .file = object_id,
3050 };
3051 undef.* = .{
3052 .n_strx = 0,
3053 .n_type = macho.N_UNDF,
3054 .n_sect = 0,
3055 .n_desc = 0,
3056 .n_value = 0,
3057 };
3058 },
3059 }
3060 } else {
3061 // Symbol is undefined.
3062 const n_strx = try self.makeString(sym_name);
3063 if (self.symbol_resolver.contains(n_strx)) continue;
3064
3065 const undef_sym_index = @intCast(u32, self.undefs.items.len);
3066 try self.undefs.append(self.base.allocator, .{
3067 .n_strx = try self.makeString(sym_name),
3068 .n_type = macho.N_UNDF,
3069 .n_sect = 0,
3070 .n_desc = sym.n_desc,
3071 .n_value = 0,
3072 });
3073 try self.symbol_resolver.putNoClobber(self.base.allocator, n_strx, .{
3074 .where = .undef,
3075 .where_index = undef_sym_index,
3076 .file = object_id,
3077 });
3078 try self.unresolved.putNoClobber(self.base.allocator, undef_sym_index, .none);
3092 continue;
30793093 }
3094
3095 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = object_id };
3096 self.resolveGlobalSymbol(sym_loc) catch |err| switch (err) {
3097 error.MultipleSymbolDefinitions => {
3098 const global = self.globals.get(sym_name).?;
3099 log.err("symbol '{s}' defined multiple times", .{sym_name});
3100 if (global.file) |file| {
3101 log.err(" first definition in '{s}'", .{self.objects.items[file].name});
3102 }
3103 log.err(" next definition in '{s}'", .{self.objects.items[object_id].name});
3104 return error.MultipleSymbolDefinitions;
3105 },
3106 else => |e| return e,
3107 };
30803108 }
30813109}
30823110
......@@ -3085,8 +3113,8 @@ fn resolveSymbolsInArchives(self: *MachO) !void {
30853113
30863114 var next_sym: usize = 0;
30873115 loop: while (next_sym < self.unresolved.count()) {
3088 const sym = self.undefs.items[self.unresolved.keys()[next_sym]];
3089 const sym_name = self.getString(sym.n_strx);
3116 const global = self.globals.values()[self.unresolved.keys()[next_sym]];
3117 const sym_name = self.getSymbolName(global);
30903118
30913119 for (self.archives.items) |archive| {
30923120 // Check if the entry exists in a static archive.
......@@ -3099,7 +3127,7 @@ fn resolveSymbolsInArchives(self: *MachO) !void {
30993127 const object_id = @intCast(u16, self.objects.items.len);
31003128 const object = try self.objects.addOne(self.base.allocator);
31013129 object.* = try archive.parseObject(self.base.allocator, self.base.options.target, offsets.items[0]);
3102 try self.resolveSymbolsInObject(object_id);
3130 try self.resolveSymbolsInObject(object, object_id);
31033131
31043132 continue :loop;
31053133 }
......@@ -3113,8 +3141,10 @@ fn resolveSymbolsInDylibs(self: *MachO) !void {
31133141
31143142 var next_sym: usize = 0;
31153143 loop: while (next_sym < self.unresolved.count()) {
3116 const sym = self.undefs.items[self.unresolved.keys()[next_sym]];
3117 const sym_name = self.getString(sym.n_strx);
3144 const global_index = self.unresolved.keys()[next_sym];
3145 const global = self.globals.values()[global_index];
3146 const sym = self.getSymbolPtr(global);
3147 const sym_name = self.getSymbolName(global);
31183148
31193149 for (self.dylibs.items) |dylib, id| {
31203150 if (!dylib.symbols.contains(sym_name)) continue;
......@@ -3126,68 +3156,23 @@ fn resolveSymbolsInDylibs(self: *MachO) !void {
31263156 }
31273157
31283158 const ordinal = self.referenced_dylibs.getIndex(dylib_id) orelse unreachable;
3129 const resolv = self.symbol_resolver.getPtr(sym.n_strx) orelse unreachable;
3130 const undef = &self.undefs.items[resolv.where_index];
3131 undef.n_type |= macho.N_EXT;
3132 undef.n_desc = @intCast(u16, ordinal + 1) * macho.N_SYMBOL_RESOLVER;
3159 sym.n_type |= macho.N_EXT;
3160 sym.n_desc = @intCast(u16, ordinal + 1) * macho.N_SYMBOL_RESOLVER;
31333161
31343162 if (dylib.weak) {
3135 undef.n_desc |= macho.N_WEAK_REF;
3136 }
3137
3138 if (self.unresolved.fetchSwapRemove(resolv.where_index)) |entry| outer_blk: {
3139 switch (entry.value) {
3140 .none => {},
3141 .got => return error.TODOGotHint,
3142 .stub => {
3143 if (self.stubs_table.contains(sym.n_strx)) break :outer_blk;
3144 const stub_helper_atom = blk: {
3145 const match = MatchingSection{
3146 .seg = self.text_segment_cmd_index.?,
3147 .sect = self.stub_helper_section_index.?,
3148 };
3149 const atom = try self.createStubHelperAtom();
3150 const atom_sym = &self.locals.items[atom.local_sym_index];
3151 const alignment = try math.powi(u32, 2, atom.alignment);
3152 const vaddr = try self.allocateAtom(atom, atom.size, alignment, match);
3153 atom_sym.n_value = vaddr;
3154 atom_sym.n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1);
3155 break :blk atom;
3156 };
3157 const laptr_atom = blk: {
3158 const match = MatchingSection{
3159 .seg = self.data_segment_cmd_index.?,
3160 .sect = self.la_symbol_ptr_section_index.?,
3161 };
3162 const atom = try self.createLazyPointerAtom(
3163 stub_helper_atom.local_sym_index,
3164 sym.n_strx,
3165 );
3166 const atom_sym = &self.locals.items[atom.local_sym_index];
3167 const alignment = try math.powi(u32, 2, atom.alignment);
3168 const vaddr = try self.allocateAtom(atom, atom.size, alignment, match);
3169 atom_sym.n_value = vaddr;
3170 atom_sym.n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1);
3171 break :blk atom;
3172 };
3173 const stub_atom = blk: {
3174 const match = MatchingSection{
3175 .seg = self.text_segment_cmd_index.?,
3176 .sect = self.stubs_section_index.?,
3177 };
3178 const atom = try self.createStubAtom(laptr_atom.local_sym_index);
3179 const atom_sym = &self.locals.items[atom.local_sym_index];
3180 const alignment = try math.powi(u32, 2, atom.alignment);
3181 const vaddr = try self.allocateAtom(atom, atom.size, alignment, match);
3182 atom_sym.n_value = vaddr;
3183 atom_sym.n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1);
3184 break :blk atom;
3185 };
3186 const stub_index = @intCast(u32, self.stubs.items.len);
3187 try self.stubs.append(self.base.allocator, stub_atom);
3188 try self.stubs_table.putNoClobber(self.base.allocator, sym.n_strx, stub_index);
3189 },
3190 }
3163 sym.n_desc |= macho.N_WEAK_REF;
3164 }
3165
3166 if (self.unresolved.fetchSwapRemove(global_index)) |entry| blk: {
3167 if (!entry.value) break :blk;
3168 if (!sym.undf()) break :blk;
3169 if (self.stubs_table.contains(global)) break :blk;
3170
3171 const stub_index = try self.allocateStubEntry(global);
3172 const stub_helper_atom = try self.createStubHelperAtom();
3173 const laptr_atom = try self.createLazyPointerAtom(stub_helper_atom.sym_index, global);
3174 const stub_atom = try self.createStubAtom(laptr_atom.sym_index);
3175 self.stubs.items[stub_index].sym_index = stub_atom.sym_index;
31913176 }
31923177
31933178 continue :loop;
......@@ -3197,39 +3182,46 @@ fn resolveSymbolsInDylibs(self: *MachO) !void {
31973182 }
31983183}
31993184
3200fn createMhExecuteHeaderSymbol(self: *MachO) !void {
3201 if (self.base.options.output_mode != .Exe) return;
3202 if (self.mh_execute_header_sym_index != null) return;
3185fn resolveSymbolsAtLoading(self: *MachO) !void {
3186 const is_lib = self.base.options.output_mode == .Lib;
3187 const is_dyn_lib = self.base.options.link_mode == .Dynamic and is_lib;
3188 const allow_undef = is_dyn_lib and (self.base.options.allow_shlib_undefined orelse false);
32033189
3204 const n_strx = try self.makeString("__mh_execute_header");
3205 const local_sym_index = @intCast(u32, self.locals.items.len);
3206 var nlist = macho.nlist_64{
3207 .n_strx = n_strx,
3208 .n_type = macho.N_SECT,
3209 .n_sect = 0,
3210 .n_desc = 0,
3211 .n_value = 0,
3212 };
3213 try self.locals.append(self.base.allocator, nlist);
3214 self.mh_execute_header_sym_index = local_sym_index;
3215
3216 if (self.symbol_resolver.getPtr(n_strx)) |resolv| {
3217 const global = &self.globals.items[resolv.where_index];
3218 if (!(global.weakDef() or !global.pext())) {
3219 log.err("symbol '__mh_execute_header' defined multiple times", .{});
3220 return error.MultipleSymbolDefinitions;
3190 var next_sym: usize = 0;
3191 while (next_sym < self.unresolved.count()) {
3192 const global_index = self.unresolved.keys()[next_sym];
3193 const global = self.globals.values()[global_index];
3194 const sym = self.getSymbolPtr(global);
3195 const sym_name = self.getSymbolName(global);
3196
3197 if (sym.discarded()) {
3198 sym.* = .{
3199 .n_strx = 0,
3200 .n_type = macho.N_UNDF,
3201 .n_sect = 0,
3202 .n_desc = 0,
3203 .n_value = 0,
3204 };
3205 _ = self.unresolved.swapRemove(global_index);
3206 continue;
3207 } else if (allow_undef) {
3208 const n_desc = @bitCast(
3209 u16,
3210 macho.BIND_SPECIAL_DYLIB_FLAT_LOOKUP * @intCast(i16, macho.N_SYMBOL_RESOLVER),
3211 );
3212 // TODO allow_shlib_undefined is an ELF flag so figure out macOS specific flags too.
3213 sym.n_type = macho.N_EXT;
3214 sym.n_desc = n_desc;
3215 _ = self.unresolved.swapRemove(global_index);
3216 continue;
32213217 }
3222 resolv.local_sym_index = local_sym_index;
3223 } else {
3224 const global_sym_index = @intCast(u32, self.globals.items.len);
3225 nlist.n_type |= macho.N_EXT;
3226 try self.globals.append(self.base.allocator, nlist);
3227 try self.symbol_resolver.putNoClobber(self.base.allocator, n_strx, .{
3228 .where = .global,
3229 .where_index = global_sym_index,
3230 .local_sym_index = local_sym_index,
3231 .file = null,
3232 });
3218
3219 log.err("undefined reference to symbol '{s}'", .{sym_name});
3220 if (global.file) |file| {
3221 log.err(" first referenced in '{s}'", .{self.objects.items[file].name});
3222 }
3223
3224 next_sym += 1;
32333225 }
32343226}
32353227
......@@ -3237,21 +3229,20 @@ fn resolveDyldStubBinder(self: *MachO) !void {
32373229 if (self.dyld_stub_binder_index != null) return;
32383230 if (self.unresolved.count() == 0) return; // no need for a stub binder if we don't have any imports
32393231
3240 const n_strx = try self.makeString("dyld_stub_binder");
3241 const sym_index = @intCast(u32, self.undefs.items.len);
3242 try self.undefs.append(self.base.allocator, .{
3232 const gpa = self.base.allocator;
3233 const n_strx = try self.strtab.insert(gpa, "dyld_stub_binder");
3234 const sym_index = @intCast(u32, self.locals.items.len);
3235 try self.locals.append(gpa, .{
32433236 .n_strx = n_strx,
32443237 .n_type = macho.N_UNDF,
32453238 .n_sect = 0,
32463239 .n_desc = 0,
32473240 .n_value = 0,
32483241 });
3249 try self.symbol_resolver.putNoClobber(self.base.allocator, n_strx, .{
3250 .where = .undef,
3251 .where_index = sym_index,
3252 });
3253 const sym = &self.undefs.items[sym_index];
3254 const sym_name = self.getString(n_strx);
3242 const sym_name = try gpa.dupe(u8, "dyld_stub_binder");
3243 const global = SymbolWithLoc{ .sym_index = sym_index, .file = null };
3244 try self.globals.putNoClobber(gpa, sym_name, global);
3245 const sym = &self.locals.items[sym_index];
32553246
32563247 for (self.dylibs.items) |dylib, id| {
32573248 if (!dylib.symbols.contains(sym_name)) continue;
......@@ -3276,205 +3267,9 @@ fn resolveDyldStubBinder(self: *MachO) !void {
32763267 }
32773268
32783269 // Add dyld_stub_binder as the final GOT entry.
3279 const target = Atom.Relocation.Target{ .global = n_strx };
3280 const atom = try self.createGotAtom(target);
3281 const got_index = @intCast(u32, self.got_entries.items.len);
3282 try self.got_entries.append(self.base.allocator, .{ .target = target, .atom = atom });
3283 try self.got_entries_table.putNoClobber(self.base.allocator, target, got_index);
3284 const match = MatchingSection{
3285 .seg = self.data_const_segment_cmd_index.?,
3286 .sect = self.got_section_index.?,
3287 };
3288 const atom_sym = &self.locals.items[atom.local_sym_index];
3289
3290 if (self.needs_prealloc) {
3291 const vaddr = try self.allocateAtom(atom, @sizeOf(u64), 8, match);
3292 log.debug("allocated {s} atom at 0x{x}", .{ self.getString(sym.n_strx), vaddr });
3293 atom_sym.n_value = vaddr;
3294 } else {
3295 const seg = &self.load_commands.items[self.data_const_segment_cmd_index.?].segment;
3296 const sect = &seg.sections.items[self.got_section_index.?];
3297 sect.size += atom.size;
3298 try self.addAtomToSection(atom, match);
3299 }
3300
3301 atom_sym.n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1);
3302}
3303
3304fn parseObjectsIntoAtoms(self: *MachO) !void {
3305 // TODO I need to see if I can simplify this logic, or perhaps split it into two functions:
3306 // one for non-prealloc traditional path, and one for incremental prealloc path.
3307 const tracy = trace(@src());
3308 defer tracy.end();
3309
3310 var parsed_atoms = std.AutoArrayHashMap(MatchingSection, *Atom).init(self.base.allocator);
3311 defer parsed_atoms.deinit();
3312
3313 var first_atoms = std.AutoArrayHashMap(MatchingSection, *Atom).init(self.base.allocator);
3314 defer first_atoms.deinit();
3315
3316 var section_metadata = std.AutoHashMap(MatchingSection, struct {
3317 size: u64,
3318 alignment: u32,
3319 }).init(self.base.allocator);
3320 defer section_metadata.deinit();
3321
3322 for (self.objects.items) |*object| {
3323 if (object.analyzed) continue;
3324
3325 try object.parseIntoAtoms(self.base.allocator, self);
3326
3327 var it = object.end_atoms.iterator();
3328 while (it.next()) |entry| {
3329 const match = entry.key_ptr.*;
3330 var atom = entry.value_ptr.*;
3331
3332 while (atom.prev) |prev| {
3333 atom = prev;
3334 }
3335
3336 const first_atom = atom;
3337
3338 const seg = self.load_commands.items[match.seg].segment;
3339 const sect = seg.sections.items[match.sect];
3340 const metadata = try section_metadata.getOrPut(match);
3341 if (!metadata.found_existing) {
3342 metadata.value_ptr.* = .{
3343 .size = sect.size,
3344 .alignment = sect.@"align",
3345 };
3346 }
3347
3348 log.debug("{s},{s}", .{ sect.segName(), sect.sectName() });
3349
3350 while (true) {
3351 const alignment = try math.powi(u32, 2, atom.alignment);
3352 const curr_size = metadata.value_ptr.size;
3353 const curr_size_aligned = mem.alignForwardGeneric(u64, curr_size, alignment);
3354 metadata.value_ptr.size = curr_size_aligned + atom.size;
3355 metadata.value_ptr.alignment = math.max(metadata.value_ptr.alignment, atom.alignment);
3356
3357 const sym = self.locals.items[atom.local_sym_index];
3358 log.debug(" {s}: n_value=0x{x}, size=0x{x}, alignment=0x{x}", .{
3359 self.getString(sym.n_strx),
3360 sym.n_value,
3361 atom.size,
3362 atom.alignment,
3363 });
3364
3365 if (atom.next) |next| {
3366 atom = next;
3367 } else break;
3368 }
3369
3370 if (parsed_atoms.getPtr(match)) |last| {
3371 last.*.next = first_atom;
3372 first_atom.prev = last.*;
3373 last.* = first_atom;
3374 }
3375 _ = try parsed_atoms.put(match, atom);
3376
3377 if (!first_atoms.contains(match)) {
3378 try first_atoms.putNoClobber(match, first_atom);
3379 }
3380 }
3381
3382 object.analyzed = true;
3383 }
3384
3385 var it = section_metadata.iterator();
3386 while (it.next()) |entry| {
3387 const match = entry.key_ptr.*;
3388 const metadata = entry.value_ptr.*;
3389 const seg = &self.load_commands.items[match.seg].segment;
3390 const sect = &seg.sections.items[match.sect];
3391 log.debug("{s},{s} => size: 0x{x}, alignment: 0x{x}", .{
3392 sect.segName(),
3393 sect.sectName(),
3394 metadata.size,
3395 metadata.alignment,
3396 });
3397
3398 sect.@"align" = math.max(sect.@"align", metadata.alignment);
3399 const needed_size = @intCast(u32, metadata.size);
3400
3401 if (self.needs_prealloc) {
3402 try self.growSection(match, needed_size);
3403 }
3404 sect.size = needed_size;
3405 }
3406
3407 for (&[_]?u16{
3408 self.text_segment_cmd_index,
3409 self.data_const_segment_cmd_index,
3410 self.data_segment_cmd_index,
3411 }) |maybe_seg_id| {
3412 const seg_id = maybe_seg_id orelse continue;
3413 const seg = self.load_commands.items[seg_id].segment;
3414
3415 for (seg.sections.items) |sect, sect_id| {
3416 const match = MatchingSection{
3417 .seg = seg_id,
3418 .sect = @intCast(u16, sect_id),
3419 };
3420 if (!section_metadata.contains(match)) continue;
3421
3422 var base_vaddr = if (self.atoms.get(match)) |last| blk: {
3423 const last_atom_sym = self.locals.items[last.local_sym_index];
3424 break :blk last_atom_sym.n_value + last.size;
3425 } else sect.addr;
3426
3427 if (self.atoms.getPtr(match)) |last| {
3428 const first_atom = first_atoms.get(match).?;
3429 last.*.next = first_atom;
3430 first_atom.prev = last.*;
3431 last.* = first_atom;
3432 }
3433 _ = try self.atoms.put(self.base.allocator, match, parsed_atoms.get(match).?);
3434
3435 if (!self.needs_prealloc) continue;
3436
3437 const n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1);
3438
3439 var atom = first_atoms.get(match).?;
3440 while (true) {
3441 const alignment = try math.powi(u32, 2, atom.alignment);
3442 base_vaddr = mem.alignForwardGeneric(u64, base_vaddr, alignment);
3443
3444 const sym = &self.locals.items[atom.local_sym_index];
3445 sym.n_value = base_vaddr;
3446 sym.n_sect = n_sect;
3447
3448 log.debug(" {s}: start=0x{x}, end=0x{x}, size=0x{x}, alignment=0x{x}", .{
3449 self.getString(sym.n_strx),
3450 base_vaddr,
3451 base_vaddr + atom.size,
3452 atom.size,
3453 atom.alignment,
3454 });
3455
3456 // Update each alias (if any)
3457 for (atom.aliases.items) |index| {
3458 const alias_sym = &self.locals.items[index];
3459 alias_sym.n_value = base_vaddr;
3460 alias_sym.n_sect = n_sect;
3461 }
3462
3463 // Update each symbol contained within the atom
3464 for (atom.contained.items) |sym_at_off| {
3465 const contained_sym = &self.locals.items[sym_at_off.local_sym_index];
3466 contained_sym.n_value = base_vaddr + sym_at_off.offset;
3467 contained_sym.n_sect = n_sect;
3468 }
3469
3470 base_vaddr += atom.size;
3471
3472 if (atom.next) |next| {
3473 atom = next;
3474 } else break;
3475 }
3476 }
3477 }
3270 const got_index = try self.allocateGotEntry(global);
3271 const got_atom = try self.createGotAtom(global);
3272 self.got_entries.items[got_index].sym_index = got_atom.sym_index;
34783273}
34793274
34803275fn addLoadDylibLC(self: *MachO, id: u16) !void {
......@@ -3511,16 +3306,8 @@ fn setEntryPoint(self: *MachO) !void {
35113306 if (self.base.options.output_mode != .Exe) return;
35123307
35133308 const seg = self.load_commands.items[self.text_segment_cmd_index.?].segment;
3514 const entry_name = self.base.options.entry orelse "_main";
3515 const n_strx = self.strtab_dir.getKeyAdapted(entry_name, StringIndexAdapter{
3516 .bytes = &self.strtab,
3517 }) orelse {
3518 log.err("entrypoint '{s}' not found", .{entry_name});
3519 return error.MissingMainEntrypoint;
3520 };
3521 const resolv = self.symbol_resolver.get(n_strx) orelse unreachable;
3522 assert(resolv.where == .global);
3523 const sym = self.globals.items[resolv.where_index];
3309 const global = try self.getEntryPoint();
3310 const sym = self.getSymbol(global);
35243311 const ec = &self.load_commands.items[self.main_cmd_index.?].main;
35253312 ec.entryoff = @intCast(u32, sym.n_value - seg.inner.vmaddr);
35263313 ec.stacksize = self.base.options.stack_size_override orelse 0;
......@@ -3529,76 +3316,77 @@ fn setEntryPoint(self: *MachO) !void {
35293316}
35303317
35313318pub fn deinit(self: *MachO) void {
3319 const gpa = self.base.allocator;
3320
35323321 if (build_options.have_llvm) {
3533 if (self.llvm_object) |llvm_object| llvm_object.destroy(self.base.allocator);
3322 if (self.llvm_object) |llvm_object| llvm_object.destroy(gpa);
35343323 }
35353324
35363325 if (self.d_sym) |*d_sym| {
3537 d_sym.deinit(self.base.allocator);
3538 }
3539
3540 self.section_ordinals.deinit(self.base.allocator);
3541 self.tlv_ptr_entries.deinit(self.base.allocator);
3542 self.tlv_ptr_entries_free_list.deinit(self.base.allocator);
3543 self.tlv_ptr_entries_table.deinit(self.base.allocator);
3544 self.got_entries.deinit(self.base.allocator);
3545 self.got_entries_free_list.deinit(self.base.allocator);
3546 self.got_entries_table.deinit(self.base.allocator);
3547 self.stubs.deinit(self.base.allocator);
3548 self.stubs_free_list.deinit(self.base.allocator);
3549 self.stubs_table.deinit(self.base.allocator);
3550 self.strtab_dir.deinit(self.base.allocator);
3551 self.strtab.deinit(self.base.allocator);
3552 self.undefs.deinit(self.base.allocator);
3553 self.globals.deinit(self.base.allocator);
3554 self.globals_free_list.deinit(self.base.allocator);
3555 self.locals.deinit(self.base.allocator);
3556 self.locals_free_list.deinit(self.base.allocator);
3557 self.symbol_resolver.deinit(self.base.allocator);
3558 self.unresolved.deinit(self.base.allocator);
3559 self.tentatives.deinit(self.base.allocator);
3326 d_sym.deinit(gpa);
3327 }
3328
3329 self.section_ordinals.deinit(gpa);
3330 self.tlv_ptr_entries.deinit(gpa);
3331 self.tlv_ptr_entries_free_list.deinit(gpa);
3332 self.tlv_ptr_entries_table.deinit(gpa);
3333 self.got_entries.deinit(gpa);
3334 self.got_entries_free_list.deinit(gpa);
3335 self.got_entries_table.deinit(gpa);
3336 self.stubs.deinit(gpa);
3337 self.stubs_free_list.deinit(gpa);
3338 self.stubs_table.deinit(gpa);
3339 self.strtab.deinit(gpa);
3340 self.locals.deinit(gpa);
3341 self.locals_free_list.deinit(gpa);
3342 self.unresolved.deinit(gpa);
3343
3344 for (self.globals.keys()) |key| {
3345 gpa.free(key);
3346 }
3347 self.globals.deinit(gpa);
35603348
35613349 for (self.objects.items) |*object| {
3562 object.deinit(self.base.allocator);
3350 object.deinit(gpa);
35633351 }
3564 self.objects.deinit(self.base.allocator);
3352 self.objects.deinit(gpa);
35653353
35663354 for (self.archives.items) |*archive| {
3567 archive.deinit(self.base.allocator);
3355 archive.deinit(gpa);
35683356 }
3569 self.archives.deinit(self.base.allocator);
3357 self.archives.deinit(gpa);
35703358
35713359 for (self.dylibs.items) |*dylib| {
3572 dylib.deinit(self.base.allocator);
3360 dylib.deinit(gpa);
35733361 }
3574 self.dylibs.deinit(self.base.allocator);
3575 self.dylibs_map.deinit(self.base.allocator);
3576 self.referenced_dylibs.deinit(self.base.allocator);
3362 self.dylibs.deinit(gpa);
3363 self.dylibs_map.deinit(gpa);
3364 self.referenced_dylibs.deinit(gpa);
35773365
35783366 for (self.load_commands.items) |*lc| {
3579 lc.deinit(self.base.allocator);
3367 lc.deinit(gpa);
35803368 }
3581 self.load_commands.deinit(self.base.allocator);
3369 self.load_commands.deinit(gpa);
35823370
35833371 for (self.managed_atoms.items) |atom| {
3584 atom.deinit(self.base.allocator);
3585 self.base.allocator.destroy(atom);
3372 atom.deinit(gpa);
3373 gpa.destroy(atom);
35863374 }
3587 self.managed_atoms.deinit(self.base.allocator);
3588 self.atoms.deinit(self.base.allocator);
3375 self.managed_atoms.deinit(gpa);
3376 self.atoms.deinit(gpa);
35893377 {
35903378 var it = self.atom_free_lists.valueIterator();
35913379 while (it.next()) |free_list| {
3592 free_list.deinit(self.base.allocator);
3380 free_list.deinit(gpa);
35933381 }
3594 self.atom_free_lists.deinit(self.base.allocator);
3382 self.atom_free_lists.deinit(gpa);
35953383 }
35963384 if (self.base.options.module) |mod| {
35973385 for (self.decls.keys()) |decl_index| {
35983386 const decl = mod.declPtr(decl_index);
3599 decl.link.macho.deinit(self.base.allocator);
3387 decl.link.macho.deinit(gpa);
36003388 }
3601 self.decls.deinit(self.base.allocator);
3389 self.decls.deinit(gpa);
36023390 } else {
36033391 assert(self.decls.count() == 0);
36043392 }
......@@ -3606,15 +3394,15 @@ pub fn deinit(self: *MachO) void {
36063394 {
36073395 var it = self.unnamed_const_atoms.valueIterator();
36083396 while (it.next()) |atoms| {
3609 atoms.deinit(self.base.allocator);
3397 atoms.deinit(gpa);
36103398 }
3611 self.unnamed_const_atoms.deinit(self.base.allocator);
3399 self.unnamed_const_atoms.deinit(gpa);
36123400 }
36133401
3614 self.atom_by_index_table.deinit(self.base.allocator);
3402 self.atom_by_index_table.deinit(gpa);
36153403
36163404 if (self.code_signature) |*csig| {
3617 csig.deinit(self.base.allocator);
3405 csig.deinit(gpa);
36183406 }
36193407}
36203408
......@@ -3670,7 +3458,7 @@ fn freeAtom(self: *MachO, atom: *Atom, match: MatchingSection, owns_atom: bool)
36703458 if (atom.prev) |prev| {
36713459 prev.next = atom.next;
36723460
3673 if (!already_have_free_list_node and prev.freeListEligible(self.*)) {
3461 if (!already_have_free_list_node and prev.freeListEligible(self)) {
36743462 // The free list is heuristics, it doesn't have to be perfect, so we can ignore
36753463 // the OOM here.
36763464 free_list.append(self.base.allocator, prev) catch {};
......@@ -3700,14 +3488,14 @@ fn shrinkAtom(self: *MachO, atom: *Atom, new_block_size: u64, match: MatchingSec
37003488}
37013489
37023490fn growAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, match: MatchingSection) !u64 {
3703 const sym = self.locals.items[atom.local_sym_index];
3491 const sym = atom.getSymbol(self);
37043492 const align_ok = mem.alignBackwardGeneric(u64, sym.n_value, alignment) == sym.n_value;
3705 const need_realloc = !align_ok or new_atom_size > atom.capacity(self.*);
3493 const need_realloc = !align_ok or new_atom_size > atom.capacity(self);
37063494 if (!need_realloc) return sym.n_value;
37073495 return self.allocateAtom(atom, new_atom_size, alignment, match);
37083496}
37093497
3710fn allocateLocalSymbol(self: *MachO) !u32 {
3498fn allocateSymbol(self: *MachO) !u32 {
37113499 try self.locals.ensureUnusedCapacity(self.base.allocator, 1);
37123500
37133501 const index = blk: {
......@@ -3733,8 +3521,9 @@ fn allocateLocalSymbol(self: *MachO) !u32 {
37333521 return index;
37343522}
37353523
3736pub fn allocateGotEntry(self: *MachO, target: Atom.Relocation.Target) !u32 {
3737 try self.got_entries.ensureUnusedCapacity(self.base.allocator, 1);
3524pub fn allocateGotEntry(self: *MachO, target: SymbolWithLoc) !u32 {
3525 const gpa = self.base.allocator;
3526 try self.got_entries.ensureUnusedCapacity(gpa, 1);
37383527
37393528 const index = blk: {
37403529 if (self.got_entries_free_list.popOrNull()) |index| {
......@@ -3748,16 +3537,13 @@ pub fn allocateGotEntry(self: *MachO, target: Atom.Relocation.Target) !u32 {
37483537 }
37493538 };
37503539
3751 self.got_entries.items[index] = .{
3752 .target = target,
3753 .atom = undefined,
3754 };
3755 try self.got_entries_table.putNoClobber(self.base.allocator, target, index);
3540 self.got_entries.items[index] = .{ .target = target, .sym_index = 0 };
3541 try self.got_entries_table.putNoClobber(gpa, target, index);
37563542
37573543 return index;
37583544}
37593545
3760pub fn allocateStubEntry(self: *MachO, n_strx: u32) !u32 {
3546pub fn allocateStubEntry(self: *MachO, target: SymbolWithLoc) !u32 {
37613547 try self.stubs.ensureUnusedCapacity(self.base.allocator, 1);
37623548
37633549 const index = blk: {
......@@ -3772,13 +3558,13 @@ pub fn allocateStubEntry(self: *MachO, n_strx: u32) !u32 {
37723558 }
37733559 };
37743560
3775 self.stubs.items[index] = undefined;
3776 try self.stubs_table.putNoClobber(self.base.allocator, n_strx, index);
3561 self.stubs.items[index] = .{ .target = target, .sym_index = 0 };
3562 try self.stubs_table.putNoClobber(self.base.allocator, target, index);
37773563
37783564 return index;
37793565}
37803566
3781pub fn allocateTlvPtrEntry(self: *MachO, target: Atom.Relocation.Target) !u32 {
3567pub fn allocateTlvPtrEntry(self: *MachO, target: SymbolWithLoc) !u32 {
37823568 try self.tlv_ptr_entries.ensureUnusedCapacity(self.base.allocator, 1);
37833569
37843570 const index = blk: {
......@@ -3793,7 +3579,7 @@ pub fn allocateTlvPtrEntry(self: *MachO, target: Atom.Relocation.Target) !u32 {
37933579 }
37943580 };
37953581
3796 self.tlv_ptr_entries.items[index] = .{ .target = target, .atom = undefined };
3582 self.tlv_ptr_entries.items[index] = .{ .target = target, .sym_index = 0 };
37973583 try self.tlv_ptr_entries_table.putNoClobber(self.base.allocator, target, index);
37983584
37993585 return index;
......@@ -3802,16 +3588,11 @@ pub fn allocateTlvPtrEntry(self: *MachO, target: Atom.Relocation.Target) !u32 {
38023588pub fn allocateDeclIndexes(self: *MachO, decl_index: Module.Decl.Index) !void {
38033589 if (self.llvm_object) |_| return;
38043590 const decl = self.base.options.module.?.declPtr(decl_index);
3805 if (decl.link.macho.local_sym_index != 0) return;
3591 if (decl.link.macho.sym_index != 0) return;
38063592
3807 decl.link.macho.local_sym_index = try self.allocateLocalSymbol();
3808 try self.atom_by_index_table.putNoClobber(self.base.allocator, decl.link.macho.local_sym_index, &decl.link.macho);
3593 decl.link.macho.sym_index = try self.allocateSymbol();
3594 try self.atom_by_index_table.putNoClobber(self.base.allocator, decl.link.macho.sym_index, &decl.link.macho);
38093595 try self.decls.putNoClobber(self.base.allocator, decl_index, null);
3810
3811 const got_target = .{ .local = decl.link.macho.local_sym_index };
3812 const got_index = try self.allocateGotEntry(got_target);
3813 const got_atom = try self.createGotAtom(got_target);
3814 self.got_entries.items[got_index].atom = got_atom;
38153596}
38163597
38173598pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {
......@@ -3862,14 +3643,14 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv
38623643 },
38633644 }
38643645
3865 const symbol = try self.placeDecl(decl_index, decl.link.macho.code.items.len);
3646 const addr = try self.placeDecl(decl_index, decl.link.macho.code.items.len);
38663647
38673648 if (decl_state) |*ds| {
38683649 try self.d_sym.?.dwarf.commitDeclState(
38693650 &self.base,
38703651 module,
38713652 decl,
3872 symbol.n_value,
3653 addr,
38733654 decl.link.macho.size,
38743655 ds,
38753656 );
......@@ -3885,8 +3666,9 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
38853666 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
38863667 defer code_buffer.deinit();
38873668
3669 const gpa = self.base.allocator;
38883670 const module = self.base.options.module.?;
3889 const gop = try self.unnamed_const_atoms.getOrPut(self.base.allocator, decl_index);
3671 const gop = try self.unnamed_const_atoms.getOrPut(gpa, decl_index);
38903672 if (!gop.found_existing) {
38913673 gop.value_ptr.* = .{};
38923674 }
......@@ -3894,25 +3676,32 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
38943676
38953677 const decl = module.declPtr(decl_index);
38963678 const decl_name = try decl.getFullyQualifiedName(module);
3897 defer self.base.allocator.free(decl_name);
3679 defer gpa.free(decl_name);
38983680
38993681 const name_str_index = blk: {
39003682 const index = unnamed_consts.items.len;
3901 const name = try std.fmt.allocPrint(self.base.allocator, "__unnamed_{s}_{d}", .{ decl_name, index });
3902 defer self.base.allocator.free(name);
3903 break :blk try self.makeString(name);
3683 const name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
3684 defer gpa.free(name);
3685 break :blk try self.strtab.insert(gpa, name);
39043686 };
3905 const name = self.getString(name_str_index);
3687 const name = self.strtab.get(name_str_index);
39063688
39073689 log.debug("allocating symbol indexes for {s}", .{name});
39083690
39093691 const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
3910 const local_sym_index = try self.allocateLocalSymbol();
3911 const atom = try self.createEmptyAtom(local_sym_index, @sizeOf(u64), math.log2(required_alignment));
3912 try self.atom_by_index_table.putNoClobber(self.base.allocator, local_sym_index, atom);
3692 const sym_index = try self.allocateSymbol();
3693 const atom = try MachO.createEmptyAtom(
3694 gpa,
3695 sym_index,
3696 @sizeOf(u64),
3697 math.log2(required_alignment),
3698 );
3699
3700 try self.managed_atoms.append(gpa, atom);
3701 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
39133702
39143703 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .none, .{
3915 .parent_atom_index = local_sym_index,
3704 .parent_atom_index = sym_index,
39163705 });
39173706 const code = switch (res) {
39183707 .externally_managed => |x| x,
......@@ -3926,7 +3715,7 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
39263715 };
39273716
39283717 atom.code.clearRetainingCapacity();
3929 try atom.code.appendSlice(self.base.allocator, code);
3718 try atom.code.appendSlice(gpa, code);
39303719
39313720 const match = try self.getMatchingSectionAtom(
39323721 atom,
......@@ -3942,18 +3731,18 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
39423731
39433732 errdefer self.freeAtom(atom, match, true);
39443733
3945 const symbol = &self.locals.items[atom.local_sym_index];
3734 const symbol = atom.getSymbolPtr(self);
39463735 symbol.* = .{
39473736 .n_strx = name_str_index,
39483737 .n_type = macho.N_SECT,
3949 .n_sect = @intCast(u8, self.section_ordinals.getIndex(match).?) + 1,
3738 .n_sect = self.getSectionOrdinal(match),
39503739 .n_desc = 0,
39513740 .n_value = addr,
39523741 };
39533742
3954 try unnamed_consts.append(self.base.allocator, atom);
3743 try unnamed_consts.append(gpa, atom);
39553744
3956 return atom.local_sym_index;
3745 return atom.sym_index;
39573746}
39583747
39593748pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index) !void {
......@@ -3995,14 +3784,14 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
39953784 }, &code_buffer, .{
39963785 .dwarf = ds,
39973786 }, .{
3998 .parent_atom_index = decl.link.macho.local_sym_index,
3787 .parent_atom_index = decl.link.macho.sym_index,
39993788 })
40003789 else
40013790 try codegen.generateSymbol(&self.base, decl.srcLoc(), .{
40023791 .ty = decl.ty,
40033792 .val = decl_val,
40043793 }, &code_buffer, .none, .{
4005 .parent_atom_index = decl.link.macho.local_sym_index,
3794 .parent_atom_index = decl.link.macho.sym_index,
40063795 });
40073796
40083797 const code = blk: {
......@@ -4025,14 +3814,14 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
40253814 },
40263815 }
40273816 };
4028 const symbol = try self.placeDecl(decl_index, code.len);
3817 const addr = try self.placeDecl(decl_index, code.len);
40293818
40303819 if (decl_state) |*ds| {
40313820 try self.d_sym.?.dwarf.commitDeclState(
40323821 &self.base,
40333822 module,
40343823 decl,
4035 symbol.n_value,
3824 addr,
40363825 decl.link.macho.size,
40373826 ds,
40383827 );
......@@ -4177,8 +3966,7 @@ fn getMatchingSectionAtom(
41773966 .@"align" = align_log_2,
41783967 })).?;
41793968 };
4180 const seg = self.load_commands.items[match.seg].segment;
4181 const sect = seg.sections.items[match.sect];
3969 const sect = self.getSection(match);
41823970 log.debug(" allocating atom '{s}' in '{s},{s}' ({d},{d})", .{
41833971 name,
41843972 sect.segName(),
......@@ -4189,12 +3977,11 @@ fn getMatchingSectionAtom(
41893977 return match;
41903978}
41913979
4192fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !*macho.nlist_64 {
3980fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !u64 {
41933981 const module = self.base.options.module.?;
41943982 const decl = module.declPtr(decl_index);
41953983 const required_alignment = decl.getAlignment(self.base.options.target);
4196 assert(decl.link.macho.local_sym_index != 0); // Caller forgot to call allocateDeclIndexes()
4197 const symbol = &self.locals.items[decl.link.macho.local_sym_index];
3984 assert(decl.link.macho.sym_index != 0); // Caller forgot to call allocateDeclIndexes()
41983985
41993986 const sym_name = try decl.getFullyQualifiedName(module);
42003987 defer self.base.allocator.free(sym_name);
......@@ -4212,7 +3999,8 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !*mac
42123999 const match = decl_ptr.*.?;
42134000
42144001 if (decl.link.macho.size != 0) {
4215 const capacity = decl.link.macho.capacity(self.*);
4002 const symbol = decl.link.macho.getSymbolPtr(self);
4003 const capacity = decl.link.macho.capacity(self);
42164004 const need_realloc = code_len > capacity or !mem.isAlignedGeneric(u64, symbol.n_value, required_alignment);
42174005
42184006 if (need_realloc) {
......@@ -4220,18 +4008,24 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !*mac
42204008 log.debug("growing {s} and moving from 0x{x} to 0x{x}", .{ sym_name, symbol.n_value, vaddr });
42214009 log.debug(" (required alignment 0x{x})", .{required_alignment});
42224010 symbol.n_value = vaddr;
4011
4012 const got_atom = self.getGotAtomForSymbol(.{
4013 .sym_index = decl.link.macho.sym_index,
4014 .file = null,
4015 }).?;
4016 got_atom.dirty = true;
42234017 } else if (code_len < decl.link.macho.size) {
42244018 self.shrinkAtom(&decl.link.macho, code_len, match);
42254019 }
42264020 decl.link.macho.size = code_len;
42274021 decl.link.macho.dirty = true;
42284022
4229 symbol.n_strx = try self.makeString(sym_name);
4023 symbol.n_strx = try self.strtab.insert(self.base.allocator, sym_name);
42304024 symbol.n_type = macho.N_SECT;
42314025 symbol.n_sect = @intCast(u8, self.text_section_index.?) + 1;
42324026 symbol.n_desc = 0;
42334027 } else {
4234 const name_str_index = try self.makeString(sym_name);
4028 const name_str_index = try self.strtab.insert(self.base.allocator, sym_name);
42354029 const addr = try self.allocateAtom(&decl.link.macho, code_len, required_alignment, match);
42364030
42374031 log.debug("allocated atom for {s} at 0x{x}", .{ sym_name, addr });
......@@ -4239,28 +4033,22 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !*mac
42394033
42404034 errdefer self.freeAtom(&decl.link.macho, match, false);
42414035
4036 const symbol = decl.link.macho.getSymbolPtr(self);
42424037 symbol.* = .{
42434038 .n_strx = name_str_index,
42444039 .n_type = macho.N_SECT,
4245 .n_sect = @intCast(u8, self.section_ordinals.getIndex(match).?) + 1,
4040 .n_sect = self.getSectionOrdinal(match),
42464041 .n_desc = 0,
42474042 .n_value = addr,
42484043 };
4249 const got_index = self.got_entries_table.get(.{ .local = decl.link.macho.local_sym_index }).?;
4250 const got_atom = self.got_entries.items[got_index].atom;
4251 const got_sym = &self.locals.items[got_atom.local_sym_index];
4252 const vaddr = try self.allocateAtom(got_atom, @sizeOf(u64), 8, .{
4253 .seg = self.data_const_segment_cmd_index.?,
4254 .sect = self.got_section_index.?,
4255 });
4256 got_sym.n_value = vaddr;
4257 got_sym.n_sect = @intCast(u8, self.section_ordinals.getIndex(.{
4258 .seg = self.data_const_segment_cmd_index.?,
4259 .sect = self.got_section_index.?,
4260 }).? + 1);
4044
4045 const got_target = SymbolWithLoc{ .sym_index = decl.link.macho.sym_index, .file = null };
4046 const got_index = try self.allocateGotEntry(got_target);
4047 const got_atom = try self.createGotAtom(got_target);
4048 self.got_entries.items[got_index].sym_index = got_atom.sym_index;
42614049 }
42624050
4263 return symbol;
4051 return decl.link.macho.getSymbol(self).n_value;
42644052}
42654053
42664054pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl: *const Module.Decl) !void {
......@@ -4280,19 +4068,23 @@ pub fn updateDeclExports(
42804068 @panic("Attempted to compile for object format that was disabled by build configuration");
42814069 }
42824070 if (build_options.have_llvm) {
4283 if (self.llvm_object) |llvm_object| return llvm_object.updateDeclExports(module, decl_index, exports);
4071 if (self.llvm_object) |llvm_object|
4072 return llvm_object.updateDeclExports(module, decl_index, exports);
42844073 }
42854074 const tracy = trace(@src());
42864075 defer tracy.end();
42874076
4288 try self.globals.ensureUnusedCapacity(self.base.allocator, exports.len);
4077 const gpa = self.base.allocator;
4078
42894079 const decl = module.declPtr(decl_index);
4290 if (decl.link.macho.local_sym_index == 0) return;
4291 const decl_sym = &self.locals.items[decl.link.macho.local_sym_index];
4080 if (decl.link.macho.sym_index == 0) return;
4081 const decl_sym = decl.link.macho.getSymbol(self);
42924082
42934083 for (exports) |exp| {
4294 const exp_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{exp.options.name});
4295 defer self.base.allocator.free(exp_name);
4084 const exp_name = try std.fmt.allocPrint(gpa, "_{s}", .{exp.options.name});
4085 defer gpa.free(exp_name);
4086
4087 log.debug("adding new export '{s}'", .{exp_name});
42964088
42974089 if (exp.options.section) |section_name| {
42984090 if (!mem.eql(u8, section_name, "__text")) {
......@@ -4300,7 +4092,7 @@ pub fn updateDeclExports(
43004092 module.gpa,
43014093 exp,
43024094 try Module.ErrorMsg.create(
4303 self.base.allocator,
4095 gpa,
43044096 decl.srcLoc(),
43054097 "Unimplemented: ExportOptions.section",
43064098 .{},
......@@ -4315,7 +4107,7 @@ pub fn updateDeclExports(
43154107 module.gpa,
43164108 exp,
43174109 try Module.ErrorMsg.create(
4318 self.base.allocator,
4110 gpa,
43194111 decl.srcLoc(),
43204112 "Unimplemented: GlobalLinkage.LinkOnce",
43214113 .{},
......@@ -4324,103 +4116,85 @@ pub fn updateDeclExports(
43244116 continue;
43254117 }
43264118
4327 const is_weak = exp.options.linkage == .Internal or exp.options.linkage == .Weak;
4328 const n_strx = try self.makeString(exp_name);
4329 if (self.symbol_resolver.getPtr(n_strx)) |resolv| {
4330 switch (resolv.where) {
4331 .global => {
4332 if (resolv.local_sym_index == decl.link.macho.local_sym_index) continue;
4333
4334 const sym = &self.globals.items[resolv.where_index];
4335
4336 if (sym.tentative()) {
4337 assert(self.tentatives.swapRemove(resolv.where_index));
4338 } else if (!is_weak and !(sym.weakDef() or sym.pext())) {
4339 _ = try module.failed_exports.put(
4340 module.gpa,
4341 exp,
4342 try Module.ErrorMsg.create(
4343 self.base.allocator,
4344 decl.srcLoc(),
4345 \\LinkError: symbol '{s}' defined multiple times
4346 \\ first definition in '{s}'
4347 ,
4348 .{ exp_name, self.objects.items[resolv.file.?].name },
4349 ),
4350 );
4351 continue;
4352 } else if (is_weak) continue; // Current symbol is weak, so skip it.
4353
4354 // Otherwise, update the resolver and the global symbol.
4355 sym.n_type = macho.N_SECT | macho.N_EXT;
4356 resolv.local_sym_index = decl.link.macho.local_sym_index;
4357 resolv.file = null;
4358 exp.link.macho.sym_index = resolv.where_index;
4359
4360 continue;
4361 },
4362 .undef => {
4363 assert(self.unresolved.swapRemove(resolv.where_index));
4364 _ = self.symbol_resolver.remove(n_strx);
4365 },
4366 }
4367 }
4368
4369 var n_type: u8 = macho.N_SECT | macho.N_EXT;
4370 var n_desc: u16 = 0;
4119 const sym_index = exp.link.macho.sym_index orelse blk: {
4120 const sym_index = try self.allocateSymbol();
4121 exp.link.macho.sym_index = sym_index;
4122 break :blk sym_index;
4123 };
4124 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
4125 const sym = self.getSymbolPtr(sym_loc);
4126 sym.* = .{
4127 .n_strx = try self.strtab.insert(gpa, exp_name),
4128 .n_type = macho.N_SECT | macho.N_EXT,
4129 .n_sect = self.getSectionOrdinal(.{
4130 .seg = self.text_segment_cmd_index.?,
4131 .sect = self.text_section_index.?, // TODO what if we export a variable?
4132 }),
4133 .n_desc = 0,
4134 .n_value = decl_sym.n_value,
4135 };
43714136
43724137 switch (exp.options.linkage) {
43734138 .Internal => {
43744139 // Symbol should be hidden, or in MachO lingo, private extern.
43754140 // We should also mark the symbol as Weak: n_desc == N_WEAK_DEF.
4376 // TODO work out when to add N_WEAK_REF.
4377 n_type |= macho.N_PEXT;
4378 n_desc |= macho.N_WEAK_DEF;
4141 sym.n_type |= macho.N_PEXT;
4142 sym.n_desc |= macho.N_WEAK_DEF;
43794143 },
43804144 .Strong => {},
43814145 .Weak => {
43824146 // Weak linkage is specified as part of n_desc field.
43834147 // Symbol's n_type is like for a symbol with strong linkage.
4384 n_desc |= macho.N_WEAK_DEF;
4148 sym.n_desc |= macho.N_WEAK_DEF;
43854149 },
43864150 else => unreachable,
43874151 }
43884152
4389 const global_sym_index = if (exp.link.macho.sym_index) |i| i else blk: {
4390 const i = if (self.globals_free_list.popOrNull()) |i| i else inner: {
4391 _ = self.globals.addOneAssumeCapacity();
4392 break :inner @intCast(u32, self.globals.items.len - 1);
4393 };
4394 break :blk i;
4395 };
4396 const sym = &self.globals.items[global_sym_index];
4397 sym.* = .{
4398 .n_strx = try self.makeString(exp_name),
4399 .n_type = n_type,
4400 .n_sect = @intCast(u8, self.text_section_index.?) + 1,
4401 .n_desc = n_desc,
4402 .n_value = decl_sym.n_value,
4153 self.resolveGlobalSymbol(sym_loc) catch |err| switch (err) {
4154 error.MultipleSymbolDefinitions => {
4155 const global = self.globals.get(exp_name).?;
4156 if (sym_loc.sym_index != global.sym_index and global.file != null) {
4157 _ = try module.failed_exports.put(module.gpa, exp, try Module.ErrorMsg.create(
4158 gpa,
4159 decl.srcLoc(),
4160 \\LinkError: symbol '{s}' defined multiple times
4161 \\ first definition in '{s}'
4162 ,
4163 .{ exp_name, self.objects.items[global.file.?].name },
4164 ));
4165 }
4166 },
4167 else => |e| return e,
44034168 };
4404 exp.link.macho.sym_index = global_sym_index;
4405
4406 try self.symbol_resolver.putNoClobber(self.base.allocator, n_strx, .{
4407 .where = .global,
4408 .where_index = global_sym_index,
4409 .local_sym_index = decl.link.macho.local_sym_index,
4410 });
44114169 }
44124170}
44134171
44144172pub fn deleteExport(self: *MachO, exp: Export) void {
44154173 if (self.llvm_object) |_| return;
44164174 const sym_index = exp.sym_index orelse return;
4417 self.globals_free_list.append(self.base.allocator, sym_index) catch {};
4418 const global = &self.globals.items[sym_index];
4419 log.debug("deleting export '{s}': {}", .{ self.getString(global.n_strx), global });
4420 assert(self.symbol_resolver.remove(global.n_strx));
4421 global.n_type = 0;
4422 global.n_strx = 0;
4423 global.n_value = 0;
4175
4176 const gpa = self.base.allocator;
4177
4178 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
4179 const sym = self.getSymbolPtr(sym_loc);
4180 const sym_name = self.getSymbolName(sym_loc);
4181 log.debug("deleting export '{s}'", .{sym_name});
4182 assert(sym.sect() and sym.ext());
4183 sym.* = .{
4184 .n_strx = 0,
4185 .n_type = 0,
4186 .n_sect = 0,
4187 .n_desc = 0,
4188 .n_value = 0,
4189 };
4190 self.locals_free_list.append(gpa, sym_index) catch {};
4191
4192 if (self.globals.get(sym_name)) |global| blk: {
4193 if (global.sym_index != sym_index) break :blk;
4194 if (global.file != null) break :blk;
4195 const kv = self.globals.fetchSwapRemove(sym_name);
4196 gpa.free(kv.?.key);
4197 }
44244198}
44254199
44264200fn freeUnnamedConsts(self: *MachO, decl_index: Module.Decl.Index) void {
......@@ -4430,11 +4204,11 @@ fn freeUnnamedConsts(self: *MachO, decl_index: Module.Decl.Index) void {
44304204 .seg = self.text_segment_cmd_index.?,
44314205 .sect = self.text_const_section_index.?,
44324206 }, true);
4433 self.locals_free_list.append(self.base.allocator, atom.local_sym_index) catch {};
4434 self.locals.items[atom.local_sym_index].n_type = 0;
4435 _ = self.atom_by_index_table.remove(atom.local_sym_index);
4436 log.debug(" adding local symbol index {d} to free list", .{atom.local_sym_index});
4437 atom.local_sym_index = 0;
4207 self.locals_free_list.append(self.base.allocator, atom.sym_index) catch {};
4208 self.locals.items[atom.sym_index].n_type = 0;
4209 _ = self.atom_by_index_table.remove(atom.sym_index);
4210 log.debug(" adding local symbol index {d} to free list", .{atom.sym_index});
4211 atom.sym_index = 0;
44384212 }
44394213 unnamed_consts.clearAndFree(self.base.allocator);
44404214}
......@@ -4452,29 +4226,33 @@ pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void {
44524226 self.freeUnnamedConsts(decl_index);
44534227 }
44544228 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
4455 if (decl.link.macho.local_sym_index != 0) {
4456 self.locals_free_list.append(self.base.allocator, decl.link.macho.local_sym_index) catch {};
4229 if (decl.link.macho.sym_index != 0) {
4230 self.locals_free_list.append(self.base.allocator, decl.link.macho.sym_index) catch {};
44574231
44584232 // Try freeing GOT atom if this decl had one
4459 if (self.got_entries_table.get(.{ .local = decl.link.macho.local_sym_index })) |got_index| {
4233 const got_target = SymbolWithLoc{ .sym_index = decl.link.macho.sym_index, .file = null };
4234 if (self.got_entries_table.get(got_target)) |got_index| {
44604235 self.got_entries_free_list.append(self.base.allocator, @intCast(u32, got_index)) catch {};
4461 self.got_entries.items[got_index] = .{ .target = .{ .local = 0 }, .atom = undefined };
4462 _ = self.got_entries_table.swapRemove(.{ .local = decl.link.macho.local_sym_index });
4236 self.got_entries.items[got_index] = .{
4237 .target = .{ .sym_index = 0, .file = null },
4238 .sym_index = 0,
4239 };
4240 _ = self.got_entries_table.remove(got_target);
44634241
44644242 if (self.d_sym) |*d_sym| {
4465 d_sym.swapRemoveRelocs(decl.link.macho.local_sym_index);
4243 d_sym.swapRemoveRelocs(decl.link.macho.sym_index);
44664244 }
44674245
44684246 log.debug(" adding GOT index {d} to free list (target local@{d})", .{
44694247 got_index,
4470 decl.link.macho.local_sym_index,
4248 decl.link.macho.sym_index,
44714249 });
44724250 }
44734251
4474 self.locals.items[decl.link.macho.local_sym_index].n_type = 0;
4475 _ = self.atom_by_index_table.remove(decl.link.macho.local_sym_index);
4476 log.debug(" adding local symbol index {d} to free list", .{decl.link.macho.local_sym_index});
4477 decl.link.macho.local_sym_index = 0;
4252 self.locals.items[decl.link.macho.sym_index].n_type = 0;
4253 _ = self.atom_by_index_table.remove(decl.link.macho.sym_index);
4254 log.debug(" adding local symbol index {d} to free list", .{decl.link.macho.sym_index});
4255 decl.link.macho.sym_index = 0;
44784256 }
44794257 if (self.d_sym) |*d_sym| {
44804258 d_sym.dwarf.freeDecl(decl);
......@@ -4486,12 +4264,12 @@ pub fn getDeclVAddr(self: *MachO, decl_index: Module.Decl.Index, reloc_info: Fil
44864264 const decl = mod.declPtr(decl_index);
44874265
44884266 assert(self.llvm_object == null);
4489 assert(decl.link.macho.local_sym_index != 0);
4267 assert(decl.link.macho.sym_index != 0);
44904268
44914269 const atom = self.atom_by_index_table.get(reloc_info.parent_atom_index).?;
44924270 try atom.relocs.append(self.base.allocator, .{
44934271 .offset = @intCast(u32, reloc_info.offset),
4494 .target = .{ .local = decl.link.macho.local_sym_index },
4272 .target = .{ .sym_index = decl.link.macho.sym_index, .file = null },
44954273 .addend = reloc_info.addend,
44964274 .subtractor = null,
44974275 .pcrel = false,
......@@ -4534,7 +4312,7 @@ fn populateMissingMetadata(self: *MachO) !void {
45344312
45354313 if (self.text_segment_cmd_index == null) {
45364314 self.text_segment_cmd_index = @intCast(u16, self.load_commands.items.len);
4537 const needed_size = if (self.needs_prealloc) blk: {
4315 const needed_size = if (self.mode == .incremental) blk: {
45384316 const headerpad_size = @maximum(self.base.options.headerpad_size orelse 0, default_headerpad_size);
45394317 const program_code_size_hint = self.base.options.program_code_size_hint;
45404318 const got_size_hint = @sizeOf(u64) * self.base.options.symbol_count_hint;
......@@ -4565,7 +4343,7 @@ fn populateMissingMetadata(self: *MachO) !void {
45654343 .aarch64 => 2,
45664344 else => unreachable, // unhandled architecture type
45674345 };
4568 const needed_size = if (self.needs_prealloc) self.base.options.program_code_size_hint else 0;
4346 const needed_size = if (self.mode == .incremental) self.base.options.program_code_size_hint else 0;
45694347 self.text_section_index = try self.initSection(
45704348 self.text_segment_cmd_index.?,
45714349 "__text",
......@@ -4588,7 +4366,7 @@ fn populateMissingMetadata(self: *MachO) !void {
45884366 .aarch64 => 3 * @sizeOf(u32),
45894367 else => unreachable, // unhandled architecture type
45904368 };
4591 const needed_size = if (self.needs_prealloc) stub_size * self.base.options.symbol_count_hint else 0;
4369 const needed_size = if (self.mode == .incremental) stub_size * self.base.options.symbol_count_hint else 0;
45924370 self.stubs_section_index = try self.initSection(
45934371 self.text_segment_cmd_index.?,
45944372 "__stubs",
......@@ -4617,7 +4395,7 @@ fn populateMissingMetadata(self: *MachO) !void {
46174395 .aarch64 => 3 * @sizeOf(u32),
46184396 else => unreachable,
46194397 };
4620 const needed_size = if (self.needs_prealloc)
4398 const needed_size = if (self.mode == .incremental)
46214399 stub_size * self.base.options.symbol_count_hint + preamble_size
46224400 else
46234401 0;
......@@ -4637,7 +4415,7 @@ fn populateMissingMetadata(self: *MachO) !void {
46374415 var vmaddr: u64 = 0;
46384416 var fileoff: u64 = 0;
46394417 var needed_size: u64 = 0;
4640 if (self.needs_prealloc) {
4418 if (self.mode == .incremental) {
46414419 const base = self.getSegmentAllocBase(&.{self.text_segment_cmd_index.?});
46424420 vmaddr = base.vmaddr;
46434421 fileoff = base.fileoff;
......@@ -4666,7 +4444,7 @@ fn populateMissingMetadata(self: *MachO) !void {
46664444 }
46674445
46684446 if (self.got_section_index == null) {
4669 const needed_size = if (self.needs_prealloc)
4447 const needed_size = if (self.mode == .incremental)
46704448 @sizeOf(u64) * self.base.options.symbol_count_hint
46714449 else
46724450 0;
......@@ -4687,7 +4465,7 @@ fn populateMissingMetadata(self: *MachO) !void {
46874465 var vmaddr: u64 = 0;
46884466 var fileoff: u64 = 0;
46894467 var needed_size: u64 = 0;
4690 if (self.needs_prealloc) {
4468 if (self.mode == .incremental) {
46914469 const base = self.getSegmentAllocBase(&.{self.data_const_segment_cmd_index.?});
46924470 vmaddr = base.vmaddr;
46934471 fileoff = base.fileoff;
......@@ -4716,7 +4494,7 @@ fn populateMissingMetadata(self: *MachO) !void {
47164494 }
47174495
47184496 if (self.la_symbol_ptr_section_index == null) {
4719 const needed_size = if (self.needs_prealloc)
4497 const needed_size = if (self.mode == .incremental)
47204498 @sizeOf(u64) * self.base.options.symbol_count_hint
47214499 else
47224500 0;
......@@ -4733,7 +4511,10 @@ fn populateMissingMetadata(self: *MachO) !void {
47334511 }
47344512
47354513 if (self.data_section_index == null) {
4736 const needed_size = if (self.needs_prealloc) @sizeOf(u64) * self.base.options.symbol_count_hint else 0;
4514 const needed_size = if (self.mode == .incremental)
4515 @sizeOf(u64) * self.base.options.symbol_count_hint
4516 else
4517 0;
47374518 const alignment: u16 = 3; // 2^3 = @sizeOf(u64)
47384519 self.data_section_index = try self.initSection(
47394520 self.data_segment_cmd_index.?,
......@@ -4745,7 +4526,10 @@ fn populateMissingMetadata(self: *MachO) !void {
47454526 }
47464527
47474528 if (self.tlv_section_index == null) {
4748 const needed_size = if (self.needs_prealloc) @sizeOf(u64) * self.base.options.symbol_count_hint else 0;
4529 const needed_size = if (self.mode == .incremental)
4530 @sizeOf(u64) * self.base.options.symbol_count_hint
4531 else
4532 0;
47494533 const alignment: u16 = 3; // 2^3 = @sizeOf(u64)
47504534 self.tlv_section_index = try self.initSection(
47514535 self.data_segment_cmd_index.?,
......@@ -4759,7 +4543,10 @@ fn populateMissingMetadata(self: *MachO) !void {
47594543 }
47604544
47614545 if (self.tlv_data_section_index == null) {
4762 const needed_size = if (self.needs_prealloc) @sizeOf(u64) * self.base.options.symbol_count_hint else 0;
4546 const needed_size = if (self.mode == .incremental)
4547 @sizeOf(u64) * self.base.options.symbol_count_hint
4548 else
4549 0;
47634550 const alignment: u16 = 3; // 2^3 = @sizeOf(u64)
47644551 self.tlv_data_section_index = try self.initSection(
47654552 self.data_segment_cmd_index.?,
......@@ -4773,7 +4560,10 @@ fn populateMissingMetadata(self: *MachO) !void {
47734560 }
47744561
47754562 if (self.tlv_bss_section_index == null) {
4776 const needed_size = if (self.needs_prealloc) @sizeOf(u64) * self.base.options.symbol_count_hint else 0;
4563 const needed_size = if (self.mode == .incremental)
4564 @sizeOf(u64) * self.base.options.symbol_count_hint
4565 else
4566 0;
47774567 const alignment: u16 = 3; // 2^3 = @sizeOf(u64)
47784568 self.tlv_bss_section_index = try self.initSection(
47794569 self.data_segment_cmd_index.?,
......@@ -4787,7 +4577,10 @@ fn populateMissingMetadata(self: *MachO) !void {
47874577 }
47884578
47894579 if (self.bss_section_index == null) {
4790 const needed_size = if (self.needs_prealloc) @sizeOf(u64) * self.base.options.symbol_count_hint else 0;
4580 const needed_size = if (self.mode == .incremental)
4581 @sizeOf(u64) * self.base.options.symbol_count_hint
4582 else
4583 0;
47914584 const alignment: u16 = 3; // 2^3 = @sizeOf(u64)
47924585 self.bss_section_index = try self.initSection(
47934586 self.data_segment_cmd_index.?,
......@@ -4804,7 +4597,7 @@ fn populateMissingMetadata(self: *MachO) !void {
48044597 self.linkedit_segment_cmd_index = @intCast(u16, self.load_commands.items.len);
48054598 var vmaddr: u64 = 0;
48064599 var fileoff: u64 = 0;
4807 if (self.needs_prealloc) {
4600 if (self.mode == .incremental) {
48084601 const base = self.getSegmentAllocBase(&.{self.data_segment_cmd_index.?});
48094602 vmaddr = base.vmaddr;
48104603 fileoff = base.fileoff;
......@@ -5028,8 +4821,6 @@ fn populateMissingMetadata(self: *MachO) !void {
50284821 });
50294822 self.load_commands_dirty = true;
50304823 }
5031
5032 self.cold_start = true;
50334824}
50344825
50354826fn calcMinHeaderpad(self: *MachO) u64 {
......@@ -5130,7 +4921,7 @@ fn allocateSegment(self: *MachO, maybe_index: ?u16, indices: []const ?u16, init_
51304921
51314922 // Allocate the sections according to their alignment at the beginning of the segment.
51324923 var start = init_size;
5133 for (seg.sections.items) |*sect, sect_id| {
4924 for (seg.sections.items) |*sect| {
51344925 const is_zerofill = sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL;
51354926 const use_llvm = build_options.have_llvm and self.base.options.use_llvm;
51364927 const use_stage1 = build_options.is_stage1 and self.base.options.use_stage1;
......@@ -5138,32 +4929,12 @@ fn allocateSegment(self: *MachO, maybe_index: ?u16, indices: []const ?u16, init_
51384929 const start_aligned = mem.alignForwardGeneric(u64, start, alignment);
51394930
51404931 // TODO handle zerofill sections in stage2
5141 sect.offset = if (is_zerofill and (use_stage1 or use_llvm)) 0 else @intCast(u32, seg.inner.fileoff + start_aligned);
4932 sect.offset = if (is_zerofill and (use_stage1 or use_llvm))
4933 0
4934 else
4935 @intCast(u32, seg.inner.fileoff + start_aligned);
51424936 sect.addr = seg.inner.vmaddr + start_aligned;
51434937
5144 // Recalculate section size given the allocated start address
5145 sect.size = if (self.atoms.get(.{
5146 .seg = index,
5147 .sect = @intCast(u16, sect_id),
5148 })) |last_atom| blk: {
5149 var atom = last_atom;
5150 while (atom.prev) |prev| {
5151 atom = prev;
5152 }
5153
5154 var base_addr = sect.addr;
5155
5156 while (true) {
5157 const atom_alignment = try math.powi(u32, 2, atom.alignment);
5158 base_addr = mem.alignForwardGeneric(u64, base_addr, atom_alignment) + atom.size;
5159 if (atom.next) |next| {
5160 atom = next;
5161 } else break;
5162 }
5163
5164 break :blk base_addr - sect.addr;
5165 } else 0;
5166
51674938 start = start_aligned + sect.size;
51684939
51694940 if (!(is_zerofill and (use_stage1 or use_llvm))) {
......@@ -5194,14 +4965,14 @@ fn initSection(
51944965 var sect = macho.section_64{
51954966 .sectname = makeStaticString(sectname),
51964967 .segname = seg.inner.segname,
5197 .size = if (self.needs_prealloc) @intCast(u32, size) else 0,
4968 .size = if (self.mode == .incremental) @intCast(u32, size) else 0,
51984969 .@"align" = alignment,
51994970 .flags = opts.flags,
52004971 .reserved1 = opts.reserved1,
52014972 .reserved2 = opts.reserved2,
52024973 };
52034974
5204 if (self.needs_prealloc) {
4975 if (self.mode == .incremental) {
52054976 const alignment_pow_2 = try math.powi(u32, 2, alignment);
52064977 const padding: ?u32 = if (segment_id == self.text_segment_cmd_index.?)
52074978 @maximum(self.base.options.headerpad_size orelse 0, default_headerpad_size)
......@@ -5419,12 +5190,30 @@ fn getSectionMaxAlignment(self: *MachO, segment_id: u16, start_sect_id: u16) !u3
54195190 return max_alignment;
54205191}
54215192
5422fn allocateAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, match: MatchingSection) !u64 {
5193fn allocateAtomCommon(self: *MachO, atom: *Atom, match: MatchingSection) !void {
5194 const sym = atom.getSymbolPtr(self);
5195 if (self.mode == .incremental) {
5196 const size = atom.size;
5197 const alignment = try math.powi(u32, 2, atom.alignment);
5198 const vaddr = try self.allocateAtom(atom, size, alignment, match);
5199 const sym_name = atom.getName(self);
5200 log.debug("allocated {s} atom at 0x{x}", .{ sym_name, vaddr });
5201 sym.n_value = vaddr;
5202 } else try self.addAtomToSection(atom, match);
5203 sym.n_sect = self.getSectionOrdinal(match);
5204}
5205
5206fn allocateAtom(
5207 self: *MachO,
5208 atom: *Atom,
5209 new_atom_size: u64,
5210 alignment: u64,
5211 match: MatchingSection,
5212) !u64 {
54235213 const tracy = trace(@src());
54245214 defer tracy.end();
54255215
5426 const seg = &self.load_commands.items[match.seg].segment;
5427 const sect = &seg.sections.items[match.sect];
5216 const sect = self.getSectionPtr(match);
54285217 var free_list = self.atom_free_lists.get(match).?;
54295218 const needs_padding = match.seg == self.text_segment_cmd_index.? and match.sect == self.text_section_index.?;
54305219 const new_atom_ideal_capacity = if (needs_padding) padToIdeal(new_atom_size) else new_atom_size;
......@@ -5445,8 +5234,8 @@ fn allocateAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, m
54455234 const big_atom = free_list.items[i];
54465235 // We now have a pointer to a live atom that has too much capacity.
54475236 // Is it enough that we could fit this new atom?
5448 const sym = self.locals.items[big_atom.local_sym_index];
5449 const capacity = big_atom.capacity(self.*);
5237 const sym = big_atom.getSymbol(self);
5238 const capacity = big_atom.capacity(self);
54505239 const ideal_capacity = if (needs_padding) padToIdeal(capacity) else capacity;
54515240 const ideal_capacity_end_vaddr = math.add(u64, sym.n_value, ideal_capacity) catch ideal_capacity;
54525241 const capacity_end_vaddr = sym.n_value + capacity;
......@@ -5456,7 +5245,7 @@ fn allocateAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, m
54565245 // Additional bookkeeping here to notice if this free list node
54575246 // should be deleted because the atom that it points to has grown to take up
54585247 // more of the extra capacity.
5459 if (!big_atom.freeListEligible(self.*)) {
5248 if (!big_atom.freeListEligible(self)) {
54605249 _ = free_list.swapRemove(i);
54615250 } else {
54625251 i += 1;
......@@ -5476,7 +5265,7 @@ fn allocateAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, m
54765265 }
54775266 break :blk new_start_vaddr;
54785267 } else if (self.atoms.get(match)) |last| {
5479 const last_symbol = self.locals.items[last.local_sym_index];
5268 const last_symbol = last.getSymbol(self);
54805269 const ideal_capacity = if (needs_padding) padToIdeal(last.size) else last.size;
54815270 const ideal_capacity_end_vaddr = last_symbol.n_value + ideal_capacity;
54825271 const new_start_vaddr = mem.alignForwardGeneric(u64, ideal_capacity_end_vaddr, alignment);
......@@ -5525,7 +5314,7 @@ fn allocateAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, m
55255314 return vaddr;
55265315}
55275316
5528fn addAtomToSection(self: *MachO, atom: *Atom, match: MatchingSection) !void {
5317pub fn addAtomToSection(self: *MachO, atom: *Atom, match: MatchingSection) !void {
55295318 if (self.atoms.getPtr(match)) |last| {
55305319 last.*.next = atom;
55315320 atom.prev = last.*;
......@@ -5533,34 +5322,42 @@ fn addAtomToSection(self: *MachO, atom: *Atom, match: MatchingSection) !void {
55335322 } else {
55345323 try self.atoms.putNoClobber(self.base.allocator, match, atom);
55355324 }
5536 const seg = &self.load_commands.items[match.seg].segment;
5537 const sect = &seg.sections.items[match.sect];
5538 sect.size += atom.size;
5325 const sect = self.getSectionPtr(match);
5326 const atom_alignment = try math.powi(u32, 2, atom.alignment);
5327 const aligned_end_addr = mem.alignForwardGeneric(u64, sect.size, atom_alignment);
5328 const padding = aligned_end_addr - sect.size;
5329 sect.size += padding + atom.size;
5330 sect.@"align" = @maximum(sect.@"align", atom.alignment);
55395331}
55405332
55415333pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 {
5542 const sym_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{name});
5543 defer self.base.allocator.free(sym_name);
5544 const n_strx = try self.makeString(sym_name);
5545
5546 if (!self.symbol_resolver.contains(n_strx)) {
5547 log.debug("adding new extern function '{s}'", .{sym_name});
5548 const sym_index = @intCast(u32, self.undefs.items.len);
5549 try self.undefs.append(self.base.allocator, .{
5550 .n_strx = n_strx,
5551 .n_type = macho.N_UNDF,
5552 .n_sect = 0,
5553 .n_desc = 0,
5554 .n_value = 0,
5555 });
5556 try self.symbol_resolver.putNoClobber(self.base.allocator, n_strx, .{
5557 .where = .undef,
5558 .where_index = sym_index,
5559 });
5560 try self.unresolved.putNoClobber(self.base.allocator, sym_index, .stub);
5334 const gpa = self.base.allocator;
5335 const sym_name = try std.fmt.allocPrint(gpa, "_{s}", .{name});
5336 const global_index = @intCast(u32, self.globals.values().len);
5337 const gop = try self.globals.getOrPut(gpa, sym_name);
5338 defer if (gop.found_existing) gpa.free(sym_name);
5339
5340 if (gop.found_existing) {
5341 // TODO audit this: can we ever reference anything from outside the Zig module?
5342 assert(gop.value_ptr.file == null);
5343 return gop.value_ptr.sym_index;
55615344 }
55625345
5563 return n_strx;
5346 const sym_index = @intCast(u32, self.locals.items.len);
5347 try self.locals.append(gpa, .{
5348 .n_strx = try self.strtab.insert(gpa, sym_name),
5349 .n_type = macho.N_UNDF,
5350 .n_sect = 0,
5351 .n_desc = 0,
5352 .n_value = 0,
5353 });
5354 gop.value_ptr.* = .{
5355 .sym_index = sym_index,
5356 .file = null,
5357 };
5358 try self.unresolved.putNoClobber(gpa, global_index, true);
5359
5360 return sym_index;
55645361}
55655362
55665363fn getSegmentAllocBase(self: MachO, indices: []const ?u16) struct { vmaddr: u64, fileoff: u64 } {
......@@ -5588,7 +5385,7 @@ fn pruneAndSortSectionsInSegment(self: *MachO, maybe_seg_id: *?u16, indices: []*
55885385
55895386 for (indices) |maybe_index| {
55905387 const old_idx = maybe_index.* orelse continue;
5591 const sect = sections[old_idx];
5388 const sect = &sections[old_idx];
55925389 if (sect.size == 0) {
55935390 log.debug("pruning section {s},{s}", .{ sect.segName(), sect.sectName() });
55945391 maybe_index.* = null;
......@@ -5596,7 +5393,7 @@ fn pruneAndSortSectionsInSegment(self: *MachO, maybe_seg_id: *?u16, indices: []*
55965393 seg.inner.nsects -= 1;
55975394 } else {
55985395 maybe_index.* = @intCast(u16, seg.sections.items.len);
5599 seg.sections.appendAssumeCapacity(sect);
5396 seg.sections.appendAssumeCapacity(sect.*);
56005397 }
56015398 try mapping.putNoClobber(old_idx, maybe_index.*);
56025399 }
......@@ -5711,7 +5508,11 @@ fn updateSectionOrdinals(self: *MachO) !void {
57115508 const tracy = trace(@src());
57125509 defer tracy.end();
57135510
5714 var ordinal_remap = std.AutoHashMap(u8, u8).init(self.base.allocator);
5511 log.debug("updating section ordinals", .{});
5512
5513 const gpa = self.base.allocator;
5514
5515 var ordinal_remap = std.AutoHashMap(u8, u8).init(gpa);
57155516 defer ordinal_remap.deinit();
57165517 var ordinals: std.AutoArrayHashMapUnmanaged(MatchingSection, void) = .{};
57175518
......@@ -5723,27 +5524,40 @@ fn updateSectionOrdinals(self: *MachO) !void {
57235524 }) |maybe_index| {
57245525 const index = maybe_index orelse continue;
57255526 const seg = self.load_commands.items[index].segment;
5726 for (seg.sections.items) |_, sect_id| {
5527 for (seg.sections.items) |sect, sect_id| {
57275528 const match = MatchingSection{
57285529 .seg = @intCast(u16, index),
57295530 .sect = @intCast(u16, sect_id),
57305531 };
5731 const old_ordinal = @intCast(u8, self.section_ordinals.getIndex(match).? + 1);
5532 const old_ordinal = self.getSectionOrdinal(match);
57325533 new_ordinal += 1;
5534 log.debug("'{s},{s}': sect({d}, '_,_') => sect({d}, '_,_')", .{
5535 sect.segName(),
5536 sect.sectName(),
5537 old_ordinal,
5538 new_ordinal,
5539 });
57335540 try ordinal_remap.putNoClobber(old_ordinal, new_ordinal);
5734 try ordinals.putNoClobber(self.base.allocator, match, {});
5541 try ordinals.putNoClobber(gpa, match, {});
57355542 }
57365543 }
57375544
5545 // FIXME Jakub
5546 // TODO no need for duping work here; simply walk the atom graph
57385547 for (self.locals.items) |*sym| {
5548 if (sym.undf()) continue;
57395549 if (sym.n_sect == 0) continue;
57405550 sym.n_sect = ordinal_remap.get(sym.n_sect).?;
57415551 }
5742 for (self.globals.items) |*sym| {
5743 sym.n_sect = ordinal_remap.get(sym.n_sect).?;
5552 for (self.objects.items) |*object| {
5553 for (object.symtab.items) |*sym| {
5554 if (sym.undf()) continue;
5555 if (sym.n_sect == 0) continue;
5556 sym.n_sect = ordinal_remap.get(sym.n_sect).?;
5557 }
57445558 }
57455559
5746 self.section_ordinals.deinit(self.base.allocator);
5560 self.section_ordinals.deinit(gpa);
57475561 self.section_ordinals = ordinals;
57485562}
57495563
......@@ -5751,11 +5565,13 @@ fn writeDyldInfoData(self: *MachO) !void {
57515565 const tracy = trace(@src());
57525566 defer tracy.end();
57535567
5754 var rebase_pointers = std.ArrayList(bind.Pointer).init(self.base.allocator);
5568 const gpa = self.base.allocator;
5569
5570 var rebase_pointers = std.ArrayList(bind.Pointer).init(gpa);
57555571 defer rebase_pointers.deinit();
5756 var bind_pointers = std.ArrayList(bind.Pointer).init(self.base.allocator);
5572 var bind_pointers = std.ArrayList(bind.Pointer).init(gpa);
57575573 defer bind_pointers.deinit();
5758 var lazy_bind_pointers = std.ArrayList(bind.Pointer).init(self.base.allocator);
5574 var lazy_bind_pointers = std.ArrayList(bind.Pointer).init(gpa);
57595575 defer lazy_bind_pointers.deinit();
57605576
57615577 {
......@@ -5768,13 +5584,17 @@ fn writeDyldInfoData(self: *MachO) !void {
57685584 if (match.seg == seg) continue; // __TEXT is non-writable
57695585 }
57705586
5771 const seg = self.load_commands.items[match.seg].segment;
5587 const seg = self.getSegment(match);
5588 const sect = self.getSection(match);
5589 log.debug("dyld info for {s},{s}", .{ sect.segName(), sect.sectName() });
57725590
57735591 while (true) {
5774 const sym = self.locals.items[atom.local_sym_index];
5592 log.debug(" ATOM(%{d}, '{s}')", .{ atom.sym_index, atom.getName(self) });
5593 const sym = atom.getSymbol(self);
57755594 const base_offset = sym.n_value - seg.inner.vmaddr;
57765595
57775596 for (atom.rebases.items) |offset| {
5597 log.debug(" | rebase at {x}", .{base_offset + offset});
57785598 try rebase_pointers.append(.{
57795599 .offset = base_offset + offset,
57805600 .segment_id = match.seg,
......@@ -5782,57 +5602,55 @@ fn writeDyldInfoData(self: *MachO) !void {
57825602 }
57835603
57845604 for (atom.bindings.items) |binding| {
5785 const resolv = self.symbol_resolver.get(binding.n_strx).?;
5786 switch (resolv.where) {
5787 .global => {
5788 // Turn into a rebase.
5789 try rebase_pointers.append(.{
5790 .offset = base_offset + binding.offset,
5791 .segment_id = match.seg,
5792 });
5793 },
5794 .undef => {
5795 const bind_sym = self.undefs.items[resolv.where_index];
5796 var flags: u4 = 0;
5797 if (bind_sym.weakRef()) {
5798 flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT);
5799 }
5800 try bind_pointers.append(.{
5801 .offset = binding.offset + base_offset,
5802 .segment_id = match.seg,
5803 .dylib_ordinal = @divTrunc(@bitCast(i16, bind_sym.n_desc), macho.N_SYMBOL_RESOLVER),
5804 .name = self.getString(bind_sym.n_strx),
5805 .bind_flags = flags,
5806 });
5807 },
5605 const bind_sym = self.getSymbol(binding.target);
5606 const bind_sym_name = self.getSymbolName(binding.target);
5607 const dylib_ordinal = @divTrunc(
5608 @bitCast(i16, bind_sym.n_desc),
5609 macho.N_SYMBOL_RESOLVER,
5610 );
5611 var flags: u4 = 0;
5612 log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{
5613 binding.offset + base_offset,
5614 bind_sym_name,
5615 dylib_ordinal,
5616 });
5617 if (bind_sym.weakRef()) {
5618 log.debug(" | marking as weak ref ", .{});
5619 flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT);
58085620 }
5621 try bind_pointers.append(.{
5622 .offset = binding.offset + base_offset,
5623 .segment_id = match.seg,
5624 .dylib_ordinal = dylib_ordinal,
5625 .name = bind_sym_name,
5626 .bind_flags = flags,
5627 });
58095628 }
58105629
58115630 for (atom.lazy_bindings.items) |binding| {
5812 const resolv = self.symbol_resolver.get(binding.n_strx).?;
5813 switch (resolv.where) {
5814 .global => {
5815 // Turn into a rebase.
5816 try rebase_pointers.append(.{
5817 .offset = base_offset + binding.offset,
5818 .segment_id = match.seg,
5819 });
5820 },
5821 .undef => {
5822 const bind_sym = self.undefs.items[resolv.where_index];
5823 var flags: u4 = 0;
5824 if (bind_sym.weakRef()) {
5825 flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT);
5826 }
5827 try lazy_bind_pointers.append(.{
5828 .offset = binding.offset + base_offset,
5829 .segment_id = match.seg,
5830 .dylib_ordinal = @divTrunc(@bitCast(i16, bind_sym.n_desc), macho.N_SYMBOL_RESOLVER),
5831 .name = self.getString(bind_sym.n_strx),
5832 .bind_flags = flags,
5833 });
5834 },
5631 const bind_sym = self.getSymbol(binding.target);
5632 const bind_sym_name = self.getSymbolName(binding.target);
5633 const dylib_ordinal = @divTrunc(
5634 @bitCast(i16, bind_sym.n_desc),
5635 macho.N_SYMBOL_RESOLVER,
5636 );
5637 var flags: u4 = 0;
5638 log.debug(" | lazy bind at {x} import('{s}') ord({d})", .{
5639 binding.offset + base_offset,
5640 bind_sym_name,
5641 dylib_ordinal,
5642 });
5643 if (bind_sym.weakRef()) {
5644 log.debug(" | marking as weak ref ", .{});
5645 flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT);
58355646 }
5647 try lazy_bind_pointers.append(.{
5648 .offset = binding.offset + base_offset,
5649 .segment_id = match.seg,
5650 .dylib_ordinal = dylib_ordinal,
5651 .name = bind_sym_name,
5652 .bind_flags = flags,
5653 });
58365654 }
58375655
58385656 if (atom.prev) |prev| {
......@@ -5843,7 +5661,7 @@ fn writeDyldInfoData(self: *MachO) !void {
58435661 }
58445662
58455663 var trie: Trie = .{};
5846 defer trie.deinit(self.base.allocator);
5664 defer trie.deinit(gpa);
58475665
58485666 {
58495667 // TODO handle macho.EXPORT_SYMBOL_FLAGS_REEXPORT and macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER.
......@@ -5852,19 +5670,40 @@ fn writeDyldInfoData(self: *MachO) !void {
58525670 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].segment;
58535671 const base_address = text_segment.inner.vmaddr;
58545672
5855 for (self.globals.items) |sym| {
5856 if (sym.n_type == 0) continue;
5857 const sym_name = self.getString(sym.n_strx);
5858 log.debug(" (putting '{s}' defined at 0x{x})", .{ sym_name, sym.n_value });
5859
5860 try trie.put(self.base.allocator, .{
5861 .name = sym_name,
5862 .vmaddr_offset = sym.n_value - base_address,
5863 .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR,
5864 });
5673 if (self.base.options.output_mode == .Exe) {
5674 for (&[_]SymbolWithLoc{
5675 try self.getEntryPoint(),
5676 self.globals.get("__mh_execute_header").?,
5677 }) |global| {
5678 const sym = self.getSymbol(global);
5679 const sym_name = self.getSymbolName(global);
5680 log.debug(" (putting '{s}' defined at 0x{x})", .{ sym_name, sym.n_value });
5681 try trie.put(gpa, .{
5682 .name = sym_name,
5683 .vmaddr_offset = sym.n_value - base_address,
5684 .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR,
5685 });
5686 }
5687 } else {
5688 assert(self.base.options.output_mode == .Lib);
5689 for (self.globals.values()) |global| {
5690 const sym = self.getSymbol(global);
5691
5692 if (sym.undf()) continue;
5693 if (!sym.ext()) continue;
5694 if (sym.n_desc == N_DESC_GCED) continue;
5695
5696 const sym_name = self.getSymbolName(global);
5697 log.debug(" (putting '{s}' defined at 0x{x})", .{ sym_name, sym.n_value });
5698 try trie.put(gpa, .{
5699 .name = sym_name,
5700 .vmaddr_offset = sym.n_value - base_address,
5701 .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR,
5702 });
5703 }
58655704 }
58665705
5867 try trie.finalize(self.base.allocator);
5706 try trie.finalize(gpa);
58685707 }
58695708
58705709 const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].segment;
......@@ -5909,8 +5748,8 @@ fn writeDyldInfoData(self: *MachO) !void {
59095748 seg.inner.filesize = dyld_info.export_off + dyld_info.export_size - seg.inner.fileoff;
59105749
59115750 const needed_size = dyld_info.export_off + dyld_info.export_size - dyld_info.rebase_off;
5912 var buffer = try self.base.allocator.alloc(u8, needed_size);
5913 defer self.base.allocator.free(buffer);
5751 var buffer = try gpa.alloc(u8, needed_size);
5752 defer gpa.free(buffer);
59145753 mem.set(u8, buffer, 0);
59155754
59165755 var stream = std.io.fixedBufferStream(buffer);
......@@ -5937,10 +5776,12 @@ fn writeDyldInfoData(self: *MachO) !void {
59375776 try self.populateLazyBindOffsetsInStubHelper(
59385777 buffer[dyld_info.lazy_bind_off - base_off ..][0..dyld_info.lazy_bind_size],
59395778 );
5779
59405780 self.load_commands_dirty = true;
59415781}
59425782
59435783fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void {
5784 const gpa = self.base.allocator;
59445785 const text_segment_cmd_index = self.text_segment_cmd_index orelse return;
59455786 const stub_helper_section_index = self.stub_helper_section_index orelse return;
59465787 const last_atom = self.atoms.get(.{
......@@ -5950,7 +5791,7 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void {
59505791 if (self.stub_helper_preamble_atom == null) return;
59515792 if (last_atom == self.stub_helper_preamble_atom.?) return;
59525793
5953 var table = std.AutoHashMap(i64, *Atom).init(self.base.allocator);
5794 var table = std.AutoHashMap(i64, *Atom).init(gpa);
59545795 defer table.deinit();
59555796
59565797 {
......@@ -5966,7 +5807,7 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void {
59665807
59675808 while (true) {
59685809 const laptr_off = blk: {
5969 const sym = self.locals.items[laptr_atom.local_sym_index];
5810 const sym = laptr_atom.getSymbol(self);
59705811 break :blk @intCast(i64, sym.n_value - base_addr);
59715812 };
59725813 try table.putNoClobber(laptr_off, stub_atom);
......@@ -5979,7 +5820,7 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void {
59795820
59805821 var stream = std.io.fixedBufferStream(buffer);
59815822 var reader = stream.reader();
5982 var offsets = std.ArrayList(struct { sym_offset: i64, offset: u32 }).init(self.base.allocator);
5823 var offsets = std.ArrayList(struct { sym_offset: i64, offset: u32 }).init(gpa);
59835824 try offsets.append(.{ .sym_offset = undefined, .offset = 0 });
59845825 defer offsets.deinit();
59855826 var valid_block = false;
......@@ -6022,10 +5863,10 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void {
60225863 }
60235864 }
60245865
6025 const sect = blk: {
6026 const seg = self.load_commands.items[text_segment_cmd_index].segment;
6027 break :blk seg.sections.items[stub_helper_section_index];
6028 };
5866 const sect = self.getSection(.{
5867 .seg = text_segment_cmd_index,
5868 .sect = stub_helper_section_index,
5869 });
60295870 const stub_offset: u4 = switch (self.base.options.target.cpu.arch) {
60305871 .x86_64 => 1,
60315872 .aarch64 => 2 * @sizeOf(u32),
......@@ -6036,79 +5877,63 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void {
60365877
60375878 while (offsets.popOrNull()) |bind_offset| {
60385879 const atom = table.get(bind_offset.sym_offset).?;
6039 const sym = self.locals.items[atom.local_sym_index];
5880 const sym = atom.getSymbol(self);
60405881 const file_offset = sect.offset + sym.n_value - sect.addr + stub_offset;
60415882 mem.writeIntLittle(u32, &buf, bind_offset.offset);
60425883 log.debug("writing lazy bind offset in stub helper of 0x{x} for symbol {s} at offset 0x{x}", .{
60435884 bind_offset.offset,
6044 self.getString(sym.n_strx),
5885 atom.getName(self),
60455886 file_offset,
60465887 });
60475888 try self.base.file.?.pwriteAll(&buf, file_offset);
60485889 }
60495890}
60505891
5892const asc_u64 = std.sort.asc(u64);
5893
60515894fn writeFunctionStarts(self: *MachO) !void {
6052 var atom = self.atoms.get(.{
6053 .seg = self.text_segment_cmd_index orelse return,
6054 .sect = self.text_section_index orelse return,
6055 }) orelse return;
5895 const text_seg_index = self.text_segment_cmd_index orelse return;
5896 const text_sect_index = self.text_section_index orelse return;
5897 const text_seg = self.load_commands.items[text_seg_index].segment;
60565898
60575899 const tracy = trace(@src());
60585900 defer tracy.end();
60595901
6060 while (atom.prev) |prev| {
6061 atom = prev;
6062 }
6063
6064 var offsets = std.ArrayList(u32).init(self.base.allocator);
6065 defer offsets.deinit();
6066
6067 const text_seg = self.load_commands.items[self.text_segment_cmd_index.?].segment;
6068 var last_off: u32 = 0;
6069
6070 while (true) {
6071 const atom_sym = self.locals.items[atom.local_sym_index];
6072
6073 if (atom_sym.n_strx != 0) blk: {
6074 if (self.symbol_resolver.get(atom_sym.n_strx)) |resolv| {
6075 assert(resolv.where == .global);
6076 if (resolv.local_sym_index != atom.local_sym_index) break :blk;
6077 }
6078
6079 const offset = @intCast(u32, atom_sym.n_value - text_seg.inner.vmaddr);
6080 const diff = offset - last_off;
5902 const gpa = self.base.allocator;
60815903
6082 if (diff == 0) break :blk;
5904 // We need to sort by address first
5905 var addresses = std.ArrayList(u64).init(gpa);
5906 defer addresses.deinit();
5907 try addresses.ensureTotalCapacityPrecise(self.globals.count());
60835908
6084 try offsets.append(diff);
6085 last_off = offset;
6086 }
5909 for (self.globals.values()) |global| {
5910 const sym = self.getSymbol(global);
5911 if (sym.undf()) continue;
5912 if (sym.n_desc == N_DESC_GCED) continue;
5913 const match = self.getMatchingSectionFromOrdinal(sym.n_sect);
5914 if (match.seg != text_seg_index or match.sect != text_sect_index) continue;
60875915
6088 for (atom.contained.items) |cont| {
6089 const cont_sym = self.locals.items[cont.local_sym_index];
5916 addresses.appendAssumeCapacity(sym.n_value);
5917 }
60905918
6091 if (cont_sym.n_strx == 0) continue;
6092 if (self.symbol_resolver.get(cont_sym.n_strx)) |resolv| {
6093 assert(resolv.where == .global);
6094 if (resolv.local_sym_index != cont.local_sym_index) continue;
6095 }
5919 std.sort.sort(u64, addresses.items, {}, asc_u64);
60965920
6097 const offset = @intCast(u32, cont_sym.n_value - text_seg.inner.vmaddr);
6098 const diff = offset - last_off;
5921 var offsets = std.ArrayList(u32).init(gpa);
5922 defer offsets.deinit();
5923 try offsets.ensureTotalCapacityPrecise(addresses.items.len);
60995924
6100 if (diff == 0) continue;
5925 var last_off: u32 = 0;
5926 for (addresses.items) |addr| {
5927 const offset = @intCast(u32, addr - text_seg.inner.vmaddr);
5928 const diff = offset - last_off;
61015929
6102 try offsets.append(diff);
6103 last_off = offset;
6104 }
5930 if (diff == 0) continue;
61055931
6106 if (atom.next) |next| {
6107 atom = next;
6108 } else break;
5932 offsets.appendAssumeCapacity(diff);
5933 last_off = offset;
61095934 }
61105935
6111 var buffer = std.ArrayList(u8).init(self.base.allocator);
5936 var buffer = std.ArrayList(u8).init(gpa);
61125937 defer buffer.deinit();
61135938
61145939 const max_size = @intCast(usize, offsets.items.len * @sizeOf(u64));
......@@ -6136,53 +5961,72 @@ fn writeFunctionStarts(self: *MachO) !void {
61365961 self.load_commands_dirty = true;
61375962}
61385963
6139fn writeDices(self: *MachO) !void {
6140 if (!self.has_dices) return;
5964fn filterDataInCode(
5965 dices: []const macho.data_in_code_entry,
5966 start_addr: u64,
5967 end_addr: u64,
5968) []const macho.data_in_code_entry {
5969 const Predicate = struct {
5970 addr: u64,
5971
5972 pub fn predicate(self: @This(), dice: macho.data_in_code_entry) bool {
5973 return dice.offset >= self.addr;
5974 }
5975 };
5976
5977 const start = MachO.findFirst(macho.data_in_code_entry, dices, 0, Predicate{ .addr = start_addr });
5978 const end = MachO.findFirst(macho.data_in_code_entry, dices, start, Predicate{ .addr = end_addr });
5979
5980 return dices[start..end];
5981}
61415982
5983fn writeDataInCode(self: *MachO) !void {
61425984 const tracy = trace(@src());
61435985 defer tracy.end();
61445986
6145 var buf = std.ArrayList(u8).init(self.base.allocator);
6146 defer buf.deinit();
5987 var out_dice = std.ArrayList(macho.data_in_code_entry).init(self.base.allocator);
5988 defer out_dice.deinit();
61475989
6148 var atom: *Atom = self.atoms.get(.{
5990 const text_sect = self.getSection(.{
61495991 .seg = self.text_segment_cmd_index orelse return,
61505992 .sect = self.text_section_index orelse return,
6151 }) orelse return;
5993 });
61525994
6153 while (atom.prev) |prev| {
6154 atom = prev;
6155 }
5995 for (self.objects.items) |object| {
5996 const dice = object.parseDataInCode() orelse continue;
5997 try out_dice.ensureUnusedCapacity(dice.len);
61565998
6157 const text_seg = self.load_commands.items[self.text_segment_cmd_index.?].segment;
6158 const text_sect = text_seg.sections.items[self.text_section_index.?];
5999 for (object.managed_atoms.items) |atom| {
6000 const sym = atom.getSymbol(self);
6001 if (sym.n_desc == N_DESC_GCED) continue;
61596002
6160 while (true) {
6161 if (atom.dices.items.len > 0) {
6162 const sym = self.locals.items[atom.local_sym_index];
6163 const base_off = math.cast(u32, sym.n_value - text_sect.addr + text_sect.offset) orelse return error.Overflow;
6164
6165 try buf.ensureUnusedCapacity(atom.dices.items.len * @sizeOf(macho.data_in_code_entry));
6166 for (atom.dices.items) |dice| {
6167 const rebased_dice = macho.data_in_code_entry{
6168 .offset = base_off + dice.offset,
6169 .length = dice.length,
6170 .kind = dice.kind,
6171 };
6172 buf.appendSliceAssumeCapacity(mem.asBytes(&rebased_dice));
6003 const match = self.getMatchingSectionFromOrdinal(sym.n_sect);
6004 if (match.seg != self.text_segment_cmd_index.? and match.sect != self.text_section_index.?) {
6005 continue;
61736006 }
6174 }
61756007
6176 if (atom.next) |next| {
6177 atom = next;
6178 } else break;
6008 const source_sym = object.getSourceSymbol(atom.sym_index) orelse continue;
6009 const source_addr = math.cast(u32, source_sym.n_value) orelse return error.Overflow;
6010 const filtered_dice = filterDataInCode(dice, source_addr, source_addr + atom.size);
6011 const base = math.cast(u32, sym.n_value - text_sect.addr + text_sect.offset) orelse
6012 return error.Overflow;
6013
6014 for (filtered_dice) |single| {
6015 const offset = single.offset - source_addr + base;
6016 out_dice.appendAssumeCapacity(.{
6017 .offset = offset,
6018 .length = single.length,
6019 .kind = single.kind,
6020 });
6021 }
6022 }
61796023 }
61806024
61816025 const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].segment;
61826026 const dice_cmd = &self.load_commands.items[self.data_in_code_cmd_index.?].linkedit_data;
61836027
61846028 const dataoff = mem.alignForwardGeneric(u64, seg.inner.fileoff + seg.inner.filesize, @alignOf(u64));
6185 const datasize = buf.items.len;
6029 const datasize = out_dice.items.len * @sizeOf(macho.data_in_code_entry);
61866030 dice_cmd.dataoff = @intCast(u32, dataoff);
61876031 dice_cmd.datasize = @intCast(u32, datasize);
61886032 seg.inner.filesize = dice_cmd.dataoff + dice_cmd.datasize - seg.inner.fileoff;
......@@ -6192,118 +6036,93 @@ fn writeDices(self: *MachO) !void {
61926036 dice_cmd.dataoff + dice_cmd.datasize,
61936037 });
61946038
6195 try self.base.file.?.pwriteAll(buf.items, dice_cmd.dataoff);
6039 try self.base.file.?.pwriteAll(mem.sliceAsBytes(out_dice.items), dice_cmd.dataoff);
61966040 self.load_commands_dirty = true;
61976041}
61986042
6199fn writeSymbolTable(self: *MachO) !void {
6043fn writeSymtab(self: *MachO) !void {
62006044 const tracy = trace(@src());
62016045 defer tracy.end();
62026046
6047 const gpa = self.base.allocator;
62036048 const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].segment;
62046049 const symtab = &self.load_commands.items[self.symtab_cmd_index.?].symtab;
62056050 const symoff = mem.alignForwardGeneric(u64, seg.inner.fileoff + seg.inner.filesize, @alignOf(macho.nlist_64));
62066051 symtab.symoff = @intCast(u32, symoff);
62076052
6208 var locals = std.ArrayList(macho.nlist_64).init(self.base.allocator);
6053 var locals = std.ArrayList(macho.nlist_64).init(gpa);
62096054 defer locals.deinit();
62106055
6211 for (self.locals.items) |sym| {
6212 if (sym.n_strx == 0) continue;
6213 if (self.symbol_resolver.get(sym.n_strx)) |_| continue;
6056 for (self.locals.items) |sym, sym_id| {
6057 if (sym.n_strx == 0) continue; // no name, skip
6058 if (sym.n_desc == N_DESC_GCED) continue; // GCed, skip
6059 const sym_loc = SymbolWithLoc{ .sym_index = @intCast(u32, sym_id), .file = null };
6060 if (self.symbolIsTemp(sym_loc)) continue; // local temp symbol, skip
6061 if (self.globals.contains(self.getSymbolName(sym_loc))) continue; // global symbol is either an export or import, skip
62146062 try locals.append(sym);
62156063 }
62166064
6217 // TODO How do we handle null global symbols in incremental context?
6218 var undefs = std.ArrayList(macho.nlist_64).init(self.base.allocator);
6219 defer undefs.deinit();
6220 var undefs_table = std.AutoHashMap(u32, u32).init(self.base.allocator);
6221 defer undefs_table.deinit();
6222 try undefs.ensureTotalCapacity(self.undefs.items.len);
6223 try undefs_table.ensureTotalCapacity(@intCast(u32, self.undefs.items.len));
6065 for (self.objects.items) |object, object_id| {
6066 for (object.symtab.items) |sym, sym_id| {
6067 if (sym.n_strx == 0) continue; // no name, skip
6068 if (sym.n_desc == N_DESC_GCED) continue; // GCed, skip
6069 const sym_loc = SymbolWithLoc{ .sym_index = @intCast(u32, sym_id), .file = @intCast(u32, object_id) };
6070 if (self.symbolIsTemp(sym_loc)) continue; // local temp symbol, skip
6071 if (self.globals.contains(self.getSymbolName(sym_loc))) continue; // global symbol is either an export or import, skip
6072 var out_sym = sym;
6073 out_sym.n_strx = try self.strtab.insert(gpa, self.getSymbolName(sym_loc));
6074 try locals.append(out_sym);
6075 }
62246076
6225 for (self.undefs.items) |sym, i| {
6226 if (sym.n_strx == 0) continue;
6227 const new_index = @intCast(u32, undefs.items.len);
6228 undefs.appendAssumeCapacity(sym);
6229 undefs_table.putAssumeCapacityNoClobber(@intCast(u32, i), new_index);
6077 if (!self.base.options.strip) {
6078 try self.generateSymbolStabs(object, &locals);
6079 }
62306080 }
62316081
6232 if (self.has_stabs) {
6233 for (self.objects.items) |object| {
6234 if (object.debug_info == null) continue;
6082 var exports = std.ArrayList(macho.nlist_64).init(gpa);
6083 defer exports.deinit();
62356084
6236 // Open scope
6237 try locals.ensureUnusedCapacity(3);
6238 locals.appendAssumeCapacity(.{
6239 .n_strx = try self.makeString(object.tu_comp_dir.?),
6240 .n_type = macho.N_SO,
6241 .n_sect = 0,
6242 .n_desc = 0,
6243 .n_value = 0,
6244 });
6245 locals.appendAssumeCapacity(.{
6246 .n_strx = try self.makeString(object.tu_name.?),
6247 .n_type = macho.N_SO,
6248 .n_sect = 0,
6249 .n_desc = 0,
6250 .n_value = 0,
6251 });
6252 locals.appendAssumeCapacity(.{
6253 .n_strx = try self.makeString(object.name),
6254 .n_type = macho.N_OSO,
6255 .n_sect = 0,
6256 .n_desc = 1,
6257 .n_value = object.mtime orelse 0,
6258 });
6085 for (self.globals.values()) |global| {
6086 const sym = self.getSymbol(global);
6087 if (sym.undf()) continue; // import, skip
6088 if (sym.n_desc == N_DESC_GCED) continue; // GCed, skip
6089 var out_sym = sym;
6090 out_sym.n_strx = try self.strtab.insert(gpa, self.getSymbolName(global));
6091 try exports.append(out_sym);
6092 }
62596093
6260 for (object.contained_atoms.items) |atom| {
6261 if (atom.stab) |stab| {
6262 const nlists = try stab.asNlists(atom.local_sym_index, self);
6263 defer self.base.allocator.free(nlists);
6264 try locals.appendSlice(nlists);
6265 } else {
6266 for (atom.contained.items) |sym_at_off| {
6267 const stab = sym_at_off.stab orelse continue;
6268 const nlists = try stab.asNlists(sym_at_off.local_sym_index, self);
6269 defer self.base.allocator.free(nlists);
6270 try locals.appendSlice(nlists);
6271 }
6272 }
6273 }
6094 var imports = std.ArrayList(macho.nlist_64).init(gpa);
6095 defer imports.deinit();
6096 var imports_table = std.AutoHashMap(SymbolWithLoc, u32).init(gpa);
6097 defer imports_table.deinit();
62746098
6275 // Close scope
6276 try locals.append(.{
6277 .n_strx = 0,
6278 .n_type = macho.N_SO,
6279 .n_sect = 0,
6280 .n_desc = 0,
6281 .n_value = 0,
6282 });
6283 }
6099 for (self.globals.values()) |global| {
6100 const sym = self.getSymbol(global);
6101 if (sym.n_strx == 0) continue; // no name, skip
6102 if (!sym.undf()) continue; // not an import, skip
6103 const new_index = @intCast(u32, imports.items.len);
6104 var out_sym = sym;
6105 out_sym.n_strx = try self.strtab.insert(gpa, self.getSymbolName(global));
6106 try imports.append(out_sym);
6107 try imports_table.putNoClobber(global, new_index);
62846108 }
62856109
62866110 const nlocals = locals.items.len;
6287 const nexports = self.globals.items.len;
6288 const nundefs = undefs.items.len;
6289
6290 const locals_off = symtab.symoff;
6291 const locals_size = nlocals * @sizeOf(macho.nlist_64);
6292 log.debug("writing local symbols from 0x{x} to 0x{x}", .{ locals_off, locals_size + locals_off });
6293 try self.base.file.?.pwriteAll(mem.sliceAsBytes(locals.items), locals_off);
6111 const nexports = exports.items.len;
6112 const nimports = imports.items.len;
6113 symtab.nsyms = @intCast(u32, nlocals + nexports + nimports);
62946114
6295 const exports_off = locals_off + locals_size;
6296 const exports_size = nexports * @sizeOf(macho.nlist_64);
6297 log.debug("writing exported symbols from 0x{x} to 0x{x}", .{ exports_off, exports_size + exports_off });
6298 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.globals.items), exports_off);
6115 var buffer = std.ArrayList(u8).init(gpa);
6116 defer buffer.deinit();
6117 try buffer.ensureTotalCapacityPrecise(symtab.nsyms * @sizeOf(macho.nlist_64));
6118 buffer.appendSliceAssumeCapacity(mem.sliceAsBytes(locals.items));
6119 buffer.appendSliceAssumeCapacity(mem.sliceAsBytes(exports.items));
6120 buffer.appendSliceAssumeCapacity(mem.sliceAsBytes(imports.items));
62996121
6300 const undefs_off = exports_off + exports_size;
6301 const undefs_size = nundefs * @sizeOf(macho.nlist_64);
6302 log.debug("writing undefined symbols from 0x{x} to 0x{x}", .{ undefs_off, undefs_size + undefs_off });
6303 try self.base.file.?.pwriteAll(mem.sliceAsBytes(undefs.items), undefs_off);
6122 log.debug("writing symtab from 0x{x} to 0x{x}", .{ symtab.symoff, symtab.symoff + buffer.items.len });
6123 try self.base.file.?.pwriteAll(buffer.items, symtab.symoff);
63046124
6305 symtab.nsyms = @intCast(u32, nlocals + nexports + nundefs);
6306 seg.inner.filesize = symtab.symoff + symtab.nsyms * @sizeOf(macho.nlist_64) - seg.inner.fileoff;
6125 seg.inner.filesize = symtab.symoff + buffer.items.len - seg.inner.fileoff;
63076126
63086127 // Update dynamic symbol table.
63096128 const dysymtab = &self.load_commands.items[self.dysymtab_cmd_index.?].dysymtab;
......@@ -6311,7 +6130,7 @@ fn writeSymbolTable(self: *MachO) !void {
63116130 dysymtab.iextdefsym = dysymtab.nlocalsym;
63126131 dysymtab.nextdefsym = @intCast(u32, nexports);
63136132 dysymtab.iundefsym = dysymtab.nlocalsym + dysymtab.nextdefsym;
6314 dysymtab.nundefsym = @intCast(u32, nundefs);
6133 dysymtab.nundefsym = @intCast(u32, nimports);
63156134
63166135 const nstubs = @intCast(u32, self.stubs_table.count());
63176136 const ngot_entries = @intCast(u32, self.got_entries_table.count());
......@@ -6327,55 +6146,62 @@ fn writeSymbolTable(self: *MachO) !void {
63276146 dysymtab.indirectsymoff + dysymtab.nindirectsyms * @sizeOf(u32),
63286147 });
63296148
6330 var buf = std.ArrayList(u8).init(self.base.allocator);
6149 var buf = std.ArrayList(u8).init(gpa);
63316150 defer buf.deinit();
63326151 try buf.ensureTotalCapacity(dysymtab.nindirectsyms * @sizeOf(u32));
63336152 const writer = buf.writer();
63346153
63356154 if (self.text_segment_cmd_index) |text_segment_cmd_index| blk: {
63366155 const stubs_section_index = self.stubs_section_index orelse break :blk;
6337 const text_segment = &self.load_commands.items[text_segment_cmd_index].segment;
6338 const stubs = &text_segment.sections.items[stubs_section_index];
6156 const stubs = self.getSectionPtr(.{
6157 .seg = text_segment_cmd_index,
6158 .sect = stubs_section_index,
6159 });
63396160 stubs.reserved1 = 0;
6340 for (self.stubs_table.keys()) |key| {
6341 const resolv = self.symbol_resolver.get(key).?;
6342 switch (resolv.where) {
6343 .global => try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL),
6344 .undef => try writer.writeIntLittle(u32, dysymtab.iundefsym + undefs_table.get(resolv.where_index).?),
6345 }
6161 for (self.stubs.items) |entry| {
6162 if (entry.sym_index == 0) continue;
6163 const atom_sym = entry.getSymbol(self);
6164 if (atom_sym.n_desc == N_DESC_GCED) continue;
6165 const target_sym = self.getSymbol(entry.target);
6166 assert(target_sym.undf());
6167 try writer.writeIntLittle(u32, dysymtab.iundefsym + imports_table.get(entry.target).?);
63466168 }
63476169 }
63486170
63496171 if (self.data_const_segment_cmd_index) |data_const_segment_cmd_index| blk: {
63506172 const got_section_index = self.got_section_index orelse break :blk;
6351 const data_const_segment = &self.load_commands.items[data_const_segment_cmd_index].segment;
6352 const got = &data_const_segment.sections.items[got_section_index];
6173 const got = self.getSectionPtr(.{
6174 .seg = data_const_segment_cmd_index,
6175 .sect = got_section_index,
6176 });
63536177 got.reserved1 = nstubs;
6354 for (self.got_entries_table.keys()) |key| {
6355 switch (key) {
6356 .local => try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL),
6357 .global => |n_strx| {
6358 const resolv = self.symbol_resolver.get(n_strx).?;
6359 switch (resolv.where) {
6360 .global => try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL),
6361 .undef => try writer.writeIntLittle(u32, dysymtab.iundefsym + undefs_table.get(resolv.where_index).?),
6362 }
6363 },
6178 for (self.got_entries.items) |entry| {
6179 if (entry.sym_index == 0) continue;
6180 const atom_sym = entry.getSymbol(self);
6181 if (atom_sym.n_desc == N_DESC_GCED) continue;
6182 const target_sym = self.getSymbol(entry.target);
6183 if (target_sym.undf()) {
6184 try writer.writeIntLittle(u32, dysymtab.iundefsym + imports_table.get(entry.target).?);
6185 } else {
6186 try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL);
63646187 }
63656188 }
63666189 }
63676190
63686191 if (self.data_segment_cmd_index) |data_segment_cmd_index| blk: {
63696192 const la_symbol_ptr_section_index = self.la_symbol_ptr_section_index orelse break :blk;
6370 const data_segment = &self.load_commands.items[data_segment_cmd_index].segment;
6371 const la_symbol_ptr = &data_segment.sections.items[la_symbol_ptr_section_index];
6193 const la_symbol_ptr = self.getSectionPtr(.{
6194 .seg = data_segment_cmd_index,
6195 .sect = la_symbol_ptr_section_index,
6196 });
63726197 la_symbol_ptr.reserved1 = nstubs + ngot_entries;
6373 for (self.stubs_table.keys()) |key| {
6374 const resolv = self.symbol_resolver.get(key).?;
6375 switch (resolv.where) {
6376 .global => try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL),
6377 .undef => try writer.writeIntLittle(u32, dysymtab.iundefsym + undefs_table.get(resolv.where_index).?),
6378 }
6198 for (self.stubs.items) |entry| {
6199 if (entry.sym_index == 0) continue;
6200 const atom_sym = entry.getSymbol(self);
6201 if (atom_sym.n_desc == N_DESC_GCED) continue;
6202 const target_sym = self.getSymbol(entry.target);
6203 assert(target_sym.undf());
6204 try writer.writeIntLittle(u32, dysymtab.iundefsym + imports_table.get(entry.target).?);
63796205 }
63806206 }
63816207
......@@ -6385,21 +6211,22 @@ fn writeSymbolTable(self: *MachO) !void {
63856211 self.load_commands_dirty = true;
63866212}
63876213
6388fn writeStringTable(self: *MachO) !void {
6214fn writeStrtab(self: *MachO) !void {
63896215 const tracy = trace(@src());
63906216 defer tracy.end();
63916217
63926218 const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].segment;
63936219 const symtab = &self.load_commands.items[self.symtab_cmd_index.?].symtab;
63946220 const stroff = mem.alignForwardGeneric(u64, seg.inner.fileoff + seg.inner.filesize, @alignOf(u64));
6395 const strsize = self.strtab.items.len;
6221
6222 const strsize = self.strtab.buffer.items.len;
63966223 symtab.stroff = @intCast(u32, stroff);
63976224 symtab.strsize = @intCast(u32, strsize);
63986225 seg.inner.filesize = symtab.stroff + symtab.strsize - seg.inner.fileoff;
63996226
64006227 log.debug("writing string table from 0x{x} to 0x{x}", .{ symtab.stroff, symtab.stroff + symtab.strsize });
64016228
6402 try self.base.file.?.pwriteAll(self.strtab.items, symtab.stroff);
6229 try self.base.file.?.pwriteAll(self.strtab.buffer.items, symtab.stroff);
64036230
64046231 self.load_commands_dirty = true;
64056232}
......@@ -6413,9 +6240,9 @@ fn writeLinkeditSegment(self: *MachO) !void {
64136240
64146241 try self.writeDyldInfoData();
64156242 try self.writeFunctionStarts();
6416 try self.writeDices();
6417 try self.writeSymbolTable();
6418 try self.writeStringTable();
6243 try self.writeDataInCode();
6244 try self.writeSymtab();
6245 try self.writeStrtab();
64196246
64206247 seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size);
64216248}
......@@ -6557,43 +6384,114 @@ pub fn makeStaticString(bytes: []const u8) [16]u8 {
65576384 return buf;
65586385}
65596386
6560pub fn makeString(self: *MachO, string: []const u8) !u32 {
6561 const gop = try self.strtab_dir.getOrPutContextAdapted(self.base.allocator, @as([]const u8, string), StringIndexAdapter{
6562 .bytes = &self.strtab,
6563 }, StringIndexContext{
6564 .bytes = &self.strtab,
6565 });
6566 if (gop.found_existing) {
6567 const off = gop.key_ptr.*;
6568 log.debug("reusing string '{s}' at offset 0x{x}", .{ string, off });
6569 return off;
6570 }
6571
6572 try self.strtab.ensureUnusedCapacity(self.base.allocator, string.len + 1);
6573 const new_off = @intCast(u32, self.strtab.items.len);
6387pub fn getSectionOrdinal(self: *MachO, match: MatchingSection) u8 {
6388 return @intCast(u8, self.section_ordinals.getIndex(match).?) + 1;
6389}
65746390
6575 log.debug("writing new string '{s}' at offset 0x{x}", .{ string, new_off });
6391pub fn getMatchingSectionFromOrdinal(self: *MachO, ord: u8) MatchingSection {
6392 const index = ord - 1;
6393 assert(index < self.section_ordinals.count());
6394 return self.section_ordinals.keys()[index];
6395}
65766396
6577 self.strtab.appendSliceAssumeCapacity(string);
6578 self.strtab.appendAssumeCapacity(0);
6397pub fn getSegmentPtr(self: *MachO, match: MatchingSection) *macho.SegmentCommand {
6398 assert(match.seg < self.load_commands.items.len);
6399 return &self.load_commands.items[match.seg].segment;
6400}
65796401
6580 gop.key_ptr.* = new_off;
6402pub fn getSegment(self: *MachO, match: MatchingSection) macho.SegmentCommand {
6403 return self.getSegmentPtr(match).*;
6404}
65816405
6582 return new_off;
6406pub fn getSectionPtr(self: *MachO, match: MatchingSection) *macho.section_64 {
6407 const seg = self.getSegmentPtr(match);
6408 assert(match.sect < seg.sections.items.len);
6409 return &seg.sections.items[match.sect];
65836410}
65846411
6585pub fn getString(self: MachO, off: u32) []const u8 {
6586 assert(off < self.strtab.items.len);
6587 return mem.sliceTo(@ptrCast([*:0]const u8, self.strtab.items.ptr + off), 0);
6412pub fn getSection(self: *MachO, match: MatchingSection) macho.section_64 {
6413 return self.getSectionPtr(match).*;
65886414}
65896415
6590pub fn symbolIsTemp(sym: macho.nlist_64, sym_name: []const u8) bool {
6416pub fn symbolIsTemp(self: *MachO, sym_with_loc: SymbolWithLoc) bool {
6417 const sym = self.getSymbol(sym_with_loc);
65916418 if (!sym.sect()) return false;
65926419 if (sym.ext()) return false;
6420 const sym_name = self.getSymbolName(sym_with_loc);
65936421 return mem.startsWith(u8, sym_name, "l") or mem.startsWith(u8, sym_name, "L");
65946422}
65956423
6596pub fn findFirst(comptime T: type, haystack: []T, start: usize, predicate: anytype) usize {
6424/// Returns pointer-to-symbol described by `sym_with_loc` descriptor.
6425pub fn getSymbolPtr(self: *MachO, sym_with_loc: SymbolWithLoc) *macho.nlist_64 {
6426 if (sym_with_loc.file) |file| {
6427 const object = &self.objects.items[file];
6428 return &object.symtab.items[sym_with_loc.sym_index];
6429 } else {
6430 return &self.locals.items[sym_with_loc.sym_index];
6431 }
6432}
6433
6434/// Returns symbol described by `sym_with_loc` descriptor.
6435pub fn getSymbol(self: *MachO, sym_with_loc: SymbolWithLoc) macho.nlist_64 {
6436 return self.getSymbolPtr(sym_with_loc).*;
6437}
6438
6439/// Returns name of the symbol described by `sym_with_loc` descriptor.
6440pub fn getSymbolName(self: *MachO, sym_with_loc: SymbolWithLoc) []const u8 {
6441 if (sym_with_loc.file) |file| {
6442 const object = self.objects.items[file];
6443 const sym = object.symtab.items[sym_with_loc.sym_index];
6444 return object.getString(sym.n_strx);
6445 } else {
6446 const sym = self.locals.items[sym_with_loc.sym_index];
6447 return self.strtab.get(sym.n_strx).?;
6448 }
6449}
6450
6451/// Returns atom if there is an atom referenced by the symbol described by `sym_with_loc` descriptor.
6452/// Returns null on failure.
6453pub fn getAtomForSymbol(self: *MachO, sym_with_loc: SymbolWithLoc) ?*Atom {
6454 if (sym_with_loc.file) |file| {
6455 const object = self.objects.items[file];
6456 return object.getAtomForSymbol(sym_with_loc.sym_index);
6457 } else {
6458 return self.atom_by_index_table.get(sym_with_loc.sym_index);
6459 }
6460}
6461
6462/// Returns GOT atom that references `sym_with_loc` if one exists.
6463/// Returns null otherwise.
6464pub fn getGotAtomForSymbol(self: *MachO, sym_with_loc: SymbolWithLoc) ?*Atom {
6465 const got_index = self.got_entries_table.get(sym_with_loc) orelse return null;
6466 return self.got_entries.items[got_index].getAtom(self);
6467}
6468
6469/// Returns stubs atom that references `sym_with_loc` if one exists.
6470/// Returns null otherwise.
6471pub fn getStubsAtomForSymbol(self: *MachO, sym_with_loc: SymbolWithLoc) ?*Atom {
6472 const stubs_index = self.stubs_table.get(sym_with_loc) orelse return null;
6473 return self.stubs.items[stubs_index].getAtom(self);
6474}
6475
6476/// Returns TLV pointer atom that references `sym_with_loc` if one exists.
6477/// Returns null otherwise.
6478pub fn getTlvPtrAtomForSymbol(self: *MachO, sym_with_loc: SymbolWithLoc) ?*Atom {
6479 const tlv_ptr_index = self.tlv_ptr_entries_table.get(sym_with_loc) orelse return null;
6480 return self.tlv_ptr_entries.items[tlv_ptr_index].getAtom(self);
6481}
6482
6483/// Returns symbol location corresponding to the set entrypoint.
6484/// Asserts output mode is executable.
6485pub fn getEntryPoint(self: MachO) error{MissingMainEntrypoint}!SymbolWithLoc {
6486 const entry_name = self.base.options.entry orelse "_main";
6487 const global = self.globals.get(entry_name) orelse {
6488 log.err("entrypoint '{s}' not found", .{entry_name});
6489 return error.MissingMainEntrypoint;
6490 };
6491 return global;
6492}
6493
6494pub fn findFirst(comptime T: type, haystack: []const T, start: usize, predicate: anytype) usize {
65976495 if (!@hasDecl(@TypeOf(predicate), "predicate"))
65986496 @compileError("Predicate is required to define fn predicate(@This(), T) bool");
65996497
......@@ -6606,6 +6504,225 @@ pub fn findFirst(comptime T: type, haystack: []T, start: usize, predicate: anyty
66066504 return i;
66076505}
66086506
6507const DebugInfo = struct {
6508 inner: dwarf.DwarfInfo,
6509 debug_info: []const u8,
6510 debug_abbrev: []const u8,
6511 debug_str: []const u8,
6512 debug_line: []const u8,
6513 debug_line_str: []const u8,
6514 debug_ranges: []const u8,
6515
6516 pub fn parse(allocator: Allocator, object: Object) !?DebugInfo {
6517 var debug_info = blk: {
6518 const index = object.dwarf_debug_info_index orelse return null;
6519 break :blk try object.getSectionContents(index);
6520 };
6521 var debug_abbrev = blk: {
6522 const index = object.dwarf_debug_abbrev_index orelse return null;
6523 break :blk try object.getSectionContents(index);
6524 };
6525 var debug_str = blk: {
6526 const index = object.dwarf_debug_str_index orelse return null;
6527 break :blk try object.getSectionContents(index);
6528 };
6529 var debug_line = blk: {
6530 const index = object.dwarf_debug_line_index orelse return null;
6531 break :blk try object.getSectionContents(index);
6532 };
6533 var debug_line_str = blk: {
6534 if (object.dwarf_debug_line_str_index) |ind| {
6535 break :blk try object.getSectionContents(ind);
6536 }
6537 break :blk &[0]u8{};
6538 };
6539 var debug_ranges = blk: {
6540 if (object.dwarf_debug_ranges_index) |ind| {
6541 break :blk try object.getSectionContents(ind);
6542 }
6543 break :blk &[0]u8{};
6544 };
6545
6546 var inner: dwarf.DwarfInfo = .{
6547 .endian = .Little,
6548 .debug_info = debug_info,
6549 .debug_abbrev = debug_abbrev,
6550 .debug_str = debug_str,
6551 .debug_line = debug_line,
6552 .debug_line_str = debug_line_str,
6553 .debug_ranges = debug_ranges,
6554 };
6555 try dwarf.openDwarfDebugInfo(&inner, allocator);
6556
6557 return DebugInfo{
6558 .inner = inner,
6559 .debug_info = debug_info,
6560 .debug_abbrev = debug_abbrev,
6561 .debug_str = debug_str,
6562 .debug_line = debug_line,
6563 .debug_line_str = debug_line_str,
6564 .debug_ranges = debug_ranges,
6565 };
6566 }
6567
6568 pub fn deinit(self: *DebugInfo, allocator: Allocator) void {
6569 self.inner.deinit(allocator);
6570 }
6571};
6572
6573pub fn generateSymbolStabs(
6574 self: *MachO,
6575 object: Object,
6576 locals: *std.ArrayList(macho.nlist_64),
6577) !void {
6578 assert(!self.base.options.strip);
6579
6580 const gpa = self.base.allocator;
6581
6582 log.debug("parsing debug info in '{s}'", .{object.name});
6583
6584 var debug_info = (try DebugInfo.parse(gpa, object)) orelse return;
6585
6586 // We assume there is only one CU.
6587 const compile_unit = debug_info.inner.findCompileUnit(0x0) catch |err| switch (err) {
6588 error.MissingDebugInfo => {
6589 // TODO audit cases with missing debug info and audit our dwarf.zig module.
6590 log.debug("invalid or missing debug info in {s}; skipping", .{object.name});
6591 return;
6592 },
6593 else => |e| return e,
6594 };
6595 const tu_name = try compile_unit.die.getAttrString(&debug_info.inner, dwarf.AT.name);
6596 const tu_comp_dir = try compile_unit.die.getAttrString(&debug_info.inner, dwarf.AT.comp_dir);
6597
6598 // Open scope
6599 try locals.ensureUnusedCapacity(3);
6600 locals.appendAssumeCapacity(.{
6601 .n_strx = try self.strtab.insert(gpa, tu_comp_dir),
6602 .n_type = macho.N_SO,
6603 .n_sect = 0,
6604 .n_desc = 0,
6605 .n_value = 0,
6606 });
6607 locals.appendAssumeCapacity(.{
6608 .n_strx = try self.strtab.insert(gpa, tu_name),
6609 .n_type = macho.N_SO,
6610 .n_sect = 0,
6611 .n_desc = 0,
6612 .n_value = 0,
6613 });
6614 locals.appendAssumeCapacity(.{
6615 .n_strx = try self.strtab.insert(gpa, object.name),
6616 .n_type = macho.N_OSO,
6617 .n_sect = 0,
6618 .n_desc = 1,
6619 .n_value = object.mtime,
6620 });
6621
6622 var stabs_buf: [4]macho.nlist_64 = undefined;
6623
6624 for (object.managed_atoms.items) |atom| {
6625 const stabs = try self.generateSymbolStabsForSymbol(
6626 atom.getSymbolWithLoc(),
6627 debug_info,
6628 &stabs_buf,
6629 );
6630 try locals.appendSlice(stabs);
6631
6632 for (atom.contained.items) |sym_at_off| {
6633 const sym_loc = SymbolWithLoc{
6634 .sym_index = sym_at_off.sym_index,
6635 .file = atom.file,
6636 };
6637 const contained_stabs = try self.generateSymbolStabsForSymbol(
6638 sym_loc,
6639 debug_info,
6640 &stabs_buf,
6641 );
6642 try locals.appendSlice(contained_stabs);
6643 }
6644 }
6645
6646 // Close scope
6647 try locals.append(.{
6648 .n_strx = 0,
6649 .n_type = macho.N_SO,
6650 .n_sect = 0,
6651 .n_desc = 0,
6652 .n_value = 0,
6653 });
6654}
6655
6656fn generateSymbolStabsForSymbol(
6657 self: *MachO,
6658 sym_loc: SymbolWithLoc,
6659 debug_info: DebugInfo,
6660 buf: *[4]macho.nlist_64,
6661) ![]const macho.nlist_64 {
6662 const gpa = self.base.allocator;
6663 const object = self.objects.items[sym_loc.file.?];
6664 const sym = self.getSymbol(sym_loc);
6665 const sym_name = self.getSymbolName(sym_loc);
6666
6667 if (sym.n_strx == 0) return buf[0..0];
6668 if (sym.n_desc == N_DESC_GCED) return buf[0..0];
6669 if (self.symbolIsTemp(sym_loc)) return buf[0..0];
6670
6671 const source_sym = object.getSourceSymbol(sym_loc.sym_index) orelse return buf[0..0];
6672 const size: ?u64 = size: {
6673 if (source_sym.tentative()) break :size null;
6674 for (debug_info.inner.func_list.items) |func| {
6675 if (func.pc_range) |range| {
6676 if (source_sym.n_value >= range.start and source_sym.n_value < range.end) {
6677 break :size range.end - range.start;
6678 }
6679 }
6680 }
6681 break :size null;
6682 };
6683
6684 if (size) |ss| {
6685 buf[0] = .{
6686 .n_strx = 0,
6687 .n_type = macho.N_BNSYM,
6688 .n_sect = sym.n_sect,
6689 .n_desc = 0,
6690 .n_value = sym.n_value,
6691 };
6692 buf[1] = .{
6693 .n_strx = try self.strtab.insert(gpa, sym_name),
6694 .n_type = macho.N_FUN,
6695 .n_sect = sym.n_sect,
6696 .n_desc = 0,
6697 .n_value = sym.n_value,
6698 };
6699 buf[2] = .{
6700 .n_strx = 0,
6701 .n_type = macho.N_FUN,
6702 .n_sect = 0,
6703 .n_desc = 0,
6704 .n_value = ss,
6705 };
6706 buf[3] = .{
6707 .n_strx = 0,
6708 .n_type = macho.N_ENSYM,
6709 .n_sect = sym.n_sect,
6710 .n_desc = 0,
6711 .n_value = ss,
6712 };
6713 return buf;
6714 } else {
6715 buf[0] = .{
6716 .n_strx = try self.strtab.insert(gpa, sym_name),
6717 .n_type = macho.N_STSYM,
6718 .n_sect = sym.n_sect,
6719 .n_desc = 0,
6720 .n_value = sym.n_value,
6721 };
6722 return buf[0..1];
6723 }
6724}
6725
66096726fn snapshotState(self: *MachO) !void {
66106727 const emit = self.base.options.emit orelse {
66116728 log.debug("no emit directory found; skipping snapshot...", .{});
......@@ -6655,7 +6772,7 @@ fn snapshotState(self: *MachO) !void {
66556772 const arena = arena_allocator.allocator();
66566773
66576774 const out_file = try emit.directory.handle.createFile("snapshots.json", .{
6658 .truncate = self.cold_start,
6775 .truncate = false,
66596776 .read = true,
66606777 });
66616778 defer out_file.close();
......@@ -6675,8 +6792,7 @@ fn snapshotState(self: *MachO) !void {
66756792 var nodes = std.ArrayList(Snapshot.Node).init(arena);
66766793
66776794 for (self.section_ordinals.keys()) |key| {
6678 const seg = self.load_commands.items[key.seg].segment;
6679 const sect = seg.sections.items[key.sect];
6795 const sect = self.getSection(key);
66806796 const sect_name = try std.fmt.allocPrint(arena, "{s},{s}", .{ sect.segName(), sect.sectName() });
66816797 try nodes.append(.{
66826798 .address = sect.addr,
......@@ -6684,6 +6800,8 @@ fn snapshotState(self: *MachO) !void {
66846800 .payload = .{ .name = sect_name },
66856801 });
66866802
6803 const is_tlv = sect.type_() == macho.S_THREAD_LOCAL_VARIABLES;
6804
66876805 var atom: *Atom = self.atoms.get(key) orelse {
66886806 try nodes.append(.{
66896807 .address = sect.addr + sect.size,
......@@ -6698,103 +6816,63 @@ fn snapshotState(self: *MachO) !void {
66986816 }
66996817
67006818 while (true) {
6701 const atom_sym = self.locals.items[atom.local_sym_index];
6702 const should_skip_atom: bool = blk: {
6703 if (self.mh_execute_header_index) |index| {
6704 if (index == atom.local_sym_index) break :blk true;
6705 }
6706 if (mem.eql(u8, self.getString(atom_sym.n_strx), "___dso_handle")) break :blk true;
6707 break :blk false;
6708 };
6709
6710 if (should_skip_atom) {
6711 if (atom.next) |next| {
6712 atom = next;
6713 } else break;
6714 continue;
6715 }
6716
6819 const atom_sym = atom.getSymbol(self);
67176820 var node = Snapshot.Node{
67186821 .address = atom_sym.n_value,
67196822 .tag = .atom_start,
67206823 .payload = .{
6721 .name = self.getString(atom_sym.n_strx),
6722 .is_global = self.symbol_resolver.contains(atom_sym.n_strx),
6824 .name = atom.getName(self),
6825 .is_global = self.globals.contains(atom.getName(self)),
67236826 },
67246827 };
67256828
67266829 var aliases = std.ArrayList([]const u8).init(arena);
6727 for (atom.aliases.items) |loc| {
6728 try aliases.append(self.getString(self.locals.items[loc].n_strx));
6830 for (atom.contained.items) |sym_off| {
6831 if (sym_off.offset == 0) {
6832 try aliases.append(self.getSymbolName(.{
6833 .sym_index = sym_off.sym_index,
6834 .file = atom.file,
6835 }));
6836 }
67296837 }
67306838 node.payload.aliases = aliases.toOwnedSlice();
67316839 try nodes.append(node);
67326840
67336841 var relocs = try std.ArrayList(Snapshot.Node).initCapacity(arena, atom.relocs.items.len);
67346842 for (atom.relocs.items) |rel| {
6735 const arch = self.base.options.target.cpu.arch;
67366843 const source_addr = blk: {
6737 const sym = self.locals.items[atom.local_sym_index];
6738 break :blk sym.n_value + rel.offset;
6844 const source_sym = atom.getSymbol(self);
6845 break :blk source_sym.n_value + rel.offset;
67396846 };
67406847 const target_addr = blk: {
6741 const is_via_got = got: {
6742 switch (arch) {
6743 .aarch64 => break :got switch (@intToEnum(macho.reloc_type_arm64, rel.@"type")) {
6744 .ARM64_RELOC_GOT_LOAD_PAGE21, .ARM64_RELOC_GOT_LOAD_PAGEOFF12 => true,
6745 else => false,
6746 },
6747 .x86_64 => break :got switch (@intToEnum(macho.reloc_type_x86_64, rel.@"type")) {
6748 .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => true,
6749 else => false,
6750 },
6751 else => unreachable,
6848 const target_atom = rel.getTargetAtom(self) orelse {
6849 // If there is no atom for target, we still need to check for special, atom-less
6850 // symbols such as `___dso_handle`.
6851 const target_name = self.getSymbolName(rel.target);
6852 if (self.globals.contains(target_name)) {
6853 const atomless_sym = self.getSymbol(rel.target);
6854 break :blk atomless_sym.n_value;
67526855 }
6856 break :blk 0;
67536857 };
6754
6755 if (is_via_got) {
6756 const got_index = self.got_entries_table.get(rel.target) orelse break :blk 0;
6757 const got_atom = self.got_entries.items[got_index].atom;
6758 break :blk self.locals.items[got_atom.local_sym_index].n_value;
6759 }
6760
6761 switch (rel.target) {
6762 .local => |sym_index| {
6763 const sym = self.locals.items[sym_index];
6764 const is_tlv = is_tlv: {
6765 const source_sym = self.locals.items[atom.local_sym_index];
6766 const match = self.section_ordinals.keys()[source_sym.n_sect - 1];
6767 const match_seg = self.load_commands.items[match.seg].segment;
6768 const match_sect = match_seg.sections.items[match.sect];
6769 break :is_tlv match_sect.type_() == macho.S_THREAD_LOCAL_VARIABLES;
6770 };
6771 if (is_tlv) {
6772 const match_seg = self.load_commands.items[self.data_segment_cmd_index.?].segment;
6773 const base_address = inner: {
6774 if (self.tlv_data_section_index) |i| {
6775 break :inner match_seg.sections.items[i].addr;
6776 } else if (self.tlv_bss_section_index) |i| {
6777 break :inner match_seg.sections.items[i].addr;
6778 } else unreachable;
6779 };
6780 break :blk sym.n_value - base_address;
6781 }
6782 break :blk sym.n_value;
6783 },
6784 .global => |n_strx| {
6785 const resolv = self.symbol_resolver.get(n_strx).?;
6786 switch (resolv.where) {
6787 .global => break :blk self.globals.items[resolv.where_index].n_value,
6788 .undef => {
6789 if (self.stubs_table.get(n_strx)) |stub_index| {
6790 const stub_atom = self.stubs.items[stub_index];
6791 break :blk self.locals.items[stub_atom.local_sym_index].n_value;
6792 }
6793 break :blk 0;
6794 },
6795 }
6796 },
6797 }
6858 const target_sym = if (target_atom.isSymbolContained(rel.target, self))
6859 self.getSymbol(rel.target)
6860 else
6861 target_atom.getSymbol(self);
6862 const base_address: u64 = if (is_tlv) base_address: {
6863 const sect_id: u16 = sect_id: {
6864 if (self.tlv_data_section_index) |i| {
6865 break :sect_id i;
6866 } else if (self.tlv_bss_section_index) |i| {
6867 break :sect_id i;
6868 } else unreachable;
6869 };
6870 break :base_address self.getSection(.{
6871 .seg = self.data_segment_cmd_index.?,
6872 .sect = sect_id,
6873 }).addr;
6874 } else 0;
6875 break :blk target_sym.n_value - base_address;
67986876 };
67996877
68006878 relocs.appendAssumeCapacity(.{
......@@ -6815,15 +6893,18 @@ fn snapshotState(self: *MachO) !void {
68156893 var next_i: usize = 0;
68166894 var last_rel: usize = 0;
68176895 while (next_i < atom.contained.items.len) : (next_i += 1) {
6818 const loc = atom.contained.items[next_i];
6819 const cont_sym = self.locals.items[loc.local_sym_index];
6820 const cont_sym_name = self.getString(cont_sym.n_strx);
6896 const loc = SymbolWithLoc{
6897 .sym_index = atom.contained.items[next_i].sym_index,
6898 .file = atom.file,
6899 };
6900 const cont_sym = self.getSymbol(loc);
6901 const cont_sym_name = self.getSymbolName(loc);
68216902 var contained_node = Snapshot.Node{
68226903 .address = cont_sym.n_value,
68236904 .tag = .atom_start,
68246905 .payload = .{
68256906 .name = cont_sym_name,
6826 .is_global = self.symbol_resolver.contains(cont_sym.n_strx),
6907 .is_global = self.globals.contains(cont_sym_name),
68276908 },
68286909 };
68296910
......@@ -6831,10 +6912,14 @@ fn snapshotState(self: *MachO) !void {
68316912 var inner_aliases = std.ArrayList([]const u8).init(arena);
68326913 while (true) {
68336914 if (next_i + 1 >= atom.contained.items.len) break;
6834 const next_sym = self.locals.items[atom.contained.items[next_i + 1].local_sym_index];
6915 const next_sym_loc = SymbolWithLoc{
6916 .sym_index = atom.contained.items[next_i + 1].sym_index,
6917 .file = atom.file,
6918 };
6919 const next_sym = self.getSymbol(next_sym_loc);
68356920 if (next_sym.n_value != cont_sym.n_value) break;
6836 const next_sym_name = self.getString(next_sym.n_strx);
6837 if (self.symbol_resolver.contains(next_sym.n_strx)) {
6921 const next_sym_name = self.getSymbolName(next_sym_loc);
6922 if (self.globals.contains(next_sym_name)) {
68386923 try inner_aliases.append(contained_node.payload.name);
68396924 contained_node.payload.name = next_sym_name;
68406925 contained_node.payload.is_global = true;
......@@ -6843,7 +6928,10 @@ fn snapshotState(self: *MachO) !void {
68436928 }
68446929
68456930 const cont_size = if (next_i + 1 < atom.contained.items.len)
6846 self.locals.items[atom.contained.items[next_i + 1].local_sym_index].n_value - cont_sym.n_value
6931 self.getSymbol(.{
6932 .sym_index = atom.contained.items[next_i + 1].sym_index,
6933 .file = atom.file,
6934 }).n_value - cont_sym.n_value
68476935 else
68486936 atom_sym.n_value + atom.size - cont_sym.n_value;
68496937
......@@ -6890,69 +6978,181 @@ fn snapshotState(self: *MachO) !void {
68906978 try writer.writeByte(']');
68916979}
68926980
6893fn logSymtab(self: MachO) void {
6894 log.debug("locals:", .{});
6895 for (self.locals.items) |sym, id| {
6896 log.debug(" {d}: {s}: @{x} in {d}", .{ id, self.getString(sym.n_strx), sym.n_value, sym.n_sect });
6981fn logSymAttributes(sym: macho.nlist_64, buf: *[9]u8) []const u8 {
6982 mem.set(u8, buf[0..4], '_');
6983 mem.set(u8, buf[4..], ' ');
6984 if (sym.sect()) {
6985 buf[0] = 's';
68976986 }
6898
6899 log.debug("globals:", .{});
6900 for (self.globals.items) |sym, id| {
6901 log.debug(" {d}: {s}: @{x} in {d}", .{ id, self.getString(sym.n_strx), sym.n_value, sym.n_sect });
6987 if (sym.ext()) {
6988 if (sym.weakDef() or sym.pext()) {
6989 buf[1] = 'w';
6990 } else {
6991 buf[1] = 'e';
6992 }
69026993 }
6903
6904 log.debug("undefs:", .{});
6905 for (self.undefs.items) |sym, id| {
6906 log.debug(" {d}: {s}: in {d}", .{ id, self.getString(sym.n_strx), sym.n_desc });
6994 if (sym.tentative()) {
6995 buf[2] = 't';
6996 }
6997 if (sym.undf()) {
6998 buf[3] = 'u';
69076999 }
7000 if (sym.n_desc == N_DESC_GCED) {
7001 mem.copy(u8, buf[5..], "DEAD");
7002 }
7003 return buf[0..];
7004}
69087005
6909 {
6910 log.debug("resolver:", .{});
6911 var it = self.symbol_resolver.iterator();
6912 while (it.next()) |entry| {
6913 log.debug(" {s} => {}", .{ self.getString(entry.key_ptr.*), entry.value_ptr.* });
7006fn logSymtab(self: *MachO) void {
7007 var buf: [9]u8 = undefined;
7008
7009 log.debug("symtab:", .{});
7010 for (self.objects.items) |object, id| {
7011 log.debug(" object({d}): {s}", .{ id, object.name });
7012 for (object.symtab.items) |sym, sym_id| {
7013 const where = if (sym.undf() and !sym.tentative()) "ord" else "sect";
7014 const def_index = if (sym.undf() and !sym.tentative())
7015 @divTrunc(sym.n_desc, macho.N_SYMBOL_RESOLVER)
7016 else
7017 sym.n_sect;
7018 log.debug(" %{d}: {s} @{x} in {s}({d}), {s}", .{
7019 sym_id,
7020 object.getString(sym.n_strx),
7021 sym.n_value,
7022 where,
7023 def_index,
7024 logSymAttributes(sym, &buf),
7025 });
69147026 }
69157027 }
7028 log.debug(" object(null)", .{});
7029 for (self.locals.items) |sym, sym_id| {
7030 const where = if (sym.undf() and !sym.tentative()) "ord" else "sect";
7031 const def_index = if (sym.undf() and !sym.tentative())
7032 @divTrunc(sym.n_desc, macho.N_SYMBOL_RESOLVER)
7033 else
7034 sym.n_sect;
7035 log.debug(" %{d}: {s} @{x} in {s}({d}), {s}", .{
7036 sym_id,
7037 self.strtab.get(sym.n_strx),
7038 sym.n_value,
7039 where,
7040 def_index,
7041 logSymAttributes(sym, &buf),
7042 });
7043 }
7044
7045 log.debug("globals table:", .{});
7046 for (self.globals.keys()) |name, id| {
7047 const value = self.globals.values()[id];
7048 log.debug(" {s} => %{d} in object({d})", .{ name, value.sym_index, value.file });
7049 }
69167050
69177051 log.debug("GOT entries:", .{});
6918 for (self.got_entries_table.values()) |value| {
6919 const key = self.got_entries.items[value].target;
6920 const atom = self.got_entries.items[value].atom;
6921 const n_value = self.locals.items[atom.local_sym_index].n_value;
6922 switch (key) {
6923 .local => |ndx| log.debug(" {d}: @{x}", .{ ndx, n_value }),
6924 .global => |n_strx| log.debug(" {s}: @{x}", .{ self.getString(n_strx), n_value }),
7052 for (self.got_entries.items) |entry, i| {
7053 const atom_sym = entry.getSymbol(self);
7054 if (atom_sym.n_desc == N_DESC_GCED) continue;
7055 const target_sym = self.getSymbol(entry.target);
7056 if (target_sym.undf()) {
7057 log.debug(" {d}@{x} => import('{s}')", .{
7058 i,
7059 atom_sym.n_value,
7060 self.getSymbolName(entry.target),
7061 });
7062 } else {
7063 log.debug(" {d}@{x} => local(%{d}) in object({d}) {s}", .{
7064 i,
7065 atom_sym.n_value,
7066 entry.target.sym_index,
7067 entry.target.file,
7068 logSymAttributes(target_sym, &buf),
7069 });
69257070 }
69267071 }
69277072
69287073 log.debug("__thread_ptrs entries:", .{});
6929 for (self.tlv_ptr_entries_table.values()) |value| {
6930 const key = self.tlv_ptr_entries.items[value].target;
6931 const atom = self.tlv_ptr_entries.items[value].atom;
6932 const n_value = self.locals.items[atom.local_sym_index].n_value;
6933 assert(key == .global);
6934 log.debug(" {s}: @{x}", .{ self.getString(key.global), n_value });
7074 for (self.tlv_ptr_entries.items) |entry, i| {
7075 const atom_sym = entry.getSymbol(self);
7076 if (atom_sym.n_desc == N_DESC_GCED) continue;
7077 const target_sym = self.getSymbol(entry.target);
7078 assert(target_sym.undf());
7079 log.debug(" {d}@{x} => import('{s}')", .{
7080 i,
7081 atom_sym.n_value,
7082 self.getSymbolName(entry.target),
7083 });
69357084 }
69367085
6937 log.debug("stubs:", .{});
6938 for (self.stubs_table.keys()) |key| {
6939 const value = self.stubs_table.get(key).?;
6940 const atom = self.stubs.items[value];
6941 const sym = self.locals.items[atom.local_sym_index];
6942 log.debug(" {s}: @{x}", .{ self.getString(key), sym.n_value });
7086 log.debug("stubs entries:", .{});
7087 for (self.stubs.items) |entry, i| {
7088 const target_sym = self.getSymbol(entry.target);
7089 const atom_sym = entry.getSymbol(self);
7090 assert(target_sym.undf());
7091 log.debug(" {d}@{x} => import('{s}')", .{
7092 i,
7093 atom_sym.n_value,
7094 self.getSymbolName(entry.target),
7095 });
69437096 }
69447097}
69457098
6946fn logSectionOrdinals(self: MachO) void {
7099fn logSectionOrdinals(self: *MachO) void {
69477100 for (self.section_ordinals.keys()) |match, i| {
6948 const seg = self.load_commands.items[match.seg].segment;
6949 const sect = seg.sections.items[match.sect];
6950 log.debug("ord {d}: {d},{d} => {s},{s}", .{
6951 i + 1,
6952 match.seg,
6953 match.sect,
6954 sect.segName(),
6955 sect.sectName(),
7101 const sect = self.getSection(match);
7102 log.debug("sect({d}, '{s},{s}')", .{ i + 1, sect.segName(), sect.sectName() });
7103 }
7104}
7105
7106fn logAtoms(self: *MachO) void {
7107 log.debug("atoms:", .{});
7108 var it = self.atoms.iterator();
7109 while (it.next()) |entry| {
7110 const match = entry.key_ptr.*;
7111 var atom = entry.value_ptr.*;
7112
7113 while (atom.prev) |prev| {
7114 atom = prev;
7115 }
7116
7117 const sect = self.getSection(match);
7118 log.debug("{s},{s}", .{ sect.segName(), sect.sectName() });
7119
7120 while (true) {
7121 self.logAtom(atom);
7122 if (atom.next) |next| {
7123 atom = next;
7124 } else break;
7125 }
7126 }
7127}
7128
7129pub fn logAtom(self: *MachO, atom: *const Atom) void {
7130 const sym = atom.getSymbol(self);
7131 const sym_name = atom.getName(self);
7132 log.debug(" ATOM(%{d}, '{s}') @ {x} (sizeof({x}), alignof({x})) in object({d}) in sect({d})", .{
7133 atom.sym_index,
7134 sym_name,
7135 sym.n_value,
7136 atom.size,
7137 atom.alignment,
7138 atom.file,
7139 sym.n_sect,
7140 });
7141
7142 for (atom.contained.items) |sym_off| {
7143 const inner_sym = self.getSymbol(.{
7144 .sym_index = sym_off.sym_index,
7145 .file = atom.file,
7146 });
7147 const inner_sym_name = self.getSymbolName(.{
7148 .sym_index = sym_off.sym_index,
7149 .file = atom.file,
7150 });
7151 log.debug(" (%{d}, '{s}') @ {x} ({x})", .{
7152 sym_off.sym_index,
7153 inner_sym_name,
7154 inner_sym.n_value,
7155 sym_off.offset,
69567156 });
69577157 }
69587158}
src/link/MachO/Atom.zig+271-397
......@@ -16,7 +16,7 @@ const Arch = std.Target.Cpu.Arch;
1616const Dwarf = @import("../Dwarf.zig");
1717const MachO = @import("../MachO.zig");
1818const Object = @import("Object.zig");
19const StringIndexAdapter = std.hash_map.StringIndexAdapter;
19const SymbolWithLoc = MachO.SymbolWithLoc;
2020
2121/// Each decl always gets a local symbol with the fully qualified name.
2222/// The vaddr and size are found here directly.
......@@ -24,10 +24,10 @@ const StringIndexAdapter = std.hash_map.StringIndexAdapter;
2424/// the symbol references, and adding that to the file offset of the section.
2525/// If this field is 0, it means the codegen size = 0 and there is no symbol or
2626/// offset table entry.
27local_sym_index: u32,
27sym_index: u32,
2828
29/// List of symbol aliases pointing to the same atom via different nlists
30aliases: std.ArrayListUnmanaged(u32) = .{},
29/// null means symbol defined by Zig source.
30file: ?u32,
3131
3232/// List of symbols contained within this atom
3333contained: std.ArrayListUnmanaged(SymbolAtOffset) = .{},
......@@ -48,26 +48,17 @@ alignment: u32,
4848relocs: std.ArrayListUnmanaged(Relocation) = .{},
4949
5050/// List of offsets contained within this atom that need rebasing by the dynamic
51/// loader in presence of ASLR.
51/// loader for example in presence of ASLR.
5252rebases: std.ArrayListUnmanaged(u64) = .{},
5353
5454/// List of offsets contained within this atom that will be dynamically bound
5555/// by the dynamic loader and contain pointers to resolved (at load time) extern
56/// symbols (aka proxies aka imports)
56/// symbols (aka proxies aka imports).
5757bindings: std.ArrayListUnmanaged(Binding) = .{},
5858
59/// List of lazy bindings
59/// List of lazy bindings (cf bindings above).
6060lazy_bindings: std.ArrayListUnmanaged(Binding) = .{},
6161
62/// List of data-in-code entries. This is currently specific to x86_64 only.
63dices: std.ArrayListUnmanaged(macho.data_in_code_entry) = .{},
64
65/// Stab entry for this atom. This is currently specific to a binary created
66/// by linking object files in a traditional sense - in incremental sense, we
67/// bypass stabs altogether to produce dSYM bundle directly with fully relocated
68/// DWARF sections.
69stab: ?Stab = null,
70
7162/// Points to the previous and next neighbours
7263next: ?*Atom,
7364prev: ?*Atom,
......@@ -77,107 +68,62 @@ dbg_info_atom: Dwarf.Atom,
7768dirty: bool = true,
7869
7970pub const Binding = struct {
80 n_strx: u32,
71 target: SymbolWithLoc,
8172 offset: u64,
8273};
8374
8475pub const SymbolAtOffset = struct {
85 local_sym_index: u32,
76 sym_index: u32,
8677 offset: u64,
87 stab: ?Stab = null,
88};
89
90pub const Stab = union(enum) {
91 function: u64,
92 static,
93 global,
94
95 pub fn asNlists(stab: Stab, local_sym_index: u32, macho_file: anytype) ![]macho.nlist_64 {
96 var nlists = std.ArrayList(macho.nlist_64).init(macho_file.base.allocator);
97 defer nlists.deinit();
98
99 const sym = macho_file.locals.items[local_sym_index];
100 switch (stab) {
101 .function => |size| {
102 try nlists.ensureUnusedCapacity(4);
103 nlists.appendAssumeCapacity(.{
104 .n_strx = 0,
105 .n_type = macho.N_BNSYM,
106 .n_sect = sym.n_sect,
107 .n_desc = 0,
108 .n_value = sym.n_value,
109 });
110 nlists.appendAssumeCapacity(.{
111 .n_strx = sym.n_strx,
112 .n_type = macho.N_FUN,
113 .n_sect = sym.n_sect,
114 .n_desc = 0,
115 .n_value = sym.n_value,
116 });
117 nlists.appendAssumeCapacity(.{
118 .n_strx = 0,
119 .n_type = macho.N_FUN,
120 .n_sect = 0,
121 .n_desc = 0,
122 .n_value = size,
123 });
124 nlists.appendAssumeCapacity(.{
125 .n_strx = 0,
126 .n_type = macho.N_ENSYM,
127 .n_sect = sym.n_sect,
128 .n_desc = 0,
129 .n_value = size,
130 });
131 },
132 .global => {
133 try nlists.append(.{
134 .n_strx = sym.n_strx,
135 .n_type = macho.N_GSYM,
136 .n_sect = 0,
137 .n_desc = 0,
138 .n_value = 0,
139 });
140 },
141 .static => {
142 try nlists.append(.{
143 .n_strx = sym.n_strx,
144 .n_type = macho.N_STSYM,
145 .n_sect = sym.n_sect,
146 .n_desc = 0,
147 .n_value = sym.n_value,
148 });
149 },
150 }
151
152 return nlists.toOwnedSlice();
153 }
15478};
15579
15680pub const Relocation = struct {
157 pub const Target = union(enum) {
158 local: u32,
159 global: u32,
160 };
161
16281 /// Offset within the atom's code buffer.
16382 /// Note relocation size can be inferred by relocation's kind.
16483 offset: u32,
16584
166 target: Target,
85 target: MachO.SymbolWithLoc,
16786
16887 addend: i64,
16988
170 subtractor: ?u32,
89 subtractor: ?MachO.SymbolWithLoc,
17190
17291 pcrel: bool,
17392
17493 length: u2,
17594
17695 @"type": u4,
96
97 pub fn getTargetAtom(self: Relocation, macho_file: *MachO) ?*Atom {
98 const is_via_got = got: {
99 switch (macho_file.base.options.target.cpu.arch) {
100 .aarch64 => break :got switch (@intToEnum(macho.reloc_type_arm64, self.@"type")) {
101 .ARM64_RELOC_GOT_LOAD_PAGE21,
102 .ARM64_RELOC_GOT_LOAD_PAGEOFF12,
103 .ARM64_RELOC_POINTER_TO_GOT,
104 => true,
105 else => false,
106 },
107 .x86_64 => break :got switch (@intToEnum(macho.reloc_type_x86_64, self.@"type")) {
108 .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => true,
109 else => false,
110 },
111 else => unreachable,
112 }
113 };
114
115 if (is_via_got) {
116 return macho_file.getGotAtomForSymbol(self.target).?; // panic means fatal error
117 }
118 if (macho_file.getStubsAtomForSymbol(self.target)) |stubs_atom| return stubs_atom;
119 if (macho_file.getTlvPtrAtomForSymbol(self.target)) |tlv_ptr_atom| return tlv_ptr_atom;
120 return macho_file.getAtomForSymbol(self.target);
121 }
177122};
178123
179124pub const empty = Atom{
180 .local_sym_index = 0,
125 .sym_index = 0,
126 .file = null,
181127 .size = 0,
182128 .alignment = 0,
183129 .prev = null,
......@@ -186,34 +132,66 @@ pub const empty = Atom{
186132};
187133
188134pub fn deinit(self: *Atom, allocator: Allocator) void {
189 self.dices.deinit(allocator);
190135 self.lazy_bindings.deinit(allocator);
191136 self.bindings.deinit(allocator);
192137 self.rebases.deinit(allocator);
193138 self.relocs.deinit(allocator);
194139 self.contained.deinit(allocator);
195 self.aliases.deinit(allocator);
196140 self.code.deinit(allocator);
197141}
198142
199143pub fn clearRetainingCapacity(self: *Atom) void {
200 self.dices.clearRetainingCapacity();
201144 self.lazy_bindings.clearRetainingCapacity();
202145 self.bindings.clearRetainingCapacity();
203146 self.rebases.clearRetainingCapacity();
204147 self.relocs.clearRetainingCapacity();
205148 self.contained.clearRetainingCapacity();
206 self.aliases.clearRetainingCapacity();
207149 self.code.clearRetainingCapacity();
208150}
209151
152/// Returns symbol referencing this atom.
153pub fn getSymbol(self: Atom, macho_file: *MachO) macho.nlist_64 {
154 return self.getSymbolPtr(macho_file).*;
155}
156
157/// Returns pointer-to-symbol referencing this atom.
158pub fn getSymbolPtr(self: Atom, macho_file: *MachO) *macho.nlist_64 {
159 return macho_file.getSymbolPtr(.{
160 .sym_index = self.sym_index,
161 .file = self.file,
162 });
163}
164
165pub fn getSymbolWithLoc(self: Atom) SymbolWithLoc {
166 return .{ .sym_index = self.sym_index, .file = self.file };
167}
168
169/// Returns true if the symbol pointed at with `sym_loc` is contained within this atom.
170/// WARNING this function assumes all atoms have been allocated in the virtual memory.
171/// Calling it without allocating with `MachO.allocateSymbols` (or equivalent) will
172/// give bogus results.
173pub fn isSymbolContained(self: Atom, sym_loc: SymbolWithLoc, macho_file: *MachO) bool {
174 const sym = macho_file.getSymbol(sym_loc);
175 if (!sym.sect()) return false;
176 const self_sym = self.getSymbol(macho_file);
177 return sym.n_value >= self_sym.n_value and sym.n_value < self_sym.n_value + self.size;
178}
179
180/// Returns the name of this atom.
181pub fn getName(self: Atom, macho_file: *MachO) []const u8 {
182 return macho_file.getSymbolName(.{
183 .sym_index = self.sym_index,
184 .file = self.file,
185 });
186}
187
210188/// Returns how much room there is to grow in virtual address space.
211189/// File offset relocation happens transparently, so it is not included in
212190/// this calculation.
213pub fn capacity(self: Atom, macho_file: MachO) u64 {
214 const self_sym = macho_file.locals.items[self.local_sym_index];
191pub fn capacity(self: Atom, macho_file: *MachO) u64 {
192 const self_sym = self.getSymbol(macho_file);
215193 if (self.next) |next| {
216 const next_sym = macho_file.locals.items[next.local_sym_index];
194 const next_sym = next.getSymbol(macho_file);
217195 return next_sym.n_value - self_sym.n_value;
218196 } else {
219197 // We are the last atom.
......@@ -222,11 +200,11 @@ pub fn capacity(self: Atom, macho_file: MachO) u64 {
222200 }
223201}
224202
225pub fn freeListEligible(self: Atom, macho_file: MachO) bool {
203pub fn freeListEligible(self: Atom, macho_file: *MachO) bool {
226204 // No need to keep a free list node for the last atom.
227205 const next = self.next orelse return false;
228 const self_sym = macho_file.locals.items[self.local_sym_index];
229 const next_sym = macho_file.locals.items[next.local_sym_index];
206 const self_sym = self.getSymbol(macho_file);
207 const next_sym = next.getSymbol(macho_file);
230208 const cap = next_sym.n_value - self_sym.n_value;
231209 const ideal_cap = MachO.padToIdeal(self.size);
232210 if (cap <= ideal_cap) return false;
......@@ -235,19 +213,20 @@ pub fn freeListEligible(self: Atom, macho_file: MachO) bool {
235213}
236214
237215const RelocContext = struct {
238 base_addr: u64 = 0,
239 allocator: Allocator,
240 object: *Object,
241216 macho_file: *MachO,
217 base_addr: u64 = 0,
218 base_offset: i32 = 0,
242219};
243220
244pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocContext) !void {
221pub fn parseRelocs(self: *Atom, relocs: []const macho.relocation_info, context: RelocContext) !void {
245222 const tracy = trace(@src());
246223 defer tracy.end();
247224
225 const gpa = context.macho_file.base.allocator;
226
248227 const arch = context.macho_file.base.options.target.cpu.arch;
249228 var addend: i64 = 0;
250 var subtractor: ?u32 = null;
229 var subtractor: ?SymbolWithLoc = null;
251230
252231 for (relocs) |rel, i| {
253232 blk: {
......@@ -284,20 +263,16 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
284263 }
285264
286265 assert(subtractor == null);
287 const sym = context.object.symtab.items[rel.r_symbolnum];
266 const sym_loc = MachO.SymbolWithLoc{
267 .sym_index = rel.r_symbolnum,
268 .file = self.file,
269 };
270 const sym = context.macho_file.getSymbol(sym_loc);
288271 if (sym.sect() and !sym.ext()) {
289 subtractor = context.object.symbol_mapping.get(rel.r_symbolnum).?;
272 subtractor = sym_loc;
290273 } else {
291 const sym_name = context.object.getString(sym.n_strx);
292 const n_strx = context.macho_file.strtab_dir.getKeyAdapted(
293 @as([]const u8, sym_name),
294 StringIndexAdapter{
295 .bytes = &context.macho_file.strtab,
296 },
297 ).?;
298 const resolv = context.macho_file.symbol_resolver.get(n_strx).?;
299 assert(resolv.where == .global);
300 subtractor = resolv.local_sym_index;
274 const sym_name = context.macho_file.getSymbolName(sym_loc);
275 subtractor = context.macho_file.globals.get(sym_name).?;
301276 }
302277 // Verify that *_SUBTRACTOR is followed by *_UNSIGNED.
303278 if (relocs.len <= i + 1) {
......@@ -328,45 +303,42 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
328303 continue;
329304 }
330305
306 const object = &context.macho_file.objects.items[self.file.?];
331307 const target = target: {
332308 if (rel.r_extern == 0) {
333309 const sect_id = @intCast(u16, rel.r_symbolnum - 1);
334 const local_sym_index = context.object.sections_as_symbols.get(sect_id) orelse blk: {
335 const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].segment;
336 const sect = seg.sections.items[sect_id];
310 const sym_index = object.sections_as_symbols.get(sect_id) orelse blk: {
311 const sect = object.getSourceSection(sect_id);
337312 const match = (try context.macho_file.getMatchingSection(sect)) orelse
338313 unreachable;
339 const local_sym_index = @intCast(u32, context.macho_file.locals.items.len);
340 try context.macho_file.locals.append(context.allocator, .{
314 const sym_index = @intCast(u32, object.symtab.items.len);
315 try object.symtab.append(gpa, .{
341316 .n_strx = 0,
342317 .n_type = macho.N_SECT,
343 .n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(match).? + 1),
318 .n_sect = context.macho_file.getSectionOrdinal(match),
344319 .n_desc = 0,
345 .n_value = 0,
320 .n_value = sect.addr,
346321 });
347 try context.object.sections_as_symbols.putNoClobber(context.allocator, sect_id, local_sym_index);
348 break :blk local_sym_index;
322 try object.sections_as_symbols.putNoClobber(gpa, sect_id, sym_index);
323 break :blk sym_index;
349324 };
350 break :target Relocation.Target{ .local = local_sym_index };
325 break :target MachO.SymbolWithLoc{ .sym_index = sym_index, .file = self.file };
351326 }
352327
353 const sym = context.object.symtab.items[rel.r_symbolnum];
354 const sym_name = context.object.getString(sym.n_strx);
328 const sym_loc = MachO.SymbolWithLoc{
329 .sym_index = rel.r_symbolnum,
330 .file = self.file,
331 };
332 const sym = context.macho_file.getSymbol(sym_loc);
355333
356334 if (sym.sect() and !sym.ext()) {
357 const sym_index = context.object.symbol_mapping.get(rel.r_symbolnum) orelse unreachable;
358 break :target Relocation.Target{ .local = sym_index };
335 break :target sym_loc;
336 } else {
337 const sym_name = context.macho_file.getSymbolName(sym_loc);
338 break :target context.macho_file.globals.get(sym_name).?;
359339 }
360
361 const n_strx = context.macho_file.strtab_dir.getKeyAdapted(
362 @as([]const u8, sym_name),
363 StringIndexAdapter{
364 .bytes = &context.macho_file.strtab,
365 },
366 ) orelse unreachable;
367 break :target Relocation.Target{ .global = n_strx };
368340 };
369 const offset = @intCast(u32, rel.r_address);
341 const offset = @intCast(u32, rel.r_address - context.base_offset);
370342
371343 switch (arch) {
372344 .aarch64 => {
......@@ -388,8 +360,7 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
388360 else
389361 mem.readIntLittle(i32, self.code.items[offset..][0..4]);
390362 if (rel.r_extern == 0) {
391 const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].segment;
392 const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr;
363 const target_sect_base_addr = object.getSourceSection(@intCast(u16, rel.r_symbolnum - 1)).addr;
393364 addend -= @intCast(i64, target_sect_base_addr);
394365 }
395366 try self.addPtrBindingOrRebase(rel, target, context);
......@@ -397,9 +368,7 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
397368 .ARM64_RELOC_TLVP_LOAD_PAGE21,
398369 .ARM64_RELOC_TLVP_LOAD_PAGEOFF12,
399370 => {
400 if (target == .global) {
401 try addTlvPtrEntry(target, context);
402 }
371 try addTlvPtrEntry(target, context);
403372 },
404373 else => {},
405374 }
......@@ -423,8 +392,7 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
423392 else
424393 mem.readIntLittle(i32, self.code.items[offset..][0..4]);
425394 if (rel.r_extern == 0) {
426 const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].segment;
427 const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr;
395 const target_sect_base_addr = object.getSourceSection(@intCast(u16, rel.r_symbolnum - 1)).addr;
428396 addend -= @intCast(i64, target_sect_base_addr);
429397 }
430398 try self.addPtrBindingOrRebase(rel, target, context);
......@@ -445,16 +413,15 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
445413 if (rel.r_extern == 0) {
446414 // Note for the future self: when r_extern == 0, we should subtract correction from the
447415 // addend.
448 const seg = context.object.load_commands.items[context.object.segment_cmd_index.?].segment;
449 const target_sect_base_addr = seg.sections.items[rel.r_symbolnum - 1].addr;
416 const target_sect_base_addr = object.getSourceSection(@intCast(u16, rel.r_symbolnum - 1)).addr;
417 // We need to add base_offset, i.e., offset of this atom wrt to the source
418 // section. Otherwise, the addend will over-/under-shoot.
450419 addend += @intCast(i64, context.base_addr + offset + 4) -
451 @intCast(i64, target_sect_base_addr);
420 @intCast(i64, target_sect_base_addr) + context.base_offset;
452421 }
453422 },
454423 .X86_64_RELOC_TLV => {
455 if (target == .global) {
456 try addTlvPtrEntry(target, context);
457 }
424 try addTlvPtrEntry(target, context);
458425 },
459426 else => {},
460427 }
......@@ -462,7 +429,7 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
462429 else => unreachable,
463430 }
464431
465 try self.relocs.append(context.allocator, .{
432 try self.relocs.append(gpa, .{
466433 .offset = offset,
467434 .target = target,
468435 .addend = addend,
......@@ -480,286 +447,182 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
480447fn addPtrBindingOrRebase(
481448 self: *Atom,
482449 rel: macho.relocation_info,
483 target: Relocation.Target,
450 target: MachO.SymbolWithLoc,
484451 context: RelocContext,
485452) !void {
486 switch (target) {
487 .global => |n_strx| {
488 try self.bindings.append(context.allocator, .{
489 .n_strx = n_strx,
490 .offset = @intCast(u32, rel.r_address),
491 });
492 },
493 .local => {
494 const source_sym = context.macho_file.locals.items[self.local_sym_index];
495 const match = context.macho_file.section_ordinals.keys()[source_sym.n_sect - 1];
496 const seg = context.macho_file.load_commands.items[match.seg].segment;
497 const sect = seg.sections.items[match.sect];
498 const sect_type = sect.type_();
499
500 const should_rebase = rebase: {
501 if (rel.r_length != 3) break :rebase false;
502
503 // TODO actually, a check similar to what dyld is doing, that is, verifying
504 // that the segment is writable should be enough here.
505 const is_right_segment = blk: {
506 if (context.macho_file.data_segment_cmd_index) |idx| {
507 if (match.seg == idx) {
508 break :blk true;
509 }
453 const gpa = context.macho_file.base.allocator;
454 const sym = context.macho_file.getSymbol(target);
455 if (sym.undf()) {
456 try self.bindings.append(gpa, .{
457 .target = target,
458 .offset = @intCast(u32, rel.r_address - context.base_offset),
459 });
460 } else {
461 const source_sym = self.getSymbol(context.macho_file);
462 const match = context.macho_file.getMatchingSectionFromOrdinal(source_sym.n_sect);
463 const sect = context.macho_file.getSection(match);
464 const sect_type = sect.type_();
465
466 const should_rebase = rebase: {
467 if (rel.r_length != 3) break :rebase false;
468
469 // TODO actually, a check similar to what dyld is doing, that is, verifying
470 // that the segment is writable should be enough here.
471 const is_right_segment = blk: {
472 if (context.macho_file.data_segment_cmd_index) |idx| {
473 if (match.seg == idx) {
474 break :blk true;
510475 }
511 if (context.macho_file.data_const_segment_cmd_index) |idx| {
512 if (match.seg == idx) {
513 break :blk true;
514 }
476 }
477 if (context.macho_file.data_const_segment_cmd_index) |idx| {
478 if (match.seg == idx) {
479 break :blk true;
515480 }
516 break :blk false;
517 };
518
519 if (!is_right_segment) break :rebase false;
520 if (sect_type != macho.S_LITERAL_POINTERS and
521 sect_type != macho.S_REGULAR and
522 sect_type != macho.S_MOD_INIT_FUNC_POINTERS and
523 sect_type != macho.S_MOD_TERM_FUNC_POINTERS)
524 {
525 break :rebase false;
526481 }
527
528 break :rebase true;
482 break :blk false;
529483 };
530484
531 if (should_rebase) {
532 try self.rebases.append(context.allocator, @intCast(u32, rel.r_address));
485 if (!is_right_segment) break :rebase false;
486 if (sect_type != macho.S_LITERAL_POINTERS and
487 sect_type != macho.S_REGULAR and
488 sect_type != macho.S_MOD_INIT_FUNC_POINTERS and
489 sect_type != macho.S_MOD_TERM_FUNC_POINTERS)
490 {
491 break :rebase false;
533492 }
534 },
493
494 break :rebase true;
495 };
496
497 if (should_rebase) {
498 try self.rebases.append(gpa, @intCast(u32, rel.r_address - context.base_offset));
499 }
535500 }
536501}
537502
538fn addTlvPtrEntry(target: Relocation.Target, context: RelocContext) !void {
503fn addTlvPtrEntry(target: MachO.SymbolWithLoc, context: RelocContext) !void {
504 const target_sym = context.macho_file.getSymbol(target);
505 if (!target_sym.undf()) return;
539506 if (context.macho_file.tlv_ptr_entries_table.contains(target)) return;
540507
541508 const index = try context.macho_file.allocateTlvPtrEntry(target);
542509 const atom = try context.macho_file.createTlvPtrAtom(target);
543 context.macho_file.tlv_ptr_entries.items[index].atom = atom;
544
545 const match = (try context.macho_file.getMatchingSection(.{
546 .segname = MachO.makeStaticString("__DATA"),
547 .sectname = MachO.makeStaticString("__thread_ptrs"),
548 .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS,
549 })).?;
550 if (!context.object.start_atoms.contains(match)) {
551 try context.object.start_atoms.putNoClobber(context.allocator, match, atom);
552 }
553 if (context.object.end_atoms.getPtr(match)) |last| {
554 last.*.next = atom;
555 atom.prev = last.*;
556 last.* = atom;
557 } else {
558 try context.object.end_atoms.putNoClobber(context.allocator, match, atom);
559 }
510 context.macho_file.tlv_ptr_entries.items[index].sym_index = atom.sym_index;
560511}
561512
562fn addGotEntry(target: Relocation.Target, context: RelocContext) !void {
513fn addGotEntry(target: MachO.SymbolWithLoc, context: RelocContext) !void {
563514 if (context.macho_file.got_entries_table.contains(target)) return;
564515
565516 const index = try context.macho_file.allocateGotEntry(target);
566517 const atom = try context.macho_file.createGotAtom(target);
567 context.macho_file.got_entries.items[index].atom = atom;
568
569 const match = MachO.MatchingSection{
570 .seg = context.macho_file.data_const_segment_cmd_index.?,
571 .sect = context.macho_file.got_section_index.?,
572 };
573 if (!context.object.start_atoms.contains(match)) {
574 try context.object.start_atoms.putNoClobber(context.allocator, match, atom);
575 }
576 if (context.object.end_atoms.getPtr(match)) |last| {
577 last.*.next = atom;
578 atom.prev = last.*;
579 last.* = atom;
580 } else {
581 try context.object.end_atoms.putNoClobber(context.allocator, match, atom);
582 }
518 context.macho_file.got_entries.items[index].sym_index = atom.sym_index;
583519}
584520
585fn addStub(target: Relocation.Target, context: RelocContext) !void {
586 if (target != .global) return;
587 if (context.macho_file.stubs_table.contains(target.global)) return;
588 // If the symbol has been resolved as defined globally elsewhere (in a different translation unit),
589 // then skip creating stub entry.
590 // TODO Is this the correct for the incremental?
591 if (context.macho_file.symbol_resolver.get(target.global).?.where == .global) return;
592
593 const stub_index = try context.macho_file.allocateStubEntry(target.global);
594
595 // TODO clean this up!
596 const stub_helper_atom = atom: {
597 const atom = try context.macho_file.createStubHelperAtom();
598 const match = MachO.MatchingSection{
599 .seg = context.macho_file.text_segment_cmd_index.?,
600 .sect = context.macho_file.stub_helper_section_index.?,
601 };
602 if (!context.object.start_atoms.contains(match)) {
603 try context.object.start_atoms.putNoClobber(context.allocator, match, atom);
604 }
605 if (context.object.end_atoms.getPtr(match)) |last| {
606 last.*.next = atom;
607 atom.prev = last.*;
608 last.* = atom;
609 } else {
610 try context.object.end_atoms.putNoClobber(context.allocator, match, atom);
611 }
612 break :atom atom;
613 };
614 const laptr_atom = atom: {
615 const atom = try context.macho_file.createLazyPointerAtom(
616 stub_helper_atom.local_sym_index,
617 target.global,
618 );
619 const match = MachO.MatchingSection{
620 .seg = context.macho_file.data_segment_cmd_index.?,
621 .sect = context.macho_file.la_symbol_ptr_section_index.?,
622 };
623 if (!context.object.start_atoms.contains(match)) {
624 try context.object.start_atoms.putNoClobber(context.allocator, match, atom);
625 }
626 if (context.object.end_atoms.getPtr(match)) |last| {
627 last.*.next = atom;
628 atom.prev = last.*;
629 last.* = atom;
630 } else {
631 try context.object.end_atoms.putNoClobber(context.allocator, match, atom);
632 }
633 break :atom atom;
634 };
635 const atom = try context.macho_file.createStubAtom(laptr_atom.local_sym_index);
636 const match = MachO.MatchingSection{
637 .seg = context.macho_file.text_segment_cmd_index.?,
638 .sect = context.macho_file.stubs_section_index.?,
639 };
640 if (!context.object.start_atoms.contains(match)) {
641 try context.object.start_atoms.putNoClobber(context.allocator, match, atom);
642 }
643 if (context.object.end_atoms.getPtr(match)) |last| {
644 last.*.next = atom;
645 atom.prev = last.*;
646 last.* = atom;
647 } else {
648 try context.object.end_atoms.putNoClobber(context.allocator, match, atom);
649 }
650 context.macho_file.stubs.items[stub_index] = atom;
521fn addStub(target: MachO.SymbolWithLoc, context: RelocContext) !void {
522 const target_sym = context.macho_file.getSymbol(target);
523 if (!target_sym.undf()) return;
524 if (context.macho_file.stubs_table.contains(target)) return;
525
526 const stub_index = try context.macho_file.allocateStubEntry(target);
527 const stub_helper_atom = try context.macho_file.createStubHelperAtom();
528 const laptr_atom = try context.macho_file.createLazyPointerAtom(stub_helper_atom.sym_index, target);
529 const stub_atom = try context.macho_file.createStubAtom(laptr_atom.sym_index);
530
531 context.macho_file.stubs.items[stub_index].sym_index = stub_atom.sym_index;
651532}
652533
653534pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
654535 const tracy = trace(@src());
655536 defer tracy.end();
656537
538 log.debug("ATOM(%{d}, '{s}')", .{ self.sym_index, self.getName(macho_file) });
539
657540 for (self.relocs.items) |rel| {
658 log.debug("relocating {}", .{rel});
659541 const arch = macho_file.base.options.target.cpu.arch;
542 switch (arch) {
543 .aarch64 => {
544 log.debug(" RELA({s}) @ {x} => %{d} in object({d})", .{
545 @tagName(@intToEnum(macho.reloc_type_arm64, rel.@"type")),
546 rel.offset,
547 rel.target.sym_index,
548 rel.target.file,
549 });
550 },
551 .x86_64 => {
552 log.debug(" RELA({s}) @ {x} => %{d} in object({d})", .{
553 @tagName(@intToEnum(macho.reloc_type_x86_64, rel.@"type")),
554 rel.offset,
555 rel.target.sym_index,
556 rel.target.file,
557 });
558 },
559 else => unreachable,
560 }
561
660562 const source_addr = blk: {
661 const sym = macho_file.locals.items[self.local_sym_index];
662 break :blk sym.n_value + rel.offset;
563 const source_sym = self.getSymbol(macho_file);
564 break :blk source_sym.n_value + rel.offset;
565 };
566 const is_tlv = is_tlv: {
567 const source_sym = self.getSymbol(macho_file);
568 const match = macho_file.getMatchingSectionFromOrdinal(source_sym.n_sect);
569 const sect = macho_file.getSection(match);
570 break :is_tlv sect.type_() == macho.S_THREAD_LOCAL_VARIABLES;
663571 };
664 var is_via_thread_ptrs: bool = false;
665572 const target_addr = blk: {
666 const is_via_got = got: {
667 switch (arch) {
668 .aarch64 => break :got switch (@intToEnum(macho.reloc_type_arm64, rel.@"type")) {
669 .ARM64_RELOC_GOT_LOAD_PAGE21,
670 .ARM64_RELOC_GOT_LOAD_PAGEOFF12,
671 .ARM64_RELOC_POINTER_TO_GOT,
672 => true,
673 else => false,
674 },
675 .x86_64 => break :got switch (@intToEnum(macho.reloc_type_x86_64, rel.@"type")) {
676 .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => true,
677 else => false,
678 },
679 else => unreachable,
680 }
573 const target_atom = rel.getTargetAtom(macho_file) orelse {
574 // If there is no atom for target, we still need to check for special, atom-less
575 // symbols such as `___dso_handle`.
576 const target_name = macho_file.getSymbolName(rel.target);
577 assert(macho_file.globals.contains(target_name));
578 const atomless_sym = macho_file.getSymbol(rel.target);
579 log.debug(" | atomless target '{s}'", .{target_name});
580 break :blk atomless_sym.n_value;
681581 };
682
683 if (is_via_got) {
684 const got_index = macho_file.got_entries_table.get(rel.target) orelse {
685 log.err("expected GOT entry for symbol", .{});
686 switch (rel.target) {
687 .local => |sym_index| log.err(" local @{d}", .{sym_index}),
688 .global => |n_strx| log.err(" global @'{s}'", .{macho_file.getString(n_strx)}),
582 log.debug(" | target ATOM(%{d}, '{s}') in object({d})", .{
583 target_atom.sym_index,
584 target_atom.getName(macho_file),
585 target_atom.file,
586 });
587 // If `rel.target` is contained within the target atom, pull its address value.
588 const target_sym = if (target_atom.isSymbolContained(rel.target, macho_file))
589 macho_file.getSymbol(rel.target)
590 else
591 target_atom.getSymbol(macho_file);
592 assert(target_sym.n_desc != MachO.N_DESC_GCED);
593 const base_address: u64 = if (is_tlv) base_address: {
594 // For TLV relocations, the value specified as a relocation is the displacement from the
595 // TLV initializer (either value in __thread_data or zero-init in __thread_bss) to the first
596 // defined TLV template init section in the following order:
597 // * wrt to __thread_data if defined, then
598 // * wrt to __thread_bss
599 const sect_id: u16 = sect_id: {
600 if (macho_file.tlv_data_section_index) |i| {
601 break :sect_id i;
602 } else if (macho_file.tlv_bss_section_index) |i| {
603 break :sect_id i;
604 } else {
605 log.err("threadlocal variables present but no initializer sections found", .{});
606 log.err(" __thread_data not found", .{});
607 log.err(" __thread_bss not found", .{});
608 return error.FailedToResolveRelocationTarget;
689609 }
690 log.err(" this is an internal linker error", .{});
691 return error.FailedToResolveRelocationTarget;
692610 };
693 const atom = macho_file.got_entries.items[got_index].atom;
694 break :blk macho_file.locals.items[atom.local_sym_index].n_value;
695 }
696
697 switch (rel.target) {
698 .local => |sym_index| {
699 const sym = macho_file.locals.items[sym_index];
700 const is_tlv = is_tlv: {
701 const source_sym = macho_file.locals.items[self.local_sym_index];
702 const match = macho_file.section_ordinals.keys()[source_sym.n_sect - 1];
703 const seg = macho_file.load_commands.items[match.seg].segment;
704 const sect = seg.sections.items[match.sect];
705 break :is_tlv sect.type_() == macho.S_THREAD_LOCAL_VARIABLES;
706 };
707 if (is_tlv) {
708 // For TLV relocations, the value specified as a relocation is the displacement from the
709 // TLV initializer (either value in __thread_data or zero-init in __thread_bss) to the first
710 // defined TLV template init section in the following order:
711 // * wrt to __thread_data if defined, then
712 // * wrt to __thread_bss
713 const seg = macho_file.load_commands.items[macho_file.data_segment_cmd_index.?].segment;
714 const base_address = inner: {
715 if (macho_file.tlv_data_section_index) |i| {
716 break :inner seg.sections.items[i].addr;
717 } else if (macho_file.tlv_bss_section_index) |i| {
718 break :inner seg.sections.items[i].addr;
719 } else {
720 log.err("threadlocal variables present but no initializer sections found", .{});
721 log.err(" __thread_data not found", .{});
722 log.err(" __thread_bss not found", .{});
723 return error.FailedToResolveRelocationTarget;
724 }
725 };
726 break :blk sym.n_value - base_address;
727 }
728 break :blk sym.n_value;
729 },
730 .global => |n_strx| {
731 // TODO Still trying to figure out how to possibly use stubs for local symbol indirection with
732 // branching instructions. If it is not possible, then the best course of action is to
733 // resurrect the former approach of defering creating synthethic atoms in __got and __la_symbol_ptr
734 // sections until we resolve the relocations.
735 const resolv = macho_file.symbol_resolver.get(n_strx).?;
736 switch (resolv.where) {
737 .global => break :blk macho_file.globals.items[resolv.where_index].n_value,
738 .undef => {
739 if (macho_file.stubs_table.get(n_strx)) |stub_index| {
740 const atom = macho_file.stubs.items[stub_index];
741 break :blk macho_file.locals.items[atom.local_sym_index].n_value;
742 } else {
743 if (macho_file.tlv_ptr_entries_table.get(rel.target)) |tlv_ptr_index| {
744 is_via_thread_ptrs = true;
745 const atom = macho_file.tlv_ptr_entries.items[tlv_ptr_index].atom;
746 break :blk macho_file.locals.items[atom.local_sym_index].n_value;
747 }
748 break :blk 0;
749 }
750 },
751 }
752 },
753 }
611 break :base_address macho_file.getSection(.{
612 .seg = macho_file.data_segment_cmd_index.?,
613 .sect = sect_id,
614 }).addr;
615 } else 0;
616 break :blk target_sym.n_value - base_address;
754617 };
755618
756 log.debug(" | source_addr = 0x{x}", .{source_addr});
757 log.debug(" | target_addr = 0x{x}", .{target_addr});
619 log.debug(" | source_addr = 0x{x}", .{source_addr});
758620
759621 switch (arch) {
760622 .aarch64 => {
761623 switch (@intToEnum(macho.reloc_type_arm64, rel.@"type")) {
762624 .ARM64_RELOC_BRANCH26 => {
625 log.debug(" | target_addr = 0x{x}", .{target_addr});
763626 const displacement = math.cast(
764627 i28,
765628 @intCast(i64, target_addr) - @intCast(i64, source_addr),
......@@ -788,6 +651,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
788651 .ARM64_RELOC_TLVP_LOAD_PAGE21,
789652 => {
790653 const actual_target_addr = @intCast(i64, target_addr) + rel.addend;
654 log.debug(" | target_addr = 0x{x}", .{actual_target_addr});
791655 const source_page = @intCast(i32, source_addr >> 12);
792656 const target_page = @intCast(i32, actual_target_addr >> 12);
793657 const pages = @bitCast(u21, @intCast(i21, target_page - source_page));
......@@ -805,6 +669,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
805669 .ARM64_RELOC_PAGEOFF12 => {
806670 const code = self.code.items[rel.offset..][0..4];
807671 const actual_target_addr = @intCast(i64, target_addr) + rel.addend;
672 log.debug(" | target_addr = 0x{x}", .{actual_target_addr});
808673 const narrowed = @truncate(u12, @intCast(u64, actual_target_addr));
809674 if (isArithmeticOp(self.code.items[rel.offset..][0..4])) {
810675 var inst = aarch64.Instruction{
......@@ -842,6 +707,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
842707 .ARM64_RELOC_GOT_LOAD_PAGEOFF12 => {
843708 const code = self.code.items[rel.offset..][0..4];
844709 const actual_target_addr = @intCast(i64, target_addr) + rel.addend;
710 log.debug(" | target_addr = 0x{x}", .{actual_target_addr});
845711 const narrowed = @truncate(u12, @intCast(u64, actual_target_addr));
846712 var inst: aarch64.Instruction = .{
847713 .load_store_register = mem.bytesToValue(meta.TagPayload(
......@@ -856,6 +722,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
856722 .ARM64_RELOC_TLVP_LOAD_PAGEOFF12 => {
857723 const code = self.code.items[rel.offset..][0..4];
858724 const actual_target_addr = @intCast(i64, target_addr) + rel.addend;
725 log.debug(" | target_addr = 0x{x}", .{actual_target_addr});
859726
860727 const RegInfo = struct {
861728 rd: u5,
......@@ -886,7 +753,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
886753 }
887754 };
888755 const narrowed = @truncate(u12, @intCast(u64, actual_target_addr));
889 var inst = if (is_via_thread_ptrs) blk: {
756 var inst = if (macho_file.tlv_ptr_entries_table.contains(rel.target)) blk: {
890757 const offset = try math.divExact(u12, narrowed, 8);
891758 break :blk aarch64.Instruction{
892759 .load_store_register = .{
......@@ -913,18 +780,20 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
913780 mem.writeIntLittle(u32, code, inst.toU32());
914781 },
915782 .ARM64_RELOC_POINTER_TO_GOT => {
783 log.debug(" | target_addr = 0x{x}", .{target_addr});
916784 const result = math.cast(i32, @intCast(i64, target_addr) - @intCast(i64, source_addr)) orelse return error.Overflow;
917785 mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, result));
918786 },
919787 .ARM64_RELOC_UNSIGNED => {
920788 const result = blk: {
921789 if (rel.subtractor) |subtractor| {
922 const sym = macho_file.locals.items[subtractor];
790 const sym = macho_file.getSymbol(subtractor);
923791 break :blk @intCast(i64, target_addr) - @intCast(i64, sym.n_value) + rel.addend;
924792 } else {
925793 break :blk @intCast(i64, target_addr) + rel.addend;
926794 }
927795 };
796 log.debug(" | target_addr = 0x{x}", .{result});
928797
929798 if (rel.length == 3) {
930799 mem.writeIntLittle(u64, self.code.items[rel.offset..][0..8], @bitCast(u64, result));
......@@ -943,6 +812,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
943812 .x86_64 => {
944813 switch (@intToEnum(macho.reloc_type_x86_64, rel.@"type")) {
945814 .X86_64_RELOC_BRANCH => {
815 log.debug(" | target_addr = 0x{x}", .{target_addr});
946816 const displacement = math.cast(
947817 i32,
948818 @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4 + rel.addend,
......@@ -950,6 +820,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
950820 mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement));
951821 },
952822 .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => {
823 log.debug(" | target_addr = 0x{x}", .{target_addr});
953824 const displacement = math.cast(
954825 i32,
955826 @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4 + rel.addend,
......@@ -957,7 +828,8 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
957828 mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement));
958829 },
959830 .X86_64_RELOC_TLV => {
960 if (!is_via_thread_ptrs) {
831 log.debug(" | target_addr = 0x{x}", .{target_addr});
832 if (!macho_file.tlv_ptr_entries_table.contains(rel.target)) {
961833 // We need to rewrite the opcode from movq to leaq.
962834 self.code.items[rel.offset - 2] = 0x8d;
963835 }
......@@ -980,6 +852,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
980852 else => unreachable,
981853 };
982854 const actual_target_addr = @intCast(i64, target_addr) + rel.addend;
855 log.debug(" | target_addr = 0x{x}", .{actual_target_addr});
983856 const displacement = math.cast(
984857 i32,
985858 actual_target_addr - @intCast(i64, source_addr + correction + 4),
......@@ -989,12 +862,13 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
989862 .X86_64_RELOC_UNSIGNED => {
990863 const result = blk: {
991864 if (rel.subtractor) |subtractor| {
992 const sym = macho_file.locals.items[subtractor];
865 const sym = macho_file.getSymbol(subtractor);
993866 break :blk @intCast(i64, target_addr) - @intCast(i64, sym.n_value) + rel.addend;
994867 } else {
995868 break :blk @intCast(i64, target_addr) + rel.addend;
996869 }
997870 };
871 log.debug(" | target_addr = 0x{x}", .{result});
998872
999873 if (rel.length == 3) {
1000874 mem.writeIntLittle(u64, self.code.items[rel.offset..][0..8], @bitCast(u64, result));
src/link/MachO/DebugSymbols.zig+51-14
......@@ -5,7 +5,7 @@ const build_options = @import("build_options");
55const assert = std.debug.assert;
66const fs = std.fs;
77const link = @import("../../link.zig");
8const log = std.log.scoped(.link);
8const log = std.log.scoped(.dsym);
99const macho = std.macho;
1010const makeStaticString = MachO.makeStaticString;
1111const math = std.math;
......@@ -17,6 +17,7 @@ const Allocator = mem.Allocator;
1717const Dwarf = @import("../Dwarf.zig");
1818const MachO = @import("../MachO.zig");
1919const Module = @import("../../Module.zig");
20const StringTable = @import("../strtab.zig").StringTable;
2021const TextBlock = MachO.TextBlock;
2122const Type = @import("../../type.zig").Type;
2223
......@@ -59,6 +60,8 @@ debug_aranges_section_dirty: bool = false,
5960debug_info_header_dirty: bool = false,
6061debug_line_header_dirty: bool = false,
6162
63strtab: StringTable(.strtab) = .{},
64
6265relocs: std.ArrayListUnmanaged(Reloc) = .{},
6366
6467pub const Reloc = struct {
......@@ -93,6 +96,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: Allocator) !void
9396 .strsize = 0,
9497 },
9598 });
99 try self.strtab.buffer.append(allocator, 0);
96100 self.load_commands_dirty = true;
97101 }
98102
......@@ -269,22 +273,36 @@ pub fn flushModule(self: *DebugSymbols, allocator: Allocator, options: link.Opti
269273
270274 for (self.relocs.items) |*reloc| {
271275 const sym = switch (reloc.@"type") {
272 .direct_load => self.base.locals.items[reloc.target],
276 .direct_load => self.base.getSymbol(.{ .sym_index = reloc.target, .file = null }),
273277 .got_load => blk: {
274 const got_index = self.base.got_entries_table.get(.{ .local = reloc.target }).?;
278 const got_index = self.base.got_entries_table.get(.{
279 .sym_index = reloc.target,
280 .file = null,
281 }).?;
275282 const got_entry = self.base.got_entries.items[got_index];
276 break :blk self.base.locals.items[got_entry.atom.local_sym_index];
283 break :blk got_entry.getSymbol(self.base);
277284 },
278285 };
279286 if (sym.n_value == reloc.prev_vaddr) continue;
280287
288 const sym_name = switch (reloc.@"type") {
289 .direct_load => self.base.getSymbolName(.{ .sym_index = reloc.target, .file = null }),
290 .got_load => blk: {
291 const got_index = self.base.got_entries_table.get(.{
292 .sym_index = reloc.target,
293 .file = null,
294 }).?;
295 const got_entry = self.base.got_entries.items[got_index];
296 break :blk got_entry.getName(self.base);
297 },
298 };
281299 const seg = &self.load_commands.items[self.dwarf_segment_cmd_index.?].segment;
282300 const sect = &seg.sections.items[self.debug_info_section_index.?];
283301 const file_offset = sect.offset + reloc.offset;
284302 log.debug("resolving relocation: {d}@{x} ('{s}') at offset {x}", .{
285303 reloc.target,
286304 sym.n_value,
287 self.base.getString(sym.n_strx),
305 sym_name,
288306 file_offset,
289307 });
290308 try self.file.pwriteAll(mem.asBytes(&sym.n_value), file_offset);
......@@ -367,6 +385,7 @@ pub fn deinit(self: *DebugSymbols, allocator: Allocator) void {
367385 }
368386 self.load_commands.deinit(allocator);
369387 self.dwarf.deinit();
388 self.strtab.deinit(allocator);
370389 self.relocs.deinit(allocator);
371390}
372391
......@@ -582,21 +601,39 @@ fn writeSymbolTable(self: *DebugSymbols) !void {
582601 const tracy = trace(@src());
583602 defer tracy.end();
584603
604 const gpa = self.base.base.allocator;
585605 const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].segment;
586606 const symtab = &self.load_commands.items[self.symtab_cmd_index.?].symtab;
587607 symtab.symoff = @intCast(u32, seg.inner.fileoff);
588608
589 var locals = std.ArrayList(macho.nlist_64).init(self.base.base.allocator);
609 var locals = std.ArrayList(macho.nlist_64).init(gpa);
590610 defer locals.deinit();
591611
592 for (self.base.locals.items) |sym| {
593 if (sym.n_strx == 0) continue;
594 if (self.base.symbol_resolver.get(sym.n_strx)) |_| continue;
595 try locals.append(sym);
612 for (self.base.locals.items) |sym, sym_id| {
613 if (sym.n_strx == 0) continue; // no name, skip
614 if (sym.n_desc == MachO.N_DESC_GCED) continue; // GCed, skip
615 const sym_loc = MachO.SymbolWithLoc{ .sym_index = @intCast(u32, sym_id), .file = null };
616 if (self.base.symbolIsTemp(sym_loc)) continue; // local temp symbol, skip
617 if (self.base.globals.contains(self.base.getSymbolName(sym_loc))) continue; // global symbol is either an export or import, skip
618 var out_sym = sym;
619 out_sym.n_strx = try self.strtab.insert(gpa, self.base.getSymbolName(sym_loc));
620 try locals.append(out_sym);
621 }
622
623 var exports = std.ArrayList(macho.nlist_64).init(gpa);
624 defer exports.deinit();
625
626 for (self.base.globals.values()) |global| {
627 const sym = self.base.getSymbol(global);
628 if (sym.undf()) continue; // import, skip
629 if (sym.n_desc == MachO.N_DESC_GCED) continue; // GCed, skip
630 var out_sym = sym;
631 out_sym.n_strx = try self.strtab.insert(gpa, self.base.getSymbolName(global));
632 try exports.append(out_sym);
596633 }
597634
598635 const nlocals = locals.items.len;
599 const nexports = self.base.globals.items.len;
636 const nexports = exports.items.len;
600637 const locals_off = symtab.symoff;
601638 const locals_size = nlocals * @sizeOf(macho.nlist_64);
602639 const exports_off = locals_off + locals_size;
......@@ -641,7 +678,7 @@ fn writeSymbolTable(self: *DebugSymbols) !void {
641678 try self.file.pwriteAll(mem.sliceAsBytes(locals.items), locals_off);
642679
643680 log.debug("writing exported symbols from 0x{x} to 0x{x}", .{ exports_off, exports_size + exports_off });
644 try self.file.pwriteAll(mem.sliceAsBytes(self.base.globals.items), exports_off);
681 try self.file.pwriteAll(mem.sliceAsBytes(exports.items), exports_off);
645682
646683 self.load_commands_dirty = true;
647684}
......@@ -655,7 +692,7 @@ fn writeStringTable(self: *DebugSymbols) !void {
655692 const symtab_size = @intCast(u32, symtab.nsyms * @sizeOf(macho.nlist_64));
656693 symtab.stroff = symtab.symoff + symtab_size;
657694
658 const needed_size = mem.alignForwardGeneric(u64, self.base.strtab.items.len, @alignOf(u64));
695 const needed_size = mem.alignForwardGeneric(u64, self.strtab.buffer.items.len, @alignOf(u64));
659696 symtab.strsize = @intCast(u32, needed_size);
660697
661698 if (symtab_size + needed_size > seg.inner.filesize) {
......@@ -692,7 +729,7 @@ fn writeStringTable(self: *DebugSymbols) !void {
692729
693730 log.debug("writing string table from 0x{x} to 0x{x}", .{ symtab.stroff, symtab.stroff + symtab.strsize });
694731
695 try self.file.pwriteAll(self.base.strtab.items, symtab.stroff);
732 try self.file.pwriteAll(self.strtab.buffer.items, symtab.stroff);
696733
697734 self.load_commands_dirty = true;
698735}
src/link/MachO/Object.zig+429-422
......@@ -3,7 +3,6 @@ const Object = @This();
33const std = @import("std");
44const build_options = @import("build_options");
55const assert = std.debug.assert;
6const dwarf = std.dwarf;
76const fs = std.fs;
87const io = std.io;
98const log = std.log.scoped(.link);
......@@ -16,13 +15,21 @@ const trace = @import("../../tracy.zig").trace;
1615const Allocator = mem.Allocator;
1716const Atom = @import("Atom.zig");
1817const MachO = @import("../MachO.zig");
18const MatchingSection = MachO.MatchingSection;
19const SymbolWithLoc = MachO.SymbolWithLoc;
1920
2021file: fs.File,
2122name: []const u8,
23mtime: u64,
24
25/// Data contents of the file. Includes sections, and data of load commands.
26/// Excludes the backing memory for the header and load commands.
27/// Initialized in `parse`.
28contents: []const u8 = undefined,
2229
2330file_offset: ?u32 = null,
2431
25header: ?macho.mach_header_64 = null,
32header: macho.mach_header_64 = undefined,
2633
2734load_commands: std.ArrayListUnmanaged(macho.LoadCommand) = .{},
2835
......@@ -42,212 +49,58 @@ dwarf_debug_line_str_index: ?u16 = null,
4249dwarf_debug_ranges_index: ?u16 = null,
4350
4451symtab: std.ArrayListUnmanaged(macho.nlist_64) = .{},
45strtab: std.ArrayListUnmanaged(u8) = .{},
46data_in_code_entries: std.ArrayListUnmanaged(macho.data_in_code_entry) = .{},
47
48// Debug info
49debug_info: ?DebugInfo = null,
50tu_name: ?[]const u8 = null,
51tu_comp_dir: ?[]const u8 = null,
52mtime: ?u64 = null,
53
54contained_atoms: std.ArrayListUnmanaged(*Atom) = .{},
55start_atoms: std.AutoHashMapUnmanaged(MachO.MatchingSection, *Atom) = .{},
56end_atoms: std.AutoHashMapUnmanaged(MachO.MatchingSection, *Atom) = .{},
57sections_as_symbols: std.AutoHashMapUnmanaged(u16, u32) = .{},
52strtab: []const u8 = &.{},
53data_in_code_entries: []const macho.data_in_code_entry = &.{},
5854
59// TODO symbol mapping and its inverse can probably be simple arrays
60// instead of hash maps.
61symbol_mapping: std.AutoHashMapUnmanaged(u32, u32) = .{},
62reverse_symbol_mapping: std.AutoHashMapUnmanaged(u32, u32) = .{},
63
64analyzed: bool = false,
65
66const DebugInfo = struct {
67 inner: dwarf.DwarfInfo,
68 debug_info: []u8,
69 debug_abbrev: []u8,
70 debug_str: []u8,
71 debug_line: []u8,
72 debug_line_str: []u8,
73 debug_ranges: []u8,
74
75 pub fn parseFromObject(allocator: Allocator, object: *const Object) !?DebugInfo {
76 var debug_info = blk: {
77 const index = object.dwarf_debug_info_index orelse return null;
78 break :blk try object.readSection(allocator, index);
79 };
80 var debug_abbrev = blk: {
81 const index = object.dwarf_debug_abbrev_index orelse return null;
82 break :blk try object.readSection(allocator, index);
83 };
84 var debug_str = blk: {
85 const index = object.dwarf_debug_str_index orelse return null;
86 break :blk try object.readSection(allocator, index);
87 };
88 var debug_line = blk: {
89 const index = object.dwarf_debug_line_index orelse return null;
90 break :blk try object.readSection(allocator, index);
91 };
92 var debug_line_str = blk: {
93 if (object.dwarf_debug_line_str_index) |ind| {
94 break :blk try object.readSection(allocator, ind);
95 }
96 break :blk try allocator.alloc(u8, 0);
97 };
98 var debug_ranges = blk: {
99 if (object.dwarf_debug_ranges_index) |ind| {
100 break :blk try object.readSection(allocator, ind);
101 }
102 break :blk try allocator.alloc(u8, 0);
103 };
55sections_as_symbols: std.AutoHashMapUnmanaged(u16, u32) = .{},
10456
105 var inner: dwarf.DwarfInfo = .{
106 .endian = .Little,
107 .debug_info = debug_info,
108 .debug_abbrev = debug_abbrev,
109 .debug_str = debug_str,
110 .debug_line = debug_line,
111 .debug_line_str = debug_line_str,
112 .debug_ranges = debug_ranges,
113 };
114 try dwarf.openDwarfDebugInfo(&inner, allocator);
115
116 return DebugInfo{
117 .inner = inner,
118 .debug_info = debug_info,
119 .debug_abbrev = debug_abbrev,
120 .debug_str = debug_str,
121 .debug_line = debug_line,
122 .debug_line_str = debug_line_str,
123 .debug_ranges = debug_ranges,
124 };
125 }
57/// List of atoms that map to the symbols parsed from this object file.
58managed_atoms: std.ArrayListUnmanaged(*Atom) = .{},
12659
127 pub fn deinit(self: *DebugInfo, allocator: Allocator) void {
128 allocator.free(self.debug_info);
129 allocator.free(self.debug_abbrev);
130 allocator.free(self.debug_str);
131 allocator.free(self.debug_line);
132 allocator.free(self.debug_line_str);
133 allocator.free(self.debug_ranges);
134 self.inner.deinit(allocator);
135 }
136};
60/// Table of atoms belonging to this object file indexed by the symbol index.
61atom_by_index_table: std.AutoHashMapUnmanaged(u32, *Atom) = .{},
13762
138pub fn deinit(self: *Object, allocator: Allocator) void {
63pub fn deinit(self: *Object, gpa: Allocator) void {
13964 for (self.load_commands.items) |*lc| {
140 lc.deinit(allocator);
65 lc.deinit(gpa);
14166 }
142 self.load_commands.deinit(allocator);
143 self.data_in_code_entries.deinit(allocator);
144 self.symtab.deinit(allocator);
145 self.strtab.deinit(allocator);
146 self.sections_as_symbols.deinit(allocator);
147 self.symbol_mapping.deinit(allocator);
148 self.reverse_symbol_mapping.deinit(allocator);
149 allocator.free(self.name);
150
151 self.contained_atoms.deinit(allocator);
152 self.start_atoms.deinit(allocator);
153 self.end_atoms.deinit(allocator);
154
155 if (self.debug_info) |*db| {
156 db.deinit(allocator);
67 self.load_commands.deinit(gpa);
68 gpa.free(self.contents);
69 self.sections_as_symbols.deinit(gpa);
70 self.atom_by_index_table.deinit(gpa);
71
72 for (self.managed_atoms.items) |atom| {
73 atom.deinit(gpa);
74 gpa.destroy(atom);
15775 }
76 self.managed_atoms.deinit(gpa);
15877
159 if (self.tu_name) |n| {
160 allocator.free(n);
161 }
162
163 if (self.tu_comp_dir) |n| {
164 allocator.free(n);
165 }
166}
167
168pub fn free(self: *Object, allocator: Allocator, macho_file: *MachO) void {
169 log.debug("freeObject {*}", .{self});
170
171 var it = self.end_atoms.iterator();
172 while (it.next()) |entry| {
173 const match = entry.key_ptr.*;
174 const first_atom = self.start_atoms.get(match).?;
175 const last_atom = entry.value_ptr.*;
176 var atom = first_atom;
177
178 while (true) {
179 if (atom.local_sym_index != 0) {
180 macho_file.locals_free_list.append(allocator, atom.local_sym_index) catch {};
181 const local = &macho_file.locals.items[atom.local_sym_index];
182 local.* = .{
183 .n_strx = 0,
184 .n_type = 0,
185 .n_sect = 0,
186 .n_desc = 0,
187 .n_value = 0,
188 };
189 atom.local_sym_index = 0;
190 }
191 if (atom == last_atom) {
192 break;
193 }
194 if (atom.next) |next| {
195 atom = next;
196 } else break;
197 }
198 }
199
200 self.freeAtoms(macho_file);
78 gpa.free(self.name);
20179}
20280
203fn freeAtoms(self: *Object, macho_file: *MachO) void {
204 var it = self.end_atoms.iterator();
205 while (it.next()) |entry| {
206 const match = entry.key_ptr.*;
207 var first_atom: *Atom = self.start_atoms.get(match).?;
208 var last_atom: *Atom = entry.value_ptr.*;
209
210 if (macho_file.atoms.getPtr(match)) |atom_ptr| {
211 if (atom_ptr.* == last_atom) {
212 if (first_atom.prev) |prev| {
213 // TODO shrink the section size here
214 atom_ptr.* = prev;
215 } else {
216 _ = macho_file.atoms.fetchRemove(match);
217 }
218 }
219 }
220
221 if (first_atom.prev) |prev| {
222 prev.next = last_atom.next;
223 } else {
224 first_atom.prev = null;
225 }
81pub fn parse(self: *Object, allocator: Allocator, target: std.Target) !void {
82 const file_stat = try self.file.stat();
83 const file_size = math.cast(usize, file_stat.size) orelse return error.Overflow;
84 self.contents = try self.file.readToEndAlloc(allocator, file_size);
22685
227 if (last_atom.next) |next| {
228 next.prev = last_atom.prev;
229 } else {
230 last_atom.next = null;
231 }
232 }
233}
86 var stream = std.io.fixedBufferStream(self.contents);
87 const reader = stream.reader();
23488
235pub fn parse(self: *Object, allocator: Allocator, target: std.Target) !void {
236 const reader = self.file.reader();
237 if (self.file_offset) |offset| {
238 try reader.context.seekTo(offset);
89 const file_offset = self.file_offset orelse 0;
90 if (file_offset > 0) {
91 try reader.context.seekTo(file_offset);
23992 }
24093
241 const header = try reader.readStruct(macho.mach_header_64);
242 if (header.filetype != macho.MH_OBJECT) {
94 self.header = try reader.readStruct(macho.mach_header_64);
95 if (self.header.filetype != macho.MH_OBJECT) {
24396 log.debug("invalid filetype: expected 0x{x}, found 0x{x}", .{
24497 macho.MH_OBJECT,
245 header.filetype,
98 self.header.filetype,
24699 });
247100 return error.NotObject;
248101 }
249102
250 const this_arch: std.Target.Cpu.Arch = switch (header.cputype) {
103 const this_arch: std.Target.Cpu.Arch = switch (self.header.cputype) {
251104 macho.CPU_TYPE_ARM64 => .aarch64,
252105 macho.CPU_TYPE_X86_64 => .x86_64,
253106 else => |value| {
......@@ -260,22 +113,10 @@ pub fn parse(self: *Object, allocator: Allocator, target: std.Target) !void {
260113 return error.MismatchedCpuArchitecture;
261114 }
262115
263 self.header = header;
264
265 try self.readLoadCommands(allocator, reader);
266 try self.parseSymtab(allocator);
267 try self.parseDataInCode(allocator);
268 try self.parseDebugInfo(allocator);
269}
270
271pub fn readLoadCommands(self: *Object, allocator: Allocator, reader: anytype) !void {
272 const header = self.header orelse unreachable; // Unreachable here signifies a fatal unexplored condition.
273 const offset = self.file_offset orelse 0;
274
275 try self.load_commands.ensureUnusedCapacity(allocator, header.ncmds);
116 try self.load_commands.ensureUnusedCapacity(allocator, self.header.ncmds);
276117
277118 var i: u16 = 0;
278 while (i < header.ncmds) : (i += 1) {
119 while (i < self.header.ncmds) : (i += 1) {
279120 var cmd = try macho.LoadCommand.read(allocator, reader);
280121 switch (cmd.cmd()) {
281122 .SEGMENT_64 => {
......@@ -305,18 +146,18 @@ pub fn readLoadCommands(self: *Object, allocator: Allocator, reader: anytype) !v
305146 }
306147 }
307148
308 sect.offset += offset;
149 sect.offset += file_offset;
309150 if (sect.reloff > 0) {
310 sect.reloff += offset;
151 sect.reloff += file_offset;
311152 }
312153 }
313154
314 seg.inner.fileoff += offset;
155 seg.inner.fileoff += file_offset;
315156 },
316157 .SYMTAB => {
317158 self.symtab_cmd_index = i;
318 cmd.symtab.symoff += offset;
319 cmd.symtab.stroff += offset;
159 cmd.symtab.symoff += file_offset;
160 cmd.symtab.stroff += file_offset;
320161 },
321162 .DYSYMTAB => {
322163 self.dysymtab_cmd_index = i;
......@@ -326,7 +167,7 @@ pub fn readLoadCommands(self: *Object, allocator: Allocator, reader: anytype) !v
326167 },
327168 .DATA_IN_CODE => {
328169 self.data_in_code_cmd_index = i;
329 cmd.linkedit_data.dataoff += offset;
170 cmd.linkedit_data.dataoff += file_offset;
330171 },
331172 else => {
332173 log.debug("Unknown load command detected: 0x{x}.", .{cmd.cmd()});
......@@ -334,21 +175,37 @@ pub fn readLoadCommands(self: *Object, allocator: Allocator, reader: anytype) !v
334175 }
335176 self.load_commands.appendAssumeCapacity(cmd);
336177 }
178
179 try self.parseSymtab(allocator);
337180}
338181
339const NlistWithIndex = struct {
340 nlist: macho.nlist_64,
182const Context = struct {
183 symtab: []const macho.nlist_64,
184 strtab: []const u8,
185};
186
187const SymbolAtIndex = struct {
341188 index: u32,
342189
343 fn lessThan(_: void, lhs: NlistWithIndex, rhs: NlistWithIndex) bool {
344 // We sort by type: defined < undefined, and
345 // afterwards by address in each group. Normally, dysymtab should
346 // be enough to guarantee the sort, but turns out not every compiler
347 // is kind enough to specify the symbols in the correct order.
348 if (lhs.nlist.sect()) {
349 if (rhs.nlist.sect()) {
190 fn getSymbol(self: SymbolAtIndex, ctx: Context) macho.nlist_64 {
191 return ctx.symtab[self.index];
192 }
193
194 fn getSymbolName(self: SymbolAtIndex, ctx: Context) []const u8 {
195 const sym = self.getSymbol(ctx);
196 assert(sym.n_strx < ctx.strtab.len);
197 return mem.sliceTo(@ptrCast([*:0]const u8, ctx.strtab.ptr + sym.n_strx), 0);
198 }
199
200 /// Returns whether lhs is less than rhs by allocated address in object file.
201 /// Undefined symbols are pushed to the back (always evaluate to true).
202 fn lessThan(ctx: Context, lhs_index: SymbolAtIndex, rhs_index: SymbolAtIndex) bool {
203 const lhs = lhs_index.getSymbol(ctx);
204 const rhs = rhs_index.getSymbol(ctx);
205 if (lhs.sect()) {
206 if (rhs.sect()) {
350207 // Same group, sort by address.
351 return lhs.nlist.n_value < rhs.nlist.n_value;
208 return lhs.n_value < rhs.n_value;
352209 } else {
353210 return true;
354211 }
......@@ -357,60 +214,108 @@ const NlistWithIndex = struct {
357214 }
358215 }
359216
360 fn filterInSection(symbols: []NlistWithIndex, sect: macho.section_64) []NlistWithIndex {
361 const Predicate = struct {
362 addr: u64,
363
364 pub fn predicate(self: @This(), symbol: NlistWithIndex) bool {
365 return symbol.nlist.n_value >= self.addr;
366 }
367 };
368
369 const start = MachO.findFirst(NlistWithIndex, symbols, 0, Predicate{ .addr = sect.addr });
370 const end = MachO.findFirst(NlistWithIndex, symbols, start, Predicate{ .addr = sect.addr + sect.size });
217 /// Returns whether lhs is less senior than rhs. The rules are:
218 /// 1. ext
219 /// 2. weak
220 /// 3. local
221 /// 4. temp (local starting with `l` prefix).
222 fn lessThanBySeniority(ctx: Context, lhs_index: SymbolAtIndex, rhs_index: SymbolAtIndex) bool {
223 const lhs = lhs_index.getSymbol(ctx);
224 const rhs = rhs_index.getSymbol(ctx);
225 if (!rhs.ext()) {
226 const lhs_name = lhs_index.getSymbolName(ctx);
227 return mem.startsWith(u8, lhs_name, "l") or mem.startsWith(u8, lhs_name, "L");
228 } else if (rhs.pext() or rhs.weakDef()) {
229 return !lhs.ext();
230 } else {
231 return false;
232 }
233 }
371234
372 return symbols[start..end];
235 /// Like lessThanBySeniority but negated.
236 fn greaterThanBySeniority(ctx: Context, lhs_index: SymbolAtIndex, rhs_index: SymbolAtIndex) bool {
237 return !lessThanBySeniority(ctx, lhs_index, rhs_index);
373238 }
374239};
375240
376fn filterDice(dices: []macho.data_in_code_entry, start_addr: u64, end_addr: u64) []macho.data_in_code_entry {
241fn filterSymbolsByAddress(
242 indexes: []SymbolAtIndex,
243 start_addr: u64,
244 end_addr: u64,
245 ctx: Context,
246) []SymbolAtIndex {
247 const Predicate = struct {
248 addr: u64,
249 ctx: Context,
250
251 pub fn predicate(pred: @This(), index: SymbolAtIndex) bool {
252 return index.getSymbol(pred.ctx).n_value >= pred.addr;
253 }
254 };
255
256 const start = MachO.findFirst(SymbolAtIndex, indexes, 0, Predicate{
257 .addr = start_addr,
258 .ctx = ctx,
259 });
260 const end = MachO.findFirst(SymbolAtIndex, indexes, start, Predicate{
261 .addr = end_addr,
262 .ctx = ctx,
263 });
264
265 return indexes[start..end];
266}
267
268fn filterRelocs(
269 relocs: []const macho.relocation_info,
270 start_addr: u64,
271 end_addr: u64,
272) []const macho.relocation_info {
377273 const Predicate = struct {
378274 addr: u64,
379275
380 pub fn predicate(self: @This(), dice: macho.data_in_code_entry) bool {
381 return dice.offset >= self.addr;
276 pub fn predicate(self: @This(), rel: macho.relocation_info) bool {
277 return rel.r_address < self.addr;
382278 }
383279 };
384280
385 const start = MachO.findFirst(macho.data_in_code_entry, dices, 0, Predicate{ .addr = start_addr });
386 const end = MachO.findFirst(macho.data_in_code_entry, dices, start, Predicate{ .addr = end_addr });
281 const start = MachO.findFirst(macho.relocation_info, relocs, 0, Predicate{ .addr = end_addr });
282 const end = MachO.findFirst(macho.relocation_info, relocs, start, Predicate{ .addr = start_addr });
387283
388 return dices[start..end];
284 return relocs[start..end];
389285}
390286
391pub fn parseIntoAtoms(self: *Object, allocator: Allocator, macho_file: *MachO) !void {
287/// Splits object into atoms assuming one-shot linking mode.
288pub fn splitIntoAtomsOneShot(self: *Object, macho_file: *MachO, object_id: u32) !void {
289 assert(macho_file.mode == .one_shot);
290
392291 const tracy = trace(@src());
393292 defer tracy.end();
394293
294 const gpa = macho_file.base.allocator;
395295 const seg = self.load_commands.items[self.segment_cmd_index.?].segment;
396296
397 log.debug("analysing {s}", .{self.name});
297 log.debug("splitting object({d}, {s}) into atoms: one-shot mode", .{ object_id, self.name });
398298
399299 // You would expect that the symbol table is at least pre-sorted based on symbol's type:
400300 // local < extern defined < undefined. Unfortunately, this is not guaranteed! For instance,
401301 // the GO compiler does not necessarily respect that therefore we sort immediately by type
402302 // and address within.
403 var sorted_all_nlists = try std.ArrayList(NlistWithIndex).initCapacity(allocator, self.symtab.items.len);
404 defer sorted_all_nlists.deinit();
303 const context = Context{
304 .symtab = self.getSourceSymtab(),
305 .strtab = self.strtab,
306 };
307 var sorted_all_syms = try std.ArrayList(SymbolAtIndex).initCapacity(gpa, context.symtab.len);
308 defer sorted_all_syms.deinit();
405309
406 for (self.symtab.items) |nlist, index| {
407 sorted_all_nlists.appendAssumeCapacity(.{
408 .nlist = nlist,
409 .index = @intCast(u32, index),
410 });
310 for (context.symtab) |_, index| {
311 sorted_all_syms.appendAssumeCapacity(.{ .index = @intCast(u32, index) });
411312 }
412313
413 sort.sort(NlistWithIndex, sorted_all_nlists.items, {}, NlistWithIndex.lessThan);
314 // We sort by type: defined < undefined, and
315 // afterwards by address in each group. Normally, dysymtab should
316 // be enough to guarantee the sort, but turns out not every compiler
317 // is kind enough to specify the symbols in the correct order.
318 sort.sort(SymbolAtIndex, sorted_all_syms.items, context, SymbolAtIndex.lessThan);
414319
415320 // Well, shit, sometimes compilers skip the dysymtab load command altogether, meaning we
416321 // have to infer the start of undef section in the symtab ourselves.
......@@ -418,226 +323,328 @@ pub fn parseIntoAtoms(self: *Object, allocator: Allocator, macho_file: *MachO) !
418323 const dysymtab = self.load_commands.items[cmd_index].dysymtab;
419324 break :blk dysymtab.iundefsym;
420325 } else blk: {
421 var iundefsym: usize = sorted_all_nlists.items.len;
326 var iundefsym: usize = sorted_all_syms.items.len;
422327 while (iundefsym > 0) : (iundefsym -= 1) {
423 const nlist = sorted_all_nlists.items[iundefsym - 1];
424 if (nlist.nlist.sect()) break;
328 const sym = sorted_all_syms.items[iundefsym - 1].getSymbol(context);
329 if (sym.sect()) break;
425330 }
426331 break :blk iundefsym;
427332 };
428333
429334 // We only care about defined symbols, so filter every other out.
430 const sorted_nlists = sorted_all_nlists.items[0..iundefsym];
335 const sorted_syms = sorted_all_syms.items[0..iundefsym];
336 const subsections_via_symbols = self.header.flags & macho.MH_SUBSECTIONS_VIA_SYMBOLS != 0;
431337
432338 for (seg.sections.items) |sect, id| {
433339 const sect_id = @intCast(u8, id);
434 log.debug("putting section '{s},{s}' as an Atom", .{ sect.segName(), sect.sectName() });
340 log.debug("splitting section '{s},{s}' into atoms", .{ sect.segName(), sect.sectName() });
435341
436342 // Get matching segment/section in the final artifact.
437343 const match = (try macho_file.getMatchingSection(sect)) orelse {
438 log.debug("unhandled section", .{});
344 log.debug(" unhandled section", .{});
439345 continue;
440346 };
441347
442 // Read section's code
443 var code = try allocator.alloc(u8, @intCast(usize, sect.size));
444 defer allocator.free(code);
445 _ = try self.file.preadAll(code, sect.offset);
446
447 // Read section's list of relocations
448 var raw_relocs = try allocator.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info));
449 defer allocator.free(raw_relocs);
450 _ = try self.file.preadAll(raw_relocs, sect.reloff);
451 const relocs = mem.bytesAsSlice(macho.relocation_info, raw_relocs);
452
453 // Symbols within this section only.
454 const filtered_nlists = NlistWithIndex.filterInSection(sorted_nlists, sect);
455
456 macho_file.has_dices = macho_file.has_dices or blk: {
457 if (self.text_section_index) |index| {
458 if (index != id) break :blk false;
459 if (self.data_in_code_entries.items.len == 0) break :blk false;
460 break :blk true;
461 }
462 break :blk false;
463 };
464 macho_file.has_stabs = macho_file.has_stabs or self.debug_info != null;
465
466 // Since there is no symbol to refer to this atom, we create
467 // a temp one, unless we already did that when working out the relocations
468 // of other atoms.
469 const atom_local_sym_index = self.sections_as_symbols.get(sect_id) orelse blk: {
470 const atom_local_sym_index = @intCast(u32, macho_file.locals.items.len);
471 try macho_file.locals.append(allocator, .{
472 .n_strx = 0,
473 .n_type = macho.N_SECT,
474 .n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1),
475 .n_desc = 0,
476 .n_value = 0,
477 });
478 try self.sections_as_symbols.putNoClobber(allocator, sect_id, atom_local_sym_index);
479 break :blk atom_local_sym_index;
480 };
481 const alignment = try math.powi(u32, 2, sect.@"align");
482 const aligned_size = mem.alignForwardGeneric(u64, sect.size, alignment);
483 const atom = try macho_file.createEmptyAtom(atom_local_sym_index, aligned_size, sect.@"align");
348 log.debug(" output sect({d}, '{s},{s}')", .{
349 macho_file.getSectionOrdinal(match),
350 macho_file.getSection(match).segName(),
351 macho_file.getSection(match).sectName(),
352 });
484353
354 const arch = macho_file.base.options.target.cpu.arch;
485355 const is_zerofill = blk: {
486356 const section_type = sect.type_();
487357 break :blk section_type == macho.S_ZEROFILL or section_type == macho.S_THREAD_LOCAL_ZEROFILL;
488358 };
489 if (!is_zerofill) {
490 mem.copy(u8, atom.code.items, code);
491 }
492359
493 // TODO stage2 bug: @alignCast shouldn't be needed
494 try atom.parseRelocs(@alignCast(@alignOf(macho.relocation_info), relocs), .{
495 .base_addr = sect.addr,
496 .allocator = allocator,
497 .object = self,
498 .macho_file = macho_file,
499 });
360 // Read section's code
361 const code: ?[]const u8 = if (!is_zerofill) try self.getSectionContents(sect_id) else null;
500362
501 if (macho_file.has_dices) {
502 const dices = filterDice(self.data_in_code_entries.items, sect.addr, sect.addr + sect.size);
503 try atom.dices.ensureTotalCapacity(allocator, dices.len);
363 // Read section's list of relocations
364 const raw_relocs = self.contents[sect.reloff..][0 .. sect.nreloc * @sizeOf(macho.relocation_info)];
365 const relocs = mem.bytesAsSlice(
366 macho.relocation_info,
367 @alignCast(@alignOf(macho.relocation_info), raw_relocs),
368 );
504369
505 for (dices) |dice| {
506 atom.dices.appendAssumeCapacity(.{
507 .offset = dice.offset - (math.cast(u32, sect.addr) orelse return error.Overflow),
508 .length = dice.length,
509 .kind = dice.kind,
510 });
370 // Symbols within this section only.
371 const filtered_syms = filterSymbolsByAddress(
372 sorted_syms,
373 sect.addr,
374 sect.addr + sect.size,
375 context,
376 );
377
378 if (subsections_via_symbols and filtered_syms.len > 0) {
379 // If the first nlist does not match the start of the section,
380 // then we need to encapsulate the memory range [section start, first symbol)
381 // as a temporary symbol and insert the matching Atom.
382 const first_sym = filtered_syms[0].getSymbol(context);
383 if (first_sym.n_value > sect.addr) {
384 const sym_index = self.sections_as_symbols.get(sect_id) orelse blk: {
385 const sym_index = @intCast(u32, self.symtab.items.len);
386 try self.symtab.append(gpa, .{
387 .n_strx = 0,
388 .n_type = macho.N_SECT,
389 .n_sect = macho_file.getSectionOrdinal(match),
390 .n_desc = 0,
391 .n_value = sect.addr,
392 });
393 try self.sections_as_symbols.putNoClobber(gpa, sect_id, sym_index);
394 break :blk sym_index;
395 };
396 const atom_size = first_sym.n_value - sect.addr;
397 const atom_code: ?[]const u8 = if (code) |cc| blk: {
398 const size = math.cast(usize, atom_size) orelse return error.Overflow;
399 break :blk cc[0..size];
400 } else null;
401 const atom = try self.createAtomFromSubsection(
402 macho_file,
403 object_id,
404 sym_index,
405 atom_size,
406 sect.@"align",
407 atom_code,
408 relocs,
409 &.{},
410 match,
411 sect,
412 );
413 try macho_file.addAtomToSection(atom, match);
511414 }
512 }
513415
514 // Since this is atom gets a helper local temporary symbol that didn't exist
515 // in the object file which encompasses the entire section, we need traverse
516 // the filtered symbols and note which symbol is contained within so that
517 // we can properly allocate addresses down the line.
518 // While we're at it, we need to update segment,section mapping of each symbol too.
519 try atom.contained.ensureTotalCapacity(allocator, filtered_nlists.len);
520
521 for (filtered_nlists) |nlist_with_index| {
522 const nlist = nlist_with_index.nlist;
523 const local_sym_index = self.symbol_mapping.get(nlist_with_index.index) orelse unreachable;
524 const local = &macho_file.locals.items[local_sym_index];
525 local.n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1);
526
527 const stab: ?Atom.Stab = if (self.debug_info) |di| blk: {
528 // TODO there has to be a better to handle this.
529 for (di.inner.func_list.items) |func| {
530 if (func.pc_range) |range| {
531 if (nlist.n_value >= range.start and nlist.n_value < range.end) {
532 break :blk Atom.Stab{
533 .function = range.end - range.start,
534 };
535 }
536 }
416 var next_sym_count: usize = 0;
417 while (next_sym_count < filtered_syms.len) {
418 const next_sym = filtered_syms[next_sym_count].getSymbol(context);
419 const addr = next_sym.n_value;
420 const atom_syms = filterSymbolsByAddress(
421 filtered_syms[next_sym_count..],
422 addr,
423 addr + 1,
424 context,
425 );
426 next_sym_count += atom_syms.len;
427
428 // We want to bubble up the first externally defined symbol here.
429 assert(atom_syms.len > 0);
430 var sorted_atom_syms = std.ArrayList(SymbolAtIndex).init(gpa);
431 defer sorted_atom_syms.deinit();
432 try sorted_atom_syms.appendSlice(atom_syms);
433 sort.sort(
434 SymbolAtIndex,
435 sorted_atom_syms.items,
436 context,
437 SymbolAtIndex.greaterThanBySeniority,
438 );
439
440 const atom_size = blk: {
441 const end_addr = if (next_sym_count < filtered_syms.len)
442 filtered_syms[next_sym_count].getSymbol(context).n_value
443 else
444 sect.addr + sect.size;
445 break :blk end_addr - addr;
446 };
447 const atom_code: ?[]const u8 = if (code) |cc| blk: {
448 const start = math.cast(usize, addr - sect.addr) orelse return error.Overflow;
449 const size = math.cast(usize, atom_size) orelse return error.Overflow;
450 break :blk cc[start..][0..size];
451 } else null;
452 const atom_align = if (addr > 0)
453 math.min(@ctz(u64, addr), sect.@"align")
454 else
455 sect.@"align";
456 const atom = try self.createAtomFromSubsection(
457 macho_file,
458 object_id,
459 sorted_atom_syms.items[0].index,
460 atom_size,
461 atom_align,
462 atom_code,
463 relocs,
464 sorted_atom_syms.items[1..],
465 match,
466 sect,
467 );
468
469 if (arch == .x86_64 and addr == sect.addr) {
470 // In x86_64 relocs, it can so happen that the compiler refers to the same
471 // atom by both the actual assigned symbol and the start of the section. In this
472 // case, we need to link the two together so add an alias.
473 const alias = self.sections_as_symbols.get(sect_id) orelse blk: {
474 const alias = @intCast(u32, self.symtab.items.len);
475 try self.symtab.append(gpa, .{
476 .n_strx = 0,
477 .n_type = macho.N_SECT,
478 .n_sect = macho_file.getSectionOrdinal(match),
479 .n_desc = 0,
480 .n_value = addr,
481 });
482 try self.sections_as_symbols.putNoClobber(gpa, sect_id, alias);
483 break :blk alias;
484 };
485 try atom.contained.append(gpa, .{
486 .sym_index = alias,
487 .offset = 0,
488 });
489 try self.atom_by_index_table.put(gpa, alias, atom);
537490 }
538 // TODO
539 // if (zld.globals.contains(zld.getString(sym.strx))) break :blk .global;
540 break :blk .static;
541 } else null;
542
543 atom.contained.appendAssumeCapacity(.{
544 .local_sym_index = local_sym_index,
545 .offset = nlist.n_value - sect.addr,
546 .stab = stab,
547 });
548 }
549491
550 if (!self.start_atoms.contains(match)) {
551 try self.start_atoms.putNoClobber(allocator, match, atom);
552 }
553
554 if (self.end_atoms.getPtr(match)) |last| {
555 last.*.next = atom;
556 atom.prev = last.*;
557 last.* = atom;
492 try macho_file.addAtomToSection(atom, match);
493 }
558494 } else {
559 try self.end_atoms.putNoClobber(allocator, match, atom);
495 // If there is no symbol to refer to this atom, we create
496 // a temp one, unless we already did that when working out the relocations
497 // of other atoms.
498 const sym_index = self.sections_as_symbols.get(sect_id) orelse blk: {
499 const sym_index = @intCast(u32, self.symtab.items.len);
500 try self.symtab.append(gpa, .{
501 .n_strx = 0,
502 .n_type = macho.N_SECT,
503 .n_sect = macho_file.getSectionOrdinal(match),
504 .n_desc = 0,
505 .n_value = sect.addr,
506 });
507 try self.sections_as_symbols.putNoClobber(gpa, sect_id, sym_index);
508 break :blk sym_index;
509 };
510 const atom = try self.createAtomFromSubsection(
511 macho_file,
512 object_id,
513 sym_index,
514 sect.size,
515 sect.@"align",
516 code,
517 relocs,
518 filtered_syms,
519 match,
520 sect,
521 );
522 try macho_file.addAtomToSection(atom, match);
560523 }
561 try self.contained_atoms.append(allocator, atom);
562524 }
563525}
564526
565fn parseSymtab(self: *Object, allocator: Allocator) !void {
566 const index = self.symtab_cmd_index orelse return;
567 const symtab_cmd = self.load_commands.items[index].symtab;
568
569 var symtab = try allocator.alloc(u8, @sizeOf(macho.nlist_64) * symtab_cmd.nsyms);
570 defer allocator.free(symtab);
571 _ = try self.file.preadAll(symtab, symtab_cmd.symoff);
572 const slice = @alignCast(@alignOf(macho.nlist_64), mem.bytesAsSlice(macho.nlist_64, symtab));
573 try self.symtab.appendSlice(allocator, slice);
574
575 var strtab = try allocator.alloc(u8, symtab_cmd.strsize);
576 defer allocator.free(strtab);
577 _ = try self.file.preadAll(strtab, symtab_cmd.stroff);
578 try self.strtab.appendSlice(allocator, strtab);
579}
580
581pub fn parseDebugInfo(self: *Object, allocator: Allocator) !void {
582 log.debug("parsing debug info in '{s}'", .{self.name});
527fn createAtomFromSubsection(
528 self: *Object,
529 macho_file: *MachO,
530 object_id: u32,
531 sym_index: u32,
532 size: u64,
533 alignment: u32,
534 code: ?[]const u8,
535 relocs: []const macho.relocation_info,
536 indexes: []const SymbolAtIndex,
537 match: MatchingSection,
538 sect: macho.section_64,
539) !*Atom {
540 const gpa = macho_file.base.allocator;
541 const sym = self.symtab.items[sym_index];
542 const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment);
543 atom.file = object_id;
544 self.symtab.items[sym_index].n_sect = macho_file.getSectionOrdinal(match);
545
546 log.debug("creating ATOM(%{d}, '{s}') in sect({d}, '{s},{s}') in object({d})", .{
547 sym_index,
548 self.getString(sym.n_strx),
549 macho_file.getSectionOrdinal(match),
550 macho_file.getSection(match).segName(),
551 macho_file.getSection(match).sectName(),
552 object_id,
553 });
554
555 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
556 try self.managed_atoms.append(gpa, atom);
557
558 if (code) |cc| {
559 assert(size == cc.len);
560 mem.copy(u8, atom.code.items, cc);
561 }
583562
584 var debug_info = blk: {
585 var di = try DebugInfo.parseFromObject(allocator, self);
586 break :blk di orelse return;
587 };
563 const base_offset = sym.n_value - sect.addr;
564 const filtered_relocs = filterRelocs(relocs, base_offset, base_offset + size);
565 try atom.parseRelocs(filtered_relocs, .{
566 .macho_file = macho_file,
567 .base_addr = sect.addr,
568 .base_offset = @intCast(i32, base_offset),
569 });
570
571 // Since this is atom gets a helper local temporary symbol that didn't exist
572 // in the object file which encompasses the entire section, we need traverse
573 // the filtered symbols and note which symbol is contained within so that
574 // we can properly allocate addresses down the line.
575 // While we're at it, we need to update segment,section mapping of each symbol too.
576 try atom.contained.ensureTotalCapacity(gpa, indexes.len);
577 for (indexes) |inner_sym_index| {
578 const inner_sym = &self.symtab.items[inner_sym_index.index];
579 inner_sym.n_sect = macho_file.getSectionOrdinal(match);
580 atom.contained.appendAssumeCapacity(.{
581 .sym_index = inner_sym_index.index,
582 .offset = inner_sym.n_value - sym.n_value,
583 });
588584
589 // We assume there is only one CU.
590 const compile_unit = debug_info.inner.findCompileUnit(0x0) catch |err| switch (err) {
591 error.MissingDebugInfo => {
592 // TODO audit cases with missing debug info and audit our dwarf.zig module.
593 log.debug("invalid or missing debug info in {s}; skipping", .{self.name});
594 return;
595 },
596 else => |e| return e,
597 };
598 const name = try compile_unit.die.getAttrString(&debug_info.inner, dwarf.AT.name);
599 const comp_dir = try compile_unit.die.getAttrString(&debug_info.inner, dwarf.AT.comp_dir);
585 try self.atom_by_index_table.putNoClobber(gpa, inner_sym_index.index, atom);
586 }
600587
601 self.debug_info = debug_info;
602 self.tu_name = try allocator.dupe(u8, name);
603 self.tu_comp_dir = try allocator.dupe(u8, comp_dir);
588 return atom;
589}
604590
605 if (self.mtime == null) {
606 self.mtime = mtime: {
607 const stat = self.file.stat() catch break :mtime 0;
608 break :mtime @intCast(u64, @divFloor(stat.mtime, 1_000_000_000));
609 };
610 }
591fn parseSymtab(self: *Object, allocator: Allocator) !void {
592 const index = self.symtab_cmd_index orelse return;
593 const symtab = self.load_commands.items[index].symtab;
594 try self.symtab.appendSlice(allocator, self.getSourceSymtab());
595 self.strtab = self.contents[symtab.stroff..][0..symtab.strsize];
611596}
612597
613pub fn parseDataInCode(self: *Object, allocator: Allocator) !void {
614 const index = self.data_in_code_cmd_index orelse return;
615 const data_in_code = self.load_commands.items[index].linkedit_data;
598pub fn getSourceSymtab(self: Object) []const macho.nlist_64 {
599 const index = self.symtab_cmd_index orelse return &[0]macho.nlist_64{};
600 const symtab = self.load_commands.items[index].symtab;
601 const symtab_size = @sizeOf(macho.nlist_64) * symtab.nsyms;
602 const raw_symtab = self.contents[symtab.symoff..][0..symtab_size];
603 return mem.bytesAsSlice(
604 macho.nlist_64,
605 @alignCast(@alignOf(macho.nlist_64), raw_symtab),
606 );
607}
616608
617 var buffer = try allocator.alloc(u8, data_in_code.datasize);
618 defer allocator.free(buffer);
609pub fn getSourceSymbol(self: Object, index: u32) ?macho.nlist_64 {
610 const symtab = self.getSourceSymtab();
611 if (index >= symtab.len) return null;
612 return symtab[index];
613}
619614
620 _ = try self.file.preadAll(buffer, data_in_code.dataoff);
615pub fn getSourceSection(self: Object, index: u16) macho.section_64 {
616 const seg = self.load_commands.items[self.segment_cmd_index.?].segment;
617 assert(index < seg.sections.items.len);
618 return seg.sections.items[index];
619}
621620
622 var stream = io.fixedBufferStream(buffer);
623 var reader = stream.reader();
624 while (true) {
625 const dice = reader.readStruct(macho.data_in_code_entry) catch |err| switch (err) {
626 error.EndOfStream => break,
627 };
628 try self.data_in_code_entries.append(allocator, dice);
629 }
621pub fn parseDataInCode(self: Object) ?[]const macho.data_in_code_entry {
622 const index = self.data_in_code_cmd_index orelse return null;
623 const data_in_code = self.load_commands.items[index].linkedit_data;
624 const raw_dice = self.contents[data_in_code.dataoff..][0..data_in_code.datasize];
625 return mem.bytesAsSlice(
626 macho.data_in_code_entry,
627 @alignCast(@alignOf(macho.data_in_code_entry), raw_dice),
628 );
630629}
631630
632fn readSection(self: Object, allocator: Allocator, index: u16) ![]u8 {
633 const seg = self.load_commands.items[self.segment_cmd_index.?].segment;
634 const sect = seg.sections.items[index];
635 var buffer = try allocator.alloc(u8, @intCast(usize, sect.size));
636 _ = try self.file.preadAll(buffer, sect.offset);
637 return buffer;
631pub fn getSectionContents(self: Object, index: u16) error{Overflow}![]const u8 {
632 const sect = self.getSourceSection(index);
633 const size = math.cast(usize, sect.size) orelse return error.Overflow;
634 log.debug("getting {s},{s} data at 0x{x} - 0x{x}", .{
635 sect.segName(),
636 sect.sectName(),
637 sect.offset,
638 sect.offset + sect.size,
639 });
640 return self.contents[sect.offset..][0..size];
638641}
639642
640643pub fn getString(self: Object, off: u32) []const u8 {
641 assert(off < self.strtab.items.len);
642 return mem.sliceTo(@ptrCast([*:0]const u8, self.strtab.items.ptr + off), 0);
644 assert(off < self.strtab.len);
645 return mem.sliceTo(@ptrCast([*:0]const u8, self.strtab.ptr + off), 0);
646}
647
648pub fn getAtomForSymbol(self: Object, sym_index: u32) ?*Atom {
649 return self.atom_by_index_table.get(sym_index);
643650}
src/link/MachO/dead_strip.zig created+292
......@@ -0,0 +1,292 @@
1const std = @import("std");
2const assert = std.debug.assert;
3const log = std.log.scoped(.dead_strip);
4const macho = std.macho;
5const math = std.math;
6const mem = std.mem;
7
8const Allocator = mem.Allocator;
9const Atom = @import("Atom.zig");
10const MachO = @import("../MachO.zig");
11const MatchingSection = MachO.MatchingSection;
12
13pub fn gcAtoms(macho_file: *MachO) !void {
14 const gpa = macho_file.base.allocator;
15 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
16 defer arena_allocator.deinit();
17 const arena = arena_allocator.allocator();
18
19 var roots = std.AutoHashMap(*Atom, void).init(arena);
20 try collectRoots(&roots, macho_file);
21
22 var alive = std.AutoHashMap(*Atom, void).init(arena);
23 try mark(roots, &alive, macho_file);
24
25 try prune(arena, alive, macho_file);
26}
27
28fn removeAtomFromSection(atom: *Atom, match: MatchingSection, macho_file: *MachO) void {
29 const sect = macho_file.getSectionPtr(match);
30
31 // If we want to enable GC for incremental codepath, we need to take into
32 // account any padding that might have been left here.
33 sect.size -= atom.size;
34
35 if (atom.prev) |prev| {
36 prev.next = atom.next;
37 }
38 if (atom.next) |next| {
39 next.prev = atom.prev;
40 } else {
41 const last = macho_file.atoms.getPtr(match).?;
42 if (atom.prev) |prev| {
43 last.* = prev;
44 } else {
45 // The section will be GCed in the next step.
46 last.* = undefined;
47 sect.size = 0;
48 }
49 }
50}
51
52fn collectRoots(roots: *std.AutoHashMap(*Atom, void), macho_file: *MachO) !void {
53 const output_mode = macho_file.base.options.output_mode;
54
55 switch (output_mode) {
56 .Exe => {
57 // Add entrypoint as GC root
58 const global = try macho_file.getEntryPoint();
59 const atom = macho_file.getAtomForSymbol(global).?; // panic here means fatal error
60 _ = try roots.getOrPut(atom);
61 },
62 else => |other| {
63 assert(other == .Lib);
64 // Add exports as GC roots
65 for (macho_file.globals.values()) |global| {
66 const sym = macho_file.getSymbol(global);
67 if (!sym.sect()) continue;
68 const atom = macho_file.getAtomForSymbol(global) orelse {
69 log.debug("skipping {s}", .{macho_file.getSymbolName(global)});
70 continue;
71 };
72 _ = try roots.getOrPut(atom);
73 log.debug("adding root", .{});
74 macho_file.logAtom(atom);
75 }
76 },
77 }
78
79 // TODO just a temp until we learn how to parse unwind records
80 if (macho_file.globals.get("___gxx_personality_v0")) |global| {
81 if (macho_file.getAtomForSymbol(global)) |atom| {
82 _ = try roots.getOrPut(atom);
83 log.debug("adding root", .{});
84 macho_file.logAtom(atom);
85 }
86 }
87
88 for (macho_file.objects.items) |object| {
89 for (object.managed_atoms.items) |atom| {
90 const source_sym = object.getSourceSymbol(atom.sym_index) orelse continue;
91 if (source_sym.tentative()) continue;
92 const source_sect = object.getSourceSection(source_sym.n_sect - 1);
93 const is_gc_root = blk: {
94 if (source_sect.isDontDeadStrip()) break :blk true;
95 if (mem.eql(u8, "__StaticInit", source_sect.sectName())) break :blk true;
96 switch (source_sect.type_()) {
97 macho.S_MOD_INIT_FUNC_POINTERS,
98 macho.S_MOD_TERM_FUNC_POINTERS,
99 => break :blk true,
100 else => break :blk false,
101 }
102 };
103 if (is_gc_root) {
104 try roots.putNoClobber(atom, {});
105 log.debug("adding root", .{});
106 macho_file.logAtom(atom);
107 }
108 }
109 }
110}
111
112fn markLive(atom: *Atom, alive: *std.AutoHashMap(*Atom, void), macho_file: *MachO) anyerror!void {
113 const gop = try alive.getOrPut(atom);
114 if (gop.found_existing) return;
115
116 log.debug("marking live", .{});
117 macho_file.logAtom(atom);
118
119 for (atom.relocs.items) |rel| {
120 const target_atom = rel.getTargetAtom(macho_file) orelse continue;
121 try markLive(target_atom, alive, macho_file);
122 }
123}
124
125fn refersLive(atom: *Atom, alive: std.AutoHashMap(*Atom, void), macho_file: *MachO) bool {
126 for (atom.relocs.items) |rel| {
127 const target_atom = rel.getTargetAtom(macho_file) orelse continue;
128 if (alive.contains(target_atom)) return true;
129 }
130 return false;
131}
132
133fn refersDead(atom: *Atom, macho_file: *MachO) bool {
134 for (atom.relocs.items) |rel| {
135 const target_atom = rel.getTargetAtom(macho_file) orelse continue;
136 const target_sym = target_atom.getSymbol(macho_file);
137 if (target_sym.n_desc == MachO.N_DESC_GCED) return true;
138 }
139 return false;
140}
141
142fn mark(
143 roots: std.AutoHashMap(*Atom, void),
144 alive: *std.AutoHashMap(*Atom, void),
145 macho_file: *MachO,
146) !void {
147 try alive.ensureUnusedCapacity(roots.count());
148
149 var it = roots.keyIterator();
150 while (it.next()) |root| {
151 try markLive(root.*, alive, macho_file);
152 }
153
154 var loop: bool = true;
155 while (loop) {
156 loop = false;
157
158 for (macho_file.objects.items) |object| {
159 for (object.managed_atoms.items) |atom| {
160 if (alive.contains(atom)) continue;
161 const source_sym = object.getSourceSymbol(atom.sym_index) orelse continue;
162 if (source_sym.tentative()) continue;
163 const source_sect = object.getSourceSection(source_sym.n_sect - 1);
164 if (source_sect.isDontDeadStripIfReferencesLive() and refersLive(atom, alive.*, macho_file)) {
165 try markLive(atom, alive, macho_file);
166 loop = true;
167 }
168 }
169 }
170 }
171}
172
173fn prune(arena: Allocator, alive: std.AutoHashMap(*Atom, void), macho_file: *MachO) !void {
174 // Any section that ends up here will be updated, that is,
175 // its size and alignment recalculated.
176 var gc_sections = std.AutoHashMap(MatchingSection, void).init(arena);
177 var loop: bool = true;
178 while (loop) {
179 loop = false;
180
181 for (macho_file.objects.items) |object| {
182 for (object.getSourceSymtab()) |_, source_index| {
183 const atom = object.getAtomForSymbol(@intCast(u32, source_index)) orelse continue;
184 if (alive.contains(atom)) continue;
185
186 const global = atom.getSymbolWithLoc();
187 const sym = atom.getSymbolPtr(macho_file);
188 const match = macho_file.getMatchingSectionFromOrdinal(sym.n_sect);
189
190 if (sym.n_desc == MachO.N_DESC_GCED) continue;
191 if (!sym.ext() and !refersDead(atom, macho_file)) continue;
192
193 macho_file.logAtom(atom);
194 sym.n_desc = MachO.N_DESC_GCED;
195 removeAtomFromSection(atom, match, macho_file);
196 _ = try gc_sections.put(match, {});
197
198 for (atom.contained.items) |sym_off| {
199 const inner = macho_file.getSymbolPtr(.{
200 .sym_index = sym_off.sym_index,
201 .file = atom.file,
202 });
203 inner.n_desc = MachO.N_DESC_GCED;
204 }
205
206 if (macho_file.got_entries_table.contains(global)) {
207 const got_atom = macho_file.getGotAtomForSymbol(global).?;
208 const got_sym = got_atom.getSymbolPtr(macho_file);
209 got_sym.n_desc = MachO.N_DESC_GCED;
210 }
211
212 if (macho_file.stubs_table.contains(global)) {
213 const stubs_atom = macho_file.getStubsAtomForSymbol(global).?;
214 const stubs_sym = stubs_atom.getSymbolPtr(macho_file);
215 stubs_sym.n_desc = MachO.N_DESC_GCED;
216 }
217
218 if (macho_file.tlv_ptr_entries_table.contains(global)) {
219 const tlv_ptr_atom = macho_file.getTlvPtrAtomForSymbol(global).?;
220 const tlv_ptr_sym = tlv_ptr_atom.getSymbolPtr(macho_file);
221 tlv_ptr_sym.n_desc = MachO.N_DESC_GCED;
222 }
223
224 loop = true;
225 }
226 }
227 }
228
229 for (macho_file.got_entries.items) |entry| {
230 const sym = entry.getSymbol(macho_file);
231 if (sym.n_desc != MachO.N_DESC_GCED) continue;
232
233 // TODO tombstone
234 const atom = entry.getAtom(macho_file);
235 const match = macho_file.getMatchingSectionFromOrdinal(sym.n_sect);
236 removeAtomFromSection(atom, match, macho_file);
237 _ = try gc_sections.put(match, {});
238 _ = macho_file.got_entries_table.remove(entry.target);
239 }
240
241 for (macho_file.stubs.items) |entry| {
242 const sym = entry.getSymbol(macho_file);
243 if (sym.n_desc != MachO.N_DESC_GCED) continue;
244
245 // TODO tombstone
246 const atom = entry.getAtom(macho_file);
247 const match = macho_file.getMatchingSectionFromOrdinal(sym.n_sect);
248 removeAtomFromSection(atom, match, macho_file);
249 _ = try gc_sections.put(match, {});
250 _ = macho_file.stubs_table.remove(entry.target);
251 }
252
253 for (macho_file.tlv_ptr_entries.items) |entry| {
254 const sym = entry.getSymbol(macho_file);
255 if (sym.n_desc != MachO.N_DESC_GCED) continue;
256
257 // TODO tombstone
258 const atom = entry.getAtom(macho_file);
259 const match = macho_file.getMatchingSectionFromOrdinal(sym.n_sect);
260 removeAtomFromSection(atom, match, macho_file);
261 _ = try gc_sections.put(match, {});
262 _ = macho_file.tlv_ptr_entries_table.remove(entry.target);
263 }
264
265 var gc_sections_it = gc_sections.iterator();
266 while (gc_sections_it.next()) |entry| {
267 const match = entry.key_ptr.*;
268 const sect = macho_file.getSectionPtr(match);
269 if (sect.size == 0) continue; // Pruning happens automatically in next step.
270
271 sect.@"align" = 0;
272 sect.size = 0;
273
274 var atom = macho_file.atoms.get(match).?;
275
276 while (atom.prev) |prev| {
277 atom = prev;
278 }
279
280 while (true) {
281 const atom_alignment = try math.powi(u32, 2, atom.alignment);
282 const aligned_end_addr = mem.alignForwardGeneric(u64, sect.size, atom_alignment);
283 const padding = aligned_end_addr - sect.size;
284 sect.size += padding + atom.size;
285 sect.@"align" = @maximum(sect.@"align", atom.alignment);
286
287 if (atom.next) |next| {
288 atom = next;
289 } else break;
290 }
291 }
292}
src/link/strtab.zig created+113
......@@ -0,0 +1,113 @@
1const std = @import("std");
2const mem = std.mem;
3
4const Allocator = mem.Allocator;
5const StringIndexAdapter = std.hash_map.StringIndexAdapter;
6const StringIndexContext = std.hash_map.StringIndexContext;
7
8pub fn StringTable(comptime log_scope: @Type(.EnumLiteral)) type {
9 return struct {
10 const Self = @This();
11
12 const log = std.log.scoped(log_scope);
13
14 buffer: std.ArrayListUnmanaged(u8) = .{},
15 table: std.HashMapUnmanaged(u32, bool, StringIndexContext, std.hash_map.default_max_load_percentage) = .{},
16
17 pub fn deinit(self: *Self, gpa: Allocator) void {
18 self.buffer.deinit(gpa);
19 self.table.deinit(gpa);
20 }
21
22 pub fn toOwnedSlice(self: *Self, gpa: Allocator) []const u8 {
23 const result = self.buffer.toOwnedSlice(gpa);
24 self.table.clearRetainingCapacity();
25 return result;
26 }
27
28 pub const PrunedResult = struct {
29 buffer: []const u8,
30 idx_map: std.AutoHashMap(u32, u32),
31 };
32
33 pub fn toPrunedResult(self: *Self, gpa: Allocator) !PrunedResult {
34 var buffer = std.ArrayList(u8).init(gpa);
35 defer buffer.deinit();
36 try buffer.ensureTotalCapacity(self.buffer.items.len);
37 buffer.appendAssumeCapacity(0);
38
39 var idx_map = std.AutoHashMap(u32, u32).init(gpa);
40 errdefer idx_map.deinit();
41 try idx_map.ensureTotalCapacity(self.table.count());
42
43 var it = self.table.iterator();
44 while (it.next()) |entry| {
45 const off = entry.key_ptr.*;
46 const save = entry.value_ptr.*;
47 if (!save) continue;
48 const new_off = @intCast(u32, buffer.items.len);
49 buffer.appendSliceAssumeCapacity(self.getAssumeExists(off));
50 idx_map.putAssumeCapacityNoClobber(off, new_off);
51 }
52
53 self.buffer.clearRetainingCapacity();
54 self.table.clearRetainingCapacity();
55
56 return PrunedResult{
57 .buffer = buffer.toOwnedSlice(),
58 .idx_map = idx_map,
59 };
60 }
61
62 pub fn insert(self: *Self, gpa: Allocator, string: []const u8) !u32 {
63 const gop = try self.table.getOrPutContextAdapted(gpa, @as([]const u8, string), StringIndexAdapter{
64 .bytes = &self.buffer,
65 }, StringIndexContext{
66 .bytes = &self.buffer,
67 });
68 if (gop.found_existing) {
69 const off = gop.key_ptr.*;
70 gop.value_ptr.* = true;
71 log.debug("reusing string '{s}' at offset 0x{x}", .{ string, off });
72 return off;
73 }
74
75 try self.buffer.ensureUnusedCapacity(gpa, string.len + 1);
76 const new_off = @intCast(u32, self.buffer.items.len);
77
78 log.debug("writing new string '{s}' at offset 0x{x}", .{ string, new_off });
79
80 self.buffer.appendSliceAssumeCapacity(string);
81 self.buffer.appendAssumeCapacity(0);
82
83 gop.key_ptr.* = new_off;
84 gop.value_ptr.* = true;
85
86 return new_off;
87 }
88
89 pub fn delete(self: *Self, string: []const u8) void {
90 const value_ptr = self.table.getPtrAdapted(@as([]const u8, string), StringIndexAdapter{
91 .bytes = &self.buffer,
92 }) orelse return;
93 value_ptr.* = false;
94 log.debug("marked '{s}' for deletion", .{string});
95 }
96
97 pub fn getOffset(self: *Self, string: []const u8) ?u32 {
98 return self.table.getKeyAdapted(string, StringIndexAdapter{
99 .bytes = &self.buffer,
100 });
101 }
102
103 pub fn get(self: Self, off: u32) ?[]const u8 {
104 log.debug("getting string at 0x{x}", .{off});
105 if (off >= self.buffer.items.len) return null;
106 return mem.sliceTo(@ptrCast([*:0]const u8, self.buffer.items.ptr + off), 0);
107 }
108
109 pub fn getAssumeExists(self: Self, off: u32) []const u8 {
110 return self.get(off) orelse unreachable;
111 }
112 };
113}
src/main.zig+11
......@@ -446,6 +446,8 @@ const usage_build_generic =
446446 \\ --compress-debug-sections=[e] Debug section compression settings
447447 \\ none No compression
448448 \\ zlib Compression with deflate/inflate
449 \\ --gc-sections Force removal of functions and data that are unreachable by the entry point or exported symbols
450 \\ --no-gc-sections Don't force removal of unreachable functions and data
449451 \\ --subsystem [subsystem] (Windows) /SUBSYSTEM:<subsystem> to the linker
450452 \\ --stack [size] Override default stack size
451453 \\ --image-base [addr] Set base address for executable image
......@@ -463,6 +465,7 @@ const usage_build_generic =
463465 \\ -search_dylibs_first (Darwin) search `libx.dylib` in each dir in library search paths, then `libx.a`
464466 \\ -headerpad [value] (Darwin) set minimum space for future expansion of the load commands in hexadecimal notation
465467 \\ -headerpad_max_install_names (Darwin) set enough space as if all paths were MAXPATHLEN
468 \\ -dead_strip (Darwin) remove functions and data that are unreachable by the entry point or exported symbols
466469 \\ -dead_strip_dylibs (Darwin) remove dylibs that are unreachable by the entry point or exported symbols
467470 \\ --import-memory (WebAssembly) import memory from the environment
468471 \\ --import-table (WebAssembly) import function table from the host environment
......@@ -969,6 +972,8 @@ fn buildOutputType(
969972 };
970973 } else if (mem.eql(u8, arg, "-headerpad_max_install_names")) {
971974 headerpad_max_install_names = true;
975 } else if (mem.eql(u8, arg, "-dead_strip")) {
976 linker_gc_sections = true;
972977 } else if (mem.eql(u8, arg, "-dead_strip_dylibs")) {
973978 dead_strip_dylibs = true;
974979 } else if (mem.eql(u8, arg, "-T") or mem.eql(u8, arg, "--script")) {
......@@ -1311,6 +1316,10 @@ fn buildOutputType(
13111316 try linker_export_symbol_names.append(arg["--export=".len..]);
13121317 } else if (mem.eql(u8, arg, "-Bsymbolic")) {
13131318 linker_bind_global_refs_locally = true;
1319 } else if (mem.eql(u8, arg, "--gc-sections")) {
1320 linker_gc_sections = true;
1321 } else if (mem.eql(u8, arg, "--no-gc-sections")) {
1322 linker_gc_sections = false;
13141323 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
13151324 debug_compile_errors = true;
13161325 } else if (mem.eql(u8, arg, "--verbose-link")) {
......@@ -1764,6 +1773,8 @@ fn buildOutputType(
17641773 };
17651774 } else if (mem.eql(u8, arg, "-headerpad_max_install_names")) {
17661775 headerpad_max_install_names = true;
1776 } else if (mem.eql(u8, arg, "-dead_strip")) {
1777 linker_gc_sections = true;
17671778 } else if (mem.eql(u8, arg, "-dead_strip_dylibs")) {
17681779 dead_strip_dylibs = true;
17691780 } else if (mem.eql(u8, arg, "--gc-sections")) {
test/cases/recursive_inline_function.0.zig+1-1
......@@ -9,5 +9,5 @@ inline fn fibonacci(n: usize) usize {
99}
1010
1111// run
12// target=x86_64-linux,arm-linux,x86_64-macos,wasm32-wasi
12// target=x86_64-linux,arm-linux,wasm32-wasi
1313//
test/link.zig+4
......@@ -60,6 +60,10 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
6060 .build_modes = true,
6161 });
6262
63 cases.addBuildFile("test/link/macho/dead_strip/build.zig", .{
64 .build_modes = false,
65 });
66
6367 cases.addBuildFile("test/link/macho/dead_strip_dylibs/build.zig", .{
6468 .build_modes = true,
6569 .requires_macos_sdk = true,
test/link/macho/dead_strip/build.zig created+49
......@@ -0,0 +1,49 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3const LibExeObjectStep = std.build.LibExeObjStep;
4
5pub fn build(b: *Builder) void {
6 const mode = b.standardReleaseOptions();
7
8 const test_step = b.step("test", "Test the program");
9 test_step.dependOn(b.getInstallStep());
10
11 {
12 // Without -dead_strip, we expect `iAmUnused` symbol present
13 const exe = createScenario(b, mode);
14
15 const check = exe.checkObject(.macho);
16 check.checkInSymtab();
17 check.checkNext("{*} (__TEXT,__text) external _iAmUnused");
18
19 test_step.dependOn(&check.step);
20
21 const run_cmd = exe.run();
22 run_cmd.expectStdOutEqual("Hello!\n");
23 test_step.dependOn(&run_cmd.step);
24 }
25
26 {
27 // With -dead_strip, no `iAmUnused` symbol should be present
28 const exe = createScenario(b, mode);
29 exe.link_gc_sections = true;
30
31 const check = exe.checkObject(.macho);
32 check.checkInSymtab();
33 check.checkNotPresent("{*} (__TEXT,__text) external _iAmUnused");
34
35 test_step.dependOn(&check.step);
36
37 const run_cmd = exe.run();
38 run_cmd.expectStdOutEqual("Hello!\n");
39 test_step.dependOn(&run_cmd.step);
40 }
41}
42
43fn createScenario(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep {
44 const exe = b.addExecutable("test", null);
45 exe.addCSourceFile("main.c", &[0][]const u8{});
46 exe.setBuildMode(mode);
47 exe.linkLibC();
48 return exe;
49}
test/link/macho/dead_strip/main.c created+14
......@@ -0,0 +1,14 @@
1#include <stdio.h>
2
3void printMe() {
4 printf("Hello!\n");
5}
6
7int main(int argc, char* argv[]) {
8 printMe();
9 return 0;
10}
11
12void iAmUnused() {
13 printf("YOU SHALL NOT PASS!\n");
14}