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 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SetVector.h" |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetAsmInfo.h" |
| 21 | #include "llvm/Target/TargetELFWriterInfo.h" |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 22 | #include "ELF.h" |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 23 | #include <list> |
Dan Gohman | c9235d2 | 2008-03-21 23:51:57 +0000 | [diff] [blame] | 24 | #include <map> |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 25 | |
| 26 | namespace llvm { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 27 | class BinaryObject; |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 28 | class ConstantStruct; |
| 29 | class ELFCodeEmitter; |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 30 | class GlobalVariable; |
Chris Lattner | 75bbdff | 2005-07-11 03:11:10 +0000 | [diff] [blame] | 31 | class Mangler; |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 32 | class MachineCodeEmitter; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 33 | class raw_ostream; |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 34 | |
| 35 | /// ELFWriter - This class implements the common target-independent code for |
| 36 | /// writing ELF files. Targets should derive a class from this to |
| 37 | /// parameterize the output format. |
| 38 | /// |
| 39 | class ELFWriter : public MachineFunctionPass { |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 40 | friend class ELFCodeEmitter; |
| 41 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 42 | static char ID; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 44 | MachineCodeEmitter &getMachineCodeEmitter() const { |
| 45 | return *(MachineCodeEmitter*)MCE; |
| 46 | } |
| 47 | |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 48 | ELFWriter(raw_ostream &O, TargetMachine &TM); |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 49 | ~ELFWriter(); |
| 50 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 51 | typedef std::vector<unsigned char> DataBuffer; |
| 52 | |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 53 | protected: |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 54 | /// Output stream to send the resultant object file to. |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 55 | raw_ostream &O; |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 56 | |
| 57 | /// Target machine description. |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 58 | TargetMachine &TM; |
| 59 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 60 | /// Target Elf Writer description. |
| 61 | const TargetELFWriterInfo *TEW; |
| 62 | |
Chris Lattner | 75bbdff | 2005-07-11 03:11:10 +0000 | [diff] [blame] | 63 | /// Mang - The object used to perform name mangling for this module. |
Chris Lattner | 75bbdff | 2005-07-11 03:11:10 +0000 | [diff] [blame] | 64 | Mangler *Mang; |
| 65 | |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 66 | /// MCE - The MachineCodeEmitter object that we are exposing to emit machine |
| 67 | /// code for functions to the .o file. |
| 68 | ELFCodeEmitter *MCE; |
| 69 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 70 | /// TAI - Target Asm Info, provide information about section names for |
| 71 | /// globals and other target specific stuff. |
| 72 | const TargetAsmInfo *TAI; |
| 73 | |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 74 | //===------------------------------------------------------------------===// |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 75 | // Properties inferred automatically from the target machine. |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 76 | //===------------------------------------------------------------------===// |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 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); |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 85 | bool runOnMachineFunction(MachineFunction &MF); |
| 86 | |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 87 | /// doFinalization - Now that the module has been completely processed, emit |
| 88 | /// the ELF file to 'O'. |
| 89 | bool doFinalization(Module &M); |
| 90 | |
| 91 | private: |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 92 | /// Blob containing the Elf header |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 93 | BinaryObject ElfHdr; |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 95 | /// SectionList - This is the list of sections that we have emitted to the |
| 96 | /// file. Once the file has been completely built, the section header table |
| 97 | /// is constructed from this info. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 98 | std::list<ELFSection> SectionList; |
| 99 | unsigned NumSections; // Always = SectionList.size() |
| 100 | |
| 101 | /// SectionLookup - This is a mapping from section name to section number in |
| 102 | /// the SectionList. |
| 103 | std::map<std::string, ELFSection*> SectionLookup; |
| 104 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 105 | /// GblSymLookup - This is a mapping from global value to a symbol index |
| 106 | /// in the symbol table. This is useful since relocations symbol references |
| 107 | /// must be quickly mapped to a symbol table index |
| 108 | std::map<const GlobalValue*, uint32_t> GblSymLookup; |
| 109 | |
| 110 | /// SymbolList - This is the list of symbols emitted to the symbol table |
| 111 | /// Local symbols go to the front and Globals to the back. |
| 112 | std::list<ELFSym> SymbolList; |
| 113 | |
| 114 | /// PendingGlobals - List of externally defined symbols that we have been |
| 115 | /// asked to emit, but have not seen a reference to. When a reference |
| 116 | /// is seen, the symbol will move from this list to the SymbolList. |
| 117 | SetVector<GlobalValue*> PendingGlobals; |
| 118 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 119 | /// getSection - Return the section with the specified name, creating a new |
| 120 | /// section if one does not already exist. |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 121 | ELFSection &getSection(const std::string &Name, unsigned Type, |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 122 | unsigned Flags = 0, unsigned Align = 0) { |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 123 | ELFSection *&SN = SectionLookup[Name]; |
| 124 | if (SN) return *SN; |
| 125 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 126 | // Remove tab from section name prefix. This is necessary becase TAI |
| 127 | // sometimes return a section name prefixed with a "\t" char. |
| 128 | std::string SectionName(Name); |
| 129 | size_t Pos = SectionName.find("\t"); |
| 130 | if (Pos != std::string::npos) |
| 131 | SectionName.erase(Pos, 1); |
| 132 | |
| 133 | SectionList.push_back(ELFSection(SectionName, isLittleEndian, is64Bit)); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 134 | SN = &SectionList.back(); |
| 135 | SN->SectionIdx = NumSections++; |
Chris Lattner | 5f9cb59 | 2005-07-16 17:35:26 +0000 | [diff] [blame] | 136 | SN->Type = Type; |
| 137 | SN->Flags = Flags; |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 138 | SN->Link = ELFSection::SHN_UNDEF; |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 139 | SN->Align = Align; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 140 | return *SN; |
| 141 | } |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 142 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 143 | /// TODO: support mangled names here to emit the right .text section |
| 144 | /// for c++ object files. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 145 | ELFSection &getTextSection() { |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 146 | return getSection(".text", ELFSection::SHT_PROGBITS, |
| 147 | ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC); |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 150 | /// Return the relocation section of section 'S'. 'RelA' is true |
| 151 | /// if the relocation section contains entries with addends. |
| 152 | ELFSection &getRelocSection(std::string SName, bool RelA) { |
| 153 | std::string RelSName(".rel"); |
| 154 | unsigned SHdrTy = RelA ? ELFSection::SHT_RELA : ELFSection::SHT_REL; |
| 155 | |
| 156 | if (RelA) RelSName.append("a"); |
| 157 | RelSName.append(SName); |
| 158 | |
| 159 | return getSection(RelSName, SHdrTy, 0, TEW->getPrefELFAlignment()); |
| 160 | } |
| 161 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 162 | ELFSection &getNonExecStackSection() { |
| 163 | return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1); |
| 164 | } |
| 165 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 166 | ELFSection &getSymbolTableSection() { |
| 167 | return getSection(".symtab", ELFSection::SHT_SYMTAB, 0); |
| 168 | } |
| 169 | |
| 170 | ELFSection &getStringTableSection() { |
| 171 | return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1); |
| 172 | } |
| 173 | |
Chris Lattner | 56c3261 | 2005-07-16 17:40:34 +0000 | [diff] [blame] | 174 | ELFSection &getDataSection() { |
| 175 | return getSection(".data", ELFSection::SHT_PROGBITS, |
| 176 | ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC); |
| 177 | } |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 56c3261 | 2005-07-16 17:40:34 +0000 | [diff] [blame] | 179 | ELFSection &getBSSSection() { |
| 180 | return getSection(".bss", ELFSection::SHT_NOBITS, |
| 181 | ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC); |
| 182 | } |
| 183 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 184 | ELFSection &getNullSection() { |
| 185 | return getSection("", ELFSection::SHT_NULL, 0); |
| 186 | } |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 188 | // As we complete the ELF file, we need to update fields in the ELF header |
| 189 | // (e.g. the location of the section table). These members keep track of |
| 190 | // the offset in ELFHeader of these various pieces to update and other |
| 191 | // locations in the file. |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 192 | unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header. |
| 193 | unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header. |
| 194 | unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 196 | private: |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 197 | void EmitFunctionDeclaration(const Function *F); |
| 198 | void EmitGlobalVar(const GlobalVariable *GV); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 199 | void EmitGlobalConstant(const Constant *C, ELFSection &GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 200 | void EmitGlobalConstantStruct(const ConstantStruct *CVS, |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 201 | ELFSection &GblS); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 202 | unsigned getGlobalELFLinkage(const GlobalVariable *GV); |
| 203 | ELFSection &getGlobalSymELFSection(const GlobalVariable *GV, ELFSym &Sym); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 204 | void EmitRelocations(); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 205 | void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 206 | void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr); |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 207 | void EmitSectionTableStringTable(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 208 | void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 209 | void EmitSymbolTable(); |
Bruno Cardoso Lopes | c236a34 | 2009-06-22 19:29:56 +0000 | [diff] [blame^] | 210 | void EmitStringTable(); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 211 | void OutputSectionsAndSectionTable(); |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 212 | }; |
| 213 | } |
| 214 | |
| 215 | #endif |