blob: 6e1b9761d9ff82adbbcbace1033bbfaf0b131233 [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
pbos@webrtc.org811269d2013-07-11 13:24:38 +000011#include "webrtc/modules/audio_device/linux/latebindingsymboltable_linux.h"
xians@google.com68efa212011-08-11 12:41:56 +000012
13#ifdef WEBRTC_LINUX
14#include <dlfcn.h>
15#endif
16
17// TODO(grunell): Either put inside webrtc namespace or use webrtc:: instead.
18using namespace webrtc;
19
20namespace webrtc_adm_linux {
21
22inline 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
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) {
42 WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, -1,
fischman@webrtc.orgf61e02c2013-02-20 23:13:46 +000043 "Can't load %s : %s", dll_name, GetDllError());
xians@google.com68efa212011-08-11 12:41:56 +000044 }
45 return handle;
46}
47
48void InternalUnloadDll(DllHandle handle) {
49#ifdef WEBRTC_LINUX
pbos@webrtc.orgf520ea52014-09-11 17:29:11 +000050// 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
pbos@webrtc.org3663fb02015-01-07 18:02:39 +000057#if !defined(ADDRESS_SANITIZER)
xians@google.com68efa212011-08-11 12:41:56 +000058 if (dlclose(handle) != 0) {
59 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1,
fischman@webrtc.orgf61e02c2013-02-20 23:13:46 +000060 "%s", GetDllError());
xians@google.com68efa212011-08-11 12:41:56 +000061 }
pbos@webrtc.org3663fb02015-01-07 18:02:39 +000062#endif // !defined(ADDRESS_SANITIZER)
xians@google.com68efa212011-08-11 12:41:56 +000063#else
64#error Not implemented
65#endif
66}
67
68static bool LoadSymbol(DllHandle handle,
69 const char *symbol_name,
70 void **symbol) {
71#ifdef WEBRTC_LINUX
72 *symbol = dlsym(handle, symbol_name);
73 char *err = dlerror();
74 if (err) {
75 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1,
76 "Error loading symbol %s : %d", symbol_name, err);
77 return false;
78 } else if (!*symbol) {
79 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1,
80 "Symbol %s is NULL", symbol_name);
81 return false;
82 }
83 return true;
84#else
85#error Not implemented
86#endif
87}
88
89// This routine MUST assign SOME value for every symbol, even if that value is
90// NULL, or else some symbols may be left with uninitialized data that the
91// caller may later interpret as a valid address.
92bool InternalLoadSymbols(DllHandle handle,
93 int num_symbols,
94 const char *const symbol_names[],
95 void *symbols[]) {
96#ifdef WEBRTC_LINUX
97 // Clear any old errors.
98 dlerror();
99#endif
100 for (int i = 0; i < num_symbols; ++i) {
101 if (!LoadSymbol(handle, symbol_names[i], &symbols[i])) {
102 return false;
103 }
104 }
105 return true;
106}
107
108} // namespace webrtc_adm_linux