From 827c5f1b826b916a533c082e558fc7c6fc38de6d Mon Sep 17 00:00:00 2001 From: Carmelo Amoroso Date: Tue, 18 Nov 2008 12:48:13 +0000 Subject: Move _dl_iterate_phdr into libc.so.0 and libc.a (as glibc does). Currently we have an implementation in ld.so and libdl.a. Signed-off-by: Carmelo Amoroso --- libc/misc/Makefile.in | 1 + libc/misc/elf/Makefile | 12 ++++++ libc/misc/elf/Makefile.in | 20 +++++++++ libc/misc/elf/dl-core.c | 20 +++++++++ libc/misc/elf/dl-iterate-phdr.c | 84 +++++++++++++++++++++++++++++++++++++ libc/misc/elf/dl-support.c | 38 +++++++++++++++++ libc/misc/internals/__uClibc_main.c | 16 +++++-- 7 files changed, 188 insertions(+), 3 deletions(-) create mode 100644 libc/misc/elf/Makefile create mode 100644 libc/misc/elf/Makefile.in create mode 100644 libc/misc/elf/dl-core.c create mode 100644 libc/misc/elf/dl-iterate-phdr.c create mode 100644 libc/misc/elf/dl-support.c (limited to 'libc/misc') diff --git a/libc/misc/Makefile.in b/libc/misc/Makefile.in index 104db366e..838081d66 100644 --- a/libc/misc/Makefile.in +++ b/libc/misc/Makefile.in @@ -12,6 +12,7 @@ include $(top_srcdir)libc/misc/assert/Makefile.in include $(top_srcdir)libc/misc/ctype/Makefile.in include $(top_srcdir)libc/misc/dirent/Makefile.in include $(top_srcdir)libc/misc/error/Makefile.in +include $(top_srcdir)libc/misc/elf/Makefile.in include $(top_srcdir)libc/misc/file/Makefile.in include $(top_srcdir)libc/misc/fnmatch/Makefile.in include $(top_srcdir)libc/misc/ftw/Makefile.in diff --git a/libc/misc/elf/Makefile b/libc/misc/elf/Makefile new file mode 100644 index 000000000..4bb6872a1 --- /dev/null +++ b/libc/misc/elf/Makefile @@ -0,0 +1,12 @@ +# Copyright (C) 2008 STMicroelectronics Ltd. +# Author: Carmelo Amoroso + +# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. +# + +top_srcdir=../../../ +top_builddir=../../../ +all: objs +include $(top_builddir)Rules.mak +include Makefile.in +include $(top_srcdir)Makerules diff --git a/libc/misc/elf/Makefile.in b/libc/misc/elf/Makefile.in new file mode 100644 index 000000000..603810d68 --- /dev/null +++ b/libc/misc/elf/Makefile.in @@ -0,0 +1,20 @@ +# Copyright (C) 2008 STMicroelectronics Ltd. +# Author: Carmelo Amoroso + +# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. +# + +libc_a_CSRC = dl-support.c dl-core.c dl-iterate-phdr.c +CFLAGS-dl-iterate-phdr.c=-D_GNU_SOURCE -I$(top_srcdir)ldso/ldso/$(TARGET_ARCH) -I$(top_srcdir)ldso/include +CFLAGS-dl-core.c=-I$(top_srcdir)ldso/ldso/$(TARGET_ARCH) -I$(top_srcdir)ldso/include + +MISC_ELF_OUT:=$(top_builddir)libc/misc/elf +MISC_ELF_OBJ:=$(patsubst %.c,$(MISC_ELF_OUT)/%.o,$(libc_a_CSRC) + +libc-static-y += $(MISC_ELF_OBJ) +libc-shared-y += $(MISC_ELF_OUT)/dl-iterate-phdr.oS + +objclean-y+= misc_elf_objclean + +misc_elf_objclean: + $(RM) $(MISC_ELF_OUT)/*.{o,os,oS} diff --git a/libc/misc/elf/dl-core.c b/libc/misc/elf/dl-core.c new file mode 100644 index 000000000..b32dcf828 --- /dev/null +++ b/libc/misc/elf/dl-core.c @@ -0,0 +1,20 @@ +/* + * This contains all symbols and functions to support + * dynamic linking into static libc. + + * Copyright (c) 2008 STMicroelectronics Ltd + * Author: Carmelo Amoroso + * + * Based on draft work by Peter S. Mazinger + * + * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. + * + */ + +#ifdef SHARED +#error "This file is not suitable for linking into dynamic libc" +#else +/* Include ldso symbols and functions used into static libc */ +#include "../../../ldso/ldso/dl-symbols.c" +#endif + diff --git a/libc/misc/elf/dl-iterate-phdr.c b/libc/misc/elf/dl-iterate-phdr.c new file mode 100644 index 000000000..a23ca700d --- /dev/null +++ b/libc/misc/elf/dl-iterate-phdr.c @@ -0,0 +1,84 @@ +/* Get loaded objects program headers. + Copyright (C) 2001,2002,2003,2004,2006,2007 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Jakub Jelinek , 2001. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + */ + + +#include +#include + +/* we want this in libc but nowhere else */ +#ifdef __USE_GNU + +extern __typeof(dl_iterate_phdr) __dl_iterate_phdr; + +hidden_proto(__dl_iterate_phdr) +int +__dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data) +{ + struct elf_resolve *l; + struct dl_phdr_info info; + int ret = 0; + + for (l = _dl_loaded_modules; l != NULL; l = l->next) { + info.dlpi_addr = l->loadaddr; + info.dlpi_name = l->libname; + info.dlpi_phdr = l->ppnt; + info.dlpi_phnum = l->n_phent; + ret = callback (&info, sizeof (struct dl_phdr_info), data); + if (ret) + break; + } + return ret; +} +hidden_def (__dl_iterate_phdr) + +# ifdef SHARED + +weak_alias(__dl_iterate_phdr, dl_iterate_phdr) + +# else + +/* dl-support.c defines these and initializes them early on. */ +extern ElfW(Phdr) *_dl_phdr; +extern size_t _dl_phnum; + +int +dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, + size_t size, void *data), void *data) +{ + if (_dl_phnum != 0) + { + /* This entry describes this statically-linked program itself. */ + struct dl_phdr_info info; + int ret; + info.dlpi_addr = 0; + info.dlpi_name = ""; + info.dlpi_phdr = _dl_phdr; + info.dlpi_phnum = _dl_phnum; + ret = (*callback) (&info, sizeof (struct dl_phdr_info), data); + if (ret) + return ret; + } + /* Then invoke callback on loaded modules, if any */ + return __dl_iterate_phdr (callback, data); +} + +# endif +#endif diff --git a/libc/misc/elf/dl-support.c b/libc/misc/elf/dl-support.c new file mode 100644 index 000000000..16de2ecb7 --- /dev/null +++ b/libc/misc/elf/dl-support.c @@ -0,0 +1,38 @@ +/* Support for dynamic linking code in static libc. + Copyright (C) 1996-2002, 2003, 2004, 2005 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +/* This file defines some things that for the dynamic linker are defined in + rtld.c and dl-sysdep.c in ways appropriate to bootstrap dynamic linking. */ + +#include +#include + +ElfW(Phdr) *_dl_phdr; +size_t _dl_phnum; + +void +internal_function +_dl_aux_init (ElfW(auxv_t) *av) +{ + /* Get the program headers base address from the aux vect */ + _dl_phdr = (ElfW(Phdr) *) av[AT_PHDR].a_un.a_val; + + /* Get the number of program headers from the aux vect */ + _dl_phnum = (size_t) av[AT_PHNUM].a_un.a_val; +} diff --git a/libc/misc/internals/__uClibc_main.c b/libc/misc/internals/__uClibc_main.c index f33ecf79f..9e194983f 100644 --- a/libc/misc/internals/__uClibc_main.c +++ b/libc/misc/internals/__uClibc_main.c @@ -72,6 +72,11 @@ uintptr_t __guard attribute_relro; # endif # endif +/* + * Needed to initialize _dl_phdr when statically linked + */ + +void internal_function _dl_aux_init (ElfW(auxv_t) *av); #endif /* !SHARED */ /* @@ -114,9 +119,8 @@ weak_alias (program_invocation_name, __progname_full) #endif /* - * Declare the __environ global variable and create a strong alias environ. - * Note: Apparently we must initialize __environ to ensure that the strong - * environ symbol is also included. + * Declare the __environ global variable and create a weak alias environ. + * This must be initialized; we cannot have a weak alias into bss. */ char **__environ = 0; weak_alias(__environ, environ) @@ -310,6 +314,12 @@ void __uClibc_main(int (*main)(int, char **, char **), int argc, } aux_dat += 2; } +#ifndef SHARED + /* Get the program headers (_dl_phdr) from the aux vector + It will be used into __libc_setup_tls. */ + + _dl_aux_init (auxvt); +#endif #endif /* We need to initialize uClibc. If we are dynamically linked this -- cgit v1.2.3