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