blob: 168fed56c19600690a3f1fc4bea2419a04e5c859 [file] [log] [blame]
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +00001//===-- lib/CodeGen/ELFCodeEmitter.cpp ------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000010#define DEBUG_TYPE "elfce"
11
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000012#include "ELFCodeEmitter.h"
13#include "llvm/Constants.h"
14#include "llvm/DerivedTypes.h"
15#include "llvm/Function.h"
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000016#include "llvm/CodeGen/BinaryObject.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000017#include "llvm/CodeGen/MachineConstantPool.h"
18#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000019#include "llvm/Target/TargetData.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000020#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000021#include "llvm/Support/Debug.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000022
23//===----------------------------------------------------------------------===//
24// ELFCodeEmitter Implementation
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
28
29/// startFunction - This callback is invoked when a new machine function is
30/// about to be emitted.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000031void ELFCodeEmitter::startFunction(MachineFunction &MF) {
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000032 // Get the ELF Section that this function belongs in.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000033 ES = &EW.getTextSection();
34
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000035 DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
36
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000037 // FIXME: better memory management, this will be replaced by BinaryObjects
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000038 BinaryData &BD = ES->getData();
39 BD.reserve(4096);
40 BufferBegin = &BD[0];
41 BufferEnd = BufferBegin + BD.capacity();
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000042
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000043 // Align the output buffer with function alignment, and
44 // upgrade the section alignment if required
45 unsigned Align =
46 TM.getELFWriterInfo()->getFunctionAlignment(MF.getFunction());
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000047 if (ES->Align < Align) ES->Align = Align;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000048 ES->Size = (ES->Size + (Align-1)) & (-Align);
49
50 // Snaity check on allocated space for text section
51 assert( ES->Size < 4096 && "no more space in TextSection" );
52
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;
61
62 // Record function start address relative to BufferBegin
63 FnStartPtr = CurBufferPtr;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000064}
65
66/// finishFunction - This callback is invoked after the function is completely
67/// finished.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000068bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
69 // Add a symbol to represent the function.
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000070 ELFSym FnSym(MF.getFunction());
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000071
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000072 // Update Section Size
73 ES->Size = CurBufferPtr - BufferBegin;
74
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000075 // Set the symbol type as a function
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000076 FnSym.setType(ELFSym::STT_FUNC);
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000077 FnSym.SectionIdx = ES->SectionIdx;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000078 FnSym.Size = CurBufferPtr-FnStartPtr;
79
80 // Offset from start of Section
81 FnSym.Value = FnStartPtr-BufferBegin;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000082
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000083 // Figure out the binding (linkage) of the symbol.
84 switch (MF.getFunction()->getLinkage()) {
85 default:
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);
91 break;
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);
98 break;
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);
104 break;
105 }
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000106
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000107 // Emit constant pool to appropriate section(s)
108 emitConstantPool(MF.getConstantPool());
109
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000110 // Relocations
111 // -----------
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000112 // If we have emitted any relocations to function-specific objects such as
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000113 // basic blocks, constant pools entries, or jump tables, record their
114 // addresses now so that we can rewrite them with the correct addresses
115 // later.
116 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
117 MachineRelocation &MR = Relocations[i];
118 intptr_t Addr;
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000119 if (MR.isGlobalValue()) {
120 EW.PendingGlobals.insert(MR.getGlobalValue());
121 } else if (MR.isBasicBlock()) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000122 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
123 MR.setConstantVal(ES->SectionIdx);
124 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000125 } else if (MR.isConstantPoolIndex()) {
126 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
127 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
128 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000129 } else {
130 assert(0 && "Unhandled relocation type");
131 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000132 ES->addRelocation(MR);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000133 }
134 Relocations.clear();
135
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000136 return false;
137}
138
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000139/// emitConstantPool - For each constant pool entry, figure out which section
140/// the constant should live in and emit the constant
141void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
142 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
143 if (CP.empty()) return;
144
145 // TODO: handle PIC codegen
146 assert(TM.getRelocationModel() != Reloc::PIC_ &&
147 "PIC codegen not yet handled for elf constant pools!");
148
149 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
150 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
151 MachineConstantPoolEntry CPE = CP[i];
152
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());
158
159 // Record the constant pool location and the section index
160 CPLocations.push_back(CstPoolSection.size());
161 CPSections.push_back(CstPoolSection.SectionIdx);
162
163 if (CPE.isMachineConstantPoolEntry())
164 assert("CPE.isMachineConstantPoolEntry not supported yet");
165
166 // Emit the constant to constant pool section
167 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
168 }
169}
170
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000171} // end namespace llvm