Files
ladybird/Userland/Libraries/LibC/libcinit.cpp
Dan Klishch 932a722623 LibC+LibELF: Do not override existing weak symbols during magic lookup
Previously, the actual behavior of magic lookup and one described in its
commit description have not matched. Instead of being weak definitions
in a library that is always in the end of load order, the definitions
were normal ones and thus were able to override other weak definitions
in LibC. While this was consistent with how DynamicLoader resolves
ambiguity between normal and weak relocations, this is not the behavior
POSIX mandates -- we should always choose first available definition wrt
load order. To fix this problem, the patch makes sure we don't define
any of magic symbols in LibC.

In addition to this, it makes all provided magic symbols functions
(instead of objects), what renders MagicWeakSymbol class unnecessary.
2024-04-21 13:34:04 -06:00

58 lines
1.1 KiB
C++

/*
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Types.h>
#include <assert.h>
#include <errno.h>
#include <sys/internals.h>
#include <unistd.h>
[[gnu::weak]] char** __environ_value() asm("__environ_value");
extern "C" {
#ifdef NO_TLS
int errno_storage;
#else
__thread int errno_storage;
#endif
bool __environ_is_malloced = false;
bool __stdio_is_initialized = false;
void* __auxiliary_vector = reinterpret_cast<void*>(explode_byte(0xe1));
#ifndef _DYNAMIC_LOADER
char** environ = reinterpret_cast<char**>(explode_byte(0xe2));
uintptr_t __stack_chk_guard;
#endif
static void __auxiliary_vector_init();
int* __errno_location()
{
return &errno_storage;
}
void __libc_init([[maybe_unused]] uintptr_t cookie)
{
#ifndef _DYNAMIC_LOADER
__stack_chk_guard = cookie;
environ = __environ_value();
#endif
__auxiliary_vector_init();
__malloc_init();
__stdio_init();
}
static void __auxiliary_vector_init()
{
char** env;
for (env = environ; *env; ++env) {
}
__auxiliary_vector = (void*)++env;
}
}