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,
36 virtual void adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset);
38 virtual void sortRelocs(const MCAssembler &Asm,
39 std::vector<ELFRelocationEntry> &Relocs);
42 class PPCELFRelocationEntry : public ELFRelocationEntry {
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));
52 PPCELFRelocationEntry::PPCELFRelocationEntry(const ELFRelocationEntry &RE)
53 : ELFRelocationEntry(RE.r_offset, RE.Index, RE.Type, RE.Symbol,
54 RE.r_addend, *RE.Fixup) {}
56 PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
57 : MCELFObjectTargetWriter(Is64Bit, OSABI,
58 Is64Bit ? ELF::EM_PPC64 : ELF::EM_PPC,
59 /*HasRelocationAddend*/ true) {}
61 PPCELFObjectWriter::~PPCELFObjectWriter() {
64 unsigned PPCELFObjectWriter::getRelocTypeInner(const MCValue &Target,
68 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
69 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
71 // determine the type of the relocation
74 switch ((unsigned)Fixup.getKind()) {
76 llvm_unreachable("Unimplemented");
77 case PPC::fixup_ppc_br24:
78 Type = ELF::R_PPC_REL24;
82 Type = ELF::R_PPC_REL32;
86 Type = ELF::R_PPC64_REL64;
90 switch ((unsigned)Fixup.getKind()) {
91 default: llvm_unreachable("invalid fixup kind!");
92 case PPC::fixup_ppc_br24:
93 Type = ELF::R_PPC_ADDR24;
95 case PPC::fixup_ppc_brcond14:
96 Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
98 case PPC::fixup_ppc_ha16:
100 default: llvm_unreachable("Unsupported Modifier");
101 case MCSymbolRefExpr::VK_PPC_TPREL16_HA:
102 Type = ELF::R_PPC_TPREL16_HA;
104 case MCSymbolRefExpr::VK_PPC_DTPREL16_HA:
105 Type = ELF::R_PPC64_DTPREL16_HA;
107 case MCSymbolRefExpr::VK_None:
108 Type = ELF::R_PPC_ADDR16_HA;
110 case MCSymbolRefExpr::VK_PPC_TOC16_HA:
111 Type = ELF::R_PPC64_TOC16_HA;
113 case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_HA:
114 Type = ELF::R_PPC64_GOT_TPREL16_HA;
116 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_HA:
117 Type = ELF::R_PPC64_GOT_TLSGD16_HA;
119 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_HA:
120 Type = ELF::R_PPC64_GOT_TLSLD16_HA;
124 case PPC::fixup_ppc_lo16:
126 default: llvm_unreachable("Unsupported Modifier");
127 case MCSymbolRefExpr::VK_PPC_TPREL16_LO:
128 Type = ELF::R_PPC_TPREL16_LO;
130 case MCSymbolRefExpr::VK_PPC_DTPREL16_LO:
131 Type = ELF::R_PPC64_DTPREL16_LO;
133 case MCSymbolRefExpr::VK_None:
134 Type = ELF::R_PPC_ADDR16_LO;
136 case MCSymbolRefExpr::VK_PPC_TOC_ENTRY:
137 Type = ELF::R_PPC64_TOC16;
139 case MCSymbolRefExpr::VK_PPC_TOC16_LO:
140 Type = ELF::R_PPC64_TOC16_LO;
142 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_LO:
143 Type = ELF::R_PPC64_GOT_TLSGD16_LO;
145 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO:
146 Type = ELF::R_PPC64_GOT_TLSLD16_LO;
150 case PPC::fixup_ppc_lo16_ds:
152 default: llvm_unreachable("Unsupported Modifier");
153 case MCSymbolRefExpr::VK_None:
154 Type = ELF::R_PPC64_ADDR16_DS;
156 case MCSymbolRefExpr::VK_PPC_TOC_ENTRY:
157 Type = ELF::R_PPC64_TOC16_DS;
159 case MCSymbolRefExpr::VK_PPC_TOC16_LO:
160 Type = ELF::R_PPC64_TOC16_LO_DS;
162 case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_LO:
163 Type = ELF::R_PPC64_GOT_TPREL16_LO_DS;
167 case PPC::fixup_ppc_tlsreg:
168 Type = ELF::R_PPC64_TLS;
170 case PPC::fixup_ppc_nofixup:
172 default: llvm_unreachable("Unsupported Modifier");
173 case MCSymbolRefExpr::VK_PPC_TLSGD:
174 Type = ELF::R_PPC64_TLSGD;
176 case MCSymbolRefExpr::VK_PPC_TLSLD:
177 Type = ELF::R_PPC64_TLSLD;
183 default: llvm_unreachable("Unsupported Modifier");
184 case MCSymbolRefExpr::VK_PPC_TOC:
185 Type = ELF::R_PPC64_TOC;
187 case MCSymbolRefExpr::VK_None:
188 Type = ELF::R_PPC64_ADDR64;
193 Type = ELF::R_PPC_ADDR32;
196 Type = ELF::R_PPC_ADDR16;
203 unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
204 const MCFixup &Fixup,
206 bool IsRelocWithSymbol,
207 int64_t Addend) const {
208 return getRelocTypeInner(Target, Fixup, IsPCRel);
211 const MCSymbol *PPCELFObjectWriter::undefinedExplicitRelSym(const MCValue &Target,
212 const MCFixup &Fixup,
213 bool IsPCRel) const {
214 assert(Target.getSymA() && "SymA cannot be 0");
215 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
217 unsigned RelocType = getRelocTypeInner(Target, Fixup, IsPCRel);
219 // The .odp creation emits a relocation against the symbol ".TOC." which
220 // create a R_PPC64_TOC relocation. However the relocation symbol name
221 // in final object creation should be NULL, since the symbol does not
222 // really exist, it is just the reference to TOC base for the current
224 bool EmitThisSym = RelocType != ELF::R_PPC64_TOC;
226 if (EmitThisSym && !Symbol.isTemporary())
231 void PPCELFObjectWriter::
232 adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) {
233 switch ((unsigned)Fixup.getKind()) {
234 case PPC::fixup_ppc_ha16:
235 case PPC::fixup_ppc_lo16:
236 case PPC::fixup_ppc_lo16_ds:
244 // The standard sorter only sorts on the r_offset field, but PowerPC can
245 // have multiple relocations at the same offset. Sort secondarily on the
246 // relocation type to avoid nondeterminism.
247 void PPCELFObjectWriter::sortRelocs(const MCAssembler &Asm,
248 std::vector<ELFRelocationEntry> &Relocs) {
250 // Copy to a temporary vector of relocation entries having a different
252 std::vector<PPCELFRelocationEntry> TmpRelocs;
254 for (std::vector<ELFRelocationEntry>::iterator R = Relocs.begin();
255 R != Relocs.end(); ++R) {
256 TmpRelocs.push_back(PPCELFRelocationEntry(*R));
259 // Sort in place by ascending r_offset and descending r_type.
260 array_pod_sort(TmpRelocs.begin(), TmpRelocs.end());
262 // Copy back to the original vector.
264 for (std::vector<PPCELFRelocationEntry>::iterator R = TmpRelocs.begin();
265 R != TmpRelocs.end(); ++R, ++I) {
266 Relocs[I] = ELFRelocationEntry(R->r_offset, R->Index, R->Type,
267 R->Symbol, R->r_addend, *R->Fixup);
272 MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS,
275 MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI);
276 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false);