authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-09 19:52:44+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-18 09:13:08+02:00
logb96339f6f6ed368d04639c1d6c67eb85c5eb3c7b
tree980fb6e348635ea69e32cebb921788432bf013cf
parent58997363d3c94aaa66960f0e87b40d5f98f0471b

macho: migrate Relocation struct


2 files changed, 60 insertions(+), 40 deletions(-)

src/link/MachO/Relocation.zig+23-11
......@@ -1,4 +1,4 @@
1tag: enum { @"extern", local },
1tag: Tag,
22offset: u32,
33target: u32,
44addend: i64,
......@@ -10,34 +10,44 @@ meta: packed struct {
1010 symbolnum: u24,
1111},
1212
13pub fn getTargetSymbol(rel: Relocation, macho_file: *MachO) *Symbol {
13pub fn getTargetSymbolRef(rel: Relocation, atom: Atom, macho_file: *MachO) MachO.Ref {
1414 assert(rel.tag == .@"extern");
15 return macho_file.getSymbol(rel.target);
15 return atom.getFile(macho_file).getSymbolRef(rel.target, macho_file);
1616}
1717
18pub fn getTargetAtom(rel: Relocation, macho_file: *MachO) *Atom {
18pub fn getTargetSymbol(rel: Relocation, atom: Atom, macho_file: *MachO) *Symbol {
19 assert(rel.tag == .@"extern");
20 const ref = atom.getFile(macho_file).getSymbolRef(rel.target, macho_file);
21 return ref.getSymbol(macho_file).?;
22}
23
24pub fn getTargetAtom(rel: Relocation, atom: Atom, macho_file: *MachO) *Atom {
1925 assert(rel.tag == .local);
20 return macho_file.getAtom(rel.target).?;
26 return atom.getFile(macho_file).getAtom(rel.target).?;
2127}
2228
23pub fn getTargetAddress(rel: Relocation, macho_file: *MachO) u64 {
29pub fn getTargetAddress(rel: Relocation, atom: Atom, macho_file: *MachO) u64 {
2430 return switch (rel.tag) {
25 .local => rel.getTargetAtom(macho_file).getAddress(macho_file),
26 .@"extern" => rel.getTargetSymbol(macho_file).getAddress(.{}, macho_file),
31 .local => rel.getTargetAtom(atom, macho_file).getAddress(macho_file),
32 .@"extern" => rel.getTargetSymbol(atom, macho_file).getAddress(.{}, macho_file),
2733 };
2834}
2935
30pub fn getGotTargetAddress(rel: Relocation, macho_file: *MachO) u64 {
36pub fn getGotTargetAddress(rel: Relocation, atom: Atom, macho_file: *MachO) u64 {
3137 return switch (rel.tag) {
3238 .local => 0,
33 .@"extern" => rel.getTargetSymbol(macho_file).getGotAddress(macho_file),
39 .@"extern" => rel.getTargetSymbol(atom, macho_file).getGotAddress(macho_file),
3440 };
3541}
3642
3743pub fn getZigGotTargetAddress(rel: Relocation, macho_file: *MachO) u64 {
44 const zo = macho_file.getZigObject().?;
3845 return switch (rel.tag) {
3946 .local => 0,
40 .@"extern" => rel.getTargetSymbol(macho_file).getZigGotAddress(macho_file),
47 .@"extern" => {
48 const ref = zo.getSymbolRef(rel.target, macho_file);
49 return ref.getSymbol(macho_file).?.getZigGotAddress(macho_file);
50 },
4151 };
4252}
4353
......@@ -155,6 +165,8 @@ pub const Type = enum {
155165 unsigned,
156166};
157167
168const Tag = enum { local, @"extern" };
169
158170const assert = std.debug.assert;
159171const macho = std.macho;
160172const math = std.math;
src/link/MachO/thunks.zig+37-29
......@@ -5,55 +5,45 @@ pub fn createThunks(sect_id: u8, macho_file: *MachO) !void {
55 const gpa = macho_file.base.comp.gpa;
66 const slice = macho_file.sections.slice();
77 const header = &slice.items(.header)[sect_id];
8 const thnks = &slice.items(.thunks)[sect_id];
89 const atoms = slice.items(.atoms)[sect_id].items;
910 assert(atoms.len > 0);
1011
11 for (atoms) |atom_index| {
12 macho_file.getAtom(atom_index).?.value = @bitCast(@as(i64, -1));
12 for (atoms) |ref| {
13 ref.getAtom(macho_file).?.value = @bitCast(@as(i64, -1));
1314 }
1415
1516 var i: usize = 0;
1617 while (i < atoms.len) {
1718 const start = i;
18 const start_atom = macho_file.getAtom(atoms[start]).?;
19 const start_atom = atoms[start].getAtom(macho_file).?;
1920 assert(start_atom.flags.alive);
20 start_atom.value = try advance(header, start_atom.size, start_atom.alignment);
21 start_atom.value = advance(header, start_atom.size, start_atom.alignment);
2122 i += 1;
2223
2324 while (i < atoms.len and
2425 header.size - start_atom.value < max_allowed_distance) : (i += 1)
2526 {
26 const atom_index = atoms[i];
27 const atom = macho_file.getAtom(atom_index).?;
27 const atom = atoms[i].getAtom(macho_file).?;
2828 assert(atom.flags.alive);
29 atom.value = try advance(header, atom.size, atom.alignment);
29 atom.value = advance(header, atom.size, atom.alignment);
3030 }
3131
3232 // Insert a thunk at the group end
3333 const thunk_index = try macho_file.addThunk();
3434 const thunk = macho_file.getThunk(thunk_index);
3535 thunk.out_n_sect = sect_id;
36 try thnks.append(gpa, thunk_index);
3637
3738 // Scan relocs in the group and create trampolines for any unreachable callsite
38 for (atoms[start..i]) |atom_index| {
39 const atom = macho_file.getAtom(atom_index).?;
40 log.debug("atom({d}) {s}", .{ atom_index, atom.getName(macho_file) });
41 for (atom.getRelocs(macho_file)) |rel| {
42 if (rel.type != .branch) continue;
43 if (isReachable(atom, rel, macho_file)) continue;
44 try thunk.symbols.put(gpa, rel.target, {});
45 }
46 try atom.addExtra(.{ .thunk = thunk_index }, macho_file);
47 atom.flags.thunk = true;
48 }
49
39 try scanRelocs(thunk_index, gpa, atoms[start..i], macho_file);
5040 thunk.value = try advance(header, thunk.size(), .@"4");
5141
5242 log.debug("thunk({d}) : {}", .{ thunk_index, thunk.fmt(macho_file) });
5343 }
5444}
5545
56fn advance(sect: *macho.section_64, size: u64, alignment: Atom.Alignment) !u64 {
46fn advance(sect: *macho.section_64, size: u64, alignment: Atom.Alignment) u64 {
5747 const offset = alignment.forward(sect.size);
5848 const padding = offset - sect.size;
5949 sect.size += padding + size;
......@@ -61,14 +51,32 @@ fn advance(sect: *macho.section_64, size: u64, alignment: Atom.Alignment) !u64 {
6151 return offset;
6252}
6353
54fn scanRelocs(thunk_index: Thunk.Index, gpa: Allocator, atoms: []const MachO.Ref, macho_file: *MachO) !void {
55 const tracy = trace(@src());
56 defer tracy.end();
57
58 const thunk = macho_file.getThunk(thunk_index);
59
60 for (atoms) |ref| {
61 const atom = ref.getAtom(macho_file).?;
62 log.debug("atom({d}) {s}", .{ atom.atom_index, atom.getName(macho_file) });
63 for (atom.getRelocs(macho_file)) |rel| {
64 if (rel.type != .branch) continue;
65 if (isReachable(atom, rel, macho_file)) continue;
66 try thunk.symbols.put(gpa, rel.getTargetSymbolRef(atom.*, macho_file), {});
67 }
68 atom.addExtra(.{ .thunk = thunk_index }, macho_file);
69 }
70}
71
6472fn isReachable(atom: *const Atom, rel: Relocation, macho_file: *MachO) bool {
65 const target = rel.getTargetSymbol(macho_file);
73 const target = rel.getTargetSymbol(atom.*, macho_file);
6674 if (target.flags.stubs or target.flags.objc_stubs) return false;
67 if (atom.out_n_sect != target.out_n_sect) return false;
75 if (atom.out_n_sect != target.getOutputSectionIndex(macho_file)) return false;
6876 const target_atom = target.getAtom(macho_file).?;
6977 if (target_atom.value == @as(u64, @bitCast(@as(i64, -1)))) return false;
7078 const saddr = @as(i64, @intCast(atom.getAddress(macho_file))) + @as(i64, @intCast(rel.offset - atom.off));
71 const taddr: i64 = @intCast(rel.getTargetAddress(macho_file));
79 const taddr: i64 = @intCast(rel.getTargetAddress(atom.*, macho_file));
7280 _ = math.cast(i28, taddr + rel.addend - saddr) orelse return false;
7381 return true;
7482}
......@@ -76,7 +84,7 @@ fn isReachable(atom: *const Atom, rel: Relocation, macho_file: *MachO) bool {
7684pub const Thunk = struct {
7785 value: u64 = 0,
7886 out_n_sect: u8 = 0,
79 symbols: std.AutoArrayHashMapUnmanaged(Symbol.Index, void) = .{},
87 symbols: std.AutoArrayHashMapUnmanaged(MachO.Ref, void) = .{},
8088
8189 pub fn deinit(thunk: *Thunk, allocator: Allocator) void {
8290 thunk.symbols.deinit(allocator);
......@@ -96,8 +104,8 @@ pub const Thunk = struct {
96104 }
97105
98106 pub fn write(thunk: Thunk, macho_file: *MachO, writer: anytype) !void {
99 for (thunk.symbols.keys(), 0..) |sym_index, i| {
100 const sym = macho_file.getSymbol(sym_index);
107 for (thunk.symbols.keys(), 0..) |ref, i| {
108 const sym = ref.getSymbol(macho_file).?;
101109 const saddr = thunk.getAddress(macho_file) + i * trampoline_size;
102110 const taddr = sym.getAddress(.{}, macho_file);
103111 const pages = try aarch64.calcNumberOfPages(@intCast(saddr), @intCast(taddr));
......@@ -144,9 +152,9 @@ pub const Thunk = struct {
144152 const thunk = ctx.thunk;
145153 const macho_file = ctx.macho_file;
146154 try writer.print("@{x} : size({x})\n", .{ thunk.value, thunk.size() });
147 for (thunk.symbols.keys()) |index| {
148 const sym = macho_file.getSymbol(index);
149 try writer.print(" %{d} : {s} : @{x}\n", .{ index, sym.getName(macho_file), sym.value });
155 for (thunk.symbols.keys()) |ref| {
156 const sym = ref.getSymbol(macho_file).?;
157 try writer.print(" {} : {s} : @{x}\n", .{ ref, sym.getName(macho_file), sym.value });
150158 }
151159 }
152160