vm: mmap support
[minix3.git] / libexec / ld.elf_so / map_object.c
blob027deeca3dccc55366557e90cc4ae8277f99c879
1 /* $NetBSD: map_object.c,v 1.45 2012/10/13 21:13:07 dholland Exp $ */
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt@3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root@ihack.net>
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by John Polstra.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __RCSID("$NetBSD: map_object.c,v 1.45 2012/10/13 21:13:07 dholland Exp $");
38 #endif /* not lint */
40 #include <errno.h>
41 #include <stddef.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <sys/mman.h>
49 #include "debug.h"
50 #include "rtld.h"
52 #ifdef __minix
53 #define munmap minix_munmap
54 #endif
56 #define MINIXVERBOSE 0
58 #if MINIXVERBOSE
59 #include <stdio.h>
60 #endif
62 static int protflags(int); /* Elf flags -> mmap protection */
64 #define EA_UNDEF (~(Elf_Addr)0)
66 static void Pread(void *addr, size_t size, int fd, off_t off)
68 int s;
69 if((s=pread(fd,addr, size, off)) < 0) {
70 _rtld_error("pread failed");
71 exit(1);
74 #if MINIXVERBOSE
75 fprintf(stderr, "read 0x%lx bytes from offset 0x%lx to addr 0x%lx\n", size, off, addr);
76 #endif
80 * Map a shared object into memory. The argument is a file descriptor,
81 * which must be open on the object and positioned at its beginning.
83 * The return value is a pointer to a newly-allocated Obj_Entry structure
84 * for the shared object. Returns NULL on failure.
86 Obj_Entry *
87 _rtld_map_object(const char *path, int fd, const struct stat *sb)
89 Obj_Entry *obj;
90 Elf_Ehdr *ehdr;
91 Elf_Phdr *phdr;
92 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
93 Elf_Phdr *phtls;
94 #endif
95 size_t phsize;
96 Elf_Phdr *phlimit;
97 Elf_Phdr *segs[2];
98 int nsegs;
99 caddr_t mapbase = MAP_FAILED;
100 size_t mapsize = 0;
101 int mapflags;
102 Elf_Off base_offset;
103 #ifdef MAP_ALIGNED
104 Elf_Addr base_alignment;
105 #endif
106 Elf_Addr base_vaddr;
107 Elf_Addr base_vlimit;
108 Elf_Addr text_vlimit;
109 int text_flags;
110 caddr_t base_addr;
111 Elf_Off data_offset;
112 Elf_Addr data_vaddr;
113 Elf_Addr data_vlimit;
114 int data_flags;
115 caddr_t data_addr;
116 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
117 Elf_Addr tls_vaddr = 0; /* Noise GCC */
118 #endif
119 Elf_Addr phdr_vaddr;
120 size_t phdr_memsz;
121 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
122 caddr_t gap_addr;
123 size_t gap_size;
124 #endif
125 int i;
126 #ifdef RTLD_LOADER
127 Elf_Addr clear_vaddr;
128 caddr_t clear_addr;
129 size_t nclear;
130 #endif
132 if (sb != NULL && sb->st_size < (off_t)sizeof (Elf_Ehdr)) {
133 _rtld_error("%s: not ELF file (too short)", path);
134 return NULL;
137 obj = _rtld_obj_new();
138 obj->path = xstrdup(path);
139 obj->pathlen = strlen(path);
140 if (sb != NULL) {
141 obj->dev = sb->st_dev;
142 obj->ino = sb->st_ino;
145 #ifdef __minix
146 ehdr = minix_mmap(NULL, _rtld_pagesz, PROT_READ|PROT_WRITE,
147 MAP_PREALLOC|MAP_ANON, -1, (off_t)0);
148 Pread(ehdr, _rtld_pagesz, fd, 0);
149 #if MINIXVERBOSE
150 fprintf(stderr, "minix mmap for header: 0x%lx\n", ehdr);
151 #endif
152 #else
153 ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd,
154 (off_t)0);
155 #endif
156 obj->ehdr = ehdr;
157 if (ehdr == MAP_FAILED) {
158 _rtld_error("%s: read error: %s", path, xstrerror(errno));
159 goto bad;
161 /* Make sure the file is valid */
162 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0) {
163 _rtld_error("%s: not ELF file (magic number bad)", path);
164 goto bad;
166 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
167 _rtld_error("%s: invalid ELF class %x; expected %x", path,
168 ehdr->e_ident[EI_CLASS], ELFCLASS);
169 goto bad;
171 /* Elf_e_ident includes class */
172 if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
173 ehdr->e_version != EV_CURRENT ||
174 ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
175 _rtld_error("%s: unsupported file version", path);
176 goto bad;
178 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
179 _rtld_error("%s: unsupported file type", path);
180 goto bad;
182 switch (ehdr->e_machine) {
183 ELFDEFNNAME(MACHDEP_ID_CASES)
184 default:
185 _rtld_error("%s: unsupported machine", path);
186 goto bad;
190 * We rely on the program header being in the first page. This is
191 * not strictly required by the ABI specification, but it seems to
192 * always true in practice. And, it simplifies things considerably.
194 assert(ehdr->e_phentsize == sizeof(Elf_Phdr));
195 assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <=
196 _rtld_pagesz);
199 * Scan the program header entries, and save key information.
201 * We rely on there being exactly two load segments, text and data,
202 * in that order.
204 phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
205 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
206 phtls = NULL;
207 #endif
208 phsize = ehdr->e_phnum * sizeof(phdr[0]);
209 obj->phdr = NULL;
210 phdr_vaddr = EA_UNDEF;
211 phdr_memsz = 0;
212 phlimit = phdr + ehdr->e_phnum;
213 nsegs = 0;
214 while (phdr < phlimit) {
215 switch (phdr->p_type) {
216 case PT_INTERP:
217 obj->interp = (void *)(uintptr_t)phdr->p_vaddr;
218 dbg(("%s: PT_INTERP %p", obj->path, obj->interp));
219 break;
221 case PT_LOAD:
222 if (nsegs < 2)
223 segs[nsegs] = phdr;
224 ++nsegs;
226 #if ELFSIZE == 64
227 #define PRImemsz PRIu64
228 #else
229 #define PRImemsz PRIu32
230 #endif
231 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_LOAD",
232 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
233 break;
235 case PT_PHDR:
236 phdr_vaddr = phdr->p_vaddr;
237 phdr_memsz = phdr->p_memsz;
238 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_PHDR",
239 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
240 break;
242 case PT_DYNAMIC:
243 obj->dynamic = (void *)(uintptr_t)phdr->p_vaddr;
244 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_DYNAMIC",
245 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
246 break;
248 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
249 case PT_TLS:
250 phtls = phdr;
251 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_TLS",
252 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
253 break;
254 #endif
257 ++phdr;
259 phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
260 obj->entry = (void *)(uintptr_t)ehdr->e_entry;
261 if (!obj->dynamic) {
262 _rtld_error("%s: not dynamically linked", path);
263 goto bad;
265 if (nsegs != 2) {
266 _rtld_error("%s: wrong number of segments (%d != 2)", path,
267 nsegs);
268 goto bad;
272 * Map the entire address space of the object as a file
273 * region to stake out our contiguous region and establish a
274 * base for relocation. We use a file mapping so that
275 * the kernel will give us whatever alignment is appropriate
276 * for the platform we're running on.
278 * We map it using the text protection, map the data segment
279 * into the right place, then map an anon segment for the bss
280 * and unmap the gaps left by padding to alignment.
283 #ifdef MAP_ALIGNED
284 base_alignment = segs[0]->p_align;
285 #endif
286 base_offset = round_down(segs[0]->p_offset);
287 base_vaddr = round_down(segs[0]->p_vaddr);
288 base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
289 text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
290 text_flags = protflags(segs[0]->p_flags);
291 data_offset = round_down(segs[1]->p_offset);
292 data_vaddr = round_down(segs[1]->p_vaddr);
293 data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz);
294 data_flags = protflags(segs[1]->p_flags);
295 #ifdef RTLD_LOADER
296 clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
297 #endif
299 obj->textsize = text_vlimit - base_vaddr;
300 obj->vaddrbase = base_vaddr;
301 obj->isdynamic = ehdr->e_type == ET_DYN;
303 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
304 if (phtls != NULL) {
305 ++_rtld_tls_dtv_generation;
306 obj->tlsindex = ++_rtld_tls_max_index;
307 obj->tlssize = phtls->p_memsz;
308 obj->tlsalign = phtls->p_align;
309 obj->tlsinitsize = phtls->p_filesz;
310 tls_vaddr = phtls->p_vaddr;
312 #endif
314 obj->phdr_loaded = false;
315 for (i = 0; i < nsegs; i++) {
316 if (phdr_vaddr != EA_UNDEF &&
317 segs[i]->p_vaddr <= phdr_vaddr &&
318 segs[i]->p_memsz >= phdr_memsz) {
319 obj->phdr_loaded = true;
320 break;
322 if (segs[i]->p_offset <= ehdr->e_phoff &&
323 segs[i]->p_memsz >= phsize) {
324 phdr_vaddr = segs[i]->p_vaddr + ehdr->e_phoff;
325 phdr_memsz = phsize;
326 obj->phdr_loaded = true;
327 break;
330 if (obj->phdr_loaded) {
331 obj->phdr = (void *)(uintptr_t)phdr_vaddr;
332 obj->phsize = phdr_memsz;
333 } else {
334 Elf_Phdr *buf;
335 buf = xmalloc(phsize);
336 if (buf == NULL) {
337 _rtld_error("%s: cannot allocate program header", path);
338 goto bad;
340 memcpy(buf, phdr, phsize);
341 obj->phdr = buf;
342 obj->phsize = phsize;
344 dbg(("%s: phdr %p phsize %zu (%s)", obj->path, obj->phdr, obj->phsize,
345 obj->phdr_loaded ? "loaded" : "allocated"));
347 /* Unmap header if it overlaps the first load section. */
348 if (base_offset < _rtld_pagesz) {
349 munmap(ehdr, _rtld_pagesz);
350 obj->ehdr = MAP_FAILED;
354 * Calculate log2 of the base section alignment.
356 mapflags = 0;
357 #ifdef MAP_ALIGNED
358 if (base_alignment > _rtld_pagesz) {
359 unsigned int log2 = 0;
360 for (; base_alignment > 1; base_alignment >>= 1)
361 log2++;
362 mapflags = MAP_ALIGNED(log2);
364 #endif
366 #ifdef RTLD_LOADER
367 base_addr = obj->isdynamic ? NULL : (caddr_t)base_vaddr;
368 #else
369 base_addr = NULL;
370 #endif
371 mapsize = base_vlimit - base_vaddr;
373 #ifndef __minix
374 mapbase = mmap(base_addr, mapsize, text_flags,
375 mapflags | MAP_FILE | MAP_PRIVATE, fd, base_offset);
376 #else
377 mapbase = minix_mmap(base_addr, mapsize, PROT_READ|PROT_WRITE,
378 MAP_ANON | MAP_PREALLOC, -1, 0);
379 #if MINIXVERBOSE
380 fprintf(stderr, "minix mmap for whole block: 0x%lx-0x%lx\n", mapbase, mapbase+mapsize);
381 #endif
382 Pread(mapbase, obj->textsize, fd, 0);
383 #endif
384 if (mapbase == MAP_FAILED) {
385 _rtld_error("mmap of entire address space failed: %s",
386 xstrerror(errno));
387 goto bad;
390 /* Overlay the data segment onto the proper region. */
391 data_addr = mapbase + (data_vaddr - base_vaddr);
392 #ifdef __minix
393 Pread(data_addr, data_vlimit - data_vaddr, fd, data_offset);
394 #else
395 if (mmap(data_addr, data_vlimit - data_vaddr, data_flags,
396 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) ==
397 MAP_FAILED) {
398 _rtld_error("mmap of data failed: %s", xstrerror(errno));
399 goto bad;
402 /* Overlay the bss segment onto the proper region. */
403 if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit,
404 data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) ==
405 MAP_FAILED) {
406 _rtld_error("mmap of bss failed: %s", xstrerror(errno));
407 goto bad;
410 /* Unmap the gap between the text and data. */
411 gap_addr = mapbase + round_up(text_vlimit - base_vaddr);
412 gap_size = data_addr - gap_addr;
413 if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) {
414 _rtld_error("mprotect of text -> data gap failed: %s",
415 xstrerror(errno));
416 goto bad;
418 #endif
420 #ifdef RTLD_LOADER
421 /* Clear any BSS in the last page of the data segment. */
422 clear_addr = mapbase + (clear_vaddr - base_vaddr);
423 if ((nclear = data_vlimit - clear_vaddr) > 0)
424 memset(clear_addr, 0, nclear);
426 /* Non-file portion of BSS mapped above. */
427 #endif
429 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
430 if (phtls != NULL)
431 obj->tlsinit = mapbase + tls_vaddr;
432 #endif
434 obj->mapbase = mapbase;
435 obj->mapsize = mapsize;
436 obj->relocbase = mapbase - base_vaddr;
438 if (obj->dynamic)
439 obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->dynamic);
440 if (obj->entry)
441 obj->entry = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->entry);
442 if (obj->interp)
443 obj->interp = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->interp);
444 if (obj->phdr_loaded)
445 obj->phdr = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->phdr);
447 return obj;
449 bad:
450 if (obj->ehdr != MAP_FAILED)
451 munmap(obj->ehdr, _rtld_pagesz);
452 if (mapbase != MAP_FAILED)
453 munmap(mapbase, mapsize);
454 _rtld_obj_free(obj);
455 return NULL;
458 void
459 _rtld_obj_free(Obj_Entry *obj)
461 Objlist_Entry *elm;
463 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
464 if (obj->tls_done)
465 _rtld_tls_offset_free(obj);
466 #endif
467 xfree(obj->path);
468 while (obj->needed != NULL) {
469 Needed_Entry *needed = obj->needed;
470 obj->needed = needed->next;
471 xfree(needed);
473 while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
474 SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
475 xfree(elm);
477 while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
478 SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
479 xfree(elm);
481 if (!obj->phdr_loaded)
482 xfree((void *)(uintptr_t)obj->phdr);
483 xfree(obj);
484 #ifdef COMBRELOC
485 _rtld_combreloc_reset(obj);
486 #endif
489 Obj_Entry *
490 _rtld_obj_new(void)
492 Obj_Entry *obj;
494 obj = CNEW(Obj_Entry);
495 SIMPLEQ_INIT(&obj->dldags);
496 SIMPLEQ_INIT(&obj->dagmembers);
497 return obj;
501 * Given a set of ELF protection flags, return the corresponding protection
502 * flags for MMAP.
504 static int
505 protflags(int elfflags)
507 int prot = 0;
509 if (elfflags & PF_R)
510 prot |= PROT_READ;
511 #ifdef RTLD_LOADER
512 if (elfflags & PF_W)
513 prot |= PROT_WRITE;
514 #endif
515 if (elfflags & PF_X)
516 prot |= PROT_EXEC;
517 return prot;