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