Home | History | Annotate | Line # | Download | only in ld.elf_so
map_object.c revision 1.41
      1 /*	$NetBSD: map_object.c,v 1.41 2010/10/16 10:27:07 skrll Exp $	 */
      2 
      3 /*
      4  * Copyright 1996 John D. Polstra.
      5  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6  * Copyright 2002 Charles M. Hannum <root (at) 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.41 2010/10/16 10:27:07 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 static int protflags(int);	/* Elf flags -> mmap protection */
     53 
     54 #define EA_UNDEF		(~(Elf_Addr)0)
     55 
     56 /*
     57  * Map a shared object into memory.  The argument is a file descriptor,
     58  * which must be open on the object and positioned at its beginning.
     59  *
     60  * The return value is a pointer to a newly-allocated Obj_Entry structure
     61  * for the shared object.  Returns NULL on failure.
     62  */
     63 Obj_Entry *
     64 _rtld_map_object(const char *path, int fd, const struct stat *sb)
     65 {
     66 	Obj_Entry	*obj;
     67 	Elf_Ehdr	*ehdr;
     68 	Elf_Phdr	*phdr;
     69 	size_t		 phsize;
     70 	Elf_Phdr	*phlimit;
     71 	Elf_Phdr	*segs[2];
     72 	int		 nsegs;
     73 	caddr_t		 mapbase = MAP_FAILED;
     74 	size_t		 mapsize = 0;
     75 	int		 mapflags;
     76 	Elf_Off		 base_offset;
     77 #ifdef MAP_ALIGNED
     78 	Elf_Addr	 base_alignment;
     79 #endif
     80 	Elf_Addr	 base_vaddr;
     81 	Elf_Addr	 base_vlimit;
     82 	Elf_Addr	 text_vlimit;
     83 	int		 text_flags;
     84 	caddr_t		 base_addr;
     85 	Elf_Off		 data_offset;
     86 	Elf_Addr	 data_vaddr;
     87 	Elf_Addr	 data_vlimit;
     88 	int		 data_flags;
     89 	caddr_t		 data_addr;
     90 	Elf_Addr	 phdr_vaddr;
     91 	size_t		 phdr_memsz;
     92 	caddr_t		 gap_addr;
     93 	size_t		 gap_size;
     94 	int i;
     95 #ifdef RTLD_LOADER
     96 	Elf_Addr	 clear_vaddr;
     97 	caddr_t		 clear_addr;
     98 	size_t		 nclear;
     99 #endif
    100 
    101 	if (sb != NULL && sb->st_size < (off_t)sizeof (Elf_Ehdr)) {
    102 		_rtld_error("%s: unrecognized file format1", path);
    103 		return NULL;
    104 	}
    105 
    106 	obj = _rtld_obj_new();
    107 	obj->path = xstrdup(path);
    108 	obj->pathlen = strlen(path);
    109 	if (sb != NULL) {
    110 		obj->dev = sb->st_dev;
    111 		obj->ino = sb->st_ino;
    112 	}
    113 
    114 	ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd,
    115 	    (off_t)0);
    116 	obj->ehdr = ehdr;
    117 	if (ehdr == MAP_FAILED) {
    118 		_rtld_error("%s: read error: %s", path, xstrerror(errno));
    119 		goto bad;
    120 	}
    121 	/* Make sure the file is valid */
    122 	if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
    123 	    ehdr->e_ident[EI_CLASS] != ELFCLASS) {
    124 		_rtld_error("%s: unrecognized file format2 [%x != %x]", path,
    125 		    ehdr->e_ident[EI_CLASS], ELFCLASS);
    126 		goto bad;
    127 	}
    128 	/* Elf_e_ident includes class */
    129 	if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
    130 	    ehdr->e_version != EV_CURRENT ||
    131 	    ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
    132 		_rtld_error("%s: unsupported file version", path);
    133 		goto bad;
    134 	}
    135 	if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
    136 		_rtld_error("%s: unsupported file type", path);
    137 		goto bad;
    138 	}
    139 	switch (ehdr->e_machine) {
    140 		ELFDEFNNAME(MACHDEP_ID_CASES)
    141 	default:
    142 		_rtld_error("%s: unsupported machine", path);
    143 		goto bad;
    144 	}
    145 
    146 	/*
    147          * We rely on the program header being in the first page.  This is
    148          * not strictly required by the ABI specification, but it seems to
    149          * always true in practice.  And, it simplifies things considerably.
    150          */
    151 	assert(ehdr->e_phentsize == sizeof(Elf_Phdr));
    152 	assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <=
    153 	    _rtld_pagesz);
    154 
    155 	/*
    156          * Scan the program header entries, and save key information.
    157          *
    158          * We rely on there being exactly two load segments, text and data,
    159          * in that order.
    160          */
    161 	phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
    162 	phsize = ehdr->e_phnum * sizeof(phdr[0]);
    163 	obj->phdr = NULL;
    164 	phdr_vaddr = EA_UNDEF;
    165 	phdr_memsz = 0;
    166 	phlimit = phdr + ehdr->e_phnum;
    167 	nsegs = 0;
    168 	while (phdr < phlimit) {
    169 		switch (phdr->p_type) {
    170 		case PT_INTERP:
    171 			obj->interp = (void *)(uintptr_t)phdr->p_vaddr;
    172  			dbg(("%s: PT_INTERP %p", obj->path, obj->interp));
    173 			break;
    174 
    175 		case PT_LOAD:
    176 			if (nsegs < 2)
    177 				segs[nsegs] = phdr;
    178 			++nsegs;
    179 			dbg(("%s: PT_LOAD %p", obj->path, phdr));
    180 			break;
    181 
    182 		case PT_PHDR:
    183 			phdr_vaddr = phdr->p_vaddr;
    184 			phdr_memsz = phdr->p_memsz;
    185 			dbg(("%s: PT_PHDR %p phsize %zu", obj->path,
    186 			    (void *)(uintptr_t)phdr_vaddr, phdr_memsz));
    187 			break;
    188 
    189 		case PT_DYNAMIC:
    190 			obj->dynamic = (void *)(uintptr_t)phdr->p_vaddr;
    191  			dbg(("%s: PT_DYNAMIC %p", obj->path, obj->dynamic));
    192 			break;
    193 		}
    194 
    195 		++phdr;
    196 	}
    197 	phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
    198 	obj->entry = (void *)(uintptr_t)ehdr->e_entry;
    199 	if (!obj->dynamic) {
    200 		_rtld_error("%s: not dynamically linked", path);
    201 		goto bad;
    202 	}
    203 	if (nsegs != 2) {
    204 		_rtld_error("%s: wrong number of segments (%d != 2)", path,
    205 		    nsegs);
    206 		goto bad;
    207 	}
    208 
    209 	/*
    210 	 * Map the entire address space of the object as a file
    211 	 * region to stake out our contiguous region and establish a
    212 	 * base for relocation.  We use a file mapping so that
    213 	 * the kernel will give us whatever alignment is appropriate
    214 	 * for the platform we're running on.
    215 	 *
    216 	 * We map it using the text protection, map the data segment
    217 	 * into the right place, then map an anon segment for the bss
    218 	 * and unmap the gaps left by padding to alignment.
    219 	 */
    220 
    221 #ifdef MAP_ALIGNED
    222 	base_alignment = segs[0]->p_align;
    223 #endif
    224 	base_offset = round_down(segs[0]->p_offset);
    225 	base_vaddr = round_down(segs[0]->p_vaddr);
    226 	base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
    227 	text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
    228 	text_flags = protflags(segs[0]->p_flags);
    229 	data_offset = round_down(segs[1]->p_offset);
    230 	data_vaddr = round_down(segs[1]->p_vaddr);
    231 	data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz);
    232 	data_flags = protflags(segs[1]->p_flags);
    233 #ifdef RTLD_LOADER
    234 	clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
    235 #endif
    236 
    237 	obj->textsize = text_vlimit - base_vaddr;
    238 	obj->vaddrbase = base_vaddr;
    239 	obj->isdynamic = ehdr->e_type == ET_DYN;
    240 
    241 	obj->phdr_loaded = false;
    242 	for (i = 0; i < nsegs; i++) {
    243 		if (phdr_vaddr != EA_UNDEF &&
    244 		    segs[i]->p_vaddr <= phdr_vaddr &&
    245 		    segs[i]->p_memsz >= phdr_memsz) {
    246 			obj->phdr_loaded = true;
    247 			break;
    248 		}
    249 		if (segs[i]->p_offset <= ehdr->e_phoff &&
    250 		    segs[i]->p_memsz >= phsize) {
    251 			phdr_vaddr = segs[i]->p_vaddr + ehdr->e_phoff;
    252 			phdr_memsz = phsize;
    253 			obj->phdr_loaded = true;
    254 			break;
    255 		}
    256 	}
    257 	if (obj->phdr_loaded) {
    258 		obj->phdr = (void *)(uintptr_t)phdr_vaddr;
    259 		obj->phsize = phdr_memsz;
    260 	} else {
    261 		Elf_Phdr *buf;
    262 		buf = xmalloc(phsize);
    263 		if (buf == NULL) {
    264 			_rtld_error("%s: cannot allocate program header", path);
    265 			goto bad;
    266 		}
    267 		memcpy(buf, phdr, phsize);
    268 		obj->phdr = buf;
    269 		obj->phsize = phsize;
    270 	}
    271 	dbg(("%s: phdr %p phsize %zu (%s)", obj->path, obj->phdr, obj->phsize,
    272 	     obj->phdr_loaded ? "loaded" : "allocated"));
    273 
    274 	/* Unmap header if it overlaps the first load section. */
    275 	if (base_offset < _rtld_pagesz) {
    276 		munmap(ehdr, _rtld_pagesz);
    277 		obj->ehdr = MAP_FAILED;
    278 	}
    279 
    280 	/*
    281 	 * Calculate log2 of the base section alignment.
    282 	 */
    283 	mapflags = 0;
    284 #ifdef MAP_ALIGNED
    285 	if (base_alignment > _rtld_pagesz) {
    286 		unsigned int log2 = 0;
    287 		for (; base_alignment > 1; base_alignment >>= 1)
    288 			log2++;
    289 		mapflags = MAP_ALIGNED(log2);
    290 	}
    291 #endif
    292 
    293 #ifdef RTLD_LOADER
    294 	base_addr = obj->isdynamic ? NULL : (caddr_t)base_vaddr;
    295 #else
    296 	base_addr = NULL;
    297 #endif
    298 	mapsize = base_vlimit - base_vaddr;
    299 	mapbase = mmap(base_addr, mapsize, text_flags,
    300 	    mapflags | MAP_FILE | MAP_PRIVATE, fd, base_offset);
    301 	if (mapbase == MAP_FAILED) {
    302 		_rtld_error("mmap of entire address space failed: %s",
    303 		    xstrerror(errno));
    304 		goto bad;
    305 	}
    306 
    307 	/* Overlay the data segment onto the proper region. */
    308 	data_addr = mapbase + (data_vaddr - base_vaddr);
    309 	if (mmap(data_addr, data_vlimit - data_vaddr, data_flags,
    310 	    MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) ==
    311 	    MAP_FAILED) {
    312 		_rtld_error("mmap of data failed: %s", xstrerror(errno));
    313 		goto bad;
    314 	}
    315 
    316 	/* Overlay the bss segment onto the proper region. */
    317 	if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit,
    318 	    data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) ==
    319 	    MAP_FAILED) {
    320 		_rtld_error("mmap of bss failed: %s", xstrerror(errno));
    321 		goto bad;
    322 	}
    323 
    324 	/* Unmap the gap between the text and data. */
    325 	gap_addr = mapbase + round_up(text_vlimit - base_vaddr);
    326 	gap_size = data_addr - gap_addr;
    327 	if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) {
    328 		_rtld_error("mprotect of text -> data gap failed: %s",
    329 		    xstrerror(errno));
    330 		goto bad;
    331 	}
    332 
    333 #ifdef RTLD_LOADER
    334 	/* Clear any BSS in the last page of the data segment. */
    335 	clear_addr = mapbase + (clear_vaddr - base_vaddr);
    336 	if ((nclear = data_vlimit - clear_vaddr) > 0)
    337 		memset(clear_addr, 0, nclear);
    338 
    339 	/* Non-file portion of BSS mapped above. */
    340 #endif
    341 
    342 	obj->mapbase = mapbase;
    343 	obj->mapsize = mapsize;
    344 	obj->relocbase = mapbase - base_vaddr;
    345 
    346 	if (obj->dynamic)
    347 		obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->dynamic);
    348 	if (obj->entry)
    349 		obj->entry = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->entry);
    350 	if (obj->interp)
    351 		obj->interp = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->interp);
    352 	if (obj->phdr_loaded)
    353 		obj->phdr =  (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->phdr);
    354 
    355 	return obj;
    356 
    357 bad:
    358 	if (obj->ehdr != MAP_FAILED)
    359 		munmap(obj->ehdr, _rtld_pagesz);
    360 	if (mapbase != MAP_FAILED)
    361 		munmap(mapbase, mapsize);
    362 	_rtld_obj_free(obj);
    363 	return NULL;
    364 }
    365 
    366 void
    367 _rtld_obj_free(Obj_Entry *obj)
    368 {
    369 	Objlist_Entry *elm;
    370 
    371 	xfree(obj->path);
    372 	while (obj->needed != NULL) {
    373 		Needed_Entry *needed = obj->needed;
    374 		obj->needed = needed->next;
    375 		xfree(needed);
    376 	}
    377 	while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
    378 		SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
    379 		xfree(elm);
    380 	}
    381 	while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
    382 		SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
    383 		xfree(elm);
    384 	}
    385 	if (!obj->phdr_loaded)
    386 		xfree((void *)(uintptr_t)obj->phdr);
    387 	xfree(obj);
    388 #ifdef COMBRELOC
    389 	_rtld_combreloc_reset(obj);
    390 #endif
    391 }
    392 
    393 Obj_Entry *
    394 _rtld_obj_new(void)
    395 {
    396 	Obj_Entry *obj;
    397 
    398 	obj = CNEW(Obj_Entry);
    399 	SIMPLEQ_INIT(&obj->dldags);
    400 	SIMPLEQ_INIT(&obj->dagmembers);
    401 	return obj;
    402 }
    403 
    404 /*
    405  * Given a set of ELF protection flags, return the corresponding protection
    406  * flags for MMAP.
    407  */
    408 static int
    409 protflags(int elfflags)
    410 {
    411 	int prot = 0;
    412 
    413 	if (elfflags & PF_R)
    414 		prot |= PROT_READ;
    415 #ifdef RTLD_LOADER
    416 	if (elfflags & PF_W)
    417 		prot |= PROT_WRITE;
    418 #endif
    419 	if (elfflags & PF_X)
    420 		prot |= PROT_EXEC;
    421 	return prot;
    422 }
    423