blob: bcc4ffb66d06cc79cf9dde80200f5500f6243fb0 [file] [log] [blame]
[email protected]84479322011-04-18 22:06:221// Copyright (c) 2011 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
[email protected]84479322011-04-18 22:06:2217NativeLibrary LoadNativeLibrary(const FilePath& library_path,
18 std::string* error) {
[email protected]be130682010-11-12 21:53:1619 // dlopen() opens the file off disk.
20 base::ThreadRestrictions::AssertIOAllowed();
21
[email protected]0ba5b7b2010-04-22 18:07:5022 // We deliberately do not use RTLD_DEEPBIND. For the history why, please
23 // refer to the bug tracker. Some useful bug reports to read include:
24 // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892,
25 // and http://crbug.com/40794.
[email protected]de8dbce2009-07-29 00:06:2126 void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY);
[email protected]84479322011-04-18 22:06:2227 if (!dl && error)
28 *error = dlerror();
[email protected]f38e25f2009-04-21 00:56:0729
30 return dl;
31}
32
33// static
34void UnloadNativeLibrary(NativeLibrary library) {
35 int ret = dlclose(library);
[email protected]803ef4ef2009-07-29 00:15:2436 if (ret < 0) {
37 LOG(ERROR) << "dlclose failed: " << dlerror();
38 NOTREACHED();
39 }
[email protected]f38e25f2009-04-21 00:56:0740}
41
42// static
43void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
[email protected]108c2a12009-06-05 22:18:0944 const char* name) {
[email protected]f38e25f2009-04-21 00:56:0745 return dlsym(library, name);
46}
47
[email protected]108c2a12009-06-05 22:18:0948// static
49string16 GetNativeLibraryName(const string16& name) {
50 return ASCIIToUTF16("lib") + name + ASCIIToUTF16(".so");
51}
52
[email protected]f38e25f2009-04-21 00:56:0753} // namespace base