load.c revision 1.4 1 1.4 kleink /* $NetBSD: load.c,v 1.4 1999/05/31 14:48:16 kleink 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.1 cgd
70 1.3 christos for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
71 1.3 christos if (strcmp(obj->path, filepath) == 0)
72 1.3 christos break;
73 1.3 christos
74 1.3 christos if (obj == NULL) { /* First use of this object, so we must map it in */
75 1.3 christos int fd;
76 1.3 christos
77 1.3 christos if ((fd = open(filepath, O_RDONLY)) == -1) {
78 1.3 christos _rtld_error("Cannot open \"%s\"", filepath);
79 1.3 christos return NULL;
80 1.3 christos }
81 1.3 christos obj = _rtld_map_object(filepath, fd);
82 1.3 christos (void)close(fd);
83 1.3 christos if (obj == NULL) {
84 1.3 christos free(filepath);
85 1.3 christos return NULL;
86 1.3 christos }
87 1.3 christos obj->path = filepath;
88 1.3 christos _rtld_digest_dynamic(obj);
89 1.1 cgd
90 1.3 christos *_rtld_objtail = obj;
91 1.3 christos _rtld_objtail = &obj->next;
92 1.1 cgd #ifdef RTLD_LOADER
93 1.3 christos _rtld_linkmap_add(obj); /* for GDB */
94 1.1 cgd #endif
95 1.3 christos if (dodebug) {
96 1.3 christos dbg((" %p .. %p: %s", obj->mapbase,
97 1.3 christos obj->mapbase + obj->mapsize - 1, obj->path));
98 1.3 christos if (obj->textrel)
99 1.3 christos dbg((" WARNING: %s has impure text",
100 1.3 christos obj->path));
101 1.3 christos }
102 1.3 christos } else
103 1.3 christos free(filepath);
104 1.1 cgd
105 1.3 christos ++obj->refcount;
106 1.3 christos return obj;
107 1.1 cgd }
108 1.1 cgd
109 1.1 cgd /*
110 1.1 cgd * Given a shared object, traverse its list of needed objects, and load
111 1.1 cgd * each of them. Returns 0 on success. Generates an error message and
112 1.1 cgd * returns -1 on failure.
113 1.1 cgd */
114 1.1 cgd int
115 1.3 christos _rtld_load_needed_objects(first)
116 1.3 christos Obj_Entry *first;
117 1.1 cgd {
118 1.3 christos Obj_Entry *obj;
119 1.3 christos int status = 0;
120 1.1 cgd
121 1.3 christos for (obj = first; obj != NULL; obj = obj->next) {
122 1.3 christos Needed_Entry *needed;
123 1.1 cgd
124 1.3 christos for (needed = obj->needed; needed != NULL;
125 1.3 christos needed = needed->next) {
126 1.3 christos const char *name = obj->strtab + needed->name;
127 1.3 christos char *libpath = _rtld_find_library(name, obj);
128 1.3 christos
129 1.3 christos if (libpath == NULL) {
130 1.3 christos status = -1;
131 1.3 christos } else {
132 1.3 christos needed->obj = _rtld_load_object(libpath, true);
133 1.3 christos if (needed->obj == NULL)
134 1.3 christos status = -1; /* FIXME - cleanup */
135 1.3 christos }
136 1.1 cgd #ifdef RTLD_LOADER
137 1.3 christos if (status == -1)
138 1.3 christos return status;
139 1.1 cgd #endif
140 1.3 christos }
141 1.1 cgd }
142 1.1 cgd
143 1.3 christos return status;
144 1.1 cgd }
145 1.4 kleink
146 1.4 kleink #ifdef RTLD_LOADER
147 1.4 kleink int
148 1.4 kleink _rtld_preload(preload_path, dodebug)
149 1.4 kleink const char *preload_path;
150 1.4 kleink bool dodebug;
151 1.4 kleink {
152 1.4 kleink const char *path;
153 1.4 kleink char *cp, *buf;
154 1.4 kleink int status = 0;
155 1.4 kleink
156 1.4 kleink if (preload_path != NULL) {
157 1.4 kleink cp = buf = xstrdup(preload_path);
158 1.4 kleink while ((path = strsep(&cp, " ")) != NULL && status == 0) {
159 1.4 kleink if (_rtld_load_object(xstrdup(path), dodebug) == NULL)
160 1.4 kleink status = -1;
161 1.4 kleink else if (dodebug)
162 1.4 kleink dbg((" preloaded \"%s\"", path));
163 1.4 kleink }
164 1.4 kleink free(buf);
165 1.4 kleink }
166 1.4 kleink
167 1.4 kleink return (status);
168 1.4 kleink }
169 1.4 kleink #endif
170