1 /* $NetBSD: search.c,v 1.20 2006/03/18 02:34:30 matt Exp $ */
4 * Copyright 1996 Matt Thomas <matt@3am-software.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 * Dynamic linker for ELF.
36 * John Polstra <jdp@polstra.com>.
39 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: search.c,v 1.20 2006/03/18 02:34:30 matt Exp $");
52 #include <sys/types.h>
63 Search_Path
*_rtld_invalid_paths
;
65 static Obj_Entry
*_rtld_search_library_path(const char *, size_t,
66 const char *, size_t, int);
69 _rtld_search_library_path(const char *name
, size_t namelen
,
70 const char *dir
, size_t dirlen
, int mode
)
72 char pathname
[MAXPATHLEN
];
77 pathnamelen
= dirlen
+ 1 + namelen
;
78 if (pathnamelen
>= sizeof(pathname
))
81 for (sp
= _rtld_invalid_paths
; sp
!= NULL
; sp
= sp
->sp_next
) {
82 if (sp
->sp_pathlen
== pathnamelen
&&
83 !memcmp(name
, sp
->sp_path
+ dirlen
+ 1, namelen
) &&
84 !memcmp(dir
, sp
->sp_path
, dirlen
)) {
89 (void)strncpy(pathname
, dir
, dirlen
);
90 pathname
[dirlen
] = '/';
91 strcpy(pathname
+ dirlen
+ 1, name
);
93 dbg((" Trying \"%s\"", pathname
));
94 obj
= _rtld_load_object(pathname
, mode
);
98 path
= NEW(Search_Path
);
99 path
->sp_pathlen
= pathnamelen
;
100 path
->sp_path
= xstrdup(pathname
);
101 path
->sp_next
= _rtld_invalid_paths
;
102 _rtld_invalid_paths
= path
;
108 * Find the library with the given name, and return its full pathname.
109 * The returned string is dynamically allocated. Generates an error
110 * message and returns NULL if the library cannot be found.
112 * If the second argument is non-NULL, then it refers to an already-
113 * loaded shared object, whose library search path will be searched.
116 _rtld_load_library(const char *name
, const Obj_Entry
*refobj
, int mode
)
118 char tmperror
[512], *tmperrorp
;
120 const char *pathname
;
124 if (strchr(name
, '/') != NULL
) { /* Hard coded pathname */
125 if (name
[0] != '/' && !_rtld_trust
) {
127 "absolute pathname required for shared object \"%s\"",
134 dbg((" Searching for \"%s\" (%p)", name
, refobj
));
136 tmperrorp
= dlerror();
137 if (tmperrorp
!= NULL
) {
138 strncpy(tmperror
, tmperrorp
, sizeof tmperror
);
139 tmperrorp
= tmperror
;
142 namelen
= strlen(name
);
144 for (sp
= _rtld_paths
; sp
!= NULL
; sp
= sp
->sp_next
)
145 if ((obj
= _rtld_search_library_path(name
, namelen
,
146 sp
->sp_path
, sp
->sp_pathlen
, mode
)) != NULL
)
150 for (sp
= refobj
->rpaths
; sp
!= NULL
; sp
= sp
->sp_next
)
151 if ((obj
= _rtld_search_library_path(name
,
152 namelen
, sp
->sp_path
, sp
->sp_pathlen
, mode
)) != NULL
)
155 for (sp
= _rtld_default_paths
; sp
!= NULL
; sp
= sp
->sp_next
)
156 if ((obj
= _rtld_search_library_path(name
, namelen
,
157 sp
->sp_path
, sp
->sp_pathlen
, mode
)) != NULL
)
160 _rtld_error("Shared object \"%s\" not found", name
);
165 * Successfully found a library; restore the dlerror state as it was
166 * before _rtld_load_library() was called (any failed call to
167 * _rtld_search_library_path() will set the dlerror state, but if the
168 * library was eventually found, then the error state should not
172 _rtld_error("%s", tmperror
);
178 return _rtld_load_object(pathname
, mode
);