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