librsloader.cpp revision 141f4435692cc53db032525e1cc602c3b417c42e
1/* 2 * Copyright 2011-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 "librsloader.h" 18 19#include "ELFObject.h" 20#include "ELFSectionSymTab.h" 21#include "ELFSymbol.h" 22 23#include "utils/serialize.h" 24 25#define LOG_TAG "bcc" 26#include "cutils/log.h" 27 28#include <llvm/ADT/OwningPtr.h> 29#include <llvm/Support/ELF.h> 30 31static inline RSExecRef wrap(ELFObject<32> *object) { 32 return reinterpret_cast<RSExecRef>(object); 33} 34 35static inline ELFObject<32> *unwrap(RSExecRef object) { 36 return reinterpret_cast<ELFObject<32> *>(object); 37} 38 39extern "C" RSExecRef 40rsloaderCreateExec(unsigned char const *buf, 41 size_t buf_size, 42 void *(*find_symbol)(void *, char const *), 43 void *find_symbol_context) { 44 45 ArchiveReaderLE AR(buf, buf_size); 46 47 llvm::OwningPtr<ELFObject<32> > object(ELFObject<32>::read(AR)); 48 if (!object) { 49 ALOGE("Unable to load the ELF object."); 50 return NULL; 51 } 52 53 //object->print(); 54 object->relocate(find_symbol, find_symbol_context); 55 if (object->getMissingSymbols()) { 56 return NULL; 57 } 58 59 return wrap(object.take()); 60} 61 62extern "C" void rsloaderUpdateSectionHeaders(RSExecRef object_, 63 unsigned char *buf) { 64 ELFObject<32> *object = unwrap(object_); 65 66 // Remap the section header addresses to match the loaded code 67 llvm::ELF::Elf32_Ehdr* header = reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(buf); 68 69 llvm::ELF::Elf32_Shdr* shtab = 70 reinterpret_cast<llvm::ELF::Elf32_Shdr*>(buf + header->e_shoff); 71 72 for (int i = 0; i < header->e_shnum; i++) { 73 if (shtab[i].sh_flags & SHF_ALLOC) { 74 ELFSectionBits<32>* bits = 75 static_cast<ELFSectionBits<32>*>(object->getSectionByIndex(i)); 76 if (bits) { 77 const unsigned char* addr = bits->getBuffer(); 78 shtab[i].sh_addr = reinterpret_cast<llvm::ELF::Elf32_Addr>(addr); 79 } 80 } 81 } 82} 83 84extern "C" void rsloaderDisposeExec(RSExecRef object) { 85 delete unwrap(object); 86} 87 88extern "C" void *rsloaderGetSymbolAddress(RSExecRef object_, 89 char const *name) { 90 ELFObject<32> *object = unwrap(object_); 91 92 ELFSectionSymTab<32> *symtab = 93 static_cast<ELFSectionSymTab<32> *>(object->getSectionByName(".symtab")); 94 95 if (!symtab) { 96 return NULL; 97 } 98 99 ELFSymbol<32> *symbol = symtab->getByName(name); 100 101 if (!symbol) { 102 ALOGV("Symbol not found: %s\n", name); 103 return NULL; 104 } 105 106 int machine = object->getHeader()->getMachine(); 107 108 return symbol->getAddress(machine, false); 109} 110 111extern "C" size_t rsloaderGetSymbolSize(RSExecRef object_, char const *name) { 112 ELFObject<32> *object = unwrap(object_); 113 114 ELFSectionSymTab<32> *symtab = 115 static_cast<ELFSectionSymTab<32> *>(object->getSectionByName(".symtab")); 116 117 if (!symtab) { 118 return NULL; 119 } 120 121 ELFSymbol<32> *symbol = symtab->getByName(name); 122 123 if (!symbol) { 124 ALOGV("Symbol not found: %s\n", name); 125 return NULL; 126 } 127 128 return (size_t)symbol->getSize(); 129} 130 131extern "C" size_t rsloaderGetFuncCount(RSExecRef object) { 132 ELFSectionSymTab<32> *symtab = static_cast<ELFSectionSymTab<32> *>( 133 unwrap(object)->getSectionByName(".symtab")); 134 135 if (!symtab) { 136 return 0; 137 } 138 139 return symtab->getFuncCount(); 140} 141 142extern "C" void rsloaderGetFuncNameList(RSExecRef object, 143 size_t size, 144 char const **list) { 145 ELFSectionSymTab<32> *symtab = static_cast<ELFSectionSymTab<32> *>( 146 unwrap(object)->getSectionByName(".symtab")); 147 148 if (symtab) { 149 symtab->getFuncNameList(size, list); 150 } 151} 152