[git commit ldso-future] ldso/libdl: hide _dl_malloc and friends, add _dl_malloc_function to _rtld_global structure

Peter S. Mazinger ps.m at gmx.net
Mon Apr 11 11:27:03 UTC 2011


commit: http://git.uclibc.org/uClibc/commit/?id=4f9fe3f11077989064ef448a28c9a235dc7744f0
branch: http://git.uclibc.org/uClibc/commit/?id=refs/heads/ldso-future

Signed-off-by: Peter S. Mazinger <ps.m at gmx.net>
---
 ldso/include/ldso.h     |   12 +++++++-----
 ldso/include/ldsodefs.h |   15 ++++++++-------
 ldso/ldso/dl-tls.c      |   14 +++++++-------
 ldso/ldso/ldso.c        |   41 ++++++++++++++++++++---------------------
 ldso/libdl/libdl.c      |   12 +++++++++---
 5 files changed, 51 insertions(+), 43 deletions(-)

diff --git a/ldso/include/ldso.h b/ldso/include/ldso.h
index 18156dd..8f7db6c 100644
--- a/ldso/include/ldso.h
+++ b/ldso/include/ldso.h
@@ -95,11 +95,13 @@ extern void **_dl_initial_error_catch_tsd(void) __attribute__((const))
 #endif /* __SUPPORT_LD_DEBUG_EARLY__ */
 
 #ifdef IS_IN_rtld
-extern void *_dl_malloc(size_t size);
-extern void *_dl_calloc(size_t __nmemb, size_t __size);
-extern void *_dl_realloc(void *__ptr, size_t __size);
-extern void *_dl_memalign(size_t __boundary, size_t __size);
-extern void _dl_free(void *);
+extern void *_dl_malloc(size_t size) attribute_hidden;
+extern void _dl_free(void *) attribute_hidden;
+# ifdef __UCLIBC_HAS_TLS__
+extern void *_dl_calloc(size_t __nmemb, size_t __size) attribute_hidden;
+extern void *_dl_realloc(void *__ptr, size_t __size) attribute_hidden;
+extern void *_dl_memalign(size_t __boundary, size_t __size) attribute_hidden;
+# endif
 #else
 # include <stdlib.h>
 # define _dl_malloc malloc
diff --git a/ldso/include/ldsodefs.h b/ldso/include/ldsodefs.h
index 90dee51..bf6f3b1 100644
--- a/ldso/include/ldsodefs.h
+++ b/ldso/include/ldsodefs.h
@@ -196,6 +196,9 @@ struct rtld_global
 	/* Used to communicate with the gdb debugger */
 	EXTERN struct r_debug *_dl_debug_addr;
 
+	EXTERN void *(*_dl_malloc_function) (size_t);
+	EXTERN void (*_dl_free_function) (void *);
+
 #ifdef __UCLIBC_HAS_THREADS__
 	/* Function pointer for catching TLS errors.  */
 	EXTERN void **(*_dl_error_catch_tsd) (void) __attribute__ ((const));
@@ -238,6 +241,11 @@ struct rtld_global
 	EXTERN size_t _dl_tls_generation;
 
 	EXTERN void (*_dl_init_static_tls) (struct link_map *);
+
+	/* uClibc-specific */
+	EXTERN void *(*_dl_calloc_function) (size_t, size_t);
+	EXTERN void *(*_dl_realloc_function) (void *, size_t);
+	EXTERN void *(*_dl_memalign_function) (size_t, size_t);
 #endif
 
 #ifdef __SUPPORT_LD_DEBUG__
@@ -269,10 +277,6 @@ struct rtld_global_ro
 	int (*_dl_fixup) (struct dyn_elf *, int);
 	void (internal_function *_dl_protect_relro) (struct elf_resolve *);
 	char *(*_dl_find_hash) (const char *, struct dyn_elf *, struct elf_resolve *, int, struct symbol_ref *);
-# if 0 /* psm: ask Bernd Schmid */
-	void *(*_dl_malloc_function) (size_t);
-	void (*_dl_free_function) (void *);
-# endif
 # ifdef __LDSO_CACHE_SUPPORT__
 	int (*_dl_map_cache) (void);
 	int (*_dl_unmap_cache) (void);
@@ -327,9 +331,6 @@ extern char *_dl_find_hash(const char *name, struct dyn_elf *rpnt, struct elf_re
 extern void _dl_protect_relro (struct elf_resolve *map)
 	internal_function attribute_shared_hidden;
 
-extern void *(*_dl_malloc_function) (size_t) /*attribute_shared_hidden*/;
-extern void (*_dl_free_function) (void *) /*attribute_shared_hidden*/;
-
 #ifdef __LDSO_CACHE_SUPPORT__
 extern int _dl_map_cache(void) attribute_shared_hidden;
 extern int _dl_unmap_cache(void) attribute_shared_hidden;
diff --git a/ldso/ldso/dl-tls.c b/ldso/ldso/dl-tls.c
index fa304c6..189bf33 100644
--- a/ldso/ldso/dl-tls.c
+++ b/ldso/ldso/dl-tls.c
@@ -30,11 +30,11 @@
 #include <dl-tls.h>
 #include <ldsodefs.h>
 
+#ifndef SHARED
 void *(*_dl_calloc_function) (size_t __nmemb, size_t __size) = NULL;
 void *(*_dl_realloc_function) (void *__ptr, size_t __size) = NULL;
 void *(*_dl_memalign_function) (size_t __boundary, size_t __size) = NULL;
-
-void (*_dl_free_function) (void *__ptr);
+#endif
 
 /* Round up N to the nearest multiple of P, where P is a power of 2
    --- without using libgcc division routines.  */
@@ -46,8 +46,8 @@ _dl_calloc (size_t __nmemb, size_t __size)
 	void *result;
 	size_t size = (__size * __nmemb);
 
-	if (_dl_calloc_function)
-		return (*_dl_calloc_function) (__nmemb, __size);
+	if (GL(dl_calloc_function))
+		return (*GL(dl_calloc_function)) (__nmemb, __size);
 
 	if ((result = _dl_malloc(size)) != NULL) {
 		_dl_memset(result, 0, size);
@@ -59,8 +59,8 @@ _dl_calloc (size_t __nmemb, size_t __size)
 void *
 _dl_realloc (void * __ptr, size_t __size)
 {
-	if (_dl_realloc_function)
-		return (*_dl_realloc_function) (__ptr, __size);
+	if (GL(dl_realloc_function))
+		return (*GL(dl_realloc_function)) (__ptr, __size);
 
 	_dl_debug_early("NOT IMPLEMENTED PROPERLY!!!\n");
 	return NULL;
@@ -202,7 +202,7 @@ _dl_nothread_init_static_tls (struct link_map *map)
 #endif
 
 /* Taken from glibc/sysdeps/generic/dl-tls.c */
-static void
+static attribute_noreturn void
 oom (void)
 {
 	_dl_debug_early("cannot allocate thread-local memory: ABORT\n");
diff --git a/ldso/ldso/ldso.c b/ldso/ldso/ldso.c
index d96396c..49c14a1 100644
--- a/ldso/ldso/ldso.c
+++ b/ldso/ldso/ldso.c
@@ -39,7 +39,14 @@ struct rtld_global _rtld_global =
 {
 	._dl_loaded_modules = NULL,
 	._dl_symbol_tables = NULL,
-	._dl_debug_addr = NULL
+	._dl_debug_addr = NULL,
+	._dl_malloc_function = NULL,
+	._dl_free_function = NULL
+#ifdef __UCLIBC_HAS_TLS__
+,	._dl_calloc_function = NULL,
+	._dl_realloc_function = NULL,
+	._dl_memalign_function = NULL
+#endif
 #ifdef __SUPPORT_LD_DEBUG__
 ,	._dl_debug = NULL
 #endif
@@ -55,10 +62,6 @@ struct rtld_global_ro _rtld_global_ro attribute_relro =
 	._dl_fixup = _dl_fixup,
 	._dl_protect_relro = _dl_protect_relro,
 	._dl_find_hash = _dl_find_hash
-# if 0 /* psm: ask Bernd Schmid */
-,	._dl_malloc_function = _dl_malloc_function,
-	._dl_free_function = _dl_free_function
-# endif
 # ifdef __LDSO_CACHE_SUPPORT__
 ,	._dl_map_cache = _dl_map_cache,
 	._dl_unmap_cache = _dl_unmap_cache
@@ -66,7 +69,7 @@ struct rtld_global_ro _rtld_global_ro attribute_relro =
 # ifdef __mips__
 ,	._dl_perform_mips_global_got_relocations = _dl_perform_mips_global_got_relocations
 # endif
-# ifdef USE_TLS
+# ifdef __UCLIBC_HAS_TLS__
 # if 0
 #  ifdef __i386__
 ,	._tls_get_addr = ___tls_get_addr_internal
@@ -215,9 +218,6 @@ static char *_dl_preload       = NULL;	/* Things to be loaded before the libs */
 char *_dl_ldsopath             = NULL;	/* Location of the shared lib loader */
 int _dl_errno                  = 0;	/* We can't use the real errno in ldso */
 
-void *(*_dl_malloc_function) (size_t size) = NULL;
-void (*_dl_free_function) (void *p) = NULL;
-
 static int _dl_secure = 1; /* Are we dealing with setuid stuff? */
 
 
@@ -321,8 +321,8 @@ void *_dl_malloc(size_t size)
 	_dl_debug_early("request for %d bytes\n", size);
 #endif
 
-	if (_dl_malloc_function)
-		return (*_dl_malloc_function) (size);
+	if (GL(dl_malloc_function))
+		return (*GL(dl_malloc_function)) (size);
 
 	if (_dl_malloc_addr - _dl_mmap_zero + size > GLRO(dl_pagesize)) {
 		size_t rounded_size;
@@ -372,8 +372,8 @@ static void *_dl_zalloc(size_t size)
 
 void _dl_free(void *p)
 {
-	if (_dl_free_function)
-		(*_dl_free_function) (p);
+	if (GL(dl_free_function))
+		(*GL(dl_free_function)) (p);
 }
 
 #ifdef __UCLIBC_HAS_TLS__
@@ -384,8 +384,8 @@ void *_dl_memalign(size_t __boundary, size_t __size)
 	size_t delta;
 	size_t rounded = 0;
 
-	if (_dl_memalign_function)
-		return (*_dl_memalign_function) (__boundary, __size);
+	if (GL(dl_memalign_function))
+		return (*GL(dl_memalign_function)) (__boundary, __size);
 
 	while (rounded < __boundary) {
 		rounded = (1 << i++);
@@ -1223,23 +1223,22 @@ static __always_inline void _dl_get_ready_to_run(struct elf_resolve *tpnt, DL_LO
 	}
 
 	/* Find the real malloc function and make ldso functions use that from now on */
-	_dl_malloc_function = (void* (*)(size_t)) (intptr_t) _dl_find_hash(__C_SYMBOL_PREFIX__ "malloc",
+	GL(dl_malloc_function) = (void* (*)(size_t)) (intptr_t) _dl_find_hash(__C_SYMBOL_PREFIX__ "malloc",
 			GL(dl_symbol_tables), NULL, ELF_RTYPE_CLASS_PLT, NULL);
 
-	_dl_free_function = (void (*)(void *)) (intptr_t)
+	GL(dl_free_function) = (void (*)(void *)) (intptr_t)
 		_dl_find_hash(__C_SYMBOL_PREFIX__ "free", GL(dl_symbol_tables), NULL, ELF_RTYPE_CLASS_PLT, NULL);
 
 #ifdef __UCLIBC_HAS_TLS__
 	/* Find the real functions and make ldso functions use them from now on */
-	_dl_calloc_function = (void* (*)(size_t, size_t)) (intptr_t)
+	GL(dl_calloc_function) = (void* (*)(size_t, size_t)) (intptr_t)
 		_dl_find_hash(__C_SYMBOL_PREFIX__ "calloc", GL(dl_symbol_tables), NULL, ELF_RTYPE_CLASS_PLT, NULL);
 
-	_dl_realloc_function = (void* (*)(void *, size_t)) (intptr_t)
+	GL(dl_realloc_function) = (void* (*)(void *, size_t)) (intptr_t)
 		_dl_find_hash(__C_SYMBOL_PREFIX__ "realloc", GL(dl_symbol_tables), NULL, ELF_RTYPE_CLASS_PLT, NULL);
 
-	_dl_memalign_function = (void* (*)(size_t, size_t)) (intptr_t)
+	GL(dl_memalign_function) = (void* (*)(size_t, size_t)) (intptr_t)
 		_dl_find_hash(__C_SYMBOL_PREFIX__ "memalign", GL(dl_symbol_tables), NULL, ELF_RTYPE_CLASS_PLT, NULL);
-
 #endif
 
 	/* Notify the debugger that all objects are now mapped in.  */
diff --git a/ldso/libdl/libdl.c b/ldso/libdl/libdl.c
index 20c827a..7fb33ef 100644
--- a/ldso/libdl/libdl.c
+++ b/ldso/libdl/libdl.c
@@ -32,6 +32,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdbool.h>
+#include <malloc.h>
 #include <ldsodefs.h>
 #include <ldso.h>
 #ifdef __UCLIBC_HAS_TLS__
@@ -46,7 +47,7 @@
 
 static const char *_dl_progname= "";           /* Program name */
 void *(*_dl_malloc_function)(size_t) = NULL;
-void (*_dl_free_function) (void *p);
+void (*_dl_free_function) (void *p) = NULL;
 char *_dl_library_path         = NULL;         /* Where we look for libraries */
 char *_dl_ldsopath             = NULL;         /* Location of the shared lib loader */
 size_t _dl_pagesize            = PAGE_SIZE; /* Store the page size for later use */
@@ -252,8 +253,13 @@ void *dlopen(const char *libname, int flag)
 
 	if (!_dl_init) {
 		_dl_init = true;
-		_dl_malloc_function = malloc;
-		_dl_free_function = free;
+		GL(dl_malloc_function) = malloc;
+		GL(dl_free_function) = free;
+#ifdef __UCLIBC_HAS_TLS__
+		GL(dl_calloc_function) = calloc;
+		GL(dl_realloc_function) = realloc;
+		GL(dl_memalign_function) = memalign;
+#endif
 	}
 	/* Cover the trivial case first */
 	if (!libname)
-- 
1.7.3.4



More information about the uClibc-cvs mailing list