1 //===-- SystemZMCAsmBackend.cpp - SystemZ assembler backend ---------------===// 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/SystemZMCFixups.h" 10 #include "MCTargetDesc/SystemZMCTargetDesc.h" 11 #include "llvm/ADT/StringSwitch.h" 12 #include "llvm/MC/MCAsmBackend.h" 13 #include "llvm/MC/MCAssembler.h" 14 #include "llvm/MC/MCContext.h" 15 #include "llvm/MC/MCELFObjectWriter.h" 16 #include "llvm/MC/MCFixupKindInfo.h" 17 #include "llvm/MC/MCInst.h" 18 #include "llvm/MC/MCObjectWriter.h" 19 #include "llvm/MC/MCSubtargetInfo.h" 20 21 using namespace llvm; 22 23 // Value is a fully-resolved relocation value: Symbol + Addend [- Pivot]. 24 // Return the bits that should be installed in a relocation field for 25 // fixup kind Kind. 26 static uint64_t extractBitsForFixup(MCFixupKind Kind, uint64_t Value, 27 const MCFixup &Fixup, MCContext &Ctx) { 28 if (Kind < FirstTargetFixupKind) 29 return Value; 30 31 switch (unsigned(Kind)) { 32 case SystemZ::FK_390_PC12DBL: 33 case SystemZ::FK_390_PC16DBL: 34 case SystemZ::FK_390_PC24DBL: 35 case SystemZ::FK_390_PC32DBL: 36 return (int64_t)Value / 2; 37 38 case SystemZ::FK_390_12: 39 if (!isUInt<12>(Value)) { 40 Ctx.reportError(Fixup.getLoc(), "displacement exceeds uint12"); 41 return 0; 42 } 43 return Value; 44 45 case SystemZ::FK_390_20: { 46 if (!isInt<20>(Value)) { 47 Ctx.reportError(Fixup.getLoc(), "displacement exceeds int20"); 48 return 0; 49 } 50 // The high byte of a 20 bit displacement value comes first. 51 uint64_t DLo = Value & 0xfff; 52 uint64_t DHi = (Value >> 12) & 0xff; 53 return (DLo << 8) | DHi; 54 } 55 56 case SystemZ::FK_390_TLS_CALL: 57 return 0; 58 } 59 60 llvm_unreachable("Unknown fixup kind!"); 61 } 62 63 namespace { 64 class SystemZMCAsmBackend : public MCAsmBackend { 65 uint8_t OSABI; 66 public: 67 SystemZMCAsmBackend(uint8_t osABI) 68 : MCAsmBackend(support::big), OSABI(osABI) {} 69 70 // Override MCAsmBackend 71 unsigned getNumFixupKinds() const override { 72 return SystemZ::NumTargetFixupKinds; 73 } 74 Optional<MCFixupKind> getFixupKind(StringRef Name) const override; 75 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override; 76 bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup, 77 const MCValue &Target) override; 78 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, 79 const MCValue &Target, MutableArrayRef<char> Data, 80 uint64_t Value, bool IsResolved, 81 const MCSubtargetInfo *STI) const override; 82 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, 83 const MCRelaxableFragment *Fragment, 84 const MCAsmLayout &Layout) const override { 85 return false; 86 } 87 bool writeNopData(raw_ostream &OS, uint64_t Count, 88 const MCSubtargetInfo *STI) const override; 89 std::unique_ptr<MCObjectTargetWriter> 90 createObjectTargetWriter() const override { 91 return createSystemZObjectWriter(OSABI); 92 } 93 }; 94 } // end anonymous namespace 95 96 Optional<MCFixupKind> SystemZMCAsmBackend::getFixupKind(StringRef Name) const { 97 unsigned Type = llvm::StringSwitch<unsigned>(Name) 98 #define ELF_RELOC(X, Y) .Case(#X, Y) 99 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def" 100 #undef ELF_RELOC 101 .Case("BFD_RELOC_NONE", ELF::R_390_NONE) 102 .Case("BFD_RELOC_8", ELF::R_390_8) 103 .Case("BFD_RELOC_16", ELF::R_390_16) 104 .Case("BFD_RELOC_32", ELF::R_390_32) 105 .Case("BFD_RELOC_64", ELF::R_390_64) 106 .Default(-1u); 107 if (Type != -1u) 108 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type); 109 return None; 110 } 111 112 const MCFixupKindInfo & 113 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { 114 const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = { 115 { "FK_390_PC12DBL", 4, 12, MCFixupKindInfo::FKF_IsPCRel }, 116 { "FK_390_PC16DBL", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, 117 { "FK_390_PC24DBL", 0, 24, MCFixupKindInfo::FKF_IsPCRel }, 118 { "FK_390_PC32DBL", 0, 32, MCFixupKindInfo::FKF_IsPCRel }, 119 { "FK_390_TLS_CALL", 0, 0, 0 }, 120 { "FK_390_12", 4, 12, 0 }, 121 { "FK_390_20", 4, 20, 0 } 122 }; 123 124 // Fixup kinds from .reloc directive are like R_390_NONE. They 125 // do not require any extra processing. 126 if (Kind >= FirstLiteralRelocationKind) 127 return MCAsmBackend::getFixupKindInfo(FK_NONE); 128 129 if (Kind < FirstTargetFixupKind) 130 return MCAsmBackend::getFixupKindInfo(Kind); 131 132 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && 133 "Invalid kind!"); 134 return Infos[Kind - FirstTargetFixupKind]; 135 } 136 137 bool SystemZMCAsmBackend::shouldForceRelocation(const MCAssembler &, 138 const MCFixup &Fixup, 139 const MCValue &) { 140 return Fixup.getKind() >= FirstLiteralRelocationKind; 141 } 142 143 void SystemZMCAsmBackend::applyFixup(const MCAssembler &Asm, 144 const MCFixup &Fixup, 145 const MCValue &Target, 146 MutableArrayRef<char> Data, uint64_t Value, 147 bool IsResolved, 148 const MCSubtargetInfo *STI) const { 149 MCFixupKind Kind = Fixup.getKind(); 150 if (Kind >= FirstLiteralRelocationKind) 151 return; 152 unsigned Offset = Fixup.getOffset(); 153 unsigned BitSize = getFixupKindInfo(Kind).TargetSize; 154 unsigned Size = (BitSize + 7) / 8; 155 156 assert(Offset + Size <= Data.size() && "Invalid fixup offset!"); 157 158 // Big-endian insertion of Size bytes. 159 Value = extractBitsForFixup(Kind, Value, Fixup, Asm.getContext()); 160 if (BitSize < 64) 161 Value &= ((uint64_t)1 << BitSize) - 1; 162 unsigned ShiftValue = (Size * 8) - 8; 163 for (unsigned I = 0; I != Size; ++I) { 164 Data[Offset + I] |= uint8_t(Value >> ShiftValue); 165 ShiftValue -= 8; 166 } 167 } 168 169 bool SystemZMCAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count, 170 const MCSubtargetInfo *STI) const { 171 for (uint64_t I = 0; I != Count; ++I) 172 OS << '\x7'; 173 return true; 174 } 175 176 MCAsmBackend *llvm::createSystemZMCAsmBackend(const Target &T, 177 const MCSubtargetInfo &STI, 178 const MCRegisterInfo &MRI, 179 const MCTargetOptions &Options) { 180 uint8_t OSABI = 181 MCELFObjectTargetWriter::getOSABI(STI.getTargetTriple().getOS()); 182 return new SystemZMCAsmBackend(OSABI); 183 } 184