blob: 1d150032c399e8f9252f4a7d2a643adf6f1e5035 [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"
Torok Edwinc25e7582009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000027
28//===----------------------------------------------------------------------===//
29// ELFCodeEmitter Implementation
30//===----------------------------------------------------------------------===//
31
32namespace llvm {
33
34/// startFunction - This callback is invoked when a new machine function is
35/// about to be emitted.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000036void ELFCodeEmitter::startFunction(MachineFunction &MF) {
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000037 DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
38
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000039 // Get the ELF Section that this function belongs in.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000040 ES = &EW.getTextSection();
41
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000042 // Set the desired binary object to be used by the code emitters
43 setBinaryObject(ES);
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000044
Bruno Cardoso Lopes3d62a412009-07-02 02:13:13 +000045 // Get the function alignment in bytes
46 unsigned Align = (1 << MF.getAlignment());
47
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000048 // The function must start on its required alignment
49 ES->emitAlignment(Align);
50
51 // 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
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000054 // Record the function start offset
55 FnStartOff = ES->getCurrentPCOffset();
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000056}
57
58/// finishFunction - This callback is invoked after the function is completely
59/// finished.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000060bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000061 // Add a symbol to represent the function.
62 const Function *F = MF.getFunction();
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000063 ELFSym *FnSym = new ELFSym(F);
64 FnSym->setType(ELFSym::STT_FUNC);
65 FnSym->setBind(EW.getGlobalELFBinding(F));
66 FnSym->setVisibility(EW.getGlobalELFVisibility(F));
67 FnSym->SectionIdx = ES->SectionIdx;
68 FnSym->Size = ES->getCurrentPCOffset()-FnStartOff;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000069
70 // Offset from start of Section
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000071 FnSym->Value = FnStartOff;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000072
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000073 if (!F->hasPrivateLinkage())
74 EW.SymbolList.push_back(FnSym);
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000075
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000076 // Emit constant pool to appropriate section(s)
77 emitConstantPool(MF.getConstantPool());
78
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +000079 // Emit jump tables to appropriate section
80 emitJumpTables(MF.getJumpTableInfo());
81
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000082 // Relocations
83 // -----------
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000084 // If we have emitted any relocations to function-specific objects such as
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000085 // basic blocks, constant pools entries, or jump tables, record their
86 // addresses now so that we can rewrite them with the correct addresses
87 // later.
88 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
89 MachineRelocation &MR = Relocations[i];
90 intptr_t Addr;
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000091 if (MR.isGlobalValue()) {
92 EW.PendingGlobals.insert(MR.getGlobalValue());
93 } else if (MR.isBasicBlock()) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000094 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
95 MR.setConstantVal(ES->SectionIdx);
96 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000097 } else if (MR.isConstantPoolIndex()) {
98 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
99 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
100 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000101 } else if (MR.isJumpTableIndex()) {
102 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
103 MR.setResultPointer((void*)Addr);
104 MR.setConstantVal(JumpTableSectionIdx);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000105 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000106 llvm_unreachable("Unhandled relocation type");
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000107 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000108 ES->addRelocation(MR);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000109 }
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000110
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000111 // Clear per-function data structures.
112 Relocations.clear();
113 CPLocations.clear();
114 CPSections.clear();
115 JTLocations.clear();
116 MBBLocations.clear();
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000117 return false;
118}
119
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000120/// emitConstantPool - For each constant pool entry, figure out which section
121/// the constant should live in and emit the constant
122void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
123 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
124 if (CP.empty()) return;
125
126 // TODO: handle PIC codegen
127 assert(TM.getRelocationModel() != Reloc::PIC_ &&
128 "PIC codegen not yet handled for elf constant pools!");
129
130 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
131 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
132 MachineConstantPoolEntry CPE = CP[i];
133
134 // Get the right ELF Section for this constant pool entry
135 std::string CstPoolName =
136 TAI->SelectSectionForMachineConst(CPE.getType())->getName();
137 ELFSection &CstPoolSection =
138 EW.getConstantPoolSection(CstPoolName, CPE.getAlignment());
139
140 // Record the constant pool location and the section index
141 CPLocations.push_back(CstPoolSection.size());
142 CPSections.push_back(CstPoolSection.SectionIdx);
143
144 if (CPE.isMachineConstantPoolEntry())
145 assert("CPE.isMachineConstantPoolEntry not supported yet");
146
147 // Emit the constant to constant pool section
148 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
149 }
150}
151
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000152/// emitJumpTables - Emit all the jump tables for a given jump table info
153/// record to the appropriate section.
154void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
155 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
156 if (JT.empty()) return;
157
158 // FIXME: handle PIC codegen
159 assert(TM.getRelocationModel() != Reloc::PIC_ &&
160 "PIC codegen not yet handled for elf jump tables!");
161
162 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
163
164 // Get the ELF Section to emit the jump table
165 unsigned Align = TM.getTargetData()->getPointerABIAlignment();
166 std::string JTName(TAI->getJumpTableDataSection());
167 ELFSection &JTSection = EW.getJumpTableSection(JTName, Align);
168 JumpTableSectionIdx = JTSection.SectionIdx;
169
170 // Entries in the JT Section are relocated against the text section
171 ELFSection &TextSection = EW.getTextSection();
172
173 // For each JT, record its offset from the start of the section
174 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
175 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
176
177 DOUT << "JTSection.size(): " << JTSection.size() << "\n";
178 DOUT << "JTLocations.size: " << JTLocations.size() << "\n";
179
180 // Record JT 'i' offset in the JT section
181 JTLocations.push_back(JTSection.size());
182
183 // Each MBB entry in the Jump table section has a relocation entry
184 // against the current text section.
185 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
186 MachineRelocation MR =
187 MachineRelocation::getBB(JTSection.size(),
188 MachineRelocation::VANILLA,
189 MBBs[mi]);
190
191 // Offset of JT 'i' in JT section
192 MR.setResultPointer((void*)getMachineBasicBlockAddress(MBBs[mi]));
193 MR.setConstantVal(TextSection.SectionIdx);
194
195 // Add the relocation to the Jump Table section
196 JTSection.addRelocation(MR);
197
198 // Output placeholder for MBB in the JT section
199 JTSection.emitWord(0);
200 }
201 }
202}
203
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000204} // end namespace llvm