2001-02-03 16:25:00 +03:00
|
|
|
/* $NetBSD: rtld.c,v 1.44 2001/02/03 13:25:00 pk Exp $ */
|
1996-12-16 23:37:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 1996 John D. Polstra.
|
|
|
|
* Copyright 1996 Matt Thomas <matt@3am-software.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by John Polstra.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dynamic linker for ELF.
|
|
|
|
*
|
|
|
|
* John Polstra <jdp@polstra.com>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1997-03-21 08:39:42 +03:00
|
|
|
#include <sys/param.h>
|
1996-12-16 23:37:55 +03:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
1996-12-19 01:27:29 +03:00
|
|
|
#include <dlfcn.h>
|
1996-12-16 23:37:55 +03:00
|
|
|
#include "debug.h"
|
|
|
|
#include "rtld.h"
|
|
|
|
|
1999-06-15 19:54:28 +04:00
|
|
|
#if !defined(lint)
|
1997-03-21 08:39:42 +03:00
|
|
|
#include "sysident.h"
|
1999-06-15 19:54:28 +04:00
|
|
|
#endif
|
1997-03-21 08:39:42 +03:00
|
|
|
|
1999-11-07 03:21:12 +03:00
|
|
|
#define END_SYM "_end"
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
/*
|
|
|
|
* Debugging support.
|
|
|
|
*/
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
typedef void (*funcptr) __P((void));
|
1996-12-16 23:37:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Function declarations.
|
|
|
|
*/
|
2000-07-19 02:33:55 +04:00
|
|
|
static void _rtld_init __P((caddr_t, int));
|
1999-03-01 19:40:07 +03:00
|
|
|
static void _rtld_exit __P((void));
|
|
|
|
|
2000-07-26 06:07:34 +04:00
|
|
|
Elf_Addr _rtld __P((Elf_Addr *));
|
1999-03-01 19:40:07 +03:00
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Data declarations.
|
|
|
|
*/
|
1999-03-01 19:40:07 +03:00
|
|
|
static char *error_message; /* Message for dlopen(), or NULL */
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
struct r_debug _rtld_debug; /* for GDB; */
|
|
|
|
bool _rtld_trust; /* False for setuid and setgid programs */
|
|
|
|
Obj_Entry *_rtld_objlist; /* Head of linked list of shared objects */
|
|
|
|
Obj_Entry **_rtld_objtail; /* Link field of last object in list */
|
|
|
|
Obj_Entry *_rtld_objmain; /* The main program shared object */
|
|
|
|
Obj_Entry _rtld_objself; /* The dynamic linker shared object */
|
|
|
|
char _rtld_path[] = _PATH_RTLD;
|
1999-11-10 21:48:19 +03:00
|
|
|
unsigned long _rtld_curmark; /* Current mark value */
|
1999-11-07 03:21:12 +03:00
|
|
|
Elf_Sym _rtld_sym_zero; /* For resolving undefined weak refs. */
|
1999-04-18 23:40:06 +04:00
|
|
|
#ifdef VARPSZ
|
|
|
|
int _rtld_pagesz; /* Page size, as provided by kernel */
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-11-07 03:21:12 +03:00
|
|
|
Objlist _rtld_list_main = /* Objects loaded at program startup */
|
|
|
|
SIMPLEQ_HEAD_INITIALIZER(_rtld_list_main);
|
|
|
|
|
1999-08-01 23:47:07 +04:00
|
|
|
Search_Path *_rtld_default_paths;
|
1999-03-01 19:40:07 +03:00
|
|
|
Search_Path *_rtld_paths;
|
1999-12-13 12:09:34 +03:00
|
|
|
|
|
|
|
Library_Xform *_rtld_xforms;
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
/*
|
|
|
|
* Global declarations normally provided by crt0.
|
|
|
|
*/
|
1999-03-01 19:40:07 +03:00
|
|
|
char *__progname;
|
|
|
|
char **environ;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1998-07-15 15:26:28 +04:00
|
|
|
#ifdef OLD_GOT
|
|
|
|
extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
|
|
|
|
#else
|
|
|
|
extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
|
1999-03-01 19:40:07 +03:00
|
|
|
extern Elf_Dyn _DYNAMIC;
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
static void _rtld_call_fini_functions __P((Obj_Entry *));
|
|
|
|
static void _rtld_call_init_functions __P((Obj_Entry *));
|
|
|
|
static Obj_Entry *_rtld_dlcheck __P((void *));
|
1999-11-07 03:21:12 +03:00
|
|
|
static void _rtld_init_dag __P((Obj_Entry *));
|
|
|
|
static void _rtld_init_dag1 __P((Obj_Entry *, Obj_Entry *));
|
|
|
|
static void _rtld_objlist_remove __P((Objlist *, Obj_Entry *));
|
|
|
|
static void _rtld_unload_object __P((Obj_Entry *, bool));
|
|
|
|
static void _rtld_unref_dag __P((Obj_Entry *));
|
|
|
|
static Obj_Entry *_rtld_obj_from_addr __P((const void *));
|
1999-03-01 19:40:07 +03:00
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
static void
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_call_fini_functions(first)
|
|
|
|
Obj_Entry *first;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-02-27 13:35:16 +03:00
|
|
|
Obj_Entry *obj;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
for (obj = first; obj != NULL; obj = obj->next)
|
1999-02-27 13:35:16 +03:00
|
|
|
if (obj->fini != NULL)
|
|
|
|
(*obj->fini)();
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_call_init_functions(first)
|
|
|
|
Obj_Entry *first;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-02-27 13:35:16 +03:00
|
|
|
if (first != NULL) {
|
|
|
|
_rtld_call_init_functions(first->next);
|
|
|
|
if (first->init != NULL)
|
|
|
|
(*first->init)();
|
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
1999-02-27 13:35:16 +03:00
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
/*
|
|
|
|
* Initialize the dynamic linker. The argument is the address at which
|
|
|
|
* the dynamic linker has been mapped into memory. The primary task of
|
|
|
|
* this function is to relocate the dynamic linker.
|
|
|
|
*/
|
|
|
|
static void
|
2000-07-19 02:33:55 +04:00
|
|
|
_rtld_init(mapbase, pagesz)
|
1999-03-01 19:40:07 +03:00
|
|
|
caddr_t mapbase;
|
2000-07-19 02:33:55 +04:00
|
|
|
int pagesz;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
Obj_Entry objself;/* The dynamic linker shared object */
|
2000-07-15 01:59:58 +04:00
|
|
|
const Elf_Ehdr *hdr = (Elf_Ehdr *) mapbase;
|
1999-02-24 21:31:00 +03:00
|
|
|
#ifdef RTLD_RELOCATE_SELF
|
1999-02-27 13:35:16 +03:00
|
|
|
int dodebug = false;
|
1999-02-24 21:31:00 +03:00
|
|
|
#else
|
1999-02-27 13:35:16 +03:00
|
|
|
int dodebug = true;
|
1999-02-24 21:31:00 +03:00
|
|
|
#endif
|
2000-07-15 01:59:58 +04:00
|
|
|
int i;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-02-27 13:35:16 +03:00
|
|
|
memset(&objself, 0, sizeof objself);
|
|
|
|
|
|
|
|
/* Conjure up an Obj_Entry structure for the dynamic linker. */
|
|
|
|
objself.path = NULL;
|
|
|
|
objself.rtld = true;
|
|
|
|
objself.mapbase = mapbase;
|
2000-07-15 01:59:58 +04:00
|
|
|
objself.phdr = (Elf_Phdr *) (mapbase + hdr->e_phoff);
|
|
|
|
for (i = 0; i < hdr->e_phnum; i++) {
|
|
|
|
if (objself.phdr[i].p_type == PT_LOAD) {
|
2000-07-19 02:33:55 +04:00
|
|
|
#ifdef VARPSZ
|
|
|
|
/* We can't touch _rtld_pagesz yet so we can't use round_*() */
|
|
|
|
#define _rnd_down(x) ((x) & ~((long)pagesz-1))
|
|
|
|
#define _rnd_up(x) _rnd_down((x) + pagesz - 1)
|
|
|
|
objself.textsize = _rnd_up(objself.phdr[i].p_vaddr + objself.phdr[i].p_memsz) - _rnd_down(objself.phdr[i].p_vaddr);
|
|
|
|
#undef _rnd_down(x)
|
|
|
|
#undef _rnd_up(x)
|
|
|
|
#else
|
2000-07-15 01:59:58 +04:00
|
|
|
objself.textsize = round_up(objself.phdr[i].p_vaddr + objself.phdr[i].p_memsz) - round_down(objself.phdr[i].p_vaddr);
|
2000-07-19 02:33:55 +04:00
|
|
|
#endif
|
2000-07-15 01:59:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-02-27 13:35:16 +03:00
|
|
|
|
1999-02-24 21:31:00 +03:00
|
|
|
#if defined(__mips__)
|
1999-02-27 13:35:16 +03:00
|
|
|
/*
|
|
|
|
* mips and ld.so currently linked at load address,
|
|
|
|
* so no relocation needed
|
|
|
|
*/
|
|
|
|
objself.relocbase = 0;
|
1998-03-25 07:12:32 +03:00
|
|
|
#else
|
1999-02-27 13:35:16 +03:00
|
|
|
objself.relocbase = mapbase;
|
1998-03-25 07:12:32 +03:00
|
|
|
#endif
|
1999-02-27 13:35:16 +03:00
|
|
|
|
|
|
|
objself.pltgot = NULL;
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
#ifdef OLD_GOT
|
1999-03-01 19:40:07 +03:00
|
|
|
objself.dynamic = (Elf_Dyn *) _GLOBAL_OFFSET_TABLE_[0];
|
1996-12-16 23:37:55 +03:00
|
|
|
#else
|
1999-03-01 19:40:07 +03:00
|
|
|
objself.dynamic = (Elf_Dyn *) & _DYNAMIC;
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
|
|
|
|
1999-02-24 21:31:00 +03:00
|
|
|
#ifdef RTLD_RELOCATE_SELF
|
1999-02-27 13:35:16 +03:00
|
|
|
/* We have not been relocated yet, so fix the dynamic address */
|
|
|
|
objself.dynamic = (Elf_Dyn *)
|
1999-03-01 19:40:07 +03:00
|
|
|
((u_long) mapbase + (char *) objself.dynamic);
|
|
|
|
#endif /* RTLD_RELOCATE_SELF */
|
1999-02-24 21:31:00 +03:00
|
|
|
|
1999-02-27 13:35:16 +03:00
|
|
|
_rtld_digest_dynamic(&objself);
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
#ifdef __alpha__
|
1999-02-27 13:35:16 +03:00
|
|
|
/* XXX XXX XXX */
|
|
|
|
objself.pltgot = NULL;
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
1999-02-27 13:35:16 +03:00
|
|
|
assert(objself.needed == NULL);
|
|
|
|
|
2000-07-15 01:59:58 +04:00
|
|
|
#if !defined(__mips__) && !defined(__i386__) && !defined(__vax__)
|
1999-02-27 13:35:16 +03:00
|
|
|
/* no relocation for mips/i386 */
|
|
|
|
assert(!objself.textrel);
|
1998-03-25 07:12:32 +03:00
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-02-27 13:35:16 +03:00
|
|
|
_rtld_relocate_objects(&objself, true, dodebug);
|
1999-02-24 21:31:00 +03:00
|
|
|
|
1999-02-27 13:35:16 +03:00
|
|
|
/*
|
|
|
|
* Now that we relocated ourselves, we can use globals.
|
|
|
|
*/
|
|
|
|
_rtld_objself = objself;
|
1999-02-24 21:31:00 +03:00
|
|
|
|
1999-02-27 13:35:16 +03:00
|
|
|
_rtld_objself.path = _rtld_path;
|
1999-08-01 23:47:07 +04:00
|
|
|
_rtld_add_paths(&_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH, true);
|
1999-02-24 21:31:00 +03:00
|
|
|
|
1999-02-27 13:35:16 +03:00
|
|
|
/*
|
|
|
|
* Set up the _rtld_objlist pointer, so that rtld symbols can be found.
|
|
|
|
*/
|
|
|
|
_rtld_objlist = &_rtld_objself;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-02-27 13:35:16 +03:00
|
|
|
/* Make the object list empty again. */
|
|
|
|
_rtld_objlist = NULL;
|
|
|
|
_rtld_objtail = &_rtld_objlist;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-02-27 13:35:16 +03:00
|
|
|
_rtld_debug.r_brk = _rtld_debug_state;
|
|
|
|
_rtld_debug.r_state = RT_CONSISTENT;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
1999-02-27 13:35:16 +03:00
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
/*
|
|
|
|
* Cleanup procedure. It will be called (by the atexit() mechanism) just
|
|
|
|
* before the process exits.
|
|
|
|
*/
|
|
|
|
static void
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_exit()
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
dbg(("rtld_exit()"));
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-02-27 13:35:16 +03:00
|
|
|
_rtld_call_fini_functions(_rtld_objlist->next);
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
1999-02-27 13:35:16 +03:00
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
/*
|
|
|
|
* Main entry point for dynamic linking. The argument is the stack
|
|
|
|
* pointer. The stack is expected to be laid out as described in the
|
|
|
|
* SVR4 ABI specification, Intel 386 Processor Supplement. Specifically,
|
|
|
|
* the stack pointer points to a word containing ARGC. Following that
|
|
|
|
* in the stack is a null-terminated sequence of pointers to argument
|
|
|
|
* strings. Then comes a null-terminated sequence of pointers to
|
|
|
|
* environment strings. Finally, there is a sequence of "auxiliary
|
|
|
|
* vector" entries.
|
|
|
|
*
|
1999-03-08 13:44:25 +03:00
|
|
|
* This function returns the entry point for the main program, the dynamic
|
|
|
|
* linker's exit procedure in sp[0], and a pointer to the main object in
|
|
|
|
* sp[1].
|
1996-12-16 23:37:55 +03:00
|
|
|
*/
|
|
|
|
Elf_Addr
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld(sp)
|
2000-07-26 06:07:34 +04:00
|
|
|
Elf_Addr *sp;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
const AuxInfo *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
|
|
|
|
*pAUX_phent, *pAUX_phnum;
|
1999-04-18 23:40:06 +04:00
|
|
|
#ifdef VARPSZ
|
|
|
|
const AuxInfo *pAUX_pagesz;
|
|
|
|
#endif
|
1999-03-01 19:40:07 +03:00
|
|
|
char **env;
|
|
|
|
const AuxInfo *aux;
|
|
|
|
const AuxInfo *auxp;
|
2000-07-26 06:07:34 +04:00
|
|
|
Elf_Addr *const osp = sp;
|
1999-03-01 19:40:07 +03:00
|
|
|
bool bind_now = 0;
|
|
|
|
const char *ld_bind_now;
|
|
|
|
const char **argv;
|
2000-07-26 19:40:07 +04:00
|
|
|
long argc;
|
1999-11-07 03:21:12 +03:00
|
|
|
Obj_Entry *obj;
|
2000-04-15 09:41:46 +04:00
|
|
|
const char **real___progname;
|
2000-06-16 23:51:05 +04:00
|
|
|
const Obj_Entry **real___mainprog_obj;
|
2000-04-15 09:41:46 +04:00
|
|
|
char ***real_environ;
|
1999-02-24 21:31:00 +03:00
|
|
|
#if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
|
1999-03-01 19:40:07 +03:00
|
|
|
int i = 0;
|
1999-01-10 21:18:56 +03:00
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
/*
|
|
|
|
* On entry, the dynamic linker itself has not been relocated yet.
|
|
|
|
* Be very careful not to reference any global data until after
|
|
|
|
* _rtld_init has returned. It is OK to reference file-scope statics
|
|
|
|
* and string constants, and to call static and global functions.
|
|
|
|
*/
|
|
|
|
/* Find the auxiliary vector on the stack. */
|
|
|
|
/* first Elf_Word reserved to address of exit routine */
|
1999-02-24 21:31:00 +03:00
|
|
|
#if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
|
1999-03-04 00:18:00 +03:00
|
|
|
dbg(("sp = %p, argc = %ld, argv = %p <%s>\n", sp, (long)sp[2],
|
1999-03-01 19:40:07 +03:00
|
|
|
&sp[3], (char *) sp[3]));
|
|
|
|
dbg(("got is at %p, dynamic is at %p\n",
|
|
|
|
_GLOBAL_OFFSET_TABLE_, &_DYNAMIC));
|
|
|
|
debug = 1;
|
|
|
|
dbg(("_ctype_ is %p\n", _ctype_));
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
sp += 2; /* skip over return argument space */
|
|
|
|
argv = (const char **) &sp[1];
|
2000-07-26 19:40:07 +04:00
|
|
|
argc = *(long *)sp;
|
|
|
|
#ifdef __sparc_v9__
|
|
|
|
/* XXX Temporary hack for argc format conversion. */
|
|
|
|
argc = (argc >> 32) | (argc & 0xffffffff);
|
|
|
|
#endif
|
2000-07-26 06:07:34 +04:00
|
|
|
sp += 2 + argc; /* Skip over argc, arguments, and NULL
|
1999-03-01 19:40:07 +03:00
|
|
|
* terminator */
|
|
|
|
env = (char **) sp;
|
|
|
|
while (*sp++ != 0) { /* Skip over environment, and NULL terminator */
|
1999-02-24 21:31:00 +03:00
|
|
|
#if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
|
1999-03-01 19:40:07 +03:00
|
|
|
dbg(("env[%d] = %p %s\n", i++, (void *)sp[-1], (char *)sp[-1]));
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
1998-10-07 06:53:27 +04:00
|
|
|
}
|
1999-03-01 19:40:07 +03:00
|
|
|
aux = (const AuxInfo *) sp;
|
|
|
|
|
|
|
|
pAUX_base = pAUX_entry = pAUX_execfd = NULL;
|
|
|
|
pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
|
1999-04-18 23:40:06 +04:00
|
|
|
#ifdef VARPSZ
|
|
|
|
pAUX_pagesz = NULL;
|
|
|
|
#endif
|
2001-02-03 16:25:00 +03:00
|
|
|
/*
|
|
|
|
* First pass through the the auxiliary vector, avoiding the use
|
|
|
|
* of a `switch() {}' statement at this stage. A `switch()' may
|
|
|
|
* be translated into code utilizing a jump table approach which
|
|
|
|
* references the equivalent of a global variable. This must be
|
|
|
|
* avoided until _rtld_init() has done its job.
|
|
|
|
*
|
|
|
|
* _rtld_init() only needs `pAUX_base' and possibly `pAUX_pagesz',
|
|
|
|
* so we look for just those in this pass.
|
|
|
|
*/
|
|
|
|
for (auxp = aux; auxp->a_type != AT_NULL; ++auxp) {
|
|
|
|
if (auxp->a_type == AT_BASE)
|
|
|
|
pAUX_base = auxp;
|
|
|
|
#ifdef VARPSZ
|
|
|
|
if (auxp->a_type == AT_PAGESZ)
|
|
|
|
pAUX_pagesz = auxp;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize and relocate ourselves. */
|
|
|
|
assert(pAUX_base != NULL);
|
|
|
|
#ifdef VARPSZ
|
|
|
|
assert(pAUX_pagesz != NULL);
|
|
|
|
_rtld_init((caddr_t) pAUX_base->a_v, (int)pAUX_pagesz->a_v);
|
|
|
|
#else
|
|
|
|
_rtld_init((caddr_t) pAUX_base->a_v, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Digest the auxiliary vector (full pass now that we can afford it). */
|
1999-10-25 17:55:06 +04:00
|
|
|
for (auxp = aux; auxp->a_type != AT_NULL; ++auxp) {
|
|
|
|
switch (auxp->a_type) {
|
|
|
|
case AT_BASE:
|
1999-03-01 19:40:07 +03:00
|
|
|
pAUX_base = auxp;
|
|
|
|
break;
|
1999-10-25 17:55:06 +04:00
|
|
|
case AT_ENTRY:
|
1999-03-01 19:40:07 +03:00
|
|
|
pAUX_entry = auxp;
|
|
|
|
break;
|
1999-10-25 17:55:06 +04:00
|
|
|
case AT_EXECFD:
|
1999-03-01 19:40:07 +03:00
|
|
|
pAUX_execfd = auxp;
|
|
|
|
break;
|
1999-10-25 17:55:06 +04:00
|
|
|
case AT_PHDR:
|
1999-03-01 19:40:07 +03:00
|
|
|
pAUX_phdr = auxp;
|
|
|
|
break;
|
1999-10-25 17:55:06 +04:00
|
|
|
case AT_PHENT:
|
1999-03-01 19:40:07 +03:00
|
|
|
pAUX_phent = auxp;
|
|
|
|
break;
|
1999-10-25 17:55:06 +04:00
|
|
|
case AT_PHNUM:
|
1999-03-01 19:40:07 +03:00
|
|
|
pAUX_phnum = auxp;
|
|
|
|
break;
|
1999-04-18 23:40:06 +04:00
|
|
|
#ifdef VARPSZ
|
1999-10-25 17:55:06 +04:00
|
|
|
case AT_PAGESZ:
|
1999-04-18 23:40:06 +04:00
|
|
|
pAUX_pagesz = auxp;
|
|
|
|
break;
|
|
|
|
#endif
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
|
2000-07-19 19:01:16 +04:00
|
|
|
#ifdef VARPSZ
|
1999-10-25 17:55:06 +04:00
|
|
|
_rtld_pagesz = (int)pAUX_pagesz->a_v;
|
1999-04-18 23:40:06 +04:00
|
|
|
#endif
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
#ifdef RTLD_DEBUG
|
1999-03-01 19:40:07 +03:00
|
|
|
dbg(("_ctype_ is %p\n", _ctype_));
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
__progname = _rtld_objself.path;
|
|
|
|
environ = env;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_trust = geteuid() == getuid() && getegid() == getgid();
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
ld_bind_now = getenv("LD_BIND_NOW");
|
|
|
|
if (ld_bind_now != NULL && *ld_bind_now != '\0')
|
|
|
|
bind_now = true;
|
|
|
|
if (_rtld_trust) {
|
1998-03-25 07:12:32 +03:00
|
|
|
#ifdef DEBUG
|
1999-03-01 19:40:07 +03:00
|
|
|
const char *ld_debug = getenv("LD_DEBUG");
|
|
|
|
if (ld_debug != NULL && *ld_debug != '\0')
|
|
|
|
debug = 1;
|
1998-03-25 07:12:32 +03:00
|
|
|
#endif
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_add_paths(&_rtld_paths, getenv("LD_LIBRARY_PATH"), true);
|
|
|
|
}
|
1999-12-13 12:09:34 +03:00
|
|
|
_rtld_process_hints(&_rtld_paths, &_rtld_xforms, _PATH_LD_HINTS, true);
|
1999-03-01 19:40:07 +03:00
|
|
|
dbg(("%s is initialized, base address = %p", __progname,
|
1999-10-25 17:55:06 +04:00
|
|
|
(void *) pAUX_base->a_v));
|
1999-03-01 19:40:07 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Load the main program, or process its program header if it is
|
|
|
|
* already loaded.
|
|
|
|
*/
|
|
|
|
if (pAUX_execfd != NULL) { /* Load the main program. */
|
1999-10-25 17:55:06 +04:00
|
|
|
int fd = pAUX_execfd->a_v;
|
1999-03-01 19:40:07 +03:00
|
|
|
dbg(("loading main program"));
|
1999-11-07 03:21:12 +03:00
|
|
|
_rtld_objmain = _rtld_map_object(argv[0], fd, NULL);
|
1999-03-01 19:40:07 +03:00
|
|
|
close(fd);
|
|
|
|
if (_rtld_objmain == NULL)
|
|
|
|
_rtld_die();
|
|
|
|
} else { /* Main program already loaded. */
|
|
|
|
const Elf_Phdr *phdr;
|
|
|
|
int phnum;
|
|
|
|
caddr_t entry;
|
|
|
|
|
|
|
|
dbg(("processing main program's program header"));
|
|
|
|
assert(pAUX_phdr != NULL);
|
1999-10-25 17:55:06 +04:00
|
|
|
phdr = (const Elf_Phdr *) pAUX_phdr->a_v;
|
1999-03-01 19:40:07 +03:00
|
|
|
assert(pAUX_phnum != NULL);
|
1999-10-25 17:55:06 +04:00
|
|
|
phnum = pAUX_phnum->a_v;
|
1999-03-01 19:40:07 +03:00
|
|
|
assert(pAUX_phent != NULL);
|
1999-10-25 17:55:06 +04:00
|
|
|
assert(pAUX_phent->a_v == sizeof(Elf_Phdr));
|
1999-03-01 19:40:07 +03:00
|
|
|
assert(pAUX_entry != NULL);
|
1999-10-25 17:55:06 +04:00
|
|
|
entry = (caddr_t) pAUX_entry->a_v;
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
|
|
|
|
}
|
|
|
|
|
2000-08-28 08:15:54 +04:00
|
|
|
if (argv[0] != NULL)
|
|
|
|
_rtld_objmain->path = xstrdup(argv[0]);
|
|
|
|
else
|
|
|
|
_rtld_objmain->path = xstrdup("main program");
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_objmain->mainprog = true;
|
1999-11-07 03:21:12 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the actual dynamic linker pathname from the executable if
|
|
|
|
* possible. (It should always be possible.) That ensures that
|
|
|
|
* gdb will find the right dynamic linker even if a non-standard
|
|
|
|
* one is being used.
|
|
|
|
*/
|
|
|
|
if (_rtld_objmain->interp != NULL &&
|
|
|
|
strcmp(_rtld_objmain->interp, _rtld_objself.path) != 0) {
|
|
|
|
free(_rtld_objself.path);
|
|
|
|
_rtld_objself.path = xstrdup(_rtld_objmain->interp);
|
|
|
|
}
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_digest_dynamic(_rtld_objmain);
|
|
|
|
|
|
|
|
_rtld_linkmap_add(_rtld_objmain);
|
|
|
|
_rtld_linkmap_add(&_rtld_objself);
|
|
|
|
|
|
|
|
/* Link the main program into the list of objects. */
|
|
|
|
*_rtld_objtail = _rtld_objmain;
|
|
|
|
_rtld_objtail = &_rtld_objmain->next;
|
|
|
|
++_rtld_objmain->refcount;
|
|
|
|
|
1999-11-07 03:21:12 +03:00
|
|
|
/* Initialize a fake symbol for resolving undefined weak references. */
|
1999-11-10 21:34:49 +03:00
|
|
|
_rtld_sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
|
1999-11-07 03:21:12 +03:00
|
|
|
_rtld_sym_zero.st_shndx = SHN_ABS;
|
|
|
|
|
1999-05-31 18:48:16 +04:00
|
|
|
/*
|
|
|
|
* Pre-load user-specified objects after the main program but before
|
|
|
|
* any shared object dependencies.
|
|
|
|
*/
|
|
|
|
dbg(("preloading objects"));
|
|
|
|
if (_rtld_trust && _rtld_preload(getenv("LD_PRELOAD"), true) == -1)
|
|
|
|
_rtld_die();
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
dbg(("loading needed objects"));
|
2000-11-11 00:31:30 +03:00
|
|
|
if (_rtld_load_needed_objects(_rtld_objmain, RTLD_GLOBAL, true) == -1)
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_die();
|
|
|
|
|
1999-11-07 03:21:12 +03:00
|
|
|
for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
|
|
|
|
_rtld_objlist_add(&_rtld_list_main, obj);
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
dbg(("relocating objects"));
|
|
|
|
if (_rtld_relocate_objects(_rtld_objmain, bind_now, true) == -1)
|
|
|
|
_rtld_die();
|
|
|
|
|
|
|
|
dbg(("doing copy relocations"));
|
|
|
|
if (_rtld_do_copy_relocations(_rtld_objmain, true) == -1)
|
|
|
|
_rtld_die();
|
|
|
|
|
2000-04-15 09:41:46 +04:00
|
|
|
/*
|
2000-06-16 23:51:05 +04:00
|
|
|
* Set the __progname, environ and, __mainprog_obj before
|
|
|
|
* calling anything that might use them.
|
2000-04-15 09:41:46 +04:00
|
|
|
*/
|
|
|
|
real___progname = _rtld_objmain_sym("__progname");
|
|
|
|
if (real___progname) {
|
|
|
|
if ((*real___progname = strrchr(argv[0], '/')) == NULL)
|
|
|
|
(*real___progname) = argv[0];
|
|
|
|
else
|
|
|
|
(*real___progname)++;
|
|
|
|
}
|
|
|
|
real_environ = _rtld_objmain_sym("environ");
|
|
|
|
if (real_environ)
|
|
|
|
*real_environ = environ;
|
2000-06-16 23:51:05 +04:00
|
|
|
real___mainprog_obj = _rtld_objmain_sym("__mainprog_obj");
|
|
|
|
if (real___mainprog_obj)
|
|
|
|
*real___mainprog_obj = _rtld_objmain;
|
2000-04-15 09:41:46 +04:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
dbg(("calling _init functions"));
|
|
|
|
_rtld_call_init_functions(_rtld_objmain->next);
|
|
|
|
|
|
|
|
dbg(("control at program entry point = %p, obj = %p, exit = %p",
|
|
|
|
_rtld_objmain->entry, _rtld_objmain, _rtld_exit));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return with the entry point and the exit procedure in at the top
|
|
|
|
* of stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
_rtld_debug_state(); /* say hello to gdb! */
|
|
|
|
|
|
|
|
((void **) osp)[0] = _rtld_exit;
|
|
|
|
((void **) osp)[1] = _rtld_objmain;
|
|
|
|
return (Elf_Addr) _rtld_objmain->entry;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_die()
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
const char *msg = _rtld_dlerror();
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
if (msg == NULL)
|
|
|
|
msg = "Fatal error";
|
|
|
|
xerrx(1, "%s\n", msg);
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static Obj_Entry *
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_dlcheck(handle)
|
|
|
|
void *handle;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
Obj_Entry *obj;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
|
|
|
|
if (obj == (Obj_Entry *) handle)
|
|
|
|
break;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
if (obj == NULL || obj->dl_refcount == 0) {
|
|
|
|
xwarnx("Invalid shared object handle %p", handle);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return obj;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-11-07 03:21:12 +03:00
|
|
|
_rtld_init_dag(root)
|
|
|
|
Obj_Entry *root;
|
|
|
|
{
|
1999-11-10 21:48:19 +03:00
|
|
|
_rtld_curmark++;
|
1999-11-07 03:21:12 +03:00
|
|
|
_rtld_init_dag1(root, root);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_rtld_init_dag1(root, obj)
|
|
|
|
Obj_Entry *root;
|
|
|
|
Obj_Entry *obj;
|
|
|
|
{
|
|
|
|
const Needed_Entry *needed;
|
|
|
|
|
1999-11-10 21:48:19 +03:00
|
|
|
if (obj->mark == _rtld_curmark)
|
1999-11-07 03:21:12 +03:00
|
|
|
return;
|
1999-11-10 21:48:19 +03:00
|
|
|
obj->mark = _rtld_curmark;
|
1999-11-07 03:21:12 +03:00
|
|
|
_rtld_objlist_add(&obj->dldags, root);
|
|
|
|
_rtld_objlist_add(&root->dagmembers, obj);
|
|
|
|
for (needed = obj->needed; needed != NULL; needed = needed->next)
|
|
|
|
if (needed->obj != NULL)
|
|
|
|
_rtld_init_dag1(root, needed->obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note, this is called only for objects loaded by dlopen().
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_rtld_unload_object(root, do_fini_funcs)
|
|
|
|
Obj_Entry *root;
|
|
|
|
bool do_fini_funcs;
|
|
|
|
{
|
|
|
|
_rtld_unref_dag(root);
|
|
|
|
if (root->refcount == 0) { /* We are finished with some objects. */
|
|
|
|
Obj_Entry *obj;
|
|
|
|
Obj_Entry **linkp;
|
|
|
|
Objlist_Entry *elm;
|
|
|
|
|
|
|
|
/* Finalize objects that are about to be unmapped. */
|
|
|
|
if (do_fini_funcs)
|
|
|
|
for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
|
|
|
|
if (obj->refcount == 0 && obj->fini != NULL)
|
|
|
|
(*obj->fini)();
|
|
|
|
|
|
|
|
/* Remove the DAG from all objects' DAG lists. */
|
|
|
|
for (elm = SIMPLEQ_FIRST(&root->dagmembers); elm; elm = SIMPLEQ_NEXT(elm, link))
|
|
|
|
_rtld_objlist_remove(&elm->obj->dldags, root);
|
|
|
|
|
|
|
|
/* Remove the DAG from the RTLD_GLOBAL list. */
|
|
|
|
_rtld_objlist_remove(&_rtld_list_global, root);
|
|
|
|
|
|
|
|
/* Unmap all objects that are no longer referenced. */
|
|
|
|
linkp = &_rtld_objlist->next;
|
|
|
|
while ((obj = *linkp) != NULL) {
|
|
|
|
if (obj->refcount == 0) {
|
|
|
|
#ifdef RTLD_DEBUG
|
|
|
|
dbg(("unloading \"%s\"", obj->path));
|
|
|
|
#endif
|
|
|
|
munmap(obj->mapbase, obj->mapsize);
|
|
|
|
_rtld_linkmap_delete(obj);
|
|
|
|
*linkp = obj->next;
|
|
|
|
_rtld_obj_free(obj);
|
|
|
|
} else
|
|
|
|
linkp = &obj->next;
|
|
|
|
}
|
|
|
|
_rtld_objtail = linkp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_rtld_unref_dag(root)
|
1999-03-01 19:40:07 +03:00
|
|
|
Obj_Entry *root;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
2000-06-03 02:52:28 +04:00
|
|
|
assert(root);
|
2000-06-03 10:51:57 +04:00
|
|
|
assert(root->refcount != 0);
|
1999-03-01 19:40:07 +03:00
|
|
|
--root->refcount;
|
|
|
|
if (root->refcount == 0) {
|
|
|
|
const Needed_Entry *needed;
|
|
|
|
|
|
|
|
for (needed = root->needed; needed != NULL;
|
2000-06-03 02:52:28 +04:00
|
|
|
needed = needed->next) {
|
|
|
|
if (needed->obj != NULL)
|
|
|
|
_rtld_unref_dag(needed->obj);
|
|
|
|
}
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_dlclose(handle)
|
|
|
|
void *handle;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
Obj_Entry *root = _rtld_dlcheck(handle);
|
|
|
|
|
|
|
|
if (root == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
_rtld_debug.r_state = RT_DELETE;
|
|
|
|
_rtld_debug_state();
|
|
|
|
|
|
|
|
--root->dl_refcount;
|
2000-02-07 22:02:49 +03:00
|
|
|
_rtld_unload_object(root, true);
|
1999-03-01 19:40:07 +03:00
|
|
|
|
|
|
|
_rtld_debug.r_state = RT_CONSISTENT;
|
|
|
|
_rtld_debug_state();
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
return 0;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_dlerror()
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
char *msg = error_message;
|
|
|
|
error_message = NULL;
|
|
|
|
return msg;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_dlopen(name, mode)
|
|
|
|
const char *name;
|
|
|
|
int mode;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
Obj_Entry **old_obj_tail = _rtld_objtail;
|
|
|
|
Obj_Entry *obj = NULL;
|
|
|
|
|
|
|
|
_rtld_debug.r_state = RT_ADD;
|
|
|
|
_rtld_debug_state();
|
|
|
|
|
|
|
|
if (name == NULL) {
|
|
|
|
obj = _rtld_objmain;
|
1999-11-07 03:21:12 +03:00
|
|
|
obj->refcount++;
|
1999-03-01 19:40:07 +03:00
|
|
|
} else {
|
1999-07-22 04:18:19 +04:00
|
|
|
char *path = _rtld_find_library(name, _rtld_objmain);
|
1999-03-01 19:40:07 +03:00
|
|
|
if (path != NULL)
|
2000-11-11 00:31:30 +03:00
|
|
|
obj = _rtld_load_object(path, mode, true);
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
if (obj != NULL) {
|
|
|
|
++obj->dl_refcount;
|
|
|
|
if (*old_obj_tail != NULL) { /* We loaded something new. */
|
|
|
|
assert(*old_obj_tail == obj);
|
|
|
|
|
2000-11-11 00:31:30 +03:00
|
|
|
if (_rtld_load_needed_objects(obj, mode, true) == -1 ||
|
1999-12-13 12:09:34 +03:00
|
|
|
(_rtld_init_dag(obj),
|
|
|
|
_rtld_relocate_objects(obj,
|
|
|
|
((mode & 3) == RTLD_NOW), true)) == -1) {
|
1999-11-07 03:21:12 +03:00
|
|
|
_rtld_unload_object(obj, false);
|
|
|
|
obj->dl_refcount--;
|
1999-03-01 19:40:07 +03:00
|
|
|
obj = NULL;
|
1999-11-07 03:21:12 +03:00
|
|
|
} else
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_call_init_functions(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_rtld_debug.r_state = RT_CONSISTENT;
|
|
|
|
_rtld_debug_state();
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
return obj;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
2000-04-15 09:41:46 +04:00
|
|
|
/*
|
|
|
|
* Find a symbol in the main program.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
_rtld_objmain_sym(name)
|
|
|
|
const char *name;
|
|
|
|
{
|
|
|
|
unsigned long hash;
|
|
|
|
const Elf_Sym *def;
|
|
|
|
const Obj_Entry *obj;
|
|
|
|
|
|
|
|
hash = _rtld_elf_hash(name);
|
|
|
|
obj = _rtld_objmain;
|
|
|
|
|
|
|
|
_rtld_curmark++;
|
|
|
|
def = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, true);
|
|
|
|
|
|
|
|
if (def != NULL)
|
|
|
|
return obj->relocbase + def->st_value;
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
void *
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_dlsym(handle, name)
|
|
|
|
void *handle;
|
|
|
|
const char *name;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-11-07 03:21:12 +03:00
|
|
|
const Obj_Entry *obj;
|
|
|
|
unsigned long hash;
|
|
|
|
const Elf_Sym *def;
|
1999-03-01 19:40:07 +03:00
|
|
|
const Obj_Entry *defobj;
|
1999-11-07 03:21:12 +03:00
|
|
|
|
|
|
|
hash = _rtld_elf_hash(name);
|
|
|
|
def = NULL;
|
|
|
|
defobj = NULL;
|
|
|
|
|
2000-04-15 09:27:49 +04:00
|
|
|
if (handle == NULL
|
|
|
|
#if 0
|
|
|
|
|| handle == RTLD_NEXT
|
1999-11-07 03:21:12 +03:00
|
|
|
#endif
|
2000-04-15 09:27:49 +04:00
|
|
|
) {
|
|
|
|
void *retaddr;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
2000-04-15 09:27:49 +04:00
|
|
|
retaddr = __builtin_return_address(0); /* __GNUC__ only */
|
|
|
|
if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
|
|
|
|
_rtld_error("Cannot determine caller's shared object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (handle == NULL) { /* Just the caller's shared object. */
|
|
|
|
def = _rtld_symlook_obj(name, hash, obj, true);
|
|
|
|
defobj = obj;
|
|
|
|
} else { /* All the shared objects after the caller's */
|
|
|
|
while ((obj = obj->next) != NULL) {
|
|
|
|
if ((def = _rtld_symlook_obj(name, hash, obj, true)) != NULL) {
|
|
|
|
defobj = obj;
|
|
|
|
break;
|
1999-11-07 03:21:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-04-15 09:27:49 +04:00
|
|
|
} else {
|
|
|
|
if ((obj = _rtld_dlcheck(handle)) == NULL)
|
|
|
|
return NULL;
|
1999-11-07 03:21:12 +03:00
|
|
|
|
2000-04-15 09:27:49 +04:00
|
|
|
if (obj->mainprog) {
|
|
|
|
/* Search main program and all libraries loaded by it. */
|
|
|
|
_rtld_curmark++;
|
|
|
|
def = _rtld_symlook_list(name, hash, &_rtld_list_main, &defobj, true);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* XXX - This isn't correct. The search should include the whole
|
|
|
|
* DAG rooted at the given object.
|
|
|
|
*/
|
|
|
|
def = _rtld_symlook_obj(name, hash, obj, true);
|
|
|
|
defobj = obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (def != NULL)
|
|
|
|
return defobj->relocbase + def->st_value;
|
|
|
|
|
|
|
|
_rtld_error("Undefined symbol \"%s\"", name);
|
1999-11-07 03:21:12 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-11-07 03:21:12 +03:00
|
|
|
int
|
|
|
|
_rtld_dladdr(addr, info)
|
|
|
|
const void *addr;
|
|
|
|
Dl_info *info;
|
|
|
|
{
|
|
|
|
const Obj_Entry *obj;
|
|
|
|
const Elf_Sym *def;
|
|
|
|
void *symbol_addr;
|
|
|
|
unsigned long symoffset;
|
|
|
|
|
|
|
|
obj = _rtld_obj_from_addr(addr);
|
|
|
|
if (obj == NULL) {
|
|
|
|
_rtld_error("No shared object contains address");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
info->dli_fname = obj->path;
|
|
|
|
info->dli_fbase = obj->mapbase;
|
|
|
|
info->dli_saddr = (void *)0;
|
|
|
|
info->dli_sname = NULL;
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
/*
|
1999-11-07 03:21:12 +03:00
|
|
|
* Walk the symbol list looking for the symbol whose address is
|
|
|
|
* closest to the address sent in.
|
|
|
|
*/
|
|
|
|
for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
|
|
|
|
def = obj->symtab + symoffset;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For skip the symbol if st_shndx is either SHN_UNDEF or
|
|
|
|
* SHN_COMMON.
|
|
|
|
*/
|
|
|
|
if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the symbol is greater than the specified address, or if it
|
|
|
|
* is further away from addr than the current nearest symbol,
|
|
|
|
* then reject it.
|
|
|
|
*/
|
|
|
|
symbol_addr = obj->relocbase + def->st_value;
|
|
|
|
if (symbol_addr > addr || symbol_addr < info->dli_saddr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Update our idea of the nearest symbol. */
|
|
|
|
info->dli_sname = obj->strtab + def->st_name;
|
|
|
|
info->dli_saddr = symbol_addr;
|
|
|
|
|
|
|
|
/* Exact match? */
|
|
|
|
if (info->dli_saddr == addr)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Error reporting function. Use it like printf. If formats the message
|
|
|
|
* into a buffer, and sets things up so that the next call to dlerror()
|
|
|
|
* will return the message.
|
|
|
|
*/
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
#ifdef __STDC__
|
|
|
|
_rtld_error(const char *fmt,...)
|
|
|
|
#else
|
|
|
|
_rtld_error(va_alist)
|
|
|
|
va_dcl
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
static char buf[512];
|
|
|
|
va_list ap;
|
|
|
|
#ifdef __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
const char *fmt;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
va_start(ap);
|
|
|
|
fmt = va_arg(ap, const char *);
|
|
|
|
#endif
|
|
|
|
xvsnprintf(buf, sizeof buf, fmt, ap);
|
|
|
|
error_message = buf;
|
|
|
|
va_end(ap);
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
1999-02-27 13:35:16 +03:00
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_debug_state()
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
/* do nothing */
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_linkmap_add(obj)
|
|
|
|
Obj_Entry *obj;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
struct link_map *l = &obj->linkmap;
|
|
|
|
struct link_map *prev;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->linkmap.l_name = obj->path;
|
|
|
|
obj->linkmap.l_addr = obj->mapbase;
|
|
|
|
obj->linkmap.l_ld = obj->dynamic;
|
1998-03-25 07:12:32 +03:00
|
|
|
#ifdef __mips__
|
1999-03-01 19:40:07 +03:00
|
|
|
/* GDB needs load offset on MIPS to use the symbols */
|
|
|
|
obj->linkmap.l_offs = obj->relocbase;
|
1998-03-25 07:12:32 +03:00
|
|
|
#endif
|
2000-07-17 06:55:52 +04:00
|
|
|
#ifdef __vax__
|
|
|
|
/* VAX shared libaries don't start at a vaddr of 0 */
|
|
|
|
obj->linkmap.l_addr -= obj->vaddrbase;
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
if (_rtld_debug.r_map == NULL) {
|
|
|
|
_rtld_debug.r_map = l;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (prev = _rtld_debug.r_map; prev->l_next != NULL; prev = prev->l_next);
|
|
|
|
l->l_prev = prev;
|
|
|
|
prev->l_next = l;
|
|
|
|
l->l_next = NULL;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_linkmap_delete(obj)
|
|
|
|
Obj_Entry *obj;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
struct link_map *l = &obj->linkmap;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
if (l->l_prev == NULL) {
|
|
|
|
if ((_rtld_debug.r_map = l->l_next) != NULL)
|
|
|
|
l->l_next->l_prev = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((l->l_prev->l_next = l->l_next) != NULL)
|
|
|
|
l->l_next->l_prev = l->l_prev;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
1999-11-07 03:21:12 +03:00
|
|
|
|
|
|
|
static Obj_Entry *
|
|
|
|
_rtld_obj_from_addr(const void *addr)
|
|
|
|
{
|
|
|
|
unsigned long endhash;
|
|
|
|
Obj_Entry *obj;
|
|
|
|
|
|
|
|
endhash = _rtld_elf_hash(END_SYM);
|
|
|
|
for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
|
|
|
|
const Elf_Sym *endsym;
|
|
|
|
|
|
|
|
if (addr < (void *) obj->mapbase)
|
|
|
|
continue;
|
|
|
|
if ((endsym = _rtld_symlook_obj(END_SYM, endhash, obj, true)) == NULL)
|
|
|
|
continue; /* No "end" symbol?! */
|
|
|
|
if (addr < (void *) (obj->relocbase + endsym->st_value))
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_rtld_objlist_remove(list, obj)
|
|
|
|
Objlist *list;
|
|
|
|
Obj_Entry *obj;
|
|
|
|
{
|
|
|
|
Objlist_Entry *elm;
|
|
|
|
|
|
|
|
if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
|
|
|
|
if ((list)->sqh_first == (elm)) {
|
|
|
|
SIMPLEQ_REMOVE_HEAD(list, elm, link);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct Struct_Objlist_Entry *curelm = (list)->sqh_first;
|
|
|
|
while (curelm->link.sqe_next != (elm))
|
|
|
|
curelm = curelm->link.sqe_next;
|
|
|
|
if((curelm->link.sqe_next =
|
|
|
|
curelm->link.sqe_next->link.sqe_next) == NULL)
|
|
|
|
(list)->sqh_last = &(curelm)->link.sqe_next;
|
|
|
|
}
|
|
|
|
free(elm);
|
|
|
|
}
|
|
|
|
}
|