blob: 9193e40bedf97127a2850e486444f5a2a286752a [file] [log] [blame]
Rafael Espindola76097852011-12-22 01:06:53 +00001//===-- ARMELFObjectWriter.cpp - ARM ELF Writer ---------------------------===//
Rafael Espindola69bbda02011-12-22 00:37:50 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Rafael Espindola69bbda02011-12-22 00:37:50 +000010#include "MCTargetDesc/ARMMCTargetDesc.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000011#include "MCTargetDesc/ARMFixupKinds.h"
Rafael Espindola69bbda02011-12-22 00:37:50 +000012#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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/Support/Debug.h"
19#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/raw_ostream.h"
Rafael Espindola69bbda02011-12-22 00:37:50 +000021
22using namespace llvm;
23
24namespace {
25 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
26 enum { DefaultEABIVersion = 0x05000000U };
Rafael Espindola69bbda02011-12-22 00:37:50 +000027 unsigned GetRelocTypeInner(const MCValue &Target,
28 const MCFixup &Fixup,
29 bool IsPCRel) const;
30
31
32 public:
33 ARMELFObjectWriter(uint8_t OSABI);
34
35 virtual ~ARMELFObjectWriter();
Rafael Espindola6db2d9262011-12-22 02:58:12 +000036
Rafael Espindola69bbda02011-12-22 00:37:50 +000037 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
38 bool IsPCRel, bool IsRelocWithSymbol,
39 int64_t Addend) const;
40 virtual unsigned getEFlags() const;
Rafael Espindola6db2d9262011-12-22 02:58:12 +000041 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
42 const MCValue &Target,
43 const MCFragment &F,
44 const MCFixup &Fixup,
45 bool IsPCRel) const;
Rafael Espindola69bbda02011-12-22 00:37:50 +000046 };
47}
48
49ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
50 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
51 ELF::EM_ARM,
52 /*HasRelocationAddend*/ false) {}
53
54ARMELFObjectWriter::~ARMELFObjectWriter() {}
55
56// FIXME: get the real EABI Version from the Triple.
57unsigned ARMELFObjectWriter::getEFlags() const {
58 return ELF::EF_ARM_EABIMASK & DefaultEABIVersion;
59}
60
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.
64
65STATISTIC(PCRelCount, "Total number of PIC Relocations");
66STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
67
68const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
69 const MCValue &Target,
70 const MCFragment &F,
71 const MCFixup &Fixup,
72 bool IsPCRel) const {
James Molloy2d8955a2012-01-28 15:58:32 +000073 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
Rafael Espindola69bbda02011-12-22 00:37:50 +000074 bool EmitThisSym = false;
75
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);
81
82 DEBUG(
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
92 << " Tmp:"
93 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
94 << Symbol.isVariable() << "/" << Symbol.isTemporary()
95 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
96
97 if (IsPCRel) { ++PCRelCount;
98 switch (RelocType) {
99 default:
100 // Most relocation types are emitted as explicit symbols
101 InNormalSection =
102 StringSwitch<bool>(Section.getSectionName())
103 .Case(".data.rel.ro.local", false)
104 .Case(".data.rel", false)
105 .Case(".bss", false)
106 .Default(true);
107 EmitThisSym = true;
108 break;
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
113 InNormalSection =
114 StringSwitch<bool>(Section.getSectionName())
115 .Case(".data.rel.ro.local", false)
116 .Case(".data.rel", false)
117 .Case(".rodata", false)
118 .Case(".bss", false)
119 .Default(true);
120 EmitThisSym = false;
121 break;
122 }
123 } else {
124 NonPCRelCount++;
125 InNormalSection =
126 StringSwitch<bool>(Section.getSectionName())
127 .Case(".data.rel.ro.local", false)
128 .Case(".rodata", false)
129 .Case(".data.rel", false)
130 .Case(".bss", false)
131 .Default(true);
132
133 switch (RelocType) {
134 default: EmitThisSym = true; break;
135 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
Logan Chien37c74612012-12-12 07:14:46 +0000136 case ELF::R_ARM_PREL31: EmitThisSym = false; break;
Rafael Espindola69bbda02011-12-22 00:37:50 +0000137 }
138 }
139
140 if (EmitThisSym)
141 return &Symbol;
142 if (! Symbol.isTemporary() && InNormalSection) {
143 return &Symbol;
144 }
145 return NULL;
146}
147
148// Need to examine the Fixup when determining whether to
149// emit the relocation as an explicit symbol or as a section relative
150// offset
151unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
152 const MCFixup &Fixup,
153 bool IsPCRel,
154 bool IsRelocWithSymbol,
155 int64_t Addend) const {
156 return GetRelocTypeInner(Target, Fixup, IsPCRel);
157}
158
159unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
160 const MCFixup &Fixup,
161 bool IsPCRel) const {
162 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
163 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
164
165 unsigned Type = 0;
166 if (IsPCRel) {
167 switch ((unsigned)Fixup.getKind()) {
Craig Topperbc219812012-02-07 02:50:20 +0000168 default: llvm_unreachable("Unimplemented");
Rafael Espindola69bbda02011-12-22 00:37:50 +0000169 case FK_Data_4:
170 switch (Modifier) {
171 default: llvm_unreachable("Unsupported Modifier");
172 case MCSymbolRefExpr::VK_None:
173 Type = ELF::R_ARM_REL32;
174 break;
175 case MCSymbolRefExpr::VK_ARM_TLSGD:
Craig Topperbc219812012-02-07 02:50:20 +0000176 llvm_unreachable("unimplemented");
Rafael Espindola69bbda02011-12-22 00:37:50 +0000177 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
178 Type = ELF::R_ARM_TLS_IE32;
179 break;
180 }
181 break;
Jim Grosbach7b25ecf2012-02-27 21:36:23 +0000182 case ARM::fixup_arm_blx:
Jan Wen Voungc9a4e262012-06-19 16:03:02 +0000183 case ARM::fixup_arm_uncondbl:
Rafael Espindola69bbda02011-12-22 00:37:50 +0000184 switch (Modifier) {
185 case MCSymbolRefExpr::VK_ARM_PLT:
186 Type = ELF::R_ARM_PLT32;
187 break;
188 default:
189 Type = ELF::R_ARM_CALL;
190 break;
191 }
192 break;
James Molloycb0809b2012-03-30 09:15:32 +0000193 case ARM::fixup_arm_condbl:
Rafael Espindola69bbda02011-12-22 00:37:50 +0000194 case ARM::fixup_arm_condbranch:
Jan Wen Voungc9a4e262012-06-19 16:03:02 +0000195 case ARM::fixup_arm_uncondbranch:
Rafael Espindola69bbda02011-12-22 00:37:50 +0000196 Type = ELF::R_ARM_JUMP24;
197 break;
Logan Chien8fccd012012-09-01 15:06:36 +0000198 case ARM::fixup_t2_condbranch:
199 case ARM::fixup_t2_uncondbranch:
200 Type = ELF::R_ARM_THM_JUMP24;
201 break;
Rafael Espindola69bbda02011-12-22 00:37:50 +0000202 case ARM::fixup_arm_movt_hi16:
203 case ARM::fixup_arm_movt_hi16_pcrel:
204 Type = ELF::R_ARM_MOVT_PREL;
205 break;
206 case ARM::fixup_arm_movw_lo16:
207 case ARM::fixup_arm_movw_lo16_pcrel:
208 Type = ELF::R_ARM_MOVW_PREL_NC;
209 break;
210 case ARM::fixup_t2_movt_hi16:
211 case ARM::fixup_t2_movt_hi16_pcrel:
212 Type = ELF::R_ARM_THM_MOVT_PREL;
213 break;
214 case ARM::fixup_t2_movw_lo16:
215 case ARM::fixup_t2_movw_lo16_pcrel:
216 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
217 break;
218 case ARM::fixup_arm_thumb_bl:
219 case ARM::fixup_arm_thumb_blx:
Rafael Espindolab975c272011-12-22 21:36:43 +0000220 Type = ELF::R_ARM_THM_CALL;
Rafael Espindola69bbda02011-12-22 00:37:50 +0000221 break;
222 }
223 } else {
224 switch ((unsigned)Fixup.getKind()) {
225 default: llvm_unreachable("invalid fixup kind!");
226 case FK_Data_4:
227 switch (Modifier) {
David Blaikie4d6ccb52012-01-20 21:51:11 +0000228 default: llvm_unreachable("Unsupported Modifier");
Logan Chien37c74612012-12-12 07:14:46 +0000229 case MCSymbolRefExpr::VK_ARM_NONE:
230 Type = ELF::R_ARM_NONE;
231 break;
Rafael Espindola69bbda02011-12-22 00:37:50 +0000232 case MCSymbolRefExpr::VK_ARM_GOT:
233 Type = ELF::R_ARM_GOT_BREL;
234 break;
235 case MCSymbolRefExpr::VK_ARM_TLSGD:
236 Type = ELF::R_ARM_TLS_GD32;
237 break;
238 case MCSymbolRefExpr::VK_ARM_TPOFF:
239 Type = ELF::R_ARM_TLS_LE32;
240 break;
241 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
242 Type = ELF::R_ARM_TLS_IE32;
243 break;
244 case MCSymbolRefExpr::VK_None:
245 Type = ELF::R_ARM_ABS32;
246 break;
247 case MCSymbolRefExpr::VK_ARM_GOTOFF:
248 Type = ELF::R_ARM_GOTOFF32;
249 break;
James Molloy34982572012-01-26 09:25:43 +0000250 case MCSymbolRefExpr::VK_ARM_TARGET1:
251 Type = ELF::R_ARM_TARGET1;
252 break;
Anton Korobeynikov12cfa112012-11-09 20:20:12 +0000253 case MCSymbolRefExpr::VK_ARM_TARGET2:
254 Type = ELF::R_ARM_TARGET2;
255 break;
Logan Chien37c74612012-12-12 07:14:46 +0000256 case MCSymbolRefExpr::VK_ARM_PREL31:
257 Type = ELF::R_ARM_PREL31;
258 break;
259 }
Rafael Espindola69bbda02011-12-22 00:37:50 +0000260 break;
261 case ARM::fixup_arm_ldst_pcrel_12:
262 case ARM::fixup_arm_pcrel_10:
263 case ARM::fixup_arm_adr_pcrel_12:
264 case ARM::fixup_arm_thumb_bl:
265 case ARM::fixup_arm_thumb_cb:
266 case ARM::fixup_arm_thumb_cp:
267 case ARM::fixup_arm_thumb_br:
Craig Topperbc219812012-02-07 02:50:20 +0000268 llvm_unreachable("Unimplemented");
Rafael Espindola69bbda02011-12-22 00:37:50 +0000269 case ARM::fixup_arm_condbranch:
Jan Wen Voungc9a4e262012-06-19 16:03:02 +0000270 case ARM::fixup_arm_uncondbranch:
Rafael Espindola69bbda02011-12-22 00:37:50 +0000271 Type = ELF::R_ARM_JUMP24;
272 break;
273 case ARM::fixup_arm_movt_hi16:
274 Type = ELF::R_ARM_MOVT_ABS;
275 break;
276 case ARM::fixup_arm_movw_lo16:
277 Type = ELF::R_ARM_MOVW_ABS_NC;
278 break;
279 case ARM::fixup_t2_movt_hi16:
280 Type = ELF::R_ARM_THM_MOVT_ABS;
281 break;
282 case ARM::fixup_t2_movw_lo16:
283 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
284 break;
285 }
286 }
287
288 return Type;
289}
290
291MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS,
292 uint8_t OSABI) {
293 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
294 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);
295}