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 // Align the output buffer with function alignment, and
44 // upgrade the section alignment if required
46 TM.getELFWriterInfo()->getFunctionAlignment(MF.getFunction());
47 if (ES->Align < Align) ES->Align = Align;
48 ES->Size = (ES->Size + (Align-1)) & (-Align);
50 // Snaity check on allocated space for text section
51 assert( ES->Size < 4096 && "no more space in TextSection" );
53 // FIXME: Using ES->Size directly here instead of calculating it from the
54 // output buffer size (impossible because the code emitter deals only in raw
55 // bytes) forces us to manually synchronize size and write padding zero bytes
56 // to the output buffer for all non-text sections. For text sections, we do
57 // not synchonize the output buffer, and we just blow up if anyone tries to
58 // write non-code to it. An assert should probably be added to
59 // AddSymbolToSection to prevent calling it on the text section.
60 CurBufferPtr = BufferBegin + ES->Size;
62 // Record function start address relative to BufferBegin
63 FnStartPtr = CurBufferPtr;
66 /// finishFunction - This callback is invoked after the function is completely
68 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
69 // Add a symbol to represent the function.
70 ELFSym FnSym(MF.getFunction());
72 // Update Section Size
73 ES->Size = CurBufferPtr - BufferBegin;
75 // Set the symbol type as a function
76 FnSym.setType(ELFSym::STT_FUNC);
77 FnSym.SectionIdx = ES->SectionIdx;
78 FnSym.Size = CurBufferPtr-FnStartPtr;
80 // Offset from start of Section
81 FnSym.Value = FnStartPtr-BufferBegin;
83 // Figure out the binding (linkage) of the symbol.
84 switch (MF.getFunction()->getLinkage()) {
86 // appending linkage is illegal for functions.
87 assert(0 && "Unknown linkage type!");
88 case GlobalValue::ExternalLinkage:
89 FnSym.setBind(ELFSym::STB_GLOBAL);
90 EW.SymbolList.push_back(FnSym);
92 case GlobalValue::LinkOnceAnyLinkage:
93 case GlobalValue::LinkOnceODRLinkage:
94 case GlobalValue::WeakAnyLinkage:
95 case GlobalValue::WeakODRLinkage:
96 FnSym.setBind(ELFSym::STB_WEAK);
97 EW.SymbolList.push_back(FnSym);
99 case GlobalValue::PrivateLinkage:
100 assert (0 && "PrivateLinkage should not be in the symbol table.");
101 case GlobalValue::InternalLinkage:
102 FnSym.setBind(ELFSym::STB_LOCAL);
103 EW.SymbolList.push_front(FnSym);
107 // Emit constant pool to appropriate section(s)
108 emitConstantPool(MF.getConstantPool());
112 // If we have emitted any relocations to function-specific objects such as
113 // basic blocks, constant pools entries, or jump tables, record their
114 // addresses now so that we can rewrite them with the correct addresses
116 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
117 MachineRelocation &MR = Relocations[i];
119 if (MR.isGlobalValue()) {
120 EW.PendingGlobals.insert(MR.getGlobalValue());
121 } else if (MR.isBasicBlock()) {
122 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
123 MR.setConstantVal(ES->SectionIdx);
124 MR.setResultPointer((void*)Addr);
125 } else if (MR.isConstantPoolIndex()) {
126 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
127 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
128 MR.setResultPointer((void*)Addr);
130 assert(0 && "Unhandled relocation type");
132 ES->addRelocation(MR);
139 /// emitConstantPool - For each constant pool entry, figure out which section
140 /// the constant should live in and emit the constant
141 void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
142 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
143 if (CP.empty()) return;
145 // TODO: handle PIC codegen
146 assert(TM.getRelocationModel() != Reloc::PIC_ &&
147 "PIC codegen not yet handled for elf constant pools!");
149 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
150 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
151 MachineConstantPoolEntry CPE = CP[i];
153 // Get the right ELF Section for this constant pool entry
154 std::string CstPoolName =
155 TAI->SelectSectionForMachineConst(CPE.getType())->getName();
156 ELFSection &CstPoolSection =
157 EW.getConstantPoolSection(CstPoolName, CPE.getAlignment());
159 // Record the constant pool location and the section index
160 CPLocations.push_back(CstPoolSection.size());
161 CPSections.push_back(CstPoolSection.SectionIdx);
163 if (CPE.isMachineConstantPoolEntry())
164 assert("CPE.isMachineConstantPoolEntry not supported yet");
166 // Emit the constant to constant pool section
167 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
171 } // end namespace llvm