blob: 3179a93833c0ad20beafe800d8e24d3a21d127b5 [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
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
[email protected]f38e25f2009-04-21 00:56:0710#include "base/logging.h"
[email protected]a4ea1f12013-06-07 18:37:0711#include "base/strings/utf_string_conversions.h"
[email protected]34b99632011-01-01 01:01:0612#include "base/threading/thread_restrictions.h"
[email protected]f38e25f2009-04-21 00:56:0713
14namespace base {
15
[email protected]0f998442014-03-25 01:59:0916std::string NativeLibraryLoadError::ToString() const {
17 return message;
18}
19
[email protected]f38e25f2009-04-21 00:56:0720// static
[email protected]84479322011-04-18 22:06:2221NativeLibrary LoadNativeLibrary(const FilePath& library_path,
[email protected]0f998442014-03-25 01:59:0922 NativeLibraryLoadError* error) {
[email protected]be130682010-11-12 21:53:1623 // dlopen() opens the file off disk.
24 base::ThreadRestrictions::AssertIOAllowed();
25
[email protected]0ba5b7b2010-04-22 18:07:5026 // We deliberately do not use RTLD_DEEPBIND. For the history why, please
27 // refer to the bug tracker. Some useful bug reports to read include:
28 // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892,
29 // and http://crbug.com/40794.
[email protected]de8dbce2009-07-29 00:06:2130 void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY);
[email protected]84479322011-04-18 22:06:2231 if (!dl && error)
[email protected]0f998442014-03-25 01:59:0932 error->message = dlerror();
[email protected]f38e25f2009-04-21 00:56:0733
34 return dl;
35}
36
37// static
38void UnloadNativeLibrary(NativeLibrary library) {
39 int ret = dlclose(library);
[email protected]803ef4ef2009-07-29 00:15:2440 if (ret < 0) {
[email protected]a42d4632011-10-26 21:48:0041 DLOG(ERROR) << "dlclose failed: " << dlerror();
[email protected]803ef4ef2009-07-29 00:15:2442 NOTREACHED();
43 }
[email protected]f38e25f2009-04-21 00:56:0744}
45
46// static
47void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
[email protected]108c2a12009-06-05 22:18:0948 const char* name) {
[email protected]f38e25f2009-04-21 00:56:0749 return dlsym(library, name);
50}
51
[email protected]108c2a12009-06-05 22:18:0952// static
53string16 GetNativeLibraryName(const string16& name) {
54 return ASCIIToUTF16("lib") + name + ASCIIToUTF16(".so");
55}
56
[email protected]f38e25f2009-04-21 00:56:0757} // namespace base