Home | History | Annotate | Line # | Download | only in ld.elf_so
map_object.c revision 1.23
      1 /*	$NetBSD: map_object.c,v 1.23 2002/10/05 00:13:27 mycroft Exp $	 */
      2 
      3 /*
      4  * Copyright 1996 John D. Polstra.
      5  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by John Polstra.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <errno.h>
     35 #include <stddef.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 #include <unistd.h>
     39 #include <sys/stat.h>
     40 #include <sys/types.h>
     41 #include <sys/mman.h>
     42 
     43 #include "rtld.h"
     44 
     45 static int protflags __P((int));	/* Elf flags -> mmap protection */
     46 
     47 /*
     48  * Map a shared object into memory.  The argument is a file descriptor,
     49  * which must be open on the object and positioned at its beginning.
     50  *
     51  * The return value is a pointer to a newly-allocated Obj_Entry structure
     52  * for the shared object.  Returns NULL on failure.
     53  */
     54 Obj_Entry *
     55 _rtld_map_object(path, fd, sb)
     56 	char *path;
     57 	int fd;
     58 	const struct stat *sb;
     59 {
     60 	Obj_Entry	*obj;
     61 	Elf_Ehdr	*ehdr;
     62 	Elf_Phdr	*phdr;
     63 	Elf_Phdr	*phlimit;
     64 	Elf_Phdr	*segs[2];
     65 	int		 nsegs;
     66 	caddr_t		 mapbase = MAP_FAILED;
     67 	size_t		 mapsize;
     68 	Elf_Off		 base_offset;
     69 	Elf_Addr	 base_vaddr;
     70 	Elf_Addr	 base_vlimit;
     71 	Elf_Addr	 text_vlimit;
     72 	int		 text_flags;
     73 	caddr_t		 base_addr;
     74 	Elf_Off		 data_offset;
     75 	Elf_Addr	 data_vaddr;
     76 	Elf_Addr	 data_vlimit;
     77 	int		 data_flags;
     78 	caddr_t		 data_addr;
     79 	caddr_t		 gap_addr;
     80 	size_t		 gap_size;
     81 #ifdef RTLD_LOADER
     82 	Elf_Addr	 clear_vaddr;
     83 	caddr_t		 clear_addr;
     84 	size_t		 nclear;
     85 #endif
     86 
     87 	obj = _rtld_obj_new();
     88 	obj->path = path;
     89 	if (sb != NULL) {
     90 		obj->dev = sb->st_dev;
     91 		obj->ino = sb->st_ino;
     92 	}
     93 
     94 	ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd,
     95 	    (off_t)0);
     96 	if (ehdr == MAP_FAILED) {
     97 		_rtld_error("%s: read error: %s", path, xstrerror(errno));
     98 		goto bad;
     99 	}
    100 	/* Make sure the file is valid */
    101 	if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
    102 	    ehdr->e_ident[EI_CLASS] != ELFCLASS) {
    103 		_rtld_error("%s: unrecognized file format", path);
    104 		goto bad;
    105 	}
    106 	/* Elf_e_ident includes class */
    107 	if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
    108 	    ehdr->e_version != EV_CURRENT ||
    109 	    ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
    110 		_rtld_error("%s: unsupported file version", path);
    111 		goto bad;
    112 	}
    113 	if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
    114 		_rtld_error("%s: unsupported file type", path);
    115 		goto bad;
    116 	}
    117 	switch (ehdr->e_machine) {
    118 		ELFDEFNNAME(MACHDEP_ID_CASES)
    119 	default:
    120 		_rtld_error("%s: unsupported machine", path);
    121 		goto bad;
    122 	}
    123 
    124 	/*
    125          * We rely on the program header being in the first page.  This is
    126          * not strictly required by the ABI specification, but it seems to
    127          * always true in practice.  And, it simplifies things considerably.
    128          */
    129 	assert(ehdr->e_phentsize == sizeof(Elf_Phdr));
    130 	assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <=
    131 	    _rtld_pagesz);
    132 
    133 	/*
    134          * Scan the program header entries, and save key information.
    135          *
    136          * We rely on there being exactly two load segments, text and data,
    137          * in that order.
    138          */
    139 	phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
    140 	phlimit = phdr + ehdr->e_phnum;
    141 	nsegs = 0;
    142 	while (phdr < phlimit) {
    143 		switch (phdr->p_type) {
    144 		case PT_INTERP:
    145 			obj->interp = (void *)phdr->p_vaddr;
    146 			break;
    147 
    148 		case PT_LOAD:
    149 			if (nsegs < 2)
    150 				segs[nsegs] = phdr;
    151 			++nsegs;
    152 			break;
    153 
    154 		case PT_DYNAMIC:
    155 			obj->dynamic = (void *)phdr->p_vaddr;
    156 			break;
    157 		}
    158 
    159 		++phdr;
    160 	}
    161 	obj->entry = (void *)ehdr->e_entry;
    162 	if (!obj->dynamic) {
    163 		_rtld_error("%s: not dynamically linked", path);
    164 		goto bad;
    165 	}
    166 	if (nsegs != 2) {
    167 		_rtld_error("%s: wrong number of segments (%d != 2)", path,
    168 		    nsegs);
    169 		goto bad;
    170 	}
    171 
    172 	/*
    173 	 * Map the entire address space of the object as a file
    174 	 * region to stake out our contiguous region and establish a
    175 	 * base for relocation.  We use a file mapping so that
    176 	 * the kernel will give us whatever alignment is appropriate
    177 	 * for the platform we're running on.
    178 	 *
    179 	 * We map it using the text protection, map the data segment
    180 	 * into the right place, then map an anon segment for the bss
    181 	 * and unmap the gaps left by padding to alignment.
    182 	 */
    183 
    184 	base_offset = round_down(segs[0]->p_offset);
    185 	base_vaddr = round_down(segs[0]->p_vaddr);
    186 	base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
    187 	text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
    188 	text_flags = protflags(segs[0]->p_flags);
    189 	data_offset = round_down(segs[1]->p_offset);
    190 	data_vaddr = round_down(segs[1]->p_vaddr);
    191 	data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz);
    192 	data_flags = protflags(segs[1]->p_flags);
    193 #ifdef RTLD_LOADER
    194 	clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
    195 #endif
    196 
    197 	obj->textsize = text_vlimit - base_vaddr;
    198 	obj->vaddrbase = base_vaddr;
    199 	obj->isdynamic = ehdr->e_type == ET_DYN;
    200 
    201 	munmap(ehdr, _rtld_pagesz);
    202 	ehdr = MAP_FAILED;
    203 
    204 #ifdef RTLD_LOADER
    205 	base_addr = obj->isdynamic ? NULL : (caddr_t)base_vaddr;
    206 #else
    207 	base_addr = NULL;
    208 #endif
    209 	mapsize = base_vlimit - base_vaddr;
    210 	mapbase = mmap(base_addr, mapsize, text_flags, MAP_FILE | MAP_PRIVATE,
    211 	    fd, base_offset);
    212 	if (mapbase == MAP_FAILED) {
    213 		_rtld_error("mmap of entire address space failed: %s",
    214 		    xstrerror(errno));
    215 		goto bad;
    216 	}
    217 
    218 	/* Overlay the data segment onto the proper region. */
    219 	data_addr = mapbase + (data_vaddr - base_vaddr);
    220 	if (mmap(data_addr, data_vlimit - data_vaddr, data_flags,
    221 	    MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) ==
    222 	    MAP_FAILED) {
    223 		_rtld_error("mmap of data failed: %s", xstrerror(errno));
    224 		goto bad;
    225 	}
    226 
    227 	/* Overlay the bss segment onto the proper region. */
    228 	if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit,
    229 	    data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) ==
    230 	    MAP_FAILED) {
    231 		_rtld_error("mmap of bss failed: %s", xstrerror(errno));
    232 		goto bad;
    233 	}
    234 
    235 	/* Unmap the gap between the text and data. */
    236 	gap_addr = mapbase + round_up(text_vlimit - base_vaddr);
    237 	gap_size = data_addr - gap_addr;
    238 	if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) {
    239 		_rtld_error("mprotect of text -> data gap failed: %s",
    240 		    xstrerror(errno));
    241 		goto bad;
    242 	}
    243 
    244 #ifdef RTLD_LOADER
    245 	/* Clear any BSS in the last page of the data segment. */
    246 	clear_addr = mapbase + (clear_vaddr - base_vaddr);
    247 	if ((nclear = data_vlimit - clear_vaddr) > 0)
    248 		memset(clear_addr, 0, nclear);
    249 
    250 	/* Non-file portion of BSS mapped above. */
    251 #endif
    252 
    253 	obj->mapbase = mapbase;
    254 	obj->mapsize = mapsize;
    255 	obj->relocbase = mapbase - base_vaddr;
    256 
    257 	if (obj->dynamic)
    258 		obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)obj->dynamic);
    259 	if (obj->entry)
    260 		obj->entry = (void *)(obj->relocbase + (Elf_Addr)obj->entry);
    261 	if (obj->interp)
    262 		obj->interp = (void *)(obj->relocbase + (Elf_Addr)obj->interp);
    263 
    264 	return obj;
    265 
    266 bad:
    267 	if (ehdr != MAP_FAILED)
    268 		munmap(ehdr, _rtld_pagesz);
    269 	if (mapbase != MAP_FAILED)
    270 		munmap(mapbase, mapsize);
    271 	_rtld_obj_free(obj);
    272 	return NULL;
    273 }
    274 
    275 void
    276 _rtld_obj_free(obj)
    277 	Obj_Entry *obj;
    278 {
    279 	Objlist_Entry *elm;
    280 
    281 	free(obj->path);
    282 	while (obj->needed != NULL) {
    283 		Needed_Entry *needed = obj->needed;
    284 		obj->needed = needed->next;
    285 		free(needed);
    286 	}
    287 	while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
    288 		SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
    289 		free(elm);
    290 	}
    291 	while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
    292 		SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
    293 		free(elm);
    294 	}
    295 	free(obj);
    296 }
    297 
    298 Obj_Entry *
    299 _rtld_obj_new(void)
    300 {
    301 	Obj_Entry *obj;
    302 
    303 	obj = CNEW(Obj_Entry);
    304 	SIMPLEQ_INIT(&obj->dldags);
    305 	SIMPLEQ_INIT(&obj->dagmembers);
    306 	return obj;
    307 }
    308 
    309 /*
    310  * Given a set of ELF protection flags, return the corresponding protection
    311  * flags for MMAP.
    312  */
    313 static int
    314 protflags(elfflags)
    315 	int elfflags;
    316 {
    317 	int prot = 0;
    318 	if (elfflags & PF_R)
    319 		prot |= PROT_READ;
    320 #ifdef RTLD_LOADER
    321 	if (elfflags & PF_W)
    322 		prot |= PROT_WRITE;
    323 #endif
    324 	if (elfflags & PF_X)
    325 		prot |= PROT_EXEC;
    326 	return prot;
    327 }
    328