LibC: Add sys/auxv.h with getauxval

This commit is contained in:
2026-07-01 12:58:09 +03:00
parent 74e94eedae
commit c0d38862f2
6 changed files with 70 additions and 24 deletions

View File

@@ -40,6 +40,7 @@ set(LIBC_SOURCES
stdlib.cpp stdlib.cpp
string.cpp string.cpp
strings.cpp strings.cpp
sys/auxv.cpp
sys/banan-os.cpp sys/banan-os.cpp
sys/epoll.cpp sys/epoll.cpp
sys/eventfd.cpp sys/eventfd.cpp

View File

@@ -149,6 +149,18 @@ __BEGIN_DECLS
#define DT_LOPROC 0x70000000 #define DT_LOPROC 0x70000000
#define DT_HIPROC 0x7FFFFFFF #define DT_HIPROC 0x7FFFFFFF
#define AT_NULL 0
#define AT_IGNORE 1
#define AT_EXECFD 2
#define AT_PHDR 3
#define AT_PHENT 4
#define AT_PHNUM 5
#define AT_PAGESZ 6
#define AT_BASE 7
#define AT_SHARED_PAGE 0xFFFF0001
#define AT_STACK_BASE 0xFFFF0002
#define AT_STACK_SIZE 0xFFFF0003
#define R_386_NONE 0 #define R_386_NONE 0
#define R_386_32 1 #define R_386_32 1
#define R_386_PC32 2 #define R_386_PC32 2

View File

@@ -0,0 +1,14 @@
#ifndef _SYS_AUXV_H
#define _SYS_AUXV_H 1
#include <sys/cdefs.h>
__BEGIN_DECLS
#include <elf.h>
unsigned long getauxval(unsigned long type);
__END_DECLS
#endif

View File

@@ -0,0 +1,40 @@
#include <sys/auxv.h>
#include <unistd.h>
#include <kernel/API/SharedPage.h>
struct auxv
{
unsigned long a_type;
unsigned long a_val;
};
static const auxv* s_auxv = nullptr;
volatile Kernel::API::SharedPage* g_shared_page = nullptr;
__attribute__((constructor(101)))
static void _init_auxv()
{
if (environ == nullptr)
return;
const char* const* null_env = environ;
while (*null_env)
null_env++;
s_auxv = reinterpret_cast<const auxv*>(null_env + 1);
if (auto value = getauxval(AT_SHARED_PAGE))
g_shared_page = reinterpret_cast<volatile Kernel::API::SharedPage*>(value);
}
unsigned long getauxval(unsigned long type)
{
if (s_auxv == nullptr)
return 0;
for (const auxv* aux = s_auxv; aux->a_type != AT_NULL; aux++)
if (aux->a_type == type)
return aux->a_val;
return 0;
}

View File

@@ -2,12 +2,6 @@
#include <BAN/Debug.h> #include <BAN/Debug.h>
#include <BAN/StringView.h> #include <BAN/StringView.h>
#include <LibELF/AuxiliaryVector.h>
#include <kernel/API/SharedPage.h>
#include <kernel/API/Syscall.h>
#include <kernel/Memory/Types.h>
#include <ctype.h> #include <ctype.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <errno.h> #include <errno.h>
@@ -19,6 +13,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/auxv.h>
#include <sys/banan-os.h> #include <sys/banan-os.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -36,7 +31,6 @@ struct init_funcs_t
extern "C" char** environ; extern "C" char** environ;
volatile Kernel::API::SharedPage* g_shared_page = nullptr;
#define DUMP_BACKTRACE 1 #define DUMP_BACKTRACE 1
#define DEMANGLE_BACKTRACE 0 #define DEMANGLE_BACKTRACE 0
@@ -58,28 +52,11 @@ void __stack_chk_fail(void) { abort(); }
static void __dump_backtrace(int, siginfo_t*, void*); static void __dump_backtrace(int, siginfo_t*, void*);
static LibELF::AuxiliaryVector* find_auxv(char** envp)
{
if (envp == nullptr)
return nullptr;
char** null_env = envp;
while (*null_env)
null_env++;
return reinterpret_cast<LibELF::AuxiliaryVector*>(null_env + 1);
}
extern "C" void _init_libc(char** environ, init_funcs_t init_funcs, init_funcs_t fini_funcs) extern "C" void _init_libc(char** environ, init_funcs_t init_funcs, init_funcs_t fini_funcs)
{ {
if (::environ == nullptr) if (::environ == nullptr)
::environ = environ; ::environ = environ;
if (auto* auxv = find_auxv(environ))
for (auto* aux = auxv; aux->a_type != LibELF::AT_NULL; aux++)
if (aux->a_type == LibELF::AT_SHARED_PAGE)
g_shared_page = static_cast<Kernel::API::SharedPage*>(aux->a_un.a_ptr);
#if defined(__x86_64__) #if defined(__x86_64__)
if (uthread* self = reinterpret_cast<uthread*>(syscall(SYS_GET_FSBASE))) if (uthread* self = reinterpret_cast<uthread*>(syscall(SYS_GET_FSBASE)))
#elif defined(__i686__) #elif defined(__i686__)

View File

@@ -27,6 +27,8 @@ namespace LibELF
AT_BASE = 7, AT_BASE = 7,
AT_SHARED_PAGE = 0xFFFF0001, AT_SHARED_PAGE = 0xFFFF0001,
AT_STACK_BASE = 0xFFFF0002,
AT_STACK_SIZE = 0xFFFF0003,
}; };
} }