3 * Copyright 2004--2010, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/latebindingsymboltable.h"
34 #include "talk/base/logging.h"
39 static const DllHandle kInvalidDllHandle = NULL;
41 #error Not implemented
44 static const char *GetDllError() {
46 const char *err = dlerror();
53 #error Not implemented
57 static bool LoadSymbol(DllHandle handle,
58 const char *symbol_name,
61 *symbol = dlsym(handle, symbol_name);
62 const char *err = dlerror();
64 LOG(LS_ERROR) << "Error loading symbol " << symbol_name << ": " << err;
66 } else if (!*symbol) {
67 // ELF allows for symbols to be NULL, but that should never happen for our
69 LOG(LS_ERROR) << "Symbol " << symbol_name << " is NULL";
74 #error Not implemented
78 LateBindingSymbolTable::LateBindingSymbolTable(const TableInfo *info,
82 handle_(kInvalidDllHandle),
83 undefined_symbols_(false) {
87 LateBindingSymbolTable::~LateBindingSymbolTable() {
91 bool LateBindingSymbolTable::IsLoaded() const {
92 return handle_ != kInvalidDllHandle;
95 bool LateBindingSymbolTable::Load() {
96 ASSERT(info_->dll_name != NULL);
97 return LoadFromPath(info_->dll_name);
100 bool LateBindingSymbolTable::LoadFromPath(const char *dll_path) {
104 if (undefined_symbols_) {
105 // We do not attempt to load again because repeated attempts are not
106 // likely to succeed and DLL loading is costly.
107 LOG(LS_ERROR) << "We know there are undefined symbols";
112 handle_ = dlopen(dll_path, RTLD_NOW);
114 #error Not implemented
117 if (handle_ == kInvalidDllHandle) {
118 LOG(LS_WARNING) << "Can't load " << dll_path << ": "
123 // Clear any old errors.
126 for (int i = 0; i < info_->num_symbols; ++i) {
127 if (!LoadSymbol(handle_, info_->symbol_names[i], &table_[i])) {
128 undefined_symbols_ = true;
136 void LateBindingSymbolTable::Unload() {
142 if (dlclose(handle_) != 0) {
143 LOG(LS_ERROR) << GetDllError();
146 #error Not implemented
149 handle_ = kInvalidDllHandle;
153 void LateBindingSymbolTable::ClearSymbols() {
154 memset(table_, 0, sizeof(void *) * info_->num_symbols);
157 } // namespace talk_base