aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.zig
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2020-11-25 11:10:17 +0100
committerJakub Konka <kubkon@jakubkonka.com>2020-11-26 11:50:09 +0100
commit10942e3f86c03d30833cc221371f30b78a4bd710 (patch)
tree69b9d317cf8d9fe7b58e4a8f67c4250c1911365a /src/codegen.zig
parent2cd84b1b3f0a6e3f030da723ec51f0be33b7a1ed (diff)
downloadzig-10942e3f86c03d30833cc221371f30b78a4bd710.tar.gz
zig-10942e3f86c03d30833cc221371f30b78a4bd710.zip
stage2 macho: Hello, Silicon!
Diffstat (limited to 'src/codegen.zig')
-rw-r--r--src/codegen.zig26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index f75ad079ae..1482e8de7e 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -1689,6 +1689,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
},
.aarch64 => {
try self.genSetReg(inst.base.src, .x30, .{ .memory = got_addr });
+ // blr x30
writeInt(u32, try self.code.addManyAsArray(4), Instruction.blr(.x30).toU32());
},
else => unreachable, // unsupported architecture on MachO
@@ -2583,10 +2584,27 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
},
.register => return self.fail(src, "TODO implement genSetReg for aarch64 {}", .{mcv}),
.memory => |addr| {
- // The value is in memory at a hard-coded address.
- // If the type is a pointer, it means the pointer address is at this memory location.
- try self.genSetReg(src, reg, .{ .immediate = addr });
- mem.writeIntLittle(u32, try self.code.addManyAsArray(4), Instruction.ldr(reg, .{ .rn = reg }).toU32());
+ if (self.bin_file.cast(link.File.MachO)) |macho_file| {
+ // For MachO, the binary, with the exception of object files, has to be a PIE.
+ // Therefore we cannot load an absolute address.
+ // Instead, we need to make use of PC-relative addressing.
+ // if (reg.id() == 0) { // x0 is special-cased
+ try self.mod_fn.owner_decl.link.macho.addPieFixup(self.bin_file.allocator, .{
+ .address = addr,
+ .start = self.code.items.len,
+ .len = 4,
+ });
+ // bl [label]
+ mem.writeIntLittle(u32, try self.code.addManyAsArray(4), Instruction.bl(0).toU32());
+ // } else {
+ // unreachable; // TODO
+ // }
+ } else {
+ // The value is in memory at a hard-coded address.
+ // If the type is a pointer, it means the pointer address is at this memory location.
+ try self.genSetReg(src, reg, .{ .immediate = addr });
+ mem.writeIntLittle(u32, try self.code.addManyAsArray(4), Instruction.ldr(reg, .{ .rn = reg }).toU32());
+ }
},
else => return self.fail(src, "TODO implement genSetReg for aarch64 {}", .{mcv}),
},