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