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/ARMMCTargetDesc.h"
11 #include "MCTargetDesc/ARMFixupKinds.h"
12 #include "llvm/ADT/Statistic.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCSectionELF.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
25 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
26 enum { DefaultEABIVersion = 0x05000000U };
27 unsigned GetRelocTypeInner(const MCValue &Target,
33 ARMELFObjectWriter(uint8_t OSABI);
35 virtual ~ARMELFObjectWriter();
37 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
38 bool IsPCRel, bool IsRelocWithSymbol) const override;
39 const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
40 const MCValue &Target, const MCFragment &F,
42 bool IsPCRel) const override;
46 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
47 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
49 /*HasRelocationAddend*/ false) {}
51 ARMELFObjectWriter::~ARMELFObjectWriter() {}
53 // In ARM, _MergedGlobals and other most symbols get emitted directly.
54 // I.e. not as an offset to a section symbol.
55 // This code is an approximation of what ARM/gcc does.
57 STATISTIC(PCRelCount, "Total number of PIC Relocations");
58 STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
60 const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
61 const MCValue &Target,
65 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
66 bool EmitThisSym = false;
68 const MCSectionELF &Section =
69 static_cast<const MCSectionELF&>(Symbol.getSection());
70 bool InNormalSection = true;
71 unsigned RelocType = 0;
72 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
73 assert(!Target.getSymB() ||
74 Target.getSymB()->getKind() == MCSymbolRefExpr::VK_None);
77 MCSymbolRefExpr::VariantKind Kind = Fixup.getAccessVariant();
78 dbgs() << "considering symbol "
79 << Section.getSectionName() << "/"
80 << Symbol.getName() << "/"
81 << " Rel:" << (unsigned)RelocType
82 << " Kind: " << (int)Kind
84 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
85 << Symbol.isVariable() << "/" << Symbol.isTemporary()
86 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
88 if (IsPCRel) { ++PCRelCount;
91 // Most relocation types are emitted as explicit symbols
93 StringSwitch<bool>(Section.getSectionName())
94 .Case(".data.rel.ro.local", false)
95 .Case(".data.rel", false)
100 case ELF::R_ARM_ABS32:
101 // But things get strange with R_ARM_ABS32
102 // In this case, most things that go in .rodata show up
103 // as section relative relocations
105 StringSwitch<bool>(Section.getSectionName())
106 .Case(".data.rel.ro.local", false)
107 .Case(".data.rel", false)
108 .Case(".rodata", false)
117 StringSwitch<bool>(Section.getSectionName())
118 .Case(".data.rel.ro.local", false)
119 .Case(".rodata", false)
120 .Case(".data.rel", false)
125 default: EmitThisSym = true; break;
126 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
127 case ELF::R_ARM_PREL31: EmitThisSym = false; break;
133 if (! Symbol.isTemporary() && InNormalSection) {
139 // Need to examine the Fixup when determining whether to
140 // emit the relocation as an explicit symbol or as a section relative
142 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
143 const MCFixup &Fixup,
145 bool IsRelocWithSymbol) const {
146 return GetRelocTypeInner(Target, Fixup, IsPCRel);
149 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
150 const MCFixup &Fixup,
151 bool IsPCRel) const {
152 MCSymbolRefExpr::VariantKind Modifier = Fixup.getAccessVariant();
156 switch ((unsigned)Fixup.getKind()) {
157 default: llvm_unreachable("Unimplemented");
160 default: llvm_unreachable("Unsupported Modifier");
161 case MCSymbolRefExpr::VK_None:
162 Type = ELF::R_ARM_REL32;
164 case MCSymbolRefExpr::VK_TLSGD:
165 llvm_unreachable("unimplemented");
166 case MCSymbolRefExpr::VK_GOTTPOFF:
167 Type = ELF::R_ARM_TLS_IE32;
171 case ARM::fixup_arm_blx:
172 case ARM::fixup_arm_uncondbl:
174 case MCSymbolRefExpr::VK_PLT:
175 Type = ELF::R_ARM_PLT32;
177 case MCSymbolRefExpr::VK_ARM_TLSCALL:
178 Type = ELF::R_ARM_TLS_CALL;
181 Type = ELF::R_ARM_CALL;
185 case ARM::fixup_arm_condbl:
186 case ARM::fixup_arm_condbranch:
187 case ARM::fixup_arm_uncondbranch:
188 Type = ELF::R_ARM_JUMP24;
190 case ARM::fixup_t2_condbranch:
191 case ARM::fixup_t2_uncondbranch:
192 Type = ELF::R_ARM_THM_JUMP24;
194 case ARM::fixup_arm_movt_hi16:
195 case ARM::fixup_arm_movt_hi16_pcrel:
196 Type = ELF::R_ARM_MOVT_PREL;
198 case ARM::fixup_arm_movw_lo16:
199 case ARM::fixup_arm_movw_lo16_pcrel:
200 Type = ELF::R_ARM_MOVW_PREL_NC;
202 case ARM::fixup_t2_movt_hi16:
203 case ARM::fixup_t2_movt_hi16_pcrel:
204 Type = ELF::R_ARM_THM_MOVT_PREL;
206 case ARM::fixup_t2_movw_lo16:
207 case ARM::fixup_t2_movw_lo16_pcrel:
208 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
210 case ARM::fixup_arm_thumb_bl:
211 case ARM::fixup_arm_thumb_blx:
213 case MCSymbolRefExpr::VK_ARM_TLSCALL:
214 Type = ELF::R_ARM_THM_TLS_CALL;
217 Type = ELF::R_ARM_THM_CALL;
223 switch ((unsigned)Fixup.getKind()) {
224 default: llvm_unreachable("invalid fixup kind!");
227 default: llvm_unreachable("Unsupported Modifier");
228 case MCSymbolRefExpr::VK_ARM_NONE:
229 Type = ELF::R_ARM_NONE;
231 case MCSymbolRefExpr::VK_GOT:
232 Type = ELF::R_ARM_GOT_BREL;
234 case MCSymbolRefExpr::VK_TLSGD:
235 Type = ELF::R_ARM_TLS_GD32;
237 case MCSymbolRefExpr::VK_TPOFF:
238 Type = ELF::R_ARM_TLS_LE32;
240 case MCSymbolRefExpr::VK_GOTTPOFF:
241 Type = ELF::R_ARM_TLS_IE32;
243 case MCSymbolRefExpr::VK_None:
244 Type = ELF::R_ARM_ABS32;
246 case MCSymbolRefExpr::VK_GOTOFF:
247 Type = ELF::R_ARM_GOTOFF32;
249 case MCSymbolRefExpr::VK_ARM_TARGET1:
250 Type = ELF::R_ARM_TARGET1;
252 case MCSymbolRefExpr::VK_ARM_TARGET2:
253 Type = ELF::R_ARM_TARGET2;
255 case MCSymbolRefExpr::VK_ARM_PREL31:
256 Type = ELF::R_ARM_PREL31;
258 case MCSymbolRefExpr::VK_ARM_TLSLDO:
259 Type = ELF::R_ARM_TLS_LDO32;
261 case MCSymbolRefExpr::VK_ARM_TLSCALL:
262 Type = ELF::R_ARM_TLS_CALL;
264 case MCSymbolRefExpr::VK_ARM_TLSDESC:
265 Type = ELF::R_ARM_TLS_GOTDESC;
267 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
268 Type = ELF::R_ARM_TLS_DESCSEQ;
272 case ARM::fixup_arm_ldst_pcrel_12:
273 case ARM::fixup_arm_pcrel_10:
274 case ARM::fixup_arm_adr_pcrel_12:
275 case ARM::fixup_arm_thumb_bl:
276 case ARM::fixup_arm_thumb_cb:
277 case ARM::fixup_arm_thumb_cp:
278 case ARM::fixup_arm_thumb_br:
279 llvm_unreachable("Unimplemented");
280 case ARM::fixup_arm_condbranch:
281 case ARM::fixup_arm_uncondbranch:
282 Type = ELF::R_ARM_JUMP24;
284 case ARM::fixup_arm_movt_hi16:
285 Type = ELF::R_ARM_MOVT_ABS;
287 case ARM::fixup_arm_movw_lo16:
288 Type = ELF::R_ARM_MOVW_ABS_NC;
290 case ARM::fixup_t2_movt_hi16:
291 Type = ELF::R_ARM_THM_MOVT_ABS;
293 case ARM::fixup_t2_movw_lo16:
294 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
302 MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS,
304 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
305 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);