1 //= AArch64WinCOFFObjectWriter.cpp - AArch64 Windows COFF Object Writer C++ =// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===---------------------------------------------------------------------===// 8 9 #include "AArch64MCTargetDesc.h" 10 #include "MCTargetDesc/AArch64FixupKinds.h" 11 #include "MCTargetDesc/AArch64MCExpr.h" 12 #include "llvm/ADT/Twine.h" 13 #include "llvm/BinaryFormat/COFF.h" 14 #include "llvm/MC/MCAsmBackend.h" 15 #include "llvm/MC/MCContext.h" 16 #include "llvm/MC/MCExpr.h" 17 #include "llvm/MC/MCFixup.h" 18 #include "llvm/MC/MCFixupKindInfo.h" 19 #include "llvm/MC/MCObjectWriter.h" 20 #include "llvm/MC/MCValue.h" 21 #include "llvm/MC/MCWinCOFFObjectWriter.h" 22 #include "llvm/Support/Casting.h" 23 #include "llvm/Support/ErrorHandling.h" 24 #include "llvm/Support/raw_ostream.h" 25 #include <cassert> 26 27 using namespace llvm; 28 29 namespace { 30 31 class AArch64WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter { 32 public: 33 AArch64WinCOFFObjectWriter() 34 : MCWinCOFFObjectTargetWriter(COFF::IMAGE_FILE_MACHINE_ARM64) {} 35 36 ~AArch64WinCOFFObjectWriter() override = default; 37 38 unsigned getRelocType(MCContext &Ctx, const MCValue &Target, 39 const MCFixup &Fixup, bool IsCrossSection, 40 const MCAsmBackend &MAB) const override; 41 42 bool recordRelocation(const MCFixup &) const override; 43 }; 44 45 } // end anonymous namespace 46 47 unsigned AArch64WinCOFFObjectWriter::getRelocType( 48 MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, 49 bool IsCrossSection, const MCAsmBackend &MAB) const { 50 unsigned FixupKind = Fixup.getKind(); 51 if (IsCrossSection) { 52 // IMAGE_REL_ARM64_REL64 does not exist. We treat FK_Data_8 as FK_PCRel_4 so 53 // that .xword a-b can lower to IMAGE_REL_ARM64_REL32. This allows generic 54 // instrumentation to not bother with the COFF limitation. A negative value 55 // needs attention. 56 if (FixupKind != FK_Data_4 && FixupKind != FK_Data_8) { 57 Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression"); 58 return COFF::IMAGE_REL_ARM64_ADDR32; 59 } 60 FixupKind = FK_PCRel_4; 61 } 62 63 auto Modifier = Target.isAbsolute() ? MCSymbolRefExpr::VK_None 64 : Target.getSymA()->getKind(); 65 const MCExpr *Expr = Fixup.getValue(); 66 67 if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(Expr)) { 68 AArch64MCExpr::VariantKind RefKind = A64E->getKind(); 69 switch (AArch64MCExpr::getSymbolLoc(RefKind)) { 70 case AArch64MCExpr::VK_ABS: 71 case AArch64MCExpr::VK_SECREL: 72 // Supported 73 break; 74 default: 75 Ctx.reportError(Fixup.getLoc(), "relocation variant " + 76 A64E->getVariantKindName() + 77 " unsupported on COFF targets"); 78 return COFF::IMAGE_REL_ARM64_ABSOLUTE; // Dummy return value 79 } 80 } 81 82 switch (FixupKind) { 83 default: { 84 if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(Expr)) { 85 Ctx.reportError(Fixup.getLoc(), "relocation type " + 86 A64E->getVariantKindName() + 87 " unsupported on COFF targets"); 88 } else { 89 const MCFixupKindInfo &Info = MAB.getFixupKindInfo(Fixup.getKind()); 90 Ctx.reportError(Fixup.getLoc(), Twine("relocation type ") + Info.Name + 91 " unsupported on COFF targets"); 92 } 93 return COFF::IMAGE_REL_ARM64_ABSOLUTE; // Dummy return value 94 } 95 96 case FK_PCRel_4: 97 return COFF::IMAGE_REL_ARM64_REL32; 98 99 case FK_Data_4: 100 switch (Modifier) { 101 default: 102 return COFF::IMAGE_REL_ARM64_ADDR32; 103 case MCSymbolRefExpr::VK_COFF_IMGREL32: 104 return COFF::IMAGE_REL_ARM64_ADDR32NB; 105 case MCSymbolRefExpr::VK_SECREL: 106 return COFF::IMAGE_REL_ARM64_SECREL; 107 } 108 109 case FK_Data_8: 110 return COFF::IMAGE_REL_ARM64_ADDR64; 111 112 case FK_SecRel_2: 113 return COFF::IMAGE_REL_ARM64_SECTION; 114 115 case FK_SecRel_4: 116 return COFF::IMAGE_REL_ARM64_SECREL; 117 118 case AArch64::fixup_aarch64_add_imm12: 119 if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(Expr)) { 120 AArch64MCExpr::VariantKind RefKind = A64E->getKind(); 121 if (RefKind == AArch64MCExpr::VK_SECREL_LO12) 122 return COFF::IMAGE_REL_ARM64_SECREL_LOW12A; 123 if (RefKind == AArch64MCExpr::VK_SECREL_HI12) 124 return COFF::IMAGE_REL_ARM64_SECREL_HIGH12A; 125 } 126 return COFF::IMAGE_REL_ARM64_PAGEOFFSET_12A; 127 128 case AArch64::fixup_aarch64_ldst_imm12_scale1: 129 case AArch64::fixup_aarch64_ldst_imm12_scale2: 130 case AArch64::fixup_aarch64_ldst_imm12_scale4: 131 case AArch64::fixup_aarch64_ldst_imm12_scale8: 132 case AArch64::fixup_aarch64_ldst_imm12_scale16: 133 if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(Expr)) { 134 AArch64MCExpr::VariantKind RefKind = A64E->getKind(); 135 if (RefKind == AArch64MCExpr::VK_SECREL_LO12) 136 return COFF::IMAGE_REL_ARM64_SECREL_LOW12L; 137 } 138 return COFF::IMAGE_REL_ARM64_PAGEOFFSET_12L; 139 140 case AArch64::fixup_aarch64_pcrel_adr_imm21: 141 return COFF::IMAGE_REL_ARM64_REL21; 142 143 case AArch64::fixup_aarch64_pcrel_adrp_imm21: 144 return COFF::IMAGE_REL_ARM64_PAGEBASE_REL21; 145 146 case AArch64::fixup_aarch64_pcrel_branch14: 147 return COFF::IMAGE_REL_ARM64_BRANCH14; 148 149 case AArch64::fixup_aarch64_pcrel_branch19: 150 return COFF::IMAGE_REL_ARM64_BRANCH19; 151 152 case AArch64::fixup_aarch64_pcrel_branch26: 153 case AArch64::fixup_aarch64_pcrel_call26: 154 return COFF::IMAGE_REL_ARM64_BRANCH26; 155 } 156 } 157 158 bool AArch64WinCOFFObjectWriter::recordRelocation(const MCFixup &Fixup) const { 159 return true; 160 } 161 162 std::unique_ptr<MCObjectTargetWriter> llvm::createAArch64WinCOFFObjectWriter() { 163 return std::make_unique<AArch64WinCOFFObjectWriter>(); 164 } 165