authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-11 22:10:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:19-07:00
log5a6a1f8a8ad1475d328c998824981c7b310987d2
tree757e2d9bbf14e8ee96b8a2911d6a713dc95c3ff9
parent98da660e453789f578c04e641286bdf8ff84bcd4

linker: update target references


19 files changed, 274 insertions(+), 191 deletions(-)

src/arch/wasm/Emit.zig+2-1
......@@ -406,7 +406,8 @@ fn emitMemAddress(emit: *Emit, inst: Mir.Inst.Index) !void {
406406 const extra_index = emit.mir.instructions.items(.data)[inst].payload;
407407 const mem = emit.mir.extraData(Mir.Memory, extra_index).data;
408408 const mem_offset = emit.offset() + 1;
409 const is_wasm32 = emit.bin_file.base.options.target.cpu.arch == .wasm32;
409 const target = emit.bin_file.comp.root_mod.resolved_target.result;
410 const is_wasm32 = target.cpu.arch == .wasm32;
410411 if (is_wasm32) {
411412 try emit.code.append(std.wasm.opcode(.i32_const));
412413 var buf: [5]u8 = undefined;
src/link/Coff.zig+7-4
......@@ -1467,6 +1467,7 @@ pub fn updateExports(
14671467 }
14681468
14691469 const ip = &mod.intern_pool;
1470 const target = self.base.comp.root_mod.resolved_target.result;
14701471
14711472 if (self.base.options.use_llvm) {
14721473 // Even in the case of LLVM, we need to notice certain exported symbols in order to
......@@ -1478,7 +1479,7 @@ pub fn updateExports(
14781479 };
14791480 const exported_decl = mod.declPtr(exported_decl_index);
14801481 if (exported_decl.getOwnedFunction(mod) == null) continue;
1481 const winapi_cc = switch (self.base.options.target.cpu.arch) {
1482 const winapi_cc = switch (target.cpu.arch) {
14821483 .x86 => std.builtin.CallingConvention.Stdcall,
14831484 else => std.builtin.CallingConvention.C,
14841485 };
......@@ -1487,7 +1488,7 @@ pub fn updateExports(
14871488 self.base.options.link_libc)
14881489 {
14891490 mod.stage1_flags.have_c_main = true;
1490 } else if (decl_cc == winapi_cc and self.base.options.target.os.tag == .windows) {
1491 } else if (decl_cc == winapi_cc and target.os.tag == .windows) {
14911492 if (ip.stringEqlSlice(exp.opts.name, "WinMain")) {
14921493 mod.stage1_flags.have_winmain = true;
14931494 } else if (ip.stringEqlSlice(exp.opts.name, "wWinMain")) {
......@@ -2200,6 +2201,7 @@ fn writeDataDirectoriesHeaders(self: *Coff) !void {
22002201}
22012202
22022203fn writeHeader(self: *Coff) !void {
2204 const target = self.base.comp.root_mod.resolved_target.result;
22032205 const gpa = self.base.comp.gpa;
22042206 var buffer = std.ArrayList(u8).init(gpa);
22052207 defer buffer.deinit();
......@@ -2225,7 +2227,7 @@ fn writeHeader(self: *Coff) !void {
22252227 const timestamp = std.time.timestamp();
22262228 const size_of_optional_header = @as(u16, @intCast(self.getOptionalHeaderSize() + self.getDataDirectoryHeadersSize()));
22272229 var coff_header = coff.CoffHeader{
2228 .machine = coff.MachineType.fromTargetCpuArch(self.base.options.target.cpu.arch),
2230 .machine = coff.MachineType.fromTargetCpuArch(target.cpu.arch),
22292231 .number_of_sections = @as(u16, @intCast(self.sections.slice().len)), // TODO what if we prune a section
22302232 .time_date_stamp = @as(u32, @truncate(@as(u64, @bitCast(timestamp)))),
22312233 .pointer_to_symbol_table = self.strtab_offset orelse 0,
......@@ -2451,8 +2453,9 @@ pub fn getEntryPoint(self: Coff) ?SymbolWithLoc {
24512453}
24522454
24532455pub fn getImageBase(self: Coff) u64 {
2456 const target = self.base.comp.root_mod.resolved_target.result;
24542457 const image_base: u64 = self.base.options.image_base_override orelse switch (self.base.comp.config.output_mode) {
2455 .Exe => switch (self.base.options.target.cpu.arch) {
2458 .Exe => switch (target.cpu.arch) {
24562459 .aarch64 => @as(u64, 0x140000000),
24572460 .x86_64, .x86 => 0x400000,
24582461 else => unreachable, // unsupported target architecture
src/link/Coff/Relocation.zig+2-1
......@@ -107,7 +107,8 @@ pub fn resolve(self: Relocation, atom_index: Atom.Index, code: []u8, image_base:
107107 .ptr_width = coff_file.ptr_width,
108108 };
109109
110 switch (coff_file.base.options.target.cpu.arch) {
110 const target = coff_file.base.comp.root_mod.resolved_target.result;
111 switch (target.cpu.arch) {
111112 .aarch64 => self.resolveAarch64(ctx),
112113 .x86, .x86_64 => self.resolveX86(ctx),
113114 else => unreachable, // unhandled target architecture
src/link/Coff/lld.zig+1-1
......@@ -49,7 +49,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
4949 const is_dyn_lib = self.base.comp.config.link_mode == .Dynamic and is_lib;
5050 const is_exe_or_dyn_lib = is_dyn_lib or self.base.comp.config.output_mode == .Exe;
5151 const link_in_crt = self.base.options.link_libc and is_exe_or_dyn_lib;
52 const target = self.base.options.target;
52 const target = self.base.comp.root_mod.resolved_target.result;
5353 const optimize_mode = self.base.comp.root_mod.optimize_mode;
5454
5555 // See link/Elf.zig for comments on how this mechanism works.
src/link/Elf/Object.zig+2-1
......@@ -54,7 +54,8 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {
5454
5555 self.header = try reader.readStruct(elf.Elf64_Ehdr);
5656
57 if (elf_file.base.options.target.cpu.arch != self.header.?.e_machine.toTargetCpuArch().?) {
57 const target = elf_file.base.comp.root_mod.resolved_target.result;
58 if (target.cpu.arch != self.header.?.e_machine.toTargetCpuArch().?) {
5859 try elf_file.reportParseError2(
5960 self.index,
6061 "invalid cpu architecture: {s}",
src/link/Elf/SharedObject.zig+2-1
......@@ -53,7 +53,8 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void {
5353
5454 self.header = try reader.readStruct(elf.Elf64_Ehdr);
5555
56 if (elf_file.base.options.target.cpu.arch != self.header.?.e_machine.toTargetCpuArch().?) {
56 const target = elf_file.base.comp.root_mod.resolved_target.result;
57 if (target.cpu.arch != self.header.?.e_machine.toTargetCpuArch().?) {
5758 try elf_file.reportParseError2(
5859 self.index,
5960 "invalid cpu architecture: {s}",
src/link/Elf/synthetic_sections.zig+4-2
......@@ -287,7 +287,8 @@ pub const ZigGotSection = struct {
287287 zig_got.flags.dirty = false;
288288 }
289289 const entry_size: u16 = elf_file.archPtrWidthBytes();
290 const endian = elf_file.base.options.target.cpu.arch.endian();
290 const target = elf_file.base.comp.root_mod.resolved_target.result;
291 const endian = target.cpu.arch.endian();
291292 const off = zig_got.entryOffset(index, elf_file);
292293 const vaddr = zig_got.entryAddress(index, elf_file);
293294 const entry = zig_got.entries.items[index];
......@@ -1575,7 +1576,8 @@ pub const ComdatGroupSection = struct {
15751576
15761577fn writeInt(value: anytype, elf_file: *Elf, writer: anytype) !void {
15771578 const entry_size = elf_file.archPtrWidthBytes();
1578 const endian = elf_file.base.options.target.cpu.arch.endian();
1579 const target = elf_file.base.comp.root_mod.resolved_target.result;
1580 const endian = target.cpu.arch.endian();
15791581 switch (entry_size) {
15801582 2 => try writer.writeInt(u16, @intCast(value), endian),
15811583 4 => try writer.writeInt(u32, @intCast(value), endian),
src/link/MachO.zig+78-48
......@@ -143,7 +143,7 @@ tlv_table: TlvSymbolTable = .{},
143143/// Hot-code swapping state.
144144hot_state: if (is_hot_update_compatible) HotUpdateState else struct {} = .{},
145145
146darwin_sdk_layout: ?SdkLayout,
146sdk_layout: ?SdkLayout,
147147/// Size of the __PAGEZERO segment.
148148pagezero_vmsize: u64,
149149/// Minimum space for future expansion of the load commands.
......@@ -184,20 +184,21 @@ pub const SdkLayout = enum {
184184
185185pub fn open(arena: Allocator, options: link.File.OpenOptions) !*MachO {
186186 if (build_options.only_c) unreachable;
187 const target = options.comp.root_mod.resolved_target.result;
188 const use_lld = build_options.have_llvm and options.comp.config.use_lld;
189 const use_llvm = options.comp.config.use_llvm;
187 const comp = options.comp;
188 const target = comp.root_mod.resolved_target.result;
189 const use_lld = build_options.have_llvm and comp.config.use_lld;
190 const use_llvm = comp.config.use_llvm;
190191 assert(target.ofmt == .macho);
191192
192 const gpa = options.comp.gpa;
193 const gpa = comp.gpa;
193194 const emit = options.emit;
194195 const mode: Mode = mode: {
195 if (use_llvm or options.module == null or options.cache_mode == .whole)
196 if (use_llvm or comp.module == null or comp.cache_mode == .whole)
196197 break :mode .zld;
197198 break :mode .incremental;
198199 };
199200 const sub_path = if (mode == .zld) blk: {
200 if (options.module == null) {
201 if (comp.module == null) {
201202 // No point in opening a file, we would not write anything to it.
202203 // Initialize with empty.
203204 return createEmpty(arena, options);
......@@ -225,13 +226,13 @@ pub fn open(arena: Allocator, options: link.File.OpenOptions) !*MachO {
225226 .read = true,
226227 .mode = link.File.determineMode(
227228 use_lld,
228 options.comp.config.output_mode,
229 options.comp.config.link_mode,
229 comp.config.output_mode,
230 comp.config.link_mode,
230231 ),
231232 });
232233 self.base.file = file;
233234
234 if (!options.strip and options.module != null) {
235 if (self.base.debug_format != .strip and comp.module != null) {
235236 // Create dSYM bundle.
236237 log.debug("creating {s}.dSYM bundle", .{sub_path});
237238
......@@ -276,14 +277,15 @@ pub fn open(arena: Allocator, options: link.File.OpenOptions) !*MachO {
276277}
277278
278279pub fn createEmpty(arena: Allocator, options: link.File.OpenOptions) !*MachO {
279 const self = try arena.create(MachO);
280 const optimize_mode = options.comp.root_mod.optimize_mode;
281 const use_llvm = options.comp.config.use_llvm;
280 const comp = options.comp;
281 const optimize_mode = comp.root_mod.optimize_mode;
282 const use_llvm = comp.config.use_llvm;
282283
284 const self = try arena.create(MachO);
283285 self.* = .{
284286 .base = .{
285287 .tag = .macho,
286 .comp = options.comp,
288 .comp = comp,
287289 .emit = options.emit,
288290 .gc_sections = options.gc_sections orelse (optimize_mode != .Debug),
289291 .stack_size = options.stack_size orelse 16777216,
......@@ -297,7 +299,7 @@ pub fn createEmpty(arena: Allocator, options: link.File.OpenOptions) !*MachO {
297299 .function_sections = options.function_sections,
298300 .data_sections = options.data_sections,
299301 },
300 .mode = if (use_llvm or options.module == null or options.cache_mode == .whole)
302 .mode = if (use_llvm or comp.module == null or comp.cache_mode == .whole)
301303 .zld
302304 else
303305 .incremental,
......@@ -305,9 +307,13 @@ pub fn createEmpty(arena: Allocator, options: link.File.OpenOptions) !*MachO {
305307 .headerpad_size = options.headerpad_size orelse default_headerpad_size,
306308 .headerpad_max_install_names = options.headerpad_max_install_names,
307309 .dead_strip_dylibs = options.dead_strip_dylibs,
310 .sdk_layout = options.darwin_sdk_layout,
311 .frameworks = options.frameworks,
312 .install_name = options.install_name,
313 .entitlements = options.entitlements,
308314 };
309315
310 if (use_llvm and options.module != null) {
316 if (use_llvm and comp.module != null) {
311317 self.llvm_object = try LlvmObject.create(arena, options);
312318 }
313319
......@@ -357,6 +363,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
357363
358364 const output_mode = self.base.comp.config.output_mode;
359365 const module = self.base.comp.module orelse return error.LinkingWithoutZigSourceUnimplemented;
366 const target = self.base.comp.root_mod.resolved_target.result;
360367
361368 if (self.lazy_syms.getPtr(.none)) |metadata| {
362369 // Most lazy symbols can be updated on first use, but
......@@ -569,7 +576,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
569576 // written out to the file.
570577 // The most important here is to have the correct vm and filesize of the __LINKEDIT segment
571578 // where the code signature goes into.
572 var codesig = CodeSignature.init(getPageSize(self.base.options.target.cpu.arch));
579 var codesig = CodeSignature.init(getPageSize(target.cpu.arch));
573580 codesig.code_directory.ident = self.base.emit.sub_path;
574581 if (self.entitlements) |path| {
575582 try codesig.addEntitlements(gpa, path);
......@@ -619,7 +626,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
619626 .version = 0,
620627 });
621628 {
622 const platform = Platform.fromTarget(self.base.options.target);
629 const platform = Platform.fromTarget(target);
623630 const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(arena, comp);
624631 if (platform.isBuildVersionCompatible()) {
625632 try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer);
......@@ -701,7 +708,7 @@ pub fn resolveLibSystem(
701708 var checked_paths = std.ArrayList([]const u8).init(arena);
702709
703710 success: {
704 if (self.base.options.darwin_sdk_layout) |sdk_layout| switch (sdk_layout) {
711 if (self.sdk_layout) |sdk_layout| switch (sdk_layout) {
705712 .sdk => {
706713 const dir = try fs.path.join(arena, &[_][]const u8{ self.base.options.sysroot.?, "usr", "lib" });
707714 if (try accessLibPath(arena, &test_path, &checked_paths, dir, "libSystem")) break :success;
......@@ -817,6 +824,7 @@ fn parseObject(
817824 defer tracy.end();
818825
819826 const gpa = self.base.comp.gpa;
827 const target = self.base.comp.root_mod.resolved_target.result;
820828 const mtime: u64 = mtime: {
821829 const stat = file.stat() catch break :mtime 0;
822830 break :mtime @as(u64, @intCast(@divFloor(stat.mtime, 1_000_000_000)));
......@@ -839,8 +847,8 @@ fn parseObject(
839847 else => unreachable,
840848 };
841849 const detected_platform = object.getPlatform();
842 const this_cpu_arch = self.base.options.target.cpu.arch;
843 const this_platform = Platform.fromTarget(self.base.options.target);
850 const this_cpu_arch = target.cpu.arch;
851 const this_platform = Platform.fromTarget(target);
844852
845853 if (this_cpu_arch != detected_cpu_arch or
846854 (detected_platform != null and !detected_platform.?.eqlTarget(this_platform)))
......@@ -867,8 +875,10 @@ pub fn parseLibrary(
867875 const tracy = trace(@src());
868876 defer tracy.end();
869877
878 const target = self.base.comp.root_mod.resolved_target.result;
879
870880 if (fat.isFatLibrary(file)) {
871 const offset = try self.parseFatLibrary(file, self.base.options.target.cpu.arch, ctx);
881 const offset = try self.parseFatLibrary(file, target.cpu.arch, ctx);
872882 try file.seekTo(offset);
873883
874884 if (Archive.isArchive(file, offset)) {
......@@ -934,6 +944,7 @@ fn parseArchive(
934944 ctx: *ParseErrorCtx,
935945) ParseError!void {
936946 const gpa = self.base.comp.gpa;
947 const target = self.base.comp.root_mod.resolved_target.result;
937948
938949 // We take ownership of the file so that we can store it for the duration of symbol resolution.
939950 // TODO we shouldn't need to do that and could pre-parse the archive like we do for zld/ELF?
......@@ -963,8 +974,8 @@ fn parseArchive(
963974 else => unreachable,
964975 };
965976 const detected_platform = object.getPlatform();
966 const this_cpu_arch = self.base.options.target.cpu.arch;
967 const this_platform = Platform.fromTarget(self.base.options.target);
977 const this_cpu_arch = target.cpu.arch;
978 const this_platform = Platform.fromTarget(target);
968979
969980 if (this_cpu_arch != detected_cpu_arch or
970981 (detected_platform != null and !detected_platform.?.eqlTarget(this_platform)))
......@@ -1015,6 +1026,7 @@ fn parseDylib(
10151026 ctx: *ParseErrorCtx,
10161027) ParseError!void {
10171028 const gpa = self.base.comp.gpa;
1029 const target = self.base.comp.root_mod.resolved_target.result;
10181030 const file_stat = try file.stat();
10191031 const file_size = math.cast(usize, file_stat.size - offset) orelse return error.Overflow;
10201032
......@@ -1038,8 +1050,8 @@ fn parseDylib(
10381050 else => unreachable,
10391051 };
10401052 const detected_platform = dylib.getPlatform(contents);
1041 const this_cpu_arch = self.base.options.target.cpu.arch;
1042 const this_platform = Platform.fromTarget(self.base.options.target);
1053 const this_cpu_arch = target.cpu.arch;
1054 const this_platform = Platform.fromTarget(target);
10431055
10441056 if (this_cpu_arch != detected_cpu_arch or
10451057 (detected_platform != null and !detected_platform.?.eqlTarget(this_platform)))
......@@ -1061,6 +1073,8 @@ fn parseLibStub(
10611073 ctx: *ParseErrorCtx,
10621074) ParseError!void {
10631075 const gpa = self.base.comp.gpa;
1076 const target = self.base.comp.root_mod.resolved_target.result;
1077
10641078 var lib_stub = try LibStub.loadFromFile(gpa, file);
10651079 defer lib_stub.deinit();
10661080
......@@ -1068,7 +1082,7 @@ fn parseLibStub(
10681082
10691083 // Verify target
10701084 {
1071 var matcher = try Dylib.TargetMatcher.init(gpa, self.base.options.target);
1085 var matcher = try Dylib.TargetMatcher.init(gpa, target);
10721086 defer matcher.deinit();
10731087
10741088 const first_tbd = lib_stub.inner[0];
......@@ -1091,7 +1105,7 @@ fn parseLibStub(
10911105
10921106 try dylib.parseFromStub(
10931107 gpa,
1094 self.base.options.target,
1108 target,
10951109 lib_stub,
10961110 @intCast(self.dylibs.items.len), // TODO defer it till later
10971111 dependent_libs,
......@@ -1236,7 +1250,8 @@ pub fn writeAtom(self: *MachO, atom_index: Atom.Index, code: []u8) !void {
12361250
12371251fn writeToMemory(self: *MachO, task: std.os.darwin.MachTask, segment_index: u8, addr: u64, code: []const u8) !void {
12381252 const segment = self.segments.items[segment_index];
1239 const cpu_arch = self.base.options.target.cpu.arch;
1253 const target = self.base.comp.root_mod.resolved_target.result;
1254 const cpu_arch = target.cpu.arch;
12401255 const nwritten = if (!segment.isWriteable())
12411256 try task.writeMemProtected(addr, code, cpu_arch)
12421257 else
......@@ -1280,7 +1295,8 @@ fn writeStubHelperPreamble(self: *MachO) !void {
12801295 if (self.stub_helper_preamble_allocated) return;
12811296
12821297 const gpa = self.base.comp.gpa;
1283 const cpu_arch = self.base.options.target.cpu.arch;
1298 const target = self.base.comp.root_mod.resolved_target.result;
1299 const cpu_arch = target.cpu.arch;
12841300 const size = stubs.stubHelperPreambleSize(cpu_arch);
12851301
12861302 var buf = try std.ArrayList(u8).initCapacity(gpa, size);
......@@ -1306,11 +1322,12 @@ fn writeStubHelperPreamble(self: *MachO) !void {
13061322}
13071323
13081324fn writeStubTableEntry(self: *MachO, index: usize) !void {
1325 const target = self.base.comp.root_mod.resolved_target.result;
13091326 const stubs_sect_id = self.stubs_section_index.?;
13101327 const stub_helper_sect_id = self.stub_helper_section_index.?;
13111328 const laptr_sect_id = self.la_symbol_ptr_section_index.?;
13121329
1313 const cpu_arch = self.base.options.target.cpu.arch;
1330 const cpu_arch = target.cpu.arch;
13141331 const stub_entry_size = stubs.stubSize(cpu_arch);
13151332 const stub_helper_entry_size = stubs.stubHelperSize(cpu_arch);
13161333 const stub_helper_preamble_size = stubs.stubHelperPreambleSize(cpu_arch);
......@@ -2243,7 +2260,7 @@ pub fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void {
22432260 .flags = macho.S_SYMBOL_STUBS |
22442261 macho.S_ATTR_PURE_INSTRUCTIONS |
22452262 macho.S_ATTR_SOME_INSTRUCTIONS,
2246 .reserved2 = stubs.stubSize(self.base.options.target.cpu.arch),
2263 .reserved2 = stubs.stubSize(target.cpu.arch),
22472264 });
22482265 self.stub_helper_section_index = try self.initSection("__TEXT", "__stub_helper", .{
22492266 .flags = macho.S_REGULAR |
......@@ -3116,7 +3133,8 @@ fn populateMissingMetadata(self: *MachO) !void {
31163133 assert(self.mode == .incremental);
31173134
31183135 const gpa = self.base.comp.gpa;
3119 const cpu_arch = self.base.options.target.cpu.arch;
3136 const target = self.base.comp.root_mod.resolved_target.result;
3137 const cpu_arch = target.cpu.arch;
31203138 const pagezero_vmsize = self.calcPagezeroSize();
31213139
31223140 if (self.pagezero_segment_cmd_index == null) {
......@@ -3263,7 +3281,8 @@ fn populateMissingMetadata(self: *MachO) !void {
32633281
32643282fn calcPagezeroSize(self: *MachO) u64 {
32653283 const output_mode = self.base.comp.config.output_mode;
3266 const page_size = getPageSize(self.base.options.target.cpu.arch);
3284 const target = self.base.comp.root_mod.resolved_target.result;
3285 const page_size = getPageSize(target.cpu.arch);
32673286 const aligned_pagezero_vmsize = mem.alignBackward(u64, self.pagezero_vmsize, page_size);
32683287 if (output_mode == .Lib) return 0;
32693288 if (aligned_pagezero_vmsize == 0) return 0;
......@@ -3305,7 +3324,8 @@ fn allocateSection(self: *MachO, segname: []const u8, sectname: []const u8, opts
33053324 reserved2: u32 = 0,
33063325}) !u8 {
33073326 const gpa = self.base.comp.gpa;
3308 const page_size = getPageSize(self.base.options.target.cpu.arch);
3327 const target = self.base.comp.root_mod.resolved_target.result;
3328 const page_size = getPageSize(target.cpu.arch);
33093329 // In incremental context, we create one section per segment pairing. This way,
33103330 // we can move the segment in raw file as we please.
33113331 const segment_id = @as(u8, @intCast(self.segments.items.len));
......@@ -3360,7 +3380,8 @@ fn growSection(self: *MachO, sect_id: u8, needed_size: u64) !void {
33603380 const segment = &self.segments.items[segment_index];
33613381 const maybe_last_atom_index = self.sections.items(.last_atom_index)[sect_id];
33623382 const sect_capacity = self.allocatedSize(header.offset);
3363 const page_size = getPageSize(self.base.options.target.cpu.arch);
3383 const target = self.base.comp.root_mod.resolved_target.result;
3384 const page_size = getPageSize(target.cpu.arch);
33643385
33653386 if (needed_size > sect_capacity) {
33663387 const new_offset = self.findFreeSpace(needed_size, page_size);
......@@ -3400,7 +3421,8 @@ fn growSection(self: *MachO, sect_id: u8, needed_size: u64) !void {
34003421}
34013422
34023423fn growSectionVirtualMemory(self: *MachO, sect_id: u8, needed_size: u64) !void {
3403 const page_size = getPageSize(self.base.options.target.cpu.arch);
3424 const target = self.base.comp.root_mod.resolved_target.result;
3425 const page_size = getPageSize(target.cpu.arch);
34043426 const header = &self.sections.items(.header)[sect_id];
34053427 const segment = self.getSegmentPtr(sect_id);
34063428 const increased_size = padToIdeal(needed_size);
......@@ -3611,7 +3633,8 @@ pub fn writeSegmentHeaders(self: *MachO, writer: anytype) !void {
36113633}
36123634
36133635pub fn writeLinkeditSegmentData(self: *MachO) !void {
3614 const page_size = getPageSize(self.base.options.target.cpu.arch);
3636 const target = self.base.comp.root_mod.resolved_target.result;
3637 const page_size = getPageSize(target.cpu.arch);
36153638 const seg = self.getLinkeditSegmentPtr();
36163639 seg.filesize = 0;
36173640 seg.vmsize = 0;
......@@ -3698,7 +3721,8 @@ fn collectRebaseData(self: *MachO, rebase: *Rebase) !void {
36983721 }
36993722
37003723 // Finally, unpack the rest.
3701 const cpu_arch = self.base.options.target.cpu.arch;
3724 const target = self.base.comp.root_mod.resolved_target.result;
3725 const cpu_arch = target.cpu.arch;
37023726 for (self.objects.items) |*object| {
37033727 for (object.atoms.items) |atom_index| {
37043728 const atom = self.getAtom(atom_index);
......@@ -3744,14 +3768,14 @@ fn collectRebaseData(self: *MachO, rebase: *Rebase) !void {
37443768 },
37453769 else => unreachable,
37463770 }
3747 const target = Atom.parseRelocTarget(self, .{
3771 const reloc_target = Atom.parseRelocTarget(self, .{
37483772 .object_id = atom.getFile().?,
37493773 .rel = rel,
37503774 .code = code,
37513775 .base_offset = ctx.base_offset,
37523776 .base_addr = ctx.base_addr,
37533777 });
3754 const target_sym = self.getSymbol(target);
3778 const target_sym = self.getSymbol(reloc_target);
37553779 if (target_sym.undf()) continue;
37563780
37573781 const base_offset = @as(i32, @intCast(sym.n_value - segment.vmaddr));
......@@ -3853,7 +3877,8 @@ fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void {
38533877 }
38543878
38553879 // Finally, unpack the rest.
3856 const cpu_arch = self.base.options.target.cpu.arch;
3880 const target = self.base.comp.root_mod.resolved_target.result;
3881 const cpu_arch = target.cpu.arch;
38573882 for (self.objects.items) |*object| {
38583883 for (object.atoms.items) |atom_index| {
38593884 const atom = self.getAtom(atom_index);
......@@ -4072,7 +4097,8 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, lazy_bind: anytype) !void {
40724097
40734098 const header = self.sections.items(.header)[stub_helper_section_index];
40744099
4075 const cpu_arch = self.base.options.target.cpu.arch;
4100 const target = self.base.comp.root_mod.resolved_target.result;
4101 const cpu_arch = target.cpu.arch;
40764102 const preamble_size = stubs.stubHelperPreambleSize(cpu_arch);
40774103 const stub_size = stubs.stubHelperSize(cpu_arch);
40784104 const stub_offset = stubs.stubOffsetInStubHelper(cpu_arch);
......@@ -4708,13 +4734,14 @@ pub fn writeUuid(self: *MachO, comp: *const Compilation, uuid_cmd_offset: u32, h
47084734}
47094735
47104736pub fn writeCodeSignaturePadding(self: *MachO, code_sig: *CodeSignature) !void {
4737 const target = self.base.comp.root_mod.resolved_target.result;
47114738 const seg = self.getLinkeditSegmentPtr();
47124739 // Code signature data has to be 16-bytes aligned for Apple tools to recognize the file
47134740 // https://github.com/opensource-apple/cctools/blob/fdb4825f303fd5c0751be524babd32958181b3ed/libstuff/checkout.c#L271
47144741 const offset = mem.alignForward(u64, seg.fileoff + seg.filesize, 16);
47154742 const needed_size = code_sig.estimateSize(offset);
47164743 seg.filesize = offset + needed_size - seg.fileoff;
4717 seg.vmsize = mem.alignForward(u64, seg.filesize, getPageSize(self.base.options.target.cpu.arch));
4744 seg.vmsize = mem.alignForward(u64, seg.filesize, getPageSize(target.cpu.arch));
47184745 log.debug("writing code signature padding from 0x{x} to 0x{x}", .{ offset, offset + needed_size });
47194746 // Pad out the space. We need to do this to calculate valid hashes for everything in the file
47204747 // except for code signature data.
......@@ -4758,7 +4785,8 @@ pub fn writeHeader(self: *MachO, ncmds: u32, sizeofcmds: u32) !void {
47584785 var header: macho.mach_header_64 = .{};
47594786 header.flags = macho.MH_NOUNDEFS | macho.MH_DYLDLINK | macho.MH_PIE | macho.MH_TWOLEVEL;
47604787
4761 switch (self.base.options.target.cpu.arch) {
4788 const target = self.base.comp.root_mod.resolved_target.result;
4789 switch (target.cpu.arch) {
47624790 .aarch64 => {
47634791 header.cputype = macho.CPU_TYPE_ARM64;
47644792 header.cpusubtype = macho.CPU_SUBTYPE_ARM_ALL;
......@@ -5123,9 +5151,10 @@ pub fn getTlvPtrEntryAddress(self: *MachO, sym_with_loc: SymbolWithLoc) ?u64 {
51235151}
51245152
51255153pub fn getStubsEntryAddress(self: *MachO, sym_with_loc: SymbolWithLoc) ?u64 {
5154 const target = self.base.comp.root_mod.resolved_target.result;
51265155 const index = self.stub_table.lookup.get(sym_with_loc) orelse return null;
51275156 const header = self.sections.items(.header)[self.stubs_section_index.?];
5128 return header.addr + stubs.stubSize(self.base.options.target.cpu.arch) * index;
5157 return header.addr + stubs.stubSize(target.cpu.arch) * index;
51295158}
51305159
51315160/// Returns symbol location corresponding to the set entrypoint if any.
......@@ -5234,8 +5263,9 @@ pub fn handleAndReportParseError(
52345263 err: ParseError,
52355264 ctx: *const ParseErrorCtx,
52365265) error{OutOfMemory}!void {
5266 const target = self.base.comp.root_mod.resolved_target.result;
52375267 const gpa = self.base.comp.gpa;
5238 const cpu_arch = self.base.options.target.cpu.arch;
5268 const cpu_arch = target.cpu.arch;
52395269 switch (err) {
52405270 error.DylibAlreadyExists => {},
52415271 error.IncompatibleDylibVersion => {
......@@ -5270,7 +5300,7 @@ pub fn handleAndReportParseError(
52705300 error.InvalidTarget => try self.reportParseError(
52715301 path,
52725302 "invalid target: expected '{}', but found '{s}'",
5273 .{ Platform.fromTarget(self.base.options.target).fmtTarget(cpu_arch), targets_string.items },
5303 .{ Platform.fromTarget(target).fmtTarget(cpu_arch), targets_string.items },
52745304 ),
52755305 error.InvalidTargetFatLibrary => try self.reportParseError(
52765306 path,
src/link/MachO/Atom.zig+17-11
......@@ -387,7 +387,8 @@ pub fn calcInnerSymbolOffset(macho_file: *MachO, atom_index: Index, sym_index: u
387387}
388388
389389pub fn scanAtomRelocs(macho_file: *MachO, atom_index: Index, relocs: []align(1) const macho.relocation_info) !void {
390 const arch = macho_file.base.options.target.cpu.arch;
390 const target = macho_file.base.comp.root_mod.resolved_target.result;
391 const arch = target.cpu.arch;
391392 const atom = macho_file.getAtom(atom_index);
392393 assert(atom.getFile() != null); // synthetic atoms do not have relocs
393394
......@@ -434,6 +435,7 @@ pub fn parseRelocTarget(macho_file: *MachO, ctx: struct {
434435 const tracy = trace(@src());
435436 defer tracy.end();
436437
438 const target = macho_file.base.comp.root_mod.resolved_target.result;
437439 const object = &macho_file.objects.items[ctx.object_id];
438440 log.debug("parsing reloc target in object({d}) '{s}' ", .{ ctx.object_id, object.name });
439441
......@@ -447,7 +449,7 @@ pub fn parseRelocTarget(macho_file: *MachO, ctx: struct {
447449 else
448450 mem.readInt(u32, ctx.code[rel_offset..][0..4], .little);
449451 } else blk: {
450 assert(macho_file.base.options.target.cpu.arch == .x86_64);
452 assert(target.cpu.arch == .x86_64);
451453 const correction: u3 = switch (@as(macho.reloc_type_x86_64, @enumFromInt(ctx.rel.r_type))) {
452454 .X86_64_RELOC_SIGNED => 0,
453455 .X86_64_RELOC_SIGNED_1 => 1,
......@@ -467,18 +469,18 @@ pub fn parseRelocTarget(macho_file: *MachO, ctx: struct {
467469
468470 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = ctx.object_id + 1 };
469471 const sym = macho_file.getSymbol(sym_loc);
470 const target = if (sym.sect() and !sym.ext())
472 const reloc_target = if (sym.sect() and !sym.ext())
471473 sym_loc
472474 else if (object.getGlobal(sym_index)) |global_index|
473475 macho_file.globals.items[global_index]
474476 else
475477 sym_loc;
476478 log.debug(" | target %{d} ('{s}') in object({?d})", .{
477 target.sym_index,
478 macho_file.getSymbolName(target),
479 target.getFile(),
479 reloc_target.sym_index,
480 macho_file.getSymbolName(reloc_target),
481 reloc_target.getFile(),
480482 });
481 return target;
483 return reloc_target;
482484}
483485
484486pub fn getRelocTargetAtomIndex(macho_file: *MachO, target: SymbolWithLoc) ?Index {
......@@ -599,7 +601,8 @@ pub fn resolveRelocs(
599601 atom_code: []u8,
600602 atom_relocs: []align(1) const macho.relocation_info,
601603) !void {
602 const arch = macho_file.base.options.target.cpu.arch;
604 const target = macho_file.base.comp.root_mod.resolved_target.result;
605 const arch = target.cpu.arch;
603606 const atom = macho_file.getAtom(atom_index);
604607 assert(atom.getFile() != null); // synthetic atoms do not have relocs
605608
......@@ -1192,7 +1195,8 @@ pub fn getAtomRelocs(macho_file: *MachO, atom_index: Index) []const macho.reloca
11921195}
11931196
11941197pub fn relocRequiresGot(macho_file: *MachO, rel: macho.relocation_info) bool {
1195 switch (macho_file.base.options.target.cpu.arch) {
1198 const target = macho_file.base.comp.root_mod.resolved_target.result;
1199 switch (target.cpu.arch) {
11961200 .aarch64 => switch (@as(macho.reloc_type_arm64, @enumFromInt(rel.r_type))) {
11971201 .ARM64_RELOC_GOT_LOAD_PAGE21,
11981202 .ARM64_RELOC_GOT_LOAD_PAGEOFF12,
......@@ -1211,7 +1215,8 @@ pub fn relocRequiresGot(macho_file: *MachO, rel: macho.relocation_info) bool {
12111215}
12121216
12131217pub fn relocIsTlv(macho_file: *MachO, rel: macho.relocation_info) bool {
1214 switch (macho_file.base.options.target.cpu.arch) {
1218 const target = macho_file.base.comp.root_mod.resolved_target.result;
1219 switch (target.cpu.arch) {
12151220 .aarch64 => switch (@as(macho.reloc_type_arm64, @enumFromInt(rel.r_type))) {
12161221 .ARM64_RELOC_TLVP_LOAD_PAGE21,
12171222 .ARM64_RELOC_TLVP_LOAD_PAGEOFF12,
......@@ -1227,7 +1232,8 @@ pub fn relocIsTlv(macho_file: *MachO, rel: macho.relocation_info) bool {
12271232}
12281233
12291234pub fn relocIsStub(macho_file: *MachO, rel: macho.relocation_info) bool {
1230 switch (macho_file.base.options.target.cpu.arch) {
1235 const target = macho_file.base.comp.root_mod.resolved_target.result;
1236 switch (target.cpu.arch) {
12311237 .aarch64 => switch (@as(macho.reloc_type_arm64, @enumFromInt(rel.r_type))) {
12321238 .ARM64_RELOC_BRANCH26 => return true,
12331239 else => return false,
src/link/MachO/DebugSymbols.zig+10-4
......@@ -39,10 +39,12 @@ pub const Reloc = struct {
3939/// You must call this function *after* `MachO.populateMissingMetadata()`
4040/// has been called to get a viable debug symbols output.
4141pub fn populateMissingMetadata(self: *DebugSymbols, macho_file: *MachO) !void {
42 const target = macho_file.base.comp.root_mod.resolved_target.result;
43
4244 if (self.dwarf_segment_cmd_index == null) {
4345 self.dwarf_segment_cmd_index = @as(u8, @intCast(self.segments.items.len));
4446
45 const page_size = MachO.getPageSize(macho_file.base.options.target.cpu.arch);
47 const page_size = MachO.getPageSize(target.cpu.arch);
4648 const off = @as(u64, @intCast(page_size));
4749 const ideal_size: u16 = 200 + 128 + 160 + 250;
4850 const needed_size = mem.alignForward(u64, padToIdeal(ideal_size), page_size);
......@@ -332,7 +334,8 @@ fn finalizeDwarfSegment(self: *DebugSymbols, macho_file: *MachO) void {
332334 file_size = @max(file_size, header.offset + header.size);
333335 }
334336
335 const page_size = MachO.getPageSize(macho_file.base.options.target.cpu.arch);
337 const target = macho_file.base.comp.root_mod.resolved_target.result;
338 const page_size = MachO.getPageSize(target.cpu.arch);
336339 const aligned_size = mem.alignForward(u64, file_size, page_size);
337340 dwarf_segment.vmaddr = base_vmaddr;
338341 dwarf_segment.filesize = aligned_size;
......@@ -394,10 +397,12 @@ fn writeSegmentHeaders(self: *DebugSymbols, macho_file: *MachO, writer: anytype)
394397}
395398
396399fn writeHeader(self: *DebugSymbols, macho_file: *MachO, ncmds: u32, sizeofcmds: u32) !void {
400 const target = macho_file.base.comp.root_mod.resolved_target.result;
401
397402 var header: macho.mach_header_64 = .{};
398403 header.filetype = macho.MH_DSYM;
399404
400 switch (macho_file.base.options.target.cpu.arch) {
405 switch (target.cpu.arch) {
401406 .aarch64 => {
402407 header.cputype = macho.CPU_TYPE_ARM64;
403408 header.cpusubtype = macho.CPU_SUBTYPE_ARM_ALL;
......@@ -435,7 +440,8 @@ fn writeLinkeditSegmentData(self: *DebugSymbols, macho_file: *MachO) !void {
435440 try self.writeSymtab(macho_file);
436441 try self.writeStrtab();
437442
438 const page_size = MachO.getPageSize(macho_file.base.options.target.cpu.arch);
443 const target = macho_file.base.comp.root_mod.resolved_target.result;
444 const page_size = MachO.getPageSize(target.cpu.arch);
439445 const seg = &self.segments.items[self.linkedit_segment_cmd_index.?];
440446 const aligned_size = mem.alignForward(u64, seg.filesize, page_size);
441447 seg.vmsize = aligned_size;
src/link/MachO/Object.zig+18-15
......@@ -355,6 +355,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) SplitIn
355355/// into subsections where each subsection then represents an Atom.
356356pub fn splitRegularSections(self: *Object, macho_file: *MachO, object_id: u32) !void {
357357 const gpa = macho_file.base.allocator;
358 const target = macho_file.base.comp.root_mod.resolved_target.result;
358359
359360 const sections = self.getSourceSections();
360361 for (sections, 0..) |sect, id| {
......@@ -448,7 +449,7 @@ pub fn splitRegularSections(self: *Object, macho_file: *MachO, object_id: u32) !
448449
449450 try self.parseRelocs(gpa, section.id);
450451
451 const cpu_arch = macho_file.base.options.target.cpu.arch;
452 const cpu_arch = target.cpu.arch;
452453 const sect_loc = filterSymbolsBySection(symtab[sect_sym_index..], sect_id + 1);
453454 const sect_start_index = sect_sym_index + sect_loc.index;
454455
......@@ -676,7 +677,8 @@ fn parseEhFrameSection(self: *Object, macho_file: *MachO, object_id: u32) !void
676677 macho_file.eh_frame_section_index = try macho_file.initSection("__TEXT", "__eh_frame", .{});
677678 }
678679
679 const cpu_arch = macho_file.base.options.target.cpu.arch;
680 const target = macho_file.base.comp.root_mod.resolved_target.result;
681 const cpu_arch = target.cpu.arch;
680682 try self.parseRelocs(gpa, sect_id);
681683 const relocs = self.getRelocs(sect_id);
682684
......@@ -704,7 +706,7 @@ fn parseEhFrameSection(self: *Object, macho_file: *MachO, object_id: u32) !void
704706 });
705707
706708 if (record.tag == .fde) {
707 const target = blk: {
709 const reloc_target = blk: {
708710 switch (cpu_arch) {
709711 .aarch64 => {
710712 assert(rel_pos.len > 0); // TODO convert to an error as the FDE eh frame is malformed
......@@ -714,13 +716,13 @@ fn parseEhFrameSection(self: *Object, macho_file: *MachO, object_id: u32) !void
714716 @as(macho.reloc_type_arm64, @enumFromInt(rel.r_type)) == .ARM64_RELOC_UNSIGNED)
715717 break rel;
716718 } else unreachable;
717 const target = Atom.parseRelocTarget(macho_file, .{
719 const reloc_target = Atom.parseRelocTarget(macho_file, .{
718720 .object_id = object_id,
719721 .rel = rel,
720722 .code = it.data[offset..],
721723 .base_offset = @as(i32, @intCast(offset)),
722724 });
723 break :blk target;
725 break :blk reloc_target;
724726 },
725727 .x86_64 => {
726728 const target_address = record.getTargetSymbolAddress(.{
......@@ -728,16 +730,16 @@ fn parseEhFrameSection(self: *Object, macho_file: *MachO, object_id: u32) !void
728730 .base_offset = offset,
729731 });
730732 const target_sym_index = self.getSymbolByAddress(target_address, null);
731 const target = if (self.getGlobal(target_sym_index)) |global_index|
733 const reloc_target = if (self.getGlobal(target_sym_index)) |global_index|
732734 macho_file.globals.items[global_index]
733735 else
734736 SymbolWithLoc{ .sym_index = target_sym_index, .file = object_id + 1 };
735 break :blk target;
737 break :blk reloc_target;
736738 },
737739 else => unreachable,
738740 }
739741 };
740 if (target.getFile() != object_id) {
742 if (reloc_target.getFile() != object_id) {
741743 log.debug("FDE at offset {x} marked DEAD", .{offset});
742744 self.eh_frame_relocs_lookup.getPtr(offset).?.dead = true;
743745 } else {
......@@ -746,12 +748,12 @@ fn parseEhFrameSection(self: *Object, macho_file: *MachO, object_id: u32) !void
746748 // very problematic when using Zig's @export feature to re-export symbols under
747749 // additional names. For that reason, we need to ensure we record aliases here
748750 // too so that we can tie them with their matching unwind records and vice versa.
749 const aliases = self.getSymbolAliases(target.sym_index);
751 const aliases = self.getSymbolAliases(reloc_target.sym_index);
750752 var i: u32 = 0;
751753 while (i < aliases.len) : (i += 1) {
752754 const actual_target = SymbolWithLoc{
753755 .sym_index = i + aliases.start,
754 .file = target.file,
756 .file = reloc_target.file,
755757 };
756758 log.debug("FDE at offset {x} tracks {s}", .{
757759 offset,
......@@ -766,7 +768,8 @@ fn parseEhFrameSection(self: *Object, macho_file: *MachO, object_id: u32) !void
766768
767769fn parseUnwindInfo(self: *Object, macho_file: *MachO, object_id: u32) !void {
768770 const gpa = macho_file.base.allocator;
769 const cpu_arch = macho_file.base.options.target.cpu.arch;
771 const target = macho_file.base.comp.root_mod.resolved_target.result;
772 const cpu_arch = target.cpu.arch;
770773 const sect_id = self.unwind_info_sect_id orelse {
771774 // If it so happens that the object had `__eh_frame` section defined but no `__compact_unwind`,
772775 // we will try fully synthesising unwind info records to somewhat match Apple ld's
......@@ -818,13 +821,13 @@ fn parseUnwindInfo(self: *Object, macho_file: *MachO, object_id: u32) !void {
818821
819822 // Find function symbol that this record describes
820823 const rel = relocs[rel_pos.start..][rel_pos.len - 1];
821 const target = Atom.parseRelocTarget(macho_file, .{
824 const reloc_target = Atom.parseRelocTarget(macho_file, .{
822825 .object_id = object_id,
823826 .rel = rel,
824827 .code = mem.asBytes(&record),
825828 .base_offset = @as(i32, @intCast(offset)),
826829 });
827 if (target.getFile() != object_id) {
830 if (reloc_target.getFile() != object_id) {
828831 log.debug("unwind record {d} marked DEAD", .{record_id});
829832 self.unwind_relocs_lookup[record_id].dead = true;
830833 } else {
......@@ -833,12 +836,12 @@ fn parseUnwindInfo(self: *Object, macho_file: *MachO, object_id: u32) !void {
833836 // very problematic when using Zig's @export feature to re-export symbols under
834837 // additional names. For that reason, we need to ensure we record aliases here
835838 // too so that we can tie them with their matching unwind records and vice versa.
836 const aliases = self.getSymbolAliases(target.sym_index);
839 const aliases = self.getSymbolAliases(reloc_target.sym_index);
837840 var i: u32 = 0;
838841 while (i < aliases.len) : (i += 1) {
839842 const actual_target = SymbolWithLoc{
840843 .sym_index = i + aliases.start,
841 .file = target.file,
844 .file = reloc_target.file,
842845 };
843846 log.debug("unwind record {d} tracks {s}", .{
844847 record_id,
src/link/MachO/Relocation.zig+4-2
......@@ -58,11 +58,12 @@ pub fn isStubTrampoline(self: Relocation, macho_file: *MachO) bool {
5858}
5959
6060pub fn getTargetBaseAddress(self: Relocation, macho_file: *MachO) ?u64 {
61 const target = macho_file.base.comp.root_mod.resolved_target.result;
6162 if (self.isStubTrampoline(macho_file)) {
6263 const index = macho_file.stub_table.lookup.get(self.target) orelse return null;
6364 const header = macho_file.sections.items(.header)[macho_file.stubs_section_index.?];
6465 return header.addr +
65 index * @import("stubs.zig").stubSize(macho_file.base.options.target.cpu.arch);
66 index * @import("stubs.zig").stubSize(target.cpu.arch);
6667 }
6768 switch (self.type) {
6869 .got, .got_page, .got_pageoff => {
......@@ -84,7 +85,8 @@ pub fn getTargetBaseAddress(self: Relocation, macho_file: *MachO) ?u64 {
8485}
8586
8687pub fn resolve(self: Relocation, macho_file: *MachO, atom_index: Atom.Index, code: []u8) void {
87 const arch = macho_file.base.options.target.cpu.arch;
88 const target = macho_file.base.comp.root_mod.resolved_target.result;
89 const arch = target.cpu.arch;
8890 const atom = macho_file.getAtom(atom_index);
8991 const source_sym = atom.getSymbol(macho_file);
9092 const source_addr = source_sym.n_value + self.offset;
src/link/MachO/UnwindInfo.zig+16-13
......@@ -184,7 +184,8 @@ pub fn deinit(info: *UnwindInfo) void {
184184pub fn scanRelocs(macho_file: *MachO) !void {
185185 if (macho_file.unwind_info_section_index == null) return;
186186
187 const cpu_arch = macho_file.base.options.target.cpu.arch;
187 const target = macho_file.base.comp.root_mod.resolved_target.result;
188 const cpu_arch = target.cpu.arch;
188189 for (macho_file.objects.items, 0..) |*object, object_id| {
189190 const unwind_records = object.getUnwindRecords();
190191 for (object.exec_atoms.items) |atom_index| {
......@@ -196,13 +197,13 @@ pub fn scanRelocs(macho_file: *MachO) !void {
196197 if (!UnwindEncoding.isDwarf(record.compactUnwindEncoding, cpu_arch)) {
197198 if (getPersonalityFunctionReloc(macho_file, @as(u32, @intCast(object_id)), record_id)) |rel| {
198199 // Personality function; add GOT pointer.
199 const target = Atom.parseRelocTarget(macho_file, .{
200 const reloc_target = Atom.parseRelocTarget(macho_file, .{
200201 .object_id = @as(u32, @intCast(object_id)),
201202 .rel = rel,
202203 .code = mem.asBytes(&record),
203204 .base_offset = @as(i32, @intCast(record_id * @sizeOf(macho.compact_unwind_entry))),
204205 });
205 try macho_file.addGotEntry(target);
206 try macho_file.addGotEntry(reloc_target);
206207 }
207208 }
208209 }
......@@ -213,7 +214,8 @@ pub fn scanRelocs(macho_file: *MachO) !void {
213214pub fn collect(info: *UnwindInfo, macho_file: *MachO) !void {
214215 if (macho_file.unwind_info_section_index == null) return;
215216
216 const cpu_arch = macho_file.base.options.target.cpu.arch;
217 const target = macho_file.base.comp.root_mod.resolved_target.result;
218 const cpu_arch = target.cpu.arch;
217219
218220 var records = std.ArrayList(macho.compact_unwind_entry).init(info.gpa);
219221 defer records.deinit();
......@@ -247,15 +249,15 @@ pub fn collect(info: *UnwindInfo, macho_file: *MachO) !void {
247249 @as(u32, @intCast(object_id)),
248250 record_id,
249251 )) |rel| {
250 const target = Atom.parseRelocTarget(macho_file, .{
252 const reloc_target = Atom.parseRelocTarget(macho_file, .{
251253 .object_id = @as(u32, @intCast(object_id)),
252254 .rel = rel,
253255 .code = mem.asBytes(&record),
254256 .base_offset = @as(i32, @intCast(record_id * @sizeOf(macho.compact_unwind_entry))),
255257 });
256 const personality_index = info.getPersonalityFunction(target) orelse inner: {
258 const personality_index = info.getPersonalityFunction(reloc_target) orelse inner: {
257259 const personality_index = info.personalities_count;
258 info.personalities[personality_index] = target;
260 info.personalities[personality_index] = reloc_target;
259261 info.personalities_count += 1;
260262 break :inner personality_index;
261263 };
......@@ -265,13 +267,13 @@ pub fn collect(info: *UnwindInfo, macho_file: *MachO) !void {
265267 }
266268
267269 if (getLsdaReloc(macho_file, @as(u32, @intCast(object_id)), record_id)) |rel| {
268 const target = Atom.parseRelocTarget(macho_file, .{
270 const reloc_target = Atom.parseRelocTarget(macho_file, .{
269271 .object_id = @as(u32, @intCast(object_id)),
270272 .rel = rel,
271273 .code = mem.asBytes(&record),
272274 .base_offset = @as(i32, @intCast(record_id * @sizeOf(macho.compact_unwind_entry))),
273275 });
274 record.lsda = @as(u64, @bitCast(target));
276 record.lsda = @as(u64, @bitCast(reloc_target));
275277 }
276278 }
277279 break :blk record;
......@@ -557,13 +559,14 @@ pub fn write(info: *UnwindInfo, macho_file: *MachO) !void {
557559 const text_sect = macho_file.sections.items(.header)[text_sect_id];
558560
559561 var personalities: [max_personalities]u32 = undefined;
560 const cpu_arch = macho_file.base.options.target.cpu.arch;
562 const target = macho_file.base.comp.root_mod.resolved_target.result;
563 const cpu_arch = target.cpu.arch;
561564
562565 log.debug("Personalities:", .{});
563 for (info.personalities[0..info.personalities_count], 0..) |target, i| {
564 const addr = macho_file.getGotEntryAddress(target).?;
566 for (info.personalities[0..info.personalities_count], 0..) |reloc_target, i| {
567 const addr = macho_file.getGotEntryAddress(reloc_target).?;
565568 personalities[i] = @as(u32, @intCast(addr - seg.vmaddr));
566 log.debug(" {d}: 0x{x} ({s})", .{ i, personalities[i], macho_file.getSymbolName(target) });
569 log.debug(" {d}: 0x{x} ({s})", .{ i, personalities[i], macho_file.getSymbolName(reloc_target) });
567570 }
568571
569572 for (info.records.items) |*rec| {
src/link/MachO/dead_strip.zig+33-29
......@@ -118,7 +118,8 @@ fn markLive(macho_file: *MachO, atom_index: Atom.Index, alive: *AtomTable) void
118118
119119 alive.putAssumeCapacityNoClobber(atom_index, {});
120120
121 const cpu_arch = macho_file.base.options.target.cpu.arch;
121 const target = macho_file.base.comp.root_mod.resolved_target.result;
122 const cpu_arch = target.cpu.arch;
122123
123124 const sym = macho_file.getSymbol(atom.getSymbolWithLoc());
124125 const header = macho_file.sections.items(.header)[sym.n_sect - 1];
......@@ -129,7 +130,7 @@ fn markLive(macho_file: *MachO, atom_index: Atom.Index, alive: *AtomTable) void
129130 const ctx = Atom.getRelocContext(macho_file, atom_index);
130131
131132 for (relocs) |rel| {
132 const target = switch (cpu_arch) {
133 const reloc_target = switch (cpu_arch) {
133134 .aarch64 => switch (@as(macho.reloc_type_arm64, @enumFromInt(rel.r_type))) {
134135 .ARM64_RELOC_ADDEND => continue,
135136 else => Atom.parseRelocTarget(macho_file, .{
......@@ -149,19 +150,19 @@ fn markLive(macho_file: *MachO, atom_index: Atom.Index, alive: *AtomTable) void
149150 }),
150151 else => unreachable,
151152 };
152 const target_sym = macho_file.getSymbol(target);
153 const target_sym = macho_file.getSymbol(reloc_target);
153154
154155 if (target_sym.undf()) continue;
155 if (target.getFile() == null) {
156 const target_sym_name = macho_file.getSymbolName(target);
156 if (reloc_target.getFile() == null) {
157 const target_sym_name = macho_file.getSymbolName(reloc_target);
157158 if (mem.eql(u8, "__mh_execute_header", target_sym_name)) continue;
158159 if (mem.eql(u8, "___dso_handle", target_sym_name)) continue;
159160
160161 unreachable; // referenced symbol not found
161162 }
162163
163 const object = macho_file.objects.items[target.getFile().?];
164 const target_atom_index = object.getAtomIndexForSymbol(target.sym_index).?;
164 const object = macho_file.objects.items[reloc_target.getFile().?];
165 const target_atom_index = object.getAtomIndexForSymbol(reloc_target.sym_index).?;
165166 log.debug(" following ATOM({d}, %{d}, {?d})", .{
166167 target_atom_index,
167168 macho_file.getAtom(target_atom_index).sym_index,
......@@ -178,7 +179,8 @@ fn refersLive(macho_file: *MachO, atom_index: Atom.Index, alive: AtomTable) bool
178179
179180 log.debug("refersLive(ATOM({d}, %{d}, {?d}))", .{ atom_index, sym_loc.sym_index, sym_loc.getFile() });
180181
181 const cpu_arch = macho_file.base.options.target.cpu.arch;
182 const target = macho_file.base.comp.root_mod.resolved_target.result;
183 const cpu_arch = target.cpu.arch;
182184
183185 const sym = macho_file.getSymbol(sym_loc);
184186 const header = macho_file.sections.items(.header)[sym.n_sect - 1];
......@@ -189,7 +191,7 @@ fn refersLive(macho_file: *MachO, atom_index: Atom.Index, alive: AtomTable) bool
189191 const ctx = Atom.getRelocContext(macho_file, atom_index);
190192
191193 for (relocs) |rel| {
192 const target = switch (cpu_arch) {
194 const reloc_target = switch (cpu_arch) {
193195 .aarch64 => switch (@as(macho.reloc_type_arm64, @enumFromInt(rel.r_type))) {
194196 .ARM64_RELOC_ADDEND => continue,
195197 else => Atom.parseRelocTarget(macho_file, .{
......@@ -210,9 +212,9 @@ fn refersLive(macho_file: *MachO, atom_index: Atom.Index, alive: AtomTable) bool
210212 else => unreachable,
211213 };
212214
213 const object = macho_file.objects.items[target.getFile().?];
214 const target_atom_index = object.getAtomIndexForSymbol(target.sym_index) orelse {
215 log.debug("atom for symbol '{s}' not found; skipping...", .{macho_file.getSymbolName(target)});
215 const object = macho_file.objects.items[reloc_target.getFile().?];
216 const target_atom_index = object.getAtomIndexForSymbol(reloc_target.sym_index) orelse {
217 log.debug("atom for symbol '{s}' not found; skipping...", .{macho_file.getSymbolName(reloc_target)});
216218 continue;
217219 };
218220 if (alive.contains(target_atom_index)) {
......@@ -271,7 +273,8 @@ fn mark(macho_file: *MachO, roots: AtomTable, alive: *AtomTable) void {
271273
272274fn markUnwindRecords(macho_file: *MachO, object_id: u32, alive: *AtomTable) void {
273275 const object = &macho_file.objects.items[object_id];
274 const cpu_arch = macho_file.base.options.target.cpu.arch;
276 const target = macho_file.base.comp.root_mod.resolved_target.result;
277 const cpu_arch = target.cpu.arch;
275278
276279 const unwind_records = object.getUnwindRecords();
277280
......@@ -310,29 +313,29 @@ fn markUnwindRecords(macho_file: *MachO, object_id: u32, alive: *AtomTable) void
310313 markEhFrameRecords(macho_file, object_id, atom_index, alive);
311314 } else {
312315 if (UnwindInfo.getPersonalityFunctionReloc(macho_file, object_id, record_id)) |rel| {
313 const target = Atom.parseRelocTarget(macho_file, .{
316 const reloc_target = Atom.parseRelocTarget(macho_file, .{
314317 .object_id = object_id,
315318 .rel = rel,
316319 .code = mem.asBytes(&record),
317320 .base_offset = @as(i32, @intCast(record_id * @sizeOf(macho.compact_unwind_entry))),
318321 });
319 const target_sym = macho_file.getSymbol(target);
322 const target_sym = macho_file.getSymbol(reloc_target);
320323 if (!target_sym.undf()) {
321 const target_object = macho_file.objects.items[target.getFile().?];
322 const target_atom_index = target_object.getAtomIndexForSymbol(target.sym_index).?;
324 const target_object = macho_file.objects.items[reloc_target.getFile().?];
325 const target_atom_index = target_object.getAtomIndexForSymbol(reloc_target.sym_index).?;
323326 markLive(macho_file, target_atom_index, alive);
324327 }
325328 }
326329
327330 if (UnwindInfo.getLsdaReloc(macho_file, object_id, record_id)) |rel| {
328 const target = Atom.parseRelocTarget(macho_file, .{
331 const reloc_target = Atom.parseRelocTarget(macho_file, .{
329332 .object_id = object_id,
330333 .rel = rel,
331334 .code = mem.asBytes(&record),
332335 .base_offset = @as(i32, @intCast(record_id * @sizeOf(macho.compact_unwind_entry))),
333336 });
334 const target_object = macho_file.objects.items[target.getFile().?];
335 const target_atom_index = target_object.getAtomIndexForSymbol(target.sym_index).?;
337 const target_object = macho_file.objects.items[reloc_target.getFile().?];
338 const target_atom_index = target_object.getAtomIndexForSymbol(reloc_target.sym_index).?;
336339 markLive(macho_file, target_atom_index, alive);
337340 }
338341 }
......@@ -341,7 +344,8 @@ fn markUnwindRecords(macho_file: *MachO, object_id: u32, alive: *AtomTable) void
341344}
342345
343346fn markEhFrameRecords(macho_file: *MachO, object_id: u32, atom_index: Atom.Index, alive: *AtomTable) void {
344 const cpu_arch = macho_file.base.options.target.cpu.arch;
347 const target = macho_file.base.comp.root_mod.resolved_target.result;
348 const cpu_arch = target.cpu.arch;
345349 const object = &macho_file.objects.items[object_id];
346350 var it = object.getEhFrameRecordsIterator();
347351 var inner_syms_it = Atom.getInnerSymbolsIterator(macho_file, atom_index);
......@@ -361,16 +365,16 @@ fn markEhFrameRecords(macho_file: *MachO, object_id: u32, atom_index: Atom.Index
361365 // Mark FDE references which should include any referenced LSDA record
362366 const relocs = eh_frame.getRelocs(macho_file, object_id, fde_offset);
363367 for (relocs) |rel| {
364 const target = Atom.parseRelocTarget(macho_file, .{
368 const reloc_target = Atom.parseRelocTarget(macho_file, .{
365369 .object_id = object_id,
366370 .rel = rel,
367371 .code = fde.data,
368372 .base_offset = @as(i32, @intCast(fde_offset)) + 4,
369373 });
370 const target_sym = macho_file.getSymbol(target);
374 const target_sym = macho_file.getSymbol(reloc_target);
371375 if (!target_sym.undf()) blk: {
372 const target_object = macho_file.objects.items[target.getFile().?];
373 const target_atom_index = target_object.getAtomIndexForSymbol(target.sym_index) orelse
376 const target_object = macho_file.objects.items[reloc_target.getFile().?];
377 const target_atom_index = target_object.getAtomIndexForSymbol(reloc_target.sym_index) orelse
374378 break :blk;
375379 markLive(macho_file, target_atom_index, alive);
376380 }
......@@ -394,11 +398,11 @@ fn markEhFrameRecords(macho_file: *MachO, object_id: u32, atom_index: Atom.Index
394398
395399 // Mark CIE references which should include any referenced personalities
396400 // that are defined locally.
397 if (cie.getPersonalityPointerReloc(macho_file, object_id, cie_offset)) |target| {
398 const target_sym = macho_file.getSymbol(target);
401 if (cie.getPersonalityPointerReloc(macho_file, object_id, cie_offset)) |reloc_target| {
402 const target_sym = macho_file.getSymbol(reloc_target);
399403 if (!target_sym.undf()) {
400 const target_object = macho_file.objects.items[target.getFile().?];
401 const target_atom_index = target_object.getAtomIndexForSymbol(target.sym_index).?;
404 const target_object = macho_file.objects.items[reloc_target.getFile().?];
405 const target_atom_index = target_object.getAtomIndexForSymbol(reloc_target.sym_index).?;
402406 markLive(macho_file, target_atom_index, alive);
403407 }
404408 }
src/link/MachO/eh_frame.zig+20-15
......@@ -35,7 +35,8 @@ pub fn calcSectionSize(macho_file: *MachO, unwind_info: *const UnwindInfo) error
3535 sect.@"align" = 3;
3636 sect.size = 0;
3737
38 const cpu_arch = macho_file.base.options.target.cpu.arch;
38 const target = macho_file.base.comp.root_mod.resolved_target.result;
39 const cpu_arch = target.cpu.arch;
3940 const gpa = macho_file.base.allocator;
4041 var size: u32 = 0;
4142
......@@ -86,7 +87,8 @@ pub fn write(macho_file: *MachO, unwind_info: *UnwindInfo) !void {
8687 const seg_id = macho_file.sections.items(.segment_index)[sect_id];
8788 const seg = macho_file.segments.items[seg_id];
8889
89 const cpu_arch = macho_file.base.options.target.cpu.arch;
90 const target = macho_file.base.comp.root_mod.resolved_target.result;
91 const cpu_arch = target.cpu.arch;
9092 const gpa = macho_file.base.allocator;
9193
9294 var eh_records = std.AutoArrayHashMap(u32, EhFrameRecord(true)).init(gpa);
......@@ -109,11 +111,11 @@ pub fn write(macho_file: *MachO, unwind_info: *UnwindInfo) !void {
109111
110112 for (object.exec_atoms.items) |atom_index| {
111113 var inner_syms_it = Atom.getInnerSymbolsIterator(macho_file, atom_index);
112 while (inner_syms_it.next()) |target| {
113 const fde_record_offset = object.eh_frame_records_lookup.get(target) orelse continue;
114 while (inner_syms_it.next()) |reloc_target| {
115 const fde_record_offset = object.eh_frame_records_lookup.get(reloc_target) orelse continue;
114116 if (object.eh_frame_relocs_lookup.get(fde_record_offset).?.dead) continue;
115117
116 const record_id = unwind_info.records_lookup.get(target) orelse continue;
118 const record_id = unwind_info.records_lookup.get(reloc_target) orelse continue;
117119 const record = &unwind_info.records.items[record_id];
118120
119121 // TODO skip this check if no __compact_unwind is present
......@@ -153,7 +155,7 @@ pub fn write(macho_file: *MachO, unwind_info: *UnwindInfo) !void {
153155 .aarch64 => {}, // relocs take care of LSDA pointers
154156 .x86_64 => {
155157 // We need to relocate target symbol address ourselves.
156 const atom_sym = macho_file.getSymbol(target);
158 const atom_sym = macho_file.getSymbol(reloc_target);
157159 try fde_record.setTargetSymbolAddress(atom_sym.n_value, .{
158160 .base_addr = sect.addr,
159161 .base_offset = eh_frame_offset,
......@@ -278,7 +280,8 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type {
278280 object_id: u32,
279281 source_offset: u32,
280282 ) ?SymbolWithLoc {
281 const cpu_arch = macho_file.base.options.target.cpu.arch;
283 const target = macho_file.base.comp.root_mod.resolved_target.result;
284 const cpu_arch = target.cpu.arch;
282285 const relocs = getRelocs(macho_file, object_id, source_offset);
283286 for (relocs) |rel| {
284287 switch (cpu_arch) {
......@@ -301,13 +304,13 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type {
301304 },
302305 else => unreachable,
303306 }
304 const target = Atom.parseRelocTarget(macho_file, .{
307 const reloc_target = Atom.parseRelocTarget(macho_file, .{
305308 .object_id = object_id,
306309 .rel = rel,
307310 .code = rec.data,
308311 .base_offset = @as(i32, @intCast(source_offset)) + 4,
309312 });
310 return target;
313 return reloc_target;
311314 }
312315 return null;
313316 }
......@@ -319,11 +322,12 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type {
319322 }) !void {
320323 comptime assert(is_mutable);
321324
322 const cpu_arch = macho_file.base.options.target.cpu.arch;
325 const target = macho_file.base.comp.root_mod.resolved_target.result;
326 const cpu_arch = target.cpu.arch;
323327 const relocs = getRelocs(macho_file, object_id, ctx.source_offset);
324328
325329 for (relocs) |rel| {
326 const target = Atom.parseRelocTarget(macho_file, .{
330 const reloc_target = Atom.parseRelocTarget(macho_file, .{
327331 .object_id = object_id,
328332 .rel = rel,
329333 .code = rec.data,
......@@ -340,14 +344,14 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type {
340344 // Address of the __eh_frame in the source object file
341345 },
342346 .ARM64_RELOC_POINTER_TO_GOT => {
343 const target_addr = macho_file.getGotEntryAddress(target).?;
347 const target_addr = macho_file.getGotEntryAddress(reloc_target).?;
344348 const result = math.cast(i32, @as(i64, @intCast(target_addr)) - @as(i64, @intCast(source_addr))) orelse
345349 return error.Overflow;
346350 mem.writeInt(i32, rec.data[rel_offset..][0..4], result, .little);
347351 },
348352 .ARM64_RELOC_UNSIGNED => {
349353 assert(rel.r_extern == 1);
350 const target_addr = Atom.getRelocTargetAddress(macho_file, target, false);
354 const target_addr = Atom.getRelocTargetAddress(macho_file, reloc_target, false);
351355 const result = @as(i64, @intCast(target_addr)) - @as(i64, @intCast(source_addr));
352356 mem.writeInt(i64, rec.data[rel_offset..][0..8], @as(i64, @intCast(result)), .little);
353357 },
......@@ -358,7 +362,7 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type {
358362 const rel_type = @as(macho.reloc_type_x86_64, @enumFromInt(rel.r_type));
359363 switch (rel_type) {
360364 .X86_64_RELOC_GOT => {
361 const target_addr = macho_file.getGotEntryAddress(target).?;
365 const target_addr = macho_file.getGotEntryAddress(reloc_target).?;
362366 const addend = mem.readInt(i32, rec.data[rel_offset..][0..4], .little);
363367 const adjusted_target_addr = @as(u64, @intCast(@as(i64, @intCast(target_addr)) + addend));
364368 const disp = try Relocation.calcPcRelativeDisplacementX86(source_addr, adjusted_target_addr, 0);
......@@ -374,7 +378,8 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type {
374378
375379 pub fn getCiePointerSource(rec: Record, object_id: u32, macho_file: *MachO, offset: u32) u32 {
376380 assert(rec.tag == .fde);
377 const cpu_arch = macho_file.base.options.target.cpu.arch;
381 const target = macho_file.base.comp.root_mod.resolved_target.result;
382 const cpu_arch = target.cpu.arch;
378383 const addend = mem.readInt(u32, rec.data[0..4], .little);
379384 switch (cpu_arch) {
380385 .aarch64 => {
src/link/MachO/load_commands.zig+3-3
......@@ -472,11 +472,11 @@ pub fn inferSdkVersion(gpa: Allocator, comp: *const Compilation) ?std.SemanticVe
472472 defer arena_allocator.deinit();
473473 const arena = arena_allocator.allocator();
474474
475 const options = comp.bin_file.options;
475 const macho_file = comp.bin_file.cast(MachO).?;
476476
477 const sdk_layout = options.darwin_sdk_layout orelse return null;
477 const sdk_layout = macho_file.sdk_layout orelse return null;
478478 const sdk_dir = switch (sdk_layout) {
479 .sdk => options.sysroot.?,
479 .sdk => macho_file.sysroot.?,
480480 .vendored => std.fs.path.join(arena, &.{ comp.zig_lib_directory.path.?, "libc", "darwin" }) catch return null,
481481 };
482482 if (readSdkVersionFromSettings(arena, sdk_dir)) |ver| {
src/link/MachO/zld.zig+18-11
......@@ -458,7 +458,7 @@ pub fn linkWithZld(
458458 }
459459
460460 try writeAtoms(macho_file);
461 if (macho_file.base.options.target.cpu.arch == .aarch64) try writeThunks(macho_file);
461 if (target.cpu.arch == .aarch64) try writeThunks(macho_file);
462462 try writeDyldPrivateAtom(macho_file);
463463
464464 if (macho_file.stubs_section_index) |_| {
......@@ -557,7 +557,7 @@ pub fn linkWithZld(
557557 .version = 0,
558558 });
559559 {
560 const platform = Platform.fromTarget(macho_file.base.options.target);
560 const platform = Platform.fromTarget(target);
561561 const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(arena, comp);
562562 if (platform.isBuildVersionCompatible()) {
563563 try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer);
......@@ -610,7 +610,8 @@ pub fn linkWithZld(
610610
611611fn createSegments(macho_file: *MachO) !void {
612612 const gpa = macho_file.base.allocator;
613 const page_size = MachO.getPageSize(macho_file.base.options.target.cpu.arch);
613 const target = macho_file.base.comp.root_mod.resolved_target.result;
614 const page_size = MachO.getPageSize(target.cpu.arch);
614615 const aligned_pagezero_vmsize = mem.alignBackward(u64, macho_file.pagezero_vmsize, page_size);
615616 if (macho_file.base.comp.config.output_mode != .Lib and aligned_pagezero_vmsize > 0) {
616617 if (aligned_pagezero_vmsize != macho_file.pagezero_vmsize) {
......@@ -755,7 +756,8 @@ fn writeDyldPrivateAtom(macho_file: *MachO) !void {
755756}
756757
757758fn writeThunks(macho_file: *MachO) !void {
758 assert(macho_file.base.options.target.cpu.arch == .aarch64);
759 const target = macho_file.base.comp.root_mod.resolved_target.result;
760 assert(target.cpu.arch == .aarch64);
759761 const gpa = macho_file.base.allocator;
760762
761763 const sect_id = macho_file.text_section_index orelse return;
......@@ -791,7 +793,8 @@ fn writePointerEntries(macho_file: *MachO, sect_id: u8, table: anytype) !void {
791793
792794fn writeStubs(macho_file: *MachO) !void {
793795 const gpa = macho_file.base.allocator;
794 const cpu_arch = macho_file.base.options.target.cpu.arch;
796 const target = macho_file.base.comp.root_mod.resolved_target.result;
797 const cpu_arch = target.cpu.arch;
795798 const stubs_header = macho_file.sections.items(.header)[macho_file.stubs_section_index.?];
796799 const la_symbol_ptr_header = macho_file.sections.items(.header)[macho_file.la_symbol_ptr_section_index.?];
797800
......@@ -813,7 +816,8 @@ fn writeStubs(macho_file: *MachO) !void {
813816
814817fn writeStubHelpers(macho_file: *MachO) !void {
815818 const gpa = macho_file.base.allocator;
816 const cpu_arch = macho_file.base.options.target.cpu.arch;
819 const target = macho_file.base.comp.root_mod.resolved_target.result;
820 const cpu_arch = target.cpu.arch;
817821 const stub_helper_header = macho_file.sections.items(.header)[macho_file.stub_helper_section_index.?];
818822
819823 const capacity = math.cast(usize, stub_helper_header.size) orelse return error.Overflow;
......@@ -856,7 +860,8 @@ fn writeStubHelpers(macho_file: *MachO) !void {
856860
857861fn writeLaSymbolPtrs(macho_file: *MachO) !void {
858862 const gpa = macho_file.base.allocator;
859 const cpu_arch = macho_file.base.options.target.cpu.arch;
863 const target = macho_file.base.comp.root_mod.resolved_target.result;
864 const cpu_arch = target.cpu.arch;
860865 const la_symbol_ptr_header = macho_file.sections.items(.header)[macho_file.la_symbol_ptr_section_index.?];
861866 const stub_helper_header = macho_file.sections.items(.header)[macho_file.stub_helper_section_index.?];
862867
......@@ -964,11 +969,12 @@ fn pruneAndSortSections(macho_file: *MachO) !void {
964969}
965970
966971fn calcSectionSizes(macho_file: *MachO) !void {
972 const target = macho_file.base.comp.root_mod.resolved_target.result;
967973 const slice = macho_file.sections.slice();
968974 for (slice.items(.header), 0..) |*header, sect_id| {
969975 if (header.size == 0) continue;
970976 if (macho_file.text_section_index) |txt| {
971 if (txt == sect_id and macho_file.base.options.target.cpu.arch == .aarch64) continue;
977 if (txt == sect_id and target.cpu.arch == .aarch64) continue;
972978 }
973979
974980 var atom_index = slice.items(.first_atom_index)[sect_id] orelse continue;
......@@ -991,7 +997,7 @@ fn calcSectionSizes(macho_file: *MachO) !void {
991997 }
992998 }
993999
994 if (macho_file.text_section_index != null and macho_file.base.options.target.cpu.arch == .aarch64) {
1000 if (macho_file.text_section_index != null and target.cpu.arch == .aarch64) {
9951001 // Create jump/branch range extenders if needed.
9961002 try thunks.createThunks(macho_file, macho_file.text_section_index.?);
9971003 }
......@@ -1043,7 +1049,7 @@ fn calcSectionSizes(macho_file: *MachO) !void {
10431049 header.@"align" = 3;
10441050 }
10451051
1046 const cpu_arch = macho_file.base.options.target.cpu.arch;
1052 const cpu_arch = target.cpu.arch;
10471053
10481054 if (macho_file.stubs_section_index) |sect_id| {
10491055 const header = &macho_file.sections.items(.header)[sect_id];
......@@ -1093,6 +1099,7 @@ fn getSegmentAllocBase(macho_file: *MachO, segment_index: u8) struct { vmaddr: u
10931099}
10941100
10951101fn allocateSegment(macho_file: *MachO, segment_index: u8, init_size: u64) !void {
1102 const target = macho_file.base.comp.root_mod.resolved_target.result;
10961103 const segment = &macho_file.segments.items[segment_index];
10971104
10981105 if (mem.eql(u8, segment.segName(), "__PAGEZERO")) return; // allocated upon creation
......@@ -1175,7 +1182,7 @@ fn allocateSegment(macho_file: *MachO, segment_index: u8, init_size: u64) !void
11751182 segment.vmsize = start;
11761183 }
11771184
1178 const page_size = MachO.getPageSize(macho_file.base.options.target.cpu.arch);
1185 const page_size = MachO.getPageSize(target.cpu.arch);
11791186 segment.filesize = mem.alignForward(u64, segment.filesize, page_size);
11801187 segment.vmsize = mem.alignForward(u64, segment.vmsize, page_size);
11811188}
src/link/Plan9.zig+30-25
......@@ -206,7 +206,8 @@ pub const Atom = struct {
206206
207207 // asserts that self.got_index != null
208208 pub fn getOffsetTableAddress(self: Atom, plan9: *Plan9) u64 {
209 const ptr_bytes = @divExact(plan9.base.options.target.ptrBitWidth(), 8);
209 const target = plan9.base.comp.root_mod.resolved_target.result;
210 const ptr_bytes = @divExact(target.ptrBitWidth(), 8);
210211 const got_addr = plan9.bases.data;
211212 const got_index = self.got_index.?;
212213 return got_addr + got_index * ptr_bytes;
......@@ -293,9 +294,11 @@ pub fn defaultBaseAddrs(arch: std.Target.Cpu.Arch) Bases {
293294 };
294295}
295296
296pub fn createEmpty(gpa: Allocator, options: link.Options) !*Plan9 {
297 if (options.use_llvm)
298 return error.LLVMBackendDoesNotSupportPlan9;
297pub fn createEmpty(arena: Allocator, options: link.File.OpenOptions) !*Plan9 {
298 _ = arena;
299 const target = options.comp.root_mod.resolved_target.result;
300 const gpa = options.comp.gpa;
301
299302 const sixtyfour_bit: bool = switch (options.target.ptrBitWidth()) {
300303 0...32 => false,
301304 33...64 => true,
......@@ -315,7 +318,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Plan9 {
315318 },
316319 .sixtyfour_bit = sixtyfour_bit,
317320 .bases = undefined,
318 .magic = try aout.magicFromArch(self.base.options.target.cpu.arch),
321 .magic = try aout.magicFromArch(target.cpu.arch),
319322 };
320323 // a / will always be in a file path
321324 try self.file_segments.put(gpa, "/", 1);
......@@ -399,6 +402,7 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: InternPool.Index, air:
399402 }
400403
401404 const gpa = self.base.comp.gpa;
405 const target = self.base.comp.root_mod.resolved_target.result;
402406 const func = mod.funcInfo(func_index);
403407 const decl_index = func.owner_decl;
404408 const decl = mod.declPtr(decl_index);
......@@ -414,7 +418,7 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: InternPool.Index, air:
414418 .end_line = undefined,
415419 .pcop_change_index = null,
416420 // we have already checked the target in the linker to make sure it is compatable
417 .pc_quanta = aout.getPCQuant(self.base.options.target.cpu.arch) catch unreachable,
421 .pc_quanta = aout.getPCQuant(target.cpu.arch) catch unreachable,
418422 };
419423 defer dbg_info_output.dbg_line.deinit();
420424
......@@ -658,6 +662,7 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
658662 }
659663
660664 const gpa = comp.gpa;
665 const target = comp.root_mod.resolved_target.result;
661666
662667 const tracy = trace(@src());
663668 defer tracy.end();
......@@ -749,9 +754,9 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
749754 atom.offset = off;
750755 log.debug("write text decl {*} ({}), lines {d} to {d}.;__GOT+0x{x} vaddr: 0x{x}", .{ decl, decl.name.fmt(&mod.intern_pool), out.start_line + 1, out.end_line, atom.got_index.? * 8, off });
751756 if (!self.sixtyfour_bit) {
752 mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), self.base.options.target.cpu.arch.endian());
757 mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), target.cpu.arch.endian());
753758 } else {
754 mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian());
759 mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, target.cpu.arch.endian());
755760 }
756761 self.syms.items[atom.sym_index.?].value = off;
757762 if (mod.decl_exports.get(decl_index)) |exports| {
......@@ -778,9 +783,9 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
778783 text_i += code.len;
779784 text_atom.offset = off;
780785 if (!self.sixtyfour_bit) {
781 mem.writeInt(u32, got_table[text_atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), self.base.options.target.cpu.arch.endian());
786 mem.writeInt(u32, got_table[text_atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), target.cpu.arch.endian());
782787 } else {
783 mem.writeInt(u64, got_table[text_atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian());
788 mem.writeInt(u64, got_table[text_atom.got_index.? * 8 ..][0..8], off, target.cpu.arch.endian());
784789 }
785790 self.syms.items[text_atom.sym_index.?].value = off;
786791 }
......@@ -791,9 +796,9 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
791796 const val = self.getAddr(text_i, .t);
792797 self.syms.items[etext_atom.sym_index.?].value = val;
793798 if (!self.sixtyfour_bit) {
794 mem.writeInt(u32, got_table[etext_atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(val)), self.base.options.target.cpu.arch.endian());
799 mem.writeInt(u32, got_table[etext_atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(val)), target.cpu.arch.endian());
795800 } else {
796 mem.writeInt(u64, got_table[etext_atom.got_index.? * 8 ..][0..8], val, self.base.options.target.cpu.arch.endian());
801 mem.writeInt(u64, got_table[etext_atom.got_index.? * 8 ..][0..8], val, target.cpu.arch.endian());
797802 }
798803 }
799804 // global offset table is in data
......@@ -815,9 +820,9 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
815820 data_i += code.len;
816821 atom.offset = off;
817822 if (!self.sixtyfour_bit) {
818 mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), self.base.options.target.cpu.arch.endian());
823 mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), target.cpu.arch.endian());
819824 } else {
820 mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian());
825 mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, target.cpu.arch.endian());
821826 }
822827 self.syms.items[atom.sym_index.?].value = off;
823828 if (mod.decl_exports.get(decl_index)) |exports| {
......@@ -838,9 +843,9 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
838843 data_i += code.len;
839844 atom.offset = off;
840845 if (!self.sixtyfour_bit) {
841 mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), self.base.options.target.cpu.arch.endian());
846 mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), target.cpu.arch.endian());
842847 } else {
843 mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian());
848 mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, target.cpu.arch.endian());
844849 }
845850 self.syms.items[atom.sym_index.?].value = off;
846851 }
......@@ -859,9 +864,9 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
859864 data_i += code.len;
860865 atom.offset = off;
861866 if (!self.sixtyfour_bit) {
862 mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), self.base.options.target.cpu.arch.endian());
867 mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), target.cpu.arch.endian());
863868 } else {
864 mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian());
869 mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, target.cpu.arch.endian());
865870 }
866871 self.syms.items[atom.sym_index.?].value = off;
867872 }
......@@ -879,9 +884,9 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
879884 data_i += code.len;
880885 data_atom.offset = off;
881886 if (!self.sixtyfour_bit) {
882 mem.writeInt(u32, got_table[data_atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), self.base.options.target.cpu.arch.endian());
887 mem.writeInt(u32, got_table[data_atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), target.cpu.arch.endian());
883888 } else {
884 mem.writeInt(u64, got_table[data_atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian());
889 mem.writeInt(u64, got_table[data_atom.got_index.? * 8 ..][0..8], off, target.cpu.arch.endian());
885890 }
886891 self.syms.items[data_atom.sym_index.?].value = off;
887892 }
......@@ -891,9 +896,9 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
891896 const val = self.getAddr(data_i, .b);
892897 self.syms.items[edata_atom.sym_index.?].value = val;
893898 if (!self.sixtyfour_bit) {
894 mem.writeInt(u32, got_table[edata_atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(val)), self.base.options.target.cpu.arch.endian());
899 mem.writeInt(u32, got_table[edata_atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(val)), target.cpu.arch.endian());
895900 } else {
896 mem.writeInt(u64, got_table[edata_atom.got_index.? * 8 ..][0..8], val, self.base.options.target.cpu.arch.endian());
901 mem.writeInt(u64, got_table[edata_atom.got_index.? * 8 ..][0..8], val, target.cpu.arch.endian());
897902 }
898903 }
899904 // end symbol (same as edata because native backends don't do .bss yet)
......@@ -902,10 +907,10 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
902907 const val = self.getAddr(data_i, .b);
903908 self.syms.items[end_atom.sym_index.?].value = val;
904909 if (!self.sixtyfour_bit) {
905 mem.writeInt(u32, got_table[end_atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(val)), self.base.options.target.cpu.arch.endian());
910 mem.writeInt(u32, got_table[end_atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(val)), target.cpu.arch.endian());
906911 } else {
907912 log.debug("write end (got_table[0x{x}] = 0x{x})", .{ end_atom.got_index.? * 8, val });
908 mem.writeInt(u64, got_table[end_atom.got_index.? * 8 ..][0..8], val, self.base.options.target.cpu.arch.endian());
913 mem.writeInt(u64, got_table[end_atom.got_index.? * 8 ..][0..8], val, target.cpu.arch.endian());
909914 }
910915 }
911916 }
......@@ -942,7 +947,7 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
942947 const source_atom = self.getAtom(source_atom_index);
943948 const source_atom_symbol = self.syms.items[source_atom.sym_index.?];
944949 const code = source_atom.code.getCode(self);
945 const endian = self.base.options.target.cpu.arch.endian();
950 const endian = target.cpu.arch.endian();
946951 for (kv.value_ptr.items) |reloc| {
947952 const offset = reloc.offset;
948953 const addend = reloc.addend;
src/link/Wasm.zig+7-4
......@@ -1119,7 +1119,8 @@ fn validateFeatures(
11191119 to_emit: *[@typeInfo(types.Feature.Tag).Enum.fields.len]bool,
11201120 emit_features_count: *u32,
11211121) !void {
1122 const cpu_features = wasm.base.options.target.cpu.features;
1122 const target = wasm.base.comp.root_mod.resolved_target.result;
1123 const cpu_features = target.cpu.features;
11231124 const infer = cpu_features.isEmpty(); // when the user did not define any features, we infer them from linked objects.
11241125 const known_features_count = @typeInfo(types.Feature.Tag).Enum.fields.len;
11251126
......@@ -1752,6 +1753,7 @@ pub fn getDeclVAddr(
17521753 decl_index: InternPool.DeclIndex,
17531754 reloc_info: link.File.RelocInfo,
17541755) !u64 {
1756 const target = wasm.base.comp.root_mod.resolved_target.result;
17551757 const gpa = wasm.base.comp.gpa;
17561758 const mod = wasm.base.comp.module.?;
17571759 const decl = mod.declPtr(decl_index);
......@@ -1762,7 +1764,7 @@ pub fn getDeclVAddr(
17621764 assert(reloc_info.parent_atom_index != 0);
17631765 const atom_index = wasm.symbol_atom.get(.{ .file = null, .index = reloc_info.parent_atom_index }).?;
17641766 const atom = wasm.getAtomPtr(atom_index);
1765 const is_wasm32 = wasm.base.options.target.cpu.arch == .wasm32;
1767 const is_wasm32 = target.cpu.arch == .wasm32;
17661768 if (decl.ty.zigTypeTag(mod) == .Fn) {
17671769 assert(reloc_info.addend == 0); // addend not allowed for function relocations
17681770 // We found a function pointer, so add it to our table,
......@@ -1825,12 +1827,13 @@ pub fn lowerAnonDecl(
18251827
18261828pub fn getAnonDeclVAddr(wasm: *Wasm, decl_val: InternPool.Index, reloc_info: link.File.RelocInfo) !u64 {
18271829 const gpa = wasm.base.comp.gpa;
1830 const target = wasm.base.comp.root_mod.resolved_target.result;
18281831 const atom_index = wasm.anon_decls.get(decl_val).?;
18291832 const target_symbol_index = wasm.getAtom(atom_index).getSymbolIndex().?;
18301833
18311834 const parent_atom_index = wasm.symbol_atom.get(.{ .file = null, .index = reloc_info.parent_atom_index }).?;
18321835 const parent_atom = wasm.getAtomPtr(parent_atom_index);
1833 const is_wasm32 = wasm.base.options.target.cpu.arch == .wasm32;
1836 const is_wasm32 = target.cpu.arch == .wasm32;
18341837 const mod = wasm.base.comp.module.?;
18351838 const ty = Type.fromInterned(mod.intern_pool.typeOf(decl_val));
18361839 if (ty.zigTypeTag(mod) == .Fn) {
......@@ -4557,7 +4560,7 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
45574560 break :blk null;
45584561 };
45594562
4560 const target = wasm.base.options.target;
4563 const target = wasm.base.comp.root_mod.resolved_target.result;
45614564
45624565 const id_symlink_basename = "lld.id";
45634566