search.c revision 1.3 1 1.3 cgd /* $NetBSD: search.c,v 1.3 1997/02/17 19:32:05 cgd Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by John Polstra.
18 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
19 1.1 cgd * derived from this software without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 cgd */
32 1.1 cgd
33 1.1 cgd /*
34 1.1 cgd * Dynamic linker for ELF.
35 1.1 cgd *
36 1.1 cgd * John Polstra <jdp (at) polstra.com>.
37 1.1 cgd */
38 1.1 cgd
39 1.1 cgd #include <err.h>
40 1.1 cgd #include <errno.h>
41 1.1 cgd #include <fcntl.h>
42 1.1 cgd #include <stdarg.h>
43 1.1 cgd #include <stdio.h>
44 1.1 cgd #include <stdlib.h>
45 1.1 cgd #include <string.h>
46 1.1 cgd #include <unistd.h>
47 1.1 cgd #include <sys/types.h>
48 1.1 cgd #include <sys/mman.h>
49 1.1 cgd #include <sys/stat.h>
50 1.1 cgd #include <dirent.h>
51 1.1 cgd
52 1.1 cgd #include "debug.h"
53 1.1 cgd #include "rtld.h"
54 1.1 cgd
55 1.1 cgd #define CONCAT(x,y) __CONCAT(x,y)
56 1.1 cgd #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
57 1.1 cgd #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
58 1.1 cgd #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
59 1.1 cgd #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
60 1.1 cgd
61 1.1 cgd /*
62 1.1 cgd * Data declarations.
63 1.1 cgd */
64 1.1 cgd
65 1.1 cgd static bool
66 1.1 cgd _rtld_check_library(
67 1.3 cgd const char *pathname)
68 1.1 cgd {
69 1.1 cgd struct stat mystat;
70 1.1 cgd Elf_Ehdr ehdr;
71 1.1 cgd int fd;
72 1.1 cgd
73 1.3 cgd if (stat(pathname, &mystat) >= 0 && S_ISREG(mystat.st_mode)) {
74 1.3 cgd if ((fd = open(pathname, O_RDONLY)) >= 0) {
75 1.1 cgd if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
76 1.1 cgd goto lose;
77 1.1 cgd
78 1.1 cgd /* Elf_e_ident includes class */
79 1.1 cgd if (memcmp(Elf_e_ident, ehdr.e_ident, Elf_e_siz) != 0)
80 1.1 cgd goto lose;
81 1.1 cgd
82 1.1 cgd switch (ehdr.e_machine) {
83 1.1 cgd ELFDEFNNAME(MACHDEP_ID_CASES)
84 1.1 cgd
85 1.1 cgd default:
86 1.1 cgd goto lose;
87 1.1 cgd }
88 1.1 cgd
89 1.1 cgd if (ehdr.e_ident[Elf_ei_version] != Elf_ev_current ||
90 1.1 cgd ehdr.e_version != Elf_ev_current ||
91 1.1 cgd ehdr.e_ident[Elf_ei_data] != ELFDEFNNAME(MACHDEP_ENDIANNESS) ||
92 1.1 cgd ehdr.e_type != Elf_et_dyn)
93 1.1 cgd goto lose;
94 1.1 cgd
95 1.2 cgd close(fd);
96 1.1 cgd return true;
97 1.1 cgd
98 1.1 cgd lose:
99 1.1 cgd close(fd);
100 1.1 cgd }
101 1.1 cgd }
102 1.1 cgd
103 1.1 cgd return false;
104 1.1 cgd }
105 1.1 cgd
106 1.1 cgd
107 1.1 cgd static char *
108 1.3 cgd _rtld_search_library_path(const char *name, int namelen, const char *dir, int dirlen)
109 1.1 cgd {
110 1.3 cgd char *pathname;
111 1.1 cgd
112 1.3 cgd pathname = xmalloc(dirlen + 1 + namelen + 1);
113 1.3 cgd strncpy(pathname, dir, dirlen);
114 1.3 cgd pathname[dirlen] = '/';
115 1.3 cgd strcpy(pathname + dirlen + 1, name);
116 1.3 cgd
117 1.3 cgd dbg(" Trying \"%s\"", pathname);
118 1.3 cgd if(_rtld_check_library(pathname)) /* We found it */
119 1.3 cgd return pathname;
120 1.1 cgd
121 1.3 cgd free(pathname);
122 1.3 cgd return NULL;
123 1.1 cgd }
124 1.1 cgd /*
125 1.1 cgd * Find the library with the given name, and return its full pathname.
126 1.1 cgd * The returned string is dynamically allocated. Generates an error
127 1.1 cgd * message and returns NULL if the library cannot be found.
128 1.1 cgd *
129 1.1 cgd * If the second argument is non-NULL, then it refers to an already-
130 1.1 cgd * loaded shared object, whose library search path will be searched.
131 1.1 cgd */
132 1.1 cgd char *
133 1.1 cgd _rtld_find_library(
134 1.1 cgd const char *name,
135 1.1 cgd const Obj_Entry *refobj)
136 1.1 cgd {
137 1.3 cgd Search_Path *sp;
138 1.1 cgd char *pathname;
139 1.3 cgd int namelen;
140 1.1 cgd
141 1.1 cgd if (strchr(name, '/') != NULL) { /* Hard coded pathname */
142 1.1 cgd if (name[0] != '/' && !_rtld_trust) {
143 1.1 cgd _rtld_error("Absolute pathname required for shared object \"%s\"",
144 1.1 cgd name);
145 1.1 cgd return NULL;
146 1.1 cgd }
147 1.1 cgd #ifdef SVR4_LIBDIR
148 1.1 cgd if (strncmp(name, SVR4_LIBDIR, SVR4_LIBDIRLEN) == 0
149 1.1 cgd && name[SVR4_LIBDIRLEN] == '/') { /* In "/usr/lib" */
150 1.1 cgd /* Map hard-coded "/usr/lib" onto our ELF library directory. */
151 1.1 cgd pathname = xmalloc(strlen(name) + LIBDIRLEN - SVR4_LIBDIRLEN + 1);
152 1.1 cgd strcpy(pathname, LIBDIR);
153 1.1 cgd strcpy(pathname + LIBDIRLEN, name + SVR4_LIBDIRLEN);
154 1.1 cgd return pathname;
155 1.1 cgd }
156 1.1 cgd #endif /* SVR4_LIBDIR */
157 1.1 cgd return xstrdup(name);
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd dbg(" Searching for \"%s\" (%p)", name, refobj);
161 1.1 cgd
162 1.3 cgd namelen = strlen(name);
163 1.3 cgd
164 1.3 cgd if (refobj != NULL)
165 1.3 cgd for (sp = refobj->rpaths; sp != NULL; sp = sp->sp_next)
166 1.3 cgd if ((pathname = _rtld_search_library_path(name, namelen,
167 1.3 cgd sp->sp_path, sp->sp_pathlen)) != NULL)
168 1.3 cgd return (pathname);
169 1.3 cgd
170 1.3 cgd for (sp = _rtld_paths; sp != NULL; sp = sp->sp_next)
171 1.3 cgd if ((pathname = _rtld_search_library_path(name, namelen,
172 1.3 cgd sp->sp_path, sp->sp_pathlen)) != NULL)
173 1.3 cgd return (pathname);
174 1.3 cgd
175 1.3 cgd #if 0
176 1.3 cgd if((refobj != NULL &&
177 1.3 cgd (pathname = _rtld_search_library_path(name, refobj->rpath)) != NULL) ||
178 1.3 cgd (pathname = _rtld_search_library_path(name, ld_library_path)) != NULL
179 1.3 cgd #ifdef SVR4_LIBDIR
180 1.3 cgd LOSE!
181 1.3 cgd || (pathname = _rtld_search_library_path(name, SVR4_LIBDIR)) != NULL
182 1.3 cgd #endif
183 1.3 cgd )
184 1.3 cgd return pathname;
185 1.3 cgd #endif
186 1.3 cgd
187 1.3 cgd _rtld_error("Shared object \"%s\" not found", name);
188 1.3 cgd return NULL;
189 1.1 cgd }
190