[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [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 | #ifndef BASE_NATIVE_LIBRARY_H_ |
| 6 | #define BASE_NATIVE_LIBRARY_H_ |
| 7 | |
| 8 | // This file defines a cross-platform "NativeLibrary" type which represents |
| 9 | // a loadable module. |
| 10 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 11 | #include <string> |
| 12 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 13 | #include "base/base_export.h" |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 15 | #include "base/strings/string_piece.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 16 | #include "build/build_config.h" |
| 17 | |
| 18 | #if defined(OS_WIN) |
| 19 | #include <windows.h> |
Avi Drissman | 5b28637 | 2020-07-28 21:59:38 | [diff] [blame] | 20 | #elif defined(OS_APPLE) |
[email protected] | 6770f2a | 2010-11-15 21:22:55 | [diff] [blame] | 21 | #import <CoreFoundation/CoreFoundation.h> |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 22 | #endif // OS_* |
| 23 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 24 | namespace base { |
| 25 | |
| 26 | #if defined(OS_WIN) |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 27 | using NativeLibrary = HMODULE; |
Avi Drissman | 5b28637 | 2020-07-28 21:59:38 | [diff] [blame] | 28 | #elif defined(OS_APPLE) |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 29 | enum NativeLibraryType { |
| 30 | BUNDLE, |
| 31 | DYNAMIC_LIB |
| 32 | }; |
[email protected] | 5625f3cf | 2013-03-15 12:10:58 | [diff] [blame] | 33 | enum NativeLibraryObjCStatus { |
| 34 | OBJC_UNKNOWN, |
| 35 | OBJC_PRESENT, |
| 36 | OBJC_NOT_PRESENT, |
| 37 | }; |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 38 | struct NativeLibraryStruct { |
| 39 | NativeLibraryType type; |
[email protected] | d8921c43 | 2010-01-28 16:08:09 | [diff] [blame] | 40 | CFBundleRefNum bundle_resource_ref; |
[email protected] | 5625f3cf | 2013-03-15 12:10:58 | [diff] [blame] | 41 | NativeLibraryObjCStatus objc_status; |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 42 | union { |
| 43 | CFBundleRef bundle; |
| 44 | void* dylib; |
| 45 | }; |
| 46 | }; |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 47 | using NativeLibrary = NativeLibraryStruct*; |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 48 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 49 | using NativeLibrary = void*; |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 50 | #endif // OS_* |
| 51 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 52 | struct BASE_EXPORT NativeLibraryLoadError { |
| 53 | #if defined(OS_WIN) |
| 54 | NativeLibraryLoadError() : code(0) {} |
| 55 | #endif // OS_WIN |
| 56 | |
| 57 | // Returns a string representation of the load error. |
| 58 | std::string ToString() const; |
| 59 | |
| 60 | #if defined(OS_WIN) |
| 61 | DWORD code; |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 62 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 63 | std::string message; |
| 64 | #endif // OS_WIN |
| 65 | }; |
| 66 | |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 67 | struct BASE_EXPORT NativeLibraryOptions { |
| 68 | NativeLibraryOptions() = default; |
| 69 | NativeLibraryOptions(const NativeLibraryOptions& options) = default; |
| 70 | |
| 71 | // If |true|, a loaded library is required to prefer local symbol resolution |
| 72 | // before considering global symbols. Note that this is already the default |
| 73 | // behavior on most systems. Setting this to |false| does not guarantee the |
| 74 | // inverse, i.e., it does not force a preference for global symbols over local |
| 75 | // ones. |
| 76 | bool prefer_own_symbols = false; |
| 77 | }; |
| 78 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 79 | // Loads a native library from disk. Release it with UnloadNativeLibrary when |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 80 | // you're done. Returns NULL on failure. |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 81 | // If |error| is not NULL, it may be filled in on load error. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 82 | BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 83 | NativeLibraryLoadError* error); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 84 | |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 85 | #if defined(OS_WIN) |
| 86 | // Loads a native library from the system directory using the appropriate flags. |
| 87 | // The function first checks to see if the library is already loaded and will |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 88 | // get a handle if so. This method results in a lock that may block the calling |
| 89 | // thread. |
| 90 | BASE_EXPORT NativeLibrary |
| 91 | LoadSystemLibrary(FilePath::StringPieceType name, |
| 92 | NativeLibraryLoadError* error = nullptr); |
| 93 | |
| 94 | // Gets the module handle for the specified system library and pins it to |
| 95 | // ensure it never gets unloaded. If the module is not loaded, it will first |
| 96 | // call LoadSystemLibrary to load it. If the module cannot be pinned, this |
| 97 | // method returns null and includes the error. This method results in a lock |
| 98 | // that may block the calling thread. |
| 99 | BASE_EXPORT NativeLibrary |
| 100 | PinSystemLibrary(FilePath::StringPieceType name, |
| 101 | NativeLibraryLoadError* error = nullptr); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 102 | #endif |
| 103 | |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 104 | // Loads a native library from disk. Release it with UnloadNativeLibrary when |
| 105 | // you're done. Returns NULL on failure. |
| 106 | // If |error| is not NULL, it may be filled in on load error. |
| 107 | BASE_EXPORT NativeLibrary LoadNativeLibraryWithOptions( |
| 108 | const FilePath& library_path, |
| 109 | const NativeLibraryOptions& options, |
| 110 | NativeLibraryLoadError* error); |
| 111 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 112 | // Unloads a native library. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 113 | BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 114 | |
| 115 | // Gets a function pointer from a native library. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 116 | BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 117 | StringPiece name); |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 118 | |
Xiaohan Wang | d807ec3 | 2018-04-03 01:31:44 | [diff] [blame] | 119 | // Returns the full platform-specific name for a native library. |name| must be |
| 120 | // ASCII. This is also the default name for the output of a gn |shared_library| |
| 121 | // target. See tools/gn/docs/reference.md#shared_library. |
| 122 | // For example for "mylib", it returns: |
| 123 | // - "mylib.dll" on Windows |
| 124 | // - "libmylib.so" on Linux |
| 125 | // - "libmylib.dylib" on Mac |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 126 | BASE_EXPORT std::string GetNativeLibraryName(StringPiece name); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 127 | |
Xiaohan Wang | d807ec3 | 2018-04-03 01:31:44 | [diff] [blame] | 128 | // Returns the full platform-specific name for a gn |loadable_module| target. |
| 129 | // See tools/gn/docs/reference.md#loadable_module |
| 130 | // The returned name is the same as GetNativeLibraryName() on all platforms |
| 131 | // except for Mac where for "mylib" it returns "mylib.so". |
| 132 | BASE_EXPORT std::string GetLoadableModuleName(StringPiece name); |
| 133 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 134 | } // namespace base |
| 135 | |
| 136 | #endif // BASE_NATIVE_LIBRARY_H_ |