LLVM API Documentation

ELFWriter.cpp

Go to the documentation of this file.
00001 //===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by Chris Lattner and is distributed under the
00006 // University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the target-independent ELF writer.  This file writes out
00011 // the ELF file in the following order:
00012 //
00013 //  #1. ELF Header
00014 //  #2. '.text' section
00015 //  #3. '.data' section
00016 //  #4. '.bss' section  (conceptual position in file)
00017 //  ...
00018 //  #X. '.shstrtab' section
00019 //  #Y. Section Table
00020 //
00021 // The entries in the section table are laid out as:
00022 //  #0. Null entry [required]
00023 //  #1. ".text" entry - the program code
00024 //  #2. ".data" entry - global variables with initializers.     [ if needed ]
00025 //  #3. ".bss" entry  - global variables without initializers.  [ if needed ]
00026 //  ...
00027 //  #N. ".shstrtab" entry - String table for the section names.
00028 //
00029 // NOTE: This code should eventually be extended to support 64-bit ELF (this
00030 // won't be hard), but we haven't done so yet!
00031 //
00032 //===----------------------------------------------------------------------===//
00033 
00034 #include "llvm/CodeGen/ELFWriter.h"
00035 #include "llvm/Module.h"
00036 #include "llvm/CodeGen/MachineCodeEmitter.h"
00037 #include "llvm/CodeGen/MachineConstantPool.h"
00038 #include "llvm/Target/TargetMachine.h"
00039 #include "llvm/Support/Mangler.h"
00040 #include <iostream>
00041 using namespace llvm;
00042 
00043 //===----------------------------------------------------------------------===//
00044 //                       ELFCodeEmitter Implementation
00045 //===----------------------------------------------------------------------===//
00046 
00047 namespace llvm {
00048   /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
00049   /// functions to the ELF file.
00050   class ELFCodeEmitter : public MachineCodeEmitter {
00051     ELFWriter &EW;
00052     ELFWriter::ELFSection *ES;  // Section to write to.
00053     std::vector<unsigned char> *OutBuffer;
00054     size_t FnStart;
00055   public:
00056     ELFCodeEmitter(ELFWriter &ew) : EW(ew), OutBuffer(0) {}
00057 
00058     void startFunction(MachineFunction &F);
00059     void finishFunction(MachineFunction &F);
00060 
00061     void emitConstantPool(MachineConstantPool *MCP) {
00062       if (MCP->isEmpty()) return;
00063       assert(0 && "unimp");
00064     }
00065     virtual void emitByte(unsigned char B) {
00066       OutBuffer->push_back(B);
00067     }
00068     virtual void emitWordAt(unsigned W, unsigned *Ptr) {
00069       assert(0 && "ni");
00070     }
00071     virtual void emitWord(unsigned W) {
00072       assert(0 && "ni");
00073     }
00074     virtual uint64_t getCurrentPCValue() {
00075       return OutBuffer->size();
00076     }
00077     virtual uint64_t getCurrentPCOffset() {
00078       return OutBuffer->size()-FnStart;
00079     }
00080     void addRelocation(const MachineRelocation &MR) {
00081       assert(0 && "relo not handled yet!");
00082     }
00083     virtual uint64_t getConstantPoolEntryAddress(unsigned Index) {
00084       assert(0 && "CP not implementated yet!");
00085       return 0;
00086     }
00087 
00088     virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment) {
00089       assert(0 && "Globals not implemented yet!");
00090       return 0;
00091     }
00092 
00093     /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
00094     void startFunctionStub(unsigned StubSize) {
00095       assert(0 && "JIT specific function called!");
00096       abort();
00097     }
00098     void *finishFunctionStub(const Function *F) {
00099       assert(0 && "JIT specific function called!");
00100       abort();
00101       return 0;
00102     }
00103   };
00104 }
00105 
00106 /// startFunction - This callback is invoked when a new machine function is
00107 /// about to be emitted.
00108 void ELFCodeEmitter::startFunction(MachineFunction &F) {
00109   // Align the output buffer to the appropriate alignment.
00110   unsigned Align = 16;   // FIXME: GENERICIZE!!
00111   // Get the ELF Section that this function belongs in.
00112   ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
00113                       ELFWriter::ELFSection::SHF_EXECINSTR |
00114                       ELFWriter::ELFSection::SHF_ALLOC);
00115   OutBuffer = &ES->SectionData;
00116 
00117   // Upgrade the section alignment if required.
00118   if (ES->Align < Align) ES->Align = Align;
00119 
00120   // Add padding zeros to the end of the buffer to make sure that the
00121   // function will start on the correct byte alignment within the section.
00122   size_t SectionOff = OutBuffer->size();
00123   ELFWriter::align(*OutBuffer, Align);
00124 
00125   FnStart = OutBuffer->size();
00126 }
00127 
00128 /// finishFunction - This callback is invoked after the function is completely
00129 /// finished.
00130 void ELFCodeEmitter::finishFunction(MachineFunction &F) {
00131   // We now know the size of the function, add a symbol to represent it.
00132   ELFWriter::ELFSym FnSym(F.getFunction());
00133 
00134   // Figure out the binding (linkage) of the symbol.
00135   switch (F.getFunction()->getLinkage()) {
00136   default:
00137     // appending linkage is illegal for functions.
00138     assert(0 && "Unknown linkage type!");
00139   case GlobalValue::ExternalLinkage:
00140     FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
00141     break;
00142   case GlobalValue::LinkOnceLinkage:
00143   case GlobalValue::WeakLinkage:
00144     FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
00145     break;
00146   case GlobalValue::InternalLinkage:
00147     FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
00148     break;
00149   }
00150 
00151   ES->Size = OutBuffer->size();
00152 
00153   FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
00154   FnSym.SectionIdx = ES->SectionIdx;
00155     FnSym.Value = FnStart;   // Value = Offset from start of Section.
00156   FnSym.Size = OutBuffer->size()-FnStart;
00157 
00158   // Finally, add it to the symtab.
00159   EW.SymbolTable.push_back(FnSym);
00160 }
00161 
00162 //===----------------------------------------------------------------------===//
00163 //                          ELFWriter Implementation
00164 //===----------------------------------------------------------------------===//
00165 
00166 ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
00167   e_machine = 0;  // e_machine defaults to 'No Machine'
00168   e_flags = 0;    // e_flags defaults to 0, no flags.
00169 
00170   is64Bit = TM.getTargetData().getPointerSizeInBits() == 64;
00171   isLittleEndian = TM.getTargetData().isLittleEndian();
00172 
00173   // Create the machine code emitter object for this target.
00174   MCE = new ELFCodeEmitter(*this);
00175   NumSections = 0;
00176 }
00177 
00178 ELFWriter::~ELFWriter() {
00179   delete MCE;
00180 }
00181 
00182 // doInitialization - Emit the file header and all of the global variables for
00183 // the module to the ELF file.
00184 bool ELFWriter::doInitialization(Module &M) {
00185   Mang = new Mangler(M);
00186 
00187   // Local alias to shortenify coming code.
00188   std::vector<unsigned char> &FH = FileHeader;
00189 
00190   outbyte(FH, 0x7F);                     // EI_MAG0
00191   outbyte(FH, 'E');                      // EI_MAG1
00192   outbyte(FH, 'L');                      // EI_MAG2
00193   outbyte(FH, 'F');                      // EI_MAG3
00194   outbyte(FH, is64Bit ? 2 : 1);          // EI_CLASS
00195   outbyte(FH, isLittleEndian ? 1 : 2);   // EI_DATA
00196   outbyte(FH, 1);                        // EI_VERSION
00197   FH.resize(16);                         // EI_PAD up to 16 bytes.
00198 
00199   // This should change for shared objects.
00200   outhalf(FH, 1);                 // e_type = ET_REL
00201   outhalf(FH, e_machine);         // e_machine = whatever the target wants
00202   outword(FH, 1);                 // e_version = 1
00203   outaddr(FH, 0);                 // e_entry = 0 -> no entry point in .o file
00204   outaddr(FH, 0);                 // e_phoff = 0 -> no program header for .o
00205 
00206   ELFHeader_e_shoff_Offset = FH.size();
00207   outaddr(FH, 0);                 // e_shoff
00208   outword(FH, e_flags);           // e_flags = whatever the target wants
00209 
00210   outhalf(FH, is64Bit ? 64 : 52); // e_ehsize = ELF header size
00211   outhalf(FH, 0);                 // e_phentsize = prog header entry size
00212   outhalf(FH, 0);                 // e_phnum     = # prog header entries = 0
00213   outhalf(FH, is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
00214 
00215 
00216   ELFHeader_e_shnum_Offset = FH.size();
00217   outhalf(FH, 0);                 // e_shnum     = # of section header ents
00218   ELFHeader_e_shstrndx_Offset = FH.size();
00219   outhalf(FH, 0);                 // e_shstrndx  = Section # of '.shstrtab'
00220 
00221   // Add the null section, which is required to be first in the file.
00222   getSection("", 0, 0);
00223 
00224   // Start up the symbol table.  The first entry in the symtab is the null
00225   // entry.
00226   SymbolTable.push_back(ELFSym(0));
00227 
00228   return false;
00229 }
00230 
00231 void ELFWriter::EmitGlobal(GlobalVariable *GV) {
00232   // If this is an external global, emit it now.  TODO: Note that it would be
00233   // better to ignore the symbol here and only add it to the symbol table if
00234   // referenced.
00235   if (!GV->hasInitializer()) {
00236     ELFSym ExternalSym(GV);
00237     ExternalSym.SetBind(ELFSym::STB_GLOBAL);
00238     ExternalSym.SetType(ELFSym::STT_NOTYPE);
00239     ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
00240     SymbolTable.push_back(ExternalSym);
00241     return;
00242   }
00243 
00244   const Type *GVType = (const Type*)GV->getType();
00245   unsigned Align = TM.getTargetData().getTypeAlignment(GVType);
00246   unsigned Size  = TM.getTargetData().getTypeSize(GVType);
00247 
00248   // If this global has a zero initializer, it is part of the .bss or common
00249   // section.
00250   if (GV->getInitializer()->isNullValue()) {
00251     // If this global is part of the common block, add it now.  Variables are
00252     // part of the common block if they are zero initialized and allowed to be
00253     // merged with other symbols.
00254     if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
00255       ELFSym CommonSym(GV);
00256       // Value for common symbols is the alignment required.
00257       CommonSym.Value = Align;
00258       CommonSym.Size  = Size;
00259       CommonSym.SetBind(ELFSym::STB_GLOBAL);
00260       CommonSym.SetType(ELFSym::STT_OBJECT);
00261       // TODO SOMEDAY: add ELF visibility.
00262       CommonSym.SectionIdx = ELFSection::SHN_COMMON;
00263       SymbolTable.push_back(CommonSym);
00264       return;
00265     }
00266 
00267     // Otherwise, this symbol is part of the .bss section.  Emit it now.
00268 
00269     // Handle alignment.  Ensure section is aligned at least as much as required
00270     // by this symbol.
00271     ELFSection &BSSSection = getBSSSection();
00272     BSSSection.Align = std::max(BSSSection.Align, Align);
00273 
00274     // Within the section, emit enough virtual padding to get us to an alignment
00275     // boundary.
00276     if (Align)
00277       BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
00278 
00279     ELFSym BSSSym(GV);
00280     BSSSym.Value = BSSSection.Size;
00281     BSSSym.Size = Size;
00282     BSSSym.SetType(ELFSym::STT_OBJECT);
00283 
00284     switch (GV->getLinkage()) {
00285     default:  // weak/linkonce handled above
00286       assert(0 && "Unexpected linkage type!");
00287     case GlobalValue::AppendingLinkage:  // FIXME: This should be improved!
00288     case GlobalValue::ExternalLinkage:
00289       BSSSym.SetBind(ELFSym::STB_GLOBAL);
00290       break;
00291     case GlobalValue::InternalLinkage:
00292       BSSSym.SetBind(ELFSym::STB_LOCAL);
00293       break;
00294     }
00295 
00296     // Set the idx of the .bss section
00297     BSSSym.SectionIdx = BSSSection.SectionIdx;
00298     SymbolTable.push_back(BSSSym);
00299 
00300     // Reserve space in the .bss section for this symbol.
00301     BSSSection.Size += Size;
00302     return;
00303   }
00304 
00305   // FIXME: handle .rodata
00306   //assert(!GV->isConstant() && "unimp");
00307 
00308   // FIXME: handle .data
00309   //assert(0 && "unimp");
00310 }
00311 
00312 
00313 bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
00314   // Nothing to do here, this is all done through the MCE object above.
00315   return false;
00316 }
00317 
00318 /// doFinalization - Now that the module has been completely processed, emit
00319 /// the ELF file to 'O'.
00320 bool ELFWriter::doFinalization(Module &M) {
00321   // Okay, the ELF header and .text sections have been completed, build the
00322   // .data, .bss, and "common" sections next.
00323   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
00324        I != E; ++I)
00325     EmitGlobal(I);
00326 
00327   // Emit the symbol table now, if non-empty.
00328   EmitSymbolTable();
00329 
00330   // FIXME: Emit the relocations now.
00331 
00332   // Emit the string table for the sections in the ELF file we have.
00333   EmitSectionTableStringTable();
00334 
00335   // Emit the sections to the .o file, and emit the section table for the file.
00336   OutputSectionsAndSectionTable();
00337 
00338   // We are done with the abstract symbols.
00339   SectionList.clear();
00340   NumSections = 0;
00341 
00342   // Release the name mangler object.
00343   delete Mang; Mang = 0;
00344   return false;
00345 }
00346 
00347 /// EmitSymbolTable - If the current symbol table is non-empty, emit the string
00348 /// table for it and then the symbol table itself.
00349 void ELFWriter::EmitSymbolTable() {
00350   if (SymbolTable.size() == 1) return;  // Only the null entry.
00351 
00352   // FIXME: compact all local symbols to the start of the symtab.
00353   unsigned FirstNonLocalSymbol = 1;
00354 
00355   ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
00356   StrTab.Align = 1;
00357 
00358   DataBuffer &StrTabBuf = StrTab.SectionData;
00359 
00360   // Set the zero'th symbol to a null byte, as required.
00361   outbyte(StrTabBuf, 0);
00362   SymbolTable[0].NameIdx = 0;
00363   unsigned Index = 1;
00364   for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
00365     // Use the name mangler to uniquify the LLVM symbol.
00366     std::string Name = Mang->getValueName(SymbolTable[i].GV);
00367 
00368     if (Name.empty()) {
00369       SymbolTable[i].NameIdx = 0;
00370     } else {
00371       SymbolTable[i].NameIdx = Index;
00372 
00373       // Add the name to the output buffer, including the null terminator.
00374       StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
00375 
00376       // Add a null terminator.
00377       StrTabBuf.push_back(0);
00378 
00379       // Keep track of the number of bytes emitted to this section.
00380       Index += Name.size()+1;
00381     }
00382   }
00383   assert(Index == StrTabBuf.size());
00384   StrTab.Size = Index;
00385 
00386   // Now that we have emitted the string table and know the offset into the
00387   // string table of each symbol, emit the symbol table itself.
00388   ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
00389   SymTab.Align = is64Bit ? 8 : 4;
00390   SymTab.Link = SymTab.SectionIdx;     // Section Index of .strtab.
00391   SymTab.Info = FirstNonLocalSymbol;   // First non-STB_LOCAL symbol.
00392   SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
00393   DataBuffer &SymTabBuf = SymTab.SectionData;
00394 
00395   if (!is64Bit) {   // 32-bit and 64-bit formats are shuffled a bit.
00396     for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
00397       ELFSym &Sym = SymbolTable[i];
00398       outword(SymTabBuf, Sym.NameIdx);
00399       outaddr32(SymTabBuf, Sym.Value);
00400       outword(SymTabBuf, Sym.Size);
00401       outbyte(SymTabBuf, Sym.Info);
00402       outbyte(SymTabBuf, Sym.Other);
00403       outhalf(SymTabBuf, Sym.SectionIdx);
00404     }
00405   } else {
00406     for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
00407       ELFSym &Sym = SymbolTable[i];
00408       outword(SymTabBuf, Sym.NameIdx);
00409       outbyte(SymTabBuf, Sym.Info);
00410       outbyte(SymTabBuf, Sym.Other);
00411       outhalf(SymTabBuf, Sym.SectionIdx);
00412       outaddr64(SymTabBuf, Sym.Value);
00413       outxword(SymTabBuf, Sym.Size);
00414     }
00415   }
00416 
00417   SymTab.Size = SymTabBuf.size();
00418 }
00419 
00420 /// EmitSectionTableStringTable - This method adds and emits a section for the
00421 /// ELF Section Table string table: the string table that holds all of the
00422 /// section names.
00423 void ELFWriter::EmitSectionTableStringTable() {
00424   // First step: add the section for the string table to the list of sections:
00425   ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
00426 
00427   // Now that we know which section number is the .shstrtab section, update the
00428   // e_shstrndx entry in the ELF header.
00429   fixhalf(FileHeader, SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
00430 
00431   // Set the NameIdx of each section in the string table and emit the bytes for
00432   // the string table.
00433   unsigned Index = 0;
00434   DataBuffer &Buf = SHStrTab.SectionData;
00435 
00436   for (std::list<ELFSection>::iterator I = SectionList.begin(),
00437          E = SectionList.end(); I != E; ++I) {
00438     // Set the index into the table.  Note if we have lots of entries with
00439     // common suffixes, we could memoize them here if we cared.
00440     I->NameIdx = Index;
00441 
00442     // Add the name to the output buffer, including the null terminator.
00443     Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
00444 
00445     // Add a null terminator.
00446     Buf.push_back(0);
00447 
00448     // Keep track of the number of bytes emitted to this section.
00449     Index += I->Name.size()+1;
00450   }
00451 
00452   // Set the size of .shstrtab now that we know what it is.
00453   assert(Index == Buf.size());
00454   SHStrTab.Size = Index;
00455 }
00456 
00457 /// OutputSectionsAndSectionTable - Now that we have constructed the file header
00458 /// and all of the sections, emit these to the ostream destination and emit the
00459 /// SectionTable.
00460 void ELFWriter::OutputSectionsAndSectionTable() {
00461   // Pass #1: Compute the file offset for each section.
00462   size_t FileOff = FileHeader.size();   // File header first.
00463 
00464   // Emit all of the section data in order.
00465   for (std::list<ELFSection>::iterator I = SectionList.begin(),
00466          E = SectionList.end(); I != E; ++I) {
00467     // Align FileOff to whatever the alignment restrictions of the section are.
00468     if (I->Align)
00469       FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
00470     I->Offset = FileOff;
00471     FileOff += I->SectionData.size();
00472   }
00473 
00474   // Align Section Header.
00475   unsigned TableAlign = is64Bit ? 8 : 4;
00476   FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
00477 
00478   // Now that we know where all of the sections will be emitted, set the e_shnum
00479   // entry in the ELF header.
00480   fixhalf(FileHeader, NumSections, ELFHeader_e_shnum_Offset);
00481 
00482   // Now that we know the offset in the file of the section table, update the
00483   // e_shoff address in the ELF header.
00484   fixaddr(FileHeader, FileOff, ELFHeader_e_shoff_Offset);
00485 
00486   // Now that we know all of the data in the file header, emit it and all of the
00487   // sections!
00488   O.write((char*)&FileHeader[0], FileHeader.size());
00489   FileOff = FileHeader.size();
00490   DataBuffer().swap(FileHeader);
00491 
00492   DataBuffer Table;
00493 
00494   // Emit all of the section data and build the section table itself.
00495   while (!SectionList.empty()) {
00496     const ELFSection &S = *SectionList.begin();
00497 
00498     // Align FileOff to whatever the alignment restrictions of the section are.
00499     if (S.Align)
00500       for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
00501            FileOff != NewFileOff; ++FileOff)
00502         O.put((char)0xAB);
00503     O.write((char*)&S.SectionData[0], S.SectionData.size());
00504     FileOff += S.SectionData.size();
00505 
00506     outword(Table, S.NameIdx);  // sh_name - Symbol table name idx
00507     outword(Table, S.Type);     // sh_type - Section contents & semantics
00508     outword(Table, S.Flags);    // sh_flags - Section flags.
00509     outaddr(Table, S.Addr);     // sh_addr - The mem addr this section is in.
00510     outaddr(Table, S.Offset);   // sh_offset - Offset from the file start.
00511     outword(Table, S.Size);     // sh_size - The section size.
00512     outword(Table, S.Link);     // sh_link - Section header table index link.
00513     outword(Table, S.Info);     // sh_info - Auxillary information.
00514     outword(Table, S.Align);    // sh_addralign - Alignment of section.
00515     outword(Table, S.EntSize);  // sh_entsize - Size of entries in the section.
00516 
00517     SectionList.pop_front();
00518   }
00519 
00520   // Align output for the section table.
00521   for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
00522        FileOff != NewFileOff; ++FileOff)
00523     O.put((char)0xAB);
00524 
00525   // Emit the section table itself.
00526   O.write((char*)&Table[0], Table.size());
00527 }