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