load.c revision 1.5 1 /* $NetBSD: load.c,v 1.5 1999/11/07 00:21:12 mycroft Exp $ */
2
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by John Polstra.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Dynamic linker for ELF.
36 *
37 * John Polstra <jdp (at) polstra.com>.
38 */
39
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <sys/types.h>
49 #include <sys/mman.h>
50 #include <dirent.h>
51
52 #include "debug.h"
53 #include "rtld.h"
54
55 /*
56 * Load a shared object into memory, if it is not already loaded. The
57 * argument must be a string allocated on the heap. This function assumes
58 * responsibility for freeing it when necessary.
59 *
60 * Returns a pointer to the Obj_Entry for the object. Returns NULL
61 * on failure.
62 */
63 Obj_Entry *
64 _rtld_load_object(filepath, dodebug)
65 char *filepath;
66 bool dodebug;
67 {
68 Obj_Entry *obj;
69 int fd = -1;
70 struct stat sb;
71
72 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
73 if (strcmp(obj->path, filepath) == 0)
74 break;
75
76 /*
77 * If we didn't find a match by pathname, open the file and check
78 * again by device and inode. This avoids false mismatches caused
79 * by multiple links or ".." in pathnames.
80 *
81 * To avoid a race, we open the file and use fstat() rather than
82 * using stat().
83 */
84 if (obj == NULL) {
85 if ((fd = open(filepath, O_RDONLY)) == -1) {
86 _rtld_error("Cannot open \"%s\"", filepath);
87 return NULL;
88 }
89 if (fstat(fd, &sb) == -1) {
90 _rtld_error("Cannot fstat \"%s\"", filepath);
91 close(fd);
92 return NULL;
93 }
94 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) {
95 if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
96 close(fd);
97 break;
98 }
99 }
100 }
101
102 if (obj == NULL) { /* First use of this object, so we must map it in */
103 obj = _rtld_map_object(filepath, fd, &sb);
104 (void)close(fd);
105 if (obj == NULL) {
106 free(filepath);
107 return NULL;
108 }
109 obj->path = filepath;
110 _rtld_digest_dynamic(obj);
111
112 *_rtld_objtail = obj;
113 _rtld_objtail = &obj->next;
114 #ifdef RTLD_LOADER
115 _rtld_linkmap_add(obj); /* for GDB */
116 #endif
117 if (dodebug) {
118 dbg((" %p .. %p: %s", obj->mapbase,
119 obj->mapbase + obj->mapsize - 1, obj->path));
120 if (obj->textrel)
121 dbg((" WARNING: %s has impure text",
122 obj->path));
123 }
124 } else
125 free(filepath);
126
127 ++obj->refcount;
128 return obj;
129 }
130
131 /*
132 * Given a shared object, traverse its list of needed objects, and load
133 * each of them. Returns 0 on success. Generates an error message and
134 * returns -1 on failure.
135 */
136 int
137 _rtld_load_needed_objects(first)
138 Obj_Entry *first;
139 {
140 Obj_Entry *obj;
141 int status = 0;
142
143 for (obj = first; obj != NULL; obj = obj->next) {
144 Needed_Entry *needed;
145
146 for (needed = obj->needed; needed != NULL;
147 needed = needed->next) {
148 const char *name = obj->strtab + needed->name;
149 char *libpath = _rtld_find_library(name, obj);
150
151 if (libpath == NULL) {
152 status = -1;
153 } else {
154 needed->obj = _rtld_load_object(libpath, true);
155 if (needed->obj == NULL)
156 status = -1; /* FIXME - cleanup */
157 }
158 #ifdef RTLD_LOADER
159 if (status == -1)
160 return status;
161 #endif
162 }
163 }
164
165 return status;
166 }
167
168 #ifdef RTLD_LOADER
169 int
170 _rtld_preload(preload_path, dodebug)
171 const char *preload_path;
172 bool dodebug;
173 {
174 const char *path;
175 char *cp, *buf;
176 int status = 0;
177
178 if (preload_path != NULL) {
179 cp = buf = xstrdup(preload_path);
180 while ((path = strsep(&cp, " ")) != NULL && status == 0) {
181 if (_rtld_load_object(xstrdup(path), dodebug) == NULL)
182 status = -1;
183 else if (dodebug)
184 dbg((" preloaded \"%s\"", path));
185 }
186 free(buf);
187 }
188
189 return (status);
190 }
191 #endif
192