-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathmonodroid-dl.hh
182 lines (152 loc) · 5.91 KB
/
monodroid-dl.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#pragma once
#include <dlfcn.h>
#include <android/dlext.h>
#include <mono/utils/mono-dl-fallback.h>
#include <java-interop-dlfcn.h>
#include <xxhash.hh>
#include <xamarin-app.hh>
#include "android-system.hh"
#include "monodroid-state.hh"
#include "search.hh"
#include "shared-constants.hh"
#include "startup-aware-lock.hh"
#include "util.hh"
namespace xamarin::android::internal
{
class MonodroidDl
{
static inline xamarin::android::mutex dso_handle_write_lock;
static unsigned int convert_dl_flags (int flags) noexcept
{
unsigned int lflags = (flags & static_cast<int> (MONO_DL_LOCAL))
? microsoft::java_interop::JAVA_INTEROP_LIB_LOAD_LOCALLY
: microsoft::java_interop::JAVA_INTEROP_LIB_LOAD_GLOBALLY;
return lflags;
}
static DSOCacheEntry* find_dso_cache_entry (hash_t hash) noexcept
{
auto equal = [](DSOCacheEntry const& entry, hash_t key) -> bool { return entry.hash == key; };
auto less_than = [](DSOCacheEntry const& entry, hash_t key) -> bool { return entry.hash < key; };
ssize_t idx = Search::binary_search<DSOCacheEntry, equal, less_than> (hash, dso_cache, application_config.number_of_dso_cache_entries);
if (idx >= 0) {
return &dso_cache[idx];
}
return nullptr;
}
static void* monodroid_dlopen_log_and_return (void *handle, char **err, const char *full_name, bool free_memory)
{
if (handle == nullptr && err != nullptr) {
const char *load_error = dlerror ();
if (load_error == nullptr) {
load_error = "Unknown error";
}
*err = Util::monodroid_strdup_printf ("Could not load library '%s'. %s", full_name, load_error);
}
if (free_memory) {
delete[] full_name;
}
return handle;
}
static void* monodroid_dlopen_ignore_component_or_load ([[maybe_unused]] hash_t name_hash, const char *name, int flags, char **err) noexcept
{
if (MonodroidState::is_startup_in_progress ()) {
auto ignore_component = [&](const char *label, MonoComponent component) -> bool {
if ((application_config.mono_components_mask & component) != component) {
log_info (LOG_ASSEMBLY, "Mono '%s' component requested but not packaged, ignoring", label);
return true;
}
return false;
};
switch (name_hash) {
case SharedConstants::mono_component_debugger_hash:
if (ignore_component ("Debugger", MonoComponent::Debugger)) {
return nullptr;
}
break;
case SharedConstants::mono_component_hot_reload_hash:
if (ignore_component ("Hot Reload", MonoComponent::HotReload)) {
return nullptr;
}
break;
case SharedConstants::mono_component_diagnostics_tracing_hash:
if (ignore_component ("Diagnostics Tracing", MonoComponent::Tracing)) {
return nullptr;
}
break;
}
}
unsigned int dl_flags = convert_dl_flags (flags);
void * handle = AndroidSystem::load_dso_from_any_directories (name, dl_flags);
if (handle != nullptr) {
return monodroid_dlopen_log_and_return (handle, err, name, false /* name_needs_free */);
}
handle = AndroidSystem::load_dso (name, dl_flags, false /* skip_existing_check */);
return monodroid_dlopen_log_and_return (handle, err, name, false /* name_needs_free */);
}
public:
[[gnu::flatten]]
static void* monodroid_dlopen (const char *name, int flags, char **err, [[maybe_unused]] void *user_data) noexcept
{
if (name == nullptr) {
log_warn (LOG_ASSEMBLY, "monodroid_dlopen got a null name. This is not supported in NET+");
return nullptr;
}
hash_t name_hash = xxhash::hash (name, strlen (name));
log_debug (LOG_ASSEMBLY, "monodroid_dlopen: hash for name '%s' is 0x%zx", name, name_hash);
DSOCacheEntry *dso = find_dso_cache_entry (name_hash);
log_debug (LOG_ASSEMBLY, "monodroid_dlopen: hash match %sfound, DSO name is '%s'", dso == nullptr ? "not " : "", dso == nullptr ? "<unknown>" : dso->name);
if (dso == nullptr) {
// DSO not known at build time, try to load it
return monodroid_dlopen_ignore_component_or_load (name_hash, name, flags, err);
} else if (dso->handle != nullptr) {
return monodroid_dlopen_log_and_return (dso->handle, err, dso->name, false /* name_needs_free */);
}
if (dso->ignore) {
log_info (LOG_ASSEMBLY, "Request to load '%s' ignored, it is known not to exist", dso->name);
return nullptr;
}
StartupAwareLock lock (dso_handle_write_lock);
#if defined (RELEASE)
if (AndroidSystem::is_embedded_dso_mode_enabled ()) {
DSOApkEntry *apk_entry = dso_apk_entries;
for (size_t i = 0; i < application_config.number_of_shared_libraries; i++) {
if (apk_entry->name_hash != dso->real_name_hash) {
apk_entry++;
continue;
}
android_dlextinfo dli;
dli.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
dli.library_fd = apk_entry->fd;
dli.library_fd_offset = apk_entry->offset;
dso->handle = android_dlopen_ext (dso->name, flags, &dli);
if (dso->handle != nullptr) {
return monodroid_dlopen_log_and_return (dso->handle, err, dso->name, false /* name_needs_free */);
}
break;
}
}
#endif
unsigned int dl_flags = convert_dl_flags (flags);
dso->handle = AndroidSystem::load_dso_from_any_directories (dso->name, dl_flags);
if (dso->handle != nullptr) {
return monodroid_dlopen_log_and_return (dso->handle, err, dso->name, false /* name_needs_free */);
}
dso->handle = AndroidSystem::load_dso_from_any_directories (name, dl_flags);
return monodroid_dlopen_log_and_return (dso->handle, err, name, false /* name_needs_free */);
}
[[gnu::flatten]]
static void* monodroid_dlsym (void *handle, const char *name, char **err, [[maybe_unused]] void *user_data)
{
void *s;
char *e = nullptr;
s = microsoft::java_interop::java_interop_lib_symbol (handle, name, &e);
if (!s && err) {
*err = Util::monodroid_strdup_printf ("Could not find symbol '%s': %s", name, e);
}
if (e) {
java_interop_free (e);
}
return s;
}
};
}