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,
113 // RTLD_NOW front-loads symbol resolution so that errors are
114 // caught early instead of causing a process abort later.
115 // RTLD_LOCAL prevents other modules from automatically
116 // seeing symbol definitions in the newly-loaded tree. This
117 // is necessary for same-named symbols in different ABI
118 // versions of the same library to not explode.
121 // RTLD_DEEPBIND makes symbol dependencies in the
122 // newly-loaded tree prefer to resolve to definitions within
123 // that tree (the default on OS X). This is necessary for
124 // same-named symbols in different ABI versions of the same
125 // library to not explode.
130 #error Not implemented
133 if (handle_ == kInvalidDllHandle) {
134 LOG(LS_WARNING) << "Can't load " << dll_path << ": "
139 // Clear any old errors.
142 for (int i = 0; i < info_->num_symbols; ++i) {
143 if (!LoadSymbol(handle_, info_->symbol_names[i], &table_[i])) {
144 undefined_symbols_ = true;
152 void LateBindingSymbolTable::Unload() {
158 if (dlclose(handle_) != 0) {
159 LOG(LS_ERROR) << GetDllError();
162 #error Not implemented
165 handle_ = kInvalidDllHandle;
169 void LateBindingSymbolTable::ClearSymbols() {
170 memset(table_, 0, sizeof(void *) * info_->num_symbols);
173 } // namespace talk_base