LLVM API Documentation

ELFCodeEmitter.cpp

Go to the documentation of this file.
00001 //===-- lib/CodeGen/ELFCodeEmitter.cpp ------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #define DEBUG_TYPE "elfce"
00011 
00012 #include "ELF.h"
00013 #include "ELFWriter.h"
00014 #include "ELFCodeEmitter.h"
00015 #include "llvm/Constants.h"
00016 #include "llvm/DerivedTypes.h"
00017 #include "llvm/Function.h"
00018 #include "llvm/CodeGen/BinaryObject.h"
00019 #include "llvm/CodeGen/MachineConstantPool.h"
00020 #include "llvm/CodeGen/MachineFunction.h"
00021 #include "llvm/CodeGen/MachineJumpTableInfo.h"
00022 #include "llvm/CodeGen/MachineRelocation.h"
00023 #include "llvm/Target/TargetData.h"
00024 #include "llvm/Target/TargetELFWriterInfo.h"
00025 #include "llvm/Target/TargetMachine.h"
00026 #include "llvm/Target/TargetAsmInfo.h"
00027 #include "llvm/Support/Debug.h"
00028 #include "llvm/Support/ErrorHandling.h"
00029 #include "llvm/Support/raw_ostream.h"
00030 
00031 //===----------------------------------------------------------------------===//
00032 //                       ELFCodeEmitter Implementation
00033 //===----------------------------------------------------------------------===//
00034 
00035 namespace llvm {
00036 
00037 /// startFunction - This callback is invoked when a new machine function is
00038 /// about to be emitted.
00039 void ELFCodeEmitter::startFunction(MachineFunction &MF) {
00040   DEBUG(errs() << "processing function: "
00041         << MF.getFunction()->getName() << "\n");
00042 
00043   // Get the ELF Section that this function belongs in.
00044   ES = &EW.getTextSection(MF.getFunction());
00045 
00046   // Set the desired binary object to be used by the code emitters
00047   setBinaryObject(ES);
00048 
00049   // Get the function alignment in bytes
00050   unsigned Align = (1 << MF.getAlignment());
00051 
00052   // The function must start on its required alignment
00053   ES->emitAlignment(Align);
00054 
00055   // Update the section alignment if needed.
00056   ES->Align = std::max(ES->Align, Align);
00057 
00058   // Record the function start offset
00059   FnStartOff = ES->getCurrentPCOffset();
00060 
00061   // Emit constant pool and jump tables to their appropriate sections.
00062   // They need to be emitted before the function because in some targets
00063   // the later may reference JT or CP entry address.
00064   emitConstantPool(MF.getConstantPool());
00065   emitJumpTables(MF.getJumpTableInfo());
00066 }
00067 
00068 /// finishFunction - This callback is invoked after the function is completely
00069 /// finished.
00070 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
00071   // Add a symbol to represent the function.
00072   const Function *F = MF.getFunction();
00073   ELFSym *FnSym = ELFSym::getGV(F, EW.getGlobalELFBinding(F), ELFSym::STT_FUNC,
00074                                 EW.getGlobalELFVisibility(F));
00075   FnSym->SectionIdx = ES->SectionIdx;
00076   FnSym->Size = ES->getCurrentPCOffset()-FnStartOff;
00077   EW.AddPendingGlobalSymbol(F, true);
00078 
00079   // Offset from start of Section
00080   FnSym->Value = FnStartOff;
00081 
00082   if (!F->hasPrivateLinkage())
00083     EW.SymbolList.push_back(FnSym);
00084 
00085   // Patch up Jump Table Section relocations to use the real MBBs offsets
00086   // now that the MBB label offsets inside the function are known.
00087   if (!MF.getJumpTableInfo()->isEmpty()) {
00088     ELFSection &JTSection = EW.getJumpTableSection();
00089     for (std::vector<MachineRelocation>::iterator MRI = JTRelocations.begin(),
00090          MRE = JTRelocations.end(); MRI != MRE; ++MRI) {
00091       MachineRelocation &MR = *MRI;
00092       unsigned MBBOffset = getMachineBasicBlockAddress(MR.getBasicBlock());
00093       MR.setResultPointer((void*)MBBOffset);
00094       MR.setConstantVal(ES->SectionIdx);
00095       JTSection.addRelocation(MR);
00096     }
00097   }
00098 
00099   // If we have emitted any relocations to function-specific objects such as
00100   // basic blocks, constant pools entries, or jump tables, record their
00101   // addresses now so that we can rewrite them with the correct addresses later
00102   for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
00103     MachineRelocation &MR = Relocations[i];
00104     intptr_t Addr;
00105     if (MR.isGlobalValue()) {
00106       EW.AddPendingGlobalSymbol(MR.getGlobalValue());
00107     } else if (MR.isExternalSymbol()) {
00108       EW.AddPendingExternalSymbol(MR.getExternalSymbol());
00109     } else if (MR.isBasicBlock()) {
00110       Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
00111       MR.setConstantVal(ES->SectionIdx);
00112       MR.setResultPointer((void*)Addr);
00113     } else if (MR.isConstantPoolIndex()) {
00114       Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
00115       MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
00116       MR.setResultPointer((void*)Addr);
00117     } else if (MR.isJumpTableIndex()) {
00118       ELFSection &JTSection = EW.getJumpTableSection();
00119       Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
00120       MR.setConstantVal(JTSection.SectionIdx);
00121       MR.setResultPointer((void*)Addr);
00122     } else {
00123       llvm_unreachable("Unhandled relocation type");
00124     }
00125     ES->addRelocation(MR);
00126   }
00127 
00128   // Clear per-function data structures.
00129   JTRelocations.clear();
00130   Relocations.clear();
00131   CPLocations.clear();
00132   CPSections.clear();
00133   JTLocations.clear();
00134   MBBLocations.clear();
00135   return false;
00136 }
00137 
00138 /// emitConstantPool - For each constant pool entry, figure out which section
00139 /// the constant should live in and emit the constant
00140 void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
00141   const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
00142   if (CP.empty()) return;
00143 
00144   // TODO: handle PIC codegen
00145   assert(TM.getRelocationModel() != Reloc::PIC_ &&
00146          "PIC codegen not yet handled for elf constant pools!");
00147 
00148   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
00149     MachineConstantPoolEntry CPE = CP[i];
00150 
00151     // Record the constant pool location and the section index
00152     ELFSection &CstPool = EW.getConstantPoolSection(CPE);
00153     CPLocations.push_back(CstPool.size());
00154     CPSections.push_back(CstPool.SectionIdx);
00155 
00156     if (CPE.isMachineConstantPoolEntry())
00157       assert("CPE.isMachineConstantPoolEntry not supported yet");
00158 
00159     // Emit the constant to constant pool section
00160     EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPool);
00161   }
00162 }
00163 
00164 /// emitJumpTables - Emit all the jump tables for a given jump table info
00165 /// record to the appropriate section.
00166 void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
00167   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
00168   if (JT.empty()) return;
00169 
00170   // FIXME: handle PIC codegen
00171   assert(TM.getRelocationModel() != Reloc::PIC_ &&
00172          "PIC codegen not yet handled for elf jump tables!");
00173 
00174   const TargetELFWriterInfo *TEW = TM.getELFWriterInfo();
00175   unsigned EntrySize = MJTI->getEntrySize();
00176 
00177   // Get the ELF Section to emit the jump table
00178   ELFSection &JTSection = EW.getJumpTableSection();
00179 
00180   // For each JT, record its offset from the start of the section
00181   for (unsigned i = 0, e = JT.size(); i != e; ++i) {
00182     const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
00183 
00184     // Record JT 'i' offset in the JT section
00185     JTLocations.push_back(JTSection.size());
00186 
00187     // Each MBB entry in the Jump table section has a relocation entry
00188     // against the current text section.
00189     for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
00190       unsigned MachineRelTy = TEW->getAbsoluteLabelMachineRelTy();
00191       MachineRelocation MR =
00192         MachineRelocation::getBB(JTSection.size(), MachineRelTy, MBBs[mi]);
00193 
00194       // Add the relocation to the Jump Table section
00195       JTRelocations.push_back(MR);
00196 
00197       // Output placeholder for MBB in the JT section
00198       for (unsigned s=0; s < EntrySize; ++s)
00199         JTSection.emitByte(0);
00200     }
00201   }
00202 }
00203 
00204 } // end namespace llvm