1 //===- ELFWriter.h --------------------------------------------------------===// 2 // 3 // The MCLinker Project 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #ifndef MCLD_ELF_WRITER_H 10 #define MCLD_ELF_WRITER_H 11 #ifdef ENABLE_UNITTEST 12 #include <gtest.h> 13 #endif 14 15 #include <llvm/Support/ELF.h> 16 #include <mcld/MC/MCLDOutput.h> 17 18 namespace mcld 19 { 20 21 class MCLDInfo; 22 class Layout; 23 class GNULDBackend; 24 class Relocation; 25 class LDSection; 26 class SectionData; 27 28 /** \class ELFWriter 29 * \brief ELFWriter provides basic functions to write ELF sections, symbols, 30 * and so on. 31 */ 32 class ELFWriter 33 { 34 public: 35 typedef uint64_t FileOffset; 36 37 protected: ELFWriter(GNULDBackend & pBackend)38 ELFWriter(GNULDBackend& pBackend) 39 : f_Backend(pBackend) { 40 } 41 42 public: ~ELFWriter()43 virtual ~ELFWriter() { } 44 target()45 GNULDBackend& target() 46 { return f_Backend; } 47 target()48 const GNULDBackend& target() const 49 { return f_Backend; } 50 51 virtual void writeELF32Header(const MCLDInfo& pInfo, 52 const Layout& pLayout, 53 const GNULDBackend& pBackend, 54 Output& pOutput) const; 55 56 virtual void writeELF64Header(const MCLDInfo& pInfo, 57 const Layout& pLayout, 58 const GNULDBackend& pBackend, 59 Output& pOutput) const; 60 61 virtual uint64_t getEntryPoint(const MCLDInfo& pInfo, 62 const Layout& pLayout, 63 const GNULDBackend& pBackend, 64 const Output& pOutput) const; 65 66 protected: 67 void emitELF32SectionHeader(Output& pOutput, MCLinker& pLinker) const; 68 69 void emitELF64SectionHeader(Output& pOutput, MCLinker& pLinker) const; 70 71 void emitELF32ProgramHeader(Output& pOutput, 72 const GNULDBackend& pBackend) const; 73 74 void emitELF64ProgramHeader(Output& pOutput, 75 const GNULDBackend& pBackend) const; 76 77 // emitShStrTab - emit .shstrtab 78 void emitELF32ShStrTab(Output& pOutput, MCLinker& pLinker) const; 79 80 void emitELF64ShStrTab(Output& pOutput, MCLinker& pLinker) const; 81 82 void emitSectionData(const Layout& pLayout, 83 const LDSection& pSection, 84 MemoryRegion& pRegion) const; 85 86 void emitRelocation(const Layout& pLayout, 87 const Output& pOutput, 88 const LDSection& pSection, 89 MemoryRegion& pRegion) const; 90 91 void emitRel(const Layout& pLayout, 92 const Output& pOutput, 93 const SectionData& pSectionData, 94 MemoryRegion& pRegion) const; 95 96 void emitRela(const Layout& pLayout, 97 const Output& pOutput, 98 const SectionData& pSectionData, 99 MemoryRegion& pRegion) const; 100 101 private: 102 // getSectEntrySize - compute ElfXX_Shdr::sh_entsize 103 uint64_t getELF32SectEntrySize(const LDSection& pSection) const; 104 105 // getSectEntrySize - compute ElfXX_Shdr::sh_entsize 106 uint64_t getELF64SectEntrySize(const LDSection& pSection) const; 107 108 // getSectEntrySize - compute ElfXX_Shdr::sh_link 109 uint64_t getSectLink(const LDSection& pSection, const Output& pOutput) const; 110 111 // getSectEntrySize - compute ElfXX_Shdr::sh_info 112 uint64_t getSectInfo(const LDSection& pSection, const Output& pOutput) const; 113 114 uint64_t getELF32LastStartOffset(const Output& pOutput) const; 115 116 uint64_t getELF64LastStartOffset(const Output& pOutput) const; 117 118 protected: 119 GNULDBackend& f_Backend; 120 }; 121 122 } // namespace of mcld 123 124 #endif 125 126