From a707192f6c249d397594b2d6a0b2f38f3cdf4da7 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Fri, 30 Jul 2004 03:32:41 +0000 Subject: Teach libdl to use normal libc symbols whenever possible, i.e. malloc rather than _dl_malloc, fprintf rather than _dl_printf, etc. -Erik --- ldso/Makefile | 11 +++++-- ldso/include/.cvsignore | 8 +++-- ldso/include/ldso.h | 1 + ldso/ldso/.cvsignore | 1 - ldso/ldso/Makefile | 9 ++---- ldso/ldso/dl-elf.c | 37 --------------------- ldso/ldso/ldso.c | 44 ++++++++++++++++++++++--- ldso/libdl/Makefile | 4 +-- ldso/libdl/libdl.c | 85 ++++++++++++++++++------------------------------- 9 files changed, 90 insertions(+), 110 deletions(-) diff --git a/ldso/Makefile b/ldso/Makefile index 3836a01d3..802d4563a 100644 --- a/ldso/Makefile +++ b/ldso/Makefile @@ -36,7 +36,13 @@ else echo "Not building libdl" endif -headers: +include/dl-progname.h: Makefile + echo "const char *_dl_progname=\""$(UCLIBC_LDSO)"\";" \ + > include/dl-progname.h + echo -e "#include \"$(TARGET_ARCH)/elfinterp.c\"" \ + >> include/dl-progname.h + +headers: include/dl-progname.h $(LN) -fs $(TOPDIR)../include/elf.h include/ $(LN) -fs ../ldso/$(TARGET_ARCH)/dl-startup.h include/ $(LN) -fs ../ldso/$(TARGET_ARCH)/dl-syscalls.h include/ @@ -45,4 +51,5 @@ headers: clean: set -e ; for d in $(ALL_SUBDIRS) ; do $(MAKE) -C $$d $@ ; done -find . -name '*~' | xargs $(RM) - $(RM) include/elf.h include/boot1_arch.h include/ld_syscalls.h include/ld_sysdep.h + $(RM) include/elf.h include/boot1_arch.h include/ld_syscalls.h \ + include/ld_sysdep.h include/dl-progname.h diff --git a/ldso/include/.cvsignore b/ldso/include/.cvsignore index b48ed1fa5..20a602fa4 100644 --- a/ldso/include/.cvsignore +++ b/ldso/include/.cvsignore @@ -1,4 +1,6 @@ +dl-arch.h +dl-progname.h +dl-startup.h +dl-syscalls.h +dl-sysdep.h elf.h -ld_syscalls.h -ld_sysdep.h -boot1_arch.h diff --git a/ldso/include/ldso.h b/ldso/include/ldso.h index 6c01cebc1..6bb87c264 100644 --- a/ldso/include/ldso.h +++ b/ldso/include/ldso.h @@ -46,6 +46,7 @@ extern unsigned long *_dl_brkp; /* The end of the data segment for brk an extern unsigned long *_dl_envp; /* The environment address */ extern int _dl_secure; /* Are we dealing with setuid stuff? */ extern size_t _dl_pagesize; /* Store the page size for use later */ +extern const char *_dl_progname; /* The name of the shared library loader */ #ifdef __SUPPORT_LD_DEBUG__ extern char *_dl_debug; diff --git a/ldso/ldso/.cvsignore b/ldso/ldso/.cvsignore index 5fe696107..90e77f02f 100644 --- a/ldso/ldso/.cvsignore +++ b/ldso/ldso/.cvsignore @@ -1,2 +1 @@ ld-uclibc.so* -dl-progname.h diff --git a/ldso/ldso/Makefile b/ldso/ldso/Makefile index db493c3bc..3c1d04530 100644 --- a/ldso/ldso/Makefile +++ b/ldso/ldso/Makefile @@ -70,18 +70,13 @@ XXFLAGS := $(XXFLAGS:-fomit-frame-pointer=) all: lib -lib:: dl-progname.h $(OBJS) $(DLINK_OBJS) +lib:: $(OBJS) $(DLINK_OBJS) $(LD) $(LDFLAGS) -e _dl_boot -soname=$(UCLIBC_LDSO) \ -o $(LDSO_FULLNAME) $(OBJS) $(LIBGCC); $(INSTALL) -d $(TOPDIR)lib $(INSTALL) -m 755 $(LDSO_FULLNAME) $(TOPDIR)lib $(LN) -sf $(LDSO_FULLNAME) $(TOPDIR)lib/$(UCLIBC_LDSO) -dl-progname.h: Makefile - echo "const char *_dl_progname=\""$(UCLIBC_LDSO)"\";" > dl-progname.h - echo "#include \"$(TARGET_ARCH)/elfinterp.c\"" >> dl-progname.h - - $(COBJS): %.o : %.c $(CC) $(XXFLAGS) -I../libdl -c $< -o $@ $(STRIPTOOL) -x -R .note -R .comment $*.o @@ -93,4 +88,4 @@ $(AOBJS): %.o : %.S ldso.o: $(CSRC) clean: - $(RM) $(UCLIBC_LDSO)* $(OBJS) $(LDSO_FULLNAME)* core *.o *.a *.s *.i dl-progname.h ldso.h *~ + $(RM) $(UCLIBC_LDSO)* $(OBJS) $(LDSO_FULLNAME)* core *.o *.a *.s *.i ldso.h *~ diff --git a/ldso/ldso/dl-elf.c b/ldso/ldso/dl-elf.c index d27e4ccb3..2dd6fc4d0 100644 --- a/ldso/ldso/dl-elf.c +++ b/ldso/ldso/dl-elf.c @@ -888,40 +888,3 @@ char *_dl_strdup(const char *string) return retval; } -void *(*_dl_malloc_function) (size_t size) = NULL; -void *_dl_malloc(int size) -{ - void *retval; - -#if 0 -#ifdef __SUPPORT_LD_DEBUG_EARLY__ - _dl_dprintf(2, "malloc: request for %d bytes\n", size); -#endif -#endif - - if (_dl_malloc_function) - return (*_dl_malloc_function) (size); - - if (_dl_malloc_addr - _dl_mmap_zero + size > _dl_pagesize) { -#ifdef __SUPPORT_LD_DEBUG_EARLY__ - _dl_dprintf(2, "malloc: mmapping more memory\n"); -#endif - _dl_mmap_zero = _dl_malloc_addr = _dl_mmap((void *) 0, size, - PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (_dl_mmap_check_error(_dl_mmap_zero)) { - _dl_dprintf(2, "%s: mmap of a spare page failed!\n", _dl_progname); - _dl_exit(20); - } - } - retval = _dl_malloc_addr; - _dl_malloc_addr += size; - - /* - * Align memory to 4 byte boundary. Some platforms require this, others - * simply get better performance. - */ - _dl_malloc_addr = (unsigned char *) (((unsigned long) _dl_malloc_addr + 3) & ~(3)); - return retval; -} - - diff --git a/ldso/ldso/ldso.c b/ldso/ldso/ldso.c index 9b7c7380e..a37b250ce 100644 --- a/ldso/ldso/ldso.c +++ b/ldso/ldso/ldso.c @@ -41,13 +41,12 @@ char *_dl_library_path = 0; /* Where we look for libraries */ char *_dl_preload = 0; /* Things to be loaded before the libs */ char *_dl_ldsopath = 0; /* Location of the shared lib loader */ -unsigned char *_dl_malloc_addr = 0; /* Lets _dl_malloc use the already allocated memory page */ -unsigned char *_dl_mmap_zero = 0; /* Also used by _dl_malloc */ unsigned long *_dl_brkp = 0; /* The end of the data segment for brk and sbrk */ unsigned long *_dl_envp = 0; /* The environment address */ int _dl_secure = 1; /* Are we dealing with setuid stuff? */ int _dl_errno = 0; /* We can't use the real errno in ldso */ size_t _dl_pagesize = 0; /* Store the page size for use later */ +struct r_debug *_dl_debug_addr = NULL; /* Used to communicate with the gdb debugger */ @@ -74,8 +73,8 @@ void _dl_debug_state(void) { } -/* This global variable is also to communicate with debuggers such as gdb. */ -struct r_debug *_dl_debug_addr = NULL; +static unsigned char *_dl_malloc_addr = 0; /* Lets _dl_malloc use the already allocated memory page */ +static unsigned char *_dl_mmap_zero = 0; /* Also used by _dl_malloc */ @@ -757,5 +756,42 @@ static int _dl_suid_ok(void) return 0; } +void *(*_dl_malloc_function) (size_t size) = NULL; +void *_dl_malloc(int size) +{ + void *retval; + +#if 0 +#ifdef __SUPPORT_LD_DEBUG_EARLY__ + _dl_dprintf(2, "malloc: request for %d bytes\n", size); +#endif +#endif + + if (_dl_malloc_function) + return (*_dl_malloc_function) (size); + + if (_dl_malloc_addr - _dl_mmap_zero + size > _dl_pagesize) { +#ifdef __SUPPORT_LD_DEBUG_EARLY__ + _dl_dprintf(2, "malloc: mmapping more memory\n"); +#endif + _dl_mmap_zero = _dl_malloc_addr = _dl_mmap((void *) 0, size, + PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (_dl_mmap_check_error(_dl_mmap_zero)) { + _dl_dprintf(2, "%s: mmap of a spare page failed!\n", _dl_progname); + _dl_exit(20); + } + } + retval = _dl_malloc_addr; + _dl_malloc_addr += size; + + /* + * Align memory to 4 byte boundary. Some platforms require this, others + * simply get better performance. + */ + _dl_malloc_addr = (unsigned char *) (((unsigned long) _dl_malloc_addr + 3) & ~(3)); + return retval; +} + + #include "dl-hash.c" #include "dl-elf.c" diff --git a/ldso/libdl/Makefile b/ldso/libdl/Makefile index 55fff3988..b8e635b6c 100644 --- a/ldso/libdl/Makefile +++ b/ldso/libdl/Makefile @@ -23,12 +23,12 @@ include $(TOPDIR)Rules.mak XXFLAGS=$(XWARNINGS) $(OPTIMIZATION) $(XARCH_CFLAGS) $(CPU_CFLAGS) \ -DUCLIBC_RUNTIME_PREFIX=\"$(RUNTIME_PREFIX)\" \ - -fno-builtin -nostdinc -D_LIBC -I$(TOPDIR)ldso/include -I. -I$(TOPDIR)include + -fno-builtin -nostdinc -D_LIBC -I$(TOPDIR)ldso/include -I$(TOPDIR)ldso/ldso -I. -I$(TOPDIR)include ifeq ($(DODEBUG),y) XXFLAGS=$(XWARNINGS) -O0 -g3 $(XARCH_CFLAGS) $(CPU_CFLAGS) \ -DUCLIBC_RUNTIME_PREFIX=\"$(RUNTIME_PREFIX)\" \ - -fno-builtin -nostdinc -D_LIBC -I$(TOPDIR)ldso/include -I. -I$(TOPDIR)include + -fno-builtin -nostdinc -D_LIBC -I$(TOPDIR)ldso/include -I$(TOPDIR)ldso/ldso I. -I$(TOPDIR)include endif XXFLAGS+=$(shell $(CC) -print-search-dirs | sed -ne "s/install: *\(.*\)/-I\1include/gp") diff --git a/ldso/libdl/libdl.c b/ldso/libdl/libdl.c index 0218d8aa5..f20041bc2 100644 --- a/ldso/libdl/libdl.c +++ b/ldso/libdl/libdl.c @@ -31,6 +31,7 @@ #include +#include #if defined (__LIBDL_SHARED__) @@ -38,7 +39,6 @@ /* When libdl is loaded as a shared library, we need to load in * and use a pile of symbols from ldso... */ -extern void _dl_dprintf(int, const char *, ...) __attribute__ ((__weak__)); extern char *_dl_find_hash(const char *, struct dyn_elf *, int) __attribute__ ((__weak__)); extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **, @@ -64,13 +64,6 @@ extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt) #endif #ifdef __SUPPORT_LD_DEBUG__ extern char *_dl_debug __attribute__ ((__weak__)); -extern char *_dl_debug_symbols __attribute__ ((__weak__)); -extern char *_dl_debug_move __attribute__ ((__weak__)); -extern char *_dl_debug_reloc __attribute__ ((__weak__)); -extern char *_dl_debug_detail __attribute__ ((__weak__)); -extern char *_dl_debug_nofixups __attribute__ ((__weak__)); -extern char *_dl_debug_bindings __attribute__ ((__weak__)); -extern int _dl_debug_file __attribute__ ((__weak__)); #endif @@ -81,22 +74,15 @@ extern int _dl_debug_file __attribute__ ((__weak__)); #ifdef __SUPPORT_LD_DEBUG__ char *_dl_debug = 0; -char *_dl_debug_symbols = 0; -char *_dl_debug_move = 0; -char *_dl_debug_reloc = 0; -char *_dl_debug_detail = 0; -char *_dl_debug_nofixups = 0; -char *_dl_debug_bindings = 0; -int _dl_debug_file = 2; #endif -char *_dl_library_path = 0; -char *_dl_ldsopath = 0; +char *_dl_library_path = 0; /* Where we look for libraries */ +char *_dl_ldsopath = 0; /* Location of the shared lib loader */ +int _dl_errno = 0; /* We can't use the real errno in ldso */ +size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */ +/* This global variable is also to communicate with debuggers such as gdb. */ struct r_debug *_dl_debug_addr = NULL; -static unsigned char *_dl_malloc_addr, *_dl_mmap_zero; -void *(*_dl_malloc_function) (size_t size); -int _dl_errno = 0; -int _dl_fixup(struct dyn_elf *rpnt, int lazy); -#include "../ldso/dl-progname.h" /* Pull in the name of ld.so */ +#define _dl_malloc malloc +#include "dl-progname.h" #include "../ldso/dl-hash.c" #define _dl_trace_loaded_objects 0 #include "../ldso/dl-elf.c" @@ -142,7 +128,7 @@ static void __attribute__ ((destructor)) dl_cleanup(void) } } -void *_dlopen(const char *libname, int flag) +void *dlopen(const char *libname, int flag) { struct elf_resolve *tpnt, *tfrom, *tcurr; struct dyn_elf *dyn_chain, *rpnt = NULL; @@ -163,7 +149,9 @@ void *_dlopen(const char *libname, int flag) /* Have the dynamic linker use the regular malloc function now */ if (!dl_init) { dl_init++; +#if defined (__LIBDL_SHARED__) _dl_malloc_function = malloc; +#endif } /* Cover the trivial case first */ @@ -191,7 +179,7 @@ void *_dlopen(const char *libname, int flag) /* Try to load the specified library */ #ifdef __SUPPORT_LD_DEBUG__ if(_dl_debug) - _dl_dprintf(_dl_debug_file, "Trying to dlopen '%s'\n", (char*)libname); + fprintf(stderr, "Trying to dlopen '%s'\n", (char*)libname); #endif if (!(tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0))) tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0); @@ -220,7 +208,7 @@ void *_dlopen(const char *libname, int flag) #ifdef __SUPPORT_LD_DEBUG__ if(_dl_debug) - _dl_dprintf(_dl_debug_file, "Looking for needed libraries\n"); + fprintf(stderr, "Looking for needed libraries\n"); #endif for (tcurr = tpnt; tcurr; tcurr = tcurr->next) @@ -240,7 +228,7 @@ void *_dlopen(const char *libname, int flag) #ifdef __SUPPORT_LD_DEBUG__ if(_dl_debug) - _dl_dprintf(_dl_debug_file, "Trying to load '%s', needed by '%s'\n", + fprintf(stderr, "Trying to load '%s', needed by '%s'\n", lpntstr, tcurr->libname); #endif @@ -273,7 +261,7 @@ void *_dlopen(const char *libname, int flag) #ifdef __SUPPORT_LD_DEBUG__ if(_dl_debug) - _dl_dprintf(_dl_debug_file, "Beginning dlopen relocation fixups\n"); + fprintf(stderr, "Beginning dlopen relocation fixups\n"); #endif /* * OK, now all of the kids are tucked into bed in their proper addresses. @@ -298,11 +286,6 @@ void *_dlopen(const char *libname, int flag) } } -#if 0 //def __SUPPORT_LD_DEBUG__ - if(_dl_debug) - _dlinfo(); -#endif - #if defined (__LIBDL_SHARED__) /* Find the last library so we can run things in the right order */ for (tpnt = dyn_chain->dyn; tpnt->next!=NULL; tpnt = tpnt->next) @@ -328,7 +311,7 @@ void *_dlopen(const char *libname, int flag) if (dl_elf_func && *dl_elf_func != NULL) { #ifdef __SUPPORT_LD_DEBUG__ if(_dl_debug) - _dl_dprintf(2, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func); + fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func); #endif (*dl_elf_func) (); } @@ -339,7 +322,7 @@ void *_dlopen(const char *libname, int flag) if (dl_elf_func && *dl_elf_func != NULL) { #ifdef __SUPPORT_LD_DEBUG__ if(_dl_debug) - _dl_dprintf(2, "setting up dtors for library %s at '%x'\n", tpnt->libname, dl_elf_func); + fprintf(stderr, "setting up dtors for library %s at '%x'\n", tpnt->libname, dl_elf_func); #endif atexit(dl_elf_func); } @@ -354,9 +337,8 @@ oops: do_dlclose(dyn_chain, 0); return NULL; } -weak_alias(_dlopen, dlopen); -void *_dlsym(void *vhandle, const char *name) +void *dlsym(void *vhandle, const char *name) { struct elf_resolve *tpnt, *tfrom; struct dyn_elf *handle; @@ -409,7 +391,6 @@ void *_dlsym(void *vhandle, const char *name) _dl_error_number = LD_NO_SYMBOL; return ret; } -weak_alias(_dlsym, dlsym); static int do_dlclose(void *vhandle, int need_fini) { @@ -535,13 +516,12 @@ static int do_dlclose(void *vhandle, int need_fini) return 0; } -int _dlclose(void *vhandle) +int dlclose(void *vhandle) { return do_dlclose(vhandle, 1); } -weak_alias(_dlclose, dlclose); -const char *_dlerror(void) +const char *dlerror(void) { const char *retval; @@ -551,22 +531,21 @@ const char *_dlerror(void) _dl_error_number = 0; return retval; } -weak_alias(_dlerror, dlerror); /* * Dump information to stderrr about the current loaded modules */ static char *type[] = { "Lib", "Exe", "Int", "Mod" }; -void _dlinfo(void) +void dlinfo(void) { struct elf_resolve *tpnt; struct dyn_elf *rpnt, *hpnt; - _dl_dprintf(2, "List of loaded modules\n"); + fprintf(stderr, "List of loaded modules\n"); /* First start with a complete list of all of the loaded files. */ for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) { - _dl_dprintf(2, "\t%x %x %x %s %d %s\n", + fprintf(stderr, "\t%x %x %x %s %d %s\n", (unsigned) tpnt->loadaddr, (unsigned) tpnt, (unsigned) tpnt->symbol_scope, type[tpnt->libtype], @@ -574,21 +553,20 @@ void _dlinfo(void) } /* Next dump the module list for the application itself */ - _dl_dprintf(2, "\nModules for application (%x):\n", + fprintf(stderr, "\nModules for application (%x):\n", (unsigned) _dl_symbol_tables); for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) - _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname); + fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname); for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) { - _dl_dprintf(2, "Modules for handle %x\n", (unsigned) hpnt); + fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt); for (rpnt = hpnt; rpnt; rpnt = rpnt->next) - _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, + fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname); } } -weak_alias(_dlinfo, dlinfo); -int _dladdr(void *__address, Dl_info * __dlip) +int dladdr(void *__address, Dl_info * __dlip) { struct elf_resolve *pelf; struct elf_resolve *rpnt; @@ -601,7 +579,7 @@ int _dladdr(void *__address, Dl_info * __dlip) pelf = NULL; #if 0 - _dl_dprintf(2, "dladdr( %x, %x )\n", __address, __dlip); + fprintf(stderr, "dladdr( %x, %x )\n", __address, __dlip); #endif for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) { @@ -609,7 +587,7 @@ int _dladdr(void *__address, Dl_info * __dlip) tpnt = rpnt; #if 0 - _dl_dprintf(2, "Module \"%s\" at %x\n", + fprintf(stderr, "Module \"%s\" at %x\n", tpnt->libname, tpnt->loadaddr); #endif if (tpnt->loadaddr < (ElfW(Addr)) __address @@ -650,7 +628,7 @@ int _dladdr(void *__address, Dl_info * __dlip) sf = 1; } #if 0 - _dl_dprintf(2, "Symbol \"%s\" at %x\n", + fprintf(stderr, "Symbol \"%s\" at %x\n", strtab + symtab[si].st_name, symbol_addr); #endif } @@ -665,4 +643,3 @@ int _dladdr(void *__address, Dl_info * __dlip) return 1; } } -weak_alias(_dladdr, dladdr); -- cgit v1.2.3