Skip to content

Commit

Permalink
macho: redo relocation handling and lazy bind globals
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
kubkon committed Oct 13, 2021
1 parent a3104a4 commit fc302f0
Show file tree
Hide file tree
Showing 6 changed files with 857 additions and 1,219 deletions.
45 changes: 28 additions & 17 deletions src/codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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", .{});
Expand Down Expand Up @@ -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", .{});
Expand Down Expand Up @@ -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", .{});
Expand Down
Loading

0 comments on commit fc302f0

Please sign in to comment.