load.c revision 1.32 1 /* $NetBSD: load.c,v 1.32 2007/05/18 21:44:08 christos Exp $ */
2
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root (at) ihack.net>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by John Polstra.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * Dynamic linker for ELF.
37 *
38 * John Polstra <jdp (at) polstra.com>.
39 */
40
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: load.c,v 1.32 2007/05/18 21:44:08 christos Exp $");
44 #endif /* not lint */
45
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <sys/types.h>
55 #include <sys/param.h>
56 #include <sys/mman.h>
57 #include <sys/sysctl.h>
58 #include <dirent.h>
59
60 #include "debug.h"
61 #include "rtld.h"
62
63 static bool _rtld_load_by_name(const char *, Obj_Entry *, Needed_Entry **,
64 int);
65
66 #ifdef RTLD_LOADER
67 Objlist _rtld_list_main = /* Objects loaded at program startup */
68 SIMPLEQ_HEAD_INITIALIZER(_rtld_list_main);
69 Objlist _rtld_list_global = /* Objects dlopened with RTLD_GLOBAL */
70 SIMPLEQ_HEAD_INITIALIZER(_rtld_list_global);
71
72 void
73 _rtld_objlist_add(Objlist *list, Obj_Entry *obj)
74 {
75 Objlist_Entry *elm;
76
77 elm = NEW(Objlist_Entry);
78 elm->obj = obj;
79 SIMPLEQ_INSERT_TAIL(list, elm, link);
80 }
81
82 Objlist_Entry *
83 _rtld_objlist_find(Objlist *list, const Obj_Entry *obj)
84 {
85 Objlist_Entry *elm;
86
87 SIMPLEQ_FOREACH(elm, list, link) {
88 if (elm->obj == obj)
89 return elm;
90 }
91 return NULL;
92 }
93 #endif
94
95 /*
96 * Load a shared object into memory, if it is not already loaded.
97 *
98 * Returns a pointer to the Obj_Entry for the object. Returns NULL
99 * on failure.
100 */
101 Obj_Entry *
102 _rtld_load_object(const char *filepath, int mode)
103 {
104 Obj_Entry *obj;
105 int fd = -1;
106 struct stat sb;
107 size_t pathlen = strlen(filepath);
108
109 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
110 if (pathlen == obj->pathlen && !strcmp(obj->path, filepath))
111 break;
112
113 /*
114 * If we didn't find a match by pathname, open the file and check
115 * again by device and inode. This avoids false mismatches caused
116 * by multiple links or ".." in pathnames.
117 *
118 * To avoid a race, we open the file and use fstat() rather than
119 * using stat().
120 */
121 if (obj == NULL) {
122 if ((fd = open(filepath, O_RDONLY)) == -1) {
123 _rtld_error("Cannot open \"%s\"", filepath);
124 return NULL;
125 }
126 if (fstat(fd, &sb) == -1) {
127 _rtld_error("Cannot fstat \"%s\"", filepath);
128 close(fd);
129 return NULL;
130 }
131 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) {
132 if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
133 close(fd);
134 break;
135 }
136 }
137 }
138
139 if (obj == NULL) { /* First use of this object, so we must map it in */
140 char *p, pathname[MAXPATHLEN];
141 const char *q;
142
143 obj = _rtld_map_object(filepath, fd, &sb);
144 (void)close(fd);
145 if (obj == NULL)
146 return NULL;
147
148 (void)strlcpy(pathname, filepath, sizeof(pathname));
149 if ((p = strrchr(q = pathname, '/')) != NULL)
150 *p = '\0';
151 else
152 q = ".";
153 _rtld_digest_dynamic(q, obj);
154
155 *_rtld_objtail = obj;
156 _rtld_objtail = &obj->next;
157 #ifdef RTLD_LOADER
158 _rtld_linkmap_add(obj); /* for GDB */
159 #endif
160 dbg((" %p .. %p: %s", obj->mapbase,
161 obj->mapbase + obj->mapsize - 1, obj->path));
162 if (obj->textrel)
163 dbg((" WARNING: %s has impure text", obj->path));
164 }
165
166 ++obj->refcount;
167 #ifdef RTLD_LOADER
168 if (mode & RTLD_MAIN && !obj->mainref) {
169 obj->mainref = 1;
170 rdbg(("adding %p (%s) to _rtld_list_main", obj, obj->path));
171 _rtld_objlist_add(&_rtld_list_main, obj);
172 }
173 if (mode & RTLD_GLOBAL && !obj->globalref) {
174 obj->globalref = 1;
175 rdbg(("adding %p (%s) to _rtld_list_global", obj, obj->path));
176 _rtld_objlist_add(&_rtld_list_global, obj);
177 }
178 #endif
179 return obj;
180 }
181
182 static bool
183 _rtld_load_by_name(const char *name, Obj_Entry *obj, Needed_Entry **needed, int mode)
184 {
185 Library_Xform *x = _rtld_xforms;
186 Obj_Entry *o = NULL;
187 size_t i, j;
188 bool got = false;
189 union {
190 int i;
191 u_quad_t q;
192 char s[16];
193 } val;
194
195 dbg(("load by name %s %p", name, x));
196 for (; x; x = x->next) {
197 if (strcmp(x->name, name) != 0)
198 continue;
199
200 j = sizeof(val);
201 if ((i = _rtld_sysctl(x->ctlname, &val, &j)) == -1) {
202 xwarnx(_PATH_LD_HINTS ": invalid/unknown sysctl for %s (%d)",
203 name, errno);
204 break;
205 }
206
207 switch (i) {
208 case CTLTYPE_QUAD:
209 xsnprintf(val.s, sizeof(val.s), "%" PRIu64, val.q);
210 break;
211 case CTLTYPE_INT:
212 xsnprintf(val.s, sizeof(val.s), "%d", val.i);
213 break;
214 case CTLTYPE_STRING:
215 break;
216 default:
217 xwarnx("unsupported sysctl type %d", (int)i);
218 break;
219 }
220
221 dbg(("sysctl returns %s", val.s));
222
223 for (i = 0; i < RTLD_MAX_ENTRY && x->entry[i].value != NULL;
224 i++) {
225 dbg(("entry %ld", (unsigned long)i));
226 if (strcmp(x->entry[i].value, val.s) == 0)
227 break;
228 }
229
230 if (i == RTLD_MAX_ENTRY) {
231 xwarnx("sysctl value %s not found for lib%s",
232 val.s, name);
233 break;
234 }
235 /* XXX: This can mess up debuggers, cause we lie about
236 * what we loaded in the needed objects */
237 for (j = 0; j < RTLD_MAX_LIBRARY &&
238 x->entry[i].library[j] != NULL; j++) {
239 o = _rtld_load_library(x->entry[i].library[j], obj,
240 mode);
241 if (o == NULL) {
242 xwarnx("could not load %s for %s",
243 x->entry[i].library[j], name);
244 continue;
245 }
246 got = true;
247 if (j == 0)
248 (*needed)->obj = o;
249 else {
250 /* make a new one and put it in the chain */
251 Needed_Entry *ne = xmalloc(sizeof(*ne));
252 ne->name = (*needed)->name;
253 ne->obj = o;
254 ne->next = (*needed)->next;
255 (*needed)->next = ne;
256 *needed = ne;
257 }
258
259 }
260
261 }
262
263 if (got)
264 return true;
265
266 return ((*needed)->obj = _rtld_load_library(name, obj, mode)) != NULL;
267 }
268
269
270 /*
271 * Given a shared object, traverse its list of needed objects, and load
272 * each of them. Returns 0 on success. Generates an error message and
273 * returns -1 on failure.
274 */
275 int
276 _rtld_load_needed_objects(Obj_Entry *first, int mode)
277 {
278 Obj_Entry *obj;
279 int status = 0;
280
281 for (obj = first; obj != NULL; obj = obj->next) {
282 Needed_Entry *needed;
283
284 for (needed = obj->needed; needed != NULL;
285 needed = needed->next) {
286 const char *name = obj->strtab + needed->name;
287 if (!_rtld_load_by_name(name, obj, &needed, mode))
288 status = -1; /* FIXME - cleanup */
289 #ifdef RTLD_LOADER
290 if (status == -1)
291 return status;
292 #endif
293 }
294 }
295
296 return status;
297 }
298
299 #ifdef RTLD_LOADER
300 int
301 _rtld_preload(const char *preload_path)
302 {
303 const char *path;
304 char *cp, *buf;
305 int status = 0;
306
307 if (preload_path != NULL && *preload_path != '\0') {
308 cp = buf = xstrdup(preload_path);
309 while ((path = strsep(&cp, " :")) != NULL && status == 0) {
310 if (!_rtld_load_object(path, RTLD_MAIN))
311 status = -1;
312 else
313 dbg((" preloaded \"%s\"", path));
314 }
315 free(buf);
316 }
317
318 return (status);
319 }
320 #endif
321