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