search.c revision 1.19 1 /* $NetBSD: search.c,v 1.19 2004/10/22 05:39:57 skrll 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 <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: search.c,v 1.19 2004/10/22 05:39:57 skrll Exp $");
42 #endif /* not lint */
43
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <sys/types.h>
53 #include <sys/mman.h>
54 #include <sys/stat.h>
55 #include <dirent.h>
56
57 #include "debug.h"
58 #include "rtld.h"
59
60 /*
61 * Data declarations.
62 */
63 Search_Path *_rtld_invalid_paths;
64
65 static Obj_Entry *_rtld_search_library_path(const char *, size_t,
66 const char *, size_t, int);
67
68 static Obj_Entry *
69 _rtld_search_library_path(const char *name, size_t namelen,
70 const char *dir, size_t dirlen, int mode)
71 {
72 char *pathname;
73 size_t pathnamelen;
74 Obj_Entry *obj;
75 Search_Path *sp;
76
77 pathnamelen = dirlen + 1 + namelen;
78
79 for (sp = _rtld_invalid_paths; sp != NULL; sp = sp->sp_next) {
80 if (sp->sp_pathlen == pathnamelen &&
81 !memcmp(name, sp->sp_path + dirlen + 1, namelen) &&
82 !memcmp(dir, sp->sp_path, dirlen)) {
83 return NULL;
84 }
85 }
86
87 pathname = xmalloc(pathnamelen + 1);
88 (void)strncpy(pathname, dir, dirlen);
89 pathname[dirlen] = '/';
90 strcpy(pathname + dirlen + 1, name);
91
92 dbg((" Trying \"%s\"", pathname));
93 obj = _rtld_load_object(pathname, mode);
94 if (obj == NULL) {
95 Search_Path *path;
96
97 path = NEW(Search_Path);
98 path->sp_pathlen = pathnamelen;
99 path->sp_path = pathname;
100 path->sp_next = _rtld_invalid_paths;
101 _rtld_invalid_paths = path;
102 }
103 return obj;
104 }
105
106 /*
107 * Find the library with the given name, and return its full pathname.
108 * The returned string is dynamically allocated. Generates an error
109 * message and returns NULL if the library cannot be found.
110 *
111 * If the second argument is non-NULL, then it refers to an already-
112 * loaded shared object, whose library search path will be searched.
113 */
114 Obj_Entry *
115 _rtld_load_library(const char *name, const Obj_Entry *refobj, int mode)
116 {
117 char tmperror[512], *tmperrorp;
118 Search_Path *sp;
119 char *pathname;
120 int namelen;
121 Obj_Entry *obj;
122
123 if (strchr(name, '/') != NULL) { /* Hard coded pathname */
124 if (name[0] != '/' && !_rtld_trust) {
125 _rtld_error(
126 "absolute pathname required for shared object \"%s\"",
127 name);
128 return NULL;
129 }
130 pathname = xstrdup(name);
131 goto found;
132 }
133 dbg((" Searching for \"%s\" (%p)", name, refobj));
134
135 tmperrorp = dlerror();
136 if (tmperrorp != NULL) {
137 strncpy(tmperror, tmperrorp, sizeof tmperror);
138 tmperrorp = tmperror;
139 }
140
141 namelen = strlen(name);
142
143 for (sp = _rtld_paths; sp != NULL; sp = sp->sp_next)
144 if ((obj = _rtld_search_library_path(name, namelen,
145 sp->sp_path, sp->sp_pathlen, mode)) != NULL)
146 goto pathfound;
147
148 if (refobj != NULL)
149 for (sp = refobj->rpaths; sp != NULL; sp = sp->sp_next)
150 if ((obj = _rtld_search_library_path(name,
151 namelen, sp->sp_path, sp->sp_pathlen, mode)) != NULL)
152 goto pathfound;
153
154 for (sp = _rtld_default_paths; sp != NULL; sp = sp->sp_next)
155 if ((obj = _rtld_search_library_path(name, namelen,
156 sp->sp_path, sp->sp_pathlen, mode)) != NULL)
157 goto pathfound;
158
159 _rtld_error("Shared object \"%s\" not found", name);
160 return NULL;
161
162 pathfound:
163 /*
164 * Successfully found a library; restore the dlerror state as it was
165 * before _rtld_load_library() was called (any failed call to
166 * _rtld_search_library_path() will set the dlerror state, but if the
167 * library was eventually found, then the error state should not
168 * change.
169 */
170 if (tmperrorp)
171 _rtld_error("%s", tmperror);
172 else
173 (void)dlerror();
174 return obj;
175
176 found:
177 obj = _rtld_load_object(pathname, mode);
178 if (obj == NULL)
179 free(pathname);
180 return obj;
181 }
182