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