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