1 //------------------------------------------------------------------------------ 2 // Copyright (c) 2003, Ingo Weinhold 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a 5 // copy of this software and associated documentation files (the "Software"), 6 // to deal in the Software without restriction, including without limitation 7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 // and/or sell copies of the Software, and to permit persons to whom the 9 // Software is furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 // DEALINGS IN THE SOFTWARE. 21 // 22 // File Name: ElfFile.h 23 // Author: Ingo Weinhold (bonefish@users.sf.net) 24 // Description: Interface declarations of classes for accessing ELF file, 25 // or more precisely for iterating through their relocation 26 // sections. 27 //------------------------------------------------------------------------------ 28 29 #ifndef ELF_FILE_H 30 #define ELF_FILE_H 31 32 #include <File.h> 33 #include <image.h> 34 35 #include "Elf.h" 36 37 38 namespace SymbolPatcher { 39 40 class ElfFile; 41 class ElfSection; 42 43 // ElfSymbol 44 class ElfSymbol { 45 public: 46 ElfSymbol(ElfSection* section = NULL, 47 int32 index = -1); 48 ~ElfSymbol(); 49 50 void SetTo(ElfSection* section, int32 index); 51 void Unset(); 52 53 const Elf_Sym* GetSymbolStruct(); 54 const char* GetName(); 55 uint32 GetBinding(); 56 uint32 GetType(); 57 uint32 GetTargetSectionIndex(); 58 59 private: 60 ElfSection* fSection; 61 int32 fIndex; 62 Elf_Sym* fSymbol; 63 }; 64 65 // ElfRelocation 66 class ElfRelocation { 67 public: 68 ElfRelocation(ElfSection* section = NULL, 69 int32 index = -1); 70 ~ElfRelocation(); 71 72 void SetTo(ElfSection* section, int32 index); 73 void Unset(); 74 75 Elf_Rel* GetRelocationStruct(); 76 uint32 GetType(); 77 uint32 GetSymbolIndex(); 78 Elf_Addr GetOffset(); 79 status_t GetSymbol(ElfSymbol* symbol); 80 81 private: 82 ElfSection* fSection; 83 int32 fIndex; 84 Elf_Rel* fRelocation; 85 }; 86 87 // ElfRelocationIterator 88 class ElfRelocationIterator { 89 public: 90 ElfRelocationIterator(ElfFile* file); 91 ~ElfRelocationIterator(); 92 93 bool GetNext(ElfRelocation* relocation); 94 95 private: 96 ElfSection* _FindNextSection(); 97 98 private: 99 ElfFile* fFile; 100 int32 fSectionIndex; 101 int32 fEntryIndex; 102 }; 103 104 // ElfFile 105 class ElfFile { 106 public: 107 ElfFile(); 108 ~ElfFile(); 109 status_t SetTo(const char *filename); 110 void Unset(); 111 void Unload(); 112 113 BFile* GetFile() { return &fFile; } 114 115 const char* GetSectionHeaderStrings(size_t* size); 116 const char* GetStringSectionStrings(int32 index, 117 size_t* size); 118 119 int32 CountSections() const { return fSectionCount; } 120 ElfSection* SectionAt(int32 index, bool load = false); 121 122 void Dump(); 123 124 private: 125 status_t _SetTo(const char *filename); 126 127 Elf_Shdr* _SectionHeaderAt(int32 index); 128 129 status_t _LoadSection(int32 index); 130 131 private: 132 BFile fFile; 133 Elf_Ehdr fHeader; 134 uint8* fSectionHeaders; 135 ElfSection* fSections; 136 int32 fSectionCount; 137 size_t fSectionHeaderSize; 138 }; 139 140 } // namespace SymbolPatcher 141 142 using namespace SymbolPatcher; 143 144 145 #endif // ELF_FILE_H 146