authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-15 22:02:40+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-18 10:00:04+02:00
logdc6480dba5ec4533f6a20292f93fe5c25a1a689f
treeed1911eb4dd676adef6022cc9d8947b48dafd660
parent618c7a3546bb5e73a4f7ff4bd38e53700f63c90c

macho: allow for add and ldr when resolving GOT_LOAD_* relocs


3 files changed, 11 insertions(+), 28 deletions(-)

src/arch/aarch64/Emit.zig+3-2
......@@ -845,7 +845,8 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
845845 try emit.writeInstruction(Instruction.adrp(reg.to64(), 0));
846846
847847 switch (tag) {
848 .load_memory_got => {
848 .load_memory_got,
849 => {
849850 // ldr reg, reg, offset
850851 try emit.writeInstruction(Instruction.ldr(
851852 reg,
......@@ -871,8 +872,8 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
871872 Instruction.LoadStoreOffset.imm(0),
872873 ));
873874 },
874 .load_memory_ptr_got,
875875 .load_memory_ptr_direct,
876 .load_memory_ptr_got,
876877 => {
877878 // add reg, reg, offset
878879 try emit.writeInstruction(Instruction.add(reg, reg, 0, false));
src/link/MachO.zig+1
......@@ -3434,6 +3434,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
34343434 }
34353435
34363436 if (self.text_section_index == null) {
3437 // Sadly, segments need unique string identfiers for some reason.
34373438 self.text_section_index = try self.allocateSection("__TEXT1", "__text", .{
34383439 .size = self.base.options.program_code_size_hint,
34393440 .alignment = switch (cpu_arch) {
src/link/MachO/Relocation.zig+7-26
......@@ -135,7 +135,9 @@ fn resolveAarch64(
135135 inst.pc_relative_address.immlo = @truncate(u2, pages);
136136 mem.writeIntLittle(u32, &buffer, inst.toU32());
137137 },
138 .ARM64_RELOC_PAGEOFF12 => {
138 .ARM64_RELOC_PAGEOFF12,
139 .ARM64_RELOC_GOT_LOAD_PAGEOFF12,
140 => {
139141 const narrowed = @truncate(u12, @intCast(u64, target_addr));
140142 if (isArithmeticOp(&buffer)) {
141143 var inst = aarch64.Instruction{
......@@ -170,18 +172,6 @@ fn resolveAarch64(
170172 mem.writeIntLittle(u32, &buffer, inst.toU32());
171173 }
172174 },
173 .ARM64_RELOC_GOT_LOAD_PAGEOFF12 => {
174 const narrowed = @truncate(u12, @intCast(u64, target_addr));
175 var inst: aarch64.Instruction = .{
176 .load_store_register = mem.bytesToValue(meta.TagPayload(
177 aarch64.Instruction,
178 aarch64.Instruction.load_store_register,
179 ), &buffer),
180 };
181 const offset = @divExact(narrowed, 8);
182 inst.load_store_register.offset = offset;
183 mem.writeIntLittle(u32, &buffer, inst.toU32());
184 },
185175 .ARM64_RELOC_TLVP_LOAD_PAGEOFF12 => {
186176 const RegInfo = struct {
187177 rd: u5,
......@@ -226,11 +216,8 @@ fn resolveAarch64(
226216 mem.writeIntLittle(u32, &buffer, inst.toU32());
227217 },
228218 .ARM64_RELOC_POINTER_TO_GOT => {
229 const result = math.cast(
230 i32,
231 @intCast(i64, target_addr) - @intCast(i64, source_addr),
232 ) orelse return error.Overflow;
233 mem.writeIntLittle(u32, &buffer, @bitCast(u32, result));
219 const result = @intCast(i32, @intCast(i64, target_addr) - @intCast(i64, source_addr));
220 mem.writeIntLittle(i32, &buffer, result);
234221 },
235222 .ARM64_RELOC_SUBTRACTOR => unreachable,
236223 .ARM64_RELOC_ADDEND => unreachable,
......@@ -255,10 +242,7 @@ fn resolveX8664(
255242 .X86_64_RELOC_GOT_LOAD,
256243 .X86_64_RELOC_TLV,
257244 => {
258 const displacement = math.cast(
259 i32,
260 @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4,
261 ) orelse return error.Overflow;
245 const displacement = @intCast(i32, @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4);
262246 mem.writeIntLittle(u32, buffer[0..4], @bitCast(u32, displacement));
263247 break :blk buffer[0..4];
264248 },
......@@ -274,10 +258,7 @@ fn resolveX8664(
274258 .X86_64_RELOC_SIGNED_4 => 4,
275259 else => unreachable,
276260 };
277 const displacement = math.cast(
278 i32,
279 target_addr - @intCast(i64, source_addr + correction + 4),
280 ) orelse return error.Overflow;
261 const displacement = @intCast(i32, target_addr - @intCast(i64, source_addr + correction + 4));
281262 mem.writeIntLittle(u32, buffer[0..4], @bitCast(u32, displacement));
282263 break :blk buffer[0..4];
283264 },