Browse Source

Support non-cached entries in getauxval

Previously the getauxval implementation was limited to the auxiliary
vector entries cached in _dl_auxvt. To also support entries outside of
that cached subset, store the start address of the auxiliary vector,
and if an uncached entry type is encountered search the entire auxiliary
vector.

Signed-off-by: Marcus Haehnel <marcus.haehnel@kernkonzept.com>
Georg Kotheimer 2 months ago
parent
commit
737a0edf4d
4 changed files with 22 additions and 18 deletions
  1. 2 1
      ldso/include/ldso.h
  2. 4 0
      ldso/ldso/dl-startup.c
  3. 13 17
      libc/misc/auxvt/getauxval.c
  4. 3 0
      libc/misc/elf/dl-support.c

+ 2 - 1
ldso/include/ldso.h

@@ -192,7 +192,8 @@ extern void *_dl_get_ready_to_run(struct elf_resolve *tpnt, DL_LOADADDR_TYPE loa
 #endif
 
 #define AUX_MAX_AT_ID 40
-extern ElfW(auxv_t) _dl_auxvt[AUX_MAX_AT_ID];
+extern ElfW(auxv_t) _dl_auxvt[AUX_MAX_AT_ID]; /* Cache frequently accessed auxiliary vector entries */
+extern ElfW(auxv_t) *_dl_auxv_start;          /* Start of the auxiliary vector */
 
 void load_vdso(void *sys_info_ehdr, char **envp );
 

+ 4 - 0
ldso/ldso/dl-startup.c

@@ -100,6 +100,7 @@ extern ElfW(Addr) _begin[] attribute_hidden;
 
 
 ElfW(auxv_t) _dl_auxvt[AUX_MAX_AT_ID];
+ElfW(auxv_t) *_dl_auxv_start;
 
 #ifdef LDSO_NEED_DPNT
 ElfW(Dyn) *_dl_saved_dpnt = 0;
@@ -131,6 +132,7 @@ DL_START(unsigned long args)
 	struct elf_resolve tpnt_tmp;
 	struct elf_resolve *tpnt = &tpnt_tmp;
 	ElfW(auxv_t) _dl_auxvt_tmp[AUX_MAX_AT_ID];
+	ElfW(auxv_t) *_dl_auxv_start_tmp;
 	ElfW(Dyn) *dpnt;
 	uint32_t  *p32;
 
@@ -166,6 +168,7 @@ DL_START(unsigned long args)
 	/* The junk on the stack immediately following the environment is
 	 * the Auxiliary Vector Table.  Read out the elements of the auxvt,
 	 * sort and store them in auxvt for later use. */
+	_dl_auxv_start_tmp = (ElfW(auxv_t) *)aux_dat;
 	while (*aux_dat) {
 		ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
 
@@ -367,6 +370,7 @@ DL_START(unsigned long args)
 	*  now the globals work. so copy the aux vector
 	*/
 	_dl_memcpy( _dl_auxvt, _dl_auxvt_tmp, sizeof( ElfW(auxv_t) ) * AUX_MAX_AT_ID );
+	_dl_auxv_start = _dl_auxv_start_tmp;
 
 	_dl_elf_main = (int (*)(int, char **, char **))
 			_dl_get_ready_to_run(tpnt, load_addr, envp, argv

+ 13 - 17
libc/misc/auxvt/getauxval.c

@@ -17,32 +17,28 @@
  *  <http://www.gnu.org/licenses/>.
  */
 
-#include "errno.h"
-#include "ldso.h"
-#include "sys/auxv.h"
+#include <errno.h>
+#include <ldso.h>
+#include <sys/auxv.h>
 
-
-/*
- *
- * aarch64 gcc 11 uses __getauxval() in init_have_lse_atomics()
- *
- */
 unsigned long int __getauxval (unsigned long int __type)
 {
-	if ( __type >= AUX_MAX_AT_ID ){
+	// Requested value part of cached subset of auxiliary vector?
+	if (__type < AUX_MAX_AT_ID) {
+		if (_dl_auxvt[__type].a_type == __type)
+			return _dl_auxvt[__type].a_un.a_val;
+
 		__set_errno (ENOENT);
 		return 0;
 	}
 
-	if ( _dl_auxvt[__type].a_type == __type){
-		return _dl_auxvt[__type].a_un.a_val;
-	}
+	// Otherwise we have to iterate the auxiliary vector.
+	for (ElfW(auxv_t) *entry = _dl_auxv_start; entry->a_type != AT_NULL; entry++)
+		if (entry->a_type == __type)
+			return entry->a_un.a_val;
 
 	__set_errno (ENOENT);
 	return 0;
 }
 
-unsigned long int getauxval (unsigned long int __type){
-	return __getauxval( __type );
-}
-
+weak_alias(__getauxval, getauxval)

+ 3 - 0
libc/misc/elf/dl-support.c

@@ -33,10 +33,13 @@ size_t _dl_phnum;
 size_t _dl_pagesize;
 
 ElfW(auxv_t) _dl_auxvt[AUX_MAX_AT_ID];
+ElfW(auxv_t) *_dl_auxv_start;
 
 void internal_function _dl_aux_init (ElfW(auxv_t) *av);
 void internal_function _dl_aux_init (ElfW(auxv_t) *av)
 {
+   _dl_auxv_start = av;
+
    memset(_dl_auxvt, 0x00, sizeof(_dl_auxvt));
    for (; av->a_type != AT_NULL; av++)
      {