1 //===-- lib/CodeGen/ELFCodeEmitter.cpp ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "elfce"
13 #include "ELFWriter.h"
14 #include "ELFCodeEmitter.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/BinaryObject.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineJumpTableInfo.h"
21 #include "llvm/CodeGen/MachineRelocation.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetAsmInfo.h"
25 #include "llvm/Support/Debug.h"
27 //===----------------------------------------------------------------------===//
28 // ELFCodeEmitter Implementation
29 //===----------------------------------------------------------------------===//
33 /// startFunction - This callback is invoked when a new machine function is
34 /// about to be emitted.
35 void ELFCodeEmitter::startFunction(MachineFunction &MF) {
36 DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
38 // Get the ELF Section that this function belongs in.
39 ES = &EW.getTextSection();
41 // Set the desired binary object to be used by the code emitters
44 // Get the function alignment in bytes
45 unsigned Align = (1 << MF.getAlignment());
47 // The function must start on its required alignment
48 ES->emitAlignment(Align);
50 // Update the section alignment if needed.
51 if (ES->Align < Align) ES->Align = Align;
53 // Record the function start offset
54 FnStartOff = ES->getCurrentPCOffset();
57 /// finishFunction - This callback is invoked after the function is completely
59 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
60 // Add a symbol to represent the function.
61 const Function *F = MF.getFunction();
63 FnSym.setType(ELFSym::STT_FUNC);
64 FnSym.setBind(EW.getGlobalELFLinkage(F));
65 FnSym.setVisibility(EW.getGlobalELFVisibility(F));
66 FnSym.SectionIdx = ES->SectionIdx;
67 FnSym.Size = ES->getCurrentPCOffset()-FnStartOff;
69 // Offset from start of Section
70 FnSym.Value = FnStartOff;
72 // 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);
77 EW.SymbolList.push_back(FnSym);
80 // Emit constant pool to appropriate section(s)
81 emitConstantPool(MF.getConstantPool());
83 // Emit jump tables to appropriate section
84 emitJumpTables(MF.getJumpTableInfo());
88 // If we have emitted any relocations to function-specific objects such as
89 // basic blocks, constant pools entries, or jump tables, record their
90 // addresses now so that we can rewrite them with the correct addresses
92 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
93 MachineRelocation &MR = Relocations[i];
95 if (MR.isGlobalValue()) {
96 EW.PendingGlobals.insert(MR.getGlobalValue());
97 } else if (MR.isBasicBlock()) {
98 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
99 MR.setConstantVal(ES->SectionIdx);
100 MR.setResultPointer((void*)Addr);
101 } else if (MR.isConstantPoolIndex()) {
102 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
103 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
104 MR.setResultPointer((void*)Addr);
105 } else if (MR.isJumpTableIndex()) {
106 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
107 MR.setResultPointer((void*)Addr);
108 MR.setConstantVal(JumpTableSectionIdx);
110 assert(0 && "Unhandled relocation type");
112 ES->addRelocation(MR);
115 // Clear per-function data structures.
120 MBBLocations.clear();
124 /// emitConstantPool - For each constant pool entry, figure out which section
125 /// the constant should live in and emit the constant
126 void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
127 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
128 if (CP.empty()) return;
130 // TODO: handle PIC codegen
131 assert(TM.getRelocationModel() != Reloc::PIC_ &&
132 "PIC codegen not yet handled for elf constant pools!");
134 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
135 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
136 MachineConstantPoolEntry CPE = CP[i];
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());
144 // Record the constant pool location and the section index
145 CPLocations.push_back(CstPoolSection.size());
146 CPSections.push_back(CstPoolSection.SectionIdx);
148 if (CPE.isMachineConstantPoolEntry())
149 assert("CPE.isMachineConstantPoolEntry not supported yet");
151 // Emit the constant to constant pool section
152 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection);
156 /// emitJumpTables - Emit all the jump tables for a given jump table info
157 /// record to the appropriate section.
158 void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
159 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
160 if (JT.empty()) return;
162 // FIXME: handle PIC codegen
163 assert(TM.getRelocationModel() != Reloc::PIC_ &&
164 "PIC codegen not yet handled for elf jump tables!");
166 const TargetAsmInfo *TAI = TM.getTargetAsmInfo();
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;
174 // Entries in the JT Section are relocated against the text section
175 ELFSection &TextSection = EW.getTextSection();
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;
181 DOUT << "JTSection.size(): " << JTSection.size() << "\n";
182 DOUT << "JTLocations.size: " << JTLocations.size() << "\n";
184 // Record JT 'i' offset in the JT section
185 JTLocations.push_back(JTSection.size());
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,
195 // Offset of JT 'i' in JT section
196 MR.setResultPointer((void*)getMachineBasicBlockAddress(MBBs[mi]));
197 MR.setConstantVal(TextSection.SectionIdx);
199 // Add the relocation to the Jump Table section
200 JTSection.addRelocation(MR);
202 // Output placeholder for MBB in the JT section
203 JTSection.emitWord(0);
208 } // end namespace llvm