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 "webrtc/modules/audio_device/linux/latebindingsymboltable_linux.h"
12
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,
43               "Can't load %s : %s", dll_name, GetDllError());
44  }
45  return handle;
46}
47
48void 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    WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1,
60               "%s", GetDllError());
61  }
62#endif  // !defined(ADDRESS_SANITIZER)
63#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
109