zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 4ca9a8d192f4c800f10cdb3bd39c94922b6fb9b8 (tree)
parent 3832b582292d3065f600e7c7a8393c411e6cdb0a
Author: Jakub Konka <kubkon@jakubkonka.com>
Date:   Thu,  3 Feb 2022 19:19:48 +0100

x64: implement storing to MCValue.memory for PIE targets

Diffstat:
Msrc/arch/x86_64/CodeGen.zig | 28++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)

diff --git 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(.{