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