authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-10 00:21:34+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-13 23:55:37+01:00
logf0d7ec6f33634edb0ddb3ba5d5b306e9f2de5418
tree0e0d7082e8d828d9dcefc668634e2aba45059643
parent1b91a9f4c840741e77e5c7ce826c5f80c9f9ee80

macho: add x86_64 support


2 files changed, 93 insertions(+), 20 deletions(-)

src/codegen.zig+19-3
......@@ -1876,14 +1876,30 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
18761876 });
18771877 break :blk index;
18781878 };
1879 const start = self.code.items.len;
1880 const len: usize = blk: {
1881 switch (arch) {
1882 .x86_64 => {
1883 // callq
1884 try self.code.ensureCapacity(self.code.items.len + 5);
1885 self.code.appendSliceAssumeCapacity(&[5]u8{ 0xe8, 0x0, 0x0, 0x0, 0x0 });
1886 break :blk 5;
1887 },
1888 .aarch64 => {
1889 // bl
1890 writeInt(u32, try self.code.addManyAsArray(4), 0);
1891 break :blk 4;
1892 },
1893 else => unreachable, // unsupported architecture on MachO
1894 }
1895 };
18791896 try macho_file.stub_fixups.append(self.bin_file.allocator, .{
18801897 .symbol = symbol,
18811898 .already_defined = already_defined,
1882 .start = self.code.items.len,
1883 .len = 4,
1899 .start = start,
1900 .len = len,
18841901 });
18851902 // We mark the space and fix it up later.
1886 writeInt(u32, try self.code.addManyAsArray(4), 0);
18871903 } else {
18881904 return self.fail(inst.base.src, "TODO implement calling bitcasted functions", .{});
18891905 }
src/link/MachO.zig+74-17
......@@ -1241,14 +1241,18 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
12411241 for (self.stub_fixups.items) |fixup| {
12421242 const stub_addr = stubs.addr + fixup.symbol * stubs.reserved2;
12431243 const text_addr = symbol.n_value + fixup.start;
1244 const displacement = @intCast(u32, stub_addr - text_addr);
1245 var placeholder = code_buffer.items[fixup.start..][0..fixup.len];
12461244 switch (self.base.options.target.cpu.arch) {
1247 .x86_64 => return error.TODOImplementStubFixupsForx86_64,
1245 .x86_64 => {
1246 const displacement = @intCast(u32, stub_addr - text_addr - fixup.len);
1247 var placeholder = code_buffer.items[fixup.start + fixup.len - @sizeOf(u32) ..][0..@sizeOf(u32)];
1248 mem.writeIntSliceLittle(u32, placeholder, displacement);
1249 },
12481250 .aarch64 => {
1251 const displacement = @intCast(u32, stub_addr - text_addr);
1252 var placeholder = code_buffer.items[fixup.start..][0..fixup.len];
12491253 mem.writeIntSliceLittle(u32, placeholder, aarch64.Instruction.bl(@intCast(i28, displacement)).toU32());
12501254 },
1251 else => unreachable,
1255 else => unreachable, // unsupported target architecture
12521256 }
12531257 if (!fixup.already_defined) {
12541258 try self.writeStub(fixup.symbol);
......@@ -1565,6 +1569,11 @@ pub fn populateMissingMetadata(self: *MachO) !void {
15651569 .aarch64 => 2,
15661570 else => unreachable, // unhandled architecture type
15671571 };
1572 const stub_size: u4 = switch (self.base.options.target.cpu.arch) {
1573 .x86_64 => 6,
1574 .aarch64 => 2 * @sizeOf(u32),
1575 else => unreachable, // unhandled architecture type
1576 };
15681577 const flags = macho.S_SYMBOL_STUBS | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS;
15691578 const needed_size = @sizeOf(u64) * self.base.options.symbol_count_hint;
15701579 const off = text_segment.findFreeSpace(needed_size, @alignOf(u64), self.header_pad);
......@@ -1583,7 +1592,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
15831592 .nreloc = 0,
15841593 .flags = flags,
15851594 .reserved1 = 0,
1586 .reserved2 = 2 * @sizeOf(u32),
1595 .reserved2 = stub_size,
15871596 .reserved3 = 0,
15881597 });
15891598 self.header_dirty = true;
......@@ -2044,7 +2053,30 @@ pub fn populateMissingMetadata(self: *MachO) !void {
20442053 const data_const_segment = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
20452054 const got = &data_const_segment.sections.items[self.data_got_section_index.?];
20462055 switch (self.base.options.target.cpu.arch) {
2047 .x86_64 => return error.TODOImplementStubHelperForX86_64,
2056 .x86_64 => {
2057 const code_size = 15;
2058 var code: [code_size]u8 = undefined;
2059 // lea %r11, [rip + disp]
2060 code[0] = 0x4c;
2061 code[1] = 0x8d;
2062 code[2] = 0x1d;
2063 {
2064 const displacement = @intCast(u32, data.addr - stub_helper.addr - 7);
2065 mem.writeIntLittle(u32, code[3..7], displacement);
2066 }
2067 // push %r11
2068 code[7] = 0x41;
2069 code[8] = 0x53;
2070 // jmp [rip + disp]
2071 code[9] = 0xff;
2072 code[10] = 0x25;
2073 {
2074 const displacement = @intCast(u32, got.addr - stub_helper.addr - code_size);
2075 mem.writeIntLittle(u32, code[11..], displacement);
2076 }
2077 self.stub_helper_stubs_start_off = stub_helper.offset + code_size;
2078 try self.base.file.?.pwriteAll(&code, stub_helper.offset);
2079 },
20482080 .aarch64 => {
20492081 var code: [4 * @sizeOf(u32)]u8 = undefined;
20502082 {
......@@ -2410,7 +2442,12 @@ fn writeLazySymbolPointer(self: *MachO, index: u32) !void {
24102442 const data_segment = self.load_commands.items[self.data_segment_cmd_index.?].Segment;
24112443 const la_symbol_ptr = data_segment.sections.items[self.la_symbol_ptr_section_index.?];
24122444
2413 const stub_off = self.stub_helper_stubs_start_off.? + index * 3 * @sizeOf(u32);
2445 const stub_size: u4 = switch (self.base.options.target.cpu.arch) {
2446 .x86_64 => 10,
2447 .aarch64 => 3 * @sizeOf(u32),
2448 else => unreachable,
2449 };
2450 const stub_off = self.stub_helper_stubs_start_off.? + index * stub_size;
24142451 const end = stub_helper.addr + stub_off - stub_helper.offset;
24152452 var buf: [@sizeOf(u64)]u8 = undefined;
24162453 mem.writeIntLittle(u64, &buf, end);
......@@ -2428,42 +2465,62 @@ fn writeStub(self: *MachO, index: u32) !void {
24282465 const stub_off = stubs.offset + index * stubs.reserved2;
24292466 const stub_addr = stubs.addr + index * stubs.reserved2;
24302467 const la_ptr_addr = la_symbol_ptr.addr + index * @sizeOf(u64);
2431 const displacement = la_ptr_addr - stub_addr;
24322468 log.debug("writing stub at 0x{x}", .{stub_off});
2469 var code = try self.base.allocator.alloc(u8, stubs.reserved2);
2470 defer self.base.allocator.free(code);
24332471 switch (self.base.options.target.cpu.arch) {
2434 .x86_64 => return error.TODOImplementWritingStubsForx86_64,
2472 .x86_64 => {
2473 const displacement = @intCast(u32, la_ptr_addr - stub_addr - stubs.reserved2);
2474 // jmp
2475 code[0] = 0xff;
2476 code[1] = 0x25;
2477 mem.writeIntLittle(u32, code[2..][0..4], displacement);
2478 },
24352479 .aarch64 => {
2436 var code: [2 * @sizeOf(u32)]u8 = undefined;
2480 const displacement = la_ptr_addr - stub_addr;
24372481 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.ldr(.x16, .{
24382482 .literal = @intCast(u19, displacement / 4),
24392483 }).toU32());
24402484 mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.br(.x16).toU32());
2441 try self.base.file.?.pwriteAll(&code, stub_off);
24422485 },
24432486 else => unreachable,
24442487 }
2488 try self.base.file.?.pwriteAll(code, stub_off);
24452489}
24462490
24472491fn writeStubInStubHelper(self: *MachO, index: u32) !void {
24482492 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
24492493 const stub_helper = text_segment.sections.items[self.stub_helper_section_index.?];
24502494
2451 const stub_off = self.stub_helper_stubs_start_off.? + index * 3 * @sizeOf(u32);
2452 const end = stub_helper.addr + stub_off - stub_helper.offset;
2453 const displacement = @intCast(i64, stub_helper.addr) - @intCast(i64, end + 4);
2495 const stub_size: u4 = switch (self.base.options.target.cpu.arch) {
2496 .x86_64 => 10,
2497 .aarch64 => 3 * @sizeOf(u32),
2498 else => unreachable,
2499 };
2500 const stub_off = self.stub_helper_stubs_start_off.? + index * stub_size;
2501 var code = try self.base.allocator.alloc(u8, stub_size);
2502 defer self.base.allocator.free(code);
24542503 switch (self.base.options.target.cpu.arch) {
2455 .x86_64 => return error.TODOImplementWritingStubsInStubHelperForx86_64,
2504 .x86_64 => {
2505 const displacement = @intCast(i32, @intCast(i64, stub_helper.offset) - @intCast(i64, stub_off) - stub_size);
2506 // pushq
2507 code[0] = 0x68;
2508 mem.writeIntLittle(u32, code[1..][0..4], index * 0xd); // TODO
2509 // jmpq
2510 code[5] = 0xe9;
2511 mem.writeIntLittle(u32, code[6..][0..4], @bitCast(u32, displacement));
2512 },
24562513 .aarch64 => {
2457 var code: [3 * @sizeOf(u32)]u8 = undefined;
2514 const displacement = @intCast(i64, stub_helper.offset) - @intCast(i64, stub_off) - 4;
24582515 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.ldr(.w16, .{
24592516 .literal = 0x2,
24602517 }).toU32());
24612518 mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.b(@intCast(i28, displacement)).toU32());
24622519 mem.writeIntLittle(u32, code[8..12], index * 0xd); // TODO This is the size of lazy binding opcode block.
2463 try self.base.file.?.pwriteAll(&code, stub_off);
24642520 },
24652521 else => unreachable,
24662522 }
2523 try self.base.file.?.pwriteAll(code, stub_off);
24672524}
24682525
24692526fn relocateSymbolTable(self: *MachO) !void {