xref: /minix/libexec/ld.elf_so/map_object.c (revision 2f98b65a)
1 /*	$NetBSD: map_object.c,v 1.52 2013/08/03 13:17:05 skrll Exp $	 */
2 
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.
8  *
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.
22  *
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.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __RCSID("$NetBSD: map_object.c,v 1.52 2013/08/03 13:17:05 skrll Exp $");
38 #endif /* not lint */
39 
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>
48 
49 #include "debug.h"
50 #include "rtld.h"
51 
52 #if defined(__minix)
53 #define MINIXVERBOSE 0
54 
55 #if MINIXVERBOSE
56 #include <stdio.h>
57 #endif
58 
59 #endif /* defined(__minix) */
60 
61 static int protflags(int);	/* Elf flags -> mmap protection */
62 
63 #define EA_UNDEF		(~(Elf_Addr)0)
64 
65 /*
66  * Map a shared object into memory.  The argument is a file descriptor,
67  * which must be open on the object and positioned at its beginning.
68  *
69  * The return value is a pointer to a newly-allocated Obj_Entry structure
70  * for the shared object.  Returns NULL on failure.
71  */
72 Obj_Entry *
73 _rtld_map_object(const char *path, int fd, const struct stat *sb)
74 {
75 	Obj_Entry	*obj;
76 	Elf_Ehdr	*ehdr;
77 	Elf_Phdr	*phdr;
78 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
79 	Elf_Phdr	*phtls;
80 #endif
81 	size_t		 phsize;
82 	Elf_Phdr	*phlimit;
83 	Elf_Phdr	*segs[2];
84 	int		 nsegs;
85 	caddr_t		 mapbase = MAP_FAILED;
86 	size_t		 mapsize = 0;
87 	int		 mapflags;
88 	Elf_Off		 base_offset;
89 #ifdef MAP_ALIGNED
90 	Elf_Addr	 base_alignment;
91 #endif
92 	Elf_Addr	 base_vaddr;
93 	Elf_Addr	 base_vlimit;
94 	Elf_Addr	 text_vlimit;
95 	int		 text_flags;
96 	caddr_t		 base_addr;
97 	Elf_Off		 data_offset;
98 	Elf_Addr	 data_vaddr;
99 	Elf_Addr	 data_vlimit;
100 	int		 data_flags;
101 	caddr_t		 data_addr;
102 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
103 	Elf_Addr	 tls_vaddr = 0; /* Noise GCC */
104 #endif
105 	Elf_Addr	 phdr_vaddr;
106 	size_t		 phdr_memsz;
107 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
108 	caddr_t		 gap_addr;
109 	size_t		 gap_size;
110 #endif
111 	int i;
112 #ifdef RTLD_LOADER
113 	Elf_Addr	 clear_vaddr;
114 	caddr_t		 clear_addr;
115 	size_t		 nclear;
116 #endif
117 #if defined(__minix)
118 	Elf_Addr bsslen;
119 #endif /* defined(__minix) */
120 
121 	if (sb != NULL && sb->st_size < (off_t)sizeof (Elf_Ehdr)) {
122 		_rtld_error("%s: not ELF file (too short)", path);
123 		return NULL;
124 	}
125 
126 	obj = _rtld_obj_new();
127 	obj->path = xstrdup(path);
128 	obj->pathlen = strlen(path);
129 	if (sb != NULL) {
130 		obj->dev = sb->st_dev;
131 		obj->ino = sb->st_ino;
132 	}
133 
134 	ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd,
135 	    (off_t)0);
136 	obj->ehdr = ehdr;
137 	if (ehdr == MAP_FAILED) {
138 #if defined(__minix)
139 		return _rtld_map_object_fallback(path, fd, sb);
140 #else
141 		_rtld_error("%s: read error: %s", path, xstrerror(errno));
142 		goto bad;
143 #endif
144 	}
145 	/* Make sure the file is valid */
146 	if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0) {
147 		_rtld_error("%s: not ELF file (magic number bad)", path);
148 		goto bad;
149 	}
150 	if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
151 		_rtld_error("%s: invalid ELF class %x; expected %x", path,
152 		    ehdr->e_ident[EI_CLASS], ELFCLASS);
153 		goto bad;
154 	}
155 	/* Elf_e_ident includes class */
156 	if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
157 	    ehdr->e_version != EV_CURRENT ||
158 	    ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
159 		_rtld_error("%s: unsupported file version", path);
160 		goto bad;
161 	}
162 	if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
163 		_rtld_error("%s: unsupported file type", path);
164 		goto bad;
165 	}
166 	switch (ehdr->e_machine) {
167 		ELFDEFNNAME(MACHDEP_ID_CASES)
168 	default:
169 		_rtld_error("%s: unsupported machine", path);
170 		goto bad;
171 	}
172 
173 	/*
174          * We rely on the program header being in the first page.  This is
175          * not strictly required by the ABI specification, but it seems to
176          * always true in practice.  And, it simplifies things considerably.
177          */
178 	assert(ehdr->e_phentsize == sizeof(Elf_Phdr));
179 	assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <=
180 	    _rtld_pagesz);
181 
182 	/*
183          * Scan the program header entries, and save key information.
184          *
185          * We rely on there being exactly two load segments, text and data,
186          * in that order.
187          */
188 	phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
189 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
190 	phtls = NULL;
191 #endif
192 	phsize = ehdr->e_phnum * sizeof(phdr[0]);
193 	obj->phdr = NULL;
194 	phdr_vaddr = EA_UNDEF;
195 	phdr_memsz = 0;
196 	phlimit = phdr + ehdr->e_phnum;
197 	nsegs = 0;
198 	while (phdr < phlimit) {
199 		switch (phdr->p_type) {
200 		case PT_INTERP:
201 			obj->interp = (void *)(uintptr_t)phdr->p_vaddr;
202  			dbg(("%s: PT_INTERP %p", obj->path, obj->interp));
203 			break;
204 
205 		case PT_LOAD:
206 			if (nsegs < 2)
207 				segs[nsegs] = phdr;
208 			++nsegs;
209 
210 			dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_LOAD",
211 			    (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
212 			break;
213 
214 		case PT_PHDR:
215 			phdr_vaddr = phdr->p_vaddr;
216 			phdr_memsz = phdr->p_memsz;
217 			dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_PHDR",
218 			    (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
219 			break;
220 
221 		case PT_DYNAMIC:
222 			obj->dynamic = (void *)(uintptr_t)phdr->p_vaddr;
223 			dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_DYNAMIC",
224 			    (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
225 			break;
226 
227 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
228 		case PT_TLS:
229 			phtls = phdr;
230 			dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_TLS",
231 			    (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
232 			break;
233 #endif
234 #ifdef __ARM_EABI__
235 		case PT_ARM_EXIDX:
236 			obj->exidx_start = (void *)(uintptr_t)phdr->p_vaddr;
237 			obj->exidx_sz = phdr->p_memsz;
238 			break;
239 #endif
240 		}
241 
242 		++phdr;
243 	}
244 	phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
245 	obj->entry = (void *)(uintptr_t)ehdr->e_entry;
246 	if (!obj->dynamic) {
247 		_rtld_error("%s: not dynamically linked", path);
248 		goto bad;
249 	}
250 	if (nsegs != 2) {
251 		_rtld_error("%s: wrong number of segments (%d != 2)", path,
252 		    nsegs);
253 		goto bad;
254 	}
255 
256 	/*
257 	 * Map the entire address space of the object as a file
258 	 * region to stake out our contiguous region and establish a
259 	 * base for relocation.  We use a file mapping so that
260 	 * the kernel will give us whatever alignment is appropriate
261 	 * for the platform we're running on.
262 	 *
263 	 * We map it using the text protection, map the data segment
264 	 * into the right place, then map an anon segment for the bss
265 	 * and unmap the gaps left by padding to alignment.
266 	 */
267 
268 #ifdef MAP_ALIGNED
269 	base_alignment = segs[0]->p_align;
270 #endif
271 	base_offset = round_down(segs[0]->p_offset);
272 	base_vaddr = round_down(segs[0]->p_vaddr);
273 	base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
274 	text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
275 	text_flags = protflags(segs[0]->p_flags);
276 	data_offset = round_down(segs[1]->p_offset);
277 	data_vaddr = round_down(segs[1]->p_vaddr);
278 	data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz);
279 	data_flags = protflags(segs[1]->p_flags);
280 #ifdef RTLD_LOADER
281 	clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
282 #endif
283 
284 	obj->textsize = text_vlimit - base_vaddr;
285 	obj->vaddrbase = base_vaddr;
286 	obj->isdynamic = ehdr->e_type == ET_DYN;
287 
288 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
289 	if (phtls != NULL) {
290 		++_rtld_tls_dtv_generation;
291 		obj->tlsindex = ++_rtld_tls_max_index;
292 		obj->tlssize = phtls->p_memsz;
293 		obj->tlsalign = phtls->p_align;
294 		obj->tlsinitsize = phtls->p_filesz;
295 		tls_vaddr = phtls->p_vaddr;
296 	}
297 #endif
298 
299 	obj->phdr_loaded = false;
300 	for (i = 0; i < nsegs; i++) {
301 		if (phdr_vaddr != EA_UNDEF &&
302 		    segs[i]->p_vaddr <= phdr_vaddr &&
303 		    segs[i]->p_memsz >= phdr_memsz) {
304 			obj->phdr_loaded = true;
305 			break;
306 		}
307 		if (segs[i]->p_offset <= ehdr->e_phoff &&
308 		    segs[i]->p_memsz >= phsize) {
309 			phdr_vaddr = segs[i]->p_vaddr + ehdr->e_phoff;
310 			phdr_memsz = phsize;
311 			obj->phdr_loaded = true;
312 			break;
313 		}
314 	}
315 	if (obj->phdr_loaded) {
316 		obj->phdr = (void *)(uintptr_t)phdr_vaddr;
317 		obj->phsize = phdr_memsz;
318 	} else {
319 		Elf_Phdr *buf;
320 		buf = xmalloc(phsize);
321 		if (buf == NULL) {
322 			_rtld_error("%s: cannot allocate program header", path);
323 			goto bad;
324 		}
325 		memcpy(buf, phdr, phsize);
326 		obj->phdr = buf;
327 		obj->phsize = phsize;
328 	}
329 	dbg(("%s: phdr %p phsize %zu (%s)", obj->path, obj->phdr, obj->phsize,
330 	     obj->phdr_loaded ? "loaded" : "allocated"));
331 
332 	/* Unmap header if it overlaps the first load section. */
333 	if (base_offset < _rtld_pagesz) {
334 		munmap(ehdr, _rtld_pagesz);
335 		obj->ehdr = MAP_FAILED;
336 	}
337 
338 	/*
339 	 * Calculate log2 of the base section alignment.
340 	 */
341 	mapflags = 0;
342 #ifdef MAP_ALIGNED
343 	if (base_alignment > _rtld_pagesz) {
344 		unsigned int log2 = 0;
345 		for (; base_alignment > 1; base_alignment >>= 1)
346 			log2++;
347 		mapflags = MAP_ALIGNED(log2);
348 	}
349 #endif
350 
351 #ifdef RTLD_LOADER
352 	base_addr = obj->isdynamic ? NULL : (caddr_t)base_vaddr;
353 #else
354 	base_addr = NULL;
355 #endif
356 	mapsize = base_vlimit - base_vaddr;
357 	mapbase = mmap(base_addr, mapsize, text_flags,
358 	    mapflags | MAP_FILE | MAP_PRIVATE, fd, base_offset);
359 	if (mapbase == MAP_FAILED) {
360 		_rtld_error("mmap of entire address space failed: %s",
361 		    xstrerror(errno));
362 		goto bad;
363 	}
364 
365 	/* Overlay the data segment onto the proper region. */
366 	data_addr = mapbase + (data_vaddr - base_vaddr);
367 	if (mmap(data_addr, data_vlimit - data_vaddr, data_flags,
368 	    MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) ==
369 	    MAP_FAILED) {
370 		_rtld_error("mmap of data failed: %s", xstrerror(errno));
371 		goto bad;
372 	}
373 
374 	/* Overlay the bss segment onto the proper region. */
375 #if defined(__minix)
376 	bsslen = base_vlimit - data_vlimit;
377 	if (bsslen > 0 &&
378 		mmap(mapbase + data_vlimit - base_vaddr, bsslen,
379 #else
380 	if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit,
381 #endif /* defined(__minix) */
382 	    data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) ==
383 	    MAP_FAILED) {
384 		_rtld_error("mmap of bss failed: %s", xstrerror(errno));
385 		goto bad;
386 	}
387 
388 	/* Unmap the gap between the text and data. */
389 #if !defined(__minix)
390 	gap_addr = mapbase + round_up(text_vlimit - base_vaddr);
391 	gap_size = data_addr - gap_addr;
392 	if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) {
393 		_rtld_error("mprotect of text -> data gap failed: %s",
394 		    xstrerror(errno));
395 		goto bad;
396 	}
397 #endif /* !defined(__minix) */
398 
399 #ifdef RTLD_LOADER
400 	/* Clear any BSS in the last page of the data segment. */
401 	clear_addr = mapbase + (clear_vaddr - base_vaddr);
402 	if ((nclear = data_vlimit - clear_vaddr) > 0)
403 		memset(clear_addr, 0, nclear);
404 
405 	/* Non-file portion of BSS mapped above. */
406 #endif
407 
408 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
409 	if (phtls != NULL)
410 		obj->tlsinit = mapbase + tls_vaddr;
411 #endif
412 
413 	obj->mapbase = mapbase;
414 	obj->mapsize = mapsize;
415 	obj->relocbase = mapbase - base_vaddr;
416 
417 	if (obj->dynamic)
418 		obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->dynamic);
419 	if (obj->entry)
420 		obj->entry = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->entry);
421 	if (obj->interp)
422 		obj->interp = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->interp);
423 	if (obj->phdr_loaded)
424 		obj->phdr =  (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->phdr);
425 #ifdef __ARM_EABI__
426 	if (obj->exidx_start)
427 		obj->exidx_start = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->exidx_start);
428 #endif
429 
430 	return obj;
431 
432 bad:
433 	if (obj->ehdr != MAP_FAILED)
434 		munmap(obj->ehdr, _rtld_pagesz);
435 	if (mapbase != MAP_FAILED)
436 		munmap(mapbase, mapsize);
437 	_rtld_obj_free(obj);
438 	return NULL;
439 }
440 
441 void
442 _rtld_obj_free(Obj_Entry *obj)
443 {
444 	Objlist_Entry *elm;
445 	Name_Entry *entry;
446 
447 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
448 	if (obj->tls_done)
449 		_rtld_tls_offset_free(obj);
450 #endif
451 	xfree(obj->path);
452 	while (obj->needed != NULL) {
453 		Needed_Entry *needed = obj->needed;
454 		obj->needed = needed->next;
455 		xfree(needed);
456 	}
457 	while ((entry = SIMPLEQ_FIRST(&obj->names)) != NULL) {
458 		SIMPLEQ_REMOVE_HEAD(&obj->names, link);
459 		xfree(entry);
460 	}
461 	while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
462 		SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
463 		xfree(elm);
464 	}
465 	while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
466 		SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
467 		xfree(elm);
468 	}
469 	if (!obj->phdr_loaded)
470 		xfree((void *)(uintptr_t)obj->phdr);
471 	xfree(obj);
472 #ifdef COMBRELOC
473 	_rtld_combreloc_reset(obj);
474 #endif
475 }
476 
477 Obj_Entry *
478 _rtld_obj_new(void)
479 {
480 	Obj_Entry *obj;
481 
482 	obj = CNEW(Obj_Entry);
483 	SIMPLEQ_INIT(&obj->names);
484 	SIMPLEQ_INIT(&obj->dldags);
485 	SIMPLEQ_INIT(&obj->dagmembers);
486 	return obj;
487 }
488 
489 /*
490  * Given a set of ELF protection flags, return the corresponding protection
491  * flags for MMAP.
492  */
493 static int
494 protflags(int elfflags)
495 {
496 	int prot = 0;
497 
498 	if (elfflags & PF_R)
499 		prot |= PROT_READ;
500 #ifdef RTLD_LOADER
501 	if (elfflags & PF_W)
502 		prot |= PROT_WRITE;
503 #endif
504 	if (elfflags & PF_X)
505 		prot |= PROT_EXEC;
506 #if defined(__minix)
507 	/* Minix has to map it writable so we can do relocations
508 	 * as we don't have mprotect() yet.
509 	 */
510 	prot |= PROT_WRITE;
511 #endif /* defined(__minix) */
512 	return prot;
513 }
514