LLVM API Documentation
00001 //===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===// 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 // This file defines the ELFWriter class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef ELFWRITER_H 00015 #define ELFWRITER_H 00016 00017 #include "llvm/CodeGen/MachineFunctionPass.h" 00018 #include <list> 00019 #include <map> 00020 00021 namespace llvm { 00022 class GlobalVariable; 00023 class Mangler; 00024 class MachineCodeEmitter; 00025 class ELFCodeEmitter; 00026 class raw_ostream; 00027 00028 /// ELFWriter - This class implements the common target-independent code for 00029 /// writing ELF files. Targets should derive a class from this to 00030 /// parameterize the output format. 00031 /// 00032 class ELFWriter : public MachineFunctionPass { 00033 friend class ELFCodeEmitter; 00034 public: 00035 static char ID; 00036 00037 MachineCodeEmitter &getMachineCodeEmitter() const { 00038 return *(MachineCodeEmitter*)MCE; 00039 } 00040 00041 ELFWriter(raw_ostream &O, TargetMachine &TM); 00042 ~ELFWriter(); 00043 00044 typedef std::vector<unsigned char> DataBuffer; 00045 00046 protected: 00047 /// Output stream to send the resultant object file to. 00048 /// 00049 raw_ostream &O; 00050 00051 /// Target machine description. 00052 /// 00053 TargetMachine &TM; 00054 00055 /// Mang - The object used to perform name mangling for this module. 00056 /// 00057 Mangler *Mang; 00058 00059 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine 00060 /// code for functions to the .o file. 00061 ELFCodeEmitter *MCE; 00062 00063 //===------------------------------------------------------------------===// 00064 // Properties to be set by the derived class ctor, used to configure the 00065 // ELFWriter. 00066 00067 // e_machine - This field is the target specific value to emit as the 00068 // e_machine member of the ELF header. 00069 unsigned short e_machine; 00070 00071 // e_flags - The machine flags for the target. This defaults to zero. 00072 unsigned e_flags; 00073 00074 //===------------------------------------------------------------------===// 00075 // Properties inferred automatically from the target machine. 00076 // 00077 00078 /// is64Bit/isLittleEndian - This information is inferred from the target 00079 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file. 00080 bool is64Bit, isLittleEndian; 00081 00082 /// doInitialization - Emit the file header and all of the global variables 00083 /// for the module to the ELF file. 00084 bool doInitialization(Module &M); 00085 00086 bool runOnMachineFunction(MachineFunction &MF); 00087 00088 00089 /// doFinalization - Now that the module has been completely processed, emit 00090 /// the ELF file to 'O'. 00091 bool doFinalization(Module &M); 00092 00093 private: 00094 // The buffer we accumulate the file header into. Note that this should be 00095 // changed into something much more efficient later (and the bitcode writer 00096 // as well!). 00097 DataBuffer FileHeader; 00098 00099 /// ELFSection - This struct contains information about each section that is 00100 /// emitted to the file. This is eventually turned into the section header 00101 /// table at the end of the file. 00102 struct ELFSection { 00103 std::string Name; // Name of the section. 00104 unsigned NameIdx; // Index in .shstrtab of name, once emitted. 00105 unsigned Type; 00106 unsigned Flags; 00107 uint64_t Addr; 00108 unsigned Offset; 00109 unsigned Size; 00110 unsigned Link; 00111 unsigned Info; 00112 unsigned Align; 00113 unsigned EntSize; 00114 00115 /// SectionIdx - The number of the section in the Section Table. 00116 /// 00117 unsigned short SectionIdx; 00118 00119 /// SectionData - The actual data for this section which we are building 00120 /// up for emission to the file. 00121 DataBuffer SectionData; 00122 00123 enum { SHT_NULL = 0, SHT_PROGBITS = 1, SHT_SYMTAB = 2, SHT_STRTAB = 3, 00124 SHT_RELA = 4, SHT_HASH = 5, SHT_DYNAMIC = 6, SHT_NOTE = 7, 00125 SHT_NOBITS = 8, SHT_REL = 9, SHT_SHLIB = 10, SHT_DYNSYM = 11 }; 00126 enum { SHN_UNDEF = 0, SHN_ABS = 0xFFF1, SHN_COMMON = 0xFFF2 }; 00127 enum { // SHF - ELF Section Header Flags 00128 SHF_WRITE = 1 << 0, // Writable 00129 SHF_ALLOC = 1 << 1, // Mapped into the process addr space 00130 SHF_EXECINSTR = 1 << 2, // Executable 00131 SHF_MERGE = 1 << 4, // Might be merged if equal 00132 SHF_STRINGS = 1 << 5, // Contains null-terminated strings 00133 SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index 00134 SHF_LINK_ORDER = 1 << 7, // Preserve order after combining 00135 SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required 00136 SHF_GROUP = 1 << 9, // Section is a member of a group 00137 SHF_TLS = 1 << 10 // Section holds thread-local data 00138 }; 00139 00140 ELFSection(const std::string &name) 00141 : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0), 00142 Link(0), Info(0), Align(0), EntSize(0) { 00143 } 00144 }; 00145 00146 /// SectionList - This is the list of sections that we have emitted to the 00147 /// file. Once the file has been completely built, the section header table 00148 /// is constructed from this info. 00149 std::list<ELFSection> SectionList; 00150 unsigned NumSections; // Always = SectionList.size() 00151 00152 /// SectionLookup - This is a mapping from section name to section number in 00153 /// the SectionList. 00154 std::map<std::string, ELFSection*> SectionLookup; 00155 00156 /// getSection - Return the section with the specified name, creating a new 00157 /// section if one does not already exist. 00158 ELFSection &getSection(const std::string &Name, 00159 unsigned Type, unsigned Flags = 0) { 00160 ELFSection *&SN = SectionLookup[Name]; 00161 if (SN) return *SN; 00162 00163 SectionList.push_back(Name); 00164 SN = &SectionList.back(); 00165 SN->SectionIdx = NumSections++; 00166 SN->Type = Type; 00167 SN->Flags = Flags; 00168 return *SN; 00169 } 00170 00171 ELFSection &getDataSection() { 00172 return getSection(".data", ELFSection::SHT_PROGBITS, 00173 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC); 00174 } 00175 ELFSection &getBSSSection() { 00176 return getSection(".bss", ELFSection::SHT_NOBITS, 00177 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC); 00178 } 00179 00180 /// ELFSym - This struct contains information about each symbol that is 00181 /// added to logical symbol table for the module. This is eventually 00182 /// turned into a real symbol table in the file. 00183 struct ELFSym { 00184 const GlobalValue *GV; // The global value this corresponds to. 00185 unsigned NameIdx; // Index in .strtab of name, once emitted. 00186 uint64_t Value; 00187 unsigned Size; 00188 unsigned char Info; 00189 unsigned char Other; 00190 unsigned short SectionIdx; 00191 00192 enum { STB_LOCAL = 0, STB_GLOBAL = 1, STB_WEAK = 2 }; 00193 enum { STT_NOTYPE = 0, STT_OBJECT = 1, STT_FUNC = 2, STT_SECTION = 3, 00194 STT_FILE = 4 }; 00195 ELFSym(const GlobalValue *gv) : GV(gv), Value(0), Size(0), Info(0), 00196 Other(0), SectionIdx(0) {} 00197 00198 void SetBind(unsigned X) { 00199 assert(X == (X & 0xF) && "Bind value out of range!"); 00200 Info = (Info & 0x0F) | (X << 4); 00201 } 00202 void SetType(unsigned X) { 00203 assert(X == (X & 0xF) && "Type value out of range!"); 00204 Info = (Info & 0xF0) | X; 00205 } 00206 }; 00207 00208 /// SymbolTable - This is the list of symbols we have emitted to the file. 00209 /// This actually gets rearranged before emission to the file (to put the 00210 /// local symbols first in the list). 00211 std::vector<ELFSym> SymbolTable; 00212 00213 // As we complete the ELF file, we need to update fields in the ELF header 00214 // (e.g. the location of the section table). These members keep track of 00215 // the offset in ELFHeader of these various pieces to update and other 00216 // locations in the file. 00217 unsigned ELFHeader_e_shoff_Offset; // e_shoff in ELF header. 00218 unsigned ELFHeader_e_shstrndx_Offset; // e_shstrndx in ELF header. 00219 unsigned ELFHeader_e_shnum_Offset; // e_shnum in ELF header. 00220 private: 00221 void EmitGlobal(GlobalVariable *GV); 00222 00223 void EmitSymbolTable(); 00224 00225 void EmitSectionTableStringTable(); 00226 void OutputSectionsAndSectionTable(); 00227 }; 00228 } 00229 00230 #endif
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.