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