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