authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-01 00:24:06+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-07 22:42:55+02:00
logaac4c1d3b225ff4cd7138d9aae599c9540c7f04e
tree69930c62692421f54d7e3587ceaf12370bf6ca16
parent0ebeb58d91b23acbd2ad3a168af19459af63a8f6

coff: fix contents of IAT, and ensure codegen loads addr into reg

As far as I can see, unlike with MachO, we don't have any stubs helper routines available and need to load a bound pointer into a register to then call it.

4 files changed, 140 insertions(+), 27 deletions(-)

src/arch/x86_64/CodeGen.zig+60-16
......@@ -137,6 +137,7 @@ pub const MCValue = union(enum) {
137137 /// If the type is a pointer, it means the pointer is referenced indirectly via GOT.
138138 /// When lowered, linker will emit a relocation of type X86_64_RELOC_GOT.
139139 got_load: u32,
140 imports_load: u32,
140141 /// The value is in memory referenced directly via symbol index.
141142 /// If the type is a pointer, it means the pointer is referenced directly via symbol index.
142143 /// When lowered, linker will emit a relocation of type X86_64_RELOC_SIGNED.
......@@ -156,6 +157,7 @@ pub const MCValue = union(enum) {
156157 .ptr_stack_offset,
157158 .direct_load,
158159 .got_load,
160 .imports_load,
159161 => true,
160162 else => false,
161163 };
......@@ -2274,6 +2276,7 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void {
22742276 .memory,
22752277 .got_load,
22762278 .direct_load,
2279 .imports_load,
22772280 => {
22782281 try self.loadMemPtrIntoRegister(addr_reg, Type.usize, array);
22792282 },
......@@ -2618,6 +2621,7 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo
26182621 .memory,
26192622 .got_load,
26202623 .direct_load,
2624 .imports_load,
26212625 => {
26222626 const reg = try self.copyToTmpRegister(ptr_ty, ptr);
26232627 try self.load(dst_mcv, .{ .register = reg }, ptr_ty);
......@@ -2655,6 +2659,7 @@ fn loadMemPtrIntoRegister(self: *Self, reg: Register, ptr_ty: Type, ptr: MCValue
26552659 switch (ptr) {
26562660 .got_load,
26572661 .direct_load,
2662 .imports_load,
26582663 => |sym_index| {
26592664 const abi_size = @intCast(u32, ptr_ty.abiSize(self.target.*));
26602665 const mod = self.bin_file.options.module.?;
......@@ -2666,6 +2671,7 @@ fn loadMemPtrIntoRegister(self: *Self, reg: Register, ptr_ty: Type, ptr: MCValue
26662671 const flags: u2 = switch (ptr) {
26672672 .got_load => 0b00,
26682673 .direct_load => 0b01,
2674 .imports_load => 0b10,
26692675 else => unreachable,
26702676 };
26712677 _ = try self.addInst(.{
......@@ -2763,6 +2769,7 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
27632769 },
27642770 .got_load,
27652771 .direct_load,
2772 .imports_load,
27662773 .memory,
27672774 .stack_offset,
27682775 => {
......@@ -2783,6 +2790,7 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
27832790 },
27842791 .got_load,
27852792 .direct_load,
2793 .imports_load,
27862794 .memory,
27872795 => {
27882796 const value_lock: ?RegisterLock = switch (value) {
......@@ -2854,6 +2862,7 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
28542862 },
28552863 .got_load,
28562864 .direct_load,
2865 .imports_load,
28572866 .memory,
28582867 => {
28592868 if (abi_size <= 8) {
......@@ -3565,6 +3574,7 @@ fn genBinOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MCValu
35653574 .memory,
35663575 .got_load,
35673576 .direct_load,
3577 .imports_load,
35683578 .eflags,
35693579 => {
35703580 assert(abi_size <= 8);
......@@ -3650,7 +3660,10 @@ fn genBinOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MCValu
36503660 => {
36513661 return self.fail("TODO implement x86 ADD/SUB/CMP source memory", .{});
36523662 },
3653 .got_load, .direct_load => {
3663 .got_load,
3664 .direct_load,
3665 .imports_load,
3666 => {
36543667 return self.fail("TODO implement x86 ADD/SUB/CMP source symbol at index in linker", .{});
36553668 },
36563669 .eflags => {
......@@ -3661,7 +3674,10 @@ fn genBinOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MCValu
36613674 .memory => {
36623675 return self.fail("TODO implement x86 ADD/SUB/CMP destination memory", .{});
36633676 },
3664 .got_load, .direct_load => {
3677 .got_load,
3678 .direct_load,
3679 .imports_load,
3680 => {
36653681 return self.fail("TODO implement x86 ADD/SUB/CMP destination symbol at index", .{});
36663682 },
36673683 }
......@@ -3729,7 +3745,10 @@ fn genIntMulComplexOpMir(self: *Self, dst_ty: Type, dst_mcv: MCValue, src_mcv: M
37293745 .memory => {
37303746 return self.fail("TODO implement x86 multiply source memory", .{});
37313747 },
3732 .got_load, .direct_load => {
3748 .got_load,
3749 .direct_load,
3750 .imports_load,
3751 => {
37333752 return self.fail("TODO implement x86 multiply source symbol at index in linker", .{});
37343753 },
37353754 .eflags => {
......@@ -3773,7 +3792,10 @@ fn genIntMulComplexOpMir(self: *Self, dst_ty: Type, dst_mcv: MCValue, src_mcv: M
37733792 .memory, .stack_offset => {
37743793 return self.fail("TODO implement x86 multiply source memory", .{});
37753794 },
3776 .got_load, .direct_load => {
3795 .got_load,
3796 .direct_load,
3797 .imports_load,
3798 => {
37773799 return self.fail("TODO implement x86 multiply source symbol at index in linker", .{});
37783800 },
37793801 .eflags => {
......@@ -3784,7 +3806,10 @@ fn genIntMulComplexOpMir(self: *Self, dst_ty: Type, dst_mcv: MCValue, src_mcv: M
37843806 .memory => {
37853807 return self.fail("TODO implement x86 multiply destination memory", .{});
37863808 },
3787 .got_load, .direct_load => {
3809 .got_load,
3810 .direct_load,
3811 .imports_load,
3812 => {
37883813 return self.fail("TODO implement x86 multiply destination symbol at index in linker", .{});
37893814 },
37903815 }
......@@ -3948,6 +3973,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
39483973 .memory => unreachable,
39493974 .got_load => unreachable,
39503975 .direct_load => unreachable,
3976 .imports_load => unreachable,
39513977 .eflags => unreachable,
39523978 .register_overflow => unreachable,
39533979 }
......@@ -4025,15 +4051,16 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
40254051 });
40264052 }
40274053 const sym_index = try coff_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
4054 try self.genSetReg(Type.initTag(.usize), .rax, .{
4055 .imports_load = sym_index,
4056 });
40284057 _ = try self.addInst(.{
4029 .tag = .call_extern,
4030 .ops = undefined,
4031 .data = .{
4032 .relocation = .{
4033 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.coff.sym_index,
4034 .sym_index = sym_index,
4035 },
4036 },
4058 .tag = .call,
4059 .ops = Mir.Inst.Ops.encode(.{
4060 .reg1 = .rax,
4061 .flags = 0b01,
4062 }),
4063 .data = undefined,
40374064 });
40384065 } else {
40394066 return self.fail("TODO implement calling bitcasted functions", .{});
......@@ -4443,7 +4470,11 @@ fn genVarDbgInfo(
44434470 leb128.writeILEB128(dbg_info.writer(), -off) catch unreachable;
44444471 dbg_info.items[fixup] += @intCast(u8, dbg_info.items.len - fixup - 2);
44454472 },
4446 .memory, .got_load, .direct_load => {
4473 .memory,
4474 .got_load,
4475 .direct_load,
4476 .imports_load,
4477 => {
44474478 const ptr_width = @intCast(u8, @divExact(self.target.cpu.arch.ptrBitWidth(), 8));
44484479 const is_ptr = switch (tag) {
44494480 .dbg_var_ptr => true,
......@@ -4474,7 +4505,10 @@ fn genVarDbgInfo(
44744505 try dbg_info.append(DW.OP.deref);
44754506 }
44764507 switch (mcv) {
4477 .got_load, .direct_load => |index| try dw.addExprlocReloc(index, offset, is_ptr),
4508 .got_load,
4509 .direct_load,
4510 .imports_load,
4511 => |index| try dw.addExprlocReloc(index, offset, is_ptr),
44784512 else => {},
44794513 }
44804514 },
......@@ -5474,6 +5508,7 @@ fn genSetStackArg(self: *Self, ty: Type, stack_offset: i32, mcv: MCValue) InnerE
54745508 .memory,
54755509 .direct_load,
54765510 .got_load,
5511 .imports_load,
54775512 => {
54785513 if (abi_size <= 8) {
54795514 const reg = try self.copyToTmpRegister(ty, mcv);
......@@ -5721,6 +5756,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: i32, mcv: MCValue, opts: Inl
57215756 .memory,
57225757 .got_load,
57235758 .direct_load,
5759 .imports_load,
57245760 => {
57255761 if (abi_size <= 8) {
57265762 const reg = try self.copyToTmpRegister(ty, mcv);
......@@ -5848,6 +5884,7 @@ fn genInlineMemcpy(
58485884 .memory,
58495885 .got_load,
58505886 .direct_load,
5887 .imports_load,
58515888 => {
58525889 try self.loadMemPtrIntoRegister(dst_addr_reg, Type.usize, dst_ptr);
58535890 },
......@@ -5883,6 +5920,7 @@ fn genInlineMemcpy(
58835920 .memory,
58845921 .got_load,
58855922 .direct_load,
5923 .imports_load,
58865924 => {
58875925 try self.loadMemPtrIntoRegister(src_addr_reg, Type.usize, src_ptr);
58885926 },
......@@ -6021,6 +6059,7 @@ fn genInlineMemset(
60216059 .memory,
60226060 .got_load,
60236061 .direct_load,
6062 .imports_load,
60246063 => {
60256064 try self.loadMemPtrIntoRegister(addr_reg, Type.usize, dst_ptr);
60266065 },
......@@ -6261,6 +6300,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
62616300 },
62626301 .direct_load,
62636302 .got_load,
6303 .imports_load,
62646304 => {
62656305 switch (ty.zigTypeTag()) {
62666306 .Float => {
......@@ -6655,7 +6695,11 @@ fn airMemcpy(self: *Self, inst: Air.Inst.Index) !void {
66556695 // TODO Is this the only condition for pointer dereference for memcpy?
66566696 const src: MCValue = blk: {
66576697 switch (src_ptr) {
6658 .got_load, .direct_load, .memory => {
6698 .got_load,
6699 .direct_load,
6700 .imports_load,
6701 .memory,
6702 => {
66596703 const reg = try self.register_manager.allocReg(null, gp);
66606704 try self.loadMemPtrIntoRegister(reg, src_ty, src_ptr);
66616705 _ = try self.addInst(.{
src/arch/x86_64/Emit.zig+3-2
......@@ -985,8 +985,8 @@ fn mirLeaPic(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
985985 const relocation = emit.mir.instructions.items(.data)[inst].relocation;
986986
987987 switch (ops.flags) {
988 0b00, 0b01 => {},
989 else => return emit.fail("TODO unused LEA PIC variants 0b10 and 0b11", .{}),
988 0b00, 0b01, 0b10 => {},
989 else => return emit.fail("TODO unused LEA PIC variant 0b11", .{}),
990990 }
991991
992992 // lea reg1, [rip + reloc]
......@@ -1024,6 +1024,7 @@ fn mirLeaPic(emit: *Emit, inst: Mir.Inst.Index) InnerError!void {
10241024 .@"type" = switch (ops.flags) {
10251025 0b00 => .got,
10261026 0b01 => .direct,
1027 0b10 => .imports,
10271028 else => unreachable,
10281029 },
10291030 .target = .{ .sym_index = relocation.sym_index, .file = null },
src/arch/x86_64/Mir.zig+1
......@@ -180,6 +180,7 @@ pub const Inst = struct {
180180 /// ops flags: form:
181181 /// 0b00 reg1, [rip + reloc] // via GOT PIC
182182 /// 0b01 reg1, [rip + reloc] // direct load PIC
183 /// 0b10 reg1, [rip + reloc] // via imports table PIC
183184 /// Notes:
184185 /// * `Data` contains `relocation`
185186 lea_pic,
src/link/Coff.zig+76-9
......@@ -123,6 +123,7 @@ pub const Reloc = struct {
123123 @"type": enum {
124124 got,
125125 direct,
126 imports,
126127 },
127128 target: SymbolWithLoc,
128129 offset: u32,
......@@ -812,18 +813,18 @@ fn resolveRelocs(self: *Coff, atom: *Atom) !void {
812813 break :blk got_atom.getSymbol(self).value;
813814 },
814815 .direct => blk: {
815 if (self.getImportAtomForSymbol(reloc.target)) |import_atom| {
816 break :blk import_atom.getSymbol(self).value;
817 }
818816 break :blk self.getSymbol(reloc.target).value;
819817 },
818 .imports => blk: {
819 const import_atom = self.getImportAtomForSymbol(reloc.target) orelse continue;
820 break :blk import_atom.getSymbol(self).value;
821 },
820822 };
821823 const target_vaddr_with_addend = target_vaddr + reloc.addend;
822
823824 if (target_vaddr_with_addend == reloc.prev_vaddr) continue;
824825
825826 log.debug(" ({x}: [() => 0x{x} ({s})) ({s})", .{
826 reloc.offset,
827 source_sym.value + reloc.offset,
827828 target_vaddr_with_addend,
828829 self.getSymbolName(reloc.target),
829830 @tagName(reloc.@"type"),
......@@ -833,7 +834,7 @@ fn resolveRelocs(self: *Coff, atom: *Atom) !void {
833834 const source_vaddr = source_sym.value + reloc.offset;
834835 const disp = target_vaddr_with_addend - source_vaddr - 4;
835836 try self.base.file.?.pwriteAll(mem.asBytes(&@intCast(u32, disp)), file_offset + reloc.offset);
836 return;
837 continue;
837838 }
838839
839840 switch (self.ptr_width) {
......@@ -1345,8 +1346,8 @@ pub fn flushModule(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
13451346 try self.resolveRelocs(atom.*);
13461347 }
13471348 }
1348 try self.writeBaseRelocations();
13491349 try self.writeImportTable();
1350 try self.writeBaseRelocations();
13501351
13511352 if (self.getEntryPoint()) |entry_sym_loc| {
13521353 self.entry_addr = self.getSymbol(entry_sym_loc).value;
......@@ -1487,14 +1488,80 @@ fn writeBaseRelocations(self: *Coff) !void {
14871488
14881489fn writeImportTable(self: *Coff) !void {
14891490 const gpa = self.base.allocator;
1490 _ = gpa;
14911491
14921492 const section = self.sections.get(self.idata_section_index.?);
14931493 const iat_rva = section.header.virtual_address;
14941494 const iat_size = blk: {
14951495 const last_atom = section.last_atom.?;
1496 break :blk last_atom.getSymbol(self).value + last_atom.size - iat_rva;
1496 break :blk last_atom.getSymbol(self).value + last_atom.size * 2 - iat_rva; // account for sentinel zero pointer
1497 };
1498
1499 const dll_name = "KERNEL32.dll";
1500
1501 var import_dir_entry = coff.ImportDirectoryEntry{
1502 .import_lookup_table_rva = @sizeOf(coff.ImportDirectoryEntry) * 2,
1503 .time_date_stamp = 0,
1504 .forwarder_chain = 0,
1505 .name_rva = 0,
1506 .import_address_table_rva = iat_rva,
1507 };
1508
1509 // TODO: we currently assume there's only one (implicit) DLL - ntdll
1510 var lookup_table = std.ArrayList(coff.ImportLookupEntry64.ByName).init(gpa);
1511 defer lookup_table.deinit();
1512
1513 var names_table = std.ArrayList(u8).init(gpa);
1514 defer names_table.deinit();
1515
1516 // TODO: check if import is still valid
1517 for (self.imports_table.keys()) |target| {
1518 const target_name = self.getSymbolName(target);
1519 const start = names_table.items.len;
1520 mem.writeIntLittle(u16, try names_table.addManyAsArray(2), 0); // TODO: currently, hint is set to 0 as we haven't yet parsed any DLL
1521 try names_table.appendSlice(target_name);
1522 try names_table.append(0);
1523 const end = names_table.items.len;
1524 if (!mem.isAlignedGeneric(usize, end - start, @sizeOf(u16))) {
1525 try names_table.append(0);
1526 }
1527 try lookup_table.append(.{ .name_table_rva = @intCast(u31, start) });
1528 }
1529 try lookup_table.append(.{ .name_table_rva = 0 }); // the sentinel
1530
1531 const dir_entry_size = @sizeOf(coff.ImportDirectoryEntry) + lookup_table.items.len * @sizeOf(coff.ImportLookupEntry64.ByName) + names_table.items.len + dll_name.len + 1;
1532 const needed_size = iat_size + dir_entry_size + @sizeOf(coff.ImportDirectoryEntry);
1533 const sect_capacity = self.allocatedSize(section.header.pointer_to_raw_data);
1534 assert(needed_size < sect_capacity); // TODO: implement expanding .idata section
1535
1536 // Fixup offsets
1537 const base_rva = iat_rva + iat_size;
1538 import_dir_entry.import_lookup_table_rva += base_rva;
1539 import_dir_entry.name_rva = @intCast(u32, base_rva + dir_entry_size + @sizeOf(coff.ImportDirectoryEntry) - dll_name.len - 1);
1540
1541 for (lookup_table.items[0 .. lookup_table.items.len - 1]) |*lk| {
1542 lk.name_table_rva += @intCast(u31, base_rva + @sizeOf(coff.ImportDirectoryEntry) * 2 + lookup_table.items.len * @sizeOf(coff.ImportLookupEntry64.ByName));
1543 }
1544
1545 var buffer = std.ArrayList(u8).init(gpa);
1546 defer buffer.deinit();
1547 try buffer.ensureTotalCapacity(dir_entry_size + @sizeOf(coff.ImportDirectoryEntry));
1548 buffer.appendSliceAssumeCapacity(mem.asBytes(&import_dir_entry));
1549 buffer.appendNTimesAssumeCapacity(0, @sizeOf(coff.ImportDirectoryEntry)); // the sentinel; TODO: I think doing all of the above on bytes directly might be cleaner
1550 buffer.appendSliceAssumeCapacity(mem.sliceAsBytes(lookup_table.items));
1551 buffer.appendSliceAssumeCapacity(names_table.items);
1552 buffer.appendSliceAssumeCapacity(dll_name);
1553 buffer.appendAssumeCapacity(0);
1554
1555 try self.base.file.?.pwriteAll(buffer.items, section.header.pointer_to_raw_data + iat_size);
1556 // Override the IAT atoms
1557 // TODO: we should rewrite only dirtied atoms, but that's for way later
1558 try self.base.file.?.pwriteAll(mem.sliceAsBytes(lookup_table.items), section.header.pointer_to_raw_data);
1559
1560 self.data_directories[@enumToInt(coff.DirectoryEntry.IMPORT)] = .{
1561 .virtual_address = iat_rva + iat_size,
1562 .size = @intCast(u32, @sizeOf(coff.ImportDirectoryEntry) * 2),
14971563 };
1564
14981565 self.data_directories[@enumToInt(coff.DirectoryEntry.IAT)] = .{
14991566 .virtual_address = iat_rva,
15001567 .size = iat_size,