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"
28 #include "llvm/Support/raw_ostream.h"
30 //===----------------------------------------------------------------------===//
31 // ELFCodeEmitter Implementation
32 //===----------------------------------------------------------------------===//
36 /// startFunction - This callback is invoked when a new machine function is
37 /// about to be emitted.
38 void ELFCodeEmitter::startFunction(MachineFunction &MF) {
39 DEBUG(errs() << "processing function: "
40 << MF.getFunction()->getName() << "\n");
42 // Get the ELF Section that this function belongs in.
43 ES = &EW.getTextSection();
45 // Set the desired binary object to be used by the code emitters
48 // Get the function alignment in bytes
49 unsigned Align = (1 << MF.getAlignment());
51 // The function must start on its required alignment
52 ES->emitAlignment(Align);
54 // Update the section alignment if needed.
55 if (ES->Align < Align) ES->Align = Align;
57 // Record the function start offset
58 FnStartOff = ES->getCurrentPCOffset();
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());
67 /// finishFunction - This callback is invoked after the function is completely
69 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
70 // Add a symbol to represent the function.
71 const Function *F = MF.getFunction();
72 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;
79 // 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);
83 // Offset from start of Section
84 FnSym->Value = FnStartOff;
86 if (!F->hasPrivateLinkage())
87 EW.SymbolList.push_back(FnSym);
89 // 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);
103 // If we have emitted any relocations to function-specific objects such as
104 // basic blocks, constant pools entries, or jump tables, record their
105 // addresses now so that we can rewrite them with the correct addresses
107 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
108 MachineRelocation &MR = Relocations[i];
110 if (MR.isGlobalValue()) {
111 EW.PendingGlobals.insert(MR.getGlobalValue());
112 } else if (MR.isBasicBlock()) {
113 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
114 MR.setConstantVal(ES->SectionIdx);
115 MR.setResultPointer((void*)Addr);
116 } else if (MR.isConstantPoolIndex()) {
117 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
118 MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
119 MR.setResultPointer((void*)Addr);
120 } else if (MR.isJumpTableIndex()) {
121 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
122 MR.setConstantVal(JTSection.SectionIdx);
123 MR.setResultPointer((void*)Addr);
125 llvm_unreachable("Unhandled relocation type");
127 ES->addRelocation(MR);
130 // Clear per-function data structures.
131 JTRelocations.clear();
136 MBBLocations.clear();
140 /// emitConstantPool - For each constant pool entry, figure out which section
141 /// the constant should live in and emit the constant
142 void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
143 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
144 if (CP.empty()) return;
146 // TODO: handle PIC codegen
147 assert(TM.getRelocationModel() != Reloc::PIC_ &&
148 "PIC codegen not yet handled for elf constant pools!");
150 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
151 MachineConstantPoolEntry CPE = CP[i];
153 // Record the constant pool location and the section index
154 ELFSection &CstPool = EW.getConstantPoolSection(CPE);
155 CPLocations.push_back(CstPool.size());
156 CPSections.push_back(CstPool.SectionIdx);
158 if (CPE.isMachineConstantPoolEntry())
159 assert("CPE.isMachineConstantPoolEntry not supported yet");
161 // Emit the constant to constant pool section
162 EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPool);
166 /// emitJumpTables - Emit all the jump tables for a given jump table info
167 /// record to the appropriate section.
168 void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
169 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
170 if (JT.empty()) return;
172 // FIXME: handle PIC codegen
173 assert(TM.getRelocationModel() != Reloc::PIC_ &&
174 "PIC codegen not yet handled for elf jump tables!");
176 const TargetELFWriterInfo *TEW = TM.getELFWriterInfo();
177 unsigned EntrySize = MJTI->getEntrySize();
179 // Get the ELF Section to emit the jump table
180 ELFSection &JTSection = EW.getJumpTableSection();
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;
186 // Record JT 'i' offset in the JT section
187 JTLocations.push_back(JTSection.size());
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) {
192 unsigned MachineRelTy = TEW->getAbsoluteLabelMachineRelTy();
193 MachineRelocation MR =
194 MachineRelocation::getBB(JTSection.size(), MachineRelTy, MBBs[mi]);
196 // Add the relocation to the Jump Table section
197 JTRelocations.push_back(MR);
199 // Output placeholder for MBB in the JT section
200 for (unsigned s=0; s < EntrySize; ++s)
201 JTSection.emitByte(0);
206 } // end namespace llvm