LLVM  5.0.0
ARMELFObjectWriter.cpp
Go to the documentation of this file.
1 //===-- ARMELFObjectWriter.cpp - ARM 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"
13 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCFixup.h"
17 #include "llvm/MC/MCValue.h"
20 #include <cstdint>
21 
22 using namespace llvm;
23 
24 namespace {
25 
26  class ARMELFObjectWriter : public MCELFObjectTargetWriter {
27  enum { DefaultEABIVersion = 0x05000000U };
28 
29  unsigned GetRelocTypeInner(const MCValue &Target, const MCFixup &Fixup,
30  bool IsPCRel, MCContext &Ctx) const;
31 
32  public:
33  ARMELFObjectWriter(uint8_t OSABI);
34 
35  ~ARMELFObjectWriter() override = default;
36 
37  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
38  const MCFixup &Fixup, bool IsPCRel) const override;
39 
40  bool needsRelocateWithSymbol(const MCSymbol &Sym,
41  unsigned Type) const override;
42  };
43 
44 } // end anonymous namespace
45 
46 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
47  : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
48  ELF::EM_ARM,
49  /*HasRelocationAddend*/ false) {}
50 
51 bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
52  unsigned Type) const {
53  // FIXME: This is extremely conservative. This really needs to use a
54  // whitelist with a clear explanation for why each realocation needs to
55  // point to the symbol, not to the section.
56  switch (Type) {
57  default:
58  return true;
59 
60  case ELF::R_ARM_PREL31:
61  case ELF::R_ARM_ABS32:
62  return false;
63  }
64 }
65 
66 // Need to examine the Fixup when determining whether to
67 // emit the relocation as an explicit symbol or as a section relative
68 // offset
70  const MCFixup &Fixup,
71  bool IsPCRel) const {
72  return GetRelocTypeInner(Target, Fixup, IsPCRel, Ctx);
73 }
74 
75 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
76  const MCFixup &Fixup,
77  bool IsPCRel,
78  MCContext &Ctx) const {
80 
81  unsigned Type = 0;
82  if (IsPCRel) {
83  switch ((unsigned)Fixup.getKind()) {
84  default:
85  Ctx.reportFatalError(Fixup.getLoc(), "unsupported relocation on symbol");
86  return ELF::R_ARM_NONE;
87  case FK_Data_4:
88  switch (Modifier) {
89  default: llvm_unreachable("Unsupported Modifier");
91  Type = ELF::R_ARM_REL32;
92  break;
94  llvm_unreachable("unimplemented");
96  Type = ELF::R_ARM_TLS_IE32;
97  break;
99  Type = ELF::R_ARM_GOT_PREL;
100  break;
102  Type = ELF::R_ARM_PREL31;
103  break;
104  }
105  break;
106  case ARM::fixup_arm_blx:
108  switch (Modifier) {
110  Type = ELF::R_ARM_CALL;
111  break;
113  Type = ELF::R_ARM_TLS_CALL;
114  break;
115  default:
116  Type = ELF::R_ARM_CALL;
117  break;
118  }
119  break;
123  Type = ELF::R_ARM_JUMP24;
124  break;
126  Type = ELF::R_ARM_THM_JUMP19;
127  break;
129  Type = ELF::R_ARM_THM_JUMP24;
130  break;
132  Type = ELF::R_ARM_MOVT_PREL;
133  break;
135  Type = ELF::R_ARM_MOVW_PREL_NC;
136  break;
138  Type = ELF::R_ARM_THM_MOVT_PREL;
139  break;
141  Type = ELF::R_ARM_THM_MOVW_PREL_NC;
142  break;
144  Type = ELF::R_ARM_THM_JUMP11;
145  break;
147  Type = ELF::R_ARM_THM_JUMP8;
148  break;
151  switch (Modifier) {
153  Type = ELF::R_ARM_THM_TLS_CALL;
154  break;
155  default:
156  Type = ELF::R_ARM_THM_CALL;
157  break;
158  }
159  break;
160  }
161  } else {
162  switch ((unsigned)Fixup.getKind()) {
163  default:
164  Ctx.reportFatalError(Fixup.getLoc(), "unsupported relocation on symbol");
165  return ELF::R_ARM_NONE;
166  case FK_Data_1:
167  switch (Modifier) {
168  default: llvm_unreachable("unsupported Modifier");
170  Type = ELF::R_ARM_ABS8;
171  break;
172  }
173  break;
174  case FK_Data_2:
175  switch (Modifier) {
176  default: llvm_unreachable("unsupported modifier");
178  Type = ELF::R_ARM_ABS16;
179  break;
180  }
181  break;
182  case FK_Data_4:
183  switch (Modifier) {
184  default: llvm_unreachable("Unsupported Modifier");
186  Type = ELF::R_ARM_NONE;
187  break;
189  Type = ELF::R_ARM_GOT_BREL;
190  break;
192  Type = ELF::R_ARM_TLS_GD32;
193  break;
195  Type = ELF::R_ARM_TLS_LE32;
196  break;
198  Type = ELF::R_ARM_TLS_IE32;
199  break;
201  Type = ELF::R_ARM_ABS32;
202  break;
204  Type = ELF::R_ARM_GOTOFF32;
205  break;
207  Type = ELF::R_ARM_GOT_PREL;
208  break;
210  Type = ELF::R_ARM_TARGET1;
211  break;
213  Type = ELF::R_ARM_TARGET2;
214  break;
216  Type = ELF::R_ARM_PREL31;
217  break;
219  Type = ELF::R_ARM_SBREL32;
220  break;
222  Type = ELF::R_ARM_TLS_LDO32;
223  break;
225  Type = ELF::R_ARM_TLS_CALL;
226  break;
228  Type = ELF::R_ARM_TLS_GOTDESC;
229  break;
231  Type = ELF::R_ARM_TLS_LDM32;
232  break;
234  Type = ELF::R_ARM_TLS_DESCSEQ;
235  break;
236  }
237  break;
245  llvm_unreachable("Unimplemented");
248  Type = ELF::R_ARM_JUMP24;
249  break;
251  switch (Modifier) {
252  default: llvm_unreachable("Unsupported Modifier");
254  Type = ELF::R_ARM_MOVT_ABS;
255  break;
257  Type = ELF:: R_ARM_MOVT_BREL;
258  break;
259  }
260  break;
262  switch (Modifier) {
263  default: llvm_unreachable("Unsupported Modifier");
265  Type = ELF::R_ARM_MOVW_ABS_NC;
266  break;
268  Type = ELF:: R_ARM_MOVW_BREL_NC;
269  break;
270  }
271  break;
273  switch (Modifier) {
274  default: llvm_unreachable("Unsupported Modifier");
276  Type = ELF::R_ARM_THM_MOVT_ABS;
277  break;
279  Type = ELF:: R_ARM_THM_MOVT_BREL;
280  break;
281  }
282  break;
284  switch (Modifier) {
285  default: llvm_unreachable("Unsupported Modifier");
287  Type = ELF::R_ARM_THM_MOVW_ABS_NC;
288  break;
290  Type = ELF:: R_ARM_THM_MOVW_BREL_NC;
291  break;
292  }
293  break;
294  }
295  }
296 
297  return Type;
298 }
299 
301  uint8_t OSABI,
302  bool IsLittleEndian) {
303  MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
304  return createELFObjectWriter(MOTW, OS, IsLittleEndian);
305 }
SMLoc getLoc() const
Definition: MCFixup.h:112
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
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:59
MCObjectWriter * createARMELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI, bool IsLittleEndian)
Construct an ELF Mach-O object writer.
LLVM_ATTRIBUTE_NORETURN void reportFatalError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:622
Function Alias Analysis false
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
MCSymbolRefExpr::VariantKind getAccessVariant() const
Definition: MCValue.cpp:46
MCFixupKind getKind() const
Definition: MCFixup.h:93
A one-byte fixup.
Definition: MCFixup.h:24
Target - Wrapper for Target specific information.
llvm_unreachable("Unknown RegBank!")
MCObjectWriter * createELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
A two-byte fixup.
Definition: MCFixup.h:25