librsloader.cpp revision ce2e754ed4b1aefe607bbbddd8ce0e6cba824e8f
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 LOGE("Unable to load the ELF object."); 50 return NULL; 51 } 52 53 //object->print(); 54 object->relocate(find_symbol, find_symbol_context); 55 56 return wrap(object.take()); 57} 58 59extern "C" void rsloaderUpdateSectionHeaders(RSExecRef object_, 60 unsigned char *buf) { 61 ELFObject<32> *object = unwrap(object_); 62 63 // Remap the section header addresses to match the loaded code 64 llvm::ELF::Elf32_Ehdr* header = reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(buf); 65 66 llvm::ELF::Elf32_Shdr* shtab = 67 reinterpret_cast<llvm::ELF::Elf32_Shdr*>(buf + header->e_shoff); 68 69 for (int i = 0; i < header->e_shnum; i++) { 70 if (shtab[i].sh_flags & SHF_ALLOC) { 71 ELFSectionBits<32>* bits = 72 static_cast<ELFSectionBits<32>*>(object->getSectionByIndex(i)); 73 if (bits) { 74 const unsigned char* addr = bits->getBuffer(); 75 shtab[i].sh_addr = reinterpret_cast<llvm::ELF::Elf32_Addr>(addr); 76 } 77 } 78 } 79} 80 81extern "C" void rsloaderDisposeExec(RSExecRef object) { 82 delete unwrap(object); 83} 84 85extern "C" void *rsloaderGetSymbolAddress(RSExecRef object_, 86 char const *name) { 87 ELFObject<32> *object = unwrap(object_); 88 89 ELFSectionSymTab<32> *symtab = 90 static_cast<ELFSectionSymTab<32> *>(object->getSectionByName(".symtab")); 91 92 if (!symtab) { 93 return NULL; 94 } 95 96 ELFSymbol<32> *symbol = symtab->getByName(name); 97 98 if (!symbol) { 99 LOGE("Symbol not found: %s\n", name); 100 return NULL; 101 } 102 103 return symbol->getAddress(false); 104} 105 106extern "C" size_t rsloaderGetSymbolSize(RSExecRef object_, char const *name) { 107 ELFObject<32> *object = unwrap(object_); 108 109 ELFSectionSymTab<32> *symtab = 110 static_cast<ELFSectionSymTab<32> *>(object->getSectionByName(".symtab")); 111 112 if (!symtab) { 113 return NULL; 114 } 115 116 ELFSymbol<32> *symbol = symtab->getByName(name); 117 118 if (!symbol) { 119 LOGE("Symbol not found: %s\n", name); 120 return NULL; 121 } 122 123 return (size_t)symbol->getSize(); 124} 125 126extern "C" size_t rsloaderGetFuncCount(RSExecRef object) { 127 ELFSectionSymTab<32> *symtab = static_cast<ELFSectionSymTab<32> *>( 128 unwrap(object)->getSectionByName(".symtab")); 129 130 if (!symtab) { 131 return 0; 132 } 133 134 return symtab->getFuncCount(); 135} 136 137extern "C" void rsloaderGetFuncNameList(RSExecRef object, 138 size_t size, 139 char const **list) { 140 ELFSectionSymTab<32> *symtab = static_cast<ELFSectionSymTab<32> *>( 141 unwrap(object)->getSectionByName(".symtab")); 142 143 if (symtab) { 144 symtab->getFuncNameList(size, list); 145 } 146} 147