LLVM  10.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 
51  bool &IsPCRel) {
52  switch (unsigned(Kind)) {
53  default:
54  llvm_unreachable("Unimplemented");
55  case FK_NONE:
56  return RT64_NONE;
58  Modifier = MCSymbolRefExpr::VK_GOT;
59  IsPCRel = true;
60  return RT64_64;
61  case FK_Data_8:
62  return RT64_64;
65  if (Modifier == MCSymbolRefExpr::VK_None && !IsPCRel)
66  return RT64_32S;
67  return RT64_32;
69  Modifier = MCSymbolRefExpr::VK_GOT;
70  IsPCRel = true;
71  return RT64_32;
72  case FK_Data_4:
73  case FK_PCRel_4:
78  return RT64_32;
80  Modifier = MCSymbolRefExpr::VK_PLT;
81  return RT64_32;
82  case FK_PCRel_2:
83  case FK_Data_2:
84  return RT64_16;
85  case FK_PCRel_1:
86  case FK_Data_1:
87  return RT64_8;
88  }
89 }
90 
91 static void checkIs32(MCContext &Ctx, SMLoc Loc, X86_64RelType Type) {
92  if (Type != RT64_32)
93  Ctx.reportError(Loc,
94  "32 bit reloc applied to a field with a different size");
95 }
96 
97 static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
99  X86_64RelType Type, bool IsPCRel,
100  MCFixupKind Kind) {
101  switch (Modifier) {
102  default:
103  llvm_unreachable("Unimplemented");
106  switch (Type) {
107  case RT64_NONE:
108  if (Modifier == MCSymbolRefExpr::VK_None)
109  return ELF::R_X86_64_NONE;
110  llvm_unreachable("Unimplemented");
111  case RT64_64:
112  return IsPCRel ? ELF::R_X86_64_PC64 : ELF::R_X86_64_64;
113  case RT64_32:
114  return IsPCRel ? ELF::R_X86_64_PC32 : ELF::R_X86_64_32;
115  case RT64_32S:
116  return ELF::R_X86_64_32S;
117  case RT64_16:
118  return IsPCRel ? ELF::R_X86_64_PC16 : ELF::R_X86_64_16;
119  case RT64_8:
120  return IsPCRel ? ELF::R_X86_64_PC8 : ELF::R_X86_64_8;
121  }
122  llvm_unreachable("unexpected relocation type!");
124  switch (Type) {
125  case RT64_64:
126  return IsPCRel ? ELF::R_X86_64_GOTPC64 : ELF::R_X86_64_GOT64;
127  case RT64_32:
128  return IsPCRel ? ELF::R_X86_64_GOTPC32 : ELF::R_X86_64_GOT32;
129  case RT64_32S:
130  case RT64_16:
131  case RT64_8:
132  case RT64_NONE:
133  llvm_unreachable("Unimplemented");
134  }
135  llvm_unreachable("unexpected relocation type!");
137  assert(Type == RT64_64);
138  assert(!IsPCRel);
139  return ELF::R_X86_64_GOTOFF64;
141  assert(!IsPCRel);
142  switch (Type) {
143  case RT64_64:
144  return ELF::R_X86_64_TPOFF64;
145  case RT64_32:
146  return ELF::R_X86_64_TPOFF32;
147  case RT64_32S:
148  case RT64_16:
149  case RT64_8:
150  case RT64_NONE:
151  llvm_unreachable("Unimplemented");
152  }
153  llvm_unreachable("unexpected relocation type!");
155  assert(!IsPCRel);
156  switch (Type) {
157  case RT64_64:
158  return ELF::R_X86_64_DTPOFF64;
159  case RT64_32:
160  return ELF::R_X86_64_DTPOFF32;
161  case RT64_32S:
162  case RT64_16:
163  case RT64_8:
164  case RT64_NONE:
165  llvm_unreachable("Unimplemented");
166  }
167  llvm_unreachable("unexpected relocation type!");
169  assert(!IsPCRel);
170  switch (Type) {
171  case RT64_64:
172  return ELF::R_X86_64_SIZE64;
173  case RT64_32:
174  return ELF::R_X86_64_SIZE32;
175  case RT64_32S:
176  case RT64_16:
177  case RT64_8:
178  case RT64_NONE:
179  llvm_unreachable("Unimplemented");
180  }
181  llvm_unreachable("unexpected relocation type!");
183  return ELF::R_X86_64_TLSDESC_CALL;
185  return ELF::R_X86_64_GOTPC32_TLSDESC;
187  checkIs32(Ctx, Loc, Type);
188  return ELF::R_X86_64_TLSGD;
190  checkIs32(Ctx, Loc, Type);
191  return ELF::R_X86_64_GOTTPOFF;
193  checkIs32(Ctx, Loc, Type);
194  return ELF::R_X86_64_TLSLD;
196  checkIs32(Ctx, Loc, Type);
197  return ELF::R_X86_64_PLT32;
199  checkIs32(Ctx, Loc, Type);
200  // Older versions of ld.bfd/ld.gold/lld
201  // do not support GOTPCRELX/REX_GOTPCRELX,
202  // and we want to keep back-compatibility.
203  if (!Ctx.getAsmInfo()->canRelaxRelocations())
204  return ELF::R_X86_64_GOTPCREL;
205  switch (unsigned(Kind)) {
206  default:
207  return ELF::R_X86_64_GOTPCREL;
209  return ELF::R_X86_64_GOTPCRELX;
212  return ELF::R_X86_64_REX_GOTPCRELX;
213  }
214  llvm_unreachable("unexpected relocation type!");
215  }
216 }
217 
219 
221  switch (T) {
222  case RT64_NONE:
223  return RT32_NONE;
224  case RT64_64:
225  llvm_unreachable("Unimplemented");
226  case RT64_32:
227  case RT64_32S:
228  return RT32_32;
229  case RT64_16:
230  return RT32_16;
231  case RT64_8:
232  return RT32_8;
233  }
234  llvm_unreachable("unexpected relocation type!");
235 }
236 
237 static unsigned getRelocType32(MCContext &Ctx,
239  X86_32RelType Type, bool IsPCRel,
240  MCFixupKind Kind) {
241  switch (Modifier) {
242  default:
243  llvm_unreachable("Unimplemented");
246  switch (Type) {
247  case RT32_NONE:
248  if (Modifier == MCSymbolRefExpr::VK_None)
249  return ELF::R_386_NONE;
250  llvm_unreachable("Unimplemented");
251  case RT32_32:
252  return IsPCRel ? ELF::R_386_PC32 : ELF::R_386_32;
253  case RT32_16:
254  return IsPCRel ? ELF::R_386_PC16 : ELF::R_386_16;
255  case RT32_8:
256  return IsPCRel ? ELF::R_386_PC8 : ELF::R_386_8;
257  }
258  llvm_unreachable("unexpected relocation type!");
260  assert(Type == RT32_32);
261  if (IsPCRel)
262  return ELF::R_386_GOTPC;
263  // Older versions of ld.bfd/ld.gold/lld do not support R_386_GOT32X and we
264  // want to maintain compatibility.
265  if (!Ctx.getAsmInfo()->canRelaxRelocations())
266  return ELF::R_386_GOT32;
267 
269  ? ELF::R_386_GOT32X
270  : ELF::R_386_GOT32;
272  assert(Type == RT32_32);
273  assert(!IsPCRel);
274  return ELF::R_386_GOTOFF;
276  return ELF::R_386_TLS_DESC_CALL;
278  return ELF::R_386_TLS_GOTDESC;
280  assert(Type == RT32_32);
281  assert(!IsPCRel);
282  return ELF::R_386_TLS_LE_32;
284  assert(Type == RT32_32);
285  assert(!IsPCRel);
286  return ELF::R_386_TLS_LDO_32;
288  assert(Type == RT32_32);
289  assert(!IsPCRel);
290  return ELF::R_386_TLS_GD;
292  assert(Type == RT32_32);
293  assert(!IsPCRel);
294  return ELF::R_386_TLS_IE_32;
296  assert(Type == RT32_32);
297  return ELF::R_386_PLT32;
299  assert(Type == RT32_32);
300  assert(!IsPCRel);
301  return ELF::R_386_TLS_IE;
303  assert(Type == RT32_32);
304  assert(!IsPCRel);
305  return ELF::R_386_TLS_LE;
307  assert(Type == RT32_32);
308  assert(!IsPCRel);
309  return ELF::R_386_TLS_GOTIE;
311  assert(Type == RT32_32);
312  assert(!IsPCRel);
313  return ELF::R_386_TLS_LDM;
314  }
315 }
316 
318  const MCFixup &Fixup,
319  bool IsPCRel) const {
321  MCFixupKind Kind = Fixup.getKind();
322  X86_64RelType Type = getType64(Kind, Modifier, IsPCRel);
323  if (getEMachine() == ELF::EM_X86_64)
324  return getRelocType64(Ctx, Fixup.getLoc(), Modifier, Type, IsPCRel, Kind);
325 
326  assert((getEMachine() == ELF::EM_386 || getEMachine() == ELF::EM_IAMCU) &&
327  "Unsupported ELF machine type.");
328  return getRelocType32(Ctx, Modifier, getType32(Type), IsPCRel, Kind);
329 }
330 
331 std::unique_ptr<MCObjectTargetWriter>
332 llvm::createX86ELFObjectWriter(bool IsELF64, uint8_t OSABI, uint16_t EMachine) {
333  return std::make_unique<X86ELFObjectWriter>(IsELF64, OSABI, EMachine);
334 }
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:318
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:656
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:77
A one-byte pc relative fixup.
Definition: MCFixup.h:29
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:26
Context object for machine code objects.
Definition: MCContext.h:65
static unsigned getRelocType32(MCContext &Ctx, MCSymbolRefExpr::VariantKind Modifier, X86_32RelType Type, bool IsPCRel, MCFixupKind Kind)
std::unique_ptr< MCObjectTargetWriter > createX86ELFObjectWriter(bool IsELF64, uint8_t OSABI, uint16_t EMachine)
Construct an X86 ELF object writer.
static X86_64RelType getType64(MCFixupKind Kind, MCSymbolRefExpr::VariantKind &Modifier, bool &IsPCRel)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
static X86_32RelType getType32(X86_64RelType T)
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:22
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:687
#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
SMLoc getLoc() const
Definition: MCFixup.h:197
A two-byte pc relative fixup.
Definition: MCFixup.h:30
A four-byte pc relative fixup.
Definition: MCFixup.h:31
A no-op fixup.
Definition: MCFixup.h:23
Target - Wrapper for Target specific information.
static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc, MCSymbolRefExpr::VariantKind Modifier, X86_64RelType Type, bool IsPCRel, MCFixupKind Kind)
A eight-byte fixup.
Definition: MCFixup.h:27
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Represents a location in source code.
Definition: SMLoc.h:23
A two-byte fixup.
Definition: MCFixup.h:25
MCFixupKind getKind() const
Definition: MCFixup.h:126