From 7b728abbef0b2b1d243afa206d728abe72c27fc0 Mon Sep 17 00:00:00 2001
From: lordrasmus <lordrasmus@gmail.com>
Date: Mon, 29 May 2023 20:32:24 +0200
Subject: [PATCH 1/9] add vsdo support

---
 extra/Configs/Config.in   | 17 +++++++++++++++++
 extra/Configs/Config.mips |  1 +
 ldso/include/ldso.h       |  7 +++++++
 ldso/ldso/Makefile.in     |  5 +++--
 ldso/ldso/dl-startup.c    |  3 +++
 ldso/ldso/ldso.c          |  4 +++-
 6 files changed, 34 insertions(+), 3 deletions(-)
 mode change 100644 => 100755 extra/Configs/Config.in
 mode change 100644 => 100755 extra/Configs/Config.mips

diff --git a/extra/Configs/Config.in b/extra/Configs/Config.in
old mode 100644
new mode 100755
index 6bbb6f572..323c2092f
--- a/extra/Configs/Config.in
+++ b/extra/Configs/Config.in
@@ -455,6 +455,22 @@ config LDSO_PRELINK_SUPPORT
 	  time. It also is able to load and handle prelinked libraries and
 	  binaries at runtime.
 
+config ARCH_VDSO_SUPPORT
+	bool
+	
+config VDSO_SUPPORT
+	bool "Dynamic linker vDSO support"
+	depends on ARCH_VDSO_SUPPORT
+	help
+	  Enable this option to support vDSO loading
+
+	  vDSO provides access to some kernel function without a systemcall
+	  if provided by the kernel
+
+	  most archs support gettimeofday() and clock_gettime()
+
+	  vDSO loading can be disabled via VDSO_DISABLE environment variable
+
 config UCLIBC_STATIC_LDCONFIG
 	bool "Link ldconfig statically"
 	depends on HAVE_SHARED
@@ -2321,6 +2337,7 @@ config SUPPORT_LD_DEBUG
 	    nofixups      never fixes up jump relocations
 	    bindings      displays the resolve processing (function calls);
 	                  detail shows the relocation patch
+	    vdso          display vdso symbol table processing
 	    all           Enable everything!
 
 	  The additional environment variable:
diff --git a/extra/Configs/Config.mips b/extra/Configs/Config.mips
old mode 100644
new mode 100755
index 8137d8be4..14b784fcc
--- a/extra/Configs/Config.mips
+++ b/extra/Configs/Config.mips
@@ -13,6 +13,7 @@ config FORCE_OPTIONS_FOR_ARCH
 	select ARCH_ANY_ENDIAN
 	select ARCH_HAS_UCONTEXT
 	select ARCH_HAS_DEPRECATED_SYSCALLS
+	select ARCH_VDSO_SUPPORT
 
 choice
 	prompt "Target ABI"
diff --git a/ldso/include/ldso.h b/ldso/include/ldso.h
index c035b3b52..8d9d057a0 100755
--- a/ldso/include/ldso.h
+++ b/ldso/include/ldso.h
@@ -109,6 +109,7 @@ extern char *_dl_debug_reloc;
 extern char *_dl_debug_detail;
 extern char *_dl_debug_nofixups;
 extern char *_dl_debug_bindings;
+extern char *_dl_debug_vdso;
 extern int   _dl_debug_file;
 # define __dl_debug_dprint(fmt, args...) \
 	_dl_dprintf(_dl_debug_file, "%s:%i: " fmt, __func__, __LINE__, ## args);
@@ -193,4 +194,10 @@ extern void *_dl_get_ready_to_run(struct elf_resolve *tpnt, DL_LOADADDR_TYPE loa
 #define AUX_MAX_AT_ID 40
 extern ElfW(auxv_t) _dl_auxvt[AUX_MAX_AT_ID];
 
+void load_vdso( uint32_t sys_info_ehdr, char **envp );
+
+#ifdef __VDSO_SUPPORT__
+extern void* _dl__vdso_gettimeofday;
+#endif
+
 #endif /* _LDSO_H */
diff --git a/ldso/ldso/Makefile.in b/ldso/ldso/Makefile.in
index 2d3886d73..6e8a0c388 100644
--- a/ldso/ldso/Makefile.in
+++ b/ldso/ldso/Makefile.in
@@ -63,14 +63,15 @@ ldso_FULL_NAME := $(UCLIBC_LDSO_NAME)-$(VERSION).so
 $(UCLIBC_LDSO_NAME)_DIR := $(top_srcdir)ldso/ldso
 $(UCLIBC_LDSO_NAME)_OUT := $(top_builddir)ldso/ldso
 
-$(UCLIBC_LDSO_NAME)_CSRC := $($(UCLIBC_LDSO_NAME)_DIR)/ldso.c
+$(UCLIBC_LDSO_NAME)_CSRC := $($(UCLIBC_LDSO_NAME)_DIR)/ldso.c $($(UCLIBC_LDSO_NAME)_DIR)/dl-vdso.c
 # prerequesites
 $($(UCLIBC_LDSO_NAME)_OUT)/ldso.o $($(UCLIBC_LDSO_NAME)_OUT)/ldso.oS: \
 	$($(UCLIBC_LDSO_NAME)_DIR)/dl-debug.c \
 	$($(UCLIBC_LDSO_NAME)_DIR)/dl-startup.c \
 	$($(UCLIBC_LDSO_NAME)_DIR)/dl-array.c \
 	$($(UCLIBC_LDSO_NAME)_DIR)/dl-hash.c \
-	$($(UCLIBC_LDSO_NAME)_DIR)/dl-elf.c
+	$($(UCLIBC_LDSO_NAME)_DIR)/dl-elf.c \
+	$($(UCLIBC_LDSO_NAME)_DIR)/dl-vdso.c
 $(UCLIBC_LDSO_NAME)_COBJ := $(patsubst $($(UCLIBC_LDSO_NAME)_DIR)/%.c,$($(UCLIBC_LDSO_NAME)_OUT)/%.o,$($(UCLIBC_LDSO_NAME)_CSRC))
 
 $(UCLIBC_LDSO_NAME)_SSRC := $(wildcard $($(UCLIBC_LDSO_NAME)_DIR)/$(TARGET_ARCH)/*.S)
diff --git a/ldso/ldso/dl-startup.c b/ldso/ldso/dl-startup.c
index e36d688b4..989711fcc 100644
--- a/ldso/ldso/dl-startup.c
+++ b/ldso/ldso/dl-startup.c
@@ -367,6 +367,9 @@ DL_START(unsigned long args)
 			_dl_get_ready_to_run(tpnt, load_addr, envp, argv
 					     DL_GET_READY_TO_RUN_EXTRA_ARGS);
 
+
+	load_vdso(_dl_auxvt[AT_SYSINFO_EHDR].a_un.a_val, envp);
+
 	/* Transfer control to the application.  */
 	SEND_STDERR_DEBUG("transfering control to application @ ");
 	SEND_ADDRESS_STDERR_DEBUG(_dl_elf_main, 1);
diff --git a/ldso/ldso/ldso.c b/ldso/ldso/ldso.c
index 8e4914df5..435bd43bc 100755
--- a/ldso/ldso/ldso.c
+++ b/ldso/ldso/ldso.c
@@ -73,6 +73,7 @@ char *_dl_debug_reloc     = NULL;
 char *_dl_debug_detail    = NULL;
 char *_dl_debug_nofixups  = NULL;
 char *_dl_debug_bindings  = NULL;
+char *_dl_debug_vdso      = NULL;
 int   _dl_debug_file      = 2;
 #endif
 
@@ -791,7 +792,7 @@ of this helper program; chances are you did not intend to run this program.\n\
 	if (_dl_debug) {
 		if (_dl_strstr(_dl_debug, "all")) {
 			_dl_debug_detail = _dl_debug_move = _dl_debug_symbols
-				= _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = (void*)1;
+				= _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = _dl_debug_vdso = (void*)1;
 		} else {
 			_dl_debug_detail   = _dl_strstr(_dl_debug, "detail");
 			_dl_debug_move     = _dl_strstr(_dl_debug, "move");
@@ -799,6 +800,7 @@ of this helper program; chances are you did not intend to run this program.\n\
 			_dl_debug_reloc    = _dl_strstr(_dl_debug, "reloc");
 			_dl_debug_nofixups = _dl_strstr(_dl_debug, "nofix");
 			_dl_debug_bindings = _dl_strstr(_dl_debug, "bind");
+			_dl_debug_vdso     = _dl_strstr(_dl_debug, "vdso");
 		}
 	}
 
-- 
2.37.2

