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