78f0dae51c0a9f9fa95836a4f1b21b1425fb4786
[oota-llvm.git] / lib / CodeGen / ELFCodeEmitter.cpp
1 //===-- lib/CodeGen/ELFCodeEmitter.cpp ------------------------------------===//
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 #define DEBUG_TYPE "elfce"
11
12 #include "ELF.h"
13 #include "ELFWriter.h"
14 #include "ELFCodeEmitter.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/BinaryObject.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineJumpTableInfo.h"
21 #include "llvm/CodeGen/MachineRelocation.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetAsmInfo.h"
25 #include "llvm/Support/Debug.h"
26
27 //===----------------------------------------------------------------------===//
28 //                       ELFCodeEmitter Implementation
29 //===----------------------------------------------------------------------===//
30
31 namespace llvm {
32
33 /// startFunction - This callback is invoked when a new machine function is
34 /// about to be emitted.
35 void ELFCodeEmitter::startFunction(MachineFunction &MF) {
36   DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
37
38   // Get the ELF Section that this function belongs in.
39   ES = &EW.getTextSection();
40
41   // Set the desired binary object to be used by the code emitters
42   setBinaryObject(ES);
43
44   // Get the function alignment in bytes
45   unsigned Align = (1 << MF.getAlignment());
46
47   // The function must start on its required alignment
48   ES->emitAlignment(Align);
49
50   // Update the section alignment if needed.
51   if (ES->Align < Align) ES->Align = Align;
52
53   // Record the function start offset
54   FnStartOff = ES->getCurrentPCOffset();
55 }
56
57 /// finishFunction - This callback is invoked after the function is completely
58 /// finished.
59 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
60   // Add a symbol to represent the function.
61   const Function *F = MF.getFunction();
62   ELFSym FnSym(F);
63   FnSym.setType(ELFSym::STT_FUNC);
64   FnSym.setBind(EW.getGlobalELFLinkage(F));
65   FnSym.setVisibility(EW.getGlobalELFVisibility(F));
66   FnSym.SectionIdx = ES->SectionIdx;
67   FnSym.Size = ES->getCurrentPCOffset()-FnStartOff;
68
69   // Offset from start of Section
70   FnSym.Value = FnStartOff;
71
72   // Locals should go on the symbol list front
73   if (!F->hasPrivateLinkage()) {
74     if (FnSym.getBind() == ELFSym::STB_LOCAL)
75       EW.SymbolList.push_front(FnSym);
76     else
77       EW.SymbolList.push_back(FnSym);
78   }
79
80   // Emit constant pool to appropriate section(s)
81   emitConstantPool(MF.getConstantPool());
82
83   // Emit jump tables to appropriate section
84   emitJumpTables(MF.getJumpTableInfo());
85
86   // Relocations
87   // -----------
88   // If we have emitted any relocations to function-specific objects such as
89   // basic blocks, constant pools entries, or jump tables, record their
90   // addresses now so that we can rewrite them with the correct addresses
91   // later.
92   for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
93     MachineRelocation &MR = Relocations[i];
94     intptr_t Addr;
95     if (MR.isGlobalValue()) {
96       EW.PendingGlobals.insert(MR.getGlobalValue());
97     } else if (MR.isBasicBlock()) {
98       Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
99       MR.setConstantVal(ES->SectionIdx);
100       MR.setResultPointer((void*)Addr);
101     } else if (MR.isConstantPoolIndex()) {
102       Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
103       MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
104       MR.setResultPointer((void*)Addr);
105     } else if (MR.isJumpTableIndex()) {
106       Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
107       MR.setResultPointer((void*)Addr);
108       MR.setConstantVal(JumpTableSectionIdx);
109     } else {
110       assert(0 && "Unhandled relocation type");
111     }
112     ES->addRelocation(MR);
113   }
114
115   // Clear per-function data structures.
116   Relocations.clear();
117   CPLocations.clear();
118   CPSections.clear();
119   JTLocations.clear();
120   MBBLocations.clear();
121   return false;
122 }
123
124 /// emitConstantPool - For each constant pool entry, figure out which section
125 /// the constant should live in and emit the constant
126 void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
127   const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
128   if (CP.empty()) return;
129
130   // TODO: handle PIC codegen
131   assert(TM.getRelocationModel() != Reloc::PIC_ &&
132          "PIC codegen not yet handled for elf constant pools!");
133
134   const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
135   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
136     MachineConstantPoolEntry CPE = CP[i];
137
138     // Get the right ELF Section for this constant pool entry
139     std::string CstPoolName =
140       TAI->SelectSectionForMachineConst(CPE.getType())->getName();
141     ELFSection &CstPoolSection =
142       EW.getConstantPoolSection(CstPoolName, CPE.getAlignment());
143
144     // Record the constant pool location and the section index
145     CPLocations.push_back(CstPoolSection.size());
146     CPSections.push_back(CstPoolSection.SectionIdx);
147
148     if (CPE.isMachineConstantPoolEntry())
149       assert("CPE.isMachineConstantPoolEntry not supported yet");
150
151     // Emit the constant to constant pool section
152     EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
153   }
154 }
155
156 /// emitJumpTables - Emit all the jump tables for a given jump table info
157 /// record to the appropriate section.
158 void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
159   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
160   if (JT.empty()) return;
161
162   // FIXME: handle PIC codegen
163   assert(TM.getRelocationModel() != Reloc::PIC_ &&
164          "PIC codegen not yet handled for elf jump tables!");
165
166   const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
167
168   // Get the ELF Section to emit the jump table
169   unsigned Align = TM.getTargetData()->getPointerABIAlignment();
170   std::string JTName(TAI->getJumpTableDataSection());
171   ELFSection &JTSection = EW.getJumpTableSection(JTName, Align);
172   JumpTableSectionIdx = JTSection.SectionIdx;
173
174   // Entries in the JT Section are relocated against the text section
175   ELFSection &TextSection = EW.getTextSection();
176
177   // For each JT, record its offset from the start of the section
178   for (unsigned i = 0, e = JT.size(); i != e; ++i) {
179     const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
180
181     DOUT << "JTSection.size(): " << JTSection.size() << "\n";
182     DOUT << "JTLocations.size: " << JTLocations.size() << "\n";
183
184     // Record JT 'i' offset in the JT section
185     JTLocations.push_back(JTSection.size());
186
187     // Each MBB entry in the Jump table section has a relocation entry
188     // against the current text section.
189     for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
190       MachineRelocation MR =
191         MachineRelocation::getBB(JTSection.size(),
192                                  MachineRelocation::VANILLA,
193                                  MBBs[mi]);
194
195       // Offset of JT 'i' in JT section
196       MR.setResultPointer((void*)getMachineBasicBlockAddress(MBBs[mi]));
197       MR.setConstantVal(TextSection.SectionIdx);
198
199       // Add the relocation to the Jump Table section
200       JTSection.addRelocation(MR);
201
202       // Output placeholder for MBB in the JT section
203       JTSection.emitWord(0);
204     }
205   }
206 }
207
208 } // end namespace llvm