authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-03 10:52:29+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-03 10:52:29+01:00
log0a0f90f949942b19950455647bc955b369407dd4
tree44debca2e688edfcee5db118bbc4c5967dd304f3
parentaa1aa9886333e8b260901572142a227e5544f4df

macho: migrate thunks to use new relative addressing mechanism


4 files changed, 11 insertions(+), 16 deletions(-)

src/link/MachO.zig+1-10
......@@ -596,7 +596,6 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node
596596
597597 try self.allocateSections();
598598 self.allocateSegments();
599 self.allocateAtoms();
600599 self.allocateSyntheticSymbols();
601600 try self.allocateLinkeditSegment();
602601
......@@ -2392,14 +2391,6 @@ fn allocateSegments(self: *MachO) void {
23922391 }
23932392}
23942393
2395pub fn allocateAtoms(self: *MachO) void {
2396 // TODO: redo this like atoms
2397 for (self.thunks.items) |*thunk| {
2398 const header = self.sections.items(.header)[thunk.out_n_sect];
2399 thunk.value += header.addr;
2400 }
2401}
2402
24032394fn allocateSyntheticSymbols(self: *MachO) void {
24042395 const text_seg = self.getTextSegment();
24052396
......@@ -2608,7 +2599,7 @@ fn writeAtoms(self: *MachO) !void {
26082599
26092600 for (self.thunks.items) |thunk| {
26102601 const header = slice.items(.header)[thunk.out_n_sect];
2611 const offset = thunk.value - header.addr + header.offset;
2602 const offset = thunk.value + header.offset;
26122603 const buffer = try gpa.alloc(u8, thunk.size());
26132604 defer gpa.free(buffer);
26142605 var stream = std.io.fixedBufferStream(buffer);
src/link/MachO/Atom.zig+1-1
......@@ -706,7 +706,7 @@ fn resolveRelocInner(
706706 .aarch64 => {
707707 const disp: i28 = math.cast(i28, S + A - P) orelse blk: {
708708 const thunk = self.getThunk(macho_file);
709 const S_: i64 = @intCast(thunk.getAddress(rel.target));
709 const S_: i64 = @intCast(thunk.getTargetAddress(rel.target, macho_file));
710710 break :blk math.cast(i28, S_ + A - P) orelse return error.Overflow;
711711 };
712712 var inst = aarch64.Instruction{
src/link/MachO/relocatable.zig-1
......@@ -61,7 +61,6 @@ pub fn flush(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u
6161 try createSegment(macho_file);
6262 try allocateSections(macho_file);
6363 allocateSegment(macho_file);
64 macho_file.allocateAtoms();
6564
6665 var off = off: {
6766 const seg = macho_file.segments.items[0];
src/link/MachO/thunks.zig+9-4
......@@ -66,7 +66,7 @@ fn isReachable(atom: *const Atom, rel: Relocation, macho_file: *MachO) bool {
6666 if (atom.out_n_sect != target.out_n_sect) return false;
6767 const target_atom = target.getAtom(macho_file).?;
6868 if (target_atom.value == @as(u64, @bitCast(@as(i64, -1)))) return false;
69 const saddr = @as(i64, @intCast(atom.value)) + @as(i64, @intCast(rel.offset - atom.off));
69 const saddr = @as(i64, @intCast(atom.getAddress(macho_file))) + @as(i64, @intCast(rel.offset - atom.off));
7070 const taddr: i64 = @intCast(rel.getTargetAddress(macho_file));
7171 _ = math.cast(i28, taddr + rel.addend - saddr) orelse return false;
7272 return true;
......@@ -85,14 +85,19 @@ pub const Thunk = struct {
8585 return thunk.symbols.keys().len * trampoline_size;
8686 }
8787
88 pub fn getAddress(thunk: Thunk, sym_index: Symbol.Index) u64 {
89 return thunk.value + thunk.symbols.getIndex(sym_index).? * trampoline_size;
88 pub fn getAddress(thunk: Thunk, macho_file: *MachO) u64 {
89 const header = macho_file.sections.items(.header)[thunk.out_n_sect];
90 return header.addr + thunk.value;
91 }
92
93 pub fn getTargetAddress(thunk: Thunk, sym_index: Symbol.Index, macho_file: *MachO) u64 {
94 return thunk.getAddress(macho_file) + thunk.symbols.getIndex(sym_index).? * trampoline_size;
9095 }
9196
9297 pub fn write(thunk: Thunk, macho_file: *MachO, writer: anytype) !void {
9398 for (thunk.symbols.keys(), 0..) |sym_index, i| {
9499 const sym = macho_file.getSymbol(sym_index);
95 const saddr = thunk.value + i * trampoline_size;
100 const saddr = thunk.getAddress(macho_file) + i * trampoline_size;
96101 const taddr = sym.getAddress(.{}, macho_file);
97102 const pages = try Relocation.calcNumberOfPages(saddr, taddr);
98103 try writer.writeInt(u32, aarch64.Instruction.adrp(.x16, pages).toU32(), .little);