load.c revision 1.6 1 1.6 christos /* $NetBSD: load.c,v 1.6 1999/12/13 09:09:34 christos 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.6 christos static Obj_Entry *_rtld_load_by_name __P((const char *, Obj_Entry *, bool));
58 1.6 christos
59 1.1 cgd /*
60 1.1 cgd * Load a shared object into memory, if it is not already loaded. The
61 1.1 cgd * argument must be a string allocated on the heap. This function assumes
62 1.1 cgd * responsibility for freeing it when necessary.
63 1.1 cgd *
64 1.1 cgd * Returns a pointer to the Obj_Entry for the object. Returns NULL
65 1.1 cgd * on failure.
66 1.1 cgd */
67 1.1 cgd Obj_Entry *
68 1.3 christos _rtld_load_object(filepath, dodebug)
69 1.3 christos char *filepath;
70 1.3 christos bool dodebug;
71 1.1 cgd {
72 1.3 christos Obj_Entry *obj;
73 1.5 mycroft int fd = -1;
74 1.5 mycroft struct stat sb;
75 1.1 cgd
76 1.3 christos for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
77 1.3 christos if (strcmp(obj->path, filepath) == 0)
78 1.3 christos break;
79 1.3 christos
80 1.5 mycroft /*
81 1.5 mycroft * If we didn't find a match by pathname, open the file and check
82 1.5 mycroft * again by device and inode. This avoids false mismatches caused
83 1.5 mycroft * by multiple links or ".." in pathnames.
84 1.5 mycroft *
85 1.5 mycroft * To avoid a race, we open the file and use fstat() rather than
86 1.5 mycroft * using stat().
87 1.5 mycroft */
88 1.5 mycroft if (obj == NULL) {
89 1.3 christos if ((fd = open(filepath, O_RDONLY)) == -1) {
90 1.3 christos _rtld_error("Cannot open \"%s\"", filepath);
91 1.3 christos return NULL;
92 1.3 christos }
93 1.5 mycroft if (fstat(fd, &sb) == -1) {
94 1.5 mycroft _rtld_error("Cannot fstat \"%s\"", filepath);
95 1.5 mycroft close(fd);
96 1.5 mycroft return NULL;
97 1.5 mycroft }
98 1.5 mycroft for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) {
99 1.5 mycroft if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
100 1.5 mycroft close(fd);
101 1.5 mycroft break;
102 1.5 mycroft }
103 1.5 mycroft }
104 1.5 mycroft }
105 1.5 mycroft
106 1.5 mycroft if (obj == NULL) { /* First use of this object, so we must map it in */
107 1.5 mycroft obj = _rtld_map_object(filepath, fd, &sb);
108 1.3 christos (void)close(fd);
109 1.3 christos if (obj == NULL) {
110 1.3 christos free(filepath);
111 1.3 christos return NULL;
112 1.3 christos }
113 1.3 christos obj->path = filepath;
114 1.3 christos _rtld_digest_dynamic(obj);
115 1.1 cgd
116 1.3 christos *_rtld_objtail = obj;
117 1.3 christos _rtld_objtail = &obj->next;
118 1.1 cgd #ifdef RTLD_LOADER
119 1.3 christos _rtld_linkmap_add(obj); /* for GDB */
120 1.1 cgd #endif
121 1.3 christos if (dodebug) {
122 1.3 christos dbg((" %p .. %p: %s", obj->mapbase,
123 1.3 christos obj->mapbase + obj->mapsize - 1, obj->path));
124 1.3 christos if (obj->textrel)
125 1.3 christos dbg((" WARNING: %s has impure text",
126 1.3 christos obj->path));
127 1.3 christos }
128 1.3 christos } else
129 1.3 christos free(filepath);
130 1.1 cgd
131 1.3 christos ++obj->refcount;
132 1.3 christos return obj;
133 1.1 cgd }
134 1.1 cgd
135 1.6 christos static Obj_Entry *
136 1.6 christos _rtld_load_by_name(name, obj, dodebug)
137 1.6 christos const char *name;
138 1.6 christos Obj_Entry *obj;
139 1.6 christos bool dodebug;
140 1.6 christos {
141 1.6 christos Library_Xform *x = _rtld_xforms;
142 1.6 christos Obj_Entry *o = NULL;
143 1.6 christos size_t i, j;
144 1.6 christos int ctlname[2];
145 1.6 christos char *libpath;
146 1.6 christos union {
147 1.6 christos int i;
148 1.6 christos char s[16];
149 1.6 christos } val;
150 1.6 christos
151 1.6 christos ctlname[0] = CTL_MACHDEP;
152 1.6 christos if (dodebug)
153 1.6 christos dbg(("load by name %s %p", name, x));
154 1.6 christos for (; x; x = x->next) {
155 1.6 christos if (strcmp(x->name, name) != 0)
156 1.6 christos continue;
157 1.6 christos
158 1.6 christos ctlname[1] = x->ctl;
159 1.6 christos
160 1.6 christos i = sizeof(val);
161 1.6 christos
162 1.6 christos if (sysctl(ctlname, 2, &val, &i, NULL, 0) == -1) {
163 1.6 christos warn("sysctl");
164 1.6 christos break;
165 1.6 christos }
166 1.6 christos
167 1.6 christos switch (x->ctltype) {
168 1.6 christos case CTLTYPE_INT:
169 1.6 christos xsnprintf(val.s, sizeof(val.s), "%d", val.i);
170 1.6 christos break;
171 1.6 christos case CTLTYPE_STRING:
172 1.6 christos break;
173 1.6 christos default:
174 1.6 christos warnx("unsupport sysctl type %d", x->ctltype);
175 1.6 christos break;
176 1.6 christos }
177 1.6 christos
178 1.6 christos if (dodebug)
179 1.6 christos dbg(("sysctl returns %s", val.s));
180 1.6 christos
181 1.6 christos for (i = 0; i < RTLD_MAX_ENTRY && x->entry[i].value != NULL;
182 1.6 christos i++) {
183 1.6 christos if (dodebug)
184 1.6 christos dbg(("entry %d", i));
185 1.6 christos if (strcmp(x->entry[i].value, val.s) == 0)
186 1.6 christos break;
187 1.6 christos }
188 1.6 christos
189 1.6 christos if (i == RTLD_MAX_ENTRY) {
190 1.6 christos warnx("sysctl value %s not found for lib%s",
191 1.6 christos val.s, name);
192 1.6 christos break;
193 1.6 christos }
194 1.6 christos /* XXX: This can mess up debuggers, cause we lie about
195 1.6 christos * what we loaded in the needed objects */
196 1.6 christos for (j = 0; j < RTLD_MAX_LIBRARY &&
197 1.6 christos x->entry[i].library[j] != NULL; j++) {
198 1.6 christos libpath = _rtld_find_library(
199 1.6 christos x->entry[i].library[j], obj);
200 1.6 christos if (libpath == NULL) {
201 1.6 christos warnx("could not load lib%s for lib%s",
202 1.6 christos x->entry[i].library[j], name);
203 1.6 christos continue;
204 1.6 christos }
205 1.6 christos o = _rtld_load_object(libpath, true);
206 1.6 christos if (o == NULL)
207 1.6 christos continue;
208 1.6 christos }
209 1.6 christos
210 1.6 christos }
211 1.6 christos
212 1.6 christos if (o != NULL)
213 1.6 christos return o;
214 1.6 christos
215 1.6 christos libpath = _rtld_find_library(name, obj);
216 1.6 christos if (libpath == NULL)
217 1.6 christos return NULL;
218 1.6 christos return _rtld_load_object(libpath, true);
219 1.6 christos
220 1.6 christos }
221 1.6 christos
222 1.6 christos
223 1.1 cgd /*
224 1.1 cgd * Given a shared object, traverse its list of needed objects, and load
225 1.1 cgd * each of them. Returns 0 on success. Generates an error message and
226 1.1 cgd * returns -1 on failure.
227 1.1 cgd */
228 1.1 cgd int
229 1.6 christos _rtld_load_needed_objects(first, dodebug)
230 1.3 christos Obj_Entry *first;
231 1.6 christos bool dodebug;
232 1.1 cgd {
233 1.3 christos Obj_Entry *obj;
234 1.3 christos int status = 0;
235 1.1 cgd
236 1.3 christos for (obj = first; obj != NULL; obj = obj->next) {
237 1.3 christos Needed_Entry *needed;
238 1.1 cgd
239 1.3 christos for (needed = obj->needed; needed != NULL;
240 1.3 christos needed = needed->next) {
241 1.3 christos const char *name = obj->strtab + needed->name;
242 1.6 christos needed->obj = _rtld_load_by_name(name, obj, dodebug);
243 1.6 christos if (needed->obj == NULL)
244 1.6 christos status = -1; /* FIXME - cleanup */
245 1.1 cgd #ifdef RTLD_LOADER
246 1.3 christos if (status == -1)
247 1.3 christos return status;
248 1.1 cgd #endif
249 1.3 christos }
250 1.1 cgd }
251 1.1 cgd
252 1.3 christos return status;
253 1.1 cgd }
254 1.4 kleink
255 1.4 kleink #ifdef RTLD_LOADER
256 1.4 kleink int
257 1.4 kleink _rtld_preload(preload_path, dodebug)
258 1.4 kleink const char *preload_path;
259 1.4 kleink bool dodebug;
260 1.4 kleink {
261 1.4 kleink const char *path;
262 1.4 kleink char *cp, *buf;
263 1.4 kleink int status = 0;
264 1.4 kleink
265 1.4 kleink if (preload_path != NULL) {
266 1.4 kleink cp = buf = xstrdup(preload_path);
267 1.4 kleink while ((path = strsep(&cp, " ")) != NULL && status == 0) {
268 1.4 kleink if (_rtld_load_object(xstrdup(path), dodebug) == NULL)
269 1.4 kleink status = -1;
270 1.4 kleink else if (dodebug)
271 1.4 kleink dbg((" preloaded \"%s\"", path));
272 1.4 kleink }
273 1.4 kleink free(buf);
274 1.4 kleink }
275 1.4 kleink
276 1.4 kleink return (status);
277 1.4 kleink }
278 1.4 kleink #endif
279