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();
58 // Emit constant pool and jump tables to their appropriate sections.
59 // They need to be emitted before the function because in some targets
60 // the later may reference JT or CP entry address.
61 emitConstantPool(MF.getConstantPool());
62 emitJumpTables(MF.getJumpTableInfo());
65 /// finishFunction - This callback is invoked after the function is completely
67 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
68 // Add a symbol to represent the function.
69 const Function *F = MF.getFunction();
70 ELFSym *FnSym = new ELFSym(F);
71 FnSym->setType(ELFSym::STT_FUNC);
72 FnSym->setBind(EW.getGlobalELFBinding(F));
73 FnSym->setVisibility(EW.getGlobalELFVisibility(F));
74 FnSym->SectionIdx = ES->SectionIdx;
75 FnSym->Size = ES->getCurrentPCOffset()-FnStartOff;
77 // keep track of the emitted function leaving its symbol index as zero
78 // to be patched up later when emitting the symbol table
79 EW.setGlobalSymLookup(F, 0);
81 // Offset from start of Section
82 FnSym->Value = FnStartOff;
84 if (!F->hasPrivateLinkage())
85 EW.SymbolList.push_back(FnSym);
87 // Patch up Jump Table Section relocations to use the real MBBs offsets
88 // now that the MBB label offsets inside the function are known.
89 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);
101 // If we have emitted any relocations to function-specific objects such as
102 // basic blocks, constant pools entries, or jump tables, record their
103 // addresses now so that we can rewrite them with the correct addresses
105 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
106 MachineRelocation &MR = Relocations[i];
108 if (MR.isGlobalValue()) {
109 EW.PendingGlobals.insert(MR.getGlobalValue());
110 } else if (MR.isBasicBlock()) {
111 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
112 MR.setConstantVal(ES->SectionIdx);
113 MR.setResultPointer((void*)Addr);
114 } else if (MR.isConstantPoolIndex()) {
115 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
116 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
117 MR.setResultPointer((void*)Addr);
118 } else if (MR.isJumpTableIndex()) {
119 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
120 MR.setConstantVal(JTSection.SectionIdx);
121 MR.setResultPointer((void*)Addr);
123 llvm_unreachable("Unhandled relocation type");
125 ES->addRelocation(MR);
128 // Clear per-function data structures.
129 JTRelocations.clear();
134 MBBLocations.clear();
138 /// emitConstantPool - For each constant pool entry, figure out which section
139 /// the constant should live in and emit the constant
140 void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
141 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
142 if (CP.empty()) return;
144 // TODO: handle PIC codegen
145 assert(TM.getRelocationModel() != Reloc::PIC_ &&
146 "PIC codegen not yet handled for elf constant pools!");
148 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
149 MachineConstantPoolEntry CPE = CP[i];
151 // Record the constant pool location and the section index
152 ELFSection &CstPool = EW.getConstantPoolSection(CPE);
153 CPLocations.push_back(CstPool.size());
154 CPSections.push_back(CstPool.SectionIdx);
156 if (CPE.isMachineConstantPoolEntry())
157 assert("CPE.isMachineConstantPoolEntry not supported yet");
159 // Emit the constant to constant pool section
160 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPool);
164 /// emitJumpTables - Emit all the jump tables for a given jump table info
165 /// record to the appropriate section.
166 void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
167 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
168 if (JT.empty()) return;
170 // FIXME: handle PIC codegen
171 assert(TM.getRelocationModel() != Reloc::PIC_ &&
172 "PIC codegen not yet handled for elf jump tables!");
174 const TargetELFWriterInfo *TEW = TM.getELFWriterInfo();
175 unsigned EntrySize = MJTI->getEntrySize();
177 // Get the ELF Section to emit the jump table
178 ELFSection &JTSection = EW.getJumpTableSection();
180 // For each JT, record its offset from the start of the section
181 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
182 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
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 unsigned MachineRelTy = TEW->getAbsoluteLabelMachineRelTy();
191 MachineRelocation MR =
192 MachineRelocation::getBB(JTSection.size(), MachineRelTy, MBBs[mi]);
194 // Add the relocation to the Jump Table section
195 JTRelocations.push_back(MR);
197 // Output placeholder for MBB in the JT section
198 for (unsigned s=0; s < EntrySize; ++s)
199 JTSection.emitByte(0);
204 } // end namespace llvm