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