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/MachineConstantPool.h"
17 #include "llvm/CodeGen/MachineJumpTableInfo.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Support/Debug.h"
21 //===----------------------------------------------------------------------===//
22 // ELFCodeEmitter Implementation
23 //===----------------------------------------------------------------------===//
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();
33 DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
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();
40 // Align the output buffer with function alignment, and
41 // upgrade the section alignment if required
43 TM.getELFWriterInfo()->getFunctionAlignment(MF.getFunction());
44 if (ES->Align < Align) ES->Align = Align;
45 ES->Size = (ES->Size + (Align-1)) & (-Align);
47 // Snaity check on allocated space for text section
48 assert( ES->Size < 4096 && "no more space in TextSection" );
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;
59 // Record function start address relative to BufferBegin
60 FnStartPtr = CurBufferPtr;
63 /// finishFunction - This callback is invoked after the function is completely
65 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
66 // Add a symbol to represent the function.
67 ELFSym FnSym(MF.getFunction());
69 // Update Section Size
70 ES->Size = CurBufferPtr - BufferBegin;
72 // Figure out the binding (linkage) of the symbol.
73 switch (MF.getFunction()->getLinkage()) {
75 // appending linkage is illegal for functions.
76 assert(0 && "Unknown linkage type!");
77 case GlobalValue::ExternalLinkage:
78 FnSym.SetBind(ELFSym::STB_GLOBAL);
80 case GlobalValue::LinkOnceAnyLinkage:
81 case GlobalValue::LinkOnceODRLinkage:
82 case GlobalValue::WeakAnyLinkage:
83 case GlobalValue::WeakODRLinkage:
84 FnSym.SetBind(ELFSym::STB_WEAK);
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);
93 // Set the symbol type as a function
94 FnSym.SetType(ELFSym::STT_FUNC);
96 FnSym.SectionIdx = ES->SectionIdx;
97 FnSym.Size = CurBufferPtr-FnStartPtr;
99 // Offset from start of Section
100 FnSym.Value = FnStartPtr-BufferBegin;
102 // Finally, add it to the symtab.
103 EW.SymbolTable.push_back(FnSym);
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
111 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
112 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->Relocations.push_back(MR);
131 } // end namespace llvm