1 //===-- X86ELFObjectWriter.cpp - X86 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 };
27 const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
28 const MCValue &Target,
32 unsigned GetRelocTypeInner(const MCValue &Target,
38 ARMELFObjectWriter(uint8_t OSABI);
40 virtual ~ARMELFObjectWriter();
42 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
43 bool IsPCRel, bool IsRelocWithSymbol,
44 int64_t Addend) const;
45 virtual unsigned getEFlags() const;
49 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
50 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
52 /*HasRelocationAddend*/ false) {}
54 ARMELFObjectWriter::~ARMELFObjectWriter() {}
56 // FIXME: get the real EABI Version from the Triple.
57 unsigned ARMELFObjectWriter::getEFlags() const {
58 return ELF::EF_ARM_EABIMASK & DefaultEABIVersion;
61 // In ARM, _MergedGlobals and other most symbols get emitted directly.
62 // I.e. not as an offset to a section symbol.
63 // This code is an approximation of what ARM/gcc does.
65 STATISTIC(PCRelCount, "Total number of PIC Relocations");
66 STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
68 const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
69 const MCValue &Target,
73 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
74 bool EmitThisSym = false;
76 const MCSectionELF &Section =
77 static_cast<const MCSectionELF&>(Symbol.getSection());
78 bool InNormalSection = true;
79 unsigned RelocType = 0;
80 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
83 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
84 MCSymbolRefExpr::VariantKind Kind2;
85 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
86 MCSymbolRefExpr::VK_None;
87 dbgs() << "considering symbol "
88 << Section.getSectionName() << "/"
89 << Symbol.getName() << "/"
90 << " Rel:" << (unsigned)RelocType
91 << " Kind: " << (int)Kind << "/" << (int)Kind2
93 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
94 << Symbol.isVariable() << "/" << Symbol.isTemporary()
95 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
97 if (IsPCRel) { ++PCRelCount;
100 // Most relocation types are emitted as explicit symbols
102 StringSwitch<bool>(Section.getSectionName())
103 .Case(".data.rel.ro.local", false)
104 .Case(".data.rel", false)
109 case ELF::R_ARM_ABS32:
110 // But things get strange with R_ARM_ABS32
111 // In this case, most things that go in .rodata show up
112 // as section relative relocations
114 StringSwitch<bool>(Section.getSectionName())
115 .Case(".data.rel.ro.local", false)
116 .Case(".data.rel", false)
117 .Case(".rodata", false)
126 StringSwitch<bool>(Section.getSectionName())
127 .Case(".data.rel.ro.local", false)
128 .Case(".rodata", false)
129 .Case(".data.rel", false)
134 default: EmitThisSym = true; break;
135 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
141 if (! Symbol.isTemporary() && InNormalSection) {
147 // Need to examine the Fixup when determining whether to
148 // emit the relocation as an explicit symbol or as a section relative
150 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
151 const MCFixup &Fixup,
153 bool IsRelocWithSymbol,
154 int64_t Addend) const {
155 return GetRelocTypeInner(Target, Fixup, IsPCRel);
158 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
159 const MCFixup &Fixup,
160 bool IsPCRel) const {
161 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
162 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
166 switch ((unsigned)Fixup.getKind()) {
167 default: assert(0 && "Unimplemented");
170 default: llvm_unreachable("Unsupported Modifier");
171 case MCSymbolRefExpr::VK_None:
172 Type = ELF::R_ARM_REL32;
174 case MCSymbolRefExpr::VK_ARM_TLSGD:
175 assert(0 && "unimplemented");
177 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
178 Type = ELF::R_ARM_TLS_IE32;
182 case ARM::fixup_arm_uncondbranch:
184 case MCSymbolRefExpr::VK_ARM_PLT:
185 Type = ELF::R_ARM_PLT32;
188 Type = ELF::R_ARM_CALL;
192 case ARM::fixup_arm_condbranch:
193 Type = ELF::R_ARM_JUMP24;
195 case ARM::fixup_arm_movt_hi16:
196 case ARM::fixup_arm_movt_hi16_pcrel:
197 Type = ELF::R_ARM_MOVT_PREL;
199 case ARM::fixup_arm_movw_lo16:
200 case ARM::fixup_arm_movw_lo16_pcrel:
201 Type = ELF::R_ARM_MOVW_PREL_NC;
203 case ARM::fixup_t2_movt_hi16:
204 case ARM::fixup_t2_movt_hi16_pcrel:
205 Type = ELF::R_ARM_THM_MOVT_PREL;
207 case ARM::fixup_t2_movw_lo16:
208 case ARM::fixup_t2_movw_lo16_pcrel:
209 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
211 case ARM::fixup_arm_thumb_bl:
212 case ARM::fixup_arm_thumb_blx:
214 case MCSymbolRefExpr::VK_ARM_PLT:
215 Type = ELF::R_ARM_THM_CALL;
218 Type = ELF::R_ARM_NONE;
224 switch ((unsigned)Fixup.getKind()) {
225 default: llvm_unreachable("invalid fixup kind!");
228 default: llvm_unreachable("Unsupported Modifier"); break;
229 case MCSymbolRefExpr::VK_ARM_GOT:
230 Type = ELF::R_ARM_GOT_BREL;
232 case MCSymbolRefExpr::VK_ARM_TLSGD:
233 Type = ELF::R_ARM_TLS_GD32;
235 case MCSymbolRefExpr::VK_ARM_TPOFF:
236 Type = ELF::R_ARM_TLS_LE32;
238 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
239 Type = ELF::R_ARM_TLS_IE32;
241 case MCSymbolRefExpr::VK_None:
242 Type = ELF::R_ARM_ABS32;
244 case MCSymbolRefExpr::VK_ARM_GOTOFF:
245 Type = ELF::R_ARM_GOTOFF32;
249 case ARM::fixup_arm_ldst_pcrel_12:
250 case ARM::fixup_arm_pcrel_10:
251 case ARM::fixup_arm_adr_pcrel_12:
252 case ARM::fixup_arm_thumb_bl:
253 case ARM::fixup_arm_thumb_cb:
254 case ARM::fixup_arm_thumb_cp:
255 case ARM::fixup_arm_thumb_br:
256 assert(0 && "Unimplemented");
258 case ARM::fixup_arm_uncondbranch:
259 Type = ELF::R_ARM_CALL;
261 case ARM::fixup_arm_condbranch:
262 Type = ELF::R_ARM_JUMP24;
264 case ARM::fixup_arm_movt_hi16:
265 Type = ELF::R_ARM_MOVT_ABS;
267 case ARM::fixup_arm_movw_lo16:
268 Type = ELF::R_ARM_MOVW_ABS_NC;
270 case ARM::fixup_t2_movt_hi16:
271 Type = ELF::R_ARM_THM_MOVT_ABS;
273 case ARM::fixup_t2_movw_lo16:
274 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
282 MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS,
284 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
285 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);