ldivmod, uldivmod: fix qdivrem calls
[minix.git] / libexec / ld.elf_so / load.c
blob4ddf854f3b23b8b71f2ed01c00f31ca923a1f071
1 /* $NetBSD: load.c,v 1.42 2010/12/24 12:41:43 skrll 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.42 2010/12/24 12:41:43 skrll 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 #ifdef __minix
61 #define munmap minix_munmap
62 #endif
64 #include "debug.h"
65 #include "rtld.h"
67 static bool _rtld_load_by_name(const char *, Obj_Entry *, Needed_Entry **,
68 int);
70 #ifdef RTLD_LOADER
71 Objlist _rtld_list_main = /* Objects loaded at program startup */
72 SIMPLEQ_HEAD_INITIALIZER(_rtld_list_main);
73 Objlist _rtld_list_global = /* Objects dlopened with RTLD_GLOBAL */
74 SIMPLEQ_HEAD_INITIALIZER(_rtld_list_global);
76 void
77 _rtld_objlist_push_head(Objlist *list, Obj_Entry *obj)
79 Objlist_Entry *elm;
81 elm = NEW(Objlist_Entry);
82 elm->obj = obj;
83 SIMPLEQ_INSERT_HEAD(list, elm, link);
86 void
87 _rtld_objlist_push_tail(Objlist *list, Obj_Entry *obj)
89 Objlist_Entry *elm;
91 elm = NEW(Objlist_Entry);
92 elm->obj = obj;
93 SIMPLEQ_INSERT_TAIL(list, elm, link);
96 Objlist_Entry *
97 _rtld_objlist_find(Objlist *list, const Obj_Entry *obj)
99 Objlist_Entry *elm;
101 SIMPLEQ_FOREACH(elm, list, link) {
102 if (elm->obj == obj)
103 return elm;
105 return NULL;
107 #endif
110 * Load a shared object into memory, if it is not already loaded.
112 * Returns a pointer to the Obj_Entry for the object. Returns NULL
113 * on failure.
115 Obj_Entry *
116 _rtld_load_object(const char *filepath, int flags)
118 Obj_Entry *obj;
119 int fd = -1;
120 struct stat sb;
121 size_t pathlen = strlen(filepath);
123 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
124 if (pathlen == obj->pathlen && !strcmp(obj->path, filepath))
125 break;
128 * If we didn't find a match by pathname, open the file and check
129 * again by device and inode. This avoids false mismatches caused
130 * by multiple links or ".." in pathnames.
132 * To avoid a race, we open the file and use fstat() rather than
133 * using stat().
135 if (obj == NULL) {
136 if ((fd = open(filepath, O_RDONLY)) == -1) {
137 _rtld_error("Cannot open \"%s\"", filepath);
138 return NULL;
140 if (fstat(fd, &sb) == -1) {
141 _rtld_error("Cannot fstat \"%s\"", filepath);
142 close(fd);
143 return NULL;
145 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) {
146 if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
147 close(fd);
148 break;
153 if (obj == NULL) { /* First use of this object, so we must map it in */
154 obj = _rtld_map_object(filepath, fd, &sb);
155 (void)close(fd);
156 if (obj == NULL)
157 return NULL;
158 _rtld_digest_dynamic(filepath, obj);
160 if (flags & _RTLD_DLOPEN) {
161 if (obj->z_noopen || (flags & _RTLD_NOLOAD)) {
162 dbg(("refusing to load non-loadable \"%s\"",
163 obj->path));
164 _rtld_error("Cannot dlopen non-loadable %s",
165 obj->path);
166 munmap(obj->mapbase, obj->mapsize);
167 _rtld_obj_free(obj);
168 return OBJ_ERR;
172 *_rtld_objtail = obj;
173 _rtld_objtail = &obj->next;
174 _rtld_objcount++;
175 _rtld_objloads++;
176 #ifdef RTLD_LOADER
177 _rtld_linkmap_add(obj); /* for GDB */
178 #endif
179 dbg((" %p .. %p: %s", obj->mapbase,
180 obj->mapbase + obj->mapsize - 1, obj->path));
181 if (obj->textrel)
182 dbg((" WARNING: %s has impure text", obj->path));
185 ++obj->refcount;
186 #ifdef RTLD_LOADER
187 if (flags & _RTLD_MAIN && !obj->mainref) {
188 obj->mainref = 1;
189 dbg(("adding %p (%s) to _rtld_list_main", obj, obj->path));
190 _rtld_objlist_push_tail(&_rtld_list_main, obj);
192 if (flags & _RTLD_GLOBAL && !obj->globalref) {
193 obj->globalref = 1;
194 dbg(("adding %p (%s) to _rtld_list_global", obj, obj->path));
195 _rtld_objlist_push_tail(&_rtld_list_global, obj);
197 #endif
198 return obj;
201 static bool
202 _rtld_load_by_name(const char *name, Obj_Entry *obj, Needed_Entry **needed,
203 int flags)
205 #if !defined(__minix)
206 Library_Xform *x = _rtld_xforms;
207 Obj_Entry *o = NULL;
208 size_t j;
209 ssize_t i;
210 #endif /* !defined(__minix) */
211 bool got = false;
212 #if !defined(__minix)
213 union {
214 int i;
215 u_quad_t q;
216 char s[16];
217 } val;
218 #endif /* !defined(__minix) */
220 #if !defined(__minix)
221 dbg(("load by name %s %p", name, x));
222 for (; x; x = x->next) {
223 if (strcmp(x->name, name) != 0)
224 continue;
226 j = sizeof(val);
227 if ((i = _rtld_sysctl(x->ctlname, &val, &j)) == -1) {
228 xwarnx(_PATH_LD_HINTS ": invalid/unknown sysctl for %s (%d)",
229 name, errno);
230 break;
233 switch (i) {
234 case CTLTYPE_QUAD:
235 xsnprintf(val.s, sizeof(val.s), "%" PRIu64, val.q);
236 break;
237 case CTLTYPE_INT:
238 xsnprintf(val.s, sizeof(val.s), "%d", val.i);
239 break;
240 case CTLTYPE_STRING:
241 break;
242 default:
243 xwarnx("unsupported sysctl type %d", (int)i);
244 break;
247 dbg(("sysctl returns %s", val.s));
249 for (i = 0; i < RTLD_MAX_ENTRY && x->entry[i].value != NULL;
250 i++) {
251 dbg(("entry %ld", (unsigned long)i));
252 if (strcmp(x->entry[i].value, val.s) == 0)
253 break;
256 if (i == RTLD_MAX_ENTRY) {
257 xwarnx("sysctl value %s not found for lib%s",
258 val.s, name);
259 break;
262 for (j = 0; j < RTLD_MAX_LIBRARY &&
263 x->entry[i].library[j] != NULL; j++) {
264 o = _rtld_load_library(x->entry[i].library[j], obj,
265 flags);
266 if (o == NULL) {
267 xwarnx("could not load %s for %s",
268 x->entry[i].library[j], name);
269 continue;
271 got = true;
272 if (j == 0)
273 (*needed)->obj = o;
274 else {
275 /* make a new one and put it in the chain */
276 Needed_Entry *ne = xmalloc(sizeof(*ne));
277 ne->name = (*needed)->name;
278 ne->obj = o;
279 ne->next = (*needed)->next;
280 (*needed)->next = ne;
281 *needed = ne;
287 #endif /* !defined(__minix) */
289 if (got)
290 return true;
292 return ((*needed)->obj = _rtld_load_library(name, obj, flags)) != NULL;
297 * Given a shared object, traverse its list of needed objects, and load
298 * each of them. Returns 0 on success. Generates an error message and
299 * returns -1 on failure.
302 _rtld_load_needed_objects(Obj_Entry *first, int flags)
304 Obj_Entry *obj;
305 int status = 0;
307 for (obj = first; obj != NULL; obj = obj->next) {
308 Needed_Entry *needed;
310 for (needed = obj->needed; needed != NULL;
311 needed = needed->next) {
312 const char *name = obj->strtab + needed->name;
313 #ifdef RTLD_LOADER
314 Obj_Entry *nobj;
315 #endif
316 if (!_rtld_load_by_name(name, obj, &needed,
317 flags & ~_RTLD_NOLOAD))
318 status = -1; /* FIXME - cleanup */
319 #ifdef RTLD_LOADER
320 if (status == -1)
321 return status;
323 if (flags & _RTLD_MAIN)
324 continue;
326 nobj = needed->obj;
327 if (nobj->z_nodelete && !obj->ref_nodel) {
328 dbg(("obj %s nodelete", nobj->path));
329 _rtld_ref_dag(nobj);
330 nobj->ref_nodel = true;
332 #endif
336 return status;
339 #ifdef RTLD_LOADER
341 _rtld_preload(const char *preload_path)
343 const char *path;
344 char *cp, *buf;
345 int status = 0;
347 if (preload_path != NULL && *preload_path != '\0') {
348 cp = buf = xstrdup(preload_path);
349 while ((path = strsep(&cp, " :")) != NULL && status == 0) {
350 if (!_rtld_load_object(path, _RTLD_MAIN))
351 status = -1;
352 else
353 dbg((" preloaded \"%s\"", path));
355 xfree(buf);
358 return status;
360 #endif