• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "rtc_base/logging.h"
14 
15 #ifdef WEBRTC_LINUX
16 #include <dlfcn.h>
17 #endif
18 
19 namespace webrtc {
20 namespace adm_linux {
21 
GetDllError()22 inline static const char* GetDllError() {
23 #ifdef WEBRTC_LINUX
24   char* err = dlerror();
25   if (err) {
26     return err;
27   } else {
28     return "No error";
29   }
30 #else
31 #error Not implemented
32 #endif
33 }
34 
InternalLoadDll(const char dll_name[])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) {
42     RTC_LOG(LS_WARNING) << "Can't load " << dll_name << " : " << GetDllError();
43   }
44   return handle;
45 }
46 
InternalUnloadDll(DllHandle handle)47 void InternalUnloadDll(DllHandle handle) {
48 #ifdef WEBRTC_LINUX
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
56 #if !defined(ADDRESS_SANITIZER)
57   if (dlclose(handle) != 0) {
58     RTC_LOG(LS_ERROR) << GetDllError();
59   }
60 #endif  // !defined(ADDRESS_SANITIZER)
61 #else
62 #error Not implemented
63 #endif
64 }
65 
LoadSymbol(DllHandle handle,const char * symbol_name,void ** symbol)66 static bool LoadSymbol(DllHandle handle,
67                        const char* symbol_name,
68                        void** symbol) {
69 #ifdef WEBRTC_LINUX
70   *symbol = dlsym(handle, symbol_name);
71   char* err = dlerror();
72   if (err) {
73     RTC_LOG(LS_ERROR) << "Error loading symbol " << symbol_name << " : " << err;
74     return false;
75   } else if (!*symbol) {
76     RTC_LOG(LS_ERROR) << "Symbol " << symbol_name << " is NULL";
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.
InternalLoadSymbols(DllHandle handle,int num_symbols,const char * const symbol_names[],void * symbols[])88 bool InternalLoadSymbols(DllHandle handle,
89                          int num_symbols,
90                          const char* const symbol_names[],
91                          void* symbols[]) {
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 
104 }  // namespace adm_linux
105 }  // namespace webrtc
106