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 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
38 bool IsPCRel, bool IsRelocWithSymbol,
39 int64_t Addend) const;
40 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
41 const MCValue &Target,
48 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
49 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
51 /*HasRelocationAddend*/ false) {}
53 ARMELFObjectWriter::~ARMELFObjectWriter() {}
55 // In ARM, _MergedGlobals and other most symbols get emitted directly.
56 // I.e. not as an offset to a section symbol.
57 // This code is an approximation of what ARM/gcc does.
59 STATISTIC(PCRelCount, "Total number of PIC Relocations");
60 STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
62 const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
63 const MCValue &Target,
67 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
68 bool EmitThisSym = false;
70 const MCSectionELF &Section =
71 static_cast<const MCSectionELF&>(Symbol.getSection());
72 bool InNormalSection = true;
73 unsigned RelocType = 0;
74 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
77 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
78 MCSymbolRefExpr::VariantKind Kind2;
79 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
80 MCSymbolRefExpr::VK_None;
81 dbgs() << "considering symbol "
82 << Section.getSectionName() << "/"
83 << Symbol.getName() << "/"
84 << " Rel:" << (unsigned)RelocType
85 << " Kind: " << (int)Kind << "/" << (int)Kind2
87 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
88 << Symbol.isVariable() << "/" << Symbol.isTemporary()
89 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
91 if (IsPCRel) { ++PCRelCount;
94 // Most relocation types are emitted as explicit symbols
96 StringSwitch<bool>(Section.getSectionName())
97 .Case(".data.rel.ro.local", false)
98 .Case(".data.rel", false)
103 case ELF::R_ARM_ABS32:
104 // But things get strange with R_ARM_ABS32
105 // In this case, most things that go in .rodata show up
106 // as section relative relocations
108 StringSwitch<bool>(Section.getSectionName())
109 .Case(".data.rel.ro.local", false)
110 .Case(".data.rel", false)
111 .Case(".rodata", false)
120 StringSwitch<bool>(Section.getSectionName())
121 .Case(".data.rel.ro.local", false)
122 .Case(".rodata", false)
123 .Case(".data.rel", false)
128 default: EmitThisSym = true; break;
129 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
130 case ELF::R_ARM_PREL31: EmitThisSym = false; break;
136 if (! Symbol.isTemporary() && InNormalSection) {
142 // Need to examine the Fixup when determining whether to
143 // emit the relocation as an explicit symbol or as a section relative
145 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
146 const MCFixup &Fixup,
148 bool IsRelocWithSymbol,
149 int64_t Addend) const {
150 return GetRelocTypeInner(Target, Fixup, IsPCRel);
153 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
154 const MCFixup &Fixup,
155 bool IsPCRel) const {
156 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
157 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
161 switch ((unsigned)Fixup.getKind()) {
162 default: llvm_unreachable("Unimplemented");
165 default: llvm_unreachable("Unsupported Modifier");
166 case MCSymbolRefExpr::VK_None:
167 Type = ELF::R_ARM_REL32;
169 case MCSymbolRefExpr::VK_TLSGD:
170 llvm_unreachable("unimplemented");
171 case MCSymbolRefExpr::VK_GOTTPOFF:
172 Type = ELF::R_ARM_TLS_IE32;
176 case ARM::fixup_arm_blx:
177 case ARM::fixup_arm_uncondbl:
179 case MCSymbolRefExpr::VK_PLT:
180 Type = ELF::R_ARM_PLT32;
182 case MCSymbolRefExpr::VK_ARM_TLSCALL:
183 Type = ELF::R_ARM_TLS_CALL;
186 Type = ELF::R_ARM_CALL;
190 case ARM::fixup_arm_condbl:
191 case ARM::fixup_arm_condbranch:
192 case ARM::fixup_arm_uncondbranch:
193 Type = ELF::R_ARM_JUMP24;
195 case ARM::fixup_t2_condbranch:
196 case ARM::fixup_t2_uncondbranch:
197 Type = ELF::R_ARM_THM_JUMP24;
199 case ARM::fixup_arm_movt_hi16:
200 case ARM::fixup_arm_movt_hi16_pcrel:
201 Type = ELF::R_ARM_MOVT_PREL;
203 case ARM::fixup_arm_movw_lo16:
204 case ARM::fixup_arm_movw_lo16_pcrel:
205 Type = ELF::R_ARM_MOVW_PREL_NC;
207 case ARM::fixup_t2_movt_hi16:
208 case ARM::fixup_t2_movt_hi16_pcrel:
209 Type = ELF::R_ARM_THM_MOVT_PREL;
211 case ARM::fixup_t2_movw_lo16:
212 case ARM::fixup_t2_movw_lo16_pcrel:
213 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
215 case ARM::fixup_arm_thumb_bl:
216 case ARM::fixup_arm_thumb_blx:
218 case MCSymbolRefExpr::VK_ARM_TLSCALL:
219 Type = ELF::R_ARM_THM_TLS_CALL;
222 Type = ELF::R_ARM_THM_CALL;
228 switch ((unsigned)Fixup.getKind()) {
229 default: llvm_unreachable("invalid fixup kind!");
232 default: llvm_unreachable("Unsupported Modifier");
233 case MCSymbolRefExpr::VK_ARM_NONE:
234 Type = ELF::R_ARM_NONE;
236 case MCSymbolRefExpr::VK_GOT:
237 Type = ELF::R_ARM_GOT_BREL;
239 case MCSymbolRefExpr::VK_TLSGD:
240 Type = ELF::R_ARM_TLS_GD32;
242 case MCSymbolRefExpr::VK_TPOFF:
243 Type = ELF::R_ARM_TLS_LE32;
245 case MCSymbolRefExpr::VK_GOTTPOFF:
246 Type = ELF::R_ARM_TLS_IE32;
248 case MCSymbolRefExpr::VK_None:
249 Type = ELF::R_ARM_ABS32;
251 case MCSymbolRefExpr::VK_GOTOFF:
252 Type = ELF::R_ARM_GOTOFF32;
254 case MCSymbolRefExpr::VK_ARM_TARGET1:
255 Type = ELF::R_ARM_TARGET1;
257 case MCSymbolRefExpr::VK_ARM_TARGET2:
258 Type = ELF::R_ARM_TARGET2;
260 case MCSymbolRefExpr::VK_ARM_PREL31:
261 Type = ELF::R_ARM_PREL31;
263 case MCSymbolRefExpr::VK_ARM_TLSLDO:
264 Type = ELF::R_ARM_TLS_LDO32;
266 case MCSymbolRefExpr::VK_ARM_TLSCALL:
267 Type = ELF::R_ARM_TLS_CALL;
269 case MCSymbolRefExpr::VK_ARM_TLSDESC:
270 Type = ELF::R_ARM_TLS_GOTDESC;
272 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
273 Type = ELF::R_ARM_TLS_DESCSEQ;
277 case ARM::fixup_arm_ldst_pcrel_12:
278 case ARM::fixup_arm_pcrel_10:
279 case ARM::fixup_arm_adr_pcrel_12:
280 case ARM::fixup_arm_thumb_bl:
281 case ARM::fixup_arm_thumb_cb:
282 case ARM::fixup_arm_thumb_cp:
283 case ARM::fixup_arm_thumb_br:
284 llvm_unreachable("Unimplemented");
285 case ARM::fixup_arm_condbranch:
286 case ARM::fixup_arm_uncondbranch:
287 Type = ELF::R_ARM_JUMP24;
289 case ARM::fixup_arm_movt_hi16:
290 Type = ELF::R_ARM_MOVT_ABS;
292 case ARM::fixup_arm_movw_lo16:
293 Type = ELF::R_ARM_MOVW_ABS_NC;
295 case ARM::fixup_t2_movt_hi16:
296 Type = ELF::R_ARM_THM_MOVT_ABS;
298 case ARM::fixup_t2_movw_lo16:
299 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
307 MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS,
309 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
310 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);