LLVM  8.0.1
SystemZMCObjectWriter.cpp
Go to the documentation of this file.
1 //===-- SystemZMCObjectWriter.cpp - SystemZ ELF writer --------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
12 #include "llvm/BinaryFormat/ELF.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCFixup.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCValue.h"
19 #include <cassert>
20 #include <cstdint>
21 
22 using namespace llvm;
23 
24 namespace {
25 
26 class SystemZObjectWriter : public MCELFObjectTargetWriter {
27 public:
28  SystemZObjectWriter(uint8_t OSABI);
29  ~SystemZObjectWriter() override = default;
30 
31 protected:
32  // Override MCELFObjectTargetWriter.
33  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
34  const MCFixup &Fixup, bool IsPCRel) const override;
35 };
36 
37 } // end anonymous namespace
38 
39 SystemZObjectWriter::SystemZObjectWriter(uint8_t OSABI)
40  : MCELFObjectTargetWriter(/*Is64Bit=*/true, OSABI, ELF::EM_S390,
41  /*HasRelocationAddend=*/ true) {}
42 
43 // Return the relocation type for an absolute value of MCFixupKind Kind.
44 static unsigned getAbsoluteReloc(unsigned Kind) {
45  switch (Kind) {
46  case FK_Data_1: return ELF::R_390_8;
47  case FK_Data_2: return ELF::R_390_16;
48  case FK_Data_4: return ELF::R_390_32;
49  case FK_Data_8: return ELF::R_390_64;
50  }
51  llvm_unreachable("Unsupported absolute address");
52 }
53 
54 // Return the relocation type for a PC-relative value of MCFixupKind Kind.
55 static unsigned getPCRelReloc(unsigned Kind) {
56  switch (Kind) {
57  case FK_Data_2: return ELF::R_390_PC16;
58  case FK_Data_4: return ELF::R_390_PC32;
59  case FK_Data_8: return ELF::R_390_PC64;
60  case SystemZ::FK_390_PC12DBL: return ELF::R_390_PC12DBL;
61  case SystemZ::FK_390_PC16DBL: return ELF::R_390_PC16DBL;
62  case SystemZ::FK_390_PC24DBL: return ELF::R_390_PC24DBL;
63  case SystemZ::FK_390_PC32DBL: return ELF::R_390_PC32DBL;
64  }
65  llvm_unreachable("Unsupported PC-relative address");
66 }
67 
68 // Return the R_390_TLS_LE* relocation type for MCFixupKind Kind.
69 static unsigned getTLSLEReloc(unsigned Kind) {
70  switch (Kind) {
71  case FK_Data_4: return ELF::R_390_TLS_LE32;
72  case FK_Data_8: return ELF::R_390_TLS_LE64;
73  }
74  llvm_unreachable("Unsupported absolute address");
75 }
76 
77 // Return the R_390_TLS_LDO* relocation type for MCFixupKind Kind.
78 static unsigned getTLSLDOReloc(unsigned Kind) {
79  switch (Kind) {
80  case FK_Data_4: return ELF::R_390_TLS_LDO32;
81  case FK_Data_8: return ELF::R_390_TLS_LDO64;
82  }
83  llvm_unreachable("Unsupported absolute address");
84 }
85 
86 // Return the R_390_TLS_LDM* relocation type for MCFixupKind Kind.
87 static unsigned getTLSLDMReloc(unsigned Kind) {
88  switch (Kind) {
89  case FK_Data_4: return ELF::R_390_TLS_LDM32;
90  case FK_Data_8: return ELF::R_390_TLS_LDM64;
91  case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_LDCALL;
92  }
93  llvm_unreachable("Unsupported absolute address");
94 }
95 
96 // Return the R_390_TLS_GD* relocation type for MCFixupKind Kind.
97 static unsigned getTLSGDReloc(unsigned Kind) {
98  switch (Kind) {
99  case FK_Data_4: return ELF::R_390_TLS_GD32;
100  case FK_Data_8: return ELF::R_390_TLS_GD64;
101  case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_GDCALL;
102  }
103  llvm_unreachable("Unsupported absolute address");
104 }
105 
106 // Return the PLT relocation counterpart of MCFixupKind Kind.
107 static unsigned getPLTReloc(unsigned Kind) {
108  switch (Kind) {
109  case SystemZ::FK_390_PC12DBL: return ELF::R_390_PLT12DBL;
110  case SystemZ::FK_390_PC16DBL: return ELF::R_390_PLT16DBL;
111  case SystemZ::FK_390_PC24DBL: return ELF::R_390_PLT24DBL;
112  case SystemZ::FK_390_PC32DBL: return ELF::R_390_PLT32DBL;
113  }
114  llvm_unreachable("Unsupported absolute address");
115 }
116 
118  const MCValue &Target,
119  const MCFixup &Fixup,
120  bool IsPCRel) const {
122  unsigned Kind = Fixup.getKind();
123  switch (Modifier) {
125  if (IsPCRel)
126  return getPCRelReloc(Kind);
127  return getAbsoluteReloc(Kind);
128 
130  assert(!IsPCRel && "NTPOFF shouldn't be PC-relative");
131  return getTLSLEReloc(Kind);
132 
134  if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
135  return ELF::R_390_TLS_IEENT;
136  llvm_unreachable("Only PC-relative INDNTPOFF accesses are supported for now");
137 
139  assert(!IsPCRel && "DTPOFF shouldn't be PC-relative");
140  return getTLSLDOReloc(Kind);
141 
143  assert(!IsPCRel && "TLSLDM shouldn't be PC-relative");
144  return getTLSLDMReloc(Kind);
145 
147  assert(!IsPCRel && "TLSGD shouldn't be PC-relative");
148  return getTLSGDReloc(Kind);
149 
151  if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
152  return ELF::R_390_GOTENT;
153  llvm_unreachable("Only PC-relative GOT accesses are supported for now");
154 
156  assert(IsPCRel && "@PLT shouldt be PC-relative");
157  return getPLTReloc(Kind);
158 
159  default:
160  llvm_unreachable("Modifier not supported");
161  }
162 }
163 
164 std::unique_ptr<MCObjectTargetWriter>
166  return llvm::make_unique<SystemZObjectWriter>(OSABI);
167 }
This class represents lattice values for constants.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
block Block Frequency true
MCSymbolRefExpr::VariantKind getAccessVariant() const
Definition: MCValue.cpp:47
static unsigned getTLSGDReloc(unsigned Kind)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:74
std::unique_ptr< MCObjectTargetWriter > createSystemZObjectWriter(uint8_t OSABI)
static unsigned getRelocType(const MCValue &Target, const MCFixupKind FixupKind, const bool IsPCRel)
Translates generic PPC fixup kind to Mach-O/PPC relocation type enum.
A four-byte fixup.
Definition: MCFixup.h:26
static unsigned getPCRelReloc(unsigned Kind)
Context object for machine code objects.
Definition: MCContext.h:63
static unsigned getTLSLEReloc(unsigned Kind)
static unsigned getPLTReloc(unsigned Kind)
static unsigned getTLSLDMReloc(unsigned Kind)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A one-byte fixup.
Definition: MCFixup.h:24
PowerPC TLS Dynamic Call Fixup
static unsigned getAbsoluteReloc(unsigned Kind)
Target - Wrapper for Target specific information.
A eight-byte fixup.
Definition: MCFixup.h:27
static unsigned getTLSLDOReloc(unsigned Kind)
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A two-byte fixup.
Definition: MCFixup.h:25
MCFixupKind getKind() const
Definition: MCFixup.h:123