From f12da0af13057fe6440d973c13b92c7b9627eccd Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 28 Oct 2021 00:45:42 +0300 Subject: [PATCH] LibC+LibELF: Move getauxval and AT_* flags to sys/auxv.h --- Userland/Libraries/LibC/CMakeLists.txt | 1 + Userland/Libraries/LibC/stdlib.cpp | 13 ------ Userland/Libraries/LibC/stdlib.h | 2 - Userland/Libraries/LibC/sys/auxv.cpp | 25 ++++++++++ Userland/Libraries/LibC/sys/auxv.h | 52 +++++++++++++++++++++ Userland/Libraries/LibELF/AuxiliaryVector.h | 38 +-------------- 6 files changed, 79 insertions(+), 52 deletions(-) create mode 100644 Userland/Libraries/LibC/sys/auxv.cpp create mode 100644 Userland/Libraries/LibC/sys/auxv.h diff --git a/Userland/Libraries/LibC/CMakeLists.txt b/Userland/Libraries/LibC/CMakeLists.txt index 7115afe75b3..18469b41135 100644 --- a/Userland/Libraries/LibC/CMakeLists.txt +++ b/Userland/Libraries/LibC/CMakeLists.txt @@ -45,6 +45,7 @@ set(LIBC_SOURCES strings.cpp stubs.cpp syslog.cpp + sys/auxv.cpp sys/file.cpp sys/mman.cpp sys/prctl.cpp diff --git a/Userland/Libraries/LibC/stdlib.cpp b/Userland/Libraries/LibC/stdlib.cpp index f3d27502537..a2f7fd76514 100644 --- a/Userland/Libraries/LibC/stdlib.cpp +++ b/Userland/Libraries/LibC/stdlib.cpp @@ -180,19 +180,6 @@ inline int generate_unique_filename(char* pattern, Callback callback) extern "C" { -long getauxval(long type) -{ - errno = 0; - - auxv_t* auxvp = (auxv_t*)__auxiliary_vector; - for (; auxvp->a_type != AT_NULL; ++auxvp) { - if (auxvp->a_type == type) - return auxvp->a_un.a_val; - } - errno = ENOENT; - return 0; -} - void exit(int status) { __cxa_finalize(nullptr); diff --git a/Userland/Libraries/LibC/stdlib.h b/Userland/Libraries/LibC/stdlib.h index cdd5fc4f35e..8d624a6aa8c 100644 --- a/Userland/Libraries/LibC/stdlib.h +++ b/Userland/Libraries/LibC/stdlib.h @@ -98,6 +98,4 @@ int posix_openpt(int flags); int grantpt(int fd); int unlockpt(int fd); -long getauxval(long type); - __END_DECLS diff --git a/Userland/Libraries/LibC/sys/auxv.cpp b/Userland/Libraries/LibC/sys/auxv.cpp new file mode 100644 index 00000000000..38323df9f6b --- /dev/null +++ b/Userland/Libraries/LibC/sys/auxv.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021, Idan Horowitz + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +extern "C" { + +long getauxval(long type) +{ + errno = 0; + + auxv_t* auxvp = (auxv_t*)__auxiliary_vector; + for (; auxvp->a_type != AT_NULL; ++auxvp) { + if (auxvp->a_type == type) + return auxvp->a_un.a_val; + } + errno = ENOENT; + return 0; +} +} diff --git a/Userland/Libraries/LibC/sys/auxv.h b/Userland/Libraries/LibC/sys/auxv.h new file mode 100644 index 00000000000..e5d6a877029 --- /dev/null +++ b/Userland/Libraries/LibC/sys/auxv.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021, Idan Horowitz + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +__BEGIN_DECLS + +#define AT_NULL 0 /* No length, last entry's a_type has this value */ +#define AT_IGNORE 1 /* Entry has no meaning, a_un undefined */ +#define AT_EXECFD 2 /* a_val contains a file descriptor of the main program image */ +#define AT_PHDR 3 /* a_ptr contains pointer to program header table of main program image */ +#define AT_PHENT 4 /* a_val holds size of program header table entries */ +#define AT_PHNUM 5 /* a_val holds number of program header table entries */ +#define AT_PAGESZ 6 /* a_val gives system page size in bytes */ +#define AT_BASE 7 /* a_ptr holds base address that Loader was loaded into memory */ +#define AT_FLAGS 8 /* a_val holds 1 bit flags. Undefined flags are 0 */ +#define AT_ENTRY 9 /* a_ptr holds entry point of the main program */ +#define AT_NOTELF 10 /* a_val non-zero if the program is not ELF */ +#define AT_UID 11 /* a_val holds real user id of process */ +#define AT_EUID 12 /* a_val holds effective user id of process */ +#define AT_GID 13 /* a_val holds real group id of process */ +#define AT_EGID 14 /* a_val holds effective group id of process */ +#define AT_PLATFORM 15 /* a_val points to a string containing platform name */ +#define AT_HWCAP 16 /* a_val contains bitmask of CPU features. Equivalent to CPUID 1.EDX*/ +#define AT_CLKTCK 17 /* a_val contains frequence at which times() increments. (Re: Spec. What is times()?) */ +#define AT_SECURE 23 /* a_val holds 1 if program in secure mode (e.g. suid). Otherwise 0 */ +#define AT_BASE_PLATFORM 24 /* a_ptr points to a string identifying base platform name, which might be different from platform (e.g x86_64 when in i386 compat) */ +#define AT_RANDOM 25 /* a_ptr points to 16 securely generated random bytes */ +#define AT_HWCAP2 26 /* a_val holds extended hw feature mask. Currently 0 */ +#define AT_EXECFN 31 /* a_ptr points to filename of executed program */ +#define AT_EXE_BASE 32 /* a_ptr holds base address where main program was loaded into memory */ +#define AT_EXE_SIZE 33 /* a_val holds the size of the main program in memory */ + +/* Auxiliary Vector types, from Intel386 ABI ver 1.0 section 2.3.3 */ +typedef struct +{ + long a_type; /* Note: Extended to long from int, for ease of compatibility w/64 bit */ + union { + long a_val; + void* a_ptr; + void (*a_fnc)(); /* In spec, not used */ + } a_un; +} auxv_t; + +long getauxval(long type); + +__END_DECLS diff --git a/Userland/Libraries/LibELF/AuxiliaryVector.h b/Userland/Libraries/LibELF/AuxiliaryVector.h index 0804914cb78..832395855e0 100644 --- a/Userland/Libraries/LibELF/AuxiliaryVector.h +++ b/Userland/Libraries/LibELF/AuxiliaryVector.h @@ -8,46 +8,10 @@ #include #include - -/* Auxiliary Vector types, from Intel386 ABI ver 1.0 section 2.3.3 */ -typedef struct -{ - long a_type; /* Note: Extended to long from int, for ease of compatibility w/64 bit */ - union { - long a_val; - void* a_ptr; - void (*a_fnc)(); /* In spec, not used */ - } a_un; -} auxv_t; +#include static_assert(sizeof(auxv_t) % sizeof(FlatPtr) == 0); -#define AT_NULL 0 /* No length, last entry's a_type has this value */ -#define AT_IGNORE 1 /* Entry has no meaning, a_un undefined */ -#define AT_EXECFD 2 /* a_val contains a file descriptor of the main program image */ -#define AT_PHDR 3 /* a_ptr contains pointer to program header table of main program image */ -#define AT_PHENT 4 /* a_val holds size of program header table entries */ -#define AT_PHNUM 5 /* a_val holds number of program header table entries */ -#define AT_PAGESZ 6 /* a_val gives system page size in bytes */ -#define AT_BASE 7 /* a_ptr holds base address that Loader was loaded into memory */ -#define AT_FLAGS 8 /* a_val holds 1 bit flags. Undefined flags are 0 */ -#define AT_ENTRY 9 /* a_ptr holds entry point of the main program */ -#define AT_NOTELF 10 /* a_val non-zero if the program is not ELF */ -#define AT_UID 11 /* a_val holds real user id of process */ -#define AT_EUID 12 /* a_val holds effective user id of process */ -#define AT_GID 13 /* a_val holds real group id of process */ -#define AT_EGID 14 /* a_val holds effective group id of process */ -#define AT_PLATFORM 15 /* a_val points to a string containing platform name */ -#define AT_HWCAP 16 /* a_val contains bitmask of CPU features. Equivalent to CPUID 1.EDX*/ -#define AT_CLKTCK 17 /* a_val contains frequence at which times() increments. (Re: Spec. What is times()?) */ -#define AT_SECURE 23 /* a_val holds 1 if program in secure mode (e.g. suid). Otherwise 0 */ -#define AT_BASE_PLATFORM 24 /* a_ptr points to a string identifying base platform name, which might be different from platform (e.g x86_64 when in i386 compat) */ -#define AT_RANDOM 25 /* a_ptr points to 16 securely generated random bytes */ -#define AT_HWCAP2 26 /* a_val holds extended hw feature mask. Currently 0 */ -#define AT_EXECFN 31 /* a_ptr points to filename of executed program */ -#define AT_EXE_BASE 32 /* a_ptr holds base address where main program was loaded into memory */ -#define AT_EXE_SIZE 33 /* a_val holds the size of the main program in memory */ - namespace ELF { struct AuxiliaryValue {