[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 2 | // 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] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 10 | #include "base/logging.h" |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 13 | #include "base/threading/scoped_blocking_call.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 14 | |
| 15 | namespace base { |
| 16 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 17 | std::string NativeLibraryLoadError::ToString() const { |
| 18 | return message; |
| 19 | } |
| 20 | |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 21 | NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path, |
| 22 | const NativeLibraryOptions& options, |
| 23 | NativeLibraryLoadError* error) { |
[email protected] | be13068 | 2010-11-12 21:53:16 | [diff] [blame] | 24 | // dlopen() opens the file off disk. |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 25 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
[email protected] | be13068 | 2010-11-12 21:53:16 | [diff] [blame] | 26 | |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 27 | // We deliberately do not use RTLD_DEEPBIND by default. For the history why, |
| 28 | // please refer to the bug tracker. Some useful bug reports to read include: |
[email protected] | 0ba5b7b | 2010-04-22 18:07:50 | [diff] [blame] | 29 | // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892, |
| 30 | // and http://crbug.com/40794. |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 31 | int flags = RTLD_LAZY; |
slan | 76e9f56 | 2016-10-03 19:30:26 | [diff] [blame] | 32 | #if defined(OS_ANDROID) || !defined(RTLD_DEEPBIND) |
| 33 | // Certain platforms don't define RTLD_DEEPBIND. Android dlopen() requires |
| 34 | // further investigation, as it might vary across versions. Crash here to |
| 35 | // warn developers that they're trying to rely on uncertain behavior. |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 36 | CHECK(!options.prefer_own_symbols); |
| 37 | #else |
| 38 | if (options.prefer_own_symbols) |
| 39 | flags |= RTLD_DEEPBIND; |
| 40 | #endif |
| 41 | void* dl = dlopen(library_path.value().c_str(), flags); |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 42 | if (!dl && error) |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 43 | error->message = dlerror(); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 44 | |
| 45 | return dl; |
| 46 | } |
| 47 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 48 | void UnloadNativeLibrary(NativeLibrary library) { |
| 49 | int ret = dlclose(library); |
[email protected] | 803ef4ef | 2009-07-29 00:15:24 | [diff] [blame] | 50 | if (ret < 0) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 51 | DLOG(ERROR) << "dlclose failed: " << dlerror(); |
[email protected] | 803ef4ef | 2009-07-29 00:15:24 | [diff] [blame] | 52 | NOTREACHED(); |
| 53 | } |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 56 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 57 | StringPiece name) { |
| 58 | return dlsym(library, name.data()); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 59 | } |
| 60 | |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 61 | std::string GetNativeLibraryName(StringPiece name) { |
| 62 | DCHECK(IsStringASCII(name)); |
| 63 | return "lib" + name.as_string() + ".so"; |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 64 | } |
| 65 | |
Xiaohan Wang | d807ec3 | 2018-04-03 01:31:44 | [diff] [blame] | 66 | std::string GetLoadableModuleName(StringPiece name) { |
| 67 | return GetNativeLibraryName(name); |
| 68 | } |
| 69 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 70 | } // namespace base |