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