1 /* 2 * Copyright 2011, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ELF_OBJECT_H 18 #define ELF_OBJECT_H 19 20 #include "ELFTypes.h" 21 #include "MemChunk.h" 22 23 #include "utils/rsl_assert.h" 24 25 #include <memory> 26 #include <string> 27 #include <vector> 28 29 template <unsigned Bitwidth> 30 class ELFObject { 31 public: 32 ELF_TYPE_INTRO_TO_TEMPLATE_SCOPE(Bitwidth); 33 34 private: 35 std::unique_ptr<ELFHeaderTy> header; 36 std::unique_ptr<ELFSectionHeaderTableTy> shtab; 37 std::vector<ELFSectionTy *> stab; 38 39 MemChunk SHNCommonData; 40 unsigned char *SHNCommonDataPtr; 41 size_t SHNCommonDataFreeSize; 42 43 bool missingSymbols; 44 45 // TODO: Need refactor! initSHNCommonDataSize(size_t SHNCommonDataSize)46 bool initSHNCommonDataSize(size_t SHNCommonDataSize) { 47 rsl_assert(!SHNCommonDataPtr && "Can't init twice."); 48 if (!SHNCommonData.allocate(SHNCommonDataSize)) { 49 return false; 50 } 51 52 SHNCommonDataPtr = SHNCommonData.getBuffer(); 53 SHNCommonDataFreeSize = SHNCommonDataSize; 54 return true; 55 } 56 57 private: ELFObject()58 ELFObject() : SHNCommonDataPtr(NULL), missingSymbols(false) { } 59 60 public: 61 template <typename Archiver> 62 static ELFObject *read(Archiver &AR); 63 getHeader()64 ELFHeaderTy const *getHeader() const { 65 return header.get(); 66 } 67 getSectionHeaderTable()68 ELFSectionHeaderTableTy const *getSectionHeaderTable() const { 69 return shtab.get(); 70 } 71 72 char const *getSectionName(size_t i) const; 73 ELFSectionTy const *getSectionByIndex(size_t i) const; 74 ELFSectionTy *getSectionByIndex(size_t i); 75 ELFSectionTy const *getSectionByName(std::string const &str) const; 76 ELFSectionTy *getSectionByName(std::string const &str); 77 getMissingSymbols()78 inline bool getMissingSymbols() const { 79 return missingSymbols; 80 } 81 82 void *allocateSHNCommonData(size_t size, size_t align = 1) { 83 rsl_assert(size > 0 && align != 0); 84 85 rsl_assert(SHNCommonDataPtr && "Must init common data size before use!"); 86 87 // Ensure alignment 88 size_t rem = ((uintptr_t)SHNCommonDataPtr) % align; 89 if (rem != 0) { 90 SHNCommonDataPtr += align - rem; 91 SHNCommonDataFreeSize -= align - rem; 92 } 93 94 // Ensure the free size is sufficient 95 if (SHNCommonDataFreeSize < size) { 96 return NULL; 97 } 98 99 // Allcoate 100 void *result = SHNCommonDataPtr; 101 SHNCommonDataPtr += size; 102 SHNCommonDataFreeSize -= size; 103 104 return result; 105 } 106 107 void relocate(void *(*find_sym)(void *context, char const *name), 108 void *context); 109 110 void print() const; 111 ~ELFObject()112 ~ELFObject() { 113 for (size_t i = 0; i < stab.size(); ++i) { 114 // Delete will check the pointer is nullptr or not by himself. 115 delete stab[i]; 116 } 117 } 118 119 private: 120 void relocateARM(void *(*find_sym)(void *context, char const *name), 121 void *context, 122 ELFSectionRelTableTy *reltab, 123 ELFSectionProgBitsTy *text); 124 125 void relocateAARCH64(void *(*find_sym)(void *context, char const *name), 126 void *context, 127 ELFSectionRelTableTy *reltab, 128 ELFSectionProgBitsTy *text); 129 130 void relocateX86_32(void *(*find_sym)(void *context, char const *name), 131 void *context, 132 ELFSectionRelTableTy *reltab, 133 ELFSectionProgBitsTy *text); 134 135 void relocateX86_64(void *(*find_sym)(void *context, char const *name), 136 void *context, 137 ELFSectionRelTableTy *reltab, 138 ELFSectionProgBitsTy *text); 139 140 void relocateMIPS(void *(*find_sym)(void *context, char const *name), 141 void *context, 142 ELFSectionRelTableTy *reltab, 143 ELFSectionProgBitsTy *text); 144 }; 145 146 #include "impl/ELFObject.hxx" 147 148 #endif // ELF_OBJECT_H 149