blob: 623507a95897f48ac814a3ef9387f38719d7c881 [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 Lopes3d62a412009-07-02 02:13:13 +000043 // Get the function alignment in bytes
44 unsigned Align = (1 << MF.getAlignment());
45
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.
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000048 if (ES->Align < Align) ES->Align = Align;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000049 ES->Size = (ES->Size + (Align-1)) & (-Align);
50
51 // Snaity check on allocated space for text section
52 assert( ES->Size < 4096 && "no more space in TextSection" );
53
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;
62
63 // Record function start address relative to BufferBegin
64 FnStartPtr = CurBufferPtr;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000065}
66
67/// finishFunction - This callback is invoked after the function is completely
68/// finished.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000069bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
70 // Add a symbol to represent the function.
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000071 ELFSym FnSym(MF.getFunction());
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000072
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000073 // Update Section Size
74 ES->Size = CurBufferPtr - BufferBegin;
75
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000076 // Set the symbol type as a function
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000077 FnSym.setType(ELFSym::STT_FUNC);
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000078 FnSym.SectionIdx = ES->SectionIdx;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000079 FnSym.Size = CurBufferPtr-FnStartPtr;
80
81 // Offset from start of Section
82 FnSym.Value = FnStartPtr-BufferBegin;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000083
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000084 // Figure out the binding (linkage) of the symbol.
85 switch (MF.getFunction()->getLinkage()) {
86 default:
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);
92 break;
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);
99 break;
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);
105 break;
106 }
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000107
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000108 // Emit constant pool to appropriate section(s)
109 emitConstantPool(MF.getConstantPool());
110
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000111 // Relocations
112 // -----------
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000113 // If we have emitted any relocations to function-specific objects such as
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000114 // basic blocks, constant pools entries, or jump tables, record their
115 // addresses now so that we can rewrite them with the correct addresses
116 // later.
117 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
118 MachineRelocation &MR = Relocations[i];
119 intptr_t Addr;
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000120 if (MR.isGlobalValue()) {
121 EW.PendingGlobals.insert(MR.getGlobalValue());
122 } else if (MR.isBasicBlock()) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000123 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
124 MR.setConstantVal(ES->SectionIdx);
125 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000126 } else if (MR.isConstantPoolIndex()) {
127 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
128 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
129 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000130 } else {
131 assert(0 && "Unhandled relocation type");
132 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000133 ES->addRelocation(MR);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000134 }
135 Relocations.clear();
136
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000137 return false;
138}
139
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000140/// emitConstantPool - For each constant pool entry, figure out which section
141/// the constant should live in and emit the constant
142void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
143 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
144 if (CP.empty()) return;
145
146 // TODO: handle PIC codegen
147 assert(TM.getRelocationModel() != Reloc::PIC_ &&
148 "PIC codegen not yet handled for elf constant pools!");
149
150 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
151 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
152 MachineConstantPoolEntry CPE = CP[i];
153
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());
159
160 // Record the constant pool location and the section index
161 CPLocations.push_back(CstPoolSection.size());
162 CPSections.push_back(CstPoolSection.SectionIdx);
163
164 if (CPE.isMachineConstantPoolEntry())
165 assert("CPE.isMachineConstantPoolEntry not supported yet");
166
167 // Emit the constant to constant pool section
168 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
169 }
170}
171
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000172} // end namespace llvm