blob: 2dc434b7be25af2395a80b313defa3e8be08616f [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"
thestig02c965b2016-06-14 18:52:2311#include "base/strings/string_util.h"
[email protected]a4ea1f12013-06-07 18:37:0712#include "base/strings/utf_string_conversions.h"
[email protected]34b99632011-01-01 01:01:0613#include "base/threading/thread_restrictions.h"
[email protected]f38e25f2009-04-21 00:56:0714
15namespace base {
16
[email protected]0f998442014-03-25 01:59:0917std::string NativeLibraryLoadError::ToString() const {
18 return message;
19}
20
[email protected]f38e25f2009-04-21 00:56:0721// static
[email protected]84479322011-04-18 22:06:2222NativeLibrary LoadNativeLibrary(const FilePath& library_path,
[email protected]0f998442014-03-25 01:59:0923 NativeLibraryLoadError* error) {
[email protected]be130682010-11-12 21:53:1624 // dlopen() opens the file off disk.
thestige38fbd62016-06-10 21:54:4025 ThreadRestrictions::AssertIOAllowed();
[email protected]be130682010-11-12 21:53:1626
[email protected]0ba5b7b2010-04-22 18:07:5027 // We deliberately do not use RTLD_DEEPBIND. For the history why, please
28 // refer to the bug tracker. Some useful bug reports to read include:
29 // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892,
30 // and http://crbug.com/40794.
[email protected]de8dbce2009-07-29 00:06:2131 void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY);
[email protected]84479322011-04-18 22:06:2232 if (!dl && error)
[email protected]0f998442014-03-25 01:59:0933 error->message = dlerror();
[email protected]f38e25f2009-04-21 00:56:0734
35 return dl;
36}
37
38// static
39void UnloadNativeLibrary(NativeLibrary library) {
40 int ret = dlclose(library);
[email protected]803ef4ef2009-07-29 00:15:2441 if (ret < 0) {
[email protected]a42d4632011-10-26 21:48:0042 DLOG(ERROR) << "dlclose failed: " << dlerror();
[email protected]803ef4ef2009-07-29 00:15:2443 NOTREACHED();
44 }
[email protected]f38e25f2009-04-21 00:56:0745}
46
47// static
48void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
thestige38fbd62016-06-10 21:54:4049 StringPiece name) {
50 return dlsym(library, name.data());
[email protected]f38e25f2009-04-21 00:56:0751}
52
[email protected]108c2a12009-06-05 22:18:0953// static
thestig02c965b2016-06-14 18:52:2354std::string GetNativeLibraryName(StringPiece name) {
55 DCHECK(IsStringASCII(name));
56 return "lib" + name.as_string() + ".so";
[email protected]108c2a12009-06-05 22:18:0957}
58
[email protected]f38e25f2009-04-21 00:56:0759} // namespace base