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/PPCFixupKinds.h"
11 #include "MCTargetDesc/PPCMCTargetDesc.h"
12 #include "llvm/MC/MCELFObjectWriter.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCValue.h"
20 class PPCELFObjectWriter : public MCELFObjectTargetWriter {
22 PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI);
24 virtual ~PPCELFObjectWriter();
26 virtual unsigned getRelocTypeInner(const MCValue &Target,
29 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
30 bool IsPCRel, bool IsRelocWithSymbol,
31 int64_t Addend) const;
32 virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
35 virtual void adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset);
39 PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
40 : MCELFObjectTargetWriter(Is64Bit, OSABI,
41 Is64Bit ? ELF::EM_PPC64 : ELF::EM_PPC,
42 /*HasRelocationAddend*/ true) {}
44 PPCELFObjectWriter::~PPCELFObjectWriter() {
47 unsigned PPCELFObjectWriter::getRelocTypeInner(const MCValue &Target,
51 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
52 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
54 // determine the type of the relocation
57 switch ((unsigned)Fixup.getKind()) {
59 llvm_unreachable("Unimplemented");
60 case PPC::fixup_ppc_br24:
61 Type = ELF::R_PPC_REL24;
64 Type = ELF::R_PPC_REL32;
68 switch ((unsigned)Fixup.getKind()) {
69 default: llvm_unreachable("invalid fixup kind!");
70 case PPC::fixup_ppc_br24:
71 Type = ELF::R_PPC_ADDR24;
73 case PPC::fixup_ppc_brcond14:
74 Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
76 case PPC::fixup_ppc_ha16:
77 Type = ELF::R_PPC_ADDR16_HA;
79 case PPC::fixup_ppc_lo16:
80 Type = ELF::R_PPC_ADDR16_LO;
82 case PPC::fixup_ppc_lo14:
83 Type = ELF::R_PPC_ADDR14;
85 case PPC::fixup_ppc_toc:
86 Type = ELF::R_PPC64_TOC;
88 case PPC::fixup_ppc_toc16:
89 Type = ELF::R_PPC64_TOC16;
91 case PPC::fixup_ppc_toc16_ds:
92 Type = ELF::R_PPC64_TOC16_DS;
96 default: llvm_unreachable("Unsupported Modifier");
97 case MCSymbolRefExpr::VK_PPC_TOC:
98 Type = ELF::R_PPC64_TOC;
100 case MCSymbolRefExpr::VK_None:
101 Type = ELF::R_PPC64_ADDR64;
106 Type = ELF::R_PPC_ADDR32;
109 Type = ELF::R_PPC_ADDR16;
116 unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
117 const MCFixup &Fixup,
119 bool IsRelocWithSymbol,
120 int64_t Addend) const {
121 return getRelocTypeInner(Target, Fixup, IsPCRel);
124 const MCSymbol *PPCELFObjectWriter::undefinedExplicitRelSym(const MCValue &Target,
125 const MCFixup &Fixup,
126 bool IsPCRel) const {
127 assert(Target.getSymA() && "SymA cannot be 0");
128 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
130 unsigned RelocType = getRelocTypeInner(Target, Fixup, IsPCRel);
132 // The .odp creation emits a relocation against the symbol ".TOC." which
133 // create a R_PPC64_TOC relocation. However the relocation symbol name
134 // in final object creation should be NULL, since the symbol does not
135 // really exist, it is just the reference to TOC base for the current
137 bool EmitThisSym = RelocType != ELF::R_PPC64_TOC;
139 if (EmitThisSym && !Symbol.isTemporary())
144 void PPCELFObjectWriter::
145 adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) {
146 switch ((unsigned)Fixup.getKind()) {
147 case PPC::fixup_ppc_ha16:
148 case PPC::fixup_ppc_lo16:
156 MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS,
159 MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI);
160 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false);