authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-03 19:19:48+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-04 00:37:43+01:00
log4ca9a8d192f4c800f10cdb3bd39c94922b6fb9b8
treec9bb405aad71648af39139d32ee1532182b782b2
parent3832b582292d3065f600e7c7a8393c411e6cdb0a

x64: implement storing to MCValue.memory for PIE targets


1 files changed, 20 insertions(+), 8 deletions(-)

src/arch/x86_64/CodeGen.zig+20-8
......@@ -1811,17 +1811,29 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
18111811 }
18121812 },
18131813 .memory => |addr| {
1814 if (self.bin_file.options.pie) {
1815 return self.fail("TODO implement storing to memory when targeting PIE", .{});
1816 }
1817
1818 // TODO: in case the address fits in an imm32 we can use [ds:imm32]
1819 // instead of wasting an instruction copying the address to a register
1820
18211814 value.freezeIfRegister(&self.register_manager);
18221815 defer value.unfreezeIfRegister(&self.register_manager);
18231816
1824 const addr_reg = try self.copyToTmpRegister(ptr_ty, .{ .immediate = addr });
1817 const addr_reg: Register = blk: {
1818 if (self.bin_file.options.pie) {
1819 const addr_reg = try self.register_manager.allocReg(null);
1820 _ = try self.addInst(.{
1821 .tag = .lea,
1822 .ops = (Mir.Ops{
1823 .reg1 = addr_reg.to64(),
1824 .flags = 0b10,
1825 }).encode(),
1826 .data = .{ .got_entry = @truncate(u32, addr) },
1827 });
1828 break :blk addr_reg;
1829 } else {
1830 // TODO: in case the address fits in an imm32 we can use [ds:imm32]
1831 // instead of wasting an instruction copying the address to a register
1832 const addr_reg = try self.copyToTmpRegister(ptr_ty, .{ .immediate = addr });
1833 break :blk addr_reg;
1834 }
1835 };
1836
18251837 // to get the actual address of the value we want to modify we have to go through the GOT
18261838 // mov reg, [reg]
18271839 _ = try self.addInst(.{