|
| 1 | +/* |
| 2 | + * Dynamic Link Library by Parra Studios |
| 3 | + * A library for dynamic loading and linking shared objects at run-time. |
| 4 | + * |
| 5 | + * Copyright (C) 2016 - 2026 Vicente Eduardo Ferrer Garcia <vic798@gmail.com> |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#if defined(__HAIKU__) |
| 22 | + |
| 23 | +/* -- Headers -- */ |
| 24 | + |
| 25 | +#include <dynlink/dynlink.h> |
| 26 | + |
| 27 | +#include <dynlink/dynlink_impl.h> |
| 28 | + |
| 29 | +#include <log/log.h> |
| 30 | + |
| 31 | +#include <string.h> |
| 32 | + |
| 33 | +#include <stdint.h> |
| 34 | + |
| 35 | +#include <dlfcn.h> |
| 36 | + |
| 37 | +#include <be/kernel/image.h> |
| 38 | + |
| 39 | +/* -- Methods -- */ |
| 40 | + |
| 41 | +const char *dynlink_impl_interface_prefix_haiku(void) |
| 42 | +{ |
| 43 | + static const char prefix_haiku[] = "lib"; |
| 44 | + |
| 45 | + return prefix_haiku; |
| 46 | +} |
| 47 | + |
| 48 | +const char *dynlink_impl_interface_extension_haiku(void) |
| 49 | +{ |
| 50 | + static const char extension_haiku[] = "so"; |
| 51 | + |
| 52 | + return extension_haiku; |
| 53 | +} |
| 54 | + |
| 55 | +dynlink_impl dynlink_impl_interface_load_haiku(dynlink handle) |
| 56 | +{ |
| 57 | + dynlink_flags flags = dynlink_get_flags(handle); |
| 58 | + image_id impl = 0; |
| 59 | + |
| 60 | + if (DYNLINK_FLAGS_CHECK(flags, DYNLINK_FLAGS_BIND_SELF)) |
| 61 | + { |
| 62 | + int mode; |
| 63 | + void *self_impl; |
| 64 | + |
| 65 | + DYNLINK_FLAGS_SET(mode, 0); |
| 66 | + |
| 67 | + if (DYNLINK_FLAGS_CHECK(flags, DYNLINK_FLAGS_BIND_NOW)) |
| 68 | + { |
| 69 | + DYNLINK_FLAGS_ADD(mode, RTLD_NOW); |
| 70 | + } |
| 71 | + |
| 72 | + if (DYNLINK_FLAGS_CHECK(flags, DYNLINK_FLAGS_BIND_LAZY)) |
| 73 | + { |
| 74 | + DYNLINK_FLAGS_ADD(mode, RTLD_LAZY); |
| 75 | + } |
| 76 | + |
| 77 | + if (DYNLINK_FLAGS_CHECK(flags, DYNLINK_FLAGS_BIND_LOCAL)) |
| 78 | + { |
| 79 | + DYNLINK_FLAGS_ADD(mode, RTLD_LOCAL); |
| 80 | + } |
| 81 | + |
| 82 | + if (DYNLINK_FLAGS_CHECK(flags, DYNLINK_FLAGS_BIND_GLOBAL)) |
| 83 | + { |
| 84 | + DYNLINK_FLAGS_ADD(mode, RTLD_GLOBAL); |
| 85 | + } |
| 86 | + |
| 87 | + self_impl = dlopen(NULL, mode); |
| 88 | + |
| 89 | + if (self_impl == NULL) |
| 90 | + { |
| 91 | + log_write("metacall", LOG_LEVEL_ERROR, "DynLink error: %s", dlerror()); |
| 92 | + return NULL; |
| 93 | + } |
| 94 | + |
| 95 | + return self_impl; |
| 96 | + } |
| 97 | + |
| 98 | + impl = load_add_on(dynlink_get_path(handle)); |
| 99 | + |
| 100 | + if (impl < B_NO_ERROR) |
| 101 | + { |
| 102 | + log_write("metacall", LOG_LEVEL_ERROR, "DynLink error: failed to load BeOS/Haiku image add-on with error code %d", (int)impl); |
| 103 | + return NULL; |
| 104 | + } |
| 105 | + |
| 106 | + return (dynlink_impl)(intptr_t)impl; |
| 107 | +} |
| 108 | + |
| 109 | +int dynlink_impl_interface_symbol_haiku(dynlink handle, dynlink_impl impl, const char *name, dynlink_symbol_addr *addr) |
| 110 | +{ |
| 111 | + void *symbol = NULL; |
| 112 | + |
| 113 | + if (DYNLINK_FLAGS_CHECK(dynlink_get_flags(handle), DYNLINK_FLAGS_BIND_SELF)) |
| 114 | + { |
| 115 | + symbol = dlsym(impl, name); |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + int err = get_image_symbol((image_id)(intptr_t)impl, name, B_SYMBOL_TYPE_ANY, &symbol); |
| 120 | + |
| 121 | + if (err != B_OK) |
| 122 | + { |
| 123 | + log_write("metacall", LOG_LEVEL_ERROR, "DynLink error: failed to load BeOS/Haiku symbol %s", name); |
| 124 | + return 1; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + dynlink_symbol_cast(void *, symbol, *addr); |
| 129 | + |
| 130 | + return (*addr == NULL); |
| 131 | +} |
| 132 | + |
| 133 | +int dynlink_impl_interface_unload_haiku(dynlink handle, dynlink_impl impl) |
| 134 | +{ |
| 135 | + dynlink_flags flags = dynlink_get_flags(handle); |
| 136 | + |
| 137 | + (void)handle; |
| 138 | + |
| 139 | + /* Skip unlink when using global handle for loading symbols of the current process */ |
| 140 | + if (DYNLINK_FLAGS_CHECK(flags, DYNLINK_FLAGS_BIND_SELF)) |
| 141 | + { |
| 142 | + return 0; |
| 143 | + } |
| 144 | + |
| 145 | +#if defined(__MEMORYCHECK__) || defined(__ADDRESS_SANITIZER__) || defined(__THREAD_SANITIZER__) || defined(__MEMORY_SANITIZER__) |
| 146 | + /* Disable dlclose when running with address sanitizer in order to maintain stacktraces */ |
| 147 | + (void)impl; |
| 148 | + return 0; |
| 149 | +#else |
| 150 | + return ((image_id)(intptr_t)impl > 0) && (unload_add_on((image_id)(intptr_t)impl) < B_NO_ERROR); |
| 151 | + // (void)impl; |
| 152 | + // return 0; |
| 153 | +#endif |
| 154 | +} |
| 155 | + |
| 156 | +dynlink_impl_interface dynlink_impl_interface_singleton(void) |
| 157 | +{ |
| 158 | + static struct dynlink_impl_interface_type impl_interface_haiku = { |
| 159 | + &dynlink_impl_interface_prefix_haiku, |
| 160 | + &dynlink_impl_interface_extension_haiku, |
| 161 | + &dynlink_impl_interface_load_haiku, |
| 162 | + &dynlink_impl_interface_symbol_haiku, |
| 163 | + &dynlink_impl_interface_unload_haiku, |
| 164 | + }; |
| 165 | + |
| 166 | + return &impl_interface_haiku; |
| 167 | +} |
| 168 | + |
| 169 | +#elif defined(__BEOS__) |
| 170 | +#include "dynlink_impl_beos.c" |
| 171 | +#endif |
0 commit comments