Zonr Chang | ee69009 | 2012-04-12 12:19:15 +0800 | [diff] [blame] | 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 | |
Stephen Hines | 2f6a493 | 2012-05-03 12:27:13 -0700 | [diff] [blame] | 17 | #include "SymbolResolvers.h" |
Zonr Chang | ee69009 | 2012-04-12 12:19:15 +0800 | [diff] [blame] | 18 | |
| 19 | #include <dlfcn.h> |
| 20 | |
| 21 | #include <cassert> |
| 22 | #include <cstdio> |
| 23 | #include <new> |
| 24 | |
| 25 | using namespace bcc; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // DyldSymbolResolver |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | DyldSymbolResolver::DyldSymbolResolver(const char *pFileName, |
| 31 | bool pLazyBinding) : mError(NULL) { |
| 32 | int flags = (pLazyBinding) ? RTLD_LAZY : RTLD_NOW; |
| 33 | |
| 34 | // Make the symbol within the given library to be local such that it won't |
| 35 | // be available for symbol resolution of subsequently loaded libraries. |
| 36 | flags |= RTLD_LOCAL; |
| 37 | |
| 38 | mHandle = ::dlopen(pFileName, flags); |
| 39 | if (mHandle == NULL) { |
| 40 | const char *err = ::dlerror(); |
| 41 | |
| 42 | #define DYLD_ERROR_MSG_PATTERN "Failed to load %s! (%s)" |
| 43 | size_t error_length = ::strlen(DYLD_ERROR_MSG_PATTERN) + |
| 44 | ::strlen(pFileName) + 1; |
| 45 | if (err != NULL) { |
| 46 | error_length += ::strlen(err); |
| 47 | } |
| 48 | |
| 49 | mError = new (std::nothrow) char [error_length]; |
| 50 | if (mError != NULL) { |
| 51 | ::snprintf(mError, error_length, DYLD_ERROR_MSG_PATTERN, pFileName, |
| 52 | ((err != NULL) ? err : "")); |
| 53 | } |
| 54 | } |
| 55 | #undef DYLD_ERROR_MSG_PATTERN |
| 56 | } |
| 57 | |
| 58 | void *DyldSymbolResolver::getAddress(const char *pName) { |
| 59 | assert((mHandle != NULL) && "Invalid DyldSymbolResolver!"); |
| 60 | return ::dlsym(mHandle, pName); |
| 61 | } |
| 62 | |
| 63 | DyldSymbolResolver::~DyldSymbolResolver() { |
| 64 | ::dlclose(mHandle); |
| 65 | delete [] mError; |
| 66 | } |