xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 1 | /* |
kjellander@webrtc.org | a9e363e | 2014-10-07 12:49:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 3 | * |
kjellander@webrtc.org | a9e363e | 2014-10-07 12:49:34 +0000 | [diff] [blame] | 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. |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_device/linux/latebindingsymboltable_linux.h" |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/logging.h" |
nisse | 0ffdcc5 | 2017-03-30 00:31:15 -0700 | [diff] [blame] | 14 | |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 15 | #ifdef WEBRTC_LINUX |
| 16 | #include <dlfcn.h> |
| 17 | #endif |
| 18 | |
kwiberg | 6daffe2 | 2017-04-18 01:26:40 -0700 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | namespace adm_linux { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 21 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 22 | inline static const char* GetDllError() { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 23 | #ifdef WEBRTC_LINUX |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 24 | char* err = dlerror(); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 25 | if (err) { |
| 26 | return err; |
| 27 | } else { |
| 28 | return "No error"; |
| 29 | } |
| 30 | #else |
| 31 | #error Not implemented |
| 32 | #endif |
| 33 | } |
| 34 | |
| 35 | DllHandle InternalLoadDll(const char dll_name[]) { |
| 36 | #ifdef WEBRTC_LINUX |
| 37 | DllHandle handle = dlopen(dll_name, RTLD_NOW); |
| 38 | #else |
| 39 | #error Not implemented |
| 40 | #endif |
| 41 | if (handle == kInvalidDllHandle) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 42 | RTC_LOG(LS_WARNING) << "Can't load " << dll_name << " : " << GetDllError(); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 43 | } |
| 44 | return handle; |
| 45 | } |
| 46 | |
| 47 | void InternalUnloadDll(DllHandle handle) { |
| 48 | #ifdef WEBRTC_LINUX |
pbos@webrtc.org | f520ea5 | 2014-09-11 17:29:11 +0000 | [diff] [blame] | 49 | // TODO(pbos): Remove this dlclose() exclusion when leaks and suppressions from |
| 50 | // here are gone (or AddressSanitizer can display them properly). |
| 51 | // |
| 52 | // Skip dlclose() on AddressSanitizer as leaks including this module in the |
| 53 | // stack trace gets displayed as <unknown module> instead of the actual library |
| 54 | // -> it can not be suppressed. |
| 55 | // https://code.google.com/p/address-sanitizer/issues/detail?id=89 |
pbos@webrtc.org | 3663fb0 | 2015-01-07 18:02:39 +0000 | [diff] [blame] | 56 | #if !defined(ADDRESS_SANITIZER) |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 57 | if (dlclose(handle) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 58 | RTC_LOG(LS_ERROR) << GetDllError(); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 59 | } |
pbos@webrtc.org | 3663fb0 | 2015-01-07 18:02:39 +0000 | [diff] [blame] | 60 | #endif // !defined(ADDRESS_SANITIZER) |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 61 | #else |
| 62 | #error Not implemented |
| 63 | #endif |
| 64 | } |
| 65 | |
| 66 | static bool LoadSymbol(DllHandle handle, |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 67 | const char* symbol_name, |
| 68 | void** symbol) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 69 | #ifdef WEBRTC_LINUX |
| 70 | *symbol = dlsym(handle, symbol_name); |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 71 | char* err = dlerror(); |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 72 | if (err) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 73 | RTC_LOG(LS_ERROR) << "Error loading symbol " << symbol_name << " : " << err; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 74 | return false; |
| 75 | } else if (!*symbol) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 76 | RTC_LOG(LS_ERROR) << "Symbol " << symbol_name << " is NULL"; |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | return true; |
| 80 | #else |
| 81 | #error Not implemented |
| 82 | #endif |
| 83 | } |
| 84 | |
| 85 | // This routine MUST assign SOME value for every symbol, even if that value is |
| 86 | // NULL, or else some symbols may be left with uninitialized data that the |
| 87 | // caller may later interpret as a valid address. |
| 88 | bool InternalLoadSymbols(DllHandle handle, |
| 89 | int num_symbols, |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 90 | const char* const symbol_names[], |
| 91 | void* symbols[]) { |
xians@google.com | 68efa21 | 2011-08-11 12:41:56 +0000 | [diff] [blame] | 92 | #ifdef WEBRTC_LINUX |
| 93 | // Clear any old errors. |
| 94 | dlerror(); |
| 95 | #endif |
| 96 | for (int i = 0; i < num_symbols; ++i) { |
| 97 | if (!LoadSymbol(handle, symbol_names[i], &symbols[i])) { |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
kwiberg | 6daffe2 | 2017-04-18 01:26:40 -0700 | [diff] [blame] | 104 | } // namespace adm_linux |
| 105 | } // namespace webrtc |