blob: 3459716af1c68d550fe246b70d3d70979a736efd [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
rockot596a0dd2016-08-26 00:57:5122NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
23 const NativeLibraryOptions& options,
24 NativeLibraryLoadError* error) {
[email protected]be130682010-11-12 21:53:1625 // dlopen() opens the file off disk.
thestige38fbd62016-06-10 21:54:4026 ThreadRestrictions::AssertIOAllowed();
[email protected]be130682010-11-12 21:53:1627
rockot596a0dd2016-08-26 00:57:5128 // We deliberately do not use RTLD_DEEPBIND by default. For the history why,
29 // please refer to the bug tracker. Some useful bug reports to read include:
[email protected]0ba5b7b2010-04-22 18:07:5030 // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892,
31 // and http://crbug.com/40794.
rockot596a0dd2016-08-26 00:57:5132 int flags = RTLD_LAZY;
slan76e9f562016-10-03 19:30:2633#if defined(OS_ANDROID) || !defined(RTLD_DEEPBIND)
34 // Certain platforms don't define RTLD_DEEPBIND. Android dlopen() requires
35 // further investigation, as it might vary across versions. Crash here to
36 // warn developers that they're trying to rely on uncertain behavior.
rockot596a0dd2016-08-26 00:57:5137 CHECK(!options.prefer_own_symbols);
38#else
39 if (options.prefer_own_symbols)
40 flags |= RTLD_DEEPBIND;
41#endif
42 void* dl = dlopen(library_path.value().c_str(), flags);
[email protected]84479322011-04-18 22:06:2243 if (!dl && error)
[email protected]0f998442014-03-25 01:59:0944 error->message = dlerror();
[email protected]f38e25f2009-04-21 00:56:0745
46 return dl;
47}
48
49// static
50void UnloadNativeLibrary(NativeLibrary library) {
51 int ret = dlclose(library);
[email protected]803ef4ef2009-07-29 00:15:2452 if (ret < 0) {
[email protected]a42d4632011-10-26 21:48:0053 DLOG(ERROR) << "dlclose failed: " << dlerror();
[email protected]803ef4ef2009-07-29 00:15:2454 NOTREACHED();
55 }
[email protected]f38e25f2009-04-21 00:56:0756}
57
58// static
59void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
thestige38fbd62016-06-10 21:54:4060 StringPiece name) {
61 return dlsym(library, name.data());
[email protected]f38e25f2009-04-21 00:56:0762}
63
[email protected]108c2a12009-06-05 22:18:0964// static
thestig02c965b2016-06-14 18:52:2365std::string GetNativeLibraryName(StringPiece name) {
66 DCHECK(IsStringASCII(name));
67 return "lib" + name.as_string() + ".so";
[email protected]108c2a12009-06-05 22:18:0968}
69
[email protected]f38e25f2009-04-21 00:56:0770} // namespace base