native_library_linux.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2009 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/string_util.h"
12
13namespace base {
14
15// static
16NativeLibrary LoadNativeLibrary(const FilePath& library_path) {
17  // We deliberately do not use RTLD_DEEPBIND.  For the history why, please
18  // refer to the bug tracker.  Some useful bug reports to read include:
19  // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892,
20  // and http://crbug.com/40794.
21  void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY);
22  if (!dl) {
23    std::string error_message = dlerror();
24    // Some obsolete plugins depend on libxul or libxpcom.
25    // Ignore the error messages when failing to load these.
26    if (error_message.find("libxul.so") == std::string::npos &&
27        error_message.find("libxpcom.so") == std::string::npos) {
28      LOG(ERROR) << "dlopen failed when trying to open " << library_path.value()
29                 << ": " << error_message;
30    }
31  }
32
33  return dl;
34}
35
36// static
37void UnloadNativeLibrary(NativeLibrary library) {
38  int ret = dlclose(library);
39  if (ret < 0) {
40    LOG(ERROR) << "dlclose failed: " << dlerror();
41    NOTREACHED();
42  }
43}
44
45// static
46void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
47                                          const char* name) {
48  return dlsym(library, name);
49}
50
51// static
52string16 GetNativeLibraryName(const string16& name) {
53  return ASCIIToUTF16("lib") + name + ASCIIToUTF16(".so");
54}
55
56}  // namespace base
57