1 /* 2 * Copyright 2012, 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 #include "ELFObjectLoaderImpl.h" 18 19 #include <llvm/Support/ELF.h> 20 21 // The following files are included from librsloader. 22 #include "ELFObject.h" 23 #include "ELFSectionSymTab.h" 24 #include "ELFSymbol.h" 25 #include "utils/serialize.h" 26 27 #include "bcc/ExecutionEngine/SymbolResolverInterface.h" 28 #include "bcc/Support/Log.h" 29 30 using namespace bcc; 31 32 bool ELFObjectLoaderImpl::load(const void *pMem, size_t pMemSize) { 33 ArchiveReaderLE reader(reinterpret_cast<const unsigned char *>(pMem), 34 pMemSize); 35 36 mObject = ELFObject<32>::read(reader); 37 if (mObject == NULL) { 38 ALOGE("Unable to load the ELF object!"); 39 return false; 40 } 41 42 // Retrive the pointer to the symbol table. 43 mSymTab = static_cast<ELFSectionSymTab<32> *>( 44 mObject->getSectionByName(".symtab")); 45 if (mSymTab == NULL) { 46 ALOGW("Object doesn't contain any symbol table."); 47 } 48 49 return true; 50 } 51 52 bool ELFObjectLoaderImpl::relocate(SymbolResolverInterface &pResolver) { 53 mObject->relocate(SymbolResolverInterface::LookupFunction, &pResolver); 54 55 if (mObject->getMissingSymbols()) { 56 ALOGE("Some symbols are found to be undefined during relocation!"); 57 return false; 58 } 59 60 return true; 61 } 62 63 bool ELFObjectLoaderImpl::prepareDebugImage(void *pDebugImg, 64 size_t pDebugImgSize) { 65 // Update the value of sh_addr in pDebugImg to its corresponding section in 66 // the mObject. 67 llvm::ELF::Elf32_Ehdr *elf_header = 68 reinterpret_cast<llvm::ELF::Elf32_Ehdr *>(pDebugImg); 69 70 if (elf_header->e_shoff > pDebugImgSize) { 71 ALOGE("Invalid section header table offset found! (e_shoff = %d)", 72 elf_header->e_shoff); 73 return false; 74 } 75 76 if ((elf_header->e_shoff + 77 sizeof(llvm::ELF::Elf32_Shdr) * elf_header->e_shnum) > pDebugImgSize) { 78 ALOGE("Invalid image supplied (debug image doesn't contain all the section" 79 "header or corrupted image)! (e_shoff = %d, e_shnum = %d)", 80 elf_header->e_shoff, elf_header->e_shnum); 81 return false; 82 } 83 84 llvm::ELF::Elf32_Shdr *section_header_table = 85 reinterpret_cast<llvm::ELF::Elf32_Shdr *>( 86 reinterpret_cast<uint8_t*>(pDebugImg) + elf_header->e_shoff); 87 88 for (unsigned i = 0; i < elf_header->e_shnum; i++) { 89 if (section_header_table[i].sh_flags & llvm::ELF::SHF_ALLOC) { 90 ELFSectionBits<32> *section = 91 static_cast<ELFSectionBits<32> *>(mObject->getSectionByIndex(i)); 92 if (section != NULL) { 93 section_header_table[i].sh_addr = 94 reinterpret_cast<llvm::ELF::Elf32_Addr>(section->getBuffer()); 95 } 96 } 97 } 98 99 return true; 100 } 101 102 void *ELFObjectLoaderImpl::getSymbolAddress(const char *pName) const { 103 if (mSymTab == NULL) { 104 return NULL; 105 } 106 107 const ELFSymbol<32> *symbol = mSymTab->getByName(pName); 108 if (symbol == NULL) { 109 ALOGV("Request symbol '%s' is not found in the object!", pName); 110 return NULL; 111 } 112 113 return symbol->getAddress(mObject->getHeader()->getMachine(), 114 /* autoAlloc */false); 115 } 116 117 size_t ELFObjectLoaderImpl::getSymbolSize(const char *pName) const { 118 if (mSymTab == NULL) { 119 return 0; 120 } 121 122 const ELFSymbol<32> *symbol = mSymTab->getByName(pName); 123 124 if (symbol == NULL) { 125 ALOGV("Request symbol '%s' is not found in the object!", pName); 126 return 0; 127 } 128 129 return static_cast<size_t>(symbol->getSize()); 130 131 } 132 133 bool 134 ELFObjectLoaderImpl::getSymbolNameList(android::Vector<const char *>& pNameList, 135 ObjectLoader::SymbolType pType) const { 136 if (mSymTab == NULL) { 137 return false; 138 } 139 140 unsigned elf_type; 141 switch (pType) { 142 case ObjectLoader::kFunctionType: { 143 elf_type = llvm::ELF::STT_FUNC; 144 break; 145 } 146 case ObjectLoader::kUnknownType: { 147 break; 148 } 149 default: { 150 assert(false && "Invalid symbol type given!"); 151 return false; 152 } 153 } 154 155 for (size_t i = 0, e = mSymTab->size(); i != e; i++) { 156 ELFSymbol<32> *symbol = (*mSymTab)[i]; 157 if (symbol == NULL) { 158 continue; 159 } 160 161 if ((pType == ObjectLoader::kUnknownType) || 162 (symbol->getType() == elf_type)) { 163 const char *symbol_name = symbol->getName(); 164 if (symbol_name != NULL) { 165 pNameList.push_back(symbol_name); 166 } 167 } 168 } 169 170 return true; 171 } 172 173 ELFObjectLoaderImpl::~ELFObjectLoaderImpl() { 174 delete mObject; 175 return; 176 } 177