1 //===-- lib/CodeGen/ELFCodeEmitter.cpp ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "elfce"
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"
22 //===----------------------------------------------------------------------===//
23 // ELFCodeEmitter Implementation
24 //===----------------------------------------------------------------------===//
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();
34 DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
36 // FIXME: better memory management, this will be replaced by BinaryObjects
37 BinaryData &BD = ES->getData();
40 BufferEnd = BufferBegin + BD.capacity();
42 // Align the output buffer with function alignment, and
43 // upgrade the section alignment if required
45 TM.getELFWriterInfo()->getFunctionAlignment(MF.getFunction());
46 if (ES->Align < Align) ES->Align = Align;
47 ES->Size = (ES->Size + (Align-1)) & (-Align);
49 // Snaity check on allocated space for text section
50 assert( ES->Size < 4096 && "no more space in TextSection" );
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;
61 // Record function start address relative to BufferBegin
62 FnStartPtr = CurBufferPtr;
65 /// finishFunction - This callback is invoked after the function is completely
67 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
68 // Add a symbol to represent the function.
69 ELFSym FnSym(MF.getFunction());
71 // Update Section Size
72 ES->Size = CurBufferPtr - BufferBegin;
74 // Set the symbol type as a function
75 FnSym.setType(ELFSym::STT_FUNC);
76 FnSym.SectionIdx = ES->SectionIdx;
77 FnSym.Size = CurBufferPtr-FnStartPtr;
79 // Offset from start of Section
80 FnSym.Value = FnStartPtr-BufferBegin;
82 // Figure out the binding (linkage) of the symbol.
83 switch (MF.getFunction()->getLinkage()) {
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);
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);
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);
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
112 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
113 MachineRelocation &MR = Relocations[i];
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());
122 assert(0 && "Unhandled relocation type");
124 ES->addRelocation(MR);
131 } // end namespace llvm