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