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