blob: 1c27428cc33667144726960ca959e5545d520093 [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"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000028#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000029
30//===----------------------------------------------------------------------===//
31// ELFCodeEmitter Implementation
32//===----------------------------------------------------------------------===//
33
34namespace llvm {
35
36/// startFunction - This callback is invoked when a new machine function is
37/// about to be emitted.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000038void ELFCodeEmitter::startFunction(MachineFunction &MF) {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000039 DEBUG(errs() << "processing function: "
40 << MF.getFunction()->getName() << "\n");
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000041
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000042 // Get the ELF Section that this function belongs in.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000043 ES = &EW.getTextSection();
44
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000045 // Set the desired binary object to be used by the code emitters
46 setBinaryObject(ES);
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000047
Bruno Cardoso Lopes3d62a412009-07-02 02:13:13 +000048 // Get the function alignment in bytes
49 unsigned Align = (1 << MF.getAlignment());
50
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000051 // The function must start on its required alignment
52 ES->emitAlignment(Align);
53
54 // Update the section alignment if needed.
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000055 if (ES->Align < Align) ES->Align = Align;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000056
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000057 // Record the function start offset
58 FnStartOff = ES->getCurrentPCOffset();
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +000059
60 // Emit constant pool and jump tables to their appropriate sections.
61 // They need to be emitted before the function because in some targets
62 // the later may reference JT or CP entry address.
63 emitConstantPool(MF.getConstantPool());
64 emitJumpTables(MF.getJumpTableInfo());
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000065}
66
67/// finishFunction - This callback is invoked after the function is completely
68/// finished.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000069bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000070 // Add a symbol to represent the function.
71 const Function *F = MF.getFunction();
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000072 ELFSym *FnSym = new ELFSym(F);
73 FnSym->setType(ELFSym::STT_FUNC);
74 FnSym->setBind(EW.getGlobalELFBinding(F));
75 FnSym->setVisibility(EW.getGlobalELFVisibility(F));
76 FnSym->SectionIdx = ES->SectionIdx;
77 FnSym->Size = ES->getCurrentPCOffset()-FnStartOff;
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000078
Bruno Cardoso Lopes68491c12009-07-21 06:51:32 +000079 // keep track of the emitted function leaving its symbol index as zero
80 // to be patched up later when emitting the symbol table
81 EW.setGlobalSymLookup(F, 0);
82
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000083 // Offset from start of Section
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000084 FnSym->Value = FnStartOff;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000085
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +000086 if (!F->hasPrivateLinkage())
87 EW.SymbolList.push_back(FnSym);
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000088
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +000089 // Patch up Jump Table Section relocations to use the real MBBs offsets
90 // now that the MBB label offsets inside the function are known.
91 ELFSection &JTSection = EW.getJumpTableSection();
92 for (std::vector<MachineRelocation>::iterator MRI = JTRelocations.begin(),
93 MRE = JTRelocations.end(); MRI != MRE; ++MRI) {
94 MachineRelocation &MR = *MRI;
95 unsigned MBBOffset = getMachineBasicBlockAddress(MR.getBasicBlock());
96 MR.setResultPointer((void*)MBBOffset);
97 MR.setConstantVal(ES->SectionIdx);
98 JTSection.addRelocation(MR);
99 }
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000100
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000101 // Relocations
102 // -----------
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000103 // If we have emitted any relocations to function-specific objects such as
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000104 // basic blocks, constant pools entries, or jump tables, record their
105 // addresses now so that we can rewrite them with the correct addresses
106 // later.
107 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
108 MachineRelocation &MR = Relocations[i];
109 intptr_t Addr;
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000110 if (MR.isGlobalValue()) {
111 EW.PendingGlobals.insert(MR.getGlobalValue());
112 } else if (MR.isBasicBlock()) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000113 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
114 MR.setConstantVal(ES->SectionIdx);
115 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000116 } else if (MR.isConstantPoolIndex()) {
117 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
118 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
119 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000120 } else if (MR.isJumpTableIndex()) {
121 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +0000122 MR.setConstantVal(JTSection.SectionIdx);
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000123 MR.setResultPointer((void*)Addr);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000124 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000125 llvm_unreachable("Unhandled relocation type");
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000126 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000127 ES->addRelocation(MR);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000128 }
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000129
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000130 // Clear per-function data structures.
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +0000131 JTRelocations.clear();
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000132 Relocations.clear();
133 CPLocations.clear();
134 CPSections.clear();
135 JTLocations.clear();
136 MBBLocations.clear();
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000137 return false;
138}
139
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000140/// emitConstantPool - For each constant pool entry, figure out which section
141/// the constant should live in and emit the constant
142void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
143 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
144 if (CP.empty()) return;
145
146 // TODO: handle PIC codegen
147 assert(TM.getRelocationModel() != Reloc::PIC_ &&
148 "PIC codegen not yet handled for elf constant pools!");
149
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000150 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
151 MachineConstantPoolEntry CPE = CP[i];
152
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000153 // Record the constant pool location and the section index
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000154 ELFSection &CstPool = EW.getConstantPoolSection(CPE);
155 CPLocations.push_back(CstPool.size());
156 CPSections.push_back(CstPool.SectionIdx);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000157
158 if (CPE.isMachineConstantPoolEntry())
159 assert("CPE.isMachineConstantPoolEntry not supported yet");
160
161 // Emit the constant to constant pool section
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000162 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPool);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000163 }
164}
165
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000166/// emitJumpTables - Emit all the jump tables for a given jump table info
167/// record to the appropriate section.
168void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
169 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
170 if (JT.empty()) return;
171
172 // FIXME: handle PIC codegen
173 assert(TM.getRelocationModel() != Reloc::PIC_ &&
174 "PIC codegen not yet handled for elf jump tables!");
175
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000176 const TargetELFWriterInfo *TEW = TM.getELFWriterInfo();
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +0000177 unsigned EntrySize = MJTI->getEntrySize();
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000178
179 // Get the ELF Section to emit the jump table
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000180 ELFSection &JTSection = EW.getJumpTableSection();
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000181
182 // For each JT, record its offset from the start of the section
183 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
184 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
185
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000186 // Record JT 'i' offset in the JT section
187 JTLocations.push_back(JTSection.size());
188
189 // Each MBB entry in the Jump table section has a relocation entry
190 // against the current text section.
191 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
Bruno Cardoso Lopes617dd7b2009-07-18 20:52:11 +0000192 unsigned MachineRelTy = TEW->getAbsoluteLabelMachineRelTy();
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000193 MachineRelocation MR =
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +0000194 MachineRelocation::getBB(JTSection.size(), MachineRelTy, MBBs[mi]);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000195
196 // Add the relocation to the Jump Table section
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +0000197 JTRelocations.push_back(MR);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000198
199 // Output placeholder for MBB in the JT section
Bruno Cardoso Lopes82a70cc2009-07-21 23:13:26 +0000200 for (unsigned s=0; s < EntrySize; ++s)
201 JTSection.emitByte(0);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000202 }
203 }
204}
205
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000206} // end namespace llvm