blob: 8416d3bda930a983ecfc70fb9a3dcb8bf53fa274 [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"
David Greene0d036d22009-08-19 22:02:07 +000020#include "llvm/CodeGen/MachineFunction.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000022#include "llvm/CodeGen/MachineRelocation.h"
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000023#include "llvm/Target/TargetData.h"
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +000024#include "llvm/Target/TargetELFWriterInfo.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000026#include "llvm/MC/MCAsmInfo.h"
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000027#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000029#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000030
31//===----------------------------------------------------------------------===//
32// ELFCodeEmitter Implementation
33//===----------------------------------------------------------------------===//
34
35namespace llvm {
36
37/// startFunction - This callback is invoked when a new machine function is
38/// about to be emitted.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000039void ELFCodeEmitter::startFunction(MachineFunction &MF) {
David Greene5957c9b2010-01-04 19:36:42 +000040 DEBUG(dbgs() << "processing function: "
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000041 << MF.getFunction()->getName() << "\n");
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000042
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000043 // Get the ELF Section that this function belongs in.
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +000044 ES = &EW.getTextSection(MF.getFunction());
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000045
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000046 // Set the desired binary object to be used by the code emitters
47 setBinaryObject(ES);
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000048
Bruno Cardoso Lopes3d62a412009-07-02 02:13:13 +000049 // Get the function alignment in bytes
50 unsigned Align = (1 << MF.getAlignment());
51
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000052 // The function must start on its required alignment
53 ES->emitAlignment(Align);
54
55 // Update the section alignment if needed.
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +000056 ES->Align = std::max(ES->Align, Align);
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000057
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000058 // Record the function start offset
59 FnStartOff = ES->getCurrentPCOffset();
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +000060
61 // Emit constant pool and jump tables to their appropriate sections.
62 // They need to be emitted before the function because in some targets
63 // the later may reference JT or CP entry address.
64 emitConstantPool(MF.getConstantPool());
Chris Lattnerb1e80392010-01-25 23:22:00 +000065 if (MF.getJumpTableInfo())
66 emitJumpTables(MF.getJumpTableInfo());
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000067}
68
69/// finishFunction - This callback is invoked after the function is completely
70/// finished.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000071bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000072 // Add a symbol to represent the function.
73 const Function *F = MF.getFunction();
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000074 ELFSym *FnSym = ELFSym::getGV(F, EW.getGlobalELFBinding(F), ELFSym::STT_FUNC,
75 EW.getGlobalELFVisibility(F));
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000076 FnSym->SectionIdx = ES->SectionIdx;
77 FnSym->Size = ES->getCurrentPCOffset()-FnStartOff;
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +000078 EW.AddPendingGlobalSymbol(F, true);
Bruno Cardoso Lopes68491c12009-07-21 06:51:32 +000079
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000080 // Offset from start of Section
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000081 FnSym->Value = FnStartOff;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000082
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000083 if (!F->hasPrivateLinkage())
84 EW.SymbolList.push_back(FnSym);
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000085
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +000086 // Patch up Jump Table Section relocations to use the real MBBs offsets
87 // now that the MBB label offsets inside the function are known.
Chris Lattnerb1e80392010-01-25 23:22:00 +000088 if (MF.getJumpTableInfo()) {
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +000089 ELFSection &JTSection = EW.getJumpTableSection();
90 for (std::vector<MachineRelocation>::iterator MRI = JTRelocations.begin(),
91 MRE = JTRelocations.end(); MRI != MRE; ++MRI) {
92 MachineRelocation &MR = *MRI;
93 unsigned MBBOffset = getMachineBasicBlockAddress(MR.getBasicBlock());
94 MR.setResultPointer((void*)MBBOffset);
95 MR.setConstantVal(ES->SectionIdx);
96 JTSection.addRelocation(MR);
97 }
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +000098 }
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +000099
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000100 // If we have emitted any relocations to function-specific objects such as
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000101 // basic blocks, constant pools entries, or jump tables, record their
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000102 // addresses now so that we can rewrite them with the correct addresses later
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000103 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
104 MachineRelocation &MR = Relocations[i];
105 intptr_t Addr;
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000106 if (MR.isGlobalValue()) {
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +0000107 EW.AddPendingGlobalSymbol(MR.getGlobalValue());
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000108 } else if (MR.isExternalSymbol()) {
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +0000109 EW.AddPendingExternalSymbol(MR.getExternalSymbol());
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000110 } else if (MR.isBasicBlock()) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000111 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
112 MR.setConstantVal(ES->SectionIdx);
113 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000114 } else if (MR.isConstantPoolIndex()) {
115 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
116 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
117 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000118 } else if (MR.isJumpTableIndex()) {
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000119 ELFSection &JTSection = EW.getJumpTableSection();
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000120 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +0000121 MR.setConstantVal(JTSection.SectionIdx);
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000122 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000123 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000124 llvm_unreachable("Unhandled relocation type");
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000125 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000126 ES->addRelocation(MR);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000127 }
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000128
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000129 // Clear per-function data structures.
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +0000130 JTRelocations.clear();
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000131 Relocations.clear();
132 CPLocations.clear();
133 CPSections.clear();
134 JTLocations.clear();
135 MBBLocations.clear();
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000136 return false;
137}
138
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000139/// emitConstantPool - For each constant pool entry, figure out which section
140/// the constant should live in and emit the constant
141void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
142 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
143 if (CP.empty()) return;
144
145 // TODO: handle PIC codegen
146 assert(TM.getRelocationModel() != Reloc::PIC_ &&
147 "PIC codegen not yet handled for elf constant pools!");
148
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000149 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
150 MachineConstantPoolEntry CPE = CP[i];
151
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000152 // Record the constant pool location and the section index
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000153 ELFSection &CstPool = EW.getConstantPoolSection(CPE);
154 CPLocations.push_back(CstPool.size());
155 CPSections.push_back(CstPool.SectionIdx);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000156
157 if (CPE.isMachineConstantPoolEntry())
158 assert("CPE.isMachineConstantPoolEntry not supported yet");
159
160 // Emit the constant to constant pool section
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000161 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPool);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000162 }
163}
164
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000165/// emitJumpTables - Emit all the jump tables for a given jump table info
166/// record to the appropriate section.
167void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
168 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
169 if (JT.empty()) return;
170
171 // FIXME: handle PIC codegen
172 assert(TM.getRelocationModel() != Reloc::PIC_ &&
173 "PIC codegen not yet handled for elf jump tables!");
174
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000175 const TargetELFWriterInfo *TEW = TM.getELFWriterInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +0000176 unsigned EntrySize = 4; //MJTI->getEntrySize();
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000177
178 // Get the ELF Section to emit the jump table
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000179 ELFSection &JTSection = EW.getJumpTableSection();
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000180
181 // For each JT, record its offset from the start of the section
182 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
183 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
184
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000185 // Record JT 'i' offset in the JT section
186 JTLocations.push_back(JTSection.size());
187
188 // Each MBB entry in the Jump table section has a relocation entry
189 // against the current text section.
190 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
Bruno Cardoso Lopes617dd7b2009-07-18 20:52:11 +0000191 unsigned MachineRelTy = TEW->getAbsoluteLabelMachineRelTy();
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000192 MachineRelocation MR =
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +0000193 MachineRelocation::getBB(JTSection.size(), MachineRelTy, MBBs[mi]);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000194
195 // Add the relocation to the Jump Table section
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +0000196 JTRelocations.push_back(MR);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000197
198 // Output placeholder for MBB in the JT section
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +0000199 for (unsigned s=0; s < EntrySize; ++s)
200 JTSection.emitByte(0);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000201 }
202 }
203}
204
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000205} // end namespace llvm