2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-04-24 20:39:58 +02:00
|
|
|
* Copyright (c) 2021, Gunnar Beutner <gunnar@beutner.name>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2022-08-13 20:58:47 +02:00
|
|
|
#include <AK/Types.h>
|
2022-08-14 13:18:35 +02:00
|
|
|
#include <bits/dlfcn_integration.h>
|
2022-08-13 20:58:47 +02:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
#include <string.h>
|
2019-12-31 16:47:33 -05:00
|
|
|
|
2022-08-13 20:58:47 +02:00
|
|
|
// These are filled in by the dynamic loader.
|
2024-01-20 16:20:42 -05:00
|
|
|
[[gnu::weak]] DlCloseFunction __dlclose;
|
|
|
|
|
[[gnu::weak]] DlOpenFunction __dlopen;
|
|
|
|
|
[[gnu::weak]] DlSymFunction __dlsym;
|
|
|
|
|
[[gnu::weak]] DlAddrFunction __dladdr;
|
2022-08-13 20:58:47 +02:00
|
|
|
|
|
|
|
|
// FIXME: use thread_local and a String once TLS works
|
|
|
|
|
#ifdef NO_TLS
|
|
|
|
|
char* s_dlerror_text = NULL;
|
|
|
|
|
bool s_dlerror_retrieved = false;
|
|
|
|
|
#else
|
|
|
|
|
__thread char* s_dlerror_text = NULL;
|
|
|
|
|
__thread bool s_dlerror_retrieved = false;
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
static void store_error(ByteString const& error)
|
2022-08-13 20:58:47 +02:00
|
|
|
{
|
|
|
|
|
free(s_dlerror_text);
|
|
|
|
|
s_dlerror_text = strdup(error.characters());
|
|
|
|
|
s_dlerror_retrieved = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int dlclose(void* handle)
|
|
|
|
|
{
|
|
|
|
|
auto result = __dlclose(handle);
|
|
|
|
|
if (result.is_error()) {
|
|
|
|
|
store_error(result.error().text);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* dlerror()
|
|
|
|
|
{
|
|
|
|
|
if (s_dlerror_retrieved) {
|
|
|
|
|
free(s_dlerror_text);
|
|
|
|
|
s_dlerror_text = nullptr;
|
|
|
|
|
}
|
|
|
|
|
s_dlerror_retrieved = true;
|
|
|
|
|
return const_cast<char*>(s_dlerror_text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* dlopen(char const* filename, int flags)
|
|
|
|
|
{
|
|
|
|
|
auto result = __dlopen(filename, flags);
|
|
|
|
|
if (result.is_error()) {
|
|
|
|
|
store_error(result.error().text);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return result.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* dlsym(void* handle, char const* symbol_name)
|
|
|
|
|
{
|
|
|
|
|
auto result = __dlsym(handle, symbol_name);
|
|
|
|
|
if (result.is_error()) {
|
|
|
|
|
store_error(result.error().text);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return result.value();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 11:49:31 +02:00
|
|
|
int dladdr(void const* addr, Dl_info* info)
|
2022-08-13 20:58:47 +02:00
|
|
|
{
|
|
|
|
|
auto result = __dladdr(addr, info);
|
|
|
|
|
if (result.is_error()) {
|
|
|
|
|
// FIXME: According to the man page glibc does _not_ make the error
|
|
|
|
|
// available via dlerror(), however we do. Does this break anything?
|
|
|
|
|
store_error(result.error().text);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|