LLVM  6.0.0svn
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/MCObjectWriter.h"
18 #include "llvm/MC/MCValue.h"
21 #include <cstdint>
22 
23 using namespace llvm;
24 
25 namespace {
26 
27  class ARMELFObjectWriter : public MCELFObjectTargetWriter {
28  enum { DefaultEABIVersion = 0x05000000U };
29 
30  unsigned GetRelocTypeInner(const MCValue &Target, const MCFixup &Fixup,
31  bool IsPCRel, MCContext &Ctx) const;
32 
33  public:
34  ARMELFObjectWriter(uint8_t OSABI);
35 
36  ~ARMELFObjectWriter() override = default;
37 
38  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
39  const MCFixup &Fixup, bool IsPCRel) const override;
40 
41  bool needsRelocateWithSymbol(const MCSymbol &Sym,
42  unsigned Type) const override;
43  };
44 
45 } // end anonymous namespace
46 
47 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
48  : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
49  ELF::EM_ARM,
50  /*HasRelocationAddend*/ false) {}
51 
52 bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
53  unsigned Type) const {
54  // FIXME: This is extremely conservative. This really needs to use a
55  // whitelist with a clear explanation for why each realocation needs to
56  // point to the symbol, not to the section.
57  switch (Type) {
58  default:
59  return true;
60 
61  case ELF::R_ARM_PREL31:
62  case ELF::R_ARM_ABS32:
63  return false;
64  }
65 }
66 
67 // Need to examine the Fixup when determining whether to
68 // emit the relocation as an explicit symbol or as a section relative
69 // offset
71  const MCFixup &Fixup,
72  bool IsPCRel) const {
73  return GetRelocTypeInner(Target, Fixup, IsPCRel, Ctx);
74 }
75 
76 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
77  const MCFixup &Fixup,
78  bool IsPCRel,
79  MCContext &Ctx) const {
81 
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:
90  llvm_unreachable("Unsupported Modifier");
91  case MCSymbolRefExpr::VK_None:
92  return ELF::R_ARM_REL32;
93  case MCSymbolRefExpr::VK_GOTTPOFF:
94  return ELF::R_ARM_TLS_IE32;
95  case MCSymbolRefExpr::VK_ARM_GOT_PREL:
96  return ELF::R_ARM_GOT_PREL;
97  case MCSymbolRefExpr::VK_ARM_PREL31:
98  return ELF::R_ARM_PREL31;
99  }
100  case ARM::fixup_arm_blx:
102  switch (Modifier) {
103  case MCSymbolRefExpr::VK_PLT:
104  return ELF::R_ARM_CALL;
105  case MCSymbolRefExpr::VK_TLSCALL:
106  return ELF::R_ARM_TLS_CALL;
107  default:
108  return ELF::R_ARM_CALL;
109  }
113  return ELF::R_ARM_JUMP24;
115  return ELF::R_ARM_THM_JUMP19;
117  return ELF::R_ARM_THM_JUMP24;
119  return ELF::R_ARM_MOVT_PREL;
121  return ELF::R_ARM_MOVW_PREL_NC;
123  return ELF::R_ARM_THM_MOVT_PREL;
125  return ELF::R_ARM_THM_MOVW_PREL_NC;
127  return ELF::R_ARM_THM_JUMP11;
129  return ELF::R_ARM_THM_JUMP8;
132  switch (Modifier) {
133  case MCSymbolRefExpr::VK_TLSCALL:
134  return ELF::R_ARM_THM_TLS_CALL;
135  default:
136  return ELF::R_ARM_THM_CALL;
137  }
138  }
139  }
140  switch ((unsigned)Fixup.getKind()) {
141  default:
142  Ctx.reportFatalError(Fixup.getLoc(), "unsupported relocation on symbol");
143  return ELF::R_ARM_NONE;
144  case FK_Data_1:
145  switch (Modifier) {
146  default:
147  llvm_unreachable("unsupported Modifier");
148  case MCSymbolRefExpr::VK_None:
149  return ELF::R_ARM_ABS8;
150  }
151  case FK_Data_2:
152  switch (Modifier) {
153  default:
154  llvm_unreachable("unsupported modifier");
155  case MCSymbolRefExpr::VK_None:
156  return ELF::R_ARM_ABS16;
157  }
158  case FK_Data_4:
159  switch (Modifier) {
160  default:
161  llvm_unreachable("Unsupported Modifier");
162  case MCSymbolRefExpr::VK_ARM_NONE:
163  return ELF::R_ARM_NONE;
164  case MCSymbolRefExpr::VK_GOT:
165  return ELF::R_ARM_GOT_BREL;
166  case MCSymbolRefExpr::VK_TLSGD:
167  return ELF::R_ARM_TLS_GD32;
168  case MCSymbolRefExpr::VK_TPOFF:
169  return ELF::R_ARM_TLS_LE32;
170  case MCSymbolRefExpr::VK_GOTTPOFF:
171  return ELF::R_ARM_TLS_IE32;
172  case MCSymbolRefExpr::VK_None:
173  return ELF::R_ARM_ABS32;
174  case MCSymbolRefExpr::VK_GOTOFF:
175  return ELF::R_ARM_GOTOFF32;
176  case MCSymbolRefExpr::VK_ARM_GOT_PREL:
177  return ELF::R_ARM_GOT_PREL;
178  case MCSymbolRefExpr::VK_ARM_TARGET1:
179  return ELF::R_ARM_TARGET1;
180  case MCSymbolRefExpr::VK_ARM_TARGET2:
181  return ELF::R_ARM_TARGET2;
182  case MCSymbolRefExpr::VK_ARM_PREL31:
183  return ELF::R_ARM_PREL31;
184  case MCSymbolRefExpr::VK_ARM_SBREL:
185  return ELF::R_ARM_SBREL32;
186  case MCSymbolRefExpr::VK_ARM_TLSLDO:
187  return ELF::R_ARM_TLS_LDO32;
188  case MCSymbolRefExpr::VK_TLSCALL:
189  return ELF::R_ARM_TLS_CALL;
190  case MCSymbolRefExpr::VK_TLSDESC:
191  return ELF::R_ARM_TLS_GOTDESC;
192  case MCSymbolRefExpr::VK_TLSLDM:
193  return ELF::R_ARM_TLS_LDM32;
194  case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
195  return ELF::R_ARM_TLS_DESCSEQ;
196  }
199  return ELF::R_ARM_JUMP24;
201  switch (Modifier) {
202  default:
203  llvm_unreachable("Unsupported Modifier");
204  case MCSymbolRefExpr::VK_None:
205  return ELF::R_ARM_MOVT_ABS;
206  case MCSymbolRefExpr::VK_ARM_SBREL:
207  return ELF::R_ARM_MOVT_BREL;
208  }
210  switch (Modifier) {
211  default:
212  llvm_unreachable("Unsupported Modifier");
213  case MCSymbolRefExpr::VK_None:
214  return ELF::R_ARM_MOVW_ABS_NC;
215  case MCSymbolRefExpr::VK_ARM_SBREL:
216  return ELF::R_ARM_MOVW_BREL_NC;
217  }
219  switch (Modifier) {
220  default:
221  llvm_unreachable("Unsupported Modifier");
222  case MCSymbolRefExpr::VK_None:
223  return ELF::R_ARM_THM_MOVT_ABS;
224  case MCSymbolRefExpr::VK_ARM_SBREL:
225  return ELF::R_ARM_THM_MOVT_BREL;
226  }
228  switch (Modifier) {
229  default:
230  llvm_unreachable("Unsupported Modifier");
231  case MCSymbolRefExpr::VK_None:
232  return ELF::R_ARM_THM_MOVW_ABS_NC;
233  case MCSymbolRefExpr::VK_ARM_SBREL:
234  return ELF::R_ARM_THM_MOVW_BREL_NC;
235  }
236  }
237 }
238 
239 std::unique_ptr<MCObjectWriter>
241  bool IsLittleEndian) {
242  return createELFObjectWriter(llvm::make_unique<ARMELFObjectWriter>(OSABI), OS,
243  IsLittleEndian);
244 }
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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
MCSymbolRefExpr::VariantKind getAccessVariant() const
Definition: MCValue.cpp:46
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
std::unique_ptr< MCObjectWriter > createELFObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
LLVM_ATTRIBUTE_NORETURN void reportFatalError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:584
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
#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:112
Target - Wrapper for Target specific information.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:337
std::unique_ptr< MCObjectWriter > createARMELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI, bool IsLittleEndian)
Construct an ELF Mach-O object writer.
A two-byte fixup.
Definition: MCFixup.h:25
MCFixupKind getKind() const
Definition: MCFixup.h:93