mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:58:13 +00:00
Demos: Add a dynamic linking demo to show off dlfcn methods
The LinkDemo program calls dlopen/dlsym/dlclose to try and load a dyanmic library from /usr/lib. It read a global variable and calls a global function (extern "C" of course :) ). There a few hacks left in the LinkLib dynamic library, however. In order to get the linker to stop complaining, we have to use -nostartfiles -ffreestanding otherwise it will link crt0.o to our shared object, which is definitely not right as the _init function for a main program (that calls main) is not suitable for our lib
This commit is contained in:
parent
21161342ef
commit
b6590b7f83
7 changed files with 179 additions and 1 deletions
72
Demos/DynamicLink/LinkLib/DynamicLib.cpp
Normal file
72
Demos/DynamicLink/LinkLib/DynamicLib.cpp
Normal file
|
@ -0,0 +1,72 @@
|
|||
#include <AK/kstdio.h>
|
||||
#include <AK/String.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// FIXME: See Makefile. We need -ffreestanding and -nostartfiles to
|
||||
// Get GCC to stop linking crt0.o w/our .so.
|
||||
// So, we need __dso_handle. ... Yikes
|
||||
extern void* __dso_handle __attribute__((__section__(".sdata")));
|
||||
extern void* __dso_handle __attribute__((__visibility__("hidden")));
|
||||
void* __dso_handle = (void*)1234; // FIXME: Is the dynamic linker supposed to set this value?
|
||||
|
||||
// FIXME: Things defined in crt0 >:(
|
||||
__thread int errno;
|
||||
char* __static_environ[] = { nullptr }; // We don't get the environment without some libc workarounds..
|
||||
char** environ = __static_environ;
|
||||
bool __environ_is_malloced = false;
|
||||
extern unsigned __stack_chk_guard;
|
||||
unsigned __stack_chk_guard = (unsigned)0xc0000c13;
|
||||
|
||||
[[noreturn]] void __stack_chk_fail()
|
||||
{
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
// FIXME: Because we need to call printf, and we don't have access to the stout file descriptor
|
||||
// from the main executable, we need to create our own copy in __stdio_init :/
|
||||
// Same deal for malloc init. We're essentially manually calling __libc_init here.
|
||||
extern "C" void __stdio_init();
|
||||
extern "C" void __malloc_init();
|
||||
|
||||
class Global {
|
||||
public:
|
||||
Global(int i)
|
||||
: m_i(i)
|
||||
{
|
||||
__malloc_init();
|
||||
__stdio_init();
|
||||
}
|
||||
|
||||
int get_i() const { return m_i; }
|
||||
|
||||
private:
|
||||
int m_i = 0;
|
||||
};
|
||||
|
||||
// This object exists to call __stdio_init and __malloc_init. Also to show that global vars work
|
||||
Global g_glob { 5 };
|
||||
|
||||
extern "C" {
|
||||
int global_lib_variable = 1234;
|
||||
|
||||
void global_lib_function()
|
||||
{
|
||||
printf("Hello from Dynamic Lib! g_glob::m_i == %d\n", g_glob.get_i());
|
||||
}
|
||||
|
||||
const char* other_lib_function(int my_argument)
|
||||
{
|
||||
dbgprintf("Hello from Dynamic Lib, now from the debug port! g_glob::m_i == %d\n", g_glob.get_i());
|
||||
|
||||
int sum = my_argument + global_lib_variable;
|
||||
|
||||
// FIXME: We can't just return AK::String::format across the lib boundary here.
|
||||
// It will use malloc from our DSO's copy of LibC, and then probably be free'd into
|
||||
// the malloc of the main program which would be what they call 'very crash'.
|
||||
// Feels very Windows :)
|
||||
static String s_string;
|
||||
s_string = String::format("Here's your string! Sum of argument and global_lib_variable: %d", sum);
|
||||
return s_string.characters();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue