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/TargetELFWriterInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
29 //===----------------------------------------------------------------------===//
30 // ELFCodeEmitter Implementation
31 //===----------------------------------------------------------------------===//
35 /// startFunction - This callback is invoked when a new machine function is
36 /// about to be emitted.
37 void ELFCodeEmitter::startFunction(MachineFunction &MF) {
38 DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
40 // Get the ELF Section that this function belongs in.
41 ES = &EW.getTextSection();
43 // Set the desired binary object to be used by the code emitters
46 // Get the function alignment in bytes
47 unsigned Align = (1 << MF.getAlignment());
49 // The function must start on its required alignment
50 ES->emitAlignment(Align);
52 // Update the section alignment if needed.
53 if (ES->Align < Align) ES->Align = Align;
55 // Record the function start offset
56 FnStartOff = ES->getCurrentPCOffset();
59 /// finishFunction - This callback is invoked after the function is completely
61 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
62 // Add a symbol to represent the function.
63 const Function *F = MF.getFunction();
64 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;
71 // Offset from start of Section
72 FnSym->Value = FnStartOff;
74 if (!F->hasPrivateLinkage())
75 EW.SymbolList.push_back(FnSym);
77 // Emit constant pool to appropriate section(s)
78 emitConstantPool(MF.getConstantPool());
80 // Emit jump tables to appropriate section
81 emitJumpTables(MF.getJumpTableInfo());
85 // If we have emitted any relocations to function-specific objects such as
86 // basic blocks, constant pools entries, or jump tables, record their
87 // addresses now so that we can rewrite them with the correct addresses
89 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
90 MachineRelocation &MR = Relocations[i];
92 if (MR.isGlobalValue()) {
93 EW.PendingGlobals.insert(MR.getGlobalValue());
94 } else if (MR.isBasicBlock()) {
95 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
96 MR.setConstantVal(ES->SectionIdx);
97 MR.setResultPointer((void*)Addr);
98 } else if (MR.isConstantPoolIndex()) {
99 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
100 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
101 MR.setResultPointer((void*)Addr);
102 } else if (MR.isJumpTableIndex()) {
103 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
104 MR.setConstantVal(JumpTableSectionIdx);
105 MR.setResultPointer((void*)Addr);
107 llvm_unreachable("Unhandled relocation type");
109 ES->addRelocation(MR);
112 // Clear per-function data structures.
117 MBBLocations.clear();
121 /// emitConstantPool - For each constant pool entry, figure out which section
122 /// the constant should live in and emit the constant
123 void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
124 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
125 if (CP.empty()) return;
127 // TODO: handle PIC codegen
128 assert(TM.getRelocationModel() != Reloc::PIC_ &&
129 "PIC codegen not yet handled for elf constant pools!");
131 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
132 MachineConstantPoolEntry CPE = CP[i];
134 // Record the constant pool location and the section index
135 ELFSection &CstPool = EW.getConstantPoolSection(CPE);
136 CPLocations.push_back(CstPool.size());
137 CPSections.push_back(CstPool.SectionIdx);
139 if (CPE.isMachineConstantPoolEntry())
140 assert("CPE.isMachineConstantPoolEntry not supported yet");
142 // Emit the constant to constant pool section
143 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPool);
147 /// emitJumpTables - Emit all the jump tables for a given jump table info
148 /// record to the appropriate section.
149 void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
150 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
151 if (JT.empty()) return;
153 // FIXME: handle PIC codegen
154 assert(TM.getRelocationModel() != Reloc::PIC_ &&
155 "PIC codegen not yet handled for elf jump tables!");
157 const TargetELFWriterInfo *TEW = TM.getELFWriterInfo();
159 // Get the ELF Section to emit the jump table
160 ELFSection &JTSection = EW.getJumpTableSection();
161 JumpTableSectionIdx = JTSection.SectionIdx;
163 // Entries in the JT Section are relocated against the text section
164 ELFSection &TextSection = EW.getTextSection();
166 // For each JT, record its offset from the start of the section
167 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
168 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
170 DOUT << "JTSection.size(): " << JTSection.size() << "\n";
171 DOUT << "JTLocations.size: " << JTLocations.size() << "\n";
173 // Record JT 'i' offset in the JT section
174 JTLocations.push_back(JTSection.size());
176 // Each MBB entry in the Jump table section has a relocation entry
177 // against the current text section.
178 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
179 unsigned MachineRelTy = TEW->getAbsoluteLabelMachineRelTy();
180 MachineRelocation MR =
181 MachineRelocation::getBB(JTSection.size(),
185 // Offset of JT 'i' in JT section
186 MR.setResultPointer((void*)getMachineBasicBlockAddress(MBBs[mi]));
187 MR.setConstantVal(TextSection.SectionIdx);
189 // Add the relocation to the Jump Table section
190 JTSection.addRelocation(MR);
192 // Output placeholder for MBB in the JT section
193 JTSection.emitWord(0);
198 } // end namespace llvm