latebindingsymboltable_linux.cc (3006B)
1 /* 2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "modules/audio_device/linux/latebindingsymboltable_linux.h" 12 13 #include <string> 14 15 #include "absl/strings/string_view.h" 16 #include "rtc_base/logging.h" 17 18 #ifdef WEBRTC_LINUX 19 #include <dlfcn.h> 20 #endif 21 22 namespace webrtc { 23 namespace adm_linux { 24 25 inline static const char* GetDllError() { 26 #ifdef WEBRTC_LINUX 27 char* err = dlerror(); 28 if (err) { 29 return err; 30 } else { 31 return "No error"; 32 } 33 #else 34 #error Not implemented 35 #endif 36 } 37 38 DllHandle InternalLoadDll(absl::string_view dll_name) { 39 #ifdef WEBRTC_LINUX 40 DllHandle handle = dlopen(std::string(dll_name).c_str(), RTLD_NOW); 41 #else 42 #error Not implemented 43 #endif 44 if (handle == kInvalidDllHandle) { 45 RTC_LOG(LS_WARNING) << "Can't load " << dll_name << " : " << GetDllError(); 46 } 47 return handle; 48 } 49 50 void InternalUnloadDll(DllHandle handle) { 51 #ifdef WEBRTC_LINUX 52 // TODO(pbos): Remove this dlclose() exclusion when leaks and suppressions from 53 // here are gone (or AddressSanitizer can display them properly). 54 // 55 // Skip dlclose() on AddressSanitizer as leaks including this module in the 56 // stack trace gets displayed as <unknown module> instead of the actual library 57 // -> it can not be suppressed. 58 // https://code.google.com/p/address-sanitizer/issues/detail?id=89 59 #if !defined(ADDRESS_SANITIZER) 60 if (dlclose(handle) != 0) { 61 RTC_LOG(LS_ERROR) << GetDllError(); 62 } 63 #endif // !defined(ADDRESS_SANITIZER) 64 #else 65 #error Not implemented 66 #endif 67 } 68 69 static bool LoadSymbol(DllHandle handle, 70 absl::string_view symbol_name, 71 void** symbol) { 72 #ifdef WEBRTC_LINUX 73 *symbol = dlsym(handle, std::string(symbol_name).c_str()); 74 char* err = dlerror(); 75 if (err) { 76 RTC_LOG(LS_ERROR) << "Error loading symbol " << symbol_name << " : " << err; 77 return false; 78 } else if (!*symbol) { 79 RTC_LOG(LS_ERROR) << "Symbol " << symbol_name << " is NULL"; 80 return false; 81 } 82 return true; 83 #else 84 #error Not implemented 85 #endif 86 } 87 88 // This routine MUST assign SOME value for every symbol, even if that value is 89 // NULL, or else some symbols may be left with uninitialized data that the 90 // caller may later interpret as a valid address. 91 bool InternalLoadSymbols(DllHandle handle, 92 int num_symbols, 93 const char* const symbol_names[], 94 void* symbols[]) { 95 #ifdef WEBRTC_LINUX 96 // Clear any old errors. 97 dlerror(); 98 #endif 99 for (int i = 0; i < num_symbols; ++i) { 100 if (!LoadSymbol(handle, symbol_names[i], &symbols[i])) { 101 return false; 102 } 103 } 104 return true; 105 } 106 107 } // namespace adm_linux 108 } // namespace webrtc