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/Support/Debug.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/MC/MCELFObjectWriter.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCSectionELF.h"
19 #include "llvm/MC/MCValue.h"
24 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
25 enum { DefaultEABIVersion = 0x05000000U };
26 unsigned GetRelocTypeInner(const MCValue &Target,
32 ARMELFObjectWriter(uint8_t OSABI);
34 virtual ~ARMELFObjectWriter();
36 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
37 bool IsPCRel, bool IsRelocWithSymbol,
38 int64_t Addend) const;
39 virtual unsigned getEFlags() 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 // FIXME: get the real EABI Version from the Triple.
56 unsigned ARMELFObjectWriter::getEFlags() const {
57 return ELF::EF_ARM_EABIMASK & DefaultEABIVersion;
60 // In ARM, _MergedGlobals and other most symbols get emitted directly.
61 // I.e. not as an offset to a section symbol.
62 // This code is an approximation of what ARM/gcc does.
64 STATISTIC(PCRelCount, "Total number of PIC Relocations");
65 STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
67 const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
68 const MCValue &Target,
72 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
73 bool EmitThisSym = false;
75 const MCSectionELF &Section =
76 static_cast<const MCSectionELF&>(Symbol.getSection());
77 bool InNormalSection = true;
78 unsigned RelocType = 0;
79 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
82 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
83 MCSymbolRefExpr::VariantKind Kind2;
84 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
85 MCSymbolRefExpr::VK_None;
86 dbgs() << "considering symbol "
87 << Section.getSectionName() << "/"
88 << Symbol.getName() << "/"
89 << " Rel:" << (unsigned)RelocType
90 << " Kind: " << (int)Kind << "/" << (int)Kind2
92 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
93 << Symbol.isVariable() << "/" << Symbol.isTemporary()
94 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
96 if (IsPCRel) { ++PCRelCount;
99 // Most relocation types are emitted as explicit symbols
101 StringSwitch<bool>(Section.getSectionName())
102 .Case(".data.rel.ro.local", false)
103 .Case(".data.rel", false)
108 case ELF::R_ARM_ABS32:
109 // But things get strange with R_ARM_ABS32
110 // In this case, most things that go in .rodata show up
111 // as section relative relocations
113 StringSwitch<bool>(Section.getSectionName())
114 .Case(".data.rel.ro.local", false)
115 .Case(".data.rel", false)
116 .Case(".rodata", false)
125 StringSwitch<bool>(Section.getSectionName())
126 .Case(".data.rel.ro.local", false)
127 .Case(".rodata", false)
128 .Case(".data.rel", false)
133 default: EmitThisSym = true; break;
134 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
140 if (! Symbol.isTemporary() && InNormalSection) {
146 // Need to examine the Fixup when determining whether to
147 // emit the relocation as an explicit symbol or as a section relative
149 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
150 const MCFixup &Fixup,
152 bool IsRelocWithSymbol,
153 int64_t Addend) const {
154 return GetRelocTypeInner(Target, Fixup, IsPCRel);
157 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
158 const MCFixup &Fixup,
159 bool IsPCRel) const {
160 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
161 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
165 switch ((unsigned)Fixup.getKind()) {
166 default: llvm_unreachable("Unimplemented");
169 default: llvm_unreachable("Unsupported Modifier");
170 case MCSymbolRefExpr::VK_None:
171 Type = ELF::R_ARM_REL32;
173 case MCSymbolRefExpr::VK_ARM_TLSGD:
174 llvm_unreachable("unimplemented");
175 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
176 Type = ELF::R_ARM_TLS_IE32;
180 case ARM::fixup_arm_uncondbranch:
182 case MCSymbolRefExpr::VK_ARM_PLT:
183 Type = ELF::R_ARM_PLT32;
186 Type = ELF::R_ARM_CALL;
190 case ARM::fixup_arm_condbranch:
191 Type = ELF::R_ARM_JUMP24;
193 case ARM::fixup_arm_movt_hi16:
194 case ARM::fixup_arm_movt_hi16_pcrel:
195 Type = ELF::R_ARM_MOVT_PREL;
197 case ARM::fixup_arm_movw_lo16:
198 case ARM::fixup_arm_movw_lo16_pcrel:
199 Type = ELF::R_ARM_MOVW_PREL_NC;
201 case ARM::fixup_t2_movt_hi16:
202 case ARM::fixup_t2_movt_hi16_pcrel:
203 Type = ELF::R_ARM_THM_MOVT_PREL;
205 case ARM::fixup_t2_movw_lo16:
206 case ARM::fixup_t2_movw_lo16_pcrel:
207 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
209 case ARM::fixup_arm_thumb_bl:
210 case ARM::fixup_arm_thumb_blx:
211 Type = ELF::R_ARM_THM_CALL;
215 switch ((unsigned)Fixup.getKind()) {
216 default: llvm_unreachable("invalid fixup kind!");
219 default: llvm_unreachable("Unsupported Modifier");
220 case MCSymbolRefExpr::VK_ARM_GOT:
221 Type = ELF::R_ARM_GOT_BREL;
223 case MCSymbolRefExpr::VK_ARM_TLSGD:
224 Type = ELF::R_ARM_TLS_GD32;
226 case MCSymbolRefExpr::VK_ARM_TPOFF:
227 Type = ELF::R_ARM_TLS_LE32;
229 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
230 Type = ELF::R_ARM_TLS_IE32;
232 case MCSymbolRefExpr::VK_None:
233 Type = ELF::R_ARM_ABS32;
235 case MCSymbolRefExpr::VK_ARM_GOTOFF:
236 Type = ELF::R_ARM_GOTOFF32;
238 case MCSymbolRefExpr::VK_ARM_TARGET1:
239 Type = ELF::R_ARM_TARGET1;
243 case ARM::fixup_arm_ldst_pcrel_12:
244 case ARM::fixup_arm_pcrel_10:
245 case ARM::fixup_arm_adr_pcrel_12:
246 case ARM::fixup_arm_thumb_bl:
247 case ARM::fixup_arm_thumb_cb:
248 case ARM::fixup_arm_thumb_cp:
249 case ARM::fixup_arm_thumb_br:
250 llvm_unreachable("Unimplemented");
251 case ARM::fixup_arm_uncondbranch:
252 Type = ELF::R_ARM_CALL;
254 case ARM::fixup_arm_condbranch:
255 Type = ELF::R_ARM_JUMP24;
257 case ARM::fixup_arm_movt_hi16:
258 Type = ELF::R_ARM_MOVT_ABS;
260 case ARM::fixup_arm_movw_lo16:
261 Type = ELF::R_ARM_MOVW_ABS_NC;
263 case ARM::fixup_t2_movt_hi16:
264 Type = ELF::R_ARM_THM_MOVT_ABS;
266 case ARM::fixup_t2_movw_lo16:
267 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
275 MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS,
277 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
278 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);