blob: def4866d5a7e937828485abeaa9b37fe76753998 [file] [log] [blame]
xians@google.com68efa212011-08-11 12:41:56 +00001/*
kjellander@webrtc.orga9e363e2014-10-07 12:49:34 +00002 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
xians@google.com68efa212011-08-11 12:41:56 +00003 *
kjellander@webrtc.orga9e363e2014-10-07 12:49:34 +00004 * 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.com68efa212011-08-11 12:41:56 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_device/linux/latebindingsymboltable_linux.h"
xians@google.com68efa212011-08-11 12:41:56 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/logging.h"
nisse0ffdcc52017-03-30 00:31:15 -070014
xians@google.com68efa212011-08-11 12:41:56 +000015#ifdef WEBRTC_LINUX
16#include <dlfcn.h>
17#endif
18
kwiberg6daffe22017-04-18 01:26:40 -070019namespace webrtc {
20namespace adm_linux {
xians@google.com68efa212011-08-11 12:41:56 +000021
Mirko Bonadei72c42502017-11-09 09:33:23 +010022inline static const char* GetDllError() {
xians@google.com68efa212011-08-11 12:41:56 +000023#ifdef WEBRTC_LINUX
Mirko Bonadei72c42502017-11-09 09:33:23 +010024 char* err = dlerror();
xians@google.com68efa212011-08-11 12:41:56 +000025 if (err) {
26 return err;
27 } else {
28 return "No error";
29 }
30#else
31#error Not implemented
32#endif
33}
34
35DllHandle 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 Bonadei675513b2017-11-09 11:09:25 +010042 RTC_LOG(LS_WARNING) << "Can't load " << dll_name << " : " << GetDllError();
xians@google.com68efa212011-08-11 12:41:56 +000043 }
44 return handle;
45}
46
47void InternalUnloadDll(DllHandle handle) {
48#ifdef WEBRTC_LINUX
pbos@webrtc.orgf520ea52014-09-11 17:29:11 +000049// 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.org3663fb02015-01-07 18:02:39 +000056#if !defined(ADDRESS_SANITIZER)
xians@google.com68efa212011-08-11 12:41:56 +000057 if (dlclose(handle) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010058 RTC_LOG(LS_ERROR) << GetDllError();
xians@google.com68efa212011-08-11 12:41:56 +000059 }
pbos@webrtc.org3663fb02015-01-07 18:02:39 +000060#endif // !defined(ADDRESS_SANITIZER)
xians@google.com68efa212011-08-11 12:41:56 +000061#else
62#error Not implemented
63#endif
64}
65
66static bool LoadSymbol(DllHandle handle,
Mirko Bonadei72c42502017-11-09 09:33:23 +010067 const char* symbol_name,
68 void** symbol) {
xians@google.com68efa212011-08-11 12:41:56 +000069#ifdef WEBRTC_LINUX
70 *symbol = dlsym(handle, symbol_name);
Mirko Bonadei72c42502017-11-09 09:33:23 +010071 char* err = dlerror();
xians@google.com68efa212011-08-11 12:41:56 +000072 if (err) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010073 RTC_LOG(LS_ERROR) << "Error loading symbol " << symbol_name << " : " << err;
xians@google.com68efa212011-08-11 12:41:56 +000074 return false;
75 } else if (!*symbol) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010076 RTC_LOG(LS_ERROR) << "Symbol " << symbol_name << " is NULL";
xians@google.com68efa212011-08-11 12:41:56 +000077 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.
88bool InternalLoadSymbols(DllHandle handle,
89 int num_symbols,
Mirko Bonadei72c42502017-11-09 09:33:23 +010090 const char* const symbol_names[],
91 void* symbols[]) {
xians@google.com68efa212011-08-11 12:41:56 +000092#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
kwiberg6daffe22017-04-18 01:26:40 -0700104} // namespace adm_linux
105} // namespace webrtc