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