2008-10-04 13:37:12 +04:00
|
|
|
/* $NetBSD: symbol.c,v 1.47 2008/10/04 09:37:12 skrll Exp $ */
|
1996-12-16 23:37:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 1996 John D. Polstra.
|
|
|
|
* Copyright 1996 Matt Thomas <matt@3am-software.com>
|
2002-10-05 15:59:03 +04:00
|
|
|
* Copyright 2002 Charles M. Hannum <root@ihack.net>
|
1996-12-16 23:37:55 +03:00
|
|
|
* 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>.
|
|
|
|
*/
|
|
|
|
|
2004-10-22 09:39:56 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2008-10-04 13:37:12 +04:00
|
|
|
__RCSID("$NetBSD: symbol.c,v 1.47 2008/10/04 09:37:12 skrll Exp $");
|
2004-10-22 09:39:56 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include "rtld.h"
|
|
|
|
|
2005-05-10 17:15:56 +04:00
|
|
|
typedef void (*fptr_t)(void);
|
|
|
|
|
2003-08-12 13:18:38 +04:00
|
|
|
static bool
|
|
|
|
_rtld_is_exported(const Elf_Sym *def)
|
|
|
|
{
|
2008-01-14 11:53:42 +03:00
|
|
|
static const fptr_t _rtld_exports[] = {
|
2005-05-10 17:15:56 +04:00
|
|
|
(fptr_t)dlopen,
|
|
|
|
(fptr_t)dlclose,
|
|
|
|
(fptr_t)dlsym,
|
|
|
|
(fptr_t)dlerror,
|
|
|
|
(fptr_t)dladdr,
|
|
|
|
NULL
|
2003-08-12 13:18:38 +04:00
|
|
|
};
|
|
|
|
int i;
|
2005-05-10 17:15:56 +04:00
|
|
|
fptr_t value;
|
2003-08-12 13:18:38 +04:00
|
|
|
|
2005-05-10 17:15:56 +04:00
|
|
|
value = (fptr_t)(_rtld_objself.relocbase + def->st_value);
|
|
|
|
for (i = 0; _rtld_exports[i] != NULL; i++) {
|
2003-08-12 13:18:38 +04:00
|
|
|
if (value == _rtld_exports[i])
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
/*
|
|
|
|
* Hash function for symbol table lookup. Don't even think about changing
|
|
|
|
* this. It is specified by the System V ABI.
|
|
|
|
*/
|
|
|
|
unsigned long
|
2003-07-24 14:12:25 +04:00
|
|
|
_rtld_elf_hash(const char *name)
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
const unsigned char *p = (const unsigned char *) name;
|
|
|
|
unsigned long h = 0;
|
|
|
|
unsigned long g;
|
2002-10-05 00:34:10 +04:00
|
|
|
unsigned long c;
|
1999-03-01 19:40:07 +03:00
|
|
|
|
2002-10-05 00:34:10 +04:00
|
|
|
for (; __predict_true((c = *p) != '\0'); p++) {
|
|
|
|
h <<= 4;
|
|
|
|
h += c;
|
|
|
|
if ((g = h & 0xf0000000) != 0) {
|
|
|
|
h ^= g;
|
1999-03-01 19:40:07 +03:00
|
|
|
h ^= g >> 24;
|
2002-10-05 00:34:10 +04:00
|
|
|
}
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
2002-10-05 00:34:10 +04:00
|
|
|
return (h);
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
1999-11-07 03:21:12 +03:00
|
|
|
const Elf_Sym *
|
2002-10-04 00:35:19 +04:00
|
|
|
_rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
|
2003-07-24 14:12:25 +04:00
|
|
|
const Obj_Entry **defobj_out, bool in_plt)
|
1999-11-07 03:21:12 +03:00
|
|
|
{
|
|
|
|
const Elf_Sym *symp;
|
|
|
|
const Elf_Sym *def;
|
2003-04-24 20:55:29 +04:00
|
|
|
const Obj_Entry *defobj;
|
1999-11-07 03:21:12 +03:00
|
|
|
const Objlist_Entry *elm;
|
|
|
|
|
|
|
|
def = NULL;
|
2003-04-24 20:55:29 +04:00
|
|
|
defobj = NULL;
|
2002-06-02 03:50:52 +04:00
|
|
|
SIMPLEQ_FOREACH(elm, objlist, link) {
|
2007-12-27 00:10:03 +03:00
|
|
|
rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
|
|
|
|
name));
|
2002-09-25 00:23:11 +04:00
|
|
|
if ((symp = _rtld_symlook_obj(name, hash, elm->obj, in_plt))
|
1999-11-07 03:21:12 +03:00
|
|
|
!= NULL) {
|
|
|
|
if ((def == NULL) ||
|
1999-11-10 21:34:49 +03:00
|
|
|
(ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
|
1999-11-07 03:21:12 +03:00
|
|
|
def = symp;
|
2003-04-24 20:55:29 +04:00
|
|
|
defobj = elm->obj;
|
1999-11-10 21:34:49 +03:00
|
|
|
if (ELF_ST_BIND(def->st_info) != STB_WEAK)
|
1999-11-07 03:21:12 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-04-24 20:55:29 +04:00
|
|
|
if (def != NULL)
|
|
|
|
*defobj_out = defobj;
|
1999-11-07 03:21:12 +03:00
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2008-10-04 13:37:12 +04:00
|
|
|
/*
|
|
|
|
* Search the symbol table of a shared object and all objects needed by it for
|
|
|
|
* a symbol of the given name. Search order is breadth-first. Returns a pointer
|
|
|
|
* to the symbol, or NULL if no definition was found.
|
|
|
|
*/
|
|
|
|
const Elf_Sym *
|
|
|
|
_rtld_symlook_needed(const char *name, unsigned long hash,
|
|
|
|
const Needed_Entry *needed, const Obj_Entry **defobj_out, bool inplt)
|
|
|
|
{
|
|
|
|
const Elf_Sym *def, *def_w;
|
|
|
|
const Needed_Entry *n;
|
|
|
|
const Obj_Entry *obj, *defobj, *defobj1;
|
|
|
|
|
|
|
|
def = def_w = NULL;
|
|
|
|
defobj = NULL;
|
|
|
|
for (n = needed; n != NULL; n = n->next) {
|
|
|
|
if ((obj = n->obj) == NULL ||
|
|
|
|
(def = _rtld_symlook_obj(name, hash, obj, inplt)) == NULL)
|
|
|
|
continue;
|
|
|
|
defobj = obj;
|
|
|
|
if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
|
|
|
|
*defobj_out = defobj;
|
|
|
|
|
|
|
|
return (def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Either the symbol definition has not been found in directly needed
|
|
|
|
* objects, or the found symbol is weak.
|
|
|
|
*/
|
|
|
|
for (n = needed; n != NULL; n = n->next) {
|
|
|
|
if ((obj = n->obj) == NULL)
|
|
|
|
continue;
|
|
|
|
def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
|
|
|
|
inplt);
|
|
|
|
if (def_w == NULL)
|
|
|
|
continue;
|
|
|
|
if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
|
|
|
|
def = def_w;
|
|
|
|
defobj = defobj1;
|
|
|
|
if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (def != NULL)
|
|
|
|
*defobj_out = defobj;
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
/*
|
|
|
|
* Search the symbol table of a single shared object for a symbol of
|
|
|
|
* the given name. Returns a pointer to the symbol, or NULL if no
|
|
|
|
* definition was found.
|
|
|
|
*
|
|
|
|
* The symbol's hash value is passed in for efficiency reasons; that
|
|
|
|
* eliminates many recomputations of the hash value.
|
|
|
|
*/
|
|
|
|
const Elf_Sym *
|
2003-07-24 14:12:25 +04:00
|
|
|
_rtld_symlook_obj(const char *name, unsigned long hash,
|
|
|
|
const Obj_Entry *obj, bool in_plt)
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
unsigned long symnum;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
for (symnum = obj->buckets[hash % obj->nbuckets];
|
|
|
|
symnum != ELF_SYM_UNDEFINED;
|
|
|
|
symnum = obj->chains[symnum]) {
|
1999-03-01 19:40:07 +03:00
|
|
|
const Elf_Sym *symp;
|
|
|
|
const char *strp;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
assert(symnum < obj->nchains);
|
|
|
|
symp = obj->symtab + symnum;
|
|
|
|
strp = obj->strtab + symp->st_name;
|
2005-01-12 00:58:27 +03:00
|
|
|
rdbg(("check \"%s\" vs \"%s\" in %p", name, strp, obj));
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
if (name[1] == strp[1] && !strcmp(name, strp)) {
|
|
|
|
if (symp->st_shndx != SHN_UNDEF)
|
1999-03-01 19:40:07 +03:00
|
|
|
return symp;
|
2002-09-25 00:23:11 +04:00
|
|
|
#ifndef __mips__
|
2002-09-25 00:27:07 +04:00
|
|
|
/*
|
|
|
|
* XXX DANGER WILL ROBINSON!
|
|
|
|
* If we have a function pointer in the executable's
|
|
|
|
* data section, it points to the executable's PLT
|
|
|
|
* slot, and there is NO relocation emitted. To make
|
|
|
|
* the function pointer comparable to function pointers
|
|
|
|
* in shared libraries, we must resolve data references
|
|
|
|
* in the libraries to point to PLT slots in the
|
|
|
|
* executable, if they exist.
|
|
|
|
*/
|
2002-09-25 00:23:11 +04:00
|
|
|
else if (!in_plt && symp->st_value != 0 &&
|
|
|
|
ELF_ST_TYPE(symp->st_info) == STT_FUNC)
|
|
|
|
return symp;
|
|
|
|
#endif
|
2002-09-13 07:40:40 +04:00
|
|
|
else
|
|
|
|
return NULL;
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
return NULL;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a symbol number in a referencing object, find the corresponding
|
|
|
|
* definition of the symbol. Returns a pointer to the symbol, or NULL if
|
|
|
|
* no definition was found. Returns a pointer to the Obj_Entry of the
|
|
|
|
* defining object via the reference parameter DEFOBJ_OUT.
|
|
|
|
*/
|
|
|
|
const Elf_Sym *
|
2003-07-24 14:12:25 +04:00
|
|
|
_rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
|
|
|
|
const Obj_Entry **defobj_out, bool in_plt)
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
2003-04-24 20:55:29 +04:00
|
|
|
const Elf_Sym *ref;
|
1999-11-07 03:21:12 +03:00
|
|
|
const Elf_Sym *def;
|
2003-04-24 20:55:29 +04:00
|
|
|
const Obj_Entry *defobj;
|
|
|
|
const char *name;
|
1999-03-01 19:40:07 +03:00
|
|
|
unsigned long hash;
|
|
|
|
|
2007-02-22 21:57:48 +03:00
|
|
|
#ifdef COMBRELOC
|
|
|
|
/*
|
|
|
|
* COMBRELOC combines multiple reloc sections and sorts them to make
|
|
|
|
* dynamic symbol lookup caching possible.
|
|
|
|
*
|
|
|
|
* So if the lookup we are doing is the same as the previous lookup
|
|
|
|
* return the cached results.
|
|
|
|
*/
|
|
|
|
static unsigned long last_symnum;
|
|
|
|
static const Elf_Sym *last_def;
|
|
|
|
static const Obj_Entry *last_refobj;
|
|
|
|
static const Obj_Entry *last_defobj;
|
|
|
|
|
|
|
|
if (symnum == last_symnum && refobj == last_refobj
|
2007-02-23 04:16:32 +03:00
|
|
|
&& in_plt == false) {
|
2007-02-22 21:57:48 +03:00
|
|
|
*defobj_out = last_defobj;
|
|
|
|
return last_def;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-04-24 20:55:29 +04:00
|
|
|
ref = refobj->symtab + symnum;
|
|
|
|
name = refobj->strtab + ref->st_name;
|
|
|
|
|
2003-08-12 13:18:38 +04:00
|
|
|
/*
|
2005-10-13 15:14:09 +04:00
|
|
|
* We don't have to do a full scale lookup if the symbol is local.
|
|
|
|
* We know it will bind to the instance in this load module; to
|
|
|
|
* which we already have a pointer (ie ref).
|
2003-08-12 13:18:38 +04:00
|
|
|
*/
|
2005-10-13 15:14:09 +04:00
|
|
|
if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
|
|
|
|
if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
|
|
|
|
_rtld_error("%s: Bogus symbol table entry %lu",
|
|
|
|
refobj->path, symnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
hash = _rtld_elf_hash(name);
|
|
|
|
defobj = NULL;
|
|
|
|
def = _rtld_symlook_default(name, hash, refobj, &defobj, in_plt);
|
|
|
|
} else {
|
|
|
|
rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
|
|
|
|
def = ref;
|
|
|
|
defobj = refobj;
|
2003-08-12 13:18:38 +04:00
|
|
|
}
|
2005-10-13 15:14:09 +04:00
|
|
|
|
2003-04-24 20:55:29 +04:00
|
|
|
/*
|
|
|
|
* If we found no definition and the reference is weak, treat the
|
|
|
|
* symbol as having the value zero.
|
|
|
|
*/
|
|
|
|
if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
|
2008-07-23 22:16:42 +04:00
|
|
|
if (in_plt) {
|
|
|
|
_rtld_error(
|
|
|
|
"%s: Trying to call undefined weak symbol `%s'",
|
|
|
|
refobj->path, name);
|
|
|
|
}
|
|
|
|
rdbg((" returning _rtld_sym_zero@_rtld_objself"));
|
2003-04-24 20:55:29 +04:00
|
|
|
def = &_rtld_sym_zero;
|
2008-07-23 22:16:42 +04:00
|
|
|
defobj = &_rtld_objself;
|
2003-04-23 21:40:25 +04:00
|
|
|
}
|
2003-08-12 13:18:38 +04:00
|
|
|
|
2007-02-22 21:57:48 +03:00
|
|
|
if (def != NULL) {
|
2003-04-24 20:55:29 +04:00
|
|
|
*defobj_out = defobj;
|
2007-02-22 21:57:48 +03:00
|
|
|
#ifdef COMBRELOC
|
2007-02-23 04:16:32 +03:00
|
|
|
if (in_plt == false) {
|
|
|
|
/*
|
|
|
|
* Cache the lookup arguments and results if this was
|
|
|
|
* non-PLT lookup.
|
|
|
|
*/
|
|
|
|
last_symnum = symnum;
|
|
|
|
last_refobj = refobj;
|
|
|
|
last_def = def;
|
|
|
|
last_defobj = defobj;
|
|
|
|
}
|
2007-02-22 21:57:48 +03:00
|
|
|
#endif
|
|
|
|
} else {
|
2003-04-24 20:55:29 +04:00
|
|
|
rdbg(("lookup failed"));
|
|
|
|
_rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
|
|
|
|
refobj->path, in_plt ? "PLT " : "", name, symnum);
|
|
|
|
}
|
|
|
|
return def;
|
2003-04-23 21:40:25 +04:00
|
|
|
}
|
2003-05-30 19:43:33 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a symbol name in a referencing object, find the corresponding
|
|
|
|
* definition of the symbol. Returns a pointer to the symbol, or NULL if
|
|
|
|
* no definition was found. Returns a pointer to the Obj_Entry of the
|
|
|
|
* defining object via the reference parameter DEFOBJ_OUT.
|
|
|
|
*/
|
|
|
|
const Elf_Sym *
|
|
|
|
_rtld_symlook_default(const char *name, unsigned long hash,
|
|
|
|
const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
|
|
|
|
{
|
2003-07-15 11:38:29 +04:00
|
|
|
const Elf_Sym *def;
|
|
|
|
const Elf_Sym *symp;
|
|
|
|
const Obj_Entry *obj;
|
|
|
|
const Obj_Entry *defobj;
|
|
|
|
const Objlist_Entry *elm;
|
|
|
|
def = NULL;
|
|
|
|
defobj = NULL;
|
2003-05-30 19:43:33 +04:00
|
|
|
|
2003-07-15 11:38:29 +04:00
|
|
|
/* Look first in the referencing object if linked symbolically. */
|
|
|
|
if (refobj->symbolic) {
|
2007-12-27 00:10:03 +03:00
|
|
|
rdbg(("search referencing object for %s", name));
|
2003-07-15 11:38:29 +04:00
|
|
|
symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
|
|
|
|
if (symp != NULL) {
|
|
|
|
def = symp;
|
|
|
|
defobj = refobj;
|
|
|
|
}
|
2003-05-30 19:43:33 +04:00
|
|
|
}
|
|
|
|
|
2003-07-15 11:38:29 +04:00
|
|
|
/* Search all objects loaded at program start up. */
|
|
|
|
if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
|
2007-12-27 00:10:03 +03:00
|
|
|
rdbg(("search _rtld_list_main for %s", name));
|
2005-10-13 15:14:09 +04:00
|
|
|
symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
|
|
|
|
in_plt);
|
2003-07-15 11:38:29 +04:00
|
|
|
if (symp != NULL &&
|
2005-10-13 15:14:09 +04:00
|
|
|
(def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
|
2003-07-15 11:38:29 +04:00
|
|
|
def = symp;
|
|
|
|
defobj = obj;
|
|
|
|
}
|
2003-05-30 19:43:33 +04:00
|
|
|
}
|
|
|
|
|
2005-10-13 15:14:09 +04:00
|
|
|
/* Search all RTLD_GLOBAL objects. */
|
|
|
|
if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
|
2007-12-27 00:10:03 +03:00
|
|
|
rdbg(("search _rtld_list_global for %s", name));
|
2005-10-13 15:14:09 +04:00
|
|
|
symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
|
|
|
|
&obj, in_plt);
|
2003-07-15 11:38:29 +04:00
|
|
|
if (symp != NULL &&
|
|
|
|
(def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
|
|
|
|
def = symp;
|
|
|
|
defobj = obj;
|
|
|
|
}
|
2003-05-30 19:43:33 +04:00
|
|
|
}
|
2005-10-13 15:14:09 +04:00
|
|
|
|
|
|
|
/* Search all dlopened DAGs containing the referencing object. */
|
|
|
|
SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
|
2003-07-15 11:38:29 +04:00
|
|
|
if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
|
|
|
|
break;
|
2007-12-27 00:10:03 +03:00
|
|
|
rdbg(("search DAG with root %p (%s) for %s", elm->obj,
|
|
|
|
elm->obj->path, name));
|
2005-10-13 15:14:09 +04:00
|
|
|
symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
|
|
|
|
&obj, in_plt);
|
2003-07-15 11:38:29 +04:00
|
|
|
if (symp != NULL &&
|
|
|
|
(def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
|
|
|
|
def = symp;
|
|
|
|
defobj = obj;
|
|
|
|
}
|
2003-05-30 19:43:33 +04:00
|
|
|
}
|
|
|
|
|
2003-07-15 11:38:29 +04:00
|
|
|
/*
|
|
|
|
* Search the dynamic linker itself, and possibly resolve the
|
|
|
|
* symbol from there. This is how the application links to
|
|
|
|
* dynamic linker services such as dlopen. Only the values listed
|
2005-10-13 15:14:09 +04:00
|
|
|
* in the "_rtld_exports" array can be resolved from the dynamic
|
|
|
|
* linker.
|
2003-07-15 11:38:29 +04:00
|
|
|
*/
|
|
|
|
if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
|
2005-10-13 15:14:09 +04:00
|
|
|
rdbg(("Search the dynamic linker itself."));
|
2003-07-15 11:39:55 +04:00
|
|
|
symp = _rtld_symlook_obj(name, hash, &_rtld_objself, in_plt);
|
2005-10-13 15:14:09 +04:00
|
|
|
if (symp != NULL && _rtld_is_exported(symp)) {
|
2003-07-15 11:38:29 +04:00
|
|
|
def = symp;
|
2003-07-15 11:39:55 +04:00
|
|
|
defobj = &_rtld_objself;
|
2003-07-15 11:38:29 +04:00
|
|
|
}
|
2003-05-30 19:43:33 +04:00
|
|
|
}
|
|
|
|
|
2003-07-15 11:38:29 +04:00
|
|
|
if (def != NULL)
|
|
|
|
*defobj_out = defobj;
|
|
|
|
return def;
|
2003-05-30 19:43:33 +04:00
|
|
|
}
|