2007-12-07 23:34:04 +03:00
|
|
|
/* $NetBSD: load.c,v 1.35 2007/12/07 20:34:04 ad 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
|
2007-12-07 23:34:04 +03:00
|
|
|
__RCSID("$NetBSD: load.c,v 1.35 2007/12/07 20:34:04 ad 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>
|
1999-12-13 12:09:34 +03:00
|
|
|
#include <sys/param.h>
|
1996-12-16 23:37:55 +03:00
|
|
|
#include <sys/mman.h>
|
1999-12-13 12:09:34 +03:00
|
|
|
#include <sys/sysctl.h>
|
1996-12-16 23:37:55 +03:00
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include "rtld.h"
|
|
|
|
|
2003-07-24 14:12:25 +04:00
|
|
|
static bool _rtld_load_by_name(const char *, Obj_Entry *, Needed_Entry **,
|
|
|
|
int);
|
1999-12-13 12:09:34 +03:00
|
|
|
|
2002-10-04 08:01:13 +04:00
|
|
|
#ifdef RTLD_LOADER
|
2002-10-04 07:59:40 +04:00
|
|
|
Objlist _rtld_list_main = /* Objects loaded at program startup */
|
|
|
|
SIMPLEQ_HEAD_INITIALIZER(_rtld_list_main);
|
2000-11-11 02:53:04 +03:00
|
|
|
Objlist _rtld_list_global = /* Objects dlopened with RTLD_GLOBAL */
|
|
|
|
SIMPLEQ_HEAD_INITIALIZER(_rtld_list_global);
|
2007-12-07 23:34:04 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
_rtld_objlist_push_head(Objlist *list, Obj_Entry *obj)
|
|
|
|
{
|
|
|
|
Objlist_Entry *elm;
|
|
|
|
|
|
|
|
elm = NEW(Objlist_Entry);
|
|
|
|
elm->obj = obj;
|
|
|
|
SIMPLEQ_INSERT_HEAD(list, elm, link);
|
|
|
|
}
|
2000-11-11 02:53:04 +03:00
|
|
|
|
|
|
|
void
|
2007-12-07 23:34:04 +03:00
|
|
|
_rtld_objlist_push_tail(Objlist *list, Obj_Entry *obj)
|
2000-11-11 02:53:04 +03:00
|
|
|
{
|
|
|
|
Objlist_Entry *elm;
|
|
|
|
|
|
|
|
elm = NEW(Objlist_Entry);
|
|
|
|
elm->obj = obj;
|
|
|
|
SIMPLEQ_INSERT_TAIL(list, elm, link);
|
|
|
|
}
|
|
|
|
|
|
|
|
Objlist_Entry *
|
|
|
|
_rtld_objlist_find(Objlist *list, const Obj_Entry *obj)
|
|
|
|
{
|
|
|
|
Objlist_Entry *elm;
|
|
|
|
|
2002-06-02 03:50:52 +04:00
|
|
|
SIMPLEQ_FOREACH(elm, list, link) {
|
2000-11-11 02:53:04 +03:00
|
|
|
if (elm->obj == obj)
|
|
|
|
return elm;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-10-04 08:04:26 +04:00
|
|
|
#endif
|
2000-11-11 02:53:04 +03:00
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
/*
|
2006-03-21 20:48:10 +03:00
|
|
|
* Load a shared object into memory, if it is not already loaded.
|
1996-12-16 23:37:55 +03:00
|
|
|
*
|
|
|
|
* Returns a pointer to the Obj_Entry for the object. Returns NULL
|
|
|
|
* on failure.
|
|
|
|
*/
|
|
|
|
Obj_Entry *
|
2006-03-21 20:48:10 +03:00
|
|
|
_rtld_load_object(const char *filepath, int mode)
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
Obj_Entry *obj;
|
1999-11-07 03:21:12 +03:00
|
|
|
int fd = -1;
|
|
|
|
struct stat sb;
|
2002-12-05 07:56:56 +03:00
|
|
|
size_t pathlen = strlen(filepath);
|
1999-03-01 19:40:07 +03:00
|
|
|
|
2002-12-05 07:56:56 +03:00
|
|
|
for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
|
|
|
|
if (pathlen == obj->pathlen && !strcmp(obj->path, filepath))
|
|
|
|
break;
|
1999-03-01 19:40:07 +03:00
|
|
|
|
1999-11-07 03:21:12 +03:00
|
|
|
/*
|
|
|
|
* If we didn't find a match by pathname, open the file and check
|
|
|
|
* again by device and inode. This avoids false mismatches caused
|
|
|
|
* by multiple links or ".." in pathnames.
|
|
|
|
*
|
|
|
|
* To avoid a race, we open the file and use fstat() rather than
|
|
|
|
* using stat().
|
|
|
|
*/
|
|
|
|
if (obj == NULL) {
|
1999-03-01 19:40:07 +03:00
|
|
|
if ((fd = open(filepath, O_RDONLY)) == -1) {
|
|
|
|
_rtld_error("Cannot open \"%s\"", filepath);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-11-07 03:21:12 +03:00
|
|
|
if (fstat(fd, &sb) == -1) {
|
|
|
|
_rtld_error("Cannot fstat \"%s\"", filepath);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) {
|
|
|
|
if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
|
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj == NULL) { /* First use of this object, so we must map it in */
|
|
|
|
obj = _rtld_map_object(filepath, fd, &sb);
|
1999-03-01 19:40:07 +03:00
|
|
|
(void)close(fd);
|
2006-03-21 20:48:10 +03:00
|
|
|
if (obj == NULL)
|
1999-03-01 19:40:07 +03:00
|
|
|
return NULL;
|
2007-09-20 11:50:32 +04:00
|
|
|
_rtld_digest_dynamic(filepath, obj);
|
1999-03-01 19:40:07 +03:00
|
|
|
|
|
|
|
*_rtld_objtail = obj;
|
|
|
|
_rtld_objtail = &obj->next;
|
1996-12-16 23:37:55 +03:00
|
|
|
#ifdef RTLD_LOADER
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_linkmap_add(obj); /* for GDB */
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
2002-09-13 02:56:28 +04:00
|
|
|
dbg((" %p .. %p: %s", obj->mapbase,
|
|
|
|
obj->mapbase + obj->mapsize - 1, obj->path));
|
|
|
|
if (obj->textrel)
|
|
|
|
dbg((" WARNING: %s has impure text", obj->path));
|
2006-03-21 20:48:10 +03:00
|
|
|
}
|
1999-03-01 19:40:07 +03:00
|
|
|
|
|
|
|
++obj->refcount;
|
2002-10-04 08:01:13 +04:00
|
|
|
#ifdef RTLD_LOADER
|
2002-10-04 00:35:19 +04:00
|
|
|
if (mode & RTLD_MAIN && !obj->mainref) {
|
|
|
|
obj->mainref = 1;
|
|
|
|
rdbg(("adding %p (%s) to _rtld_list_main", obj, obj->path));
|
2007-12-07 23:34:04 +03:00
|
|
|
_rtld_objlist_push_tail(&_rtld_list_main, obj);
|
2002-10-04 00:35:19 +04:00
|
|
|
}
|
|
|
|
if (mode & RTLD_GLOBAL && !obj->globalref) {
|
|
|
|
obj->globalref = 1;
|
|
|
|
rdbg(("adding %p (%s) to _rtld_list_global", obj, obj->path));
|
2007-12-07 23:34:04 +03:00
|
|
|
_rtld_objlist_push_tail(&_rtld_list_global, obj);
|
2002-10-04 00:35:19 +04:00
|
|
|
}
|
2002-10-04 08:01:13 +04:00
|
|
|
#endif
|
1999-03-01 19:40:07 +03:00
|
|
|
return obj;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
1999-12-13 12:22:52 +03:00
|
|
|
static bool
|
2003-07-24 14:12:25 +04:00
|
|
|
_rtld_load_by_name(const char *name, Obj_Entry *obj, Needed_Entry **needed, int mode)
|
1999-12-13 12:09:34 +03:00
|
|
|
{
|
|
|
|
Library_Xform *x = _rtld_xforms;
|
|
|
|
Obj_Entry *o = NULL;
|
|
|
|
size_t i, j;
|
1999-12-13 12:22:52 +03:00
|
|
|
bool got = false;
|
1999-12-13 12:09:34 +03:00
|
|
|
union {
|
|
|
|
int i;
|
2004-07-05 15:50:07 +04:00
|
|
|
u_quad_t q;
|
1999-12-13 12:09:34 +03:00
|
|
|
char s[16];
|
|
|
|
} val;
|
|
|
|
|
2002-09-13 02:56:28 +04:00
|
|
|
dbg(("load by name %s %p", name, x));
|
1999-12-13 12:09:34 +03:00
|
|
|
for (; x; x = x->next) {
|
|
|
|
if (strcmp(x->name, name) != 0)
|
|
|
|
continue;
|
|
|
|
|
2004-07-05 15:50:07 +04:00
|
|
|
j = sizeof(val);
|
|
|
|
if ((i = _rtld_sysctl(x->ctlname, &val, &j)) == -1) {
|
|
|
|
xwarnx(_PATH_LD_HINTS ": invalid/unknown sysctl for %s (%d)",
|
|
|
|
name, errno);
|
1999-12-13 12:09:34 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-07-05 15:50:07 +04:00
|
|
|
switch (i) {
|
|
|
|
case CTLTYPE_QUAD:
|
|
|
|
xsnprintf(val.s, sizeof(val.s), "%" PRIu64, val.q);
|
|
|
|
break;
|
1999-12-13 12:09:34 +03:00
|
|
|
case CTLTYPE_INT:
|
|
|
|
xsnprintf(val.s, sizeof(val.s), "%d", val.i);
|
|
|
|
break;
|
|
|
|
case CTLTYPE_STRING:
|
|
|
|
break;
|
|
|
|
default:
|
2004-07-05 15:50:07 +04:00
|
|
|
xwarnx("unsupported sysctl type %d", (int)i);
|
1999-12-13 12:09:34 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-09-13 02:56:28 +04:00
|
|
|
dbg(("sysctl returns %s", val.s));
|
1999-12-13 12:09:34 +03:00
|
|
|
|
|
|
|
for (i = 0; i < RTLD_MAX_ENTRY && x->entry[i].value != NULL;
|
|
|
|
i++) {
|
2002-09-13 02:56:28 +04:00
|
|
|
dbg(("entry %ld", (unsigned long)i));
|
1999-12-13 12:09:34 +03:00
|
|
|
if (strcmp(x->entry[i].value, val.s) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == RTLD_MAX_ENTRY) {
|
1999-12-27 18:36:36 +03:00
|
|
|
xwarnx("sysctl value %s not found for lib%s",
|
1999-12-13 12:09:34 +03:00
|
|
|
val.s, name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* XXX: This can mess up debuggers, cause we lie about
|
|
|
|
* what we loaded in the needed objects */
|
|
|
|
for (j = 0; j < RTLD_MAX_LIBRARY &&
|
|
|
|
x->entry[i].library[j] != NULL; j++) {
|
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
|
|
|
o = _rtld_load_library(x->entry[i].library[j], obj,
|
|
|
|
mode);
|
|
|
|
if (o == NULL) {
|
1999-12-27 18:36:36 +03:00
|
|
|
xwarnx("could not load %s for %s",
|
1999-12-13 12:09:34 +03:00
|
|
|
x->entry[i].library[j], name);
|
|
|
|
continue;
|
|
|
|
}
|
1999-12-13 12:22:52 +03:00
|
|
|
got = true;
|
|
|
|
if (j == 0)
|
|
|
|
(*needed)->obj = o;
|
|
|
|
else {
|
|
|
|
/* make a new one and put it in the chain */
|
|
|
|
Needed_Entry *ne = xmalloc(sizeof(*ne));
|
|
|
|
ne->name = (*needed)->name;
|
|
|
|
ne->obj = o;
|
|
|
|
ne->next = (*needed)->next;
|
|
|
|
(*needed)->next = ne;
|
|
|
|
*needed = ne;
|
|
|
|
}
|
|
|
|
|
1999-12-13 12:09:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
1999-12-13 12:22:52 +03:00
|
|
|
if (got)
|
|
|
|
return true;
|
1999-12-13 12:09:34 +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
|
|
|
return ((*needed)->obj = _rtld_load_library(name, obj, mode)) != NULL;
|
1999-12-13 12:09:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
/*
|
|
|
|
* Given a shared object, traverse its list of needed objects, and load
|
|
|
|
* each of them. Returns 0 on success. Generates an error message and
|
|
|
|
* returns -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
2003-07-24 14:12:25 +04:00
|
|
|
_rtld_load_needed_objects(Obj_Entry *first, int mode)
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
Obj_Entry *obj;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
for (obj = first; obj != NULL; obj = obj->next) {
|
|
|
|
Needed_Entry *needed;
|
|
|
|
|
|
|
|
for (needed = obj->needed; needed != NULL;
|
|
|
|
needed = needed->next) {
|
|
|
|
const char *name = obj->strtab + needed->name;
|
2002-09-13 02:56:28 +04:00
|
|
|
if (!_rtld_load_by_name(name, obj, &needed, mode))
|
1999-12-13 12:09:34 +03:00
|
|
|
status = -1; /* FIXME - cleanup */
|
1996-12-16 23:37:55 +03:00
|
|
|
#ifdef RTLD_LOADER
|
1999-03-01 19:40:07 +03:00
|
|
|
if (status == -1)
|
|
|
|
return status;
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
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 status;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
1999-05-31 18:48:16 +04:00
|
|
|
|
|
|
|
#ifdef RTLD_LOADER
|
|
|
|
int
|
2003-07-24 14:12:25 +04:00
|
|
|
_rtld_preload(const char *preload_path)
|
1999-05-31 18:48:16 +04:00
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
char *cp, *buf;
|
|
|
|
int status = 0;
|
|
|
|
|
2003-11-25 17:36:49 +03:00
|
|
|
if (preload_path != NULL && *preload_path != '\0') {
|
1999-05-31 18:48:16 +04:00
|
|
|
cp = buf = xstrdup(preload_path);
|
2001-11-02 18:28:36 +03:00
|
|
|
while ((path = strsep(&cp, " :")) != NULL && status == 0) {
|
2006-03-21 20:48:10 +03:00
|
|
|
if (!_rtld_load_object(path, RTLD_MAIN))
|
1999-05-31 18:48:16 +04:00
|
|
|
status = -1;
|
2002-09-13 02:56:28 +04:00
|
|
|
else
|
1999-05-31 18:48:16 +04:00
|
|
|
dbg((" preloaded \"%s\"", path));
|
|
|
|
}
|
2007-10-06 02:21:07 +04:00
|
|
|
xfree(buf);
|
1999-05-31 18:48:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (status);
|
|
|
|
}
|
|
|
|
#endif
|