aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2022-02-03 19:19:48 +0100
committerJakub Konka <kubkon@jakubkonka.com>2022-02-04 00:37:43 +0100
commit4ca9a8d192f4c800f10cdb3bd39c94922b6fb9b8 (patch)
treec9bb405aad71648af39139d32ee1532182b782b2 /src
parent3832b582292d3065f600e7c7a8393c411e6cdb0a (diff)
downloadzig-4ca9a8d192f4c800f10cdb3bd39c94922b6fb9b8.tar.gz
zig-4ca9a8d192f4c800f10cdb3bd39c94922b6fb9b8.zip
x64: implement storing to MCValue.memory for PIE targets
Diffstat (limited to 'src')
-rw-r--r--src/arch/x86_64/CodeGen.zig28
1 files changed, 20 insertions, 8 deletions
diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig
index e05a66228b..b3a292a7f4 100644
--- a/src/arch/x86_64/CodeGen.zig
+++ b/src/arch/x86_64/CodeGen.zig
@@ -1811,17 +1811,29 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
}
},
.memory => |addr| {
- if (self.bin_file.options.pie) {
- return self.fail("TODO implement storing to memory when targeting PIE", .{});
- }
-
- // TODO: in case the address fits in an imm32 we can use [ds:imm32]
- // instead of wasting an instruction copying the address to a register
-
value.freezeIfRegister(&self.register_manager);
defer value.unfreezeIfRegister(&self.register_manager);
- const addr_reg = try self.copyToTmpRegister(ptr_ty, .{ .immediate = addr });
+ const addr_reg: Register = blk: {
+ if (self.bin_file.options.pie) {
+ const addr_reg = try self.register_manager.allocReg(null);
+ _ = try self.addInst(.{
+ .tag = .lea,
+ .ops = (Mir.Ops{
+ .reg1 = addr_reg.to64(),
+ .flags = 0b10,
+ }).encode(),
+ .data = .{ .got_entry = @truncate(u32, addr) },
+ });
+ break :blk addr_reg;
+ } else {
+ // TODO: in case the address fits in an imm32 we can use [ds:imm32]
+ // instead of wasting an instruction copying the address to a register
+ const addr_reg = try self.copyToTmpRegister(ptr_ty, .{ .immediate = addr });
+ break :blk addr_reg;
+ }
+ };
+
// to get the actual address of the value we want to modify we have to go through the GOT
// mov reg, [reg]
_ = try self.addInst(.{