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