|
3 | 3 |
|
4 | 4 | #import <Foundation/Foundation.h> |
5 | 5 | #include "coreclrhost.h" |
| 6 | +#include "host_runtime_contract.h" |
6 | 7 | #include <TargetConditionals.h> |
7 | 8 | #import <os/log.h> |
8 | 9 | #include <sys/stat.h> |
|
68 | 69 | return strdup([joined UTF8String]); |
69 | 70 | } |
70 | 71 |
|
71 | | -void* |
| 72 | +const void* |
72 | 73 | pinvoke_override (const char *libraryName, const char *entrypointName) |
73 | 74 | { |
74 | 75 | if (!strcmp (libraryName, "__Internal")) |
|
79 | 80 | return NULL; |
80 | 81 | } |
81 | 82 |
|
| 83 | +#include <mach-o/dyld.h> |
| 84 | +#include <mach-o/loader.h> |
| 85 | +size_t get_image_size(const struct mach_header_64* header) |
| 86 | +{ |
| 87 | + const struct load_command* cmd = (const struct load_command*)((const char*)header + sizeof(struct mach_header_64)); |
| 88 | + |
| 89 | + size_t image_size = 0; |
| 90 | + for (uint32_t j = 0; j < header->ncmds; ++j) |
| 91 | + { |
| 92 | + if (cmd->cmd == LC_SEGMENT_64) |
| 93 | + { |
| 94 | + const struct segment_command_64* seg = (const struct segment_command_64*)cmd; |
| 95 | + size_t end_addr = (size_t)(seg->vmaddr + seg->vmsize); |
| 96 | + if (end_addr > image_size) |
| 97 | + image_size = end_addr; |
| 98 | + } |
| 99 | + |
| 100 | + cmd = (const struct load_command*)((const char*)cmd + cmd->cmdsize); |
| 101 | + } |
| 102 | + |
| 103 | + return image_size; |
| 104 | +} |
| 105 | + |
| 106 | +bool get_native_code_data(const struct host_runtime_contract_native_code_context* context, struct host_runtime_contract_native_code_data* data) |
| 107 | +{ |
| 108 | + if (!context || !data || !context->assembly_path || !context->owner_composite_name) |
| 109 | + return false; |
| 110 | + |
| 111 | + // Look for the owner composite R2R image in the same directory as the assembly |
| 112 | + char r2r_path[PATH_MAX]; |
| 113 | + const char *last_slash = strrchr(context->assembly_path, '/'); |
| 114 | + size_t dir_len = last_slash ? (size_t)(last_slash - context->assembly_path) : 0; |
| 115 | + if (dir_len >= sizeof(r2r_path) - 1) |
| 116 | + return false; |
| 117 | + |
| 118 | + strncpy(r2r_path, context->assembly_path, dir_len); |
| 119 | + int written = snprintf(r2r_path + dir_len, sizeof(r2r_path) - dir_len, "/%s", context->owner_composite_name); |
| 120 | + if (written <= 0 || (size_t)written >= sizeof(r2r_path) - dir_len) |
| 121 | + return false; |
| 122 | + |
| 123 | + void* handle = dlopen(r2r_path, RTLD_LAZY | RTLD_LOCAL); |
| 124 | + if (handle == NULL) |
| 125 | + return false; |
| 126 | + |
| 127 | + void* r2r_header = dlsym(handle, "RTR_HEADER"); |
| 128 | + if (r2r_header == NULL) |
| 129 | + { |
| 130 | + dlclose(handle); |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + Dl_info info; |
| 135 | + if (dladdr(r2r_header, &info) == 0) |
| 136 | + { |
| 137 | + dlclose(handle); |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + // The base address points to the Mach header |
| 142 | + void* base_address = info.dli_fbase; |
| 143 | + const struct mach_header_64* header = (const struct mach_header_64*)base_address; |
| 144 | + |
| 145 | + data->size = sizeof(struct host_runtime_contract_native_code_data); |
| 146 | + data->r2r_header_ptr = r2r_header; |
| 147 | + data->image_size = get_image_size(header); |
| 148 | + data->image_base = base_address; |
| 149 | + return true; |
| 150 | +} |
| 151 | + |
82 | 152 | void |
83 | 153 | mono_ios_runtime_init (void) |
84 | 154 | { |
|
116 | 186 | #endif |
117 | 187 | assert (res > 0); |
118 | 188 |
|
119 | | - char pinvoke_override_addr [16]; |
120 | | - sprintf (pinvoke_override_addr, "%p", &pinvoke_override); |
| 189 | + // Contract lasts the lifetime of the app. The app exists before the end of this function. |
| 190 | + struct host_runtime_contract host_contract = { |
| 191 | + .size = sizeof(struct host_runtime_contract), |
| 192 | + .pinvoke_override = &pinvoke_override, |
| 193 | + .get_native_code_data = &get_native_code_data |
| 194 | + }; |
| 195 | + |
| 196 | + char contract_str[19]; // 0x + 16 hex digits + '\0' |
| 197 | + snprintf(contract_str, 19, "0x%zx", (size_t)(&host_contract)); |
121 | 198 |
|
122 | 199 | // TODO: set TRUSTED_PLATFORM_ASSEMBLIES, APP_PATHS and NATIVE_DLL_SEARCH_DIRECTORIES |
123 | 200 | const char *appctx_keys [] = { |
124 | 201 | "RUNTIME_IDENTIFIER", |
125 | 202 | "APP_CONTEXT_BASE_DIRECTORY", |
126 | 203 | "TRUSTED_PLATFORM_ASSEMBLIES", |
127 | | - "PINVOKE_OVERRIDE", |
| 204 | + "HOST_RUNTIME_CONTRACT", |
128 | 205 | #if !defined(INVARIANT_GLOBALIZATION) |
129 | 206 | "ICU_DAT_FILE_PATH" |
130 | 207 | #endif |
|
133 | 210 | APPLE_RUNTIME_IDENTIFIER, |
134 | 211 | bundle, |
135 | 212 | compute_trusted_platform_assemblies(), |
136 | | - pinvoke_override_addr, |
| 213 | + contract_str, |
137 | 214 | #if !defined(INVARIANT_GLOBALIZATION) |
138 | 215 | icu_dat_path |
139 | 216 | #endif |
|
0 commit comments