1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 15:17:36 +00:00

LibC+LibELF: Implement more fully-features dlfcn functionality

This implements more of the dlfcn functionality. Most notably:

* It's now possible to dlopen() libraries which were already
  loaded at program startup time. This does not cause those
  libraries to be loaded twice.
* Errors are reported via dlerror() rather than by crashing
  the program.
* Calls to the dl*() functions are thread-safe.
This commit is contained in:
Gunnar Beutner 2021-04-24 20:39:58 +02:00 committed by Andreas Kling
parent 549d9bd3ea
commit f40ee1b03f
12 changed files with 371 additions and 200 deletions

View file

@ -0,0 +1,6 @@
set(SOURCES
dlfcn.cpp
)
serenity_libc(LibDl dl)
target_link_libraries(LibDl LibC)

View file

@ -0,0 +1,62 @@
/*
* Copyright (c) 2021, Gunnar Beutner <gunnar@beutner.name>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <AK/Types.h>
#include <LibDl/dlfcn.h>
#include <LibDl/dlfcn_integration.h>
#include <string.h>
// FIXME: use thread_local and a String once TLS works
__thread char* s_dlerror_text = NULL;
__thread bool s_dlerror_retrieved = false;
static void store_error(const String& error)
{
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(const char* file_name, int flags)
{
auto result = __dlopen(file_name, flags);
if (result.is_error()) {
store_error(result.error().text);
return nullptr;
}
return result.value();
}
void* dlsym(void* handle, const char* symbol_name)
{
auto result = __dlsym(handle, symbol_name);
if (result.is_error()) {
store_error(result.error().text);
return nullptr;
}
return result.value();
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <sys/cdefs.h>
__BEGIN_DECLS
#define RTLD_DEFAULT 0
#define RTLD_LAZY 2
#define RTLD_NOW 4
#define RTLD_GLOBAL 8
#define RTLD_LOCAL 16
int dlclose(void*);
char* dlerror();
void* dlopen(const char*, int);
void* dlsym(void*, const char*);
__END_DECLS

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2021, Gunnar Beutner <gunnar@beutner.name>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Result.h>
#include <AK/String.h>
struct DlErrorMessage {
DlErrorMessage(String&& other)
: text(move(other))
{
}
// The virtual destructor is required because we're passing this
// struct to the dynamic loader - whose operator delete differs
// from the one in libc.so
virtual ~DlErrorMessage() { }
String text;
};
typedef Result<void, DlErrorMessage> (*DlCloseFunction)(void*);
typedef Result<void*, DlErrorMessage> (*DlOpenFunction)(const char*, int);
typedef Result<void*, DlErrorMessage> (*DlSymFunction)(void*, const char*);
extern "C" {
extern DlCloseFunction __dlclose;
extern DlOpenFunction __dlopen;
extern DlSymFunction __dlsym;
}