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/TargetData.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 const TargetData *TD = TM.getTargetData();
32 const Function *F = MF.getFunction();
34 // Align the output buffer to the appropriate alignment, power of 2.
35 unsigned FnAlign = F->getAlignment();
36 unsigned TDAlign = TD->getPrefTypeAlignment(F->getType());
37 unsigned Align = std::max(FnAlign, TDAlign);
38 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
40 // Get the ELF Section that this function belongs in.
41 ES = &EW.getTextSection();
43 // FIXME: better memory management, this will be replaced by BinaryObjects
44 ES->SectionData.reserve(4096);
45 BufferBegin = &ES->SectionData[0];
46 BufferEnd = BufferBegin + ES->SectionData.capacity();
48 // Upgrade the section alignment if required.
49 if (ES->Align < Align) ES->Align = Align;
51 // Round the size up to the correct alignment for starting the new function.
52 ES->Size = (ES->Size + (Align-1)) & (-Align);
54 // Snaity check on allocated space for text section
55 assert( ES->Size < 4096 && "no more space in TextSection" );
57 // FIXME: Using ES->Size directly here instead of calculating it from the
58 // output buffer size (impossible because the code emitter deals only in raw
59 // bytes) forces us to manually synchronize size and write padding zero bytes
60 // to the output buffer for all non-text sections. For text sections, we do
61 // not synchonize the output buffer, and we just blow up if anyone tries to
62 // write non-code to it. An assert should probably be added to
63 // AddSymbolToSection to prevent calling it on the text section.
64 CurBufferPtr = BufferBegin + ES->Size;
66 // Record function start address relative to BufferBegin
67 FnStartPtr = CurBufferPtr;
70 /// finishFunction - This callback is invoked after the function is completely
72 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
73 // Add a symbol to represent the function.
74 ELFWriter::ELFSym FnSym(MF.getFunction());
76 // Figure out the binding (linkage) of the symbol.
77 switch (MF.getFunction()->getLinkage()) {
79 // appending linkage is illegal for functions.
80 assert(0 && "Unknown linkage type!");
81 case GlobalValue::ExternalLinkage:
82 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
84 case GlobalValue::LinkOnceAnyLinkage:
85 case GlobalValue::LinkOnceODRLinkage:
86 case GlobalValue::WeakAnyLinkage:
87 case GlobalValue::WeakODRLinkage:
88 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
90 case GlobalValue::PrivateLinkage:
91 assert (0 && "PrivateLinkage should not be in the symbol table.");
92 case GlobalValue::InternalLinkage:
93 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
97 // Set the symbol type as a function
98 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
100 FnSym.SectionIdx = ES->SectionIdx;
101 FnSym.Size = CurBufferPtr-FnStartPtr;
103 // Offset from start of Section
104 FnSym.Value = FnStartPtr-BufferBegin;
106 // Finally, add it to the symtab.
107 EW.SymbolTable.push_back(FnSym);
109 // Update Section Size
110 ES->Size = CurBufferPtr - BufferBegin;
114 } // end namespace llvm