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