aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.zig
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2021-09-14 14:20:11 +0200
committerGitHub <noreply@github.com>2021-09-14 14:20:11 +0200
commit85f065a5115b201b7b6b0b7325a4626211bbb642 (patch)
treee255717a101910d0b024b17cff189952e61368a2 /src/codegen.zig
parentd1908c9f661abebb2879b02c8ea3ac823fec27e7 (diff)
parent05763f43b3d8318c95891650c11ab243ce9a1fd5 (diff)
downloadzig-85f065a5115b201b7b6b0b7325a4626211bbb642.tar.gz
zig-85f065a5115b201b7b6b0b7325a4626211bbb642.zip
Merge pull request #9676 from ziglang/zld-incr
MachO: merges stage1 with self-hosted codepath
Diffstat (limited to 'src/codegen.zig')
-rw-r--r--src/codegen.zig62
1 files changed, 17 insertions, 45 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index e28192ec1f..1995d8baa7 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -2816,24 +2816,21 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
if (self.air.value(callee)) |func_value| {
if (func_value.castTag(.function)) |func_payload| {
const func = func_payload.data;
- const got_addr = blk: {
- const seg = macho_file.load_commands.items[macho_file.data_const_segment_cmd_index.?].Segment;
- const got = seg.sections.items[macho_file.got_section_index.?];
- const got_index = macho_file.got_entries_map.get(.{
- .where = .local,
- .where_index = func.owner_decl.link.macho.local_sym_index,
- }) orelse unreachable;
- break :blk got.addr + got_index * @sizeOf(u64);
- };
+ // TODO I'm hacking my way through here by repurposing .memory for storing
+ // index to the GOT target symbol index.
switch (arch) {
.x86_64 => {
- try self.genSetReg(Type.initTag(.u64), .rax, .{ .memory = got_addr });
+ try self.genSetReg(Type.initTag(.u64), .rax, .{
+ .memory = func.owner_decl.link.macho.local_sym_index,
+ });
// callq *%rax
try self.code.ensureCapacity(self.code.items.len + 2);
self.code.appendSliceAssumeCapacity(&[2]u8{ 0xff, 0xd0 });
},
.aarch64 => {
- try self.genSetReg(Type.initTag(.u64), .x30, .{ .memory = got_addr });
+ try self.genSetReg(Type.initTag(.u64), .x30, .{
+ .memory = func.owner_decl.link.macho.local_sym_index,
+ });
// blr x30
writeInt(u32, try self.code.addManyAsArray(4), Instruction.blr(.x30).toU32());
},
@@ -4345,29 +4342,20 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
}).toU32());
if (self.bin_file.cast(link.File.MachO)) |macho_file| {
- // TODO this is super awkward. We are reversing the address of the GOT entry here.
- // We should probably have it cached or move the reloc adding somewhere else.
- const got_addr = blk: {
- const seg = macho_file.load_commands.items[macho_file.data_const_segment_cmd_index.?].Segment;
- const got = seg.sections.items[macho_file.got_section_index.?];
- break :blk got.addr;
- };
- const where_index = blk: for (macho_file.got_entries.items) |key, id| {
- if (got_addr + id * @sizeOf(u64) == addr) break :blk key.where_index;
- } else unreachable;
+ // TODO I think the reloc might be in the wrong place.
const decl = macho_file.active_decl.?;
// Page reloc for adrp instruction.
try decl.link.macho.relocs.append(self.bin_file.allocator, .{
.offset = offset,
.where = .local,
- .where_index = where_index,
+ .where_index = @intCast(u32, addr),
.payload = .{ .page = .{ .kind = .got } },
});
// Pageoff reloc for adrp instruction.
try decl.link.macho.relocs.append(self.bin_file.allocator, .{
.offset = offset + 4,
.where = .local,
- .where_index = where_index,
+ .where_index = @intCast(u32, addr),
.payload = .{ .page_off = .{ .kind = .got } },
});
} else {
@@ -4628,22 +4616,13 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
const offset = @intCast(u32, self.code.items.len);
if (self.bin_file.cast(link.File.MachO)) |macho_file| {
- // TODO this is super awkward. We are reversing the address of the GOT entry here.
- // We should probably have it cached or move the reloc adding somewhere else.
- const got_addr = blk: {
- const seg = macho_file.load_commands.items[macho_file.data_const_segment_cmd_index.?].Segment;
- const got = seg.sections.items[macho_file.got_section_index.?];
- break :blk got.addr;
- };
- const where_index = blk: for (macho_file.got_entries.items) |key, id| {
- if (got_addr + id * @sizeOf(u64) == x) break :blk key.where_index;
- } else unreachable;
+ // TODO I think the reloc might be in the wrong place.
const decl = macho_file.active_decl.?;
// Load reloc for LEA instruction.
try decl.link.macho.relocs.append(self.bin_file.allocator, .{
.offset = offset - 4,
.where = .local,
- .where_index = where_index,
+ .where_index = @intCast(u32, x),
.payload = .{ .load = .{ .kind = .got } },
});
} else {
@@ -4869,17 +4848,10 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
const got_addr = got.p_vaddr + decl.link.elf.offset_table_index * ptr_bytes;
return MCValue{ .memory = got_addr };
- } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
- const got_addr = blk: {
- const seg = macho_file.load_commands.items[macho_file.data_const_segment_cmd_index.?].Segment;
- const got = seg.sections.items[macho_file.got_section_index.?];
- const got_index = macho_file.got_entries_map.get(.{
- .where = .local,
- .where_index = decl.link.macho.local_sym_index,
- }) orelse unreachable;
- break :blk got.addr + got_index * ptr_bytes;
- };
- return MCValue{ .memory = got_addr };
+ } else if (self.bin_file.cast(link.File.MachO)) |_| {
+ // TODO I'm hacking my way through here by repurposing .memory for storing
+ // index to the GOT target symbol index.
+ return MCValue{ .memory = decl.link.macho.local_sym_index };
} else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
const got_addr = coff_file.offset_table_virtual_address + decl.link.coff.offset_table_index * ptr_bytes;
return MCValue{ .memory = got_addr };