Add more methods to gather target specific elf stuff
[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/TargetMachine.h"
20 #include "llvm/Support/Debug.h"
21
22 //===----------------------------------------------------------------------===//
23 //                       ELFCodeEmitter Implementation
24 //===----------------------------------------------------------------------===//
25
26 namespace llvm {
27
28 /// startFunction - This callback is invoked when a new machine function is
29 /// about to be emitted.
30 void ELFCodeEmitter::startFunction(MachineFunction &MF) {
31   // Get the ELF Section that this function belongs in.
32   ES = &EW.getTextSection();
33
34   DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
35
36   // FIXME: better memory management, this will be replaced by BinaryObjects
37   BinaryData &BD = ES->getData();
38   BD.reserve(4096);
39   BufferBegin = &BD[0];
40   BufferEnd = BufferBegin + BD.capacity();
41
42   // Align the output buffer with function alignment, and
43   // upgrade the section alignment if required
44   unsigned Align =
45     TM.getELFWriterInfo()->getFunctionAlignment(MF.getFunction());
46   if (ES->Align < Align) ES->Align = Align;
47   ES->Size = (ES->Size + (Align-1)) & (-Align);
48
49   // Snaity check on allocated space for text section
50   assert( ES->Size < 4096 && "no more space in TextSection" );
51
52   // FIXME: Using ES->Size directly here instead of calculating it from the
53   // output buffer size (impossible because the code emitter deals only in raw
54   // bytes) forces us to manually synchronize size and write padding zero bytes
55   // to the output buffer for all non-text sections.  For text sections, we do
56   // not synchonize the output buffer, and we just blow up if anyone tries to
57   // write non-code to it.  An assert should probably be added to
58   // AddSymbolToSection to prevent calling it on the text section.
59   CurBufferPtr = BufferBegin + ES->Size;
60
61   // Record function start address relative to BufferBegin
62   FnStartPtr = CurBufferPtr;
63 }
64
65 /// finishFunction - This callback is invoked after the function is completely
66 /// finished.
67 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
68   // Add a symbol to represent the function.
69   ELFSym FnSym(MF.getFunction());
70
71   // Update Section Size
72   ES->Size = CurBufferPtr - BufferBegin;
73
74   // Set the symbol type as a function
75   FnSym.setType(ELFSym::STT_FUNC);
76   FnSym.SectionIdx = ES->SectionIdx;
77   FnSym.Size = CurBufferPtr-FnStartPtr;
78
79   // Offset from start of Section
80   FnSym.Value = FnStartPtr-BufferBegin;
81
82   // Figure out the binding (linkage) of the symbol.
83   switch (MF.getFunction()->getLinkage()) {
84   default:
85     // appending linkage is illegal for functions.
86     assert(0 && "Unknown linkage type!");
87   case GlobalValue::ExternalLinkage:
88     FnSym.setBind(ELFSym::STB_GLOBAL);
89     EW.SymbolList.push_back(FnSym);
90     break;
91   case GlobalValue::LinkOnceAnyLinkage:
92   case GlobalValue::LinkOnceODRLinkage:
93   case GlobalValue::WeakAnyLinkage:
94   case GlobalValue::WeakODRLinkage:
95     FnSym.setBind(ELFSym::STB_WEAK);
96     EW.SymbolList.push_back(FnSym);
97     break;
98   case GlobalValue::PrivateLinkage:
99     assert (0 && "PrivateLinkage should not be in the symbol table.");
100   case GlobalValue::InternalLinkage:
101     FnSym.setBind(ELFSym::STB_LOCAL);
102     EW.SymbolList.push_front(FnSym);
103     break;
104   }
105
106   // Relocations
107   // -----------
108   // If we have emitted any relocations to function-specific objects such as 
109   // basic blocks, constant pools entries, or jump tables, record their
110   // addresses now so that we can rewrite them with the correct addresses
111   // later.
112   for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
113     MachineRelocation &MR = Relocations[i];
114     intptr_t Addr;
115     if (MR.isBasicBlock()) {
116       Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
117       MR.setConstantVal(ES->SectionIdx);
118       MR.setResultPointer((void*)Addr);
119     } else if (MR.isGlobalValue()) {
120       EW.PendingGlobals.insert(MR.getGlobalValue());
121     } else {
122       assert(0 && "Unhandled relocation type");
123     }
124     ES->addRelocation(MR);
125   }
126   Relocations.clear();
127
128   return false;
129 }
130
131 } // end namespace llvm