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.
This commit is contained in:
Jakub Konka
2021-10-10 10:33:15 +02:00
parent a3104a4a78
commit fc302f00a9
6 changed files with 869 additions and 1231 deletions

View File

@@ -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", .{});