load.c revision 1.1 1 /* $NetBSD: load.c,v 1.1 1996/12/16 20:37:59 cgd 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(
65 char *filepath)
66 {
67 Obj_Entry *obj;
68
69 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
70 if (strcmp(obj->path, filepath) == 0)
71 break;
72
73 if (obj == NULL) { /* First use of this object, so we must map it in */
74 int fd;
75
76 if ((fd = open(filepath, O_RDONLY)) == -1) {
77 _rtld_error("Cannot open \"%s\"", filepath);
78 return NULL;
79 }
80 obj = _rtld_map_object(filepath, fd);
81 close(fd);
82 if (obj == NULL) {
83 free(filepath);
84 return NULL;
85 }
86
87 obj->path = filepath;
88 _rtld_digest_dynamic(obj);
89
90 *_rtld_objtail = obj;
91 _rtld_objtail = &obj->next;
92 #ifdef RTLD_LOADER
93 _rtld_linkmap_add(obj); /* for GDB */
94 #endif
95
96 dbg(" %p .. %p: %s", obj->mapbase,
97 obj->mapbase + obj->mapsize - 1, obj->path);
98 if (obj->textrel)
99 dbg(" WARNING: %s has impure text", obj->path);
100 } else
101 free(filepath);
102
103 ++obj->refcount;
104 return obj;
105 }
106
107 /*
108 * Given a shared object, traverse its list of needed objects, and load
109 * each of them. Returns 0 on success. Generates an error message and
110 * returns -1 on failure.
111 */
112 int
113 _rtld_load_needed_objects(
114 Obj_Entry *first)
115 {
116 Obj_Entry *obj;
117 int status = 0;
118
119 for (obj = first; obj != NULL; obj = obj->next) {
120 Needed_Entry *needed;
121
122 for (needed = obj->needed; needed != NULL; needed = needed->next) {
123 const char *name = obj->strtab + needed->name;
124 char *libpath = _rtld_find_library(name, obj);
125
126 if (libpath == NULL) {
127 status = -1;
128 } else {
129 needed->obj = _rtld_load_object(libpath);
130 if (needed->obj == NULL)
131 status = -1; /* FIXME - cleanup */
132 }
133 #ifdef RTLD_LOADER
134 if (status == -1)
135 return status;
136 #endif
137 }
138 }
139
140 return status;
141 }
142