load.c revision 1.24 1 /* $NetBSD: load.c,v 1.24 2002/10/05 11:59:04 mycroft 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 <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/mman.h>
52 #include <sys/sysctl.h>
53 #include <dirent.h>
54
55 #include "debug.h"
56 #include "rtld.h"
57
58 static bool _rtld_load_by_name __P((const char *, Obj_Entry *, Needed_Entry **,
59 int));
60
61 #ifdef RTLD_LOADER
62 Objlist _rtld_list_main = /* Objects loaded at program startup */
63 SIMPLEQ_HEAD_INITIALIZER(_rtld_list_main);
64 Objlist _rtld_list_global = /* Objects dlopened with RTLD_GLOBAL */
65 SIMPLEQ_HEAD_INITIALIZER(_rtld_list_global);
66
67 void
68 _rtld_objlist_add(list, obj)
69 Objlist *list;
70 Obj_Entry *obj;
71 {
72 Objlist_Entry *elm;
73
74 elm = NEW(Objlist_Entry);
75 elm->obj = obj;
76 SIMPLEQ_INSERT_TAIL(list, elm, link);
77 }
78
79 Objlist_Entry *
80 _rtld_objlist_find(Objlist *list, const Obj_Entry *obj)
81 {
82 Objlist_Entry *elm;
83
84 SIMPLEQ_FOREACH(elm, list, link) {
85 if (elm->obj == obj)
86 return elm;
87 }
88 return NULL;
89 }
90 #endif
91
92 /*
93 * Load a shared object into memory, if it is not already loaded. The
94 * argument must be a string allocated on the heap. This function assumes
95 * responsibility for freeing it when necessary.
96 *
97 * Returns a pointer to the Obj_Entry for the object. Returns NULL
98 * on failure.
99 */
100 Obj_Entry *
101 _rtld_load_object(filepath, mode)
102 char *filepath;
103 int mode;
104 {
105 Obj_Entry *obj;
106 int fd = -1;
107 struct stat sb;
108
109 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
110 if (!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 obj = _rtld_map_object(filepath, fd, &sb);
141 (void)close(fd);
142 if (obj == NULL) {
143 free(filepath);
144 return NULL;
145 }
146 _rtld_digest_dynamic(obj);
147
148 *_rtld_objtail = obj;
149 _rtld_objtail = &obj->next;
150 #ifdef RTLD_LOADER
151 _rtld_linkmap_add(obj); /* for GDB */
152 #endif
153 dbg((" %p .. %p: %s", obj->mapbase,
154 obj->mapbase + obj->mapsize - 1, obj->path));
155 if (obj->textrel)
156 dbg((" WARNING: %s has impure text", obj->path));
157 } else
158 free(filepath);
159
160 ++obj->refcount;
161 #ifdef RTLD_LOADER
162 if (mode & RTLD_MAIN && !obj->mainref) {
163 obj->mainref = 1;
164 rdbg(("adding %p (%s) to _rtld_list_main", obj, obj->path));
165 _rtld_objlist_add(&_rtld_list_main, obj);
166 }
167 if (mode & RTLD_GLOBAL && !obj->globalref) {
168 obj->globalref = 1;
169 rdbg(("adding %p (%s) to _rtld_list_global", obj, obj->path));
170 _rtld_objlist_add(&_rtld_list_global, obj);
171 }
172 #endif
173 return obj;
174 }
175
176 static bool
177 _rtld_load_by_name(name, obj, needed, mode)
178 const char *name;
179 Obj_Entry *obj;
180 Needed_Entry **needed;
181 int mode;
182 {
183 Library_Xform *x = _rtld_xforms;
184 Obj_Entry *o = NULL;
185 size_t i, j;
186 bool got = false;
187 union {
188 int i;
189 char s[16];
190 } val;
191
192 dbg(("load by name %s %p", name, x));
193 for (; x; x = x->next) {
194 if (strcmp(x->name, name) != 0)
195 continue;
196
197 i = sizeof(val);
198
199 if (sysctl(x->ctl, x->ctlmax, &val, &i, NULL, 0) == -1) {
200 xwarnx(_PATH_LD_HINTS ": unknown sysctl for %s", name);
201 break;
202 }
203
204 switch (x->ctltype[x->ctlmax - 1]) {
205 case CTLTYPE_INT:
206 xsnprintf(val.s, sizeof(val.s), "%d", val.i);
207 break;
208 case CTLTYPE_STRING:
209 break;
210 default:
211 xwarnx("unsupported sysctl type %d",
212 x->ctltype[x->ctlmax - 1]);
213 break;
214 }
215
216 dbg(("sysctl returns %s", val.s));
217
218 for (i = 0; i < RTLD_MAX_ENTRY && x->entry[i].value != NULL;
219 i++) {
220 dbg(("entry %ld", (unsigned long)i));
221 if (strcmp(x->entry[i].value, val.s) == 0)
222 break;
223 }
224
225 if (i == RTLD_MAX_ENTRY) {
226 xwarnx("sysctl value %s not found for lib%s",
227 val.s, name);
228 break;
229 }
230 /* XXX: This can mess up debuggers, cause we lie about
231 * what we loaded in the needed objects */
232 for (j = 0; j < RTLD_MAX_LIBRARY &&
233 x->entry[i].library[j] != NULL; j++) {
234 o = _rtld_load_library(x->entry[i].library[j], obj,
235 mode);
236 if (o == NULL) {
237 xwarnx("could not load %s for %s",
238 x->entry[i].library[j], name);
239 continue;
240 }
241 got = true;
242 if (j == 0)
243 (*needed)->obj = o;
244 else {
245 /* make a new one and put it in the chain */
246 Needed_Entry *ne = xmalloc(sizeof(*ne));
247 ne->name = (*needed)->name;
248 ne->obj = o;
249 ne->next = (*needed)->next;
250 (*needed)->next = ne;
251 *needed = ne;
252 }
253
254 }
255
256 }
257
258 if (got)
259 return true;
260
261 return ((*needed)->obj = _rtld_load_library(name, obj, mode)) != NULL;
262 }
263
264
265 /*
266 * Given a shared object, traverse its list of needed objects, and load
267 * each of them. Returns 0 on success. Generates an error message and
268 * returns -1 on failure.
269 */
270 int
271 _rtld_load_needed_objects(first, mode)
272 Obj_Entry *first;
273 int mode;
274 {
275 Obj_Entry *obj;
276 int status = 0;
277
278 for (obj = first; obj != NULL; obj = obj->next) {
279 Needed_Entry *needed;
280
281 for (needed = obj->needed; needed != NULL;
282 needed = needed->next) {
283 const char *name = obj->strtab + needed->name;
284 if (!_rtld_load_by_name(name, obj, &needed, mode))
285 status = -1; /* FIXME - cleanup */
286 #ifdef RTLD_LOADER
287 if (status == -1)
288 return status;
289 #endif
290 }
291 }
292
293 return status;
294 }
295
296 #ifdef RTLD_LOADER
297 int
298 _rtld_preload(preload_path)
299 const char *preload_path;
300 {
301 const char *path;
302 char *cp, *buf;
303 int status = 0;
304
305 if (preload_path != NULL) {
306 cp = buf = xstrdup(preload_path);
307 while ((path = strsep(&cp, " :")) != NULL && status == 0) {
308 if (!_rtld_load_object(xstrdup(path), RTLD_MAIN))
309 status = -1;
310 else
311 dbg((" preloaded \"%s\"", path));
312 }
313 free(buf);
314 }
315
316 return (status);
317 }
318 #endif
319