623507a95897f48ac814a3ef9387f38719d7c881
[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 "ELFCodeEmitter.h"
13 #include "llvm/Constants.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Function.h"
16 #include "llvm/CodeGen/BinaryObject.h"
17 #include "llvm/CodeGen/MachineConstantPool.h"
18 #include "llvm/CodeGen/MachineJumpTableInfo.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Support/Debug.h"
22
23 //===----------------------------------------------------------------------===//
24 //                       ELFCodeEmitter Implementation
25 //===----------------------------------------------------------------------===//
26
27 namespace llvm {
28
29 /// startFunction - This callback is invoked when a new machine function is
30 /// about to be emitted.
31 void ELFCodeEmitter::startFunction(MachineFunction &MF) {
32   // Get the ELF Section that this function belongs in.
33   ES = &EW.getTextSection();
34
35   DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
36
37   // FIXME: better memory management, this will be replaced by BinaryObjects
38   BinaryData &BD = ES->getData();
39   BD.reserve(4096);
40   BufferBegin = &BD[0];
41   BufferEnd = BufferBegin + BD.capacity();
42
43   // Get the function alignment in bytes
44   unsigned Align = (1 << MF.getAlignment());
45
46   // Align the section size with the function alignment, so the function can
47   // start in a aligned offset, also update the section alignment if needed.
48   if (ES->Align < Align) ES->Align = Align;
49   ES->Size = (ES->Size + (Align-1)) & (-Align);
50
51   // Snaity check on allocated space for text section
52   assert( ES->Size < 4096 && "no more space in TextSection" );
53
54   // FIXME: Using ES->Size directly here instead of calculating it from the
55   // output buffer size (impossible because the code emitter deals only in raw
56   // bytes) forces us to manually synchronize size and write padding zero bytes
57   // to the output buffer for all non-text sections.  For text sections, we do
58   // not synchonize the output buffer, and we just blow up if anyone tries to
59   // write non-code to it.  An assert should probably be added to
60   // AddSymbolToSection to prevent calling it on the text section.
61   CurBufferPtr = BufferBegin + ES->Size;
62
63   // Record function start address relative to BufferBegin
64   FnStartPtr = CurBufferPtr;
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   ELFSym FnSym(MF.getFunction());
72
73   // Update Section Size
74   ES->Size = CurBufferPtr - BufferBegin;
75
76   // Set the symbol type as a function
77   FnSym.setType(ELFSym::STT_FUNC);
78   FnSym.SectionIdx = ES->SectionIdx;
79   FnSym.Size = CurBufferPtr-FnStartPtr;
80
81   // Offset from start of Section
82   FnSym.Value = FnStartPtr-BufferBegin;
83
84   // Figure out the binding (linkage) of the symbol.
85   switch (MF.getFunction()->getLinkage()) {
86   default:
87     // appending linkage is illegal for functions.
88     assert(0 && "Unknown linkage type!");
89   case GlobalValue::ExternalLinkage:
90     FnSym.setBind(ELFSym::STB_GLOBAL);
91     EW.SymbolList.push_back(FnSym);
92     break;
93   case GlobalValue::LinkOnceAnyLinkage:
94   case GlobalValue::LinkOnceODRLinkage:
95   case GlobalValue::WeakAnyLinkage:
96   case GlobalValue::WeakODRLinkage:
97     FnSym.setBind(ELFSym::STB_WEAK);
98     EW.SymbolList.push_back(FnSym);
99     break;
100   case GlobalValue::PrivateLinkage:
101     assert (0 && "PrivateLinkage should not be in the symbol table.");
102   case GlobalValue::InternalLinkage:
103     FnSym.setBind(ELFSym::STB_LOCAL);
104     EW.SymbolList.push_front(FnSym);
105     break;
106   }
107
108   // Emit constant pool to appropriate section(s)
109   emitConstantPool(MF.getConstantPool());
110
111   // Relocations
112   // -----------
113   // If we have emitted any relocations to function-specific objects such as
114   // basic blocks, constant pools entries, or jump tables, record their
115   // addresses now so that we can rewrite them with the correct addresses
116   // later.
117   for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
118     MachineRelocation &MR = Relocations[i];
119     intptr_t Addr;
120     if (MR.isGlobalValue()) {
121       EW.PendingGlobals.insert(MR.getGlobalValue());
122     } else if (MR.isBasicBlock()) {
123       Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
124       MR.setConstantVal(ES->SectionIdx);
125       MR.setResultPointer((void*)Addr);
126     } else if (MR.isConstantPoolIndex()) {
127       Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
128       MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
129       MR.setResultPointer((void*)Addr);
130     } else {
131       assert(0 && "Unhandled relocation type");
132     }
133     ES->addRelocation(MR);
134   }
135   Relocations.clear();
136
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   const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
151   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
152     MachineConstantPoolEntry CPE = CP[i];
153
154     // Get the right ELF Section for this constant pool entry
155     std::string CstPoolName =
156       TAI->SelectSectionForMachineConst(CPE.getType())->getName();
157     ELFSection &CstPoolSection =
158       EW.getConstantPoolSection(CstPoolName, CPE.getAlignment());
159
160     // Record the constant pool location and the section index
161     CPLocations.push_back(CstPoolSection.size());
162     CPSections.push_back(CstPoolSection.SectionIdx);
163
164     if (CPE.isMachineConstantPoolEntry())
165       assert("CPE.isMachineConstantPoolEntry not supported yet");
166
167     // Emit the constant to constant pool section
168     EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
169   }
170 }
171
172 } // end namespace llvm