1 //===-- ARMELFObjectWriter.cpp - ARM ELF Writer ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/ARMFixupKinds.h"
11 #include "MCTargetDesc/ARMMCTargetDesc.h"
12 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCFixup.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/raw_ostream.h"
26 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
27 enum { DefaultEABIVersion = 0x05000000U };
29 unsigned GetRelocTypeInner(const MCValue &Target, const MCFixup &Fixup,
30 bool IsPCRel, MCContext &Ctx) const;
33 ARMELFObjectWriter(uint8_t OSABI);
35 ~ARMELFObjectWriter() override = default;
37 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
38 const MCFixup &Fixup, bool IsPCRel) const override;
40 bool needsRelocateWithSymbol(const MCSymbol &Sym,
41 unsigned Type) const override;
44 } // end anonymous namespace
46 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
47 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
49 /*HasRelocationAddend*/ false) {}
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.
60 case ELF::R_ARM_PREL31:
61 case ELF::R_ARM_ABS32:
66 // Need to examine the Fixup when determining whether to
67 // emit the relocation as an explicit symbol or as a section relative
69 unsigned ARMELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
72 return GetRelocTypeInner(Target, Fixup, IsPCRel, Ctx);
75 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
78 MCContext &Ctx) const {
79 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
83 switch ((unsigned)Fixup.getKind()) {
85 Ctx.reportFatalError(Fixup.getLoc(), "unsupported relocation on symbol");
86 return ELF::R_ARM_NONE;
89 default: llvm_unreachable("Unsupported Modifier");
90 case MCSymbolRefExpr::VK_None:
91 Type = ELF::R_ARM_REL32;
93 case MCSymbolRefExpr::VK_TLSGD:
94 llvm_unreachable("unimplemented");
95 case MCSymbolRefExpr::VK_GOTTPOFF:
96 Type = ELF::R_ARM_TLS_IE32;
98 case MCSymbolRefExpr::VK_ARM_GOT_PREL:
99 Type = ELF::R_ARM_GOT_PREL;
101 case MCSymbolRefExpr::VK_ARM_PREL31:
102 Type = ELF::R_ARM_PREL31;
106 case ARM::fixup_arm_blx:
107 case ARM::fixup_arm_uncondbl:
109 case MCSymbolRefExpr::VK_PLT:
110 Type = ELF::R_ARM_CALL;
112 case MCSymbolRefExpr::VK_TLSCALL:
113 Type = ELF::R_ARM_TLS_CALL;
116 Type = ELF::R_ARM_CALL;
120 case ARM::fixup_arm_condbl:
121 case ARM::fixup_arm_condbranch:
122 case ARM::fixup_arm_uncondbranch:
123 Type = ELF::R_ARM_JUMP24;
125 case ARM::fixup_t2_condbranch:
126 Type = ELF::R_ARM_THM_JUMP19;
128 case ARM::fixup_t2_uncondbranch:
129 Type = ELF::R_ARM_THM_JUMP24;
131 case ARM::fixup_arm_movt_hi16:
132 Type = ELF::R_ARM_MOVT_PREL;
134 case ARM::fixup_arm_movw_lo16:
135 Type = ELF::R_ARM_MOVW_PREL_NC;
137 case ARM::fixup_t2_movt_hi16:
138 Type = ELF::R_ARM_THM_MOVT_PREL;
140 case ARM::fixup_t2_movw_lo16:
141 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
143 case ARM::fixup_arm_thumb_br:
144 Type = ELF::R_ARM_THM_JUMP11;
146 case ARM::fixup_arm_thumb_bcc:
147 Type = ELF::R_ARM_THM_JUMP8;
149 case ARM::fixup_arm_thumb_bl:
150 case ARM::fixup_arm_thumb_blx:
152 case MCSymbolRefExpr::VK_TLSCALL:
153 Type = ELF::R_ARM_THM_TLS_CALL;
156 Type = ELF::R_ARM_THM_CALL;
162 switch ((unsigned)Fixup.getKind()) {
164 Ctx.reportFatalError(Fixup.getLoc(), "unsupported relocation on symbol");
165 return ELF::R_ARM_NONE;
168 default: llvm_unreachable("unsupported Modifier");
169 case MCSymbolRefExpr::VK_None:
170 Type = ELF::R_ARM_ABS8;
176 default: llvm_unreachable("unsupported modifier");
177 case MCSymbolRefExpr::VK_None:
178 Type = ELF::R_ARM_ABS16;
184 default: llvm_unreachable("Unsupported Modifier");
185 case MCSymbolRefExpr::VK_ARM_NONE:
186 Type = ELF::R_ARM_NONE;
188 case MCSymbolRefExpr::VK_GOT:
189 Type = ELF::R_ARM_GOT_BREL;
191 case MCSymbolRefExpr::VK_TLSGD:
192 Type = ELF::R_ARM_TLS_GD32;
194 case MCSymbolRefExpr::VK_TPOFF:
195 Type = ELF::R_ARM_TLS_LE32;
197 case MCSymbolRefExpr::VK_GOTTPOFF:
198 Type = ELF::R_ARM_TLS_IE32;
200 case MCSymbolRefExpr::VK_None:
201 Type = ELF::R_ARM_ABS32;
203 case MCSymbolRefExpr::VK_GOTOFF:
204 Type = ELF::R_ARM_GOTOFF32;
206 case MCSymbolRefExpr::VK_ARM_GOT_PREL:
207 Type = ELF::R_ARM_GOT_PREL;
209 case MCSymbolRefExpr::VK_ARM_TARGET1:
210 Type = ELF::R_ARM_TARGET1;
212 case MCSymbolRefExpr::VK_ARM_TARGET2:
213 Type = ELF::R_ARM_TARGET2;
215 case MCSymbolRefExpr::VK_ARM_PREL31:
216 Type = ELF::R_ARM_PREL31;
218 case MCSymbolRefExpr::VK_ARM_SBREL:
219 Type = ELF::R_ARM_SBREL32;
221 case MCSymbolRefExpr::VK_ARM_TLSLDO:
222 Type = ELF::R_ARM_TLS_LDO32;
224 case MCSymbolRefExpr::VK_TLSCALL:
225 Type = ELF::R_ARM_TLS_CALL;
227 case MCSymbolRefExpr::VK_TLSDESC:
228 Type = ELF::R_ARM_TLS_GOTDESC;
230 case MCSymbolRefExpr::VK_TLSLDM:
231 Type = ELF::R_ARM_TLS_LDM32;
233 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
234 Type = ELF::R_ARM_TLS_DESCSEQ;
238 case ARM::fixup_arm_ldst_pcrel_12:
239 case ARM::fixup_arm_pcrel_10:
240 case ARM::fixup_arm_adr_pcrel_12:
241 case ARM::fixup_arm_thumb_bl:
242 case ARM::fixup_arm_thumb_cb:
243 case ARM::fixup_arm_thumb_cp:
244 case ARM::fixup_arm_thumb_br:
245 llvm_unreachable("Unimplemented");
246 case ARM::fixup_arm_condbranch:
247 case ARM::fixup_arm_uncondbranch:
248 Type = ELF::R_ARM_JUMP24;
250 case ARM::fixup_arm_movt_hi16:
252 default: llvm_unreachable("Unsupported Modifier");
253 case MCSymbolRefExpr::VK_None:
254 Type = ELF::R_ARM_MOVT_ABS;
256 case MCSymbolRefExpr::VK_ARM_SBREL:
257 Type = ELF:: R_ARM_MOVT_BREL;
261 case ARM::fixup_arm_movw_lo16:
263 default: llvm_unreachable("Unsupported Modifier");
264 case MCSymbolRefExpr::VK_None:
265 Type = ELF::R_ARM_MOVW_ABS_NC;
267 case MCSymbolRefExpr::VK_ARM_SBREL:
268 Type = ELF:: R_ARM_MOVW_BREL_NC;
272 case ARM::fixup_t2_movt_hi16:
274 default: llvm_unreachable("Unsupported Modifier");
275 case MCSymbolRefExpr::VK_None:
276 Type = ELF::R_ARM_THM_MOVT_ABS;
278 case MCSymbolRefExpr::VK_ARM_SBREL:
279 Type = ELF:: R_ARM_THM_MOVT_BREL;
283 case ARM::fixup_t2_movw_lo16:
285 default: llvm_unreachable("Unsupported Modifier");
286 case MCSymbolRefExpr::VK_None:
287 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
289 case MCSymbolRefExpr::VK_ARM_SBREL:
290 Type = ELF:: R_ARM_THM_MOVW_BREL_NC;
300 MCObjectWriter *llvm::createARMELFObjectWriter(raw_pwrite_stream &OS,
302 bool IsLittleEndian) {
303 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
304 return createELFObjectWriter(MOTW, OS, IsLittleEndian);