Install sources with MKSRC=yes
[minix3.git] / libexec / ld.elf_so / load.c
blob7b8c359eae1692e7a85bb4a1a4e991503f0ebed2
1 /* $NetBSD: load.c,v 1.47 2013/11/27 18:01:33 christos Exp $ */
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt@3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root@ihack.net>
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by John Polstra.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Dynamic linker for ELF.
38 * John Polstra <jdp@polstra.com>.
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: load.c,v 1.47 2013/11/27 18:01:33 christos Exp $");
44 #endif /* not lint */
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <sys/types.h>
55 #include <sys/param.h>
56 #include <sys/mman.h>
57 #include <sys/sysctl.h>
58 #include <dirent.h>
60 #include "debug.h"
61 #include "rtld.h"
63 static bool _rtld_load_by_name(const char *, Obj_Entry *, Needed_Entry **,
64 int);
66 #ifdef RTLD_LOADER
67 Objlist _rtld_list_main = /* Objects loaded at program startup */
68 SIMPLEQ_HEAD_INITIALIZER(_rtld_list_main);
69 Objlist _rtld_list_global = /* Objects dlopened with RTLD_GLOBAL */
70 SIMPLEQ_HEAD_INITIALIZER(_rtld_list_global);
72 void
73 _rtld_objlist_push_head(Objlist *list, Obj_Entry *obj)
75 Objlist_Entry *elm;
77 elm = NEW(Objlist_Entry);
78 elm->obj = obj;
79 SIMPLEQ_INSERT_HEAD(list, elm, link);
82 void
83 _rtld_objlist_push_tail(Objlist *list, Obj_Entry *obj)
85 Objlist_Entry *elm;
87 elm = NEW(Objlist_Entry);
88 elm->obj = obj;
89 SIMPLEQ_INSERT_TAIL(list, elm, link);
92 Objlist_Entry *
93 _rtld_objlist_find(Objlist *list, const Obj_Entry *obj)
95 Objlist_Entry *elm;
97 SIMPLEQ_FOREACH(elm, list, link) {
98 if (elm->obj == obj)
99 return elm;
101 return NULL;
103 #endif
106 * Load a shared object into memory, if it is not already loaded.
108 * Returns a pointer to the Obj_Entry for the object. Returns NULL
109 * on failure.
111 Obj_Entry *
112 _rtld_load_object(const char *filepath, int flags)
114 Obj_Entry *obj;
115 int fd = -1;
116 struct stat sb;
117 size_t pathlen = strlen(filepath);
119 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
120 if (pathlen == obj->pathlen && !strcmp(obj->path, filepath))
121 break;
124 * If we didn't find a match by pathname, open the file and check
125 * again by device and inode. This avoids false mismatches caused
126 * by multiple links or ".." in pathnames.
128 * To avoid a race, we open the file and use fstat() rather than
129 * using stat().
131 if (obj == NULL) {
132 if ((fd = open(filepath, O_RDONLY)) == -1) {
133 _rtld_error("Cannot open \"%s\"", filepath);
134 return NULL;
136 if (fstat(fd, &sb) == -1) {
137 _rtld_error("Cannot fstat \"%s\"", filepath);
138 close(fd);
139 return NULL;
141 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) {
142 if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
143 close(fd);
144 break;
149 #ifdef RTLD_LOADER
150 if (pathlen == _rtld_objself.pathlen &&
151 strcmp(_rtld_objself.path, filepath) == 0) {
152 close(fd);
153 return &_rtld_objself;
155 #endif
157 if (obj == NULL) { /* First use of this object, so we must map it in */
158 obj = _rtld_map_object(filepath, fd, &sb);
159 (void)close(fd);
160 if (obj == NULL)
161 return NULL;
162 _rtld_digest_dynamic(filepath, obj);
164 if (flags & _RTLD_DLOPEN) {
165 if (obj->z_noopen || (flags & _RTLD_NOLOAD)) {
166 dbg(("refusing to load non-loadable \"%s\"",
167 obj->path));
168 _rtld_error("Cannot dlopen non-loadable %s",
169 obj->path);
170 munmap(obj->mapbase, obj->mapsize);
171 _rtld_obj_free(obj);
172 return OBJ_ERR;
176 *_rtld_objtail = obj;
177 _rtld_objtail = &obj->next;
178 _rtld_objcount++;
179 _rtld_objloads++;
180 #ifdef RTLD_LOADER
181 _rtld_linkmap_add(obj); /* for GDB */
182 #endif
183 dbg((" %p .. %p: %s", obj->mapbase,
184 obj->mapbase + obj->mapsize - 1, obj->path));
185 if (obj->textrel)
186 dbg((" WARNING: %s has impure text", obj->path));
189 ++obj->refcount;
190 #ifdef RTLD_LOADER
191 if (flags & _RTLD_MAIN && !obj->mainref) {
192 obj->mainref = 1;
193 dbg(("adding %p (%s) to _rtld_list_main", obj, obj->path));
194 _rtld_objlist_push_tail(&_rtld_list_main, obj);
196 if (flags & _RTLD_GLOBAL && !obj->globalref) {
197 obj->globalref = 1;
198 dbg(("adding %p (%s) to _rtld_list_global", obj, obj->path));
199 _rtld_objlist_push_tail(&_rtld_list_global, obj);
201 #endif
202 return obj;
205 static bool
206 _rtld_load_by_name(const char *name, Obj_Entry *obj, Needed_Entry **needed,
207 int flags)
209 #if !defined(__minix)
210 Library_Xform *x = _rtld_xforms;
211 #endif /* !defined(__minix) */
212 Obj_Entry *o;
213 #if !defined(__minix)
214 size_t j;
215 ssize_t i;
216 #endif /* !defined(__minix) */
217 bool got = false;
218 #if !defined(__minix)
219 union {
220 int i;
221 u_quad_t q;
222 char s[16];
223 } val;
225 dbg(("load by name %s %p", name, x));
226 #endif /* !defined(__minix) */
227 for (o = _rtld_objlist->next; o != NULL; o = o->next)
228 if (_rtld_object_match_name(o, name)) {
229 ++o->refcount;
230 (*needed)->obj = o;
231 return true;
234 #if !defined(__minix)
235 for (; x; x = x->next) {
236 if (strcmp(x->name, name) != 0)
237 continue;
239 j = sizeof(val);
240 if ((i = _rtld_sysctl(x->ctlname, &val, &j)) == -1) {
241 xwarnx(_PATH_LD_HINTS ": invalid/unknown sysctl for %s (%d)",
242 name, errno);
243 break;
246 switch (i) {
247 case CTLTYPE_QUAD:
248 xsnprintf(val.s, sizeof(val.s), "%" PRIu64, val.q);
249 break;
250 case CTLTYPE_INT:
251 xsnprintf(val.s, sizeof(val.s), "%d", val.i);
252 break;
253 case CTLTYPE_STRING:
254 break;
255 default:
256 xwarnx("unsupported sysctl type %d", (int)i);
257 break;
260 dbg(("sysctl returns %s", val.s));
262 for (i = 0; i < RTLD_MAX_ENTRY && x->entry[i].value != NULL;
263 i++) {
264 dbg(("entry %ld", (unsigned long)i));
265 if (strcmp(x->entry[i].value, val.s) == 0)
266 break;
269 if (i == RTLD_MAX_ENTRY) {
270 xwarnx("sysctl value %s not found for lib%s",
271 val.s, name);
272 break;
275 for (j = 0; j < RTLD_MAX_LIBRARY &&
276 x->entry[i].library[j] != NULL; j++) {
277 o = _rtld_load_library(x->entry[i].library[j], obj,
278 flags);
279 if (o == NULL) {
280 xwarnx("could not load %s for %s",
281 x->entry[i].library[j], name);
282 continue;
284 got = true;
285 if (j == 0)
286 (*needed)->obj = o;
287 else {
288 /* make a new one and put it in the chain */
289 Needed_Entry *ne = xmalloc(sizeof(*ne));
290 ne->name = (*needed)->name;
291 ne->obj = o;
292 ne->next = (*needed)->next;
293 (*needed)->next = ne;
294 *needed = ne;
300 #endif /* !defined(__minix) */
302 if (got)
303 return true;
305 return ((*needed)->obj = _rtld_load_library(name, obj, flags)) != NULL;
310 * Given a shared object, traverse its list of needed objects, and load
311 * each of them. Returns 0 on success. Generates an error message and
312 * returns -1 on failure.
315 _rtld_load_needed_objects(Obj_Entry *first, int flags)
317 Obj_Entry *obj;
318 int status = 0;
320 for (obj = first; obj != NULL; obj = obj->next) {
321 Needed_Entry *needed;
323 for (needed = obj->needed; needed != NULL;
324 needed = needed->next) {
325 const char *name = obj->strtab + needed->name;
326 #ifdef RTLD_LOADER
327 Obj_Entry *nobj;
328 #endif
329 if (!_rtld_load_by_name(name, obj, &needed,
330 flags & ~_RTLD_NOLOAD))
331 status = -1; /* FIXME - cleanup */
332 #ifdef RTLD_LOADER
333 if (status == -1)
334 return status;
336 if (flags & _RTLD_MAIN)
337 continue;
339 nobj = needed->obj;
340 if (nobj->z_nodelete && !obj->ref_nodel) {
341 dbg(("obj %s nodelete", nobj->path));
342 _rtld_ref_dag(nobj);
343 nobj->ref_nodel = true;
345 #endif
349 return status;
352 #ifdef RTLD_LOADER
354 _rtld_preload(const char *preload_path)
356 const char *path;
357 char *cp, *buf;
358 int status = 0;
360 if (preload_path != NULL && *preload_path != '\0') {
361 cp = buf = xstrdup(preload_path);
362 while ((path = strsep(&cp, " :")) != NULL && status == 0) {
363 if (!_rtld_load_object(path, _RTLD_MAIN))
364 status = -1;
365 else
366 dbg((" preloaded \"%s\"", path));
368 xfree(buf);
371 return status;
373 #endif