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   std::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 std::optional<MCFixupKind>
115 SystemZMCAsmBackend::getFixupKind(StringRef Name) const {
116   unsigned Type = llvm::StringSwitch<unsigned>(Name)
117 #define ELF_RELOC(X, Y) .Case(#X, Y)
118 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
119 #undef ELF_RELOC
120 			.Case("BFD_RELOC_NONE", ELF::R_390_NONE)
121 			.Case("BFD_RELOC_8", ELF::R_390_8)
122 			.Case("BFD_RELOC_16", ELF::R_390_16)
123 			.Case("BFD_RELOC_32", ELF::R_390_32)
124 			.Case("BFD_RELOC_64", ELF::R_390_64)
125 			.Default(-1u);
126   if (Type != -1u)
127     return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
128   return std::nullopt;
129 }
130 
131 const MCFixupKindInfo &
132 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
133   const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = {
134     { "FK_390_PC12DBL",  4, 12, MCFixupKindInfo::FKF_IsPCRel },
135     { "FK_390_PC16DBL",  0, 16, MCFixupKindInfo::FKF_IsPCRel },
136     { "FK_390_PC24DBL",  0, 24, MCFixupKindInfo::FKF_IsPCRel },
137     { "FK_390_PC32DBL",  0, 32, MCFixupKindInfo::FKF_IsPCRel },
138     { "FK_390_TLS_CALL", 0, 0, 0 },
139     { "FK_390_12",       4, 12, 0 },
140     { "FK_390_20",       4, 20, 0 }
141   };
142 
143   // Fixup kinds from .reloc directive are like R_390_NONE. They
144   // do not require any extra processing.
145   if (Kind >= FirstLiteralRelocationKind)
146     return MCAsmBackend::getFixupKindInfo(FK_NONE);
147 
148   if (Kind < FirstTargetFixupKind)
149     return MCAsmBackend::getFixupKindInfo(Kind);
150 
151   assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
152          "Invalid kind!");
153   return Infos[Kind - FirstTargetFixupKind];
154 }
155 
156 bool SystemZMCAsmBackend::shouldForceRelocation(const MCAssembler &,
157 						const MCFixup &Fixup,
158 						const MCValue &) {
159   return Fixup.getKind() >= FirstLiteralRelocationKind;
160 }
161 
162 void SystemZMCAsmBackend::applyFixup(const MCAssembler &Asm,
163                                      const MCFixup &Fixup,
164                                      const MCValue &Target,
165                                      MutableArrayRef<char> Data, uint64_t Value,
166                                      bool IsResolved,
167                                      const MCSubtargetInfo *STI) const {
168   MCFixupKind Kind = Fixup.getKind();
169   if (Kind >= FirstLiteralRelocationKind)
170     return;
171   unsigned Offset = Fixup.getOffset();
172   unsigned BitSize = getFixupKindInfo(Kind).TargetSize;
173   unsigned Size = (BitSize + 7) / 8;
174 
175   assert(Offset + Size <= Data.size() && "Invalid fixup offset!");
176 
177   // Big-endian insertion of Size bytes.
178   Value = extractBitsForFixup(Kind, Value, Fixup, Asm.getContext());
179   if (BitSize < 64)
180     Value &= ((uint64_t)1 << BitSize) - 1;
181   unsigned ShiftValue = (Size * 8) - 8;
182   for (unsigned I = 0; I != Size; ++I) {
183     Data[Offset + I] |= uint8_t(Value >> ShiftValue);
184     ShiftValue -= 8;
185   }
186 }
187 
188 bool SystemZMCAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
189                                        const MCSubtargetInfo *STI) const {
190   for (uint64_t I = 0; I != Count; ++I)
191     OS << '\x7';
192   return true;
193 }
194 
195 MCAsmBackend *llvm::createSystemZMCAsmBackend(const Target &T,
196                                               const MCSubtargetInfo &STI,
197                                               const MCRegisterInfo &MRI,
198                                               const MCTargetOptions &Options) {
199   uint8_t OSABI =
200       MCELFObjectTargetWriter::getOSABI(STI.getTargetTriple().getOS());
201   return new SystemZMCAsmBackend(OSABI);
202 }
203