1 // Copyright 2011 The Chromium Authors
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 PARTITION_ALLOC_PARTITION_ALLOC_BASE_NATIVE_LIBRARY_H_
6 #define PARTITION_ALLOC_PARTITION_ALLOC_BASE_NATIVE_LIBRARY_H_
7
8 // This file defines a cross-platform "NativeLibrary" type which represents
9 // a loadable module.
10
11 #include <string>
12
13 #include "build/build_config.h"
14 #include "partition_alloc/partition_alloc_base/component_export.h"
15 #include "partition_alloc/partition_alloc_base/files/file_path.h"
16
17 #if BUILDFLAG(IS_WIN)
18 #include <windows.h>
19 #elif BUILDFLAG(IS_APPLE)
20 #include <CoreFoundation/CoreFoundation.h>
21 #endif // OS_*
22
23 namespace partition_alloc::internal::base {
24
25 #if BUILDFLAG(IS_WIN)
26 using NativeLibrary = HMODULE;
27 #elif BUILDFLAG(IS_APPLE)
28 enum NativeLibraryType { BUNDLE, DYNAMIC_LIB };
29 enum NativeLibraryObjCStatus {
30 OBJC_UNKNOWN,
31 OBJC_PRESENT,
32 OBJC_NOT_PRESENT,
33 };
34 struct NativeLibraryStruct {
35 NativeLibraryType type;
36 CFBundleRefNum bundle_resource_ref;
37 NativeLibraryObjCStatus objc_status;
38 union {
39 CFBundleRef bundle;
40 void* dylib;
41 };
42 };
43 using NativeLibrary = NativeLibraryStruct*;
44 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
45 using NativeLibrary = void*;
46 #endif // OS_*
47
PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE)48 struct PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE) NativeLibraryLoadError {
49 #if BUILDFLAG(IS_WIN)
50 NativeLibraryLoadError() : code(0) {}
51 #endif // BUILDFLAG(IS_WIN)
52
53 // Returns a string representation of the load error.
54 std::string ToString() const;
55
56 #if BUILDFLAG(IS_WIN)
57 DWORD code;
58 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
59 std::string message;
60 #endif // BUILDFLAG(IS_WIN)
61 };
62
PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE)63 struct PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE) NativeLibraryOptions {
64 NativeLibraryOptions() = default;
65 NativeLibraryOptions(const NativeLibraryOptions& options) = default;
66
67 // If |true|, a loaded library is required to prefer local symbol resolution
68 // before considering global symbols. Note that this is already the default
69 // behavior on most systems. Setting this to |false| does not guarantee the
70 // inverse, i.e., it does not force a preference for global symbols over local
71 // ones.
72 bool prefer_own_symbols = false;
73 };
74
75 // Loads a native library from disk. Release it with UnloadNativeLibrary when
76 // you're done. Returns NULL on failure.
77 // If |error| is not NULL, it may be filled in on load error.
78 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE)
79 NativeLibrary LoadNativeLibrary(const FilePath& library_path,
80 NativeLibraryLoadError* error);
81
82 // Loads a native library from disk. Release it with UnloadNativeLibrary when
83 // you're done. Returns NULL on failure.
84 // If |error| is not NULL, it may be filled in on load error.
85 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE)
86 NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
87 const NativeLibraryOptions& options,
88 NativeLibraryLoadError* error);
89
90 // Gets a function pointer from a native library.
91 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE)
92 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
93 const std::string& name);
94
95 } // namespace partition_alloc::internal::base
96
97 #endif // PARTITION_ALLOC_PARTITION_ALLOC_BASE_NATIVE_LIBRARY_H_
98