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   auto checkFixupInRange = [&](int64_t Min, int64_t Max) -> bool {
32     int64_t SVal = int64_t(Value);
33     if (SVal < Min || SVal > Max) {
34       Ctx.reportError(Fixup.getLoc(), "operand out of range (" + Twine(SVal) +
35                                           " not between " + Twine(Min) +
36                                           " and " + Twine(Max) + ")");
37       return false;
38     }
39     return true;
40   };
41 
42   auto handlePCRelFixupValue = [&](unsigned W) -> uint64_t {
43     if (Value % 2 != 0)
44       Ctx.reportError(Fixup.getLoc(), "Non-even PC relative offset.");
45     if (!checkFixupInRange(minIntN(W) * 2, maxIntN(W) * 2))
46       return 0;
47     return (int64_t)Value / 2;
48   };
49 
50   switch (unsigned(Kind)) {
51   case SystemZ::FK_390_PC12DBL:
52     return handlePCRelFixupValue(12);
53   case SystemZ::FK_390_PC16DBL:
54     return handlePCRelFixupValue(16);
55   case SystemZ::FK_390_PC24DBL:
56     return handlePCRelFixupValue(24);
57   case SystemZ::FK_390_PC32DBL:
58     return handlePCRelFixupValue(32);
59 
60   case SystemZ::FK_390_12:
61     if (!checkFixupInRange(0, maxUIntN(12)))
62       return 0;
63     return Value;
64 
65   case SystemZ::FK_390_20: {
66     if (!checkFixupInRange(minIntN(20), maxIntN(20)))
67       return 0;
68     // The high byte of a 20 bit displacement value comes first.
69     uint64_t DLo = Value & 0xfff;
70     uint64_t DHi = (Value >> 12) & 0xff;
71     return (DLo << 8) | DHi;
72   }
73 
74   case SystemZ::FK_390_TLS_CALL:
75     return 0;
76   }
77 
78   llvm_unreachable("Unknown fixup kind!");
79 }
80 
81 namespace {
82 class SystemZMCAsmBackend : public MCAsmBackend {
83   uint8_t OSABI;
84 public:
85   SystemZMCAsmBackend(uint8_t osABI)
86       : MCAsmBackend(support::big), OSABI(osABI) {}
87 
88   // Override MCAsmBackend
89   unsigned getNumFixupKinds() const override {
90     return SystemZ::NumTargetFixupKinds;
91   }
92   Optional<MCFixupKind> getFixupKind(StringRef Name) const override;
93   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
94   bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
95                              const MCValue &Target) override;
96   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
97                   const MCValue &Target, MutableArrayRef<char> Data,
98                   uint64_t Value, bool IsResolved,
99                   const MCSubtargetInfo *STI) const override;
100   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
101                             const MCRelaxableFragment *Fragment,
102                             const MCAsmLayout &Layout) const override {
103     return false;
104   }
105   bool writeNopData(raw_ostream &OS, uint64_t Count,
106                     const MCSubtargetInfo *STI) const override;
107   std::unique_ptr<MCObjectTargetWriter>
108   createObjectTargetWriter() const override {
109     return createSystemZObjectWriter(OSABI);
110   }
111 };
112 } // end anonymous namespace
113 
114 Optional<MCFixupKind> SystemZMCAsmBackend::getFixupKind(StringRef Name) const {
115   unsigned Type = llvm::StringSwitch<unsigned>(Name)
116 #define ELF_RELOC(X, Y) .Case(#X, Y)
117 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
118 #undef ELF_RELOC
119 			.Case("BFD_RELOC_NONE", ELF::R_390_NONE)
120 			.Case("BFD_RELOC_8", ELF::R_390_8)
121 			.Case("BFD_RELOC_16", ELF::R_390_16)
122 			.Case("BFD_RELOC_32", ELF::R_390_32)
123 			.Case("BFD_RELOC_64", ELF::R_390_64)
124 			.Default(-1u);
125   if (Type != -1u)
126     return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
127   return None;
128 }
129 
130 const MCFixupKindInfo &
131 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
132   const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = {
133     { "FK_390_PC12DBL",  4, 12, MCFixupKindInfo::FKF_IsPCRel },
134     { "FK_390_PC16DBL",  0, 16, MCFixupKindInfo::FKF_IsPCRel },
135     { "FK_390_PC24DBL",  0, 24, MCFixupKindInfo::FKF_IsPCRel },
136     { "FK_390_PC32DBL",  0, 32, MCFixupKindInfo::FKF_IsPCRel },
137     { "FK_390_TLS_CALL", 0, 0, 0 },
138     { "FK_390_12",       4, 12, 0 },
139     { "FK_390_20",       4, 20, 0 }
140   };
141 
142   // Fixup kinds from .reloc directive are like R_390_NONE. They
143   // do not require any extra processing.
144   if (Kind >= FirstLiteralRelocationKind)
145     return MCAsmBackend::getFixupKindInfo(FK_NONE);
146 
147   if (Kind < FirstTargetFixupKind)
148     return MCAsmBackend::getFixupKindInfo(Kind);
149 
150   assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
151          "Invalid kind!");
152   return Infos[Kind - FirstTargetFixupKind];
153 }
154 
155 bool SystemZMCAsmBackend::shouldForceRelocation(const MCAssembler &,
156 						const MCFixup &Fixup,
157 						const MCValue &) {
158   return Fixup.getKind() >= FirstLiteralRelocationKind;
159 }
160 
161 void SystemZMCAsmBackend::applyFixup(const MCAssembler &Asm,
162                                      const MCFixup &Fixup,
163                                      const MCValue &Target,
164                                      MutableArrayRef<char> Data, uint64_t Value,
165                                      bool IsResolved,
166                                      const MCSubtargetInfo *STI) const {
167   MCFixupKind Kind = Fixup.getKind();
168   if (Kind >= FirstLiteralRelocationKind)
169     return;
170   unsigned Offset = Fixup.getOffset();
171   unsigned BitSize = getFixupKindInfo(Kind).TargetSize;
172   unsigned Size = (BitSize + 7) / 8;
173 
174   assert(Offset + Size <= Data.size() && "Invalid fixup offset!");
175 
176   // Big-endian insertion of Size bytes.
177   Value = extractBitsForFixup(Kind, Value, Fixup, Asm.getContext());
178   if (BitSize < 64)
179     Value &= ((uint64_t)1 << BitSize) - 1;
180   unsigned ShiftValue = (Size * 8) - 8;
181   for (unsigned I = 0; I != Size; ++I) {
182     Data[Offset + I] |= uint8_t(Value >> ShiftValue);
183     ShiftValue -= 8;
184   }
185 }
186 
187 bool SystemZMCAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
188                                        const MCSubtargetInfo *STI) const {
189   for (uint64_t I = 0; I != Count; ++I)
190     OS << '\x7';
191   return true;
192 }
193 
194 MCAsmBackend *llvm::createSystemZMCAsmBackend(const Target &T,
195                                               const MCSubtargetInfo &STI,
196                                               const MCRegisterInfo &MRI,
197                                               const MCTargetOptions &Options) {
198   uint8_t OSABI =
199       MCELFObjectTargetWriter::getOSABI(STI.getTargetTriple().getOS());
200   return new SystemZMCAsmBackend(OSABI);
201 }
202