authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-07 20:49:07+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-13 23:54:31+01:00
log2f7cd7119387b517b07e5feb02a13e69651fe9eb
treefbcef081c1c13775f6778b443cd47aafcdb67842
parentbb691ea16b89c29d98cc2d36bf2af029c5dd1844

macho: fully working PoC with main and exit


2 files changed, 48 insertions(+), 23 deletions(-)

src/link/MachO.zig+45-19
......@@ -1263,9 +1263,10 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
12631263 const stub_h = &text_segment.sections.items[self.stub_helper_section_index.?];
12641264 const data_segment = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
12651265 const la_ptr = &data_segment.sections.items[self.la_symbol_ptr_section_index.?];
1266 while (self.stub_fixups.popOrNull()) |fixup| {
1266 for (self.stub_fixups.items) |fixup, idx| {
1267 const i = @intCast(u32, idx);
12671268 // TODO increment offset for stub writing
1268 const stub_addr = stubs.addr;
1269 const stub_addr = stubs.addr + i * stubs.reserved2;
12691270 const text_addr = symbol.n_value + fixup.start;
12701271 const displacement = @intCast(u32, stub_addr - text_addr);
12711272 var placeholder = code_buffer.items[fixup.start..][0..fixup.len];
......@@ -1274,17 +1275,16 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
12741275 const end = stub_h.addr + self.next_stub_helper_off.? - stub_h.offset;
12751276 var buf: [@sizeOf(u64)]u8 = undefined;
12761277 mem.writeIntLittle(u64, &buf, end);
1277 try self.base.file.?.pwriteAll(&buf, la_ptr.offset);
1278 try self.base.file.?.pwriteAll(&buf, la_ptr.offset + i * @sizeOf(u64));
12781279
1279 const displacement2 = la_ptr.addr - stubs.addr;
1280 const la_ptr_addr = la_ptr.addr + i * @sizeOf(u64);
1281 const displacement2 = la_ptr_addr - stub_addr;
12801282 var ccode: [2 * @sizeOf(u32)]u8 = undefined;
12811283 mem.writeIntLittle(u32, ccode[0..4], aarch64.Instruction.ldr(.x16, .{
12821284 .literal = @intCast(u19, displacement2 / 4),
12831285 }).toU32());
12841286 mem.writeIntLittle(u32, ccode[4..8], aarch64.Instruction.br(.x16).toU32());
1285 try self.base.file.?.pwriteAll(&ccode, stubs.offset);
1286 stubs.size = 2 * @sizeOf(u32);
1287 stubs.reserved2 = 2 * @sizeOf(u32);
1287 try self.base.file.?.pwriteAll(&ccode, stubs.offset + i * stubs.reserved2);
12881288
12891289 const displacement3 = @intCast(i64, stub_h.addr) - @intCast(i64, end + 4);
12901290 var cccode: [3 * @sizeOf(u32)]u8 = undefined;
......@@ -1292,13 +1292,13 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
12921292 .literal = 0x2,
12931293 }).toU32());
12941294 mem.writeIntLittle(u32, cccode[4..8], aarch64.Instruction.b(@intCast(i28, displacement3)).toU32());
1295 mem.writeIntLittle(u32, cccode[8..12], 0);
1295 mem.writeIntLittle(u32, cccode[8..12], i * 0xd);
12961296 try self.base.file.?.pwriteAll(&cccode, self.next_stub_helper_off.?);
12971297 self.next_stub_helper_off = self.next_stub_helper_off.? + 3 * @sizeOf(u32);
12981298
12991299 try self.rebase_info_table.symbols.append(self.base.allocator, .{
13001300 .segment = 3,
1301 .offset = 0,
1301 .offset = i * stubs.reserved2,
13021302 });
13031303 self.rebase_info_dirty = true;
13041304
......@@ -1308,12 +1308,13 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
13081308 mem.copy(u8, name, name_str);
13091309 try self.lazy_binding_info_table.symbols.append(self.base.allocator, .{
13101310 .segment = 3,
1311 .offset = 0,
1311 .offset = i * @sizeOf(u64),
13121312 .dylib_ordinal = 1,
13131313 .name = name,
13141314 });
13151315 self.lazy_binding_info_dirty = true;
13161316 }
1317 self.stub_fixups.shrinkRetainingCapacity(0);
13171318
13181319 const text_section = text_segment.sections.items[self.text_section_index.?];
13191320 const section_offset = symbol.n_value - text_section.addr;
......@@ -1634,7 +1635,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
16341635 .nreloc = 0,
16351636 .flags = flags,
16361637 .reserved1 = 0,
1637 .reserved2 = 0,
1638 .reserved2 = 2 * @sizeOf(u32),
16381639 .reserved3 = 0,
16391640 });
16401641 self.header_dirty = true;
......@@ -2539,20 +2540,45 @@ fn writeIndirectSymbolTable(self: *MachO) !void {
25392540 const la = &data_seg.sections.items[self.la_symbol_ptr_section_index.?];
25402541 const dysymtab = &self.load_commands.items[self.dysymtab_cmd_index.?].Dysymtab;
25412542 dysymtab.nindirectsyms = 0;
2543
2544 var buf: [@sizeOf(u32)]u8 = undefined;
2545 var off = dysymtab.indirectsymoff;
2546 var idx: u32 = 0;
2547
2548 stubs.reserved1 = 0;
25422549 for (self.undef_symbols.items) |sym, i| {
2543 const idx = @intCast(u32, dysymtab.iundefsym + i);
2544 var buf: [@sizeOf(u32)]u8 = undefined;
2545 mem.writeIntLittle(u32, &buf, idx);
2546 try self.base.file.?.pwriteAll(&buf, dysymtab.indirectsymoff + i * @sizeOf(u32));
2550 if (i == self.dyld_stub_binder_index.?) {
2551 continue;
2552 }
2553 const symtab_idx = @intCast(u32, dysymtab.iundefsym + i);
2554 mem.writeIntLittle(u32, &buf, symtab_idx);
2555 try self.base.file.?.pwriteAll(&buf, off);
2556 off += @sizeOf(u32);
2557 dysymtab.nindirectsyms += 1;
2558 idx += 1;
2559 }
2560
2561 got.reserved1 = @intCast(u32, self.undef_symbols.items.len - 1);
2562 if (self.dyld_stub_binder_index) |i| {
2563 const symtab_idx = i + dysymtab.iundefsym;
2564 mem.writeIntLittle(u32, &buf, symtab_idx);
2565 try self.base.file.?.pwriteAll(&buf, off);
2566 off += @sizeOf(u32);
25472567 dysymtab.nindirectsyms += 1;
2568 idx += 1;
2569 }
2570
2571 la.reserved1 = got.reserved1 + 1;
2572 for (self.undef_symbols.items) |sym, i| {
25482573 if (i == self.dyld_stub_binder_index.?) {
2549 got.reserved1 = @intCast(u32, i);
25502574 continue;
25512575 }
2552 stubs.reserved1 = @intCast(u32, i);
2553 try self.base.file.?.pwriteAll(&buf, dysymtab.indirectsymoff + (i + 1) * @sizeOf(u32));
2554 la.reserved1 = @intCast(u32, i + 1);
2576 const symtab_idx = @intCast(u32, dysymtab.iundefsym + i);
2577 mem.writeIntLittle(u32, &buf, symtab_idx);
2578 try self.base.file.?.pwriteAll(&buf, off);
2579 off += @sizeOf(u32);
25552580 dysymtab.nindirectsyms += 1;
2581 idx += 1;
25562582 }
25572583}
25582584
src/link/MachO/imports.zig+3-4
......@@ -21,9 +21,8 @@ pub const RebaseInfoTable = struct {
2121
2222 /// Write the rebase info table to byte stream.
2323 pub fn write(self: RebaseInfoTable, writer: anytype) !void {
24 try writer.writeByte(macho.REBASE_OPCODE_SET_TYPE_IMM | @truncate(u4, self.rebase_type));
25
2624 for (self.symbols.items) |symbol| {
25 try writer.writeByte(macho.REBASE_OPCODE_SET_TYPE_IMM | @truncate(u4, self.rebase_type));
2726 try writer.writeByte(macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @truncate(u4, symbol.segment));
2827 try leb.writeILEB128(writer, symbol.offset);
2928 try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | @truncate(u4, 1));
......@@ -36,10 +35,10 @@ pub const RebaseInfoTable = struct {
3635 pub fn calcSize(self: *RebaseInfoTable) !u64 {
3736 var stream = std.io.countingWriter(std.io.null_writer);
3837 var writer = stream.writer();
39 var size: u64 = 1;
38 var size: u64 = 0;
4039
4140 for (self.symbols.items) |symbol| {
42 size += 1;
41 size += 2;
4342 try leb.writeILEB128(writer, symbol.offset);
4443 size += 1;
4544 }