Remove unused argument.
[oota-llvm.git] / lib / Target / ARM / MCTargetDesc / ARMELFObjectWriter.cpp
1 //===-- ARMELFObjectWriter.cpp - ARM ELF Writer ---------------------------===//
2 //
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
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"
21
22 using namespace llvm;
23
24 namespace {
25   class ARMELFObjectWriter : public MCELFObjectTargetWriter {
26     enum { DefaultEABIVersion = 0x05000000U };
27     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();
36
37     unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
38                           bool IsPCRel, bool IsRelocWithSymbol) const override;
39     const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
40                                    const MCValue &Target, const MCFragment &F,
41                                    const MCFixup &Fixup,
42                                    bool IsPCRel) const override;
43   };
44 }
45
46 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
47   : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
48                             ELF::EM_ARM,
49                             /*HasRelocationAddend*/ false) {}
50
51 ARMELFObjectWriter::~ARMELFObjectWriter() {}
52
53 // In ARM, _MergedGlobals and other most symbols get emitted directly.
54 // I.e. not as an offset to a section symbol.
55 // This code is an approximation of what ARM/gcc does.
56
57 STATISTIC(PCRelCount, "Total number of PIC Relocations");
58 STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
59
60 const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
61                                                    const MCValue &Target,
62                                                    const MCFragment &F,
63                                                    const MCFixup &Fixup,
64                                                    bool IsPCRel) const {
65   const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
66   bool EmitThisSym = false;
67
68   const MCSectionELF &Section =
69     static_cast<const MCSectionELF&>(Symbol.getSection());
70   bool InNormalSection = true;
71   unsigned RelocType = 0;
72   RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
73   assert(!Target.getSymB() ||
74          Target.getSymB()->getKind() == MCSymbolRefExpr::VK_None);
75
76   DEBUG(
77       MCSymbolRefExpr::VariantKind Kind = Fixup.getAccessVariant();
78       dbgs() << "considering symbol "
79         << Section.getSectionName() << "/"
80         << Symbol.getName() << "/"
81         << " Rel:" << (unsigned)RelocType
82         << " Kind: " << (int)Kind
83         << " Tmp:"
84         << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
85         << Symbol.isVariable() << "/" << Symbol.isTemporary()
86         << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
87
88   if (IsPCRel) { ++PCRelCount;
89     switch (RelocType) {
90     default:
91       // Most relocation types are emitted as explicit symbols
92       InNormalSection =
93         StringSwitch<bool>(Section.getSectionName())
94         .Case(".data.rel.ro.local", false)
95         .Case(".data.rel", false)
96         .Case(".bss", false)
97         .Default(true);
98       EmitThisSym = true;
99       break;
100     case ELF::R_ARM_ABS32:
101       // But things get strange with R_ARM_ABS32
102       // In this case, most things that go in .rodata show up
103       // as section relative relocations
104       InNormalSection =
105         StringSwitch<bool>(Section.getSectionName())
106         .Case(".data.rel.ro.local", false)
107         .Case(".data.rel", false)
108         .Case(".rodata", false)
109         .Case(".bss", false)
110         .Default(true);
111       EmitThisSym = false;
112       break;
113     }
114   } else {
115     NonPCRelCount++;
116     InNormalSection =
117       StringSwitch<bool>(Section.getSectionName())
118       .Case(".data.rel.ro.local", false)
119       .Case(".rodata", false)
120       .Case(".data.rel", false)
121       .Case(".bss", false)
122       .Default(true);
123
124     switch (RelocType) {
125     default: EmitThisSym = true; break;
126     case ELF::R_ARM_ABS32: EmitThisSym = false; break;
127     case ELF::R_ARM_PREL31: EmitThisSym = false; break;
128     }
129   }
130
131   if (EmitThisSym)
132     return &Symbol;
133   if (! Symbol.isTemporary() && InNormalSection) {
134     return &Symbol;
135   }
136   return NULL;
137 }
138
139 // Need to examine the Fixup when determining whether to 
140 // emit the relocation as an explicit symbol or as a section relative
141 // offset
142 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
143                                           const MCFixup &Fixup,
144                                           bool IsPCRel,
145                                           bool IsRelocWithSymbol) const {
146   return GetRelocTypeInner(Target, Fixup, IsPCRel);
147 }
148
149 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
150                                                const MCFixup &Fixup,
151                                                bool IsPCRel) const  {
152   MCSymbolRefExpr::VariantKind Modifier = Fixup.getAccessVariant();
153
154   unsigned Type = 0;
155   if (IsPCRel) {
156     switch ((unsigned)Fixup.getKind()) {
157     default: llvm_unreachable("Unimplemented");
158     case FK_Data_4:
159       switch (Modifier) {
160       default: llvm_unreachable("Unsupported Modifier");
161       case MCSymbolRefExpr::VK_None:
162         Type = ELF::R_ARM_REL32;
163         break;
164       case MCSymbolRefExpr::VK_TLSGD:
165         llvm_unreachable("unimplemented");
166       case MCSymbolRefExpr::VK_GOTTPOFF:
167         Type = ELF::R_ARM_TLS_IE32;
168         break;
169       }
170       break;
171     case ARM::fixup_arm_blx:
172     case ARM::fixup_arm_uncondbl:
173       switch (Modifier) {
174       case MCSymbolRefExpr::VK_PLT:
175         Type = ELF::R_ARM_PLT32;
176         break;
177       case MCSymbolRefExpr::VK_ARM_TLSCALL:
178         Type = ELF::R_ARM_TLS_CALL;
179         break;
180       default:
181         Type = ELF::R_ARM_CALL;
182         break;
183       }
184       break;
185     case ARM::fixup_arm_condbl:
186     case ARM::fixup_arm_condbranch:
187     case ARM::fixup_arm_uncondbranch:
188       Type = ELF::R_ARM_JUMP24;
189       break;
190     case ARM::fixup_t2_condbranch:
191     case ARM::fixup_t2_uncondbranch:
192       Type = ELF::R_ARM_THM_JUMP24;
193       break;
194     case ARM::fixup_arm_movt_hi16:
195     case ARM::fixup_arm_movt_hi16_pcrel:
196       Type = ELF::R_ARM_MOVT_PREL;
197       break;
198     case ARM::fixup_arm_movw_lo16:
199     case ARM::fixup_arm_movw_lo16_pcrel:
200       Type = ELF::R_ARM_MOVW_PREL_NC;
201       break;
202     case ARM::fixup_t2_movt_hi16:
203     case ARM::fixup_t2_movt_hi16_pcrel:
204       Type = ELF::R_ARM_THM_MOVT_PREL;
205       break;
206     case ARM::fixup_t2_movw_lo16:
207     case ARM::fixup_t2_movw_lo16_pcrel:
208       Type = ELF::R_ARM_THM_MOVW_PREL_NC;
209       break;
210     case ARM::fixup_arm_thumb_bl:
211     case ARM::fixup_arm_thumb_blx:
212       switch (Modifier) {
213       case MCSymbolRefExpr::VK_ARM_TLSCALL:
214         Type = ELF::R_ARM_THM_TLS_CALL;
215         break;
216       default:
217         Type = ELF::R_ARM_THM_CALL;
218         break;
219       }
220       break;
221     }
222   } else {
223     switch ((unsigned)Fixup.getKind()) {
224     default: llvm_unreachable("invalid fixup kind!");
225     case FK_Data_4:
226       switch (Modifier) {
227       default: llvm_unreachable("Unsupported Modifier");
228       case MCSymbolRefExpr::VK_ARM_NONE:
229         Type = ELF::R_ARM_NONE;
230         break;
231       case MCSymbolRefExpr::VK_GOT:
232         Type = ELF::R_ARM_GOT_BREL;
233         break;
234       case MCSymbolRefExpr::VK_TLSGD:
235         Type = ELF::R_ARM_TLS_GD32;
236         break;
237       case MCSymbolRefExpr::VK_TPOFF:
238         Type = ELF::R_ARM_TLS_LE32;
239         break;
240       case MCSymbolRefExpr::VK_GOTTPOFF:
241         Type = ELF::R_ARM_TLS_IE32;
242         break;
243       case MCSymbolRefExpr::VK_None:
244         Type = ELF::R_ARM_ABS32;
245         break;
246       case MCSymbolRefExpr::VK_GOTOFF:
247         Type = ELF::R_ARM_GOTOFF32;
248         break;
249       case MCSymbolRefExpr::VK_ARM_TARGET1:
250         Type = ELF::R_ARM_TARGET1;
251         break;
252       case MCSymbolRefExpr::VK_ARM_TARGET2:
253         Type = ELF::R_ARM_TARGET2;
254         break;
255       case MCSymbolRefExpr::VK_ARM_PREL31:
256         Type = ELF::R_ARM_PREL31;
257         break;
258       case MCSymbolRefExpr::VK_ARM_TLSLDO:
259         Type = ELF::R_ARM_TLS_LDO32;
260         break;
261       case MCSymbolRefExpr::VK_ARM_TLSCALL:
262         Type = ELF::R_ARM_TLS_CALL;
263         break;
264       case MCSymbolRefExpr::VK_ARM_TLSDESC:
265         Type = ELF::R_ARM_TLS_GOTDESC;
266         break;
267       case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
268         Type = ELF::R_ARM_TLS_DESCSEQ;
269         break;
270       }
271       break;
272     case ARM::fixup_arm_ldst_pcrel_12:
273     case ARM::fixup_arm_pcrel_10:
274     case ARM::fixup_arm_adr_pcrel_12:
275     case ARM::fixup_arm_thumb_bl:
276     case ARM::fixup_arm_thumb_cb:
277     case ARM::fixup_arm_thumb_cp:
278     case ARM::fixup_arm_thumb_br:
279       llvm_unreachable("Unimplemented");
280     case ARM::fixup_arm_condbranch:
281     case ARM::fixup_arm_uncondbranch:
282       Type = ELF::R_ARM_JUMP24;
283       break;
284     case ARM::fixup_arm_movt_hi16:
285       Type = ELF::R_ARM_MOVT_ABS;
286       break;
287     case ARM::fixup_arm_movw_lo16:
288       Type = ELF::R_ARM_MOVW_ABS_NC;
289       break;
290     case ARM::fixup_t2_movt_hi16:
291       Type = ELF::R_ARM_THM_MOVT_ABS;
292       break;
293     case ARM::fixup_t2_movw_lo16:
294       Type = ELF::R_ARM_THM_MOVW_ABS_NC;
295       break;
296     }
297   }
298
299   return Type;
300 }
301
302 MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS,
303                                                uint8_t OSABI) {
304   MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
305   return createELFObjectWriter(MOTW, OS,  /*IsLittleEndian=*/true);
306 }