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 | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 19 | #include "ELF.h" |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 20 | #include <list> |
Dan Gohman | c9235d2 | 2008-03-21 23:51:57 +0000 | [diff] [blame] | 21 | #include <map> |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
| 24 | class GlobalVariable; |
Chris Lattner | 75bbdff | 2005-07-11 03:11:10 +0000 | [diff] [blame] | 25 | class Mangler; |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 26 | class MachineCodeEmitter; |
| 27 | class ELFCodeEmitter; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 28 | class raw_ostream; |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 29 | |
| 30 | /// ELFWriter - This class implements the common target-independent code for |
| 31 | /// writing ELF files. Targets should derive a class from this to |
| 32 | /// parameterize the output format. |
| 33 | /// |
| 34 | class ELFWriter : public MachineFunctionPass { |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 35 | friend class ELFCodeEmitter; |
| 36 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 37 | static char ID; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 39 | MachineCodeEmitter &getMachineCodeEmitter() const { |
| 40 | return *(MachineCodeEmitter*)MCE; |
| 41 | } |
| 42 | |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 43 | ELFWriter(raw_ostream &O, TargetMachine &TM); |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 44 | ~ELFWriter(); |
| 45 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 46 | typedef std::vector<unsigned char> DataBuffer; |
| 47 | |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 48 | protected: |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 49 | /// Output stream to send the resultant object file to. |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 50 | raw_ostream &O; |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 51 | |
| 52 | /// Target machine description. |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 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. |
Chris Lattner | 75bbdff | 2005-07-11 03:11:10 +0000 | [diff] [blame] | 56 | Mangler *Mang; |
| 57 | |
Chris Lattner | 6871aed | 2005-07-11 05:15:32 +0000 | [diff] [blame] | 58 | /// MCE - The MachineCodeEmitter object that we are exposing to emit machine |
| 59 | /// code for functions to the .o file. |
| 60 | ELFCodeEmitter *MCE; |
| 61 | |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 62 | //===------------------------------------------------------------------===// |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 63 | // Properties inferred automatically from the target machine. |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 64 | //===------------------------------------------------------------------===// |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 65 | |
| 66 | /// is64Bit/isLittleEndian - This information is inferred from the target |
| 67 | /// machine directly, indicating whether to emit a 32- or 64-bit ELF file. |
| 68 | bool is64Bit, isLittleEndian; |
| 69 | |
| 70 | /// doInitialization - Emit the file header and all of the global variables |
| 71 | /// for the module to the ELF file. |
| 72 | bool doInitialization(Module &M); |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 73 | bool runOnMachineFunction(MachineFunction &MF); |
| 74 | |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 75 | /// doFinalization - Now that the module has been completely processed, emit |
| 76 | /// the ELF file to 'O'. |
| 77 | bool doFinalization(Module &M); |
| 78 | |
| 79 | private: |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 80 | // 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] | 81 | // changed into something much more efficient later (and the bitcode writer |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 82 | // as well!). |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 83 | DataBuffer FileHeader; |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 84 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 85 | /// ElfHdr - Hold information about the ELF Header |
| 86 | ELFHeader *ElfHdr; |
| 87 | |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 88 | /// SectionList - This is the list of sections that we have emitted to the |
| 89 | /// file. Once the file has been completely built, the section header table |
| 90 | /// is constructed from this info. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 91 | std::list<ELFSection> SectionList; |
| 92 | unsigned NumSections; // Always = SectionList.size() |
| 93 | |
| 94 | /// SectionLookup - This is a mapping from section name to section number in |
| 95 | /// the SectionList. |
| 96 | std::map<std::string, ELFSection*> SectionLookup; |
| 97 | |
| 98 | /// getSection - Return the section with the specified name, creating a new |
| 99 | /// section if one does not already exist. |
Chris Lattner | 5f9cb59 | 2005-07-16 17:35:26 +0000 | [diff] [blame] | 100 | ELFSection &getSection(const std::string &Name, |
| 101 | unsigned Type, unsigned Flags = 0) { |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 102 | ELFSection *&SN = SectionLookup[Name]; |
| 103 | if (SN) return *SN; |
| 104 | |
| 105 | SectionList.push_back(Name); |
| 106 | SN = &SectionList.back(); |
| 107 | SN->SectionIdx = NumSections++; |
Chris Lattner | 5f9cb59 | 2005-07-16 17:35:26 +0000 | [diff] [blame] | 108 | SN->Type = Type; |
| 109 | SN->Flags = Flags; |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 110 | SN->Link = ELFSection::SHN_UNDEF; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 111 | return *SN; |
| 112 | } |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 113 | |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 114 | ELFSection &getTextSection() { |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 115 | return getSection(".text", ELFSection::SHT_PROGBITS, |
| 116 | ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC); |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | 56c3261 | 2005-07-16 17:40:34 +0000 | [diff] [blame] | 119 | ELFSection &getDataSection() { |
| 120 | return getSection(".data", ELFSection::SHT_PROGBITS, |
| 121 | ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC); |
| 122 | } |
| 123 | ELFSection &getBSSSection() { |
| 124 | return getSection(".bss", ELFSection::SHT_NOBITS, |
| 125 | ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC); |
| 126 | } |
| 127 | |
Chris Lattner | e578ab9 | 2005-07-07 07:00:37 +0000 | [diff] [blame] | 128 | /// 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] | 129 | /// This actually gets rearranged before emission to the file (to put the |
| 130 | /// local symbols first in the list). |
Chris Lattner | e578ab9 | 2005-07-07 07:00:37 +0000 | [diff] [blame] | 131 | std::vector<ELFSym> SymbolTable; |
| 132 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 133 | /// PendingSyms - This is a list of externally defined symbols that we have |
| 134 | /// been asked to emit, but have not seen a reference to. When a reference |
| 135 | /// is seen, the symbol will move from this list to the SymbolTable. |
| 136 | SetVector<GlobalValue*> PendingGlobals; |
| 137 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 138 | // As we complete the ELF file, we need to update fields in the ELF header |
| 139 | // (e.g. the location of the section table). These members keep track of |
| 140 | // the offset in ELFHeader of these various pieces to update and other |
| 141 | // locations in the file. |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 142 | unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header. |
| 143 | unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header. |
| 144 | unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header. |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 145 | private: |
Chris Lattner | 56c3261 | 2005-07-16 17:40:34 +0000 | [diff] [blame] | 146 | void EmitGlobal(GlobalVariable *GV); |
Chris Lattner | e578ab9 | 2005-07-07 07:00:37 +0000 | [diff] [blame] | 147 | void EmitSymbolTable(); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 148 | void EmitRelocations(); |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 149 | void EmitSectionTableStringTable(); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 150 | void OutputSectionsAndSectionTable(); |
Chris Lattner | 3201815 | 2005-06-27 06:28:45 +0000 | [diff] [blame] | 151 | }; |
| 152 | } |
| 153 | |
| 154 | #endif |