blob: 78f0dae51c0a9f9fa95836a4f1b21b1425fb4786 [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 Lopes6933d3e2009-07-06 09:26:48 +000036 DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
37
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000038 // Get the ELF Section that this function belongs in.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000039 ES = &EW.getTextSection();
40
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000041 // Set the desired binary object to be used by the code emitters
42 setBinaryObject(ES);
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000043
Bruno Cardoso Lopes3d62a412009-07-02 02:13:13 +000044 // Get the function alignment in bytes
45 unsigned Align = (1 << MF.getAlignment());
46
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000047 // The function must start on its required alignment
48 ES->emitAlignment(Align);
49
50 // Update the section alignment if needed.
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000051 if (ES->Align < Align) ES->Align = Align;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000052
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000053 // Record the function start offset
54 FnStartOff = ES->getCurrentPCOffset();
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000055}
56
57/// finishFunction - This callback is invoked after the function is completely
58/// finished.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000059bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000060 // Add a symbol to represent the function.
61 const Function *F = MF.getFunction();
62 ELFSym FnSym(F);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000063 FnSym.setType(ELFSym::STT_FUNC);
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000064 FnSym.setBind(EW.getGlobalELFLinkage(F));
65 FnSym.setVisibility(EW.getGlobalELFVisibility(F));
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000066 FnSym.SectionIdx = ES->SectionIdx;
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000067 FnSym.Size = ES->getCurrentPCOffset()-FnStartOff;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000068
69 // Offset from start of Section
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000070 FnSym.Value = FnStartOff;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000071
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000072 // Locals should go on the symbol list front
73 if (!F->hasPrivateLinkage()) {
74 if (FnSym.getBind() == ELFSym::STB_LOCAL)
75 EW.SymbolList.push_front(FnSym);
76 else
77 EW.SymbolList.push_back(FnSym);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000078 }
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000079
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000080 // Emit constant pool to appropriate section(s)
81 emitConstantPool(MF.getConstantPool());
82
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +000083 // Emit jump tables to appropriate section
84 emitJumpTables(MF.getJumpTableInfo());
85
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000086 // Relocations
87 // -----------
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000088 // If we have emitted any relocations to function-specific objects such as
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000089 // basic blocks, constant pools entries, or jump tables, record their
90 // addresses now so that we can rewrite them with the correct addresses
91 // later.
92 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
93 MachineRelocation &MR = Relocations[i];
94 intptr_t Addr;
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000095 if (MR.isGlobalValue()) {
96 EW.PendingGlobals.insert(MR.getGlobalValue());
97 } else if (MR.isBasicBlock()) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000098 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
99 MR.setConstantVal(ES->SectionIdx);
100 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000101 } else if (MR.isConstantPoolIndex()) {
102 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
103 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
104 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000105 } else if (MR.isJumpTableIndex()) {
106 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
107 MR.setResultPointer((void*)Addr);
108 MR.setConstantVal(JumpTableSectionIdx);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000109 } else {
110 assert(0 && "Unhandled relocation type");
111 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000112 ES->addRelocation(MR);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000113 }
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000114
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000115 // Clear per-function data structures.
116 Relocations.clear();
117 CPLocations.clear();
118 CPSections.clear();
119 JTLocations.clear();
120 MBBLocations.clear();
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000121 return false;
122}
123
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000124/// emitConstantPool - For each constant pool entry, figure out which section
125/// the constant should live in and emit the constant
126void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
127 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
128 if (CP.empty()) return;
129
130 // TODO: handle PIC codegen
131 assert(TM.getRelocationModel() != Reloc::PIC_ &&
132 "PIC codegen not yet handled for elf constant pools!");
133
134 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
135 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
136 MachineConstantPoolEntry CPE = CP[i];
137
138 // Get the right ELF Section for this constant pool entry
139 std::string CstPoolName =
140 TAI->SelectSectionForMachineConst(CPE.getType())->getName();
141 ELFSection &CstPoolSection =
142 EW.getConstantPoolSection(CstPoolName, CPE.getAlignment());
143
144 // Record the constant pool location and the section index
145 CPLocations.push_back(CstPoolSection.size());
146 CPSections.push_back(CstPoolSection.SectionIdx);
147
148 if (CPE.isMachineConstantPoolEntry())
149 assert("CPE.isMachineConstantPoolEntry not supported yet");
150
151 // Emit the constant to constant pool section
152 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
153 }
154}
155
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000156/// emitJumpTables - Emit all the jump tables for a given jump table info
157/// record to the appropriate section.
158void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
159 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
160 if (JT.empty()) return;
161
162 // FIXME: handle PIC codegen
163 assert(TM.getRelocationModel() != Reloc::PIC_ &&
164 "PIC codegen not yet handled for elf jump tables!");
165
166 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
167
168 // Get the ELF Section to emit the jump table
169 unsigned Align = TM.getTargetData()->getPointerABIAlignment();
170 std::string JTName(TAI->getJumpTableDataSection());
171 ELFSection &JTSection = EW.getJumpTableSection(JTName, Align);
172 JumpTableSectionIdx = JTSection.SectionIdx;
173
174 // Entries in the JT Section are relocated against the text section
175 ELFSection &TextSection = EW.getTextSection();
176
177 // For each JT, record its offset from the start of the section
178 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
179 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
180
181 DOUT << "JTSection.size(): " << JTSection.size() << "\n";
182 DOUT << "JTLocations.size: " << JTLocations.size() << "\n";
183
184 // Record JT 'i' offset in the JT section
185 JTLocations.push_back(JTSection.size());
186
187 // Each MBB entry in the Jump table section has a relocation entry
188 // against the current text section.
189 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
190 MachineRelocation MR =
191 MachineRelocation::getBB(JTSection.size(),
192 MachineRelocation::VANILLA,
193 MBBs[mi]);
194
195 // Offset of JT 'i' in JT section
196 MR.setResultPointer((void*)getMachineBasicBlockAddress(MBBs[mi]));
197 MR.setConstantVal(TextSection.SectionIdx);
198
199 // Add the relocation to the Jump Table section
200 JTSection.addRelocation(MR);
201
202 // Output placeholder for MBB in the JT section
203 JTSection.emitWord(0);
204 }
205 }
206}
207
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000208} // end namespace llvm