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/TargetData.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Support/Debug.h"
23 //===----------------------------------------------------------------------===//
24 // ELFCodeEmitter Implementation
25 //===----------------------------------------------------------------------===//
29 /// startFunction - This callback is invoked when a new machine function is
30 /// about to be emitted.
31 void ELFCodeEmitter::startFunction(MachineFunction &MF) {
32 // Get the ELF Section that this function belongs in.
33 ES = &EW.getTextSection();
35 DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
37 // FIXME: better memory management, this will be replaced by BinaryObjects
38 BinaryData &BD = ES->getData();
41 BufferEnd = BufferBegin + BD.capacity();
43 // Get the function alignment in bytes
44 unsigned Align = (1 << MF.getAlignment());
46 // Align the section size with the function alignment, so the function can
47 // start in a aligned offset, also update the section alignment if needed.
48 if (ES->Align < Align) ES->Align = Align;
49 ES->Size = (ES->Size + (Align-1)) & (-Align);
51 // Snaity check on allocated space for text section
52 assert( ES->Size < 4096 && "no more space in TextSection" );
54 // FIXME: Using ES->Size directly here instead of calculating it from the
55 // output buffer size (impossible because the code emitter deals only in raw
56 // bytes) forces us to manually synchronize size and write padding zero bytes
57 // to the output buffer for all non-text sections. For text sections, we do
58 // not synchonize the output buffer, and we just blow up if anyone tries to
59 // write non-code to it. An assert should probably be added to
60 // AddSymbolToSection to prevent calling it on the text section.
61 CurBufferPtr = BufferBegin + ES->Size;
63 // Record function start address relative to BufferBegin
64 FnStartPtr = CurBufferPtr;
67 /// finishFunction - This callback is invoked after the function is completely
69 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
70 // Add a symbol to represent the function.
71 ELFSym FnSym(MF.getFunction());
73 // Update Section Size
74 ES->Size = CurBufferPtr - BufferBegin;
76 // Set the symbol type as a function
77 FnSym.setType(ELFSym::STT_FUNC);
78 FnSym.SectionIdx = ES->SectionIdx;
79 FnSym.Size = CurBufferPtr-FnStartPtr;
81 // Offset from start of Section
82 FnSym.Value = FnStartPtr-BufferBegin;
84 // Figure out the binding (linkage) of the symbol.
85 switch (MF.getFunction()->getLinkage()) {
87 // appending linkage is illegal for functions.
88 assert(0 && "Unknown linkage type!");
89 case GlobalValue::ExternalLinkage:
90 FnSym.setBind(ELFSym::STB_GLOBAL);
91 EW.SymbolList.push_back(FnSym);
93 case GlobalValue::LinkOnceAnyLinkage:
94 case GlobalValue::LinkOnceODRLinkage:
95 case GlobalValue::WeakAnyLinkage:
96 case GlobalValue::WeakODRLinkage:
97 FnSym.setBind(ELFSym::STB_WEAK);
98 EW.SymbolList.push_back(FnSym);
100 case GlobalValue::PrivateLinkage:
101 assert (0 && "PrivateLinkage should not be in the symbol table.");
102 case GlobalValue::InternalLinkage:
103 FnSym.setBind(ELFSym::STB_LOCAL);
104 EW.SymbolList.push_front(FnSym);
108 // Emit constant pool to appropriate section(s)
109 emitConstantPool(MF.getConstantPool());
113 // If we have emitted any relocations to function-specific objects such as
114 // basic blocks, constant pools entries, or jump tables, record their
115 // addresses now so that we can rewrite them with the correct addresses
117 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
118 MachineRelocation &MR = Relocations[i];
120 if (MR.isGlobalValue()) {
121 EW.PendingGlobals.insert(MR.getGlobalValue());
122 } else if (MR.isBasicBlock()) {
123 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
124 MR.setConstantVal(ES->SectionIdx);
125 MR.setResultPointer((void*)Addr);
126 } else if (MR.isConstantPoolIndex()) {
127 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
128 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
129 MR.setResultPointer((void*)Addr);
131 assert(0 && "Unhandled relocation type");
133 ES->addRelocation(MR);
140 /// emitConstantPool - For each constant pool entry, figure out which section
141 /// the constant should live in and emit the constant
142 void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
143 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
144 if (CP.empty()) return;
146 // TODO: handle PIC codegen
147 assert(TM.getRelocationModel() != Reloc::PIC_ &&
148 "PIC codegen not yet handled for elf constant pools!");
150 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
151 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
152 MachineConstantPoolEntry CPE = CP[i];
154 // Get the right ELF Section for this constant pool entry
155 std::string CstPoolName =
156 TAI->SelectSectionForMachineConst(CPE.getType())->getName();
157 ELFSection &CstPoolSection =
158 EW.getConstantPoolSection(CstPoolName, CPE.getAlignment());
160 // Record the constant pool location and the section index
161 CPLocations.push_back(CstPoolSection.size());
162 CPSections.push_back(CstPoolSection.SectionIdx);
164 if (CPE.isMachineConstantPoolEntry())
165 assert("CPE.isMachineConstantPoolEntry not supported yet");
167 // Emit the constant to constant pool section
168 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
172 } // end namespace llvm