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