- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / latebindingsymboltable.cc
1 /*
2  * libjingle
3  * Copyright 2004--2010, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
15  *
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.
26  */
27
28 #include "talk/base/latebindingsymboltable.h"
29
30 #ifdef POSIX
31 #include <dlfcn.h>
32 #endif
33
34 #include "talk/base/logging.h"
35
36 namespace talk_base {
37
38 #ifdef POSIX
39 static const DllHandle kInvalidDllHandle = NULL;
40 #else
41 #error Not implemented
42 #endif
43
44 static const char *GetDllError() {
45 #ifdef POSIX
46   const char *err = dlerror();
47   if (err) {
48     return err;
49   } else {
50     return "No error";
51   }
52 #else
53 #error Not implemented
54 #endif
55 }
56
57 static bool LoadSymbol(DllHandle handle,
58                        const char *symbol_name,
59                        void **symbol) {
60 #ifdef POSIX
61   *symbol = dlsym(handle, symbol_name);
62   const char *err = dlerror();
63   if (err) {
64     LOG(LS_ERROR) << "Error loading symbol " << symbol_name << ": " << err;
65     return false;
66   } else if (!*symbol) {
67     // ELF allows for symbols to be NULL, but that should never happen for our
68     // usage.
69     LOG(LS_ERROR) << "Symbol " << symbol_name << " is NULL";
70     return false;
71   }
72   return true;
73 #else
74 #error Not implemented
75 #endif
76 }
77
78 LateBindingSymbolTable::LateBindingSymbolTable(const TableInfo *info,
79     void **table)
80     : info_(info),
81       table_(table),
82       handle_(kInvalidDllHandle),
83       undefined_symbols_(false) {
84   ClearSymbols();
85 }
86
87 LateBindingSymbolTable::~LateBindingSymbolTable() {
88   Unload();
89 }
90
91 bool LateBindingSymbolTable::IsLoaded() const {
92   return handle_ != kInvalidDllHandle;
93 }
94
95 bool LateBindingSymbolTable::Load() {
96   ASSERT(info_->dll_name != NULL);
97   return LoadFromPath(info_->dll_name);
98 }
99
100 bool LateBindingSymbolTable::LoadFromPath(const char *dll_path) {
101   if (IsLoaded()) {
102     return true;
103   }
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";
108     return false;
109   }
110
111 #ifdef POSIX
112   handle_ = dlopen(dll_path, RTLD_NOW);
113 #else
114 #error Not implemented
115 #endif
116
117   if (handle_ == kInvalidDllHandle) {
118     LOG(LS_WARNING) << "Can't load " << dll_path << ": "
119                     << GetDllError();
120     return false;
121   }
122 #ifdef POSIX
123   // Clear any old errors.
124   dlerror();
125 #endif
126   for (int i = 0; i < info_->num_symbols; ++i) {
127     if (!LoadSymbol(handle_, info_->symbol_names[i], &table_[i])) {
128       undefined_symbols_ = true;
129       Unload();
130       return false;
131     }
132   }
133   return true;
134 }
135
136 void LateBindingSymbolTable::Unload() {
137   if (!IsLoaded()) {
138     return;
139   }
140
141 #ifdef POSIX
142   if (dlclose(handle_) != 0) {
143     LOG(LS_ERROR) << GetDllError();
144   }
145 #else
146 #error Not implemented
147 #endif
148
149   handle_ = kInvalidDllHandle;
150   ClearSymbols();
151 }
152
153 void LateBindingSymbolTable::ClearSymbols() {
154   memset(table_, 0, sizeof(void *) * info_->num_symbols);
155 }
156
157 }  // namespace talk_base