map_object.c revision 1.22 1 /* $NetBSD: map_object.c,v 1.22 2002/10/04 18:50:43 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 #include <errno.h>
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <sys/mman.h>
42
43 #include "rtld.h"
44
45 static int protflags __P((int)); /* Elf flags -> mmap protection */
46
47 /*
48 * Map a shared object into memory. The argument is a file descriptor,
49 * which must be open on the object and positioned at its beginning.
50 *
51 * The return value is a pointer to a newly-allocated Obj_Entry structure
52 * for the shared object. Returns NULL on failure.
53 */
54 Obj_Entry *
55 _rtld_map_object(path, fd, sb)
56 char *path;
57 int fd;
58 const struct stat *sb;
59 {
60 Obj_Entry *obj;
61 Elf_Ehdr *ehdr;
62 Elf_Phdr *phdr;
63 Elf_Phdr *phlimit;
64 Elf_Phdr *segs[2];
65 int nsegs;
66 caddr_t mapbase = MAP_FAILED;
67 size_t mapsize;
68 Elf_Off base_offset;
69 Elf_Addr base_vaddr;
70 Elf_Addr base_vlimit;
71 Elf_Addr text_vlimit;
72 int text_flags;
73 caddr_t base_addr;
74 Elf_Off data_offset;
75 Elf_Addr data_vaddr;
76 Elf_Addr data_vlimit;
77 int data_flags;
78 caddr_t data_addr;
79 caddr_t gap_addr;
80 size_t gap_size;
81 #ifdef RTLD_LOADER
82 Elf_Addr clear_vaddr;
83 caddr_t clear_addr;
84 size_t nclear;
85 #endif
86
87 obj = _rtld_obj_new();
88 obj->path = path;
89 if (sb != NULL) {
90 obj->dev = sb->st_dev;
91 obj->ino = sb->st_ino;
92 }
93
94 ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd,
95 (off_t)0);
96 if (ehdr == MAP_FAILED) {
97 _rtld_error("%s: read error: %s", path, xstrerror(errno));
98 goto bad;
99 }
100 /* Make sure the file is valid */
101 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
102 ehdr->e_ident[EI_CLASS] != ELFCLASS) {
103 _rtld_error("%s: unrecognized file format", path);
104 goto bad;
105 }
106 /* Elf_e_ident includes class */
107 if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
108 ehdr->e_version != EV_CURRENT ||
109 ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
110 _rtld_error("%s: unsupported file version", path);
111 goto bad;
112 }
113 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
114 _rtld_error("%s: unsupported file type", path);
115 goto bad;
116 }
117 switch (ehdr->e_machine) {
118 ELFDEFNNAME(MACHDEP_ID_CASES)
119 default:
120 _rtld_error("%s: unsupported machine", path);
121 goto bad;
122 }
123
124 /*
125 * We rely on the program header being in the first page. This is
126 * not strictly required by the ABI specification, but it seems to
127 * always true in practice. And, it simplifies things considerably.
128 */
129 assert(ehdr->e_phentsize == sizeof(Elf_Phdr));
130 assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <=
131 _rtld_pagesz);
132
133 /*
134 * Scan the program header entries, and save key information.
135 *
136 * We rely on there being exactly two load segments, text and data,
137 * in that order.
138 */
139 phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
140 phlimit = phdr + ehdr->e_phnum;
141 nsegs = 0;
142 while (phdr < phlimit) {
143 switch (phdr->p_type) {
144 case PT_INTERP:
145 obj->interp = (void *)phdr->p_vaddr;
146 break;
147
148 case PT_LOAD:
149 if (nsegs < 2)
150 segs[nsegs] = phdr;
151 ++nsegs;
152 break;
153
154 case PT_DYNAMIC:
155 obj->dynamic = (void *)phdr->p_vaddr;
156 break;
157 }
158
159 ++phdr;
160 }
161 obj->entry = (void *)ehdr->e_entry;
162 if (!obj->dynamic) {
163 _rtld_error("%s: not dynamically linked", path);
164 goto bad;
165 }
166 if (nsegs != 2) {
167 _rtld_error("%s: wrong number of segments (%d != 2)", path,
168 nsegs);
169 goto bad;
170 }
171
172 /*
173 * Map the entire address space of the object as a file
174 * region to stake out our contiguous region and establish a
175 * base for relocation. We use a file mapping so that
176 * the kernel will give us whatever alignment is appropriate
177 * for the platform we're running on.
178 *
179 * We map it using the text protection, map the data segment
180 * into the right place, then map an anon segment for the bss
181 * and unmap the gaps left by padding to alignment.
182 */
183
184 base_offset = round_down(segs[0]->p_offset);
185 base_vaddr = round_down(segs[0]->p_vaddr);
186 base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
187 text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
188 text_flags = protflags(segs[0]->p_flags);
189 data_offset = round_down(segs[1]->p_offset);
190 data_vaddr = round_down(segs[1]->p_vaddr);
191 data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz);
192 data_flags = protflags(segs[1]->p_flags);
193 clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
194
195 obj->textsize = text_vlimit - base_vaddr;
196 obj->vaddrbase = base_vaddr;
197 obj->isdynamic = ehdr->e_type == ET_DYN;
198
199 munmap(ehdr, _rtld_pagesz);
200 ehdr = MAP_FAILED;
201
202 #ifdef RTLD_LOADER
203 base_addr = obj->isdynamic ? NULL : (caddr_t)base_vaddr;
204 #else
205 base_addr = NULL;
206 #endif
207 mapsize = base_vlimit - base_vaddr;
208 mapbase = mmap(base_addr, mapsize, text_flags, MAP_FILE | MAP_PRIVATE,
209 fd, base_offset);
210 if (mapbase == MAP_FAILED) {
211 _rtld_error("mmap of entire address space failed: %s",
212 xstrerror(errno));
213 goto bad;
214 }
215
216 /* Overlay the data segment onto the proper region. */
217 data_addr = mapbase + (data_vaddr - base_vaddr);
218 if (mmap(data_addr, data_vlimit - data_vaddr, data_flags,
219 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) ==
220 MAP_FAILED) {
221 _rtld_error("mmap of data failed: %s", xstrerror(errno));
222 goto bad;
223 }
224
225 /* Overlay the bss segment onto the proper region. */
226 if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit,
227 data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) ==
228 MAP_FAILED) {
229 _rtld_error("mmap of bss failed: %s", xstrerror(errno));
230 goto bad;
231 }
232
233 /* Unmap the gap between the text and data. */
234 gap_addr = mapbase + round_up(text_vlimit - base_vaddr);
235 gap_size = data_addr - gap_addr;
236 if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) {
237 _rtld_error("mprotect of text -> data gap failed: %s",
238 xstrerror(errno));
239 goto bad;
240 }
241
242 #ifdef RTLD_LOADER
243 /* Clear any BSS in the last page of the data segment. */
244 clear_addr = mapbase + (clear_vaddr - base_vaddr);
245 if ((nclear = data_vlimit - clear_vaddr) > 0)
246 memset(clear_addr, 0, nclear);
247
248 /* Non-file portion of BSS mapped above. */
249 #endif
250
251 obj->mapbase = mapbase;
252 obj->mapsize = mapsize;
253 obj->relocbase = mapbase - base_vaddr;
254
255 if (obj->dynamic)
256 obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)obj->dynamic);
257 if (obj->entry)
258 obj->entry = (void *)(obj->relocbase + (Elf_Addr)obj->entry);
259 if (obj->interp)
260 obj->interp = (void *)(obj->relocbase + (Elf_Addr)obj->interp);
261
262 return obj;
263
264 bad:
265 if (ehdr != MAP_FAILED)
266 munmap(ehdr, _rtld_pagesz);
267 if (mapbase != MAP_FAILED)
268 munmap(mapbase, mapsize);
269 _rtld_obj_free(obj);
270 return NULL;
271 }
272
273 void
274 _rtld_obj_free(obj)
275 Obj_Entry *obj;
276 {
277 Objlist_Entry *elm;
278
279 free(obj->path);
280 while (obj->needed != NULL) {
281 Needed_Entry *needed = obj->needed;
282 obj->needed = needed->next;
283 free(needed);
284 }
285 while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
286 SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
287 free(elm);
288 }
289 while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
290 SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
291 free(elm);
292 }
293 free(obj);
294 }
295
296 Obj_Entry *
297 _rtld_obj_new(void)
298 {
299 Obj_Entry *obj;
300
301 obj = CNEW(Obj_Entry);
302 SIMPLEQ_INIT(&obj->dldags);
303 SIMPLEQ_INIT(&obj->dagmembers);
304 return obj;
305 }
306
307 /*
308 * Given a set of ELF protection flags, return the corresponding protection
309 * flags for MMAP.
310 */
311 static int
312 protflags(elfflags)
313 int elfflags;
314 {
315 int prot = 0;
316 if (elfflags & PF_R)
317 prot |= PROT_READ;
318 #ifdef RTLD_LOADER
319 if (elfflags & PF_W)
320 prot |= PROT_WRITE;
321 #endif
322 if (elfflags & PF_X)
323 prot |= PROT_EXEC;
324 return prot;
325 }
326