native_library_linux.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/native_library.h"
6
7#include <dlfcn.h>
8
9#include "base/file_path.h"
10#include "base/logging.h"
11#include "base/threading/thread_restrictions.h"
12#include "base/utf_string_conversions.h"
13
14namespace base {
15
16// static
17NativeLibrary LoadNativeLibrary(const FilePath& library_path) {
18  // dlopen() opens the file off disk.
19  base::ThreadRestrictions::AssertIOAllowed();
20
21  // We deliberately do not use RTLD_DEEPBIND.  For the history why, please
22  // refer to the bug tracker.  Some useful bug reports to read include:
23  // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892,
24  // and http://crbug.com/40794.
25  void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY);
26  if (!dl) {
27    std::string error_message = dlerror();
28    // Some obsolete plugins depend on libxul or libxpcom.
29    // Ignore the error messages when failing to load these.
30    if (error_message.find("libxul.so") == std::string::npos &&
31        error_message.find("libxpcom.so") == std::string::npos) {
32      LOG(ERROR) << "dlopen failed when trying to open " << library_path.value()
33                 << ": " << error_message;
34    }
35  }
36
37  return dl;
38}
39
40// static
41void UnloadNativeLibrary(NativeLibrary library) {
42  int ret = dlclose(library);
43  if (ret < 0) {
44    LOG(ERROR) << "dlclose failed: " << dlerror();
45    NOTREACHED();
46  }
47}
48
49// static
50void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
51                                          const char* name) {
52  return dlsym(library, name);
53}
54
55// static
56string16 GetNativeLibraryName(const string16& name) {
57  return ASCIIToUTF16("lib") + name + ASCIIToUTF16(".so");
58}
59
60}  // namespace base
61