blob: 57b75a37f2317738efa5a1468fd2325cff720bb0 [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 Lopes45f5d642009-07-02 18:29:24 +000012#include "ELF.h"
13#include "ELFWriter.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000014#include "ELFCodeEmitter.h"
15#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000018#include "llvm/CodeGen/BinaryObject.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000019#include "llvm/CodeGen/MachineConstantPool.h"
20#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000021#include "llvm/CodeGen/MachineRelocation.h"
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000022#include "llvm/Target/TargetData.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000023#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000024#include "llvm/Target/TargetAsmInfo.h"
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000025#include "llvm/Support/Debug.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000026
27//===----------------------------------------------------------------------===//
28// ELFCodeEmitter Implementation
29//===----------------------------------------------------------------------===//
30
31namespace llvm {
32
33/// startFunction - This callback is invoked when a new machine function is
34/// about to be emitted.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000035void ELFCodeEmitter::startFunction(MachineFunction &MF) {
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000036 // Get the ELF Section that this function belongs in.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000037 ES = &EW.getTextSection();
38
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000039 DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
40
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000041 // FIXME: better memory management, this will be replaced by BinaryObjects
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000042 BinaryData &BD = ES->getData();
43 BD.reserve(4096);
44 BufferBegin = &BD[0];
45 BufferEnd = BufferBegin + BD.capacity();
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000046
Bruno Cardoso Lopes3d62a412009-07-02 02:13:13 +000047 // Get the function alignment in bytes
48 unsigned Align = (1 << MF.getAlignment());
49
50 // Align the section size with the function alignment, so the function can
51 // start in a aligned offset, also update the section alignment if needed.
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000052 if (ES->Align < Align) ES->Align = Align;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000053 ES->Size = (ES->Size + (Align-1)) & (-Align);
54
55 // Snaity check on allocated space for text section
56 assert( ES->Size < 4096 && "no more space in TextSection" );
57
58 // FIXME: Using ES->Size directly here instead of calculating it from the
59 // output buffer size (impossible because the code emitter deals only in raw
60 // bytes) forces us to manually synchronize size and write padding zero bytes
61 // to the output buffer for all non-text sections. For text sections, we do
62 // not synchonize the output buffer, and we just blow up if anyone tries to
63 // write non-code to it. An assert should probably be added to
64 // AddSymbolToSection to prevent calling it on the text section.
65 CurBufferPtr = BufferBegin + ES->Size;
66
67 // Record function start address relative to BufferBegin
68 FnStartPtr = CurBufferPtr;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000069}
70
71/// finishFunction - This callback is invoked after the function is completely
72/// finished.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000073bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000074 // Update Section Size
75 ES->Size = CurBufferPtr - BufferBegin;
76
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000077 // Add a symbol to represent the function.
78 const Function *F = MF.getFunction();
79 ELFSym FnSym(F);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000080 FnSym.setType(ELFSym::STT_FUNC);
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000081 FnSym.setBind(EW.getGlobalELFLinkage(F));
82 FnSym.setVisibility(EW.getGlobalELFVisibility(F));
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000083 FnSym.SectionIdx = ES->SectionIdx;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000084 FnSym.Size = CurBufferPtr-FnStartPtr;
85
86 // Offset from start of Section
87 FnSym.Value = FnStartPtr-BufferBegin;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000088
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000089 // Locals should go on the symbol list front
90 if (!F->hasPrivateLinkage()) {
91 if (FnSym.getBind() == ELFSym::STB_LOCAL)
92 EW.SymbolList.push_front(FnSym);
93 else
94 EW.SymbolList.push_back(FnSym);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000095 }
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000096
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000097 // Emit constant pool to appropriate section(s)
98 emitConstantPool(MF.getConstantPool());
99
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000100 // Relocations
101 // -----------
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000102 // If we have emitted any relocations to function-specific objects such as
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000103 // basic blocks, constant pools entries, or jump tables, record their
104 // addresses now so that we can rewrite them with the correct addresses
105 // later.
106 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
107 MachineRelocation &MR = Relocations[i];
108 intptr_t Addr;
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000109 if (MR.isGlobalValue()) {
110 EW.PendingGlobals.insert(MR.getGlobalValue());
111 } else if (MR.isBasicBlock()) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000112 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
113 MR.setConstantVal(ES->SectionIdx);
114 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000115 } else if (MR.isConstantPoolIndex()) {
116 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
117 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
118 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000119 } else {
120 assert(0 && "Unhandled relocation type");
121 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000122 ES->addRelocation(MR);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000123 }
124 Relocations.clear();
125
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000126 return false;
127}
128
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000129/// emitConstantPool - For each constant pool entry, figure out which section
130/// the constant should live in and emit the constant
131void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
132 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
133 if (CP.empty()) return;
134
135 // TODO: handle PIC codegen
136 assert(TM.getRelocationModel() != Reloc::PIC_ &&
137 "PIC codegen not yet handled for elf constant pools!");
138
139 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
140 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
141 MachineConstantPoolEntry CPE = CP[i];
142
143 // Get the right ELF Section for this constant pool entry
144 std::string CstPoolName =
145 TAI->SelectSectionForMachineConst(CPE.getType())->getName();
146 ELFSection &CstPoolSection =
147 EW.getConstantPoolSection(CstPoolName, CPE.getAlignment());
148
149 // Record the constant pool location and the section index
150 CPLocations.push_back(CstPoolSection.size());
151 CPSections.push_back(CstPoolSection.SectionIdx);
152
153 if (CPE.isMachineConstantPoolEntry())
154 assert("CPE.isMachineConstantPoolEntry not supported yet");
155
156 // Emit the constant to constant pool section
157 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
158 }
159}
160
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000161} // end namespace llvm