Support Constant Pool Sections
[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   // Align the output buffer with function alignment, and
44   // upgrade the section alignment if required
45   unsigned Align =
46     TM.getELFWriterInfo()->getFunctionAlignment(MF.getFunction());
47   if (ES->Align < Align) ES->Align = Align;
48   ES->Size = (ES->Size + (Align-1)) & (-Align);
49
50   // Snaity check on allocated space for text section
51   assert( ES->Size < 4096 && "no more space in TextSection" );
52
53   // FIXME: Using ES->Size directly here instead of calculating it from the
54   // output buffer size (impossible because the code emitter deals only in raw
55   // bytes) forces us to manually synchronize size and write padding zero bytes
56   // to the output buffer for all non-text sections.  For text sections, we do
57   // not synchonize the output buffer, and we just blow up if anyone tries to
58   // write non-code to it.  An assert should probably be added to
59   // AddSymbolToSection to prevent calling it on the text section.
60   CurBufferPtr = BufferBegin + ES->Size;
61
62   // Record function start address relative to BufferBegin
63   FnStartPtr = CurBufferPtr;
64 }
65
66 /// finishFunction - This callback is invoked after the function is completely
67 /// finished.
68 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
69   // Add a symbol to represent the function.
70   ELFSym FnSym(MF.getFunction());
71
72   // Update Section Size
73   ES->Size = CurBufferPtr - BufferBegin;
74
75   // Set the symbol type as a function
76   FnSym.setType(ELFSym::STT_FUNC);
77   FnSym.SectionIdx = ES->SectionIdx;
78   FnSym.Size = CurBufferPtr-FnStartPtr;
79
80   // Offset from start of Section
81   FnSym.Value = FnStartPtr-BufferBegin;
82
83   // Figure out the binding (linkage) of the symbol.
84   switch (MF.getFunction()->getLinkage()) {
85   default:
86     // appending linkage is illegal for functions.
87     assert(0 && "Unknown linkage type!");
88   case GlobalValue::ExternalLinkage:
89     FnSym.setBind(ELFSym::STB_GLOBAL);
90     EW.SymbolList.push_back(FnSym);
91     break;
92   case GlobalValue::LinkOnceAnyLinkage:
93   case GlobalValue::LinkOnceODRLinkage:
94   case GlobalValue::WeakAnyLinkage:
95   case GlobalValue::WeakODRLinkage:
96     FnSym.setBind(ELFSym::STB_WEAK);
97     EW.SymbolList.push_back(FnSym);
98     break;
99   case GlobalValue::PrivateLinkage:
100     assert (0 && "PrivateLinkage should not be in the symbol table.");
101   case GlobalValue::InternalLinkage:
102     FnSym.setBind(ELFSym::STB_LOCAL);
103     EW.SymbolList.push_front(FnSym);
104     break;
105   }
106
107   // Emit constant pool to appropriate section(s)
108   emitConstantPool(MF.getConstantPool());
109
110   // Relocations
111   // -----------
112   // If we have emitted any relocations to function-specific objects such as
113   // basic blocks, constant pools entries, or jump tables, record their
114   // addresses now so that we can rewrite them with the correct addresses
115   // later.
116   for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
117     MachineRelocation &MR = Relocations[i];
118     intptr_t Addr;
119     if (MR.isGlobalValue()) {
120       EW.PendingGlobals.insert(MR.getGlobalValue());
121     } else if (MR.isBasicBlock()) {
122       Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
123       MR.setConstantVal(ES->SectionIdx);
124       MR.setResultPointer((void*)Addr);
125     } else if (MR.isConstantPoolIndex()) {
126       Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
127       MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
128       MR.setResultPointer((void*)Addr);
129     } else {
130       assert(0 && "Unhandled relocation type");
131     }
132     ES->addRelocation(MR);
133   }
134   Relocations.clear();
135
136   return false;
137 }
138
139 /// emitConstantPool - For each constant pool entry, figure out which section
140 /// the constant should live in and emit the constant
141 void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
142   const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
143   if (CP.empty()) return;
144
145   // TODO: handle PIC codegen
146   assert(TM.getRelocationModel() != Reloc::PIC_ &&
147          "PIC codegen not yet handled for elf constant pools!");
148
149   const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
150   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
151     MachineConstantPoolEntry CPE = CP[i];
152
153     // Get the right ELF Section for this constant pool entry
154     std::string CstPoolName =
155       TAI->SelectSectionForMachineConst(CPE.getType())->getName();
156     ELFSection &CstPoolSection =
157       EW.getConstantPoolSection(CstPoolName, CPE.getAlignment());
158
159     // Record the constant pool location and the section index
160     CPLocations.push_back(CstPoolSection.size());
161     CPSections.push_back(CstPoolSection.SectionIdx);
162
163     if (CPE.isMachineConstantPoolEntry())
164       assert("CPE.isMachineConstantPoolEntry not supported yet");
165
166     // Emit the constant to constant pool section
167     EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
168   }
169 }
170
171 } // end namespace llvm