diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2022-11-05 12:19:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-05 12:19:45 +0100 |
| commit | 28288dcbbf5258fe7461bbe0f6b2feeba0c2de5d (patch) | |
| tree | b2517ebf4369ffb2c078b4ceced49a3f83e33c4a /src/arch | |
| parent | 1d680459197e7ba5e163d99e07fdfe54c2ad9225 (diff) | |
| parent | 165ae04a03206af34707b5505dc042338ce4731e (diff) | |
| download | zig-28288dcbbf5258fe7461bbe0f6b2feeba0c2de5d.tar.gz zig-28288dcbbf5258fe7461bbe0f6b2feeba0c2de5d.zip | |
Merge pull request #13444 from ziglang/arm64-coff
aarch64,coff: lift-off!
Diffstat (limited to 'src/arch')
| -rw-r--r-- | src/arch/aarch64/CodeGen.zig | 99 | ||||
| -rw-r--r-- | src/arch/aarch64/Emit.zig | 67 | ||||
| -rw-r--r-- | src/arch/aarch64/Mir.zig | 4 |
3 files changed, 149 insertions, 21 deletions
diff --git a/src/arch/aarch64/CodeGen.zig b/src/arch/aarch64/CodeGen.zig index d9f94fdc71..613bdbd3fd 100644 --- a/src/arch/aarch64/CodeGen.zig +++ b/src/arch/aarch64/CodeGen.zig @@ -142,7 +142,8 @@ const MCValue = union(enum) { /// The value is in memory but requires a linker relocation fixup: /// * got - the value is referenced indirectly via GOT entry index (the linker emits a got-type reloc) /// * direct - the value is referenced directly via symbol index index (the linker emits a displacement reloc) - linker_load: struct { @"type": enum { got, direct }, sym_index: u32 }, + /// * import - the value is referenced indirectly via import entry index (the linker emits an import-type reloc) + linker_load: struct { @"type": enum { got, direct, import }, sym_index: u32 }, /// The value is one of the stack variables. /// /// If the type is a pointer, it means the pointer address is in @@ -1117,13 +1118,11 @@ fn truncRegister( }); }, 32, 64 => { - assert(dest_reg.size() == operand_reg.size()); - _ = try self.addInst(.{ .tag = .mov_register, .data = .{ .rr = .{ - .rd = dest_reg, - .rn = operand_reg, + .rd = if (int_bits == 32) dest_reg.toW() else dest_reg.toX(), + .rn = if (int_bits == 32) operand_reg.toW() else operand_reg.toX(), } }, }); }, @@ -3719,14 +3718,21 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type const tag: Mir.Inst.Tag = switch (load_struct.@"type") { .got => .load_memory_ptr_got, .direct => .load_memory_ptr_direct, + .import => unreachable, }; const mod = self.bin_file.options.module.?; + const owner_decl = mod.declPtr(self.mod_fn.owner_decl); + const atom_index = switch (self.bin_file.tag) { + .macho => owner_decl.link.macho.sym_index, + .coff => owner_decl.link.coff.sym_index, + else => unreachable, // unsupported target format + }; _ = try self.addInst(.{ .tag = tag, .data = .{ .payload = try self.addExtra(Mir.LoadMemoryPie{ .register = @enumToInt(src_reg), - .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index, + .atom_index = atom_index, .sym_index = load_struct.sym_index, }), }, @@ -4057,6 +4063,45 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. } else { return self.fail("TODO implement calling bitcasted functions", .{}); } + } else if (self.bin_file.cast(link.File.Coff)) |coff_file| { + if (func_value.castTag(.function)) |func_payload| { + const func = func_payload.data; + const fn_owner_decl = mod.declPtr(func.owner_decl); + try self.genSetReg(Type.initTag(.u64), .x30, .{ + .linker_load = .{ + .@"type" = .got, + .sym_index = fn_owner_decl.link.coff.sym_index, + }, + }); + // blr x30 + _ = try self.addInst(.{ + .tag = .blr, + .data = .{ .reg = .x30 }, + }); + } else if (func_value.castTag(.extern_fn)) |func_payload| { + const extern_fn = func_payload.data; + const decl_name = mod.declPtr(extern_fn.owner_decl).name; + if (extern_fn.lib_name) |lib_name| { + log.debug("TODO enforce that '{s}' is expected in '{s}' library", .{ + decl_name, + lib_name, + }); + } + const sym_index = try coff_file.getGlobalSymbol(mem.sliceTo(decl_name, 0)); + try self.genSetReg(Type.initTag(.u64), .x30, .{ + .linker_load = .{ + .@"type" = .import, + .sym_index = sym_index, + }, + }); + // blr x30 + _ = try self.addInst(.{ + .tag = .blr, + .data = .{ .reg = .x30 }, + }); + } else { + return self.fail("TODO implement calling bitcasted functions", .{}); + } } else if (self.bin_file.cast(link.File.Plan9)) |p9| { if (func_value.castTag(.function)) |func_payload| { try p9.seeDecl(func_payload.data.owner_decl); @@ -4077,8 +4122,6 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. } else { return self.fail("TODO implement calling bitcasted functions", .{}); } - } else if (self.bin_file.cast(link.File.Coff)) |_| { - return self.fail("TODO implement calling in COFF for {}", .{self.target.cpu.arch}); } else unreachable; } else { assert(ty.zigTypeTag() == .Pointer); @@ -5161,14 +5204,21 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro const tag: Mir.Inst.Tag = switch (load_struct.@"type") { .got => .load_memory_ptr_got, .direct => .load_memory_ptr_direct, + .import => unreachable, }; const mod = self.bin_file.options.module.?; + const owner_decl = mod.declPtr(self.mod_fn.owner_decl); + const atom_index = switch (self.bin_file.tag) { + .macho => owner_decl.link.macho.sym_index, + .coff => owner_decl.link.coff.sym_index, + else => unreachable, // unsupported target format + }; _ = try self.addInst(.{ .tag = tag, .data = .{ .payload = try self.addExtra(Mir.LoadMemoryPie{ .register = @enumToInt(src_reg), - .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index, + .atom_index = atom_index, .sym_index = load_struct.sym_index, }), }, @@ -5268,14 +5318,21 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void const tag: Mir.Inst.Tag = switch (load_struct.@"type") { .got => .load_memory_got, .direct => .load_memory_direct, + .import => .load_memory_import, }; const mod = self.bin_file.options.module.?; + const owner_decl = mod.declPtr(self.mod_fn.owner_decl); + const atom_index = switch (self.bin_file.tag) { + .macho => owner_decl.link.macho.sym_index, + .coff => owner_decl.link.coff.sym_index, + else => unreachable, // unsupported target format + }; _ = try self.addInst(.{ .tag = tag, .data = .{ .payload = try self.addExtra(Mir.LoadMemoryPie{ .register = @enumToInt(reg), - .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index, + .atom_index = atom_index, .sym_index = load_struct.sym_index, }), }, @@ -5455,14 +5512,21 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I const tag: Mir.Inst.Tag = switch (load_struct.@"type") { .got => .load_memory_ptr_got, .direct => .load_memory_ptr_direct, + .import => unreachable, }; const mod = self.bin_file.options.module.?; + const owner_decl = mod.declPtr(self.mod_fn.owner_decl); + const atom_index = switch (self.bin_file.tag) { + .macho => owner_decl.link.macho.sym_index, + .coff => owner_decl.link.coff.sym_index, + else => unreachable, // unsupported target format + }; _ = try self.addInst(.{ .tag = tag, .data = .{ .payload = try self.addExtra(Mir.LoadMemoryPie{ .register = @enumToInt(src_reg), - .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.macho.sym_index, + .atom_index = atom_index, .sym_index = load_struct.sym_index, }), }, @@ -5775,7 +5839,13 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne .sym_index = decl.link.macho.sym_index, } }; } else if (self.bin_file.cast(link.File.Coff)) |_| { - return self.fail("TODO codegen COFF const Decl pointer", .{}); + // Because COFF is PIE-always-on, we defer memory address resolution until + // the linker has enough info to perform relocations. + assert(decl.link.coff.sym_index != 0); + return MCValue{ .linker_load = .{ + .@"type" = .got, + .sym_index = decl.link.coff.sym_index, + } }; } else if (self.bin_file.cast(link.File.Plan9)) |p9| { try p9.seeDecl(decl_index); const got_addr = p9.bases.data + decl.link.plan9.got_index.? * ptr_bytes; @@ -5799,7 +5869,10 @@ fn lowerUnnamedConst(self: *Self, tv: TypedValue) InnerError!MCValue { .sym_index = local_sym_index, } }; } else if (self.bin_file.cast(link.File.Coff)) |_| { - return self.fail("TODO lower unnamed const in COFF", .{}); + return MCValue{ .linker_load = .{ + .@"type" = .direct, + .sym_index = local_sym_index, + } }; } else if (self.bin_file.cast(link.File.Plan9)) |_| { return self.fail("TODO lower unnamed const in Plan9", .{}); } else { diff --git a/src/arch/aarch64/Emit.zig b/src/arch/aarch64/Emit.zig index 8794890e9e..9608e1e9fc 100644 --- a/src/arch/aarch64/Emit.zig +++ b/src/arch/aarch64/Emit.zig @@ -145,6 +145,7 @@ pub fn emitMir( .load_memory_got => try emit.mirLoadMemoryPie(inst), .load_memory_direct => try emit.mirLoadMemoryPie(inst), + .load_memory_import => try emit.mirLoadMemoryPie(inst), .load_memory_ptr_got => try emit.mirLoadMemoryPie(inst), .load_memory_ptr_direct => try emit.mirLoadMemoryPie(inst), @@ -674,13 +675,14 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void { assert(emit.mir.instructions.items(.tag)[inst] == .call_extern); const relocation = emit.mir.instructions.items(.data)[inst].relocation; + const offset = blk: { + const offset = @intCast(u32, emit.code.items.len); + // bl + try emit.writeInstruction(Instruction.bl(0)); + break :blk offset; + }; + if (emit.bin_file.cast(link.File.MachO)) |macho_file| { - const offset = blk: { - const offset = @intCast(u32, emit.code.items.len); - // bl - try emit.writeInstruction(Instruction.bl(0)); - break :blk offset; - }; // Add relocation to the decl. const atom = macho_file.getAtomForSymbol(.{ .sym_index = relocation.atom_index, .file = null }).?; const target = macho_file.getGlobalByIndex(relocation.sym_index); @@ -692,8 +694,10 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void { .pcrel = true, .length = 2, }); + } else if (emit.bin_file.cast(link.File.Coff)) |_| { + unreachable; // Calling imports is handled via `.load_memory_import` } else { - return emit.fail("Implement call_extern for linking backends != MachO", .{}); + return emit.fail("Implement call_extern for linking backends != {{ COFF, MachO }}", .{}); } } @@ -855,7 +859,9 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void { try emit.writeInstruction(Instruction.adrp(reg.toX(), 0)); switch (tag) { - .load_memory_got => { + .load_memory_got, + .load_memory_import, + => { // ldr reg, reg, offset try emit.writeInstruction(Instruction.ldr( reg, @@ -926,6 +932,51 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void { else => unreachable, }, }); + } else if (emit.bin_file.cast(link.File.Coff)) |coff_file| { + const atom = coff_file.getAtomForSymbol(.{ .sym_index = data.atom_index, .file = null }).?; + const target = switch (tag) { + .load_memory_got, + .load_memory_ptr_got, + .load_memory_direct, + .load_memory_ptr_direct, + => link.File.Coff.SymbolWithLoc{ .sym_index = data.sym_index, .file = null }, + .load_memory_import => coff_file.getGlobalByIndex(data.sym_index), + else => unreachable, + }; + try atom.addRelocation(coff_file, .{ + .target = target, + .offset = offset, + .addend = 0, + .pcrel = true, + .length = 2, + .@"type" = switch (tag) { + .load_memory_got, + .load_memory_ptr_got, + => .got_page, + .load_memory_direct, + .load_memory_ptr_direct, + => .page, + .load_memory_import => .import_page, + else => unreachable, + }, + }); + try atom.addRelocation(coff_file, .{ + .target = target, + .offset = offset + 4, + .addend = 0, + .pcrel = false, + .length = 2, + .@"type" = switch (tag) { + .load_memory_got, + .load_memory_ptr_got, + => .got_pageoff, + .load_memory_direct, + .load_memory_ptr_direct, + => .pageoff, + .load_memory_import => .import_pageoff, + else => unreachable, + }, + }); } else { return emit.fail("TODO implement load_memory for PIE GOT indirection on this platform", .{}); } diff --git a/src/arch/aarch64/Mir.zig b/src/arch/aarch64/Mir.zig index 1855117a7f..e5541a5988 100644 --- a/src/arch/aarch64/Mir.zig +++ b/src/arch/aarch64/Mir.zig @@ -84,6 +84,10 @@ pub const Inst = struct { /// /// Payload is `LoadMemoryPie` load_memory_direct, + /// Loads the contents into a register + /// + /// Payload is `LoadMemoryPie` + load_memory_import, /// Loads the address into a register /// /// Payload is `LoadMemoryPie` |
