diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-10-04 19:05:45 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-04 19:05:45 -0400 |
| commit | 6aa668e0206da8b9233bfb90a2ffe1f692bf18bd (patch) | |
| tree | 0f3a704908d3ae8c3e182e4aeba5fbeacf065454 /src/codegen.zig | |
| parent | 302a69f127ae8542f49d9cd07c7cc49f3bbd6181 (diff) | |
| parent | c4054f8e0a6a7dea8570507d2a38b38a392ddd0d (diff) | |
| download | zig-6aa668e0206da8b9233bfb90a2ffe1f692bf18bd.tar.gz zig-6aa668e0206da8b9233bfb90a2ffe1f692bf18bd.zip | |
Merge pull request #6476 from kubkon/macho-exe
Link basic MachO executables with stage2
Diffstat (limited to 'src/codegen.zig')
| -rw-r--r-- | src/codegen.zig | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/codegen.zig b/src/codegen.zig index a1d3cc2fc4..57d2f8ff1f 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -1532,12 +1532,13 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { if (func_inst.val.cast(Value.Payload.Function)) |func_val| { const func = func_val.func; const got = &macho_file.sections.items[macho_file.got_section_index.?]; - const ptr_bytes = 8; - const got_addr = @intCast(u32, got.addr + func.owner_decl.link.macho.offset_table_index.? * ptr_bytes); - // ff 14 25 xx xx xx xx call [addr] - try self.code.ensureCapacity(self.code.items.len + 7); - self.code.appendSliceAssumeCapacity(&[3]u8{ 0xff, 0x14, 0x25 }); - mem.writeIntLittle(u32, self.code.addManyAsArrayAssumeCapacity(4), got_addr); + const got_addr = got.addr + func.owner_decl.link.macho.offset_table_index * @sizeOf(u64); + // Here, we store the got address in %rax, and then call %rax + // movabsq [addr], %rax + try self.genSetReg(inst.base.src, .rax, .{ .memory = got_addr }); + // callq *%rax + try self.code.ensureCapacity(self.code.items.len + 2); + self.code.appendSliceAssumeCapacity(&[2]u8{ 0xff, 0xd0 }); } else { return self.fail(inst.base.src, "TODO implement calling bitcasted functions", .{}); } @@ -2590,7 +2591,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { } else if (self.bin_file.cast(link.File.MachO)) |macho_file| { const decl = payload.decl; const got = &macho_file.sections.items[macho_file.got_section_index.?]; - const got_addr = got.addr + decl.link.macho.offset_table_index.? * ptr_bytes; + const got_addr = got.addr + decl.link.macho.offset_table_index * ptr_bytes; return MCValue{ .memory = got_addr }; } else if (self.bin_file.cast(link.File.Coff)) |coff_file| { const decl = payload.decl; |
