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