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,
39 int64_t Addend) const override;
40 const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
41 const MCValue &Target, const MCFragment &F,
43 bool IsPCRel) const override;
47 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
48 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
50 /*HasRelocationAddend*/ false) {}
52 ARMELFObjectWriter::~ARMELFObjectWriter() {}
54 // In ARM, _MergedGlobals and other most symbols get emitted directly.
55 // I.e. not as an offset to a section symbol.
56 // This code is an approximation of what ARM/gcc does.
58 STATISTIC(PCRelCount, "Total number of PIC Relocations");
59 STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
61 const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
62 const MCValue &Target,
66 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
67 bool EmitThisSym = false;
69 const MCSectionELF &Section =
70 static_cast<const MCSectionELF&>(Symbol.getSection());
71 bool InNormalSection = true;
72 unsigned RelocType = 0;
73 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
74 assert(!Target.getSymB() ||
75 Target.getSymB()->getKind() == MCSymbolRefExpr::VK_None);
78 MCSymbolRefExpr::VariantKind Kind = Fixup.getAccessVariant();
79 dbgs() << "considering symbol "
80 << Section.getSectionName() << "/"
81 << Symbol.getName() << "/"
82 << " Rel:" << (unsigned)RelocType
83 << " Kind: " << (int)Kind
85 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
86 << Symbol.isVariable() << "/" << Symbol.isTemporary()
87 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
89 if (IsPCRel) { ++PCRelCount;
92 // Most relocation types are emitted as explicit symbols
94 StringSwitch<bool>(Section.getSectionName())
95 .Case(".data.rel.ro.local", false)
96 .Case(".data.rel", false)
101 case ELF::R_ARM_ABS32:
102 // But things get strange with R_ARM_ABS32
103 // In this case, most things that go in .rodata show up
104 // as section relative relocations
106 StringSwitch<bool>(Section.getSectionName())
107 .Case(".data.rel.ro.local", false)
108 .Case(".data.rel", false)
109 .Case(".rodata", false)
118 StringSwitch<bool>(Section.getSectionName())
119 .Case(".data.rel.ro.local", false)
120 .Case(".rodata", false)
121 .Case(".data.rel", false)
126 default: EmitThisSym = true; break;
127 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
128 case ELF::R_ARM_PREL31: EmitThisSym = false; break;
134 if (! Symbol.isTemporary() && InNormalSection) {
140 // Need to examine the Fixup when determining whether to
141 // emit the relocation as an explicit symbol or as a section relative
143 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
144 const MCFixup &Fixup,
146 bool IsRelocWithSymbol,
147 int64_t Addend) const {
148 return GetRelocTypeInner(Target, Fixup, IsPCRel);
151 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
152 const MCFixup &Fixup,
153 bool IsPCRel) const {
154 MCSymbolRefExpr::VariantKind Modifier = Fixup.getAccessVariant();
158 switch ((unsigned)Fixup.getKind()) {
159 default: llvm_unreachable("Unimplemented");
162 default: llvm_unreachable("Unsupported Modifier");
163 case MCSymbolRefExpr::VK_None:
164 Type = ELF::R_ARM_REL32;
166 case MCSymbolRefExpr::VK_TLSGD:
167 llvm_unreachable("unimplemented");
168 case MCSymbolRefExpr::VK_GOTTPOFF:
169 Type = ELF::R_ARM_TLS_IE32;
173 case ARM::fixup_arm_blx:
174 case ARM::fixup_arm_uncondbl:
176 case MCSymbolRefExpr::VK_PLT:
177 Type = ELF::R_ARM_PLT32;
179 case MCSymbolRefExpr::VK_ARM_TLSCALL:
180 Type = ELF::R_ARM_TLS_CALL;
183 Type = ELF::R_ARM_CALL;
187 case ARM::fixup_arm_condbl:
188 case ARM::fixup_arm_condbranch:
189 case ARM::fixup_arm_uncondbranch:
190 Type = ELF::R_ARM_JUMP24;
192 case ARM::fixup_t2_condbranch:
193 case ARM::fixup_t2_uncondbranch:
194 Type = ELF::R_ARM_THM_JUMP24;
196 case ARM::fixup_arm_movt_hi16:
197 case ARM::fixup_arm_movt_hi16_pcrel:
198 Type = ELF::R_ARM_MOVT_PREL;
200 case ARM::fixup_arm_movw_lo16:
201 case ARM::fixup_arm_movw_lo16_pcrel:
202 Type = ELF::R_ARM_MOVW_PREL_NC;
204 case ARM::fixup_t2_movt_hi16:
205 case ARM::fixup_t2_movt_hi16_pcrel:
206 Type = ELF::R_ARM_THM_MOVT_PREL;
208 case ARM::fixup_t2_movw_lo16:
209 case ARM::fixup_t2_movw_lo16_pcrel:
210 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
212 case ARM::fixup_arm_thumb_bl:
213 case ARM::fixup_arm_thumb_blx:
215 case MCSymbolRefExpr::VK_ARM_TLSCALL:
216 Type = ELF::R_ARM_THM_TLS_CALL;
219 Type = ELF::R_ARM_THM_CALL;
225 switch ((unsigned)Fixup.getKind()) {
226 default: llvm_unreachable("invalid fixup kind!");
229 default: llvm_unreachable("Unsupported Modifier");
230 case MCSymbolRefExpr::VK_ARM_NONE:
231 Type = ELF::R_ARM_NONE;
233 case MCSymbolRefExpr::VK_GOT:
234 Type = ELF::R_ARM_GOT_BREL;
236 case MCSymbolRefExpr::VK_TLSGD:
237 Type = ELF::R_ARM_TLS_GD32;
239 case MCSymbolRefExpr::VK_TPOFF:
240 Type = ELF::R_ARM_TLS_LE32;
242 case MCSymbolRefExpr::VK_GOTTPOFF:
243 Type = ELF::R_ARM_TLS_IE32;
245 case MCSymbolRefExpr::VK_None:
246 Type = ELF::R_ARM_ABS32;
248 case MCSymbolRefExpr::VK_GOTOFF:
249 Type = ELF::R_ARM_GOTOFF32;
251 case MCSymbolRefExpr::VK_ARM_TARGET1:
252 Type = ELF::R_ARM_TARGET1;
254 case MCSymbolRefExpr::VK_ARM_TARGET2:
255 Type = ELF::R_ARM_TARGET2;
257 case MCSymbolRefExpr::VK_ARM_PREL31:
258 Type = ELF::R_ARM_PREL31;
260 case MCSymbolRefExpr::VK_ARM_TLSLDO:
261 Type = ELF::R_ARM_TLS_LDO32;
263 case MCSymbolRefExpr::VK_ARM_TLSCALL:
264 Type = ELF::R_ARM_TLS_CALL;
266 case MCSymbolRefExpr::VK_ARM_TLSDESC:
267 Type = ELF::R_ARM_TLS_GOTDESC;
269 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
270 Type = ELF::R_ARM_TLS_DESCSEQ;
274 case ARM::fixup_arm_ldst_pcrel_12:
275 case ARM::fixup_arm_pcrel_10:
276 case ARM::fixup_arm_adr_pcrel_12:
277 case ARM::fixup_arm_thumb_bl:
278 case ARM::fixup_arm_thumb_cb:
279 case ARM::fixup_arm_thumb_cp:
280 case ARM::fixup_arm_thumb_br:
281 llvm_unreachable("Unimplemented");
282 case ARM::fixup_arm_condbranch:
283 case ARM::fixup_arm_uncondbranch:
284 Type = ELF::R_ARM_JUMP24;
286 case ARM::fixup_arm_movt_hi16:
287 Type = ELF::R_ARM_MOVT_ABS;
289 case ARM::fixup_arm_movw_lo16:
290 Type = ELF::R_ARM_MOVW_ABS_NC;
292 case ARM::fixup_t2_movt_hi16:
293 Type = ELF::R_ARM_THM_MOVT_ABS;
295 case ARM::fixup_t2_movw_lo16:
296 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
304 MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS,
306 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
307 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);