aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2020-09-30 23:17:40 +0200
committerJakub Konka <kubkon@jakubkonka.com>2020-10-04 15:31:47 +0200
commitf8dd48bcd257a5a6a893c11b89af21b7e2ca9a79 (patch)
treefc8edef1c994776860a533ce44954da99eedda45 /src
parent5a7105401cda94fa82f07630672559659d875854 (diff)
downloadzig-f8dd48bcd257a5a6a893c11b89af21b7e2ca9a79.tar.gz
zig-f8dd48bcd257a5a6a893c11b89af21b7e2ca9a79.zip
Fix after rebase and enable stage2 tests for macOS
Also, rewrites codegen section to store symbol address in a register to then later invoke `callq` on the register.
Diffstat (limited to 'src')
-rw-r--r--src/codegen.zig12
-rw-r--r--src/link/MachO.zig11
2 files changed, 13 insertions, 10 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index a1d3cc2fc4..919d1ef457 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -1532,12 +1532,12 @@ 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
+ self.code.appendSliceAssumeCapacity(&[2]u8{ 0xff, 0xd0 });
} else {
return self.fail(inst.base.src, "TODO implement calling bitcasted functions", .{});
}
diff --git a/src/link/MachO.zig b/src/link/MachO.zig
index 342a73ca36..9a58a35dc9 100644
--- a/src/link/MachO.zig
+++ b/src/link/MachO.zig
@@ -262,11 +262,11 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
dysymtab.iundefsym = nlocals + nglobals;
dysymtab.nundefsym = nundefs;
}
- {
+ if (self.entry_addr) |addr| {
// update LC_MAIN with entry offset
const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
const main_cmd = &self.load_commands.items[self.main_cmd_index.?].EntryPoint;
- main_cmd.entryoff = self.entry_addr.? - text_segment.vmaddr;
+ main_cmd.entryoff = addr - text_segment.vmaddr;
}
{
var last_cmd_offset: usize = @sizeOf(macho.mach_header_64);
@@ -709,6 +709,7 @@ fn darwinArchString(arch: std.Target.Cpu.Arch) []const u8 {
pub fn deinit(self: *MachO) void {
self.offset_table.deinit(self.base.allocator);
self.string_table.deinit(self.base.allocator);
+ self.undef_symbols.deinit(self.base.allocator);
self.global_symbols.deinit(self.base.allocator);
self.local_symbols.deinit(self.base.allocator);
self.sections.deinit(self.base.allocator);
@@ -813,7 +814,7 @@ pub fn updateDeclExports(
try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);
module.failed_exports.putAssumeCapacityNoClobber(
exp,
- try Module.ErrorMsg.create(self.base.allocator, 0, "Unimplemented: ExportOptions.section", .{}),
+ try Compilation.ErrorMsg.create(self.base.allocator, 0, "Unimplemented: ExportOptions.section", .{}),
);
continue;
}
@@ -831,7 +832,7 @@ pub fn updateDeclExports(
try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);
module.failed_exports.putAssumeCapacityNoClobber(
exp,
- try Module.ErrorMsg.create(self.base.allocator, 0, "Unimplemented: GlobalLinkage.LinkOnce", .{}),
+ try Compilation.ErrorMsg.create(self.base.allocator, 0, "Unimplemented: GlobalLinkage.LinkOnce", .{}),
);
continue;
},
@@ -1405,6 +1406,8 @@ fn writeAllUndefSymbols(self: *MachO) !void {
}
fn writeExportTrie(self: *MachO) !void {
+ if (self.entry_addr == null) return;
+
// TODO implement mechanism for generating a prefix tree of the exported symbols
// single branch export trie
var buf = [_]u8{0} ** 24;