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 "MCTargetDesc/AArch64FixupKinds.h" 10 #include "MCTargetDesc/AArch64MCExpr.h" 11 #include "llvm/ADT/Twine.h" 12 #include "llvm/BinaryFormat/COFF.h" 13 #include "llvm/MC/MCAsmBackend.h" 14 #include "llvm/MC/MCContext.h" 15 #include "llvm/MC/MCExpr.h" 16 #include "llvm/MC/MCFixup.h" 17 #include "llvm/MC/MCFixupKindInfo.h" 18 #include "llvm/MC/MCObjectWriter.h" 19 #include "llvm/MC/MCValue.h" 20 #include "llvm/MC/MCWinCOFFObjectWriter.h" 21 #include "llvm/Support/ErrorHandling.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include <cassert> 24 25 using namespace llvm; 26 27 namespace { 28 29 class AArch64WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter { 30 public: 31 AArch64WinCOFFObjectWriter() 32 : MCWinCOFFObjectTargetWriter(COFF::IMAGE_FILE_MACHINE_ARM64) {} 33 34 ~AArch64WinCOFFObjectWriter() override = default; 35 36 unsigned getRelocType(MCContext &Ctx, const MCValue &Target, 37 const MCFixup &Fixup, bool IsCrossSection, 38 const MCAsmBackend &MAB) const override; 39 40 bool recordRelocation(const MCFixup &) const override; 41 }; 42 43 } // end anonymous namespace 44 45 unsigned AArch64WinCOFFObjectWriter::getRelocType( 46 MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, 47 bool IsCrossSection, const MCAsmBackend &MAB) const { 48 auto Modifier = Target.isAbsolute() ? MCSymbolRefExpr::VK_None 49 : Target.getSymA()->getKind(); 50 const MCExpr *Expr = Fixup.getValue(); 51 52 if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(Expr)) { 53 AArch64MCExpr::VariantKind RefKind = A64E->getKind(); 54 switch (AArch64MCExpr::getSymbolLoc(RefKind)) { 55 case AArch64MCExpr::VK_ABS: 56 case AArch64MCExpr::VK_SECREL: 57 // Supported 58 break; 59 default: 60 Ctx.reportError(Fixup.getLoc(), "relocation variant " + 61 A64E->getVariantKindName() + 62 " unsupported on COFF targets"); 63 return COFF::IMAGE_REL_ARM64_ABSOLUTE; // Dummy return value 64 } 65 } 66 67 switch (static_cast<unsigned>(Fixup.getKind())) { 68 default: { 69 if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(Expr)) { 70 Ctx.reportError(Fixup.getLoc(), "relocation type " + 71 A64E->getVariantKindName() + 72 " unsupported on COFF targets"); 73 } else { 74 const MCFixupKindInfo &Info = MAB.getFixupKindInfo(Fixup.getKind()); 75 Ctx.reportError(Fixup.getLoc(), Twine("relocation type ") + Info.Name + 76 " unsupported on COFF targets"); 77 } 78 return COFF::IMAGE_REL_ARM64_ABSOLUTE; // Dummy return value 79 } 80 81 case FK_Data_4: 82 switch (Modifier) { 83 default: 84 return COFF::IMAGE_REL_ARM64_ADDR32; 85 case MCSymbolRefExpr::VK_COFF_IMGREL32: 86 return COFF::IMAGE_REL_ARM64_ADDR32NB; 87 case MCSymbolRefExpr::VK_SECREL: 88 return COFF::IMAGE_REL_ARM64_SECREL; 89 } 90 91 case FK_Data_8: 92 return COFF::IMAGE_REL_ARM64_ADDR64; 93 94 case FK_SecRel_2: 95 return COFF::IMAGE_REL_ARM64_SECTION; 96 97 case FK_SecRel_4: 98 return COFF::IMAGE_REL_ARM64_SECREL; 99 100 case AArch64::fixup_aarch64_add_imm12: 101 if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(Expr)) { 102 AArch64MCExpr::VariantKind RefKind = A64E->getKind(); 103 if (RefKind == AArch64MCExpr::VK_SECREL_LO12) 104 return COFF::IMAGE_REL_ARM64_SECREL_LOW12A; 105 if (RefKind == AArch64MCExpr::VK_SECREL_HI12) 106 return COFF::IMAGE_REL_ARM64_SECREL_HIGH12A; 107 } 108 return COFF::IMAGE_REL_ARM64_PAGEOFFSET_12A; 109 110 case AArch64::fixup_aarch64_ldst_imm12_scale1: 111 case AArch64::fixup_aarch64_ldst_imm12_scale2: 112 case AArch64::fixup_aarch64_ldst_imm12_scale4: 113 case AArch64::fixup_aarch64_ldst_imm12_scale8: 114 case AArch64::fixup_aarch64_ldst_imm12_scale16: 115 if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(Expr)) { 116 AArch64MCExpr::VariantKind RefKind = A64E->getKind(); 117 if (RefKind == AArch64MCExpr::VK_SECREL_LO12) 118 return COFF::IMAGE_REL_ARM64_SECREL_LOW12L; 119 } 120 return COFF::IMAGE_REL_ARM64_PAGEOFFSET_12L; 121 122 case AArch64::fixup_aarch64_pcrel_adr_imm21: 123 return COFF::IMAGE_REL_ARM64_REL21; 124 125 case AArch64::fixup_aarch64_pcrel_adrp_imm21: 126 return COFF::IMAGE_REL_ARM64_PAGEBASE_REL21; 127 128 case AArch64::fixup_aarch64_pcrel_branch14: 129 return COFF::IMAGE_REL_ARM64_BRANCH14; 130 131 case AArch64::fixup_aarch64_pcrel_branch19: 132 return COFF::IMAGE_REL_ARM64_BRANCH19; 133 134 case AArch64::fixup_aarch64_pcrel_branch26: 135 case AArch64::fixup_aarch64_pcrel_call26: 136 return COFF::IMAGE_REL_ARM64_BRANCH26; 137 } 138 } 139 140 bool AArch64WinCOFFObjectWriter::recordRelocation(const MCFixup &Fixup) const { 141 return true; 142 } 143 144 namespace llvm { 145 146 std::unique_ptr<MCObjectTargetWriter> createAArch64WinCOFFObjectWriter() { 147 return std::make_unique<AArch64WinCOFFObjectWriter>(); 148 } 149 150 } // end namespace llvm 151