blob: 6e502465942b265d536cbbf82818e81ad08d51f0 [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 Lopes171375f2009-07-18 19:30:09 +000023#include "llvm/Target/TargetELFWriterInfo.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000024#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000025#include "llvm/Target/TargetAsmInfo.h"
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000026#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000028
29//===----------------------------------------------------------------------===//
30// ELFCodeEmitter Implementation
31//===----------------------------------------------------------------------===//
32
33namespace llvm {
34
35/// startFunction - This callback is invoked when a new machine function is
36/// about to be emitted.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000037void ELFCodeEmitter::startFunction(MachineFunction &MF) {
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000038 DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
39
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000040 // Get the ELF Section that this function belongs in.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000041 ES = &EW.getTextSection();
42
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000043 // Set the desired binary object to be used by the code emitters
44 setBinaryObject(ES);
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000045
Bruno Cardoso Lopes3d62a412009-07-02 02:13:13 +000046 // Get the function alignment in bytes
47 unsigned Align = (1 << MF.getAlignment());
48
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000049 // The function must start on its required alignment
50 ES->emitAlignment(Align);
51
52 // Update the section alignment if needed.
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000053 if (ES->Align < Align) ES->Align = Align;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000054
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000055 // Record the function start offset
56 FnStartOff = ES->getCurrentPCOffset();
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000057}
58
59/// finishFunction - This callback is invoked after the function is completely
60/// finished.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000061bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000062 // Add a symbol to represent the function.
63 const Function *F = MF.getFunction();
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000064 ELFSym *FnSym = new ELFSym(F);
65 FnSym->setType(ELFSym::STT_FUNC);
66 FnSym->setBind(EW.getGlobalELFBinding(F));
67 FnSym->setVisibility(EW.getGlobalELFVisibility(F));
68 FnSym->SectionIdx = ES->SectionIdx;
69 FnSym->Size = ES->getCurrentPCOffset()-FnStartOff;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000070
71 // Offset from start of Section
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000072 FnSym->Value = FnStartOff;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000073
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000074 if (!F->hasPrivateLinkage())
75 EW.SymbolList.push_back(FnSym);
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000076
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000077 // Emit constant pool to appropriate section(s)
78 emitConstantPool(MF.getConstantPool());
79
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +000080 // Emit jump tables to appropriate section
81 emitJumpTables(MF.getJumpTableInfo());
82
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000083 // Relocations
84 // -----------
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000085 // If we have emitted any relocations to function-specific objects such as
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000086 // basic blocks, constant pools entries, or jump tables, record their
87 // addresses now so that we can rewrite them with the correct addresses
88 // later.
89 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
90 MachineRelocation &MR = Relocations[i];
91 intptr_t Addr;
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000092 if (MR.isGlobalValue()) {
93 EW.PendingGlobals.insert(MR.getGlobalValue());
94 } else if (MR.isBasicBlock()) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000095 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
96 MR.setConstantVal(ES->SectionIdx);
97 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000098 } else if (MR.isConstantPoolIndex()) {
99 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
100 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
101 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000102 } else if (MR.isJumpTableIndex()) {
103 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
104 MR.setResultPointer((void*)Addr);
105 MR.setConstantVal(JumpTableSectionIdx);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000106 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000107 llvm_unreachable("Unhandled relocation type");
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000108 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000109 ES->addRelocation(MR);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000110 }
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000111
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000112 // Clear per-function data structures.
113 Relocations.clear();
114 CPLocations.clear();
115 CPSections.clear();
116 JTLocations.clear();
117 MBBLocations.clear();
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000118 return false;
119}
120
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000121/// emitConstantPool - For each constant pool entry, figure out which section
122/// the constant should live in and emit the constant
123void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
124 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
125 if (CP.empty()) return;
126
127 // TODO: handle PIC codegen
128 assert(TM.getRelocationModel() != Reloc::PIC_ &&
129 "PIC codegen not yet handled for elf constant pools!");
130
131 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
132 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
133 MachineConstantPoolEntry CPE = CP[i];
134
135 // Get the right ELF Section for this constant pool entry
136 std::string CstPoolName =
137 TAI->SelectSectionForMachineConst(CPE.getType())->getName();
138 ELFSection &CstPoolSection =
139 EW.getConstantPoolSection(CstPoolName, CPE.getAlignment());
140
141 // Record the constant pool location and the section index
142 CPLocations.push_back(CstPoolSection.size());
143 CPSections.push_back(CstPoolSection.SectionIdx);
144
145 if (CPE.isMachineConstantPoolEntry())
146 assert("CPE.isMachineConstantPoolEntry not supported yet");
147
148 // Emit the constant to constant pool section
149 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
150 }
151}
152
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000153/// emitJumpTables - Emit all the jump tables for a given jump table info
154/// record to the appropriate section.
155void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
156 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
157 if (JT.empty()) return;
158
159 // FIXME: handle PIC codegen
160 assert(TM.getRelocationModel() != Reloc::PIC_ &&
161 "PIC codegen not yet handled for elf jump tables!");
162
163 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000164 const TargetELFWriterInfo *TEW = TM.getELFWriterInfo();
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000165
166 // Get the ELF Section to emit the jump table
167 unsigned Align = TM.getTargetData()->getPointerABIAlignment();
168 std::string JTName(TAI->getJumpTableDataSection());
169 ELFSection &JTSection = EW.getJumpTableSection(JTName, Align);
170 JumpTableSectionIdx = JTSection.SectionIdx;
171
172 // Entries in the JT Section are relocated against the text section
173 ELFSection &TextSection = EW.getTextSection();
174
175 // For each JT, record its offset from the start of the section
176 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
177 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
178
179 DOUT << "JTSection.size(): " << JTSection.size() << "\n";
180 DOUT << "JTLocations.size: " << JTLocations.size() << "\n";
181
182 // Record JT 'i' offset in the JT section
183 JTLocations.push_back(JTSection.size());
184
185 // Each MBB entry in the Jump table section has a relocation entry
186 // against the current text section.
187 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
Bruno Cardoso Lopes617dd7b2009-07-18 20:52:11 +0000188 unsigned MachineRelTy = TEW->getAbsoluteLabelMachineRelTy();
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000189 MachineRelocation MR =
190 MachineRelocation::getBB(JTSection.size(),
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000191 MachineRelTy,
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000192 MBBs[mi]);
193
194 // Offset of JT 'i' in JT section
195 MR.setResultPointer((void*)getMachineBasicBlockAddress(MBBs[mi]));
196 MR.setConstantVal(TextSection.SectionIdx);
197
198 // Add the relocation to the Jump Table section
199 JTSection.addRelocation(MR);
200
201 // Output placeholder for MBB in the JT section
202 JTSection.emitWord(0);
203 }
204 }
205}
206
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000207} // end namespace llvm