You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "talk/base/latebindingsymboltable.h"
  28. #ifdef POSIX
  29. #include <dlfcn.h>
  30. #endif
  31. #include "talk/base/logging.h"
  32. namespace talk_base {
  33. #ifdef POSIX
  34. static const DllHandle kInvalidDllHandle = NULL;
  35. #else
  36. #error Not implemented
  37. #endif
  38. static const char *GetDllError() {
  39. #ifdef POSIX
  40. const char *err = dlerror();
  41. if (err) {
  42. return err;
  43. } else {
  44. return "No error";
  45. }
  46. #else
  47. #error Not implemented
  48. #endif
  49. }
  50. static bool LoadSymbol(DllHandle handle,
  51. const char *symbol_name,
  52. void **symbol) {
  53. #ifdef POSIX
  54. *symbol = dlsym(handle, symbol_name);
  55. const char *err = dlerror();
  56. if (err) {
  57. LOG(LS_ERROR) << "Error loading symbol " << symbol_name << ": " << err;
  58. return false;
  59. } else if (!*symbol) {
  60. // ELF allows for symbols to be NULL, but that should never happen for our
  61. // usage.
  62. LOG(LS_ERROR) << "Symbol " << symbol_name << " is NULL";
  63. return false;
  64. }
  65. return true;
  66. #else
  67. #error Not implemented
  68. #endif
  69. }
  70. LateBindingSymbolTable::LateBindingSymbolTable(const TableInfo *info,
  71. void **table)
  72. : info_(info),
  73. table_(table),
  74. handle_(kInvalidDllHandle),
  75. undefined_symbols_(false) {
  76. ClearSymbols();
  77. }
  78. LateBindingSymbolTable::~LateBindingSymbolTable() {
  79. Unload();
  80. }
  81. bool LateBindingSymbolTable::IsLoaded() const {
  82. return handle_ != kInvalidDllHandle;
  83. }
  84. bool LateBindingSymbolTable::Load() {
  85. ASSERT(info_->dll_name != NULL);
  86. return LoadFromPath(info_->dll_name);
  87. }
  88. bool LateBindingSymbolTable::LoadFromPath(const char *dll_path) {
  89. if (IsLoaded()) {
  90. return true;
  91. }
  92. if (undefined_symbols_) {
  93. // We do not attempt to load again because repeated attempts are not
  94. // likely to succeed and DLL loading is costly.
  95. LOG(LS_ERROR) << "We know there are undefined symbols";
  96. return false;
  97. }
  98. #ifdef POSIX
  99. handle_ = dlopen(dll_path, RTLD_NOW);
  100. #else
  101. #error Not implemented
  102. #endif
  103. if (handle_ == kInvalidDllHandle) {
  104. LOG(LS_WARNING) << "Can't load " << dll_path << ": "
  105. << GetDllError();
  106. return false;
  107. }
  108. #ifdef POSIX
  109. // Clear any old errors.
  110. dlerror();
  111. #endif
  112. for (int i = 0; i < info_->num_symbols; ++i) {
  113. if (!LoadSymbol(handle_, info_->symbol_names[i], &table_[i])) {
  114. undefined_symbols_ = true;
  115. Unload();
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. void LateBindingSymbolTable::Unload() {
  122. if (!IsLoaded()) {
  123. return;
  124. }
  125. #ifdef POSIX
  126. if (dlclose(handle_) != 0) {
  127. LOG(LS_ERROR) << GetDllError();
  128. }
  129. #else
  130. #error Not implemented
  131. #endif
  132. handle_ = kInvalidDllHandle;
  133. ClearSymbols();
  134. }
  135. void LateBindingSymbolTable::ClearSymbols() {
  136. memset(table_, 0, sizeof(void *) * info_->num_symbols);
  137. }
  138. } // namespace talk_base