1*84d9c625SLionel Sambuc /* $NetBSD: search.c,v 1.24 2013/05/06 08:02:20 skrll Exp $ */
2e83f7ba2SBen Gras
3e83f7ba2SBen Gras /*
4e83f7ba2SBen Gras * Copyright 1996 Matt Thomas <matt@3am-software.com>
5e83f7ba2SBen Gras * All rights reserved.
6e83f7ba2SBen Gras *
7e83f7ba2SBen Gras * Redistribution and use in source and binary forms, with or without
8e83f7ba2SBen Gras * modification, are permitted provided that the following conditions
9e83f7ba2SBen Gras * are met:
10e83f7ba2SBen Gras * 1. Redistributions of source code must retain the above copyright
11e83f7ba2SBen Gras * notice, this list of conditions and the following disclaimer.
12e83f7ba2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13e83f7ba2SBen Gras * notice, this list of conditions and the following disclaimer in the
14e83f7ba2SBen Gras * documentation and/or other materials provided with the distribution.
15e83f7ba2SBen Gras * 3. All advertising materials mentioning features or use of this software
16e83f7ba2SBen Gras * must display the following acknowledgement:
17e83f7ba2SBen Gras * This product includes software developed by John Polstra.
18e83f7ba2SBen Gras * 4. The name of the author may not be used to endorse or promote products
19e83f7ba2SBen Gras * derived from this software without specific prior written permission.
20e83f7ba2SBen Gras *
21e83f7ba2SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22e83f7ba2SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23e83f7ba2SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24e83f7ba2SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25e83f7ba2SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26e83f7ba2SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27e83f7ba2SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28e83f7ba2SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29e83f7ba2SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30e83f7ba2SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31e83f7ba2SBen Gras */
32e83f7ba2SBen Gras
33e83f7ba2SBen Gras /*
34e83f7ba2SBen Gras * Dynamic linker for ELF.
35e83f7ba2SBen Gras *
36e83f7ba2SBen Gras * John Polstra <jdp@polstra.com>.
37e83f7ba2SBen Gras */
38e83f7ba2SBen Gras
39e83f7ba2SBen Gras #include <sys/cdefs.h>
40e83f7ba2SBen Gras #ifndef lint
41*84d9c625SLionel Sambuc __RCSID("$NetBSD: search.c,v 1.24 2013/05/06 08:02:20 skrll Exp $");
42e83f7ba2SBen Gras #endif /* not lint */
43e83f7ba2SBen Gras
44e83f7ba2SBen Gras #include <err.h>
45e83f7ba2SBen Gras #include <errno.h>
46e83f7ba2SBen Gras #include <fcntl.h>
47e83f7ba2SBen Gras #include <stdarg.h>
48e83f7ba2SBen Gras #include <stdio.h>
49e83f7ba2SBen Gras #include <stdlib.h>
50e83f7ba2SBen Gras #include <string.h>
51e83f7ba2SBen Gras #include <unistd.h>
52e83f7ba2SBen Gras #include <sys/types.h>
53e83f7ba2SBen Gras #include <sys/mman.h>
54e83f7ba2SBen Gras #include <sys/stat.h>
55e83f7ba2SBen Gras #include <dirent.h>
56e83f7ba2SBen Gras
57e83f7ba2SBen Gras #include "debug.h"
58e83f7ba2SBen Gras #include "rtld.h"
59e83f7ba2SBen Gras
60e83f7ba2SBen Gras /*
61e83f7ba2SBen Gras * Data declarations.
62e83f7ba2SBen Gras */
63e83f7ba2SBen Gras Search_Path *_rtld_invalid_paths;
64e83f7ba2SBen Gras
65e83f7ba2SBen Gras static Obj_Entry *_rtld_search_library_path(const char *, size_t,
66e83f7ba2SBen Gras const char *, size_t, int);
67e83f7ba2SBen Gras
68e83f7ba2SBen Gras static Obj_Entry *
_rtld_search_library_path(const char * name,size_t namelen,const char * dir,size_t dirlen,int flags)69e83f7ba2SBen Gras _rtld_search_library_path(const char *name, size_t namelen,
70e83f7ba2SBen Gras const char *dir, size_t dirlen, int flags)
71e83f7ba2SBen Gras {
72e83f7ba2SBen Gras char pathname[MAXPATHLEN];
73e83f7ba2SBen Gras size_t pathnamelen;
74e83f7ba2SBen Gras Obj_Entry *obj;
75e83f7ba2SBen Gras Search_Path *sp;
76e83f7ba2SBen Gras
77e83f7ba2SBen Gras pathnamelen = dirlen + 1 + namelen;
78e83f7ba2SBen Gras if (pathnamelen >= sizeof(pathname))
79e83f7ba2SBen Gras return NULL;
80e83f7ba2SBen Gras
81e83f7ba2SBen Gras for (sp = _rtld_invalid_paths; sp != NULL; sp = sp->sp_next) {
82e83f7ba2SBen Gras if (sp->sp_pathlen == pathnamelen &&
83e83f7ba2SBen Gras sp->sp_path[dirlen] == '/' &&
84e83f7ba2SBen Gras !memcmp(name, sp->sp_path + dirlen + 1, namelen) &&
85e83f7ba2SBen Gras !memcmp(dir, sp->sp_path, dirlen)) {
86e83f7ba2SBen Gras return NULL;
87e83f7ba2SBen Gras }
88e83f7ba2SBen Gras }
89e83f7ba2SBen Gras
90e83f7ba2SBen Gras memcpy(pathname, dir, dirlen);
91e83f7ba2SBen Gras pathname[dirlen] = '/';
92e83f7ba2SBen Gras memcpy(pathname + dirlen + 1, name, namelen);
93e83f7ba2SBen Gras pathname[pathnamelen] = '\0';
94e83f7ba2SBen Gras
95e83f7ba2SBen Gras dbg((" Trying \"%s\"", pathname));
96e83f7ba2SBen Gras obj = _rtld_load_object(pathname, flags);
97e83f7ba2SBen Gras if (obj == NULL) {
98e83f7ba2SBen Gras Search_Path *path;
99e83f7ba2SBen Gras
100e83f7ba2SBen Gras path = NEW(Search_Path);
101e83f7ba2SBen Gras path->sp_pathlen = pathnamelen;
102e83f7ba2SBen Gras path->sp_path = xstrdup(pathname);
103e83f7ba2SBen Gras path->sp_next = _rtld_invalid_paths;
104e83f7ba2SBen Gras _rtld_invalid_paths = path;
105e83f7ba2SBen Gras }
106e83f7ba2SBen Gras return obj;
107e83f7ba2SBen Gras }
108e83f7ba2SBen Gras
109e83f7ba2SBen Gras /*
110e83f7ba2SBen Gras * Find the library with the given name, and return its full pathname.
111e83f7ba2SBen Gras * The returned string is dynamically allocated. Generates an error
112e83f7ba2SBen Gras * message and returns NULL if the library cannot be found.
113e83f7ba2SBen Gras *
114e83f7ba2SBen Gras * If the second argument is non-NULL, then it refers to an already-
115e83f7ba2SBen Gras * loaded shared object, whose library search path will be searched.
116e83f7ba2SBen Gras */
117e83f7ba2SBen Gras Obj_Entry *
_rtld_load_library(const char * name,const Obj_Entry * refobj,int flags)118e83f7ba2SBen Gras _rtld_load_library(const char *name, const Obj_Entry *refobj, int flags)
119e83f7ba2SBen Gras {
120e83f7ba2SBen Gras char tmperror[512], *tmperrorp;
121e83f7ba2SBen Gras Search_Path *sp;
122e83f7ba2SBen Gras const char *pathname;
123e83f7ba2SBen Gras int namelen;
124e83f7ba2SBen Gras Obj_Entry *obj;
125e83f7ba2SBen Gras
126e83f7ba2SBen Gras if (strchr(name, '/') != NULL) { /* Hard coded pathname */
127e83f7ba2SBen Gras if (name[0] != '/' && !_rtld_trust) {
128e83f7ba2SBen Gras _rtld_error(
129e83f7ba2SBen Gras "absolute pathname required for shared object \"%s\"",
130e83f7ba2SBen Gras name);
131e83f7ba2SBen Gras return NULL;
132e83f7ba2SBen Gras }
133e83f7ba2SBen Gras pathname = name;
134e83f7ba2SBen Gras goto found;
135e83f7ba2SBen Gras }
136e83f7ba2SBen Gras dbg((" Searching for \"%s\" (%p)", name, refobj));
137e83f7ba2SBen Gras
138e83f7ba2SBen Gras tmperrorp = dlerror();
139e83f7ba2SBen Gras if (tmperrorp != NULL) {
140e83f7ba2SBen Gras strncpy(tmperror, tmperrorp, sizeof tmperror);
141e83f7ba2SBen Gras tmperrorp = tmperror;
142e83f7ba2SBen Gras }
143e83f7ba2SBen Gras
144e83f7ba2SBen Gras namelen = strlen(name);
145e83f7ba2SBen Gras
146e83f7ba2SBen Gras for (sp = _rtld_paths; sp != NULL; sp = sp->sp_next)
147e83f7ba2SBen Gras if ((obj = _rtld_search_library_path(name, namelen,
148e83f7ba2SBen Gras sp->sp_path, sp->sp_pathlen, flags)) != NULL)
149e83f7ba2SBen Gras goto pathfound;
150e83f7ba2SBen Gras
151e83f7ba2SBen Gras if (refobj != NULL)
152e83f7ba2SBen Gras for (sp = refobj->rpaths; sp != NULL; sp = sp->sp_next)
153e83f7ba2SBen Gras if ((obj = _rtld_search_library_path(name,
154e83f7ba2SBen Gras namelen, sp->sp_path, sp->sp_pathlen, flags)) != NULL)
155e83f7ba2SBen Gras goto pathfound;
156e83f7ba2SBen Gras
157e83f7ba2SBen Gras for (sp = _rtld_default_paths; sp != NULL; sp = sp->sp_next)
158e83f7ba2SBen Gras if ((obj = _rtld_search_library_path(name, namelen,
159e83f7ba2SBen Gras sp->sp_path, sp->sp_pathlen, flags)) != NULL)
160e83f7ba2SBen Gras goto pathfound;
161e83f7ba2SBen Gras
162e83f7ba2SBen Gras _rtld_error("Shared object \"%s\" not found", name);
163e83f7ba2SBen Gras return NULL;
164e83f7ba2SBen Gras
165e83f7ba2SBen Gras pathfound:
166e83f7ba2SBen Gras /*
167e83f7ba2SBen Gras * The library has been found, but it couldn't be loaded for some
168e83f7ba2SBen Gras * reason.
169e83f7ba2SBen Gras */
170e83f7ba2SBen Gras if (obj == OBJ_ERR)
171e83f7ba2SBen Gras return NULL;
172e83f7ba2SBen Gras /*
173e83f7ba2SBen Gras * Successfully found a library; restore the dlerror state as it was
174e83f7ba2SBen Gras * before _rtld_load_library() was called (any failed call to
175e83f7ba2SBen Gras * _rtld_search_library_path() will set the dlerror state, but if the
176e83f7ba2SBen Gras * library was eventually found, then the error state should not
177e83f7ba2SBen Gras * change.
178e83f7ba2SBen Gras */
179e83f7ba2SBen Gras if (tmperrorp)
180e83f7ba2SBen Gras _rtld_error("%s", tmperror);
181e83f7ba2SBen Gras else
182e83f7ba2SBen Gras (void)dlerror();
183e83f7ba2SBen Gras return obj;
184e83f7ba2SBen Gras
185e83f7ba2SBen Gras found:
186e83f7ba2SBen Gras obj = _rtld_load_object(pathname, flags);
187e83f7ba2SBen Gras if (obj == OBJ_ERR)
188e83f7ba2SBen Gras return NULL;
189e83f7ba2SBen Gras
190e83f7ba2SBen Gras return obj;
191e83f7ba2SBen Gras }
192e83f7ba2SBen Gras
193