Skip to content

Commit 28288dc

Browse files
authored
Merge pull request #13444 from ziglang/arm64-coff
aarch64,coff: lift-off!
2 parents 1d68045 + 165ae04 commit 28288dc

File tree

5 files changed

+340
-58
lines changed

5 files changed

+340
-58
lines changed

lib/std/io.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub const default_mode: ModeOverride = if (is_async) Mode.evented else .blocking
3636

3737
fn getStdOutHandle() os.fd_t {
3838
if (builtin.os.tag == .windows) {
39-
if (builtin.zig_backend == .stage2_x86_64) {
39+
if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_aarch64) {
4040
// TODO: this is just a temporary workaround until we advance x86 backend further along.
4141
return os.windows.GetStdHandle(os.windows.STD_OUTPUT_HANDLE) catch os.windows.INVALID_HANDLE_VALUE;
4242
}
@@ -62,7 +62,7 @@ pub fn getStdOut() File {
6262

6363
fn getStdErrHandle() os.fd_t {
6464
if (builtin.os.tag == .windows) {
65-
if (builtin.zig_backend == .stage2_x86_64) {
65+
if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_aarch64) {
6666
// TODO: this is just a temporary workaround until we advance x86 backend further along.
6767
return os.windows.GetStdHandle(os.windows.STD_ERROR_HANDLE) catch os.windows.INVALID_HANDLE_VALUE;
6868
}
@@ -88,7 +88,7 @@ pub fn getStdErr() File {
8888

8989
fn getStdInHandle() os.fd_t {
9090
if (builtin.os.tag == .windows) {
91-
if (builtin.zig_backend == .stage2_x86_64) {
91+
if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_aarch64) {
9292
// TODO: this is just a temporary workaround until we advance x86 backend further along.
9393
return os.windows.GetStdHandle(os.windows.STD_INPUT_HANDLE) catch os.windows.INVALID_HANDLE_VALUE;
9494
}

src/arch/aarch64/CodeGen.zig

Lines changed: 86 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ const MCValue = union(enum) {
142142
/// The value is in memory but requires a linker relocation fixup:
143143
/// * got - the value is referenced indirectly via GOT entry index (the linker emits a got-type reloc)
144144
/// * direct - the value is referenced directly via symbol index index (the linker emits a displacement reloc)
145-
linker_load: struct { @"type": enum { got, direct }, sym_index: u32 },
145+
/// * import - the value is referenced indirectly via import entry index (the linker emits an import-type reloc)
146+
linker_load: struct { @"type": enum { got, direct, import }, sym_index: u32 },
146147
/// The value is one of the stack variables.
147148
///
148149
/// If the type is a pointer, it means the pointer address is in
@@ -1117,13 +1118,11 @@ fn truncRegister(
11171118
});
11181119
},
11191120
32, 64 => {
1120-
assert(dest_reg.size() == operand_reg.size());
1121-
11221121
_ = try self.addInst(.{
11231122
.tag = .mov_register,
11241123
.data = .{ .rr = .{
1125-
.rd = dest_reg,
1126-
.rn = operand_reg,
1124+
.rd = if (int_bits == 32) dest_reg.toW() else dest_reg.toX(),
1125+
.rn = if (int_bits == 32) operand_reg.toW() else operand_reg.toX(),
11271126
} },
11281127
});
11291128
},
@@ -3719,14 +3718,21 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
37193718
const tag: Mir.Inst.Tag = switch (load_struct.@"type") {
37203719
.got => .load_memory_ptr_got,
37213720
.direct => .load_memory_ptr_direct,
3721+
.import => unreachable,
37223722
};
37233723
const mod = self.bin_file.options.module.?;
3724+
const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
3725+
const atom_index = switch (self.bin_file.tag) {
3726+
.macho => owner_decl.link.macho.sym_index,
3727+
.coff => owner_decl.link.coff.sym_index,
3728+
else => unreachable, // unsupported target format
3729+
};
37243730
_ = try self.addInst(.{
37253731
.tag = tag,
37263732
.data = .{
37273733
.payload = try self.addExtra(Mir.LoadMemoryPie{
37283734
.register = @enumToInt(src_reg),
3729-
.atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
3735+
.atom_index = atom_index,
37303736
.sym_index = load_struct.sym_index,
37313737
}),
37323738
},
@@ -4057,6 +4063,45 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
40574063
} else {
40584064
return self.fail("TODO implement calling bitcasted functions", .{});
40594065
}
4066+
} else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
4067+
if (func_value.castTag(.function)) |func_payload| {
4068+
const func = func_payload.data;
4069+
const fn_owner_decl = mod.declPtr(func.owner_decl);
4070+
try self.genSetReg(Type.initTag(.u64), .x30, .{
4071+
.linker_load = .{
4072+
.@"type" = .got,
4073+
.sym_index = fn_owner_decl.link.coff.sym_index,
4074+
},
4075+
});
4076+
// blr x30
4077+
_ = try self.addInst(.{
4078+
.tag = .blr,
4079+
.data = .{ .reg = .x30 },
4080+
});
4081+
} else if (func_value.castTag(.extern_fn)) |func_payload| {
4082+
const extern_fn = func_payload.data;
4083+
const decl_name = mod.declPtr(extern_fn.owner_decl).name;
4084+
if (extern_fn.lib_name) |lib_name| {
4085+
log.debug("TODO enforce that '{s}' is expected in '{s}' library", .{
4086+
decl_name,
4087+
lib_name,
4088+
});
4089+
}
4090+
const sym_index = try coff_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
4091+
try self.genSetReg(Type.initTag(.u64), .x30, .{
4092+
.linker_load = .{
4093+
.@"type" = .import,
4094+
.sym_index = sym_index,
4095+
},
4096+
});
4097+
// blr x30
4098+
_ = try self.addInst(.{
4099+
.tag = .blr,
4100+
.data = .{ .reg = .x30 },
4101+
});
4102+
} else {
4103+
return self.fail("TODO implement calling bitcasted functions", .{});
4104+
}
40604105
} else if (self.bin_file.cast(link.File.Plan9)) |p9| {
40614106
if (func_value.castTag(.function)) |func_payload| {
40624107
try p9.seeDecl(func_payload.data.owner_decl);
@@ -4077,8 +4122,6 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
40774122
} else {
40784123
return self.fail("TODO implement calling bitcasted functions", .{});
40794124
}
4080-
} else if (self.bin_file.cast(link.File.Coff)) |_| {
4081-
return self.fail("TODO implement calling in COFF for {}", .{self.target.cpu.arch});
40824125
} else unreachable;
40834126
} else {
40844127
assert(ty.zigTypeTag() == .Pointer);
@@ -5161,14 +5204,21 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
51615204
const tag: Mir.Inst.Tag = switch (load_struct.@"type") {
51625205
.got => .load_memory_ptr_got,
51635206
.direct => .load_memory_ptr_direct,
5207+
.import => unreachable,
51645208
};
51655209
const mod = self.bin_file.options.module.?;
5210+
const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
5211+
const atom_index = switch (self.bin_file.tag) {
5212+
.macho => owner_decl.link.macho.sym_index,
5213+
.coff => owner_decl.link.coff.sym_index,
5214+
else => unreachable, // unsupported target format
5215+
};
51665216
_ = try self.addInst(.{
51675217
.tag = tag,
51685218
.data = .{
51695219
.payload = try self.addExtra(Mir.LoadMemoryPie{
51705220
.register = @enumToInt(src_reg),
5171-
.atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
5221+
.atom_index = atom_index,
51725222
.sym_index = load_struct.sym_index,
51735223
}),
51745224
},
@@ -5268,14 +5318,21 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
52685318
const tag: Mir.Inst.Tag = switch (load_struct.@"type") {
52695319
.got => .load_memory_got,
52705320
.direct => .load_memory_direct,
5321+
.import => .load_memory_import,
52715322
};
52725323
const mod = self.bin_file.options.module.?;
5324+
const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
5325+
const atom_index = switch (self.bin_file.tag) {
5326+
.macho => owner_decl.link.macho.sym_index,
5327+
.coff => owner_decl.link.coff.sym_index,
5328+
else => unreachable, // unsupported target format
5329+
};
52735330
_ = try self.addInst(.{
52745331
.tag = tag,
52755332
.data = .{
52765333
.payload = try self.addExtra(Mir.LoadMemoryPie{
52775334
.register = @enumToInt(reg),
5278-
.atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
5335+
.atom_index = atom_index,
52795336
.sym_index = load_struct.sym_index,
52805337
}),
52815338
},
@@ -5455,14 +5512,21 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I
54555512
const tag: Mir.Inst.Tag = switch (load_struct.@"type") {
54565513
.got => .load_memory_ptr_got,
54575514
.direct => .load_memory_ptr_direct,
5515+
.import => unreachable,
54585516
};
54595517
const mod = self.bin_file.options.module.?;
5518+
const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
5519+
const atom_index = switch (self.bin_file.tag) {
5520+
.macho => owner_decl.link.macho.sym_index,
5521+
.coff => owner_decl.link.coff.sym_index,
5522+
else => unreachable, // unsupported target format
5523+
};
54605524
_ = try self.addInst(.{
54615525
.tag = tag,
54625526
.data = .{
54635527
.payload = try self.addExtra(Mir.LoadMemoryPie{
54645528
.register = @enumToInt(src_reg),
5465-
.atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index,
5529+
.atom_index = atom_index,
54665530
.sym_index = load_struct.sym_index,
54675531
}),
54685532
},
@@ -5775,7 +5839,13 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
57755839
.sym_index = decl.link.macho.sym_index,
57765840
} };
57775841
} else if (self.bin_file.cast(link.File.Coff)) |_| {
5778-
return self.fail("TODO codegen COFF const Decl pointer", .{});
5842+
// Because COFF is PIE-always-on, we defer memory address resolution until
5843+
// the linker has enough info to perform relocations.
5844+
assert(decl.link.coff.sym_index != 0);
5845+
return MCValue{ .linker_load = .{
5846+
.@"type" = .got,
5847+
.sym_index = decl.link.coff.sym_index,
5848+
} };
57795849
} else if (self.bin_file.cast(link.File.Plan9)) |p9| {
57805850
try p9.seeDecl(decl_index);
57815851
const got_addr = p9.bases.data + decl.link.plan9.got_index.? * ptr_bytes;
@@ -5799,7 +5869,10 @@ fn lowerUnnamedConst(self: *Self, tv: TypedValue) InnerError!MCValue {
57995869
.sym_index = local_sym_index,
58005870
} };
58015871
} else if (self.bin_file.cast(link.File.Coff)) |_| {
5802-
return self.fail("TODO lower unnamed const in COFF", .{});
5872+
return MCValue{ .linker_load = .{
5873+
.@"type" = .direct,
5874+
.sym_index = local_sym_index,
5875+
} };
58035876
} else if (self.bin_file.cast(link.File.Plan9)) |_| {
58045877
return self.fail("TODO lower unnamed const in Plan9", .{});
58055878
} else {

src/arch/aarch64/Emit.zig

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ pub fn emitMir(
145145

146146
.load_memory_got => try emit.mirLoadMemoryPie(inst),
147147
.load_memory_direct => try emit.mirLoadMemoryPie(inst),
148+
.load_memory_import => try emit.mirLoadMemoryPie(inst),
148149
.load_memory_ptr_got => try emit.mirLoadMemoryPie(inst),
149150
.load_memory_ptr_direct => try emit.mirLoadMemoryPie(inst),
150151

@@ -674,13 +675,14 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
674675
assert(emit.mir.instructions.items(.tag)[inst] == .call_extern);
675676
const relocation = emit.mir.instructions.items(.data)[inst].relocation;
676677

678+
const offset = blk: {
679+
const offset = @intCast(u32, emit.code.items.len);
680+
// bl
681+
try emit.writeInstruction(Instruction.bl(0));
682+
break :blk offset;
683+
};
684+
677685
if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
678-
const offset = blk: {
679-
const offset = @intCast(u32, emit.code.items.len);
680-
// bl
681-
try emit.writeInstruction(Instruction.bl(0));
682-
break :blk offset;
683-
};
684686
// Add relocation to the decl.
685687
const atom = macho_file.getAtomForSymbol(.{ .sym_index = relocation.atom_index, .file = null }).?;
686688
const target = macho_file.getGlobalByIndex(relocation.sym_index);
@@ -692,8 +694,10 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
692694
.pcrel = true,
693695
.length = 2,
694696
});
697+
} else if (emit.bin_file.cast(link.File.Coff)) |_| {
698+
unreachable; // Calling imports is handled via `.load_memory_import`
695699
} else {
696-
return emit.fail("Implement call_extern for linking backends != MachO", .{});
700+
return emit.fail("Implement call_extern for linking backends != {{ COFF, MachO }}", .{});
697701
}
698702
}
699703

@@ -855,7 +859,9 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
855859
try emit.writeInstruction(Instruction.adrp(reg.toX(), 0));
856860

857861
switch (tag) {
858-
.load_memory_got => {
862+
.load_memory_got,
863+
.load_memory_import,
864+
=> {
859865
// ldr reg, reg, offset
860866
try emit.writeInstruction(Instruction.ldr(
861867
reg,
@@ -926,6 +932,51 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
926932
else => unreachable,
927933
},
928934
});
935+
} else if (emit.bin_file.cast(link.File.Coff)) |coff_file| {
936+
const atom = coff_file.getAtomForSymbol(.{ .sym_index = data.atom_index, .file = null }).?;
937+
const target = switch (tag) {
938+
.load_memory_got,
939+
.load_memory_ptr_got,
940+
.load_memory_direct,
941+
.load_memory_ptr_direct,
942+
=> link.File.Coff.SymbolWithLoc{ .sym_index = data.sym_index, .file = null },
943+
.load_memory_import => coff_file.getGlobalByIndex(data.sym_index),
944+
else => unreachable,
945+
};
946+
try atom.addRelocation(coff_file, .{
947+
.target = target,
948+
.offset = offset,
949+
.addend = 0,
950+
.pcrel = true,
951+
.length = 2,
952+
.@"type" = switch (tag) {
953+
.load_memory_got,
954+
.load_memory_ptr_got,
955+
=> .got_page,
956+
.load_memory_direct,
957+
.load_memory_ptr_direct,
958+
=> .page,
959+
.load_memory_import => .import_page,
960+
else => unreachable,
961+
},
962+
});
963+
try atom.addRelocation(coff_file, .{
964+
.target = target,
965+
.offset = offset + 4,
966+
.addend = 0,
967+
.pcrel = false,
968+
.length = 2,
969+
.@"type" = switch (tag) {
970+
.load_memory_got,
971+
.load_memory_ptr_got,
972+
=> .got_pageoff,
973+
.load_memory_direct,
974+
.load_memory_ptr_direct,
975+
=> .pageoff,
976+
.load_memory_import => .import_pageoff,
977+
else => unreachable,
978+
},
979+
});
929980
} else {
930981
return emit.fail("TODO implement load_memory for PIE GOT indirection on this platform", .{});
931982
}

src/arch/aarch64/Mir.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ pub const Inst = struct {
8484
///
8585
/// Payload is `LoadMemoryPie`
8686
load_memory_direct,
87+
/// Loads the contents into a register
88+
///
89+
/// Payload is `LoadMemoryPie`
90+
load_memory_import,
8791
/// Loads the address into a register
8892
///
8993
/// Payload is `LoadMemoryPie`

0 commit comments

Comments
 (0)