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);
76 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
77 MCSymbolRefExpr::VariantKind Kind2;
78 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
79 MCSymbolRefExpr::VK_None;
80 dbgs() << "considering symbol "
81 << Section.getSectionName() << "/"
82 << Symbol.getName() << "/"
83 << " Rel:" << (unsigned)RelocType
84 << " Kind: " << (int)Kind << "/" << (int)Kind2
86 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
87 << Symbol.isVariable() << "/" << Symbol.isTemporary()
88 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
90 if (IsPCRel) { ++PCRelCount;
93 // Most relocation types are emitted as explicit symbols
95 StringSwitch<bool>(Section.getSectionName())
96 .Case(".data.rel.ro.local", false)
97 .Case(".data.rel", false)
102 case ELF::R_ARM_ABS32:
103 // But things get strange with R_ARM_ABS32
104 // In this case, most things that go in .rodata show up
105 // as section relative relocations
107 StringSwitch<bool>(Section.getSectionName())
108 .Case(".data.rel.ro.local", false)
109 .Case(".data.rel", false)
110 .Case(".rodata", false)
119 StringSwitch<bool>(Section.getSectionName())
120 .Case(".data.rel.ro.local", false)
121 .Case(".rodata", false)
122 .Case(".data.rel", false)
127 default: EmitThisSym = true; break;
128 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
129 case ELF::R_ARM_PREL31: EmitThisSym = false; break;
135 if (! Symbol.isTemporary() && InNormalSection) {
141 // Need to examine the Fixup when determining whether to
142 // emit the relocation as an explicit symbol or as a section relative
144 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
145 const MCFixup &Fixup,
147 bool IsRelocWithSymbol,
148 int64_t Addend) const {
149 return GetRelocTypeInner(Target, Fixup, IsPCRel);
152 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
153 const MCFixup &Fixup,
154 bool IsPCRel) const {
155 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
156 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
160 switch ((unsigned)Fixup.getKind()) {
161 default: llvm_unreachable("Unimplemented");
164 default: llvm_unreachable("Unsupported Modifier");
165 case MCSymbolRefExpr::VK_None:
166 Type = ELF::R_ARM_REL32;
168 case MCSymbolRefExpr::VK_TLSGD:
169 llvm_unreachable("unimplemented");
170 case MCSymbolRefExpr::VK_GOTTPOFF:
171 Type = ELF::R_ARM_TLS_IE32;
175 case ARM::fixup_arm_blx:
176 case ARM::fixup_arm_uncondbl:
178 case MCSymbolRefExpr::VK_PLT:
179 Type = ELF::R_ARM_PLT32;
181 case MCSymbolRefExpr::VK_ARM_TLSCALL:
182 Type = ELF::R_ARM_TLS_CALL;
185 Type = ELF::R_ARM_CALL;
189 case ARM::fixup_arm_condbl:
190 case ARM::fixup_arm_condbranch:
191 case ARM::fixup_arm_uncondbranch:
192 Type = ELF::R_ARM_JUMP24;
194 case ARM::fixup_t2_condbranch:
195 case ARM::fixup_t2_uncondbranch:
196 Type = ELF::R_ARM_THM_JUMP24;
198 case ARM::fixup_arm_movt_hi16:
199 case ARM::fixup_arm_movt_hi16_pcrel:
200 Type = ELF::R_ARM_MOVT_PREL;
202 case ARM::fixup_arm_movw_lo16:
203 case ARM::fixup_arm_movw_lo16_pcrel:
204 Type = ELF::R_ARM_MOVW_PREL_NC;
206 case ARM::fixup_t2_movt_hi16:
207 case ARM::fixup_t2_movt_hi16_pcrel:
208 Type = ELF::R_ARM_THM_MOVT_PREL;
210 case ARM::fixup_t2_movw_lo16:
211 case ARM::fixup_t2_movw_lo16_pcrel:
212 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
214 case ARM::fixup_arm_thumb_bl:
215 case ARM::fixup_arm_thumb_blx:
217 case MCSymbolRefExpr::VK_ARM_TLSCALL:
218 Type = ELF::R_ARM_THM_TLS_CALL;
221 Type = ELF::R_ARM_THM_CALL;
227 switch ((unsigned)Fixup.getKind()) {
228 default: llvm_unreachable("invalid fixup kind!");
231 default: llvm_unreachable("Unsupported Modifier");
232 case MCSymbolRefExpr::VK_ARM_NONE:
233 Type = ELF::R_ARM_NONE;
235 case MCSymbolRefExpr::VK_GOT:
236 Type = ELF::R_ARM_GOT_BREL;
238 case MCSymbolRefExpr::VK_TLSGD:
239 Type = ELF::R_ARM_TLS_GD32;
241 case MCSymbolRefExpr::VK_TPOFF:
242 Type = ELF::R_ARM_TLS_LE32;
244 case MCSymbolRefExpr::VK_GOTTPOFF:
245 Type = ELF::R_ARM_TLS_IE32;
247 case MCSymbolRefExpr::VK_None:
248 Type = ELF::R_ARM_ABS32;
250 case MCSymbolRefExpr::VK_GOTOFF:
251 Type = ELF::R_ARM_GOTOFF32;
253 case MCSymbolRefExpr::VK_ARM_TARGET1:
254 Type = ELF::R_ARM_TARGET1;
256 case MCSymbolRefExpr::VK_ARM_TARGET2:
257 Type = ELF::R_ARM_TARGET2;
259 case MCSymbolRefExpr::VK_ARM_PREL31:
260 Type = ELF::R_ARM_PREL31;
262 case MCSymbolRefExpr::VK_ARM_TLSLDO:
263 Type = ELF::R_ARM_TLS_LDO32;
265 case MCSymbolRefExpr::VK_ARM_TLSCALL:
266 Type = ELF::R_ARM_TLS_CALL;
268 case MCSymbolRefExpr::VK_ARM_TLSDESC:
269 Type = ELF::R_ARM_TLS_GOTDESC;
271 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
272 Type = ELF::R_ARM_TLS_DESCSEQ;
276 case ARM::fixup_arm_ldst_pcrel_12:
277 case ARM::fixup_arm_pcrel_10:
278 case ARM::fixup_arm_adr_pcrel_12:
279 case ARM::fixup_arm_thumb_bl:
280 case ARM::fixup_arm_thumb_cb:
281 case ARM::fixup_arm_thumb_cp:
282 case ARM::fixup_arm_thumb_br:
283 llvm_unreachable("Unimplemented");
284 case ARM::fixup_arm_condbranch:
285 case ARM::fixup_arm_uncondbranch:
286 Type = ELF::R_ARM_JUMP24;
288 case ARM::fixup_arm_movt_hi16:
289 Type = ELF::R_ARM_MOVT_ABS;
291 case ARM::fixup_arm_movw_lo16:
292 Type = ELF::R_ARM_MOVW_ABS_NC;
294 case ARM::fixup_t2_movt_hi16:
295 Type = ELF::R_ARM_THM_MOVT_ABS;
297 case ARM::fixup_t2_movw_lo16:
298 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
306 MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS,
308 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
309 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);