PowerPC: Support PC-relative fixup_ppc_brcond14.
[oota-llvm.git] / lib / Target / PowerPC / MCTargetDesc / PPCELFObjectWriter.cpp
1 //===-- PPCELFObjectWriter.cpp - PPC 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/PPCMCTargetDesc.h"
11 #include "MCTargetDesc/PPCFixupKinds.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCValue.h"
16 #include "llvm/Support/ErrorHandling.h"
17
18 using namespace llvm;
19
20 namespace {
21   class PPCELFObjectWriter : public MCELFObjectTargetWriter {
22   public:
23     PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI);
24
25     virtual ~PPCELFObjectWriter();
26   protected:
27     virtual unsigned getRelocTypeInner(const MCValue &Target,
28                                        const MCFixup &Fixup,
29                                        bool IsPCRel) const;
30     virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
31                                   bool IsPCRel, bool IsRelocWithSymbol,
32                                   int64_t Addend) const;
33     virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
34                                                     const MCFixup &Fixup,
35                                                     bool IsPCRel) const;
36     virtual void adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset);
37
38     virtual void sortRelocs(const MCAssembler &Asm,
39                             std::vector<ELFRelocationEntry> &Relocs);
40   };
41
42   class PPCELFRelocationEntry : public ELFRelocationEntry {
43   public:
44     PPCELFRelocationEntry(const ELFRelocationEntry &RE);
45     bool operator<(const PPCELFRelocationEntry &RE) const {
46       return (RE.r_offset < r_offset ||
47               (RE.r_offset == r_offset && RE.Type > Type));
48     }
49   };
50 }
51
52 PPCELFRelocationEntry::PPCELFRelocationEntry(const ELFRelocationEntry &RE)
53   : ELFRelocationEntry(RE.r_offset, RE.Index, RE.Type, RE.Symbol,
54                        RE.r_addend, *RE.Fixup) {}
55
56 PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
57   : MCELFObjectTargetWriter(Is64Bit, OSABI,
58                             Is64Bit ?  ELF::EM_PPC64 : ELF::EM_PPC,
59                             /*HasRelocationAddend*/ true) {}
60
61 PPCELFObjectWriter::~PPCELFObjectWriter() {
62 }
63
64 unsigned PPCELFObjectWriter::getRelocTypeInner(const MCValue &Target,
65                                                const MCFixup &Fixup,
66                                                bool IsPCRel) const
67 {
68   MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
69     MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
70
71   // determine the type of the relocation
72   unsigned Type;
73   if (IsPCRel) {
74     switch ((unsigned)Fixup.getKind()) {
75     default:
76       llvm_unreachable("Unimplemented");
77     case PPC::fixup_ppc_br24:
78       Type = ELF::R_PPC_REL24;
79       break;
80     case PPC::fixup_ppc_brcond14:
81       Type = ELF::R_PPC_REL14;
82       break;
83     case FK_Data_4:
84     case FK_PCRel_4:
85       Type = ELF::R_PPC_REL32;
86       break;
87     case FK_Data_8:
88     case FK_PCRel_8:
89       Type = ELF::R_PPC64_REL64;
90       break;
91     }
92   } else {
93     switch ((unsigned)Fixup.getKind()) {
94       default: llvm_unreachable("invalid fixup kind!");
95     case PPC::fixup_ppc_br24:
96       Type = ELF::R_PPC_ADDR24;
97       break;
98     case PPC::fixup_ppc_brcond14:
99       Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
100       break;
101     case PPC::fixup_ppc_ha16:
102       switch (Modifier) {
103       default: llvm_unreachable("Unsupported Modifier");
104       case MCSymbolRefExpr::VK_PPC_TPREL16_HA:
105         Type = ELF::R_PPC_TPREL16_HA;
106         break;
107       case MCSymbolRefExpr::VK_PPC_DTPREL16_HA:
108         Type = ELF::R_PPC64_DTPREL16_HA;
109         break;
110       case MCSymbolRefExpr::VK_None:
111         Type = ELF::R_PPC_ADDR16_HA;
112         break;
113       case MCSymbolRefExpr::VK_PPC_TOC16_HA:
114         Type = ELF::R_PPC64_TOC16_HA;
115         break;
116       case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_HA:
117         Type = ELF::R_PPC64_GOT_TPREL16_HA;
118         break;
119       case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_HA:
120         Type = ELF::R_PPC64_GOT_TLSGD16_HA;
121         break;
122       case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_HA:
123         Type = ELF::R_PPC64_GOT_TLSLD16_HA;
124         break;
125       }
126       break;
127     case PPC::fixup_ppc_lo16:
128       switch (Modifier) {
129       default: llvm_unreachable("Unsupported Modifier");
130       case MCSymbolRefExpr::VK_PPC_TPREL16_LO:
131         Type = ELF::R_PPC_TPREL16_LO;
132         break;
133       case MCSymbolRefExpr::VK_PPC_DTPREL16_LO:
134         Type = ELF::R_PPC64_DTPREL16_LO;
135         break;
136       case MCSymbolRefExpr::VK_None:
137         Type = ELF::R_PPC_ADDR16_LO;
138         break;
139       case MCSymbolRefExpr::VK_PPC_TOC_ENTRY:
140         Type = ELF::R_PPC64_TOC16;
141         break;
142       case MCSymbolRefExpr::VK_PPC_TOC16_LO:
143         Type = ELF::R_PPC64_TOC16_LO;
144         break;
145       case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_LO:
146         Type = ELF::R_PPC64_GOT_TLSGD16_LO;
147         break;
148       case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO:
149         Type = ELF::R_PPC64_GOT_TLSLD16_LO;
150         break;
151       }
152       break;
153     case PPC::fixup_ppc_lo16_ds:
154       switch (Modifier) {
155       default: llvm_unreachable("Unsupported Modifier");
156       case MCSymbolRefExpr::VK_None:
157         Type = ELF::R_PPC64_ADDR16_DS;
158         break;
159       case MCSymbolRefExpr::VK_PPC_TOC_ENTRY:
160         Type = ELF::R_PPC64_TOC16_DS;
161         break;
162       case MCSymbolRefExpr::VK_PPC_TOC16_LO:
163         Type = ELF::R_PPC64_TOC16_LO_DS;
164         break;
165       case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_LO:
166         Type = ELF::R_PPC64_GOT_TPREL16_LO_DS;
167         break;
168       }
169       break;
170     case PPC::fixup_ppc_tlsreg:
171       Type = ELF::R_PPC64_TLS;
172       break;
173     case PPC::fixup_ppc_nofixup:
174       switch (Modifier) {
175       default: llvm_unreachable("Unsupported Modifier");
176       case MCSymbolRefExpr::VK_PPC_TLSGD:
177         Type = ELF::R_PPC64_TLSGD;
178         break;
179       case MCSymbolRefExpr::VK_PPC_TLSLD:
180         Type = ELF::R_PPC64_TLSLD;
181         break;
182       }
183       break;
184     case FK_Data_8:
185       switch (Modifier) {
186       default: llvm_unreachable("Unsupported Modifier");
187       case MCSymbolRefExpr::VK_PPC_TOC:
188         Type = ELF::R_PPC64_TOC;
189         break;
190       case MCSymbolRefExpr::VK_None:
191         Type = ELF::R_PPC64_ADDR64;
192         break;
193       }
194       break;
195     case FK_Data_4:
196       Type = ELF::R_PPC_ADDR32;
197       break;
198     case FK_Data_2:
199       Type = ELF::R_PPC_ADDR16;
200       break;
201     }
202   }
203   return Type;
204 }
205
206 unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
207                                           const MCFixup &Fixup,
208                                           bool IsPCRel,
209                                           bool IsRelocWithSymbol,
210                                           int64_t Addend) const {
211   return getRelocTypeInner(Target, Fixup, IsPCRel);
212 }
213
214 const MCSymbol *PPCELFObjectWriter::undefinedExplicitRelSym(const MCValue &Target,
215                                                             const MCFixup &Fixup,
216                                                             bool IsPCRel) const {
217   assert(Target.getSymA() && "SymA cannot be 0");
218   const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
219
220   unsigned RelocType = getRelocTypeInner(Target, Fixup, IsPCRel);
221
222   // The .odp creation emits a relocation against the symbol ".TOC." which
223   // create a R_PPC64_TOC relocation. However the relocation symbol name
224   // in final object creation should be NULL, since the symbol does not
225   // really exist, it is just the reference to TOC base for the current
226   // object file.
227   bool EmitThisSym = RelocType != ELF::R_PPC64_TOC;
228
229   if (EmitThisSym && !Symbol.isTemporary())
230     return &Symbol;
231   return NULL;
232 }
233
234 void PPCELFObjectWriter::
235 adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) {
236   switch ((unsigned)Fixup.getKind()) {
237     case PPC::fixup_ppc_ha16:
238     case PPC::fixup_ppc_lo16:
239     case PPC::fixup_ppc_lo16_ds:
240       RelocOffset += 2;
241       break;
242     default:
243       break;
244   }
245 }
246
247 // The standard sorter only sorts on the r_offset field, but PowerPC can
248 // have multiple relocations at the same offset.  Sort secondarily on the
249 // relocation type to avoid nondeterminism.
250 void PPCELFObjectWriter::sortRelocs(const MCAssembler &Asm,
251                                     std::vector<ELFRelocationEntry> &Relocs) {
252
253   // Copy to a temporary vector of relocation entries having a different
254   // sort function.
255   std::vector<PPCELFRelocationEntry> TmpRelocs;
256   
257   for (std::vector<ELFRelocationEntry>::iterator R = Relocs.begin();
258        R != Relocs.end(); ++R) {
259     TmpRelocs.push_back(PPCELFRelocationEntry(*R));
260   }
261
262   // Sort in place by ascending r_offset and descending r_type.
263   array_pod_sort(TmpRelocs.begin(), TmpRelocs.end());
264
265   // Copy back to the original vector.
266   unsigned I = 0;
267   for (std::vector<PPCELFRelocationEntry>::iterator R = TmpRelocs.begin();
268        R != TmpRelocs.end(); ++R, ++I) {
269     Relocs[I] = ELFRelocationEntry(R->r_offset, R->Index, R->Type,
270                                    R->Symbol, R->r_addend, *R->Fixup);
271   }
272 }
273
274
275 MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS,
276                                                bool Is64Bit,
277                                                uint8_t OSABI) {
278   MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI);
279   return createELFObjectWriter(MOTW, OS,  /*IsLittleEndian=*/false);
280 }