Initial TOC support for PowerPC64 object creation
[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/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"
16
17 using namespace llvm;
18
19 namespace {
20   class PPCELFObjectWriter : public MCELFObjectTargetWriter {
21   public:
22     PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI);
23
24     virtual ~PPCELFObjectWriter();
25   protected:
26     virtual unsigned getRelocTypeInner(const MCValue &Target,
27                                        const MCFixup &Fixup,
28                                        bool IsPCRel) const;
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,
33                                                     const MCFixup &Fixup,
34                                                     bool IsPCRel) const;
35     virtual void adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset);
36   };
37 }
38
39 PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
40   : MCELFObjectTargetWriter(Is64Bit, OSABI,
41                             Is64Bit ?  ELF::EM_PPC64 : ELF::EM_PPC,
42                             /*HasRelocationAddend*/ true) {}
43
44 PPCELFObjectWriter::~PPCELFObjectWriter() {
45 }
46
47 unsigned PPCELFObjectWriter::getRelocTypeInner(const MCValue &Target,
48                                                const MCFixup &Fixup,
49                                                bool IsPCRel) const
50 {
51   MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
52     MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
53
54   // determine the type of the relocation
55   unsigned Type;
56   if (IsPCRel) {
57     switch ((unsigned)Fixup.getKind()) {
58     default:
59       llvm_unreachable("Unimplemented");
60     case PPC::fixup_ppc_br24:
61       Type = ELF::R_PPC_REL24;
62       break;
63     case FK_PCRel_4:
64       Type = ELF::R_PPC_REL32;
65       break;
66     }
67   } else {
68     switch ((unsigned)Fixup.getKind()) {
69       default: llvm_unreachable("invalid fixup kind!");
70     case PPC::fixup_ppc_br24:
71       Type = ELF::R_PPC_ADDR24;
72       break;
73     case PPC::fixup_ppc_brcond14:
74       Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
75       break;
76     case PPC::fixup_ppc_ha16:
77       Type = ELF::R_PPC_ADDR16_HA;
78       break;
79     case PPC::fixup_ppc_lo16:
80       Type = ELF::R_PPC_ADDR16_LO;
81       break;
82     case PPC::fixup_ppc_lo14:
83       Type = ELF::R_PPC_ADDR14;
84       break;
85     case PPC::fixup_ppc_toc:
86       Type = ELF::R_PPC64_TOC;
87       break;
88     case PPC::fixup_ppc_toc16:
89       Type = ELF::R_PPC64_TOC16;
90       break;
91     case PPC::fixup_ppc_toc16_ds:
92       Type = ELF::R_PPC64_TOC16_DS;
93       break;
94     case FK_Data_8:
95       switch (Modifier) {
96       default: llvm_unreachable("Unsupported Modifier");
97       case MCSymbolRefExpr::VK_PPC_TOC:
98         Type = ELF::R_PPC64_TOC;
99         break;
100       case MCSymbolRefExpr::VK_None:
101         Type = ELF::R_PPC64_ADDR64;
102         break;
103       }
104       break;
105     case FK_Data_4:
106       Type = ELF::R_PPC_ADDR32;
107       break;
108     case FK_Data_2:
109       Type = ELF::R_PPC_ADDR16;
110       break;
111     }
112   }
113   return Type;
114 }
115
116 unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
117                                           const MCFixup &Fixup,
118                                           bool IsPCRel,
119                                           bool IsRelocWithSymbol,
120                                           int64_t Addend) const {
121   return getRelocTypeInner(Target, Fixup, IsPCRel);
122 }
123
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();
129
130   unsigned RelocType = getRelocTypeInner(Target, Fixup, IsPCRel);
131
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
136   // object file.
137   bool EmitThisSym = RelocType != ELF::R_PPC64_TOC;
138
139   if (EmitThisSym && !Symbol.isTemporary())
140     return &Symbol;
141   return NULL;
142 }
143
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:
149       RelocOffset += 2;
150       break;
151     default:
152       break;
153   }
154 }
155
156 MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS,
157                                                bool Is64Bit,
158                                                uint8_t OSABI) {
159   MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI);
160   return createELFObjectWriter(MOTW, OS,  /*IsLittleEndian=*/false);
161 }