blob: 25efd67c69b80aae2cd8bf22916c4cba5770556a [file] [log] [blame]
Rafael Espindola4449b212011-12-22 01:06:53 +00001//===-- ARMELFObjectWriter.cpp - ARM ELF Writer ---------------------------===//
Rafael Espindolaa0124052011-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 Espindolaa0124052011-12-22 00:37:50 +000010#include "MCTargetDesc/ARMMCTargetDesc.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "MCTargetDesc/ARMFixupKinds.h"
Rafael Espindolaa0124052011-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 Carruthed0881b2012-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 Espindolaa0124052011-12-22 00:37:50 +000021
22using namespace llvm;
23
24namespace {
25 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
26 enum { DefaultEABIVersion = 0x05000000U };
Rafael Espindolaa0124052011-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 Espindola84d00f12011-12-22 02:58:12 +000036
Craig Topperca7e3e52014-03-10 03:19:03 +000037 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,
Rafael Espindola84d00f12011-12-22 02:58:12 +000042 const MCFixup &Fixup,
Craig Topperca7e3e52014-03-10 03:19:03 +000043 bool IsPCRel) const override;
Rafael Espindolaa0124052011-12-22 00:37:50 +000044 };
45}
46
47ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
48 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
49 ELF::EM_ARM,
50 /*HasRelocationAddend*/ false) {}
51
52ARMELFObjectWriter::~ARMELFObjectWriter() {}
53
Rafael Espindolaa0124052011-12-22 00:37:50 +000054// 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.
57
58STATISTIC(PCRelCount, "Total number of PIC Relocations");
59STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
60
61const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
62 const MCValue &Target,
63 const MCFragment &F,
64 const MCFixup &Fixup,
65 bool IsPCRel) const {
James Molloyb47489d2012-01-28 15:58:32 +000066 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
Rafael Espindolaa0124052011-12-22 00:37:50 +000067 bool EmitThisSym = false;
68
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);
74
75 DEBUG(
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
85 << " Tmp:"
86 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
87 << Symbol.isVariable() << "/" << Symbol.isTemporary()
88 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
89
90 if (IsPCRel) { ++PCRelCount;
91 switch (RelocType) {
92 default:
93 // Most relocation types are emitted as explicit symbols
94 InNormalSection =
95 StringSwitch<bool>(Section.getSectionName())
96 .Case(".data.rel.ro.local", false)
97 .Case(".data.rel", false)
98 .Case(".bss", false)
99 .Default(true);
100 EmitThisSym = true;
101 break;
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
106 InNormalSection =
107 StringSwitch<bool>(Section.getSectionName())
108 .Case(".data.rel.ro.local", false)
109 .Case(".data.rel", false)
110 .Case(".rodata", false)
111 .Case(".bss", false)
112 .Default(true);
113 EmitThisSym = false;
114 break;
115 }
116 } else {
117 NonPCRelCount++;
118 InNormalSection =
119 StringSwitch<bool>(Section.getSectionName())
120 .Case(".data.rel.ro.local", false)
121 .Case(".rodata", false)
122 .Case(".data.rel", false)
123 .Case(".bss", false)
124 .Default(true);
125
126 switch (RelocType) {
127 default: EmitThisSym = true; break;
128 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
Logan Chien4dd14fb2012-12-12 07:14:46 +0000129 case ELF::R_ARM_PREL31: EmitThisSym = false; break;
Rafael Espindolaa0124052011-12-22 00:37:50 +0000130 }
131 }
132
133 if (EmitThisSym)
134 return &Symbol;
135 if (! Symbol.isTemporary() && InNormalSection) {
136 return &Symbol;
137 }
138 return NULL;
139}
140
141// Need to examine the Fixup when determining whether to
142// emit the relocation as an explicit symbol or as a section relative
143// offset
144unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
145 const MCFixup &Fixup,
146 bool IsPCRel,
147 bool IsRelocWithSymbol,
148 int64_t Addend) const {
149 return GetRelocTypeInner(Target, Fixup, IsPCRel);
150}
151
152unsigned 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();
157
158 unsigned Type = 0;
159 if (IsPCRel) {
160 switch ((unsigned)Fixup.getKind()) {
Craig Toppere55c5562012-02-07 02:50:20 +0000161 default: llvm_unreachable("Unimplemented");
Rafael Espindolaa0124052011-12-22 00:37:50 +0000162 case FK_Data_4:
163 switch (Modifier) {
164 default: llvm_unreachable("Unsupported Modifier");
165 case MCSymbolRefExpr::VK_None:
166 Type = ELF::R_ARM_REL32;
167 break;
David Peixotto8ad70b32013-12-04 22:43:20 +0000168 case MCSymbolRefExpr::VK_TLSGD:
Craig Toppere55c5562012-02-07 02:50:20 +0000169 llvm_unreachable("unimplemented");
David Peixotto8ad70b32013-12-04 22:43:20 +0000170 case MCSymbolRefExpr::VK_GOTTPOFF:
Rafael Espindolaa0124052011-12-22 00:37:50 +0000171 Type = ELF::R_ARM_TLS_IE32;
172 break;
173 }
174 break;
Jim Grosbach7b811d32012-02-27 21:36:23 +0000175 case ARM::fixup_arm_blx:
Jan Wen Voung7f5d79f2012-06-19 16:03:02 +0000176 case ARM::fixup_arm_uncondbl:
Rafael Espindolaa0124052011-12-22 00:37:50 +0000177 switch (Modifier) {
David Peixotto8ad70b32013-12-04 22:43:20 +0000178 case MCSymbolRefExpr::VK_PLT:
Rafael Espindolaa0124052011-12-22 00:37:50 +0000179 Type = ELF::R_ARM_PLT32;
180 break;
Saleem Abdulrasool6e00ca82014-01-30 04:02:31 +0000181 case MCSymbolRefExpr::VK_ARM_TLSCALL:
182 Type = ELF::R_ARM_TLS_CALL;
183 break;
Rafael Espindolaa0124052011-12-22 00:37:50 +0000184 default:
185 Type = ELF::R_ARM_CALL;
186 break;
187 }
188 break;
James Molloyfb5cd602012-03-30 09:15:32 +0000189 case ARM::fixup_arm_condbl:
Rafael Espindolaa0124052011-12-22 00:37:50 +0000190 case ARM::fixup_arm_condbranch:
Jan Wen Voung7f5d79f2012-06-19 16:03:02 +0000191 case ARM::fixup_arm_uncondbranch:
Rafael Espindolaa0124052011-12-22 00:37:50 +0000192 Type = ELF::R_ARM_JUMP24;
193 break;
Logan Chiencea03542012-09-01 15:06:36 +0000194 case ARM::fixup_t2_condbranch:
195 case ARM::fixup_t2_uncondbranch:
196 Type = ELF::R_ARM_THM_JUMP24;
197 break;
Rafael Espindolaa0124052011-12-22 00:37:50 +0000198 case ARM::fixup_arm_movt_hi16:
199 case ARM::fixup_arm_movt_hi16_pcrel:
200 Type = ELF::R_ARM_MOVT_PREL;
201 break;
202 case ARM::fixup_arm_movw_lo16:
203 case ARM::fixup_arm_movw_lo16_pcrel:
204 Type = ELF::R_ARM_MOVW_PREL_NC;
205 break;
206 case ARM::fixup_t2_movt_hi16:
207 case ARM::fixup_t2_movt_hi16_pcrel:
208 Type = ELF::R_ARM_THM_MOVT_PREL;
209 break;
210 case ARM::fixup_t2_movw_lo16:
211 case ARM::fixup_t2_movw_lo16_pcrel:
212 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
213 break;
214 case ARM::fixup_arm_thumb_bl:
215 case ARM::fixup_arm_thumb_blx:
Saleem Abdulrasool6e00ca82014-01-30 04:02:31 +0000216 switch (Modifier) {
217 case MCSymbolRefExpr::VK_ARM_TLSCALL:
218 Type = ELF::R_ARM_THM_TLS_CALL;
219 break;
220 default:
221 Type = ELF::R_ARM_THM_CALL;
222 break;
223 }
Rafael Espindolaa0124052011-12-22 00:37:50 +0000224 break;
225 }
226 } else {
227 switch ((unsigned)Fixup.getKind()) {
228 default: llvm_unreachable("invalid fixup kind!");
229 case FK_Data_4:
230 switch (Modifier) {
David Blaikie46a9f012012-01-20 21:51:11 +0000231 default: llvm_unreachable("Unsupported Modifier");
Logan Chien4dd14fb2012-12-12 07:14:46 +0000232 case MCSymbolRefExpr::VK_ARM_NONE:
233 Type = ELF::R_ARM_NONE;
234 break;
David Peixotto8ad70b32013-12-04 22:43:20 +0000235 case MCSymbolRefExpr::VK_GOT:
Rafael Espindolaa0124052011-12-22 00:37:50 +0000236 Type = ELF::R_ARM_GOT_BREL;
237 break;
David Peixotto8ad70b32013-12-04 22:43:20 +0000238 case MCSymbolRefExpr::VK_TLSGD:
Rafael Espindolaa0124052011-12-22 00:37:50 +0000239 Type = ELF::R_ARM_TLS_GD32;
240 break;
David Peixotto8ad70b32013-12-04 22:43:20 +0000241 case MCSymbolRefExpr::VK_TPOFF:
Rafael Espindolaa0124052011-12-22 00:37:50 +0000242 Type = ELF::R_ARM_TLS_LE32;
243 break;
David Peixotto8ad70b32013-12-04 22:43:20 +0000244 case MCSymbolRefExpr::VK_GOTTPOFF:
Rafael Espindolaa0124052011-12-22 00:37:50 +0000245 Type = ELF::R_ARM_TLS_IE32;
246 break;
247 case MCSymbolRefExpr::VK_None:
248 Type = ELF::R_ARM_ABS32;
249 break;
David Peixotto8ad70b32013-12-04 22:43:20 +0000250 case MCSymbolRefExpr::VK_GOTOFF:
Rafael Espindolaa0124052011-12-22 00:37:50 +0000251 Type = ELF::R_ARM_GOTOFF32;
252 break;
James Molloy6685c082012-01-26 09:25:43 +0000253 case MCSymbolRefExpr::VK_ARM_TARGET1:
254 Type = ELF::R_ARM_TARGET1;
255 break;
Anton Korobeynikova305ea52012-11-09 20:20:12 +0000256 case MCSymbolRefExpr::VK_ARM_TARGET2:
257 Type = ELF::R_ARM_TARGET2;
258 break;
Logan Chien4dd14fb2012-12-12 07:14:46 +0000259 case MCSymbolRefExpr::VK_ARM_PREL31:
260 Type = ELF::R_ARM_PREL31;
261 break;
Kai Nackee51c8132014-01-20 11:00:40 +0000262 case MCSymbolRefExpr::VK_ARM_TLSLDO:
263 Type = ELF::R_ARM_TLS_LDO32;
264 break;
Saleem Abdulrasool6e00ca82014-01-30 04:02:31 +0000265 case MCSymbolRefExpr::VK_ARM_TLSCALL:
266 Type = ELF::R_ARM_TLS_CALL;
267 break;
Saleem Abdulrasoola3f12bd2014-01-30 04:02:38 +0000268 case MCSymbolRefExpr::VK_ARM_TLSDESC:
269 Type = ELF::R_ARM_TLS_GOTDESC;
270 break;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000271 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
272 Type = ELF::R_ARM_TLS_DESCSEQ;
273 break;
Logan Chien4dd14fb2012-12-12 07:14:46 +0000274 }
Rafael Espindolaa0124052011-12-22 00:37:50 +0000275 break;
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:
Craig Toppere55c5562012-02-07 02:50:20 +0000283 llvm_unreachable("Unimplemented");
Rafael Espindolaa0124052011-12-22 00:37:50 +0000284 case ARM::fixup_arm_condbranch:
Jan Wen Voung7f5d79f2012-06-19 16:03:02 +0000285 case ARM::fixup_arm_uncondbranch:
Rafael Espindolaa0124052011-12-22 00:37:50 +0000286 Type = ELF::R_ARM_JUMP24;
287 break;
288 case ARM::fixup_arm_movt_hi16:
289 Type = ELF::R_ARM_MOVT_ABS;
290 break;
291 case ARM::fixup_arm_movw_lo16:
292 Type = ELF::R_ARM_MOVW_ABS_NC;
293 break;
294 case ARM::fixup_t2_movt_hi16:
295 Type = ELF::R_ARM_THM_MOVT_ABS;
296 break;
297 case ARM::fixup_t2_movw_lo16:
298 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
299 break;
300 }
301 }
302
303 return Type;
304}
305
306MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS,
307 uint8_t OSABI) {
308 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
309 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);
310}