Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 1 | //===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the ELFWriter class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Bill Wendling | 4b2ca1a | 2007-02-08 01:30:50 +0000 | [diff] [blame] | 14 | #ifndef ELFWRITER_H |
| 15 | #define ELFWRITER_H |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 16 | |
| 17 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 18 | #include <list> |
Dan Gohman | c9235d2 | 2008-03-21 23:51:57 +0000 | [diff] [blame] | 19 | #include <map> |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 20 | |
| 21 | namespace llvm { |
| 22 | class GlobalVariable; |
Chris Lattner | 75bbdff | 2005-07-11 03:11:10 +0000 | [diff] [blame] | 23 | class Mangler; |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 24 | class MachineCodeEmitter; |
| 25 | class ELFCodeEmitter; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame^] | 26 | class raw_ostream; |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 27 | |
| 28 | /// ELFWriter - This class implements the common target-independent code for |
| 29 | /// writing ELF files. Targets should derive a class from this to |
| 30 | /// parameterize the output format. |
| 31 | /// |
| 32 | class ELFWriter : public MachineFunctionPass { |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 33 | friend class ELFCodeEmitter; |
| 34 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 35 | static char ID; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 37 | MachineCodeEmitter &getMachineCodeEmitter() const { |
| 38 | return *(MachineCodeEmitter*)MCE; |
| 39 | } |
| 40 | |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame^] | 41 | ELFWriter(raw_ostream &O, TargetMachine &TM); |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 42 | ~ELFWriter(); |
| 43 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 44 | typedef std::vector<unsigned char> DataBuffer; |
| 45 | |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 46 | protected: |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 47 | /// Output stream to send the resultant object file to. |
| 48 | /// |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame^] | 49 | raw_ostream &O; |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 50 | |
| 51 | /// Target machine description. |
| 52 | /// |
| 53 | TargetMachine &TM; |
| 54 | |
Chris Lattner | 75bbdff | 2005-07-11 03:11:10 +0000 | [diff] [blame] | 55 | /// Mang - The object used to perform name mangling for this module. |
| 56 | /// |
| 57 | Mangler *Mang; |
| 58 | |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 59 | /// MCE - The MachineCodeEmitter object that we are exposing to emit machine |
| 60 | /// code for functions to the .o file. |
| 61 | ELFCodeEmitter *MCE; |
| 62 | |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 63 | //===------------------------------------------------------------------===// |
| 64 | // Properties to be set by the derived class ctor, used to configure the |
| 65 | // ELFWriter. |
| 66 | |
| 67 | // e_machine - This field is the target specific value to emit as the |
| 68 | // e_machine member of the ELF header. |
| 69 | unsigned short e_machine; |
| 70 | |
| 71 | // e_flags - The machine flags for the target. This defaults to zero. |
| 72 | unsigned e_flags; |
| 73 | |
| 74 | //===------------------------------------------------------------------===// |
| 75 | // Properties inferred automatically from the target machine. |
| 76 | // |
| 77 | |
| 78 | /// is64Bit/isLittleEndian - This information is inferred from the target |
| 79 | /// machine directly, indicating whether to emit a 32- or 64-bit ELF file. |
| 80 | bool is64Bit, isLittleEndian; |
| 81 | |
| 82 | /// doInitialization - Emit the file header and all of the global variables |
| 83 | /// for the module to the ELF file. |
| 84 | bool doInitialization(Module &M); |
| 85 | |
| 86 | bool runOnMachineFunction(MachineFunction &MF); |
| 87 | |
| 88 | |
| 89 | /// doFinalization - Now that the module has been completely processed, emit |
| 90 | /// the ELF file to 'O'. |
| 91 | bool doFinalization(Module &M); |
| 92 | |
| 93 | private: |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 94 | // The buffer we accumulate the file header into. Note that this should be |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 95 | // changed into something much more efficient later (and the bitcode writer |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 96 | // as well!). |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 97 | DataBuffer FileHeader; |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 98 | |
| 99 | /// ELFSection - This struct contains information about each section that is |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 100 | /// emitted to the file. This is eventually turned into the section header |
| 101 | /// table at the end of the file. |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 102 | struct ELFSection { |
| 103 | std::string Name; // Name of the section. |
| 104 | unsigned NameIdx; // Index in .shstrtab of name, once emitted. |
| 105 | unsigned Type; |
| 106 | unsigned Flags; |
| 107 | uint64_t Addr; |
| 108 | unsigned Offset; |
| 109 | unsigned Size; |
| 110 | unsigned Link; |
| 111 | unsigned Info; |
| 112 | unsigned Align; |
| 113 | unsigned EntSize; |
Chris Lattner | e578ab9 | 2005-07-07 07:00:37 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 115 | /// SectionIdx - The number of the section in the Section Table. |
| 116 | /// |
| 117 | unsigned short SectionIdx; |
| 118 | |
| 119 | /// SectionData - The actual data for this section which we are building |
| 120 | /// up for emission to the file. |
| 121 | DataBuffer SectionData; |
| 122 | |
Chris Lattner | e578ab9 | 2005-07-07 07:00:37 +0000 | [diff] [blame] | 123 | enum { SHT_NULL = 0, SHT_PROGBITS = 1, SHT_SYMTAB = 2, SHT_STRTAB = 3, |
| 124 | SHT_RELA = 4, SHT_HASH = 5, SHT_DYNAMIC = 6, SHT_NOTE = 7, |
| 125 | SHT_NOBITS = 8, SHT_REL = 9, SHT_SHLIB = 10, SHT_DYNSYM = 11 }; |
| 126 | enum { SHN_UNDEF = 0, SHN_ABS = 0xFFF1, SHN_COMMON = 0xFFF2 }; |
Chris Lattner | f5d507e | 2005-07-12 06:40:29 +0000 | [diff] [blame] | 127 | enum { // SHF - ELF Section Header Flags |
| 128 | SHF_WRITE = 1 << 0, // Writable |
| 129 | SHF_ALLOC = 1 << 1, // Mapped into the process addr space |
| 130 | SHF_EXECINSTR = 1 << 2, // Executable |
| 131 | SHF_MERGE = 1 << 4, // Might be merged if equal |
| 132 | SHF_STRINGS = 1 << 5, // Contains null-terminated strings |
| 133 | SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index |
| 134 | SHF_LINK_ORDER = 1 << 7, // Preserve order after combining |
| 135 | SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required |
| 136 | SHF_GROUP = 1 << 9, // Section is a member of a group |
Chris Lattner | 410354f | 2006-02-22 16:23:43 +0000 | [diff] [blame] | 137 | SHF_TLS = 1 << 10 // Section holds thread-local data |
Chris Lattner | f5d507e | 2005-07-12 06:40:29 +0000 | [diff] [blame] | 138 | }; |
Chris Lattner | e578ab9 | 2005-07-07 07:00:37 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 140 | ELFSection(const std::string &name) |
| 141 | : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0), |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 142 | Link(0), Info(0), Align(0), EntSize(0) { |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | /// SectionList - This is the list of sections that we have emitted to the |
| 147 | /// file. Once the file has been completely built, the section header table |
| 148 | /// is constructed from this info. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 149 | std::list<ELFSection> SectionList; |
| 150 | unsigned NumSections; // Always = SectionList.size() |
| 151 | |
| 152 | /// SectionLookup - This is a mapping from section name to section number in |
| 153 | /// the SectionList. |
| 154 | std::map<std::string, ELFSection*> SectionLookup; |
| 155 | |
| 156 | /// getSection - Return the section with the specified name, creating a new |
| 157 | /// section if one does not already exist. |
Chris Lattner | 5f9cb59 | 2005-07-16 17:35:26 +0000 | [diff] [blame] | 158 | ELFSection &getSection(const std::string &Name, |
| 159 | unsigned Type, unsigned Flags = 0) { |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 160 | ELFSection *&SN = SectionLookup[Name]; |
| 161 | if (SN) return *SN; |
| 162 | |
| 163 | SectionList.push_back(Name); |
| 164 | SN = &SectionList.back(); |
| 165 | SN->SectionIdx = NumSections++; |
Chris Lattner | 5f9cb59 | 2005-07-16 17:35:26 +0000 | [diff] [blame] | 166 | SN->Type = Type; |
| 167 | SN->Flags = Flags; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 168 | return *SN; |
| 169 | } |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 56c3261 | 2005-07-16 17:40:34 +0000 | [diff] [blame] | 171 | ELFSection &getDataSection() { |
| 172 | return getSection(".data", ELFSection::SHT_PROGBITS, |
| 173 | ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC); |
| 174 | } |
| 175 | ELFSection &getBSSSection() { |
| 176 | return getSection(".bss", ELFSection::SHT_NOBITS, |
| 177 | ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC); |
| 178 | } |
| 179 | |
Chris Lattner | e578ab9 | 2005-07-07 07:00:37 +0000 | [diff] [blame] | 180 | /// ELFSym - This struct contains information about each symbol that is |
| 181 | /// added to logical symbol table for the module. This is eventually |
| 182 | /// turned into a real symbol table in the file. |
| 183 | struct ELFSym { |
Chris Lattner | f0cf1b7 | 2005-07-11 06:16:24 +0000 | [diff] [blame] | 184 | const GlobalValue *GV; // The global value this corresponds to. |
| 185 | unsigned NameIdx; // Index in .strtab of name, once emitted. |
Chris Lattner | e578ab9 | 2005-07-07 07:00:37 +0000 | [diff] [blame] | 186 | uint64_t Value; |
| 187 | unsigned Size; |
| 188 | unsigned char Info; |
| 189 | unsigned char Other; |
| 190 | unsigned short SectionIdx; |
| 191 | |
| 192 | enum { STB_LOCAL = 0, STB_GLOBAL = 1, STB_WEAK = 2 }; |
| 193 | enum { STT_NOTYPE = 0, STT_OBJECT = 1, STT_FUNC = 2, STT_SECTION = 3, |
| 194 | STT_FILE = 4 }; |
Chris Lattner | f0cf1b7 | 2005-07-11 06:16:24 +0000 | [diff] [blame] | 195 | ELFSym(const GlobalValue *gv) : GV(gv), Value(0), Size(0), Info(0), |
| 196 | Other(0), SectionIdx(0) {} |
Chris Lattner | e578ab9 | 2005-07-07 07:00:37 +0000 | [diff] [blame] | 197 | |
| 198 | void SetBind(unsigned X) { |
| 199 | assert(X == (X & 0xF) && "Bind value out of range!"); |
| 200 | Info = (Info & 0x0F) | (X << 4); |
| 201 | } |
| 202 | void SetType(unsigned X) { |
| 203 | assert(X == (X & 0xF) && "Type value out of range!"); |
| 204 | Info = (Info & 0xF0) | X; |
| 205 | } |
| 206 | }; |
| 207 | |
| 208 | /// SymbolTable - This is the list of symbols we have emitted to the file. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 209 | /// This actually gets rearranged before emission to the file (to put the |
| 210 | /// local symbols first in the list). |
Chris Lattner | e578ab9 | 2005-07-07 07:00:37 +0000 | [diff] [blame] | 211 | std::vector<ELFSym> SymbolTable; |
| 212 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 213 | // As we complete the ELF file, we need to update fields in the ELF header |
| 214 | // (e.g. the location of the section table). These members keep track of |
| 215 | // the offset in ELFHeader of these various pieces to update and other |
| 216 | // locations in the file. |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 217 | unsigned ELFHeader_e_shoff_Offset; // e_shoff in ELF header. |
| 218 | unsigned ELFHeader_e_shstrndx_Offset; // e_shstrndx in ELF header. |
| 219 | unsigned ELFHeader_e_shnum_Offset; // e_shnum in ELF header. |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 220 | private: |
Chris Lattner | 56c3261 | 2005-07-16 17:40:34 +0000 | [diff] [blame] | 221 | void EmitGlobal(GlobalVariable *GV); |
Chris Lattner | e578ab9 | 2005-07-07 07:00:37 +0000 | [diff] [blame] | 222 | |
| 223 | void EmitSymbolTable(); |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 224 | |
| 225 | void EmitSectionTableStringTable(); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 226 | void OutputSectionsAndSectionTable(); |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 227 | }; |
| 228 | } |
| 229 | |
| 230 | #endif |