Introduce new BinaryObject (blob) class, ELF Writer modified to use it. BinaryObject...
[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   // Figure out the binding (linkage) of the symbol.
75   switch (MF.getFunction()->getLinkage()) {
76   default:
77     // appending linkage is illegal for functions.
78     assert(0 && "Unknown linkage type!");
79   case GlobalValue::ExternalLinkage:
80     FnSym.SetBind(ELFSym::STB_GLOBAL);
81     break;
82   case GlobalValue::LinkOnceAnyLinkage:
83   case GlobalValue::LinkOnceODRLinkage:
84   case GlobalValue::WeakAnyLinkage:
85   case GlobalValue::WeakODRLinkage:
86     FnSym.SetBind(ELFSym::STB_WEAK);
87     break;
88   case GlobalValue::PrivateLinkage:
89     assert (0 && "PrivateLinkage should not be in the symbol table.");
90   case GlobalValue::InternalLinkage:
91     FnSym.SetBind(ELFSym::STB_LOCAL);
92     break;
93   }
94
95   // Set the symbol type as a function
96   FnSym.SetType(ELFSym::STT_FUNC);
97
98   FnSym.SectionIdx = ES->SectionIdx;
99   FnSym.Size = CurBufferPtr-FnStartPtr;
100
101   // Offset from start of Section
102   FnSym.Value = FnStartPtr-BufferBegin;
103
104   // Finally, add it to the symtab.
105   EW.SymbolList.push_back(FnSym);
106
107   // Relocations
108   // -----------
109   // If we have emitted any relocations to function-specific objects such as 
110   // basic blocks, constant pools entries, or jump tables, record their
111   // addresses now so that we can rewrite them with the correct addresses
112   // later.
113   for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
114     MachineRelocation &MR = Relocations[i];
115     intptr_t Addr;
116
117     if (MR.isBasicBlock()) {
118       Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
119       MR.setConstantVal(ES->SectionIdx);
120       MR.setResultPointer((void*)Addr);
121     } else if (MR.isGlobalValue()) {
122       EW.PendingGlobals.insert(MR.getGlobalValue());
123     } else {
124       assert(0 && "Unhandled relocation type");
125     }
126     ES->addRelocation(MR);
127   }
128   Relocations.clear();
129
130   return false;
131 }
132
133 } // end namespace llvm