Skip to content

Commit 7be2e85

Browse files
committed
linker: update target references
1 parent 9cd4a1e commit 7be2e85

19 files changed

+274
-191
lines changed

src/arch/wasm/Emit.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

22022203
fn 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

24532455
pub 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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

15761577
fn 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),

0 commit comments

Comments
 (0)