LLVM  9.0.0svn
X86ELFObjectWriter.cpp
Go to the documentation of this file.
1 //===-- X86ELFObjectWriter.cpp - X86 ELF Writer ---------------------------===//
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 
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCFixup.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCValue.h"
20 #include <cassert>
21 #include <cstdint>
22 
23 using namespace llvm;
24 
25 namespace {
26 
27 class X86ELFObjectWriter : public MCELFObjectTargetWriter {
28 public:
29  X86ELFObjectWriter(bool IsELF64, uint8_t OSABI, uint16_t EMachine);
30  ~X86ELFObjectWriter() override = default;
31 
32 protected:
33  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
34  const MCFixup &Fixup, bool IsPCRel) const override;
35 };
36 
37 } // end anonymous namespace
38 
39 X86ELFObjectWriter::X86ELFObjectWriter(bool IsELF64, uint8_t OSABI,
40  uint16_t EMachine)
41  : MCELFObjectTargetWriter(IsELF64, OSABI, EMachine,
42  // Only i386 and IAMCU use Rel instead of RelA.
43  /*HasRelocationAddend*/
44  (EMachine != ELF::EM_386) &&
45  (EMachine != ELF::EM_IAMCU)) {}
46 
48 
49 static X86_64RelType getType64(unsigned Kind,
51  bool &IsPCRel) {
52  switch (Kind) {
53  default:
54  llvm_unreachable("Unimplemented");
56  Modifier = MCSymbolRefExpr::VK_GOT;
57  IsPCRel = true;
58  return RT64_64;
59  case FK_Data_8:
60  return RT64_64;
63  if (Modifier == MCSymbolRefExpr::VK_None && !IsPCRel)
64  return RT64_32S;
65  return RT64_32;
67  Modifier = MCSymbolRefExpr::VK_GOT;
68  IsPCRel = true;
69  return RT64_32;
70  case FK_Data_4:
71  case FK_PCRel_4:
76  return RT64_32;
78  Modifier = MCSymbolRefExpr::VK_PLT;
79  return RT64_32;
80  case FK_PCRel_2:
81  case FK_Data_2:
82  return RT64_16;
83  case FK_PCRel_1:
84  case FK_Data_1:
85  return RT64_8;
86  }
87 }
88 
89 static void checkIs32(MCContext &Ctx, SMLoc Loc, X86_64RelType Type) {
90  if (Type != RT64_32)
91  Ctx.reportError(Loc,
92  "32 bit reloc applied to a field with a different size");
93 }
94 
95 static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
97  X86_64RelType Type, bool IsPCRel,
98  unsigned Kind) {
99  switch (Modifier) {
100  default:
101  llvm_unreachable("Unimplemented");
104  switch (Type) {
105  case RT64_64:
106  return IsPCRel ? ELF::R_X86_64_PC64 : ELF::R_X86_64_64;
107  case RT64_32:
108  return IsPCRel ? ELF::R_X86_64_PC32 : ELF::R_X86_64_32;
109  case RT64_32S:
110  return ELF::R_X86_64_32S;
111  case RT64_16:
112  return IsPCRel ? ELF::R_X86_64_PC16 : ELF::R_X86_64_16;
113  case RT64_8:
114  return IsPCRel ? ELF::R_X86_64_PC8 : ELF::R_X86_64_8;
115  }
117  switch (Type) {
118  case RT64_64:
119  return IsPCRel ? ELF::R_X86_64_GOTPC64 : ELF::R_X86_64_GOT64;
120  case RT64_32:
121  return IsPCRel ? ELF::R_X86_64_GOTPC32 : ELF::R_X86_64_GOT32;
122  case RT64_32S:
123  case RT64_16:
124  case RT64_8:
125  llvm_unreachable("Unimplemented");
126  }
128  assert(Type == RT64_64);
129  assert(!IsPCRel);
130  return ELF::R_X86_64_GOTOFF64;
132  assert(!IsPCRel);
133  switch (Type) {
134  case RT64_64:
135  return ELF::R_X86_64_TPOFF64;
136  case RT64_32:
137  return ELF::R_X86_64_TPOFF32;
138  case RT64_32S:
139  case RT64_16:
140  case RT64_8:
141  llvm_unreachable("Unimplemented");
142  }
144  assert(!IsPCRel);
145  switch (Type) {
146  case RT64_64:
147  return ELF::R_X86_64_DTPOFF64;
148  case RT64_32:
149  return ELF::R_X86_64_DTPOFF32;
150  case RT64_32S:
151  case RT64_16:
152  case RT64_8:
153  llvm_unreachable("Unimplemented");
154  }
156  assert(!IsPCRel);
157  switch (Type) {
158  case RT64_64:
159  return ELF::R_X86_64_SIZE64;
160  case RT64_32:
161  return ELF::R_X86_64_SIZE32;
162  case RT64_32S:
163  case RT64_16:
164  case RT64_8:
165  llvm_unreachable("Unimplemented");
166  }
168  return ELF::R_X86_64_TLSDESC_CALL;
170  return ELF::R_X86_64_GOTPC32_TLSDESC;
172  checkIs32(Ctx, Loc, Type);
173  return ELF::R_X86_64_TLSGD;
175  checkIs32(Ctx, Loc, Type);
176  return ELF::R_X86_64_GOTTPOFF;
178  checkIs32(Ctx, Loc, Type);
179  return ELF::R_X86_64_TLSLD;
181  checkIs32(Ctx, Loc, Type);
182  return ELF::R_X86_64_PLT32;
184  checkIs32(Ctx, Loc, Type);
185  // Older versions of ld.bfd/ld.gold/lld
186  // do not support GOTPCRELX/REX_GOTPCRELX,
187  // and we want to keep back-compatibility.
188  if (!Ctx.getAsmInfo()->canRelaxRelocations())
189  return ELF::R_X86_64_GOTPCREL;
190  switch (Kind) {
191  default:
192  return ELF::R_X86_64_GOTPCREL;
194  return ELF::R_X86_64_GOTPCRELX;
197  return ELF::R_X86_64_REX_GOTPCRELX;
198  }
199  }
200 }
201 
203 
205  switch (T) {
206  case RT64_64:
207  llvm_unreachable("Unimplemented");
208  case RT64_32:
209  case RT64_32S:
210  return RT32_32;
211  case RT64_16:
212  return RT32_16;
213  case RT64_8:
214  return RT32_8;
215  }
216  llvm_unreachable("unexpected relocation type!");
217 }
218 
219 static unsigned getRelocType32(MCContext &Ctx,
221  X86_32RelType Type, bool IsPCRel,
222  unsigned Kind) {
223  switch (Modifier) {
224  default:
225  llvm_unreachable("Unimplemented");
228  switch (Type) {
229  case RT32_32:
230  return IsPCRel ? ELF::R_386_PC32 : ELF::R_386_32;
231  case RT32_16:
232  return IsPCRel ? ELF::R_386_PC16 : ELF::R_386_16;
233  case RT32_8:
234  return IsPCRel ? ELF::R_386_PC8 : ELF::R_386_8;
235  }
237  assert(Type == RT32_32);
238  if (IsPCRel)
239  return ELF::R_386_GOTPC;
240  // Older versions of ld.bfd/ld.gold/lld do not support R_386_GOT32X and we
241  // want to maintain compatibility.
242  if (!Ctx.getAsmInfo()->canRelaxRelocations())
243  return ELF::R_386_GOT32;
244 
245  return Kind == X86::reloc_signed_4byte_relax ? ELF::R_386_GOT32X
246  : ELF::R_386_GOT32;
248  assert(Type == RT32_32);
249  assert(!IsPCRel);
250  return ELF::R_386_GOTOFF;
252  assert(Type == RT32_32);
253  assert(!IsPCRel);
254  return ELF::R_386_TLS_LE_32;
256  assert(Type == RT32_32);
257  assert(!IsPCRel);
258  return ELF::R_386_TLS_LDO_32;
260  assert(Type == RT32_32);
261  assert(!IsPCRel);
262  return ELF::R_386_TLS_GD;
264  assert(Type == RT32_32);
265  assert(!IsPCRel);
266  return ELF::R_386_TLS_IE_32;
268  assert(Type == RT32_32);
269  return ELF::R_386_PLT32;
271  assert(Type == RT32_32);
272  assert(!IsPCRel);
273  return ELF::R_386_TLS_IE;
275  assert(Type == RT32_32);
276  assert(!IsPCRel);
277  return ELF::R_386_TLS_LE;
279  assert(Type == RT32_32);
280  assert(!IsPCRel);
281  return ELF::R_386_TLS_GOTIE;
283  assert(Type == RT32_32);
284  assert(!IsPCRel);
285  return ELF::R_386_TLS_LDM;
286  }
287 }
288 
290  const MCFixup &Fixup,
291  bool IsPCRel) const {
293  unsigned Kind = Fixup.getKind();
294  X86_64RelType Type = getType64(Kind, Modifier, IsPCRel);
295  if (getEMachine() == ELF::EM_X86_64)
296  return getRelocType64(Ctx, Fixup.getLoc(), Modifier, Type, IsPCRel, Kind);
297 
298  assert((getEMachine() == ELF::EM_386 || getEMachine() == ELF::EM_IAMCU) &&
299  "Unsupported ELF machine type.");
300  return getRelocType32(Ctx, Modifier, getType32(Type), IsPCRel, Kind);
301 }
302 
303 std::unique_ptr<MCObjectTargetWriter>
304 llvm::createX86ELFObjectWriter(bool IsELF64, uint8_t OSABI, uint16_t EMachine) {
305  return llvm::make_unique<X86ELFObjectWriter>(IsELF64, OSABI, EMachine);
306 }
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:292
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:39
MCSymbolRefExpr::VariantKind getAccessVariant() const
Definition: MCValue.cpp:46
bool canRelaxRelocations() const
Definition: MCAsmInfo.h:634
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:73
A one-byte pc relative fixup.
Definition: MCFixup.h:27
static void checkIs32(MCContext &Ctx, SMLoc Loc, X86_64RelType Type)
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:25
static unsigned getRelocType32(MCContext &Ctx, MCSymbolRefExpr::VariantKind Modifier, X86_32RelType Type, bool IsPCRel, unsigned Kind)
Context object for machine code objects.
Definition: MCContext.h:62
static X86_64RelType getType64(unsigned Kind, MCSymbolRefExpr::VariantKind &Modifier, bool &IsPCRel)
std::unique_ptr< MCObjectTargetWriter > createX86ELFObjectWriter(bool IsELF64, uint8_t OSABI, uint16_t EMachine)
Construct an X86 ELF object writer.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
static X86_32RelType getType32(X86_64RelType T)
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:611
static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc, MCSymbolRefExpr::VariantKind Modifier, X86_64RelType Type, bool IsPCRel, unsigned Kind)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A one-byte fixup.
Definition: MCFixup.h:23
PowerPC TLS Dynamic Call Fixup
SMLoc getLoc() const
Definition: MCFixup.h:165
A two-byte pc relative fixup.
Definition: MCFixup.h:28
A four-byte pc relative fixup.
Definition: MCFixup.h:29
Target - Wrapper for Target specific information.
A eight-byte fixup.
Definition: MCFixup.h:26
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Represents a location in source code.
Definition: SMLoc.h:23
A two-byte fixup.
Definition: MCFixup.h:24
MCFixupKind getKind() const
Definition: MCFixup.h:122