blob: e282bce2213f5d4131d4db1b1275b0eab984b12a [file] [log] [blame]
[email protected]f1d81922010-07-31 17:47:091// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]f38e25f2009-04-21 00:56:072// 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"
[email protected]34b99632011-01-01 01:01:0611#include "base/threading/thread_restrictions.h"
[email protected]f1d81922010-07-31 17:47:0912#include "base/utf_string_conversions.h"
[email protected]f38e25f2009-04-21 00:56:0713
14namespace base {
15
16// static
17NativeLibrary LoadNativeLibrary(const FilePath& library_path) {
[email protected]be130682010-11-12 21:53:1618 // dlopen() opens the file off disk.
19 base::ThreadRestrictions::AssertIOAllowed();
20
[email protected]0ba5b7b2010-04-22 18:07:5021 // 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.
[email protected]de8dbce2009-07-29 00:06:2125 void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY);
[email protected]6dd64352009-06-29 23:36:2226 if (!dl) {
[email protected]0922baea2009-08-27 23:06:3427 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 }
[email protected]6dd64352009-06-29 23:36:2235 }
[email protected]f38e25f2009-04-21 00:56:0736
37 return dl;
38}
39
40// static
41void UnloadNativeLibrary(NativeLibrary library) {
42 int ret = dlclose(library);
[email protected]803ef4ef2009-07-29 00:15:2443 if (ret < 0) {
44 LOG(ERROR) << "dlclose failed: " << dlerror();
45 NOTREACHED();
46 }
[email protected]f38e25f2009-04-21 00:56:0747}
48
49// static
50void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
[email protected]108c2a12009-06-05 22:18:0951 const char* name) {
[email protected]f38e25f2009-04-21 00:56:0752 return dlsym(library, name);
53}
54
[email protected]108c2a12009-06-05 22:18:0955// static
56string16 GetNativeLibraryName(const string16& name) {
57 return ASCIIToUTF16("lib") + name + ASCIIToUTF16(".so");
58}
59
[email protected]f38e25f2009-04-21 00:56:0760} // namespace base