Home | History | Annotate | Line # | Download | only in ld.elf_so
search.c revision 1.13
      1  1.13   mycroft /*	$NetBSD: search.c,v 1.13 2002/09/24 12:52:20 mycroft Exp $	 */
      2   1.1       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      5   1.1       cgd  * All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *      This product includes software developed by John Polstra.
     18   1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     19   1.1       cgd  *    derived from this software without specific prior written permission.
     20   1.1       cgd  *
     21   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1       cgd  */
     32   1.1       cgd 
     33   1.1       cgd /*
     34   1.1       cgd  * Dynamic linker for ELF.
     35   1.1       cgd  *
     36   1.1       cgd  * John Polstra <jdp (at) polstra.com>.
     37   1.1       cgd  */
     38   1.1       cgd 
     39   1.1       cgd #include <err.h>
     40   1.1       cgd #include <errno.h>
     41   1.1       cgd #include <fcntl.h>
     42   1.1       cgd #include <stdarg.h>
     43   1.1       cgd #include <stdio.h>
     44   1.1       cgd #include <stdlib.h>
     45   1.1       cgd #include <string.h>
     46   1.1       cgd #include <unistd.h>
     47   1.1       cgd #include <sys/types.h>
     48   1.1       cgd #include <sys/mman.h>
     49   1.1       cgd #include <sys/stat.h>
     50   1.1       cgd #include <dirent.h>
     51   1.1       cgd 
     52   1.1       cgd #include "debug.h"
     53   1.1       cgd #include "rtld.h"
     54   1.7   hannken 
     55   1.1       cgd /*
     56   1.1       cgd  * Data declarations.
     57   1.1       cgd  */
     58  1.11   mycroft static Obj_Entry *_rtld_search_library_path __P((const char *, size_t,
     59  1.11   mycroft     const char *, size_t, int));
     60   1.1       cgd 
     61  1.11   mycroft static Obj_Entry *
     62  1.11   mycroft _rtld_search_library_path(name, namelen, dir, dirlen, mode)
     63   1.4  christos 	const char *name;
     64   1.4  christos 	size_t namelen;
     65   1.4  christos 	const char *dir;
     66   1.4  christos 	size_t dirlen;
     67  1.11   mycroft 	int mode;
     68   1.1       cgd {
     69   1.3       cgd 	char *pathname;
     70  1.11   mycroft 	Obj_Entry *obj;
     71   1.1       cgd 
     72   1.3       cgd 	pathname = xmalloc(dirlen + 1 + namelen + 1);
     73   1.4  christos 	(void)strncpy(pathname, dir, dirlen);
     74   1.3       cgd 	pathname[dirlen] = '/';
     75   1.3       cgd 	strcpy(pathname + dirlen + 1, name);
     76   1.3       cgd 
     77   1.4  christos 	dbg(("  Trying \"%s\"", pathname));
     78  1.11   mycroft 	obj = _rtld_load_object(pathname, mode);
     79  1.11   mycroft 	if (obj == NULL)
     80  1.11   mycroft 		free(pathname);
     81  1.11   mycroft 	return obj;
     82   1.1       cgd }
     83   1.4  christos 
     84   1.1       cgd /*
     85   1.1       cgd  * Find the library with the given name, and return its full pathname.
     86   1.1       cgd  * The returned string is dynamically allocated.  Generates an error
     87   1.1       cgd  * message and returns NULL if the library cannot be found.
     88   1.1       cgd  *
     89   1.1       cgd  * If the second argument is non-NULL, then it refers to an already-
     90   1.1       cgd  * loaded shared object, whose library search path will be searched.
     91   1.1       cgd  */
     92  1.11   mycroft Obj_Entry *
     93  1.11   mycroft _rtld_load_library(name, refobj, mode)
     94   1.4  christos 	const char *name;
     95   1.4  christos 	const Obj_Entry *refobj;
     96  1.11   mycroft 	int mode;
     97   1.1       cgd {
     98   1.4  christos 	Search_Path *sp;
     99   1.4  christos 	char *pathname;
    100   1.4  christos 	int namelen;
    101  1.11   mycroft 	Obj_Entry *obj;
    102   1.4  christos 
    103   1.4  christos 	if (strchr(name, '/') != NULL) {	/* Hard coded pathname */
    104   1.4  christos 		if (name[0] != '/' && !_rtld_trust) {
    105   1.4  christos 			_rtld_error(
    106  1.13   mycroft 			"absolute pathname required for shared object \"%s\"",
    107   1.4  christos 			    name);
    108   1.4  christos 			return NULL;
    109   1.4  christos 		}
    110  1.11   mycroft 		pathname = xstrdup(name);
    111  1.12   mycroft 		goto found;
    112   1.1       cgd 	}
    113   1.4  christos 	dbg((" Searching for \"%s\" (%p)", name, refobj));
    114   1.1       cgd 
    115   1.3       cgd 	namelen = strlen(name);
    116   1.3       cgd 
    117   1.5    kleink 	for (sp = _rtld_paths; sp != NULL; sp = sp->sp_next)
    118  1.11   mycroft 		if ((obj = _rtld_search_library_path(name, namelen,
    119  1.11   mycroft 		    sp->sp_path, sp->sp_pathlen, mode)) != NULL)
    120  1.12   mycroft 			return obj;
    121   1.5    kleink 
    122   1.4  christos 	if (refobj != NULL)
    123   1.4  christos 		for (sp = refobj->rpaths; sp != NULL; sp = sp->sp_next)
    124  1.11   mycroft 			if ((obj = _rtld_search_library_path(name,
    125  1.11   mycroft 			    namelen, sp->sp_path, sp->sp_pathlen, mode)) != NULL)
    126  1.12   mycroft 				return obj;
    127   1.3       cgd 
    128   1.5    kleink 	for (sp = _rtld_default_paths; sp != NULL; sp = sp->sp_next)
    129  1.11   mycroft 		if ((obj = _rtld_search_library_path(name, namelen,
    130  1.11   mycroft 		    sp->sp_path, sp->sp_pathlen, mode)) != NULL)
    131  1.12   mycroft 			return obj;
    132   1.3       cgd 
    133   1.4  christos 	_rtld_error("Shared object \"%s\" not found", name);
    134   1.4  christos 	return NULL;
    135  1.11   mycroft 
    136  1.12   mycroft found:
    137  1.11   mycroft 	obj = _rtld_load_object(pathname, mode);
    138  1.11   mycroft 	if (obj == NULL)
    139  1.11   mycroft 		free(pathname);
    140  1.11   mycroft 	return obj;
    141   1.1       cgd }
    142