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