aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.zig
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2021-10-10 10:33:15 +0200
committerJakub Konka <kubkon@jakubkonka.com>2021-10-13 16:17:10 +0200
commitfc302f00a9de5de0490f4a66720e75946763c695 (patch)
tree3ac9dfd61b29a6dbab7054abe6e71daf7f085e6c /src/codegen.zig
parenta3104a4a78089f3260c0dd3f4a96012c6d73a63b (diff)
downloadzig-fc302f00a9de5de0490f4a66720e75946763c695.tar.gz
zig-fc302f00a9de5de0490f4a66720e75946763c695.zip
macho: redo relocation handling and lazy bind globals
* apply late symbol resolution for globals - instead of resolving the exact location of a symbol in locals, globals or undefs, we postpone the exact resolution until we have a full picture for relocation resolution. * fixup stubs to defined symbols - this is currently a hack rather than a final solution. I'll need to work out the details to make it more approachable. Currently, we preemptively create a stub for a lazy bound global and fix up stub offsets in stub helper routine if the global turns out to be undefined only. This is quite wasteful in terms of space as we create stub, stub helper and lazy ptr atoms but don't use them for defined globals. * change log scope to .link for macho. * remove redundant code paths from Object and Atom. * drastically simplify the contents of Relocation struct (i.e., it is now a simple superset of macho.relocation_info), clean up relocation parsing and resolution logic.
Diffstat (limited to 'src/codegen.zig')
-rw-r--r--src/codegen.zig45
1 files changed, 28 insertions, 17 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index 2a33cca702..49628b93f6 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -3018,7 +3018,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
}
} else if (func_value.castTag(.extern_fn)) |func_payload| {
const decl = func_payload.data;
- const resolv = try macho_file.addExternFn(mem.spanZ(decl.name));
+ const n_strx = try macho_file.addExternFn(mem.spanZ(decl.name));
const offset = blk: {
switch (arch) {
.x86_64 => {
@@ -3039,14 +3039,16 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
// Add relocation to the decl.
try macho_file.active_decl.?.link.macho.relocs.append(self.bin_file.allocator, .{
.offset = offset,
- .where = switch (resolv.where) {
- .local => .local,
- .undef => .undef,
+ .target = .{ .global = n_strx },
+ .addend = 0,
+ .subtractor = null,
+ .pcrel = true,
+ .length = 2,
+ .@"type" = switch (arch) {
+ .aarch64 => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_BRANCH26),
+ .x86_64 => @enumToInt(std.macho.reloc_type_x86_64.X86_64_RELOC_BRANCH),
+ else => unreachable,
},
- .where_index = resolv.where_index,
- .payload = .{ .branch = .{
- .arch = arch,
- } },
});
} else {
return self.fail("TODO implement calling bitcasted functions", .{});
@@ -4540,16 +4542,22 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
// Page reloc for adrp instruction.
try decl.link.macho.relocs.append(self.bin_file.allocator, .{
.offset = offset,
- .where = .local,
- .where_index = @intCast(u32, addr),
- .payload = .{ .page = .{ .kind = .got } },
+ .target = .{ .local = @intCast(u32, addr) },
+ .addend = 0,
+ .subtractor = null,
+ .pcrel = true,
+ .length = 2,
+ .@"type" = @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGE21),
});
// Pageoff reloc for adrp instruction.
try decl.link.macho.relocs.append(self.bin_file.allocator, .{
.offset = offset + 4,
- .where = .local,
- .where_index = @intCast(u32, addr),
- .payload = .{ .page_off = .{ .kind = .got } },
+ .target = .{ .local = @intCast(u32, addr) },
+ .addend = 0,
+ .subtractor = null,
+ .pcrel = false,
+ .length = 2,
+ .@"type" = @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGEOFF12),
});
} else {
return self.fail("TODO implement genSetReg for PIE GOT indirection on this platform", .{});
@@ -4814,9 +4822,12 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
// Load reloc for LEA instruction.
try decl.link.macho.relocs.append(self.bin_file.allocator, .{
.offset = offset - 4,
- .where = .local,
- .where_index = @intCast(u32, x),
- .payload = .{ .load = .{ .kind = .got } },
+ .target = .{ .local = @intCast(u32, x) },
+ .addend = 0,
+ .subtractor = null,
+ .pcrel = true,
+ .length = 2,
+ .@"type" = @enumToInt(std.macho.reloc_type_x86_64.X86_64_RELOC_GOT),
});
} else {
return self.fail("TODO implement genSetReg for PIE GOT indirection on this platform", .{});