1 //===-- PPCELFObjectWriter.cpp - PPC 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/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"
21 class PPCELFObjectWriter : public MCELFObjectTargetWriter {
23 PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI);
25 virtual ~PPCELFObjectWriter();
27 virtual unsigned getRelocTypeInner(const MCValue &Target,
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,
37 virtual void sortRelocs(const MCAssembler &Asm,
38 std::vector<ELFRelocationEntry> &Relocs);
41 class PPCELFRelocationEntry : public ELFRelocationEntry {
43 PPCELFRelocationEntry(const ELFRelocationEntry &RE);
44 bool operator<(const PPCELFRelocationEntry &RE) const {
45 return (RE.r_offset < r_offset ||
46 (RE.r_offset == r_offset && RE.Type > Type));
51 PPCELFRelocationEntry::PPCELFRelocationEntry(const ELFRelocationEntry &RE)
52 : ELFRelocationEntry(RE.r_offset, RE.Index, RE.Type, RE.Symbol,
53 RE.r_addend, *RE.Fixup) {}
55 PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
56 : MCELFObjectTargetWriter(Is64Bit, OSABI,
57 Is64Bit ? ELF::EM_PPC64 : ELF::EM_PPC,
58 /*HasRelocationAddend*/ true) {}
60 PPCELFObjectWriter::~PPCELFObjectWriter() {
63 unsigned PPCELFObjectWriter::getRelocTypeInner(const MCValue &Target,
67 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
68 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
70 // determine the type of the relocation
73 switch ((unsigned)Fixup.getKind()) {
75 llvm_unreachable("Unimplemented");
76 case PPC::fixup_ppc_br24:
77 Type = ELF::R_PPC_REL24;
79 case PPC::fixup_ppc_brcond14:
80 Type = ELF::R_PPC_REL14;
84 Type = ELF::R_PPC_REL32;
88 Type = ELF::R_PPC64_REL64;
92 switch ((unsigned)Fixup.getKind()) {
93 default: llvm_unreachable("invalid fixup kind!");
94 case PPC::fixup_ppc_br24:
95 Type = ELF::R_PPC_ADDR24;
97 case PPC::fixup_ppc_brcond14:
98 Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
100 case PPC::fixup_ppc_ha16:
102 default: llvm_unreachable("Unsupported Modifier");
103 case MCSymbolRefExpr::VK_PPC_TPREL16_HA:
104 Type = ELF::R_PPC_TPREL16_HA;
106 case MCSymbolRefExpr::VK_PPC_DTPREL16_HA:
107 Type = ELF::R_PPC64_DTPREL16_HA;
109 case MCSymbolRefExpr::VK_PPC_GAS_HA16:
110 case MCSymbolRefExpr::VK_PPC_DARWIN_HA16:
111 Type = ELF::R_PPC_ADDR16_HA;
113 case MCSymbolRefExpr::VK_PPC_TOC16_HA:
114 Type = ELF::R_PPC64_TOC16_HA;
116 case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_HA:
117 Type = ELF::R_PPC64_GOT_TPREL16_HA;
119 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_HA:
120 Type = ELF::R_PPC64_GOT_TLSGD16_HA;
122 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_HA:
123 Type = ELF::R_PPC64_GOT_TLSLD16_HA;
127 case PPC::fixup_ppc_lo16:
129 default: llvm_unreachable("Unsupported Modifier");
130 case MCSymbolRefExpr::VK_PPC_TPREL16_LO:
131 Type = ELF::R_PPC_TPREL16_LO;
133 case MCSymbolRefExpr::VK_PPC_DTPREL16_LO:
134 Type = ELF::R_PPC64_DTPREL16_LO;
136 case MCSymbolRefExpr::VK_None:
137 Type = ELF::R_PPC_ADDR16;
139 case MCSymbolRefExpr::VK_PPC_GAS_LO16:
140 case MCSymbolRefExpr::VK_PPC_DARWIN_LO16:
141 Type = ELF::R_PPC_ADDR16_LO;
143 case MCSymbolRefExpr::VK_PPC_TOC_ENTRY:
144 Type = ELF::R_PPC64_TOC16;
146 case MCSymbolRefExpr::VK_PPC_TOC16_LO:
147 Type = ELF::R_PPC64_TOC16_LO;
149 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_LO:
150 Type = ELF::R_PPC64_GOT_TLSGD16_LO;
152 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO:
153 Type = ELF::R_PPC64_GOT_TLSLD16_LO;
157 case PPC::fixup_ppc_lo16_ds:
159 default: llvm_unreachable("Unsupported Modifier");
160 case MCSymbolRefExpr::VK_None:
161 Type = ELF::R_PPC64_ADDR16_DS;
163 case MCSymbolRefExpr::VK_PPC_GAS_LO16:
164 case MCSymbolRefExpr::VK_PPC_DARWIN_LO16:
165 Type = ELF::R_PPC64_ADDR16_LO_DS;
167 case MCSymbolRefExpr::VK_PPC_TOC_ENTRY:
168 Type = ELF::R_PPC64_TOC16_DS;
170 case MCSymbolRefExpr::VK_PPC_TOC16_LO:
171 Type = ELF::R_PPC64_TOC16_LO_DS;
173 case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_LO:
174 Type = ELF::R_PPC64_GOT_TPREL16_LO_DS;
178 case PPC::fixup_ppc_tlsreg:
179 Type = ELF::R_PPC64_TLS;
181 case PPC::fixup_ppc_nofixup:
183 default: llvm_unreachable("Unsupported Modifier");
184 case MCSymbolRefExpr::VK_PPC_TLSGD:
185 Type = ELF::R_PPC64_TLSGD;
187 case MCSymbolRefExpr::VK_PPC_TLSLD:
188 Type = ELF::R_PPC64_TLSLD;
194 default: llvm_unreachable("Unsupported Modifier");
195 case MCSymbolRefExpr::VK_PPC_TOC:
196 Type = ELF::R_PPC64_TOC;
198 case MCSymbolRefExpr::VK_None:
199 Type = ELF::R_PPC64_ADDR64;
204 Type = ELF::R_PPC_ADDR32;
207 Type = ELF::R_PPC_ADDR16;
214 unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
215 const MCFixup &Fixup,
217 bool IsRelocWithSymbol,
218 int64_t Addend) const {
219 return getRelocTypeInner(Target, Fixup, IsPCRel);
222 const MCSymbol *PPCELFObjectWriter::undefinedExplicitRelSym(const MCValue &Target,
223 const MCFixup &Fixup,
224 bool IsPCRel) const {
225 assert(Target.getSymA() && "SymA cannot be 0");
226 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
228 unsigned RelocType = getRelocTypeInner(Target, Fixup, IsPCRel);
230 // The .odp creation emits a relocation against the symbol ".TOC." which
231 // create a R_PPC64_TOC relocation. However the relocation symbol name
232 // in final object creation should be NULL, since the symbol does not
233 // really exist, it is just the reference to TOC base for the current
235 bool EmitThisSym = RelocType != ELF::R_PPC64_TOC;
237 if (EmitThisSym && !Symbol.isTemporary())
242 // The standard sorter only sorts on the r_offset field, but PowerPC can
243 // have multiple relocations at the same offset. Sort secondarily on the
244 // relocation type to avoid nondeterminism.
245 void PPCELFObjectWriter::sortRelocs(const MCAssembler &Asm,
246 std::vector<ELFRelocationEntry> &Relocs) {
248 // Copy to a temporary vector of relocation entries having a different
250 std::vector<PPCELFRelocationEntry> TmpRelocs;
252 for (std::vector<ELFRelocationEntry>::iterator R = Relocs.begin();
253 R != Relocs.end(); ++R) {
254 TmpRelocs.push_back(PPCELFRelocationEntry(*R));
257 // Sort in place by ascending r_offset and descending r_type.
258 array_pod_sort(TmpRelocs.begin(), TmpRelocs.end());
260 // Copy back to the original vector.
262 for (std::vector<PPCELFRelocationEntry>::iterator R = TmpRelocs.begin();
263 R != TmpRelocs.end(); ++R, ++I) {
264 Relocs[I] = ELFRelocationEntry(R->r_offset, R->Index, R->Type,
265 R->Symbol, R->r_addend, *R->Fixup);
270 MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS,
273 MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI);
274 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false);