Home | History | Annotate | Line # | Download | only in ld.elf_so
map_object.c revision 1.62
      1  1.62   hannken /*	$NetBSD: map_object.c,v 1.62 2022/03/30 08:26:45 hannken 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.31     skrll #include <sys/cdefs.h>
     36  1.31     skrll #ifndef lint
     37  1.62   hannken __RCSID("$NetBSD: map_object.c,v 1.62 2022/03/30 08:26:45 hannken Exp $");
     38  1.31     skrll #endif /* not lint */
     39  1.31     skrll 
     40   1.1       cgd #include <errno.h>
     41   1.1       cgd #include <stddef.h>
     42  1.10   mycroft #include <stdlib.h>
     43   1.1       cgd #include <string.h>
     44   1.1       cgd #include <unistd.h>
     45  1.10   mycroft #include <sys/stat.h>
     46   1.1       cgd #include <sys/types.h>
     47   1.1       cgd #include <sys/mman.h>
     48   1.1       cgd 
     49  1.41     skrll #include "debug.h"
     50   1.1       cgd #include "rtld.h"
     51   1.7   hannken 
     52  1.30     skrll static int protflags(int);	/* Elf flags -> mmap protection */
     53   1.1       cgd 
     54  1.41     skrll #define EA_UNDEF		(~(Elf_Addr)0)
     55  1.41     skrll 
     56   1.1       cgd /*
     57   1.1       cgd  * Map a shared object into memory.  The argument is a file descriptor,
     58   1.1       cgd  * which must be open on the object and positioned at its beginning.
     59   1.1       cgd  *
     60   1.1       cgd  * The return value is a pointer to a newly-allocated Obj_Entry structure
     61   1.1       cgd  * for the shared object.  Returns NULL on failure.
     62   1.1       cgd  */
     63   1.1       cgd Obj_Entry *
     64  1.34  christos _rtld_map_object(const char *path, int fd, const struct stat *sb)
     65   1.1       cgd {
     66  1.18  junyoung 	Obj_Entry	*obj;
     67  1.18  junyoung 	Elf_Ehdr	*ehdr;
     68  1.18  junyoung 	Elf_Phdr	*phdr;
     69  1.42     joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
     70  1.42     joerg 	Elf_Phdr	*phtls;
     71  1.42     joerg #endif
     72  1.41     skrll 	size_t		 phsize;
     73  1.18  junyoung 	Elf_Phdr	*phlimit;
     74  1.18  junyoung 	Elf_Phdr	*segs[2];
     75  1.18  junyoung 	int		 nsegs;
     76  1.22   mycroft 	caddr_t		 mapbase = MAP_FAILED;
     77  1.32     lukem 	size_t		 mapsize = 0;
     78  1.27      matt 	int		 mapflags;
     79  1.18  junyoung 	Elf_Off		 base_offset;
     80  1.27      matt 	Elf_Addr	 base_alignment;
     81  1.18  junyoung 	Elf_Addr	 base_vaddr;
     82  1.18  junyoung 	Elf_Addr	 base_vlimit;
     83  1.18  junyoung 	Elf_Addr	 text_vlimit;
     84  1.22   mycroft 	int		 text_flags;
     85  1.60     joerg 	void		*base_addr;
     86  1.18  junyoung 	Elf_Off		 data_offset;
     87  1.18  junyoung 	Elf_Addr	 data_vaddr;
     88  1.18  junyoung 	Elf_Addr	 data_vlimit;
     89  1.22   mycroft 	int		 data_flags;
     90  1.18  junyoung 	caddr_t		 data_addr;
     91  1.42     joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
     92  1.42     joerg 	Elf_Addr	 tls_vaddr = 0; /* Noise GCC */
     93  1.42     joerg #endif
     94  1.41     skrll 	Elf_Addr	 phdr_vaddr;
     95  1.41     skrll 	size_t		 phdr_memsz;
     96  1.18  junyoung 	caddr_t		 gap_addr;
     97  1.18  junyoung 	size_t		 gap_size;
     98  1.41     skrll 	int i;
     99   1.1       cgd #ifdef RTLD_LOADER
    100  1.18  junyoung 	Elf_Addr	 clear_vaddr;
    101  1.18  junyoung 	caddr_t		 clear_addr;
    102  1.18  junyoung 	size_t		 nclear;
    103   1.1       cgd #endif
    104  1.54  christos #ifdef GNU_RELRO
    105  1.54  christos 	Elf_Addr 	 relro_page;
    106  1.54  christos 	size_t		 relro_size;
    107  1.54  christos #endif
    108  1.26      fvdl 
    109  1.38  christos 	if (sb != NULL && sb->st_size < (off_t)sizeof (Elf_Ehdr)) {
    110  1.45  dholland 		_rtld_error("%s: not ELF file (too short)", path);
    111  1.26      fvdl 		return NULL;
    112  1.26      fvdl 	}
    113   1.1       cgd 
    114  1.22   mycroft 	obj = _rtld_obj_new();
    115  1.34  christos 	obj->path = xstrdup(path);
    116  1.25  junyoung 	obj->pathlen = strlen(path);
    117  1.22   mycroft 	if (sb != NULL) {
    118  1.22   mycroft 		obj->dev = sb->st_dev;
    119  1.22   mycroft 		obj->ino = sb->st_ino;
    120  1.22   mycroft 	}
    121  1.22   mycroft 
    122  1.20   mycroft 	ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd,
    123  1.16   mycroft 	    (off_t)0);
    124  1.36        ad 	obj->ehdr = ehdr;
    125  1.20   mycroft 	if (ehdr == MAP_FAILED) {
    126   1.4  christos 		_rtld_error("%s: read error: %s", path, xstrerror(errno));
    127  1.22   mycroft 		goto bad;
    128   1.4  christos 	}
    129   1.4  christos 	/* Make sure the file is valid */
    130  1.45  dholland 	if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0) {
    131  1.45  dholland 		_rtld_error("%s: not ELF file (magic number bad)", path);
    132  1.45  dholland 		goto bad;
    133  1.45  dholland 	}
    134  1.45  dholland 	if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
    135  1.45  dholland 		_rtld_error("%s: invalid ELF class %x; expected %x", path,
    136  1.40     skrll 		    ehdr->e_ident[EI_CLASS], ELFCLASS);
    137  1.16   mycroft 		goto bad;
    138   1.4  christos 	}
    139   1.4  christos 	/* Elf_e_ident includes class */
    140  1.17  junyoung 	if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
    141  1.17  junyoung 	    ehdr->e_version != EV_CURRENT ||
    142  1.17  junyoung 	    ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
    143  1.16   mycroft 		_rtld_error("%s: unsupported file version", path);
    144  1.16   mycroft 		goto bad;
    145  1.16   mycroft 	}
    146  1.17  junyoung 	if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
    147  1.16   mycroft 		_rtld_error("%s: unsupported file type", path);
    148  1.16   mycroft 		goto bad;
    149   1.4  christos 	}
    150  1.17  junyoung 	switch (ehdr->e_machine) {
    151   1.4  christos 		ELFDEFNNAME(MACHDEP_ID_CASES)
    152   1.4  christos 	default:
    153  1.16   mycroft 		_rtld_error("%s: unsupported machine", path);
    154  1.16   mycroft 		goto bad;
    155   1.4  christos 	}
    156   1.4  christos 
    157   1.4  christos 	/*
    158   1.4  christos          * We rely on the program header being in the first page.  This is
    159   1.4  christos          * not strictly required by the ABI specification, but it seems to
    160   1.4  christos          * always true in practice.  And, it simplifies things considerably.
    161   1.4  christos          */
    162  1.17  junyoung 	assert(ehdr->e_phentsize == sizeof(Elf_Phdr));
    163  1.19  junyoung 	assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <=
    164  1.19  junyoung 	    _rtld_pagesz);
    165   1.4  christos 
    166   1.4  christos 	/*
    167   1.4  christos          * Scan the program header entries, and save key information.
    168   1.4  christos          *
    169   1.4  christos          * We rely on there being exactly two load segments, text and data,
    170   1.4  christos          * in that order.
    171   1.4  christos          */
    172  1.17  junyoung 	phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
    173  1.42     joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    174  1.42     joerg 	phtls = NULL;
    175  1.42     joerg #endif
    176  1.41     skrll 	phsize = ehdr->e_phnum * sizeof(phdr[0]);
    177  1.41     skrll 	obj->phdr = NULL;
    178  1.54  christos #ifdef GNU_RELRO
    179  1.54  christos 	relro_page = 0;
    180  1.54  christos 	relro_size = 0;
    181  1.54  christos #endif
    182  1.41     skrll 	phdr_vaddr = EA_UNDEF;
    183  1.41     skrll 	phdr_memsz = 0;
    184  1.17  junyoung 	phlimit = phdr + ehdr->e_phnum;
    185   1.4  christos 	nsegs = 0;
    186   1.4  christos 	while (phdr < phlimit) {
    187   1.4  christos 		switch (phdr->p_type) {
    188  1.10   mycroft 		case PT_INTERP:
    189  1.37       mrg 			obj->interp = (void *)(uintptr_t)phdr->p_vaddr;
    190  1.41     skrll  			dbg(("%s: PT_INTERP %p", obj->path, obj->interp));
    191  1.10   mycroft 			break;
    192   1.1       cgd 
    193   1.8    kleink 		case PT_LOAD:
    194  1.12   mycroft 			if (nsegs < 2)
    195  1.12   mycroft 				segs[nsegs] = phdr;
    196   1.4  christos 			++nsegs;
    197  1.44    martin 
    198  1.44    martin 			dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_LOAD",
    199  1.43  christos 			    (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
    200   1.4  christos 			break;
    201   1.4  christos 
    202  1.41     skrll 		case PT_PHDR:
    203  1.41     skrll 			phdr_vaddr = phdr->p_vaddr;
    204  1.41     skrll 			phdr_memsz = phdr->p_memsz;
    205  1.44    martin 			dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_PHDR",
    206  1.43  christos 			    (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
    207  1.41     skrll 			break;
    208  1.48     skrll 
    209  1.54  christos #ifdef GNU_RELRO
    210  1.54  christos 		case PT_GNU_RELRO:
    211  1.54  christos 			relro_page = phdr->p_vaddr;
    212  1.54  christos 			relro_size = phdr->p_memsz;
    213  1.54  christos 			break;
    214  1.54  christos #endif
    215  1.54  christos 
    216   1.8    kleink 		case PT_DYNAMIC:
    217  1.37       mrg 			obj->dynamic = (void *)(uintptr_t)phdr->p_vaddr;
    218  1.44    martin 			dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_DYNAMIC",
    219  1.43  christos 			    (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
    220   1.4  christos 			break;
    221  1.42     joerg 
    222  1.42     joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    223  1.42     joerg 		case PT_TLS:
    224  1.42     joerg 			phtls = phdr;
    225  1.44    martin 			dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_TLS",
    226  1.43  christos 			    (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
    227  1.42     joerg 			break;
    228  1.42     joerg #endif
    229  1.46     skrll #ifdef __ARM_EABI__
    230  1.46     skrll 		case PT_ARM_EXIDX:
    231  1.46     skrll 			obj->exidx_start = (void *)(uintptr_t)phdr->p_vaddr;
    232  1.46     skrll 			obj->exidx_sz = phdr->p_memsz;
    233  1.46     skrll 			break;
    234  1.46     skrll #endif
    235   1.4  christos 		}
    236   1.1       cgd 
    237   1.4  christos 		++phdr;
    238   1.4  christos 	}
    239  1.41     skrll 	phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
    240  1.37       mrg 	obj->entry = (void *)(uintptr_t)ehdr->e_entry;
    241  1.22   mycroft 	if (!obj->dynamic) {
    242  1.12   mycroft 		_rtld_error("%s: not dynamically linked", path);
    243  1.16   mycroft 		goto bad;
    244  1.12   mycroft 	}
    245  1.12   mycroft 	if (nsegs != 2) {
    246  1.12   mycroft 		_rtld_error("%s: wrong number of segments (%d != 2)", path,
    247  1.12   mycroft 		    nsegs);
    248  1.16   mycroft 		goto bad;
    249   1.4  christos 	}
    250   1.1       cgd 
    251   1.4  christos 	/*
    252  1.11       chs 	 * Map the entire address space of the object as a file
    253   1.5   thorpej 	 * region to stake out our contiguous region and establish a
    254  1.11       chs 	 * base for relocation.  We use a file mapping so that
    255  1.11       chs 	 * the kernel will give us whatever alignment is appropriate
    256  1.11       chs 	 * for the platform we're running on.
    257   1.5   thorpej 	 *
    258  1.11       chs 	 * We map it using the text protection, map the data segment
    259  1.11       chs 	 * into the right place, then map an anon segment for the bss
    260  1.11       chs 	 * and unmap the gaps left by padding to alignment.
    261   1.5   thorpej 	 */
    262  1.11       chs 
    263  1.27      matt 	base_alignment = segs[0]->p_align;
    264   1.4  christos 	base_offset = round_down(segs[0]->p_offset);
    265   1.4  christos 	base_vaddr = round_down(segs[0]->p_vaddr);
    266   1.4  christos 	base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
    267  1.11       chs 	text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
    268  1.22   mycroft 	text_flags = protflags(segs[0]->p_flags);
    269  1.22   mycroft 	data_offset = round_down(segs[1]->p_offset);
    270  1.22   mycroft 	data_vaddr = round_down(segs[1]->p_vaddr);
    271  1.22   mycroft 	data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz);
    272  1.22   mycroft 	data_flags = protflags(segs[1]->p_flags);
    273  1.23   mycroft #ifdef RTLD_LOADER
    274  1.22   mycroft 	clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
    275  1.23   mycroft #endif
    276  1.22   mycroft 
    277  1.22   mycroft 	obj->textsize = text_vlimit - base_vaddr;
    278  1.22   mycroft 	obj->vaddrbase = base_vaddr;
    279  1.22   mycroft 	obj->isdynamic = ehdr->e_type == ET_DYN;
    280  1.22   mycroft 
    281  1.42     joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    282  1.42     joerg 	if (phtls != NULL) {
    283  1.42     joerg 		++_rtld_tls_dtv_generation;
    284  1.42     joerg 		obj->tlsindex = ++_rtld_tls_max_index;
    285  1.42     joerg 		obj->tlssize = phtls->p_memsz;
    286  1.42     joerg 		obj->tlsalign = phtls->p_align;
    287  1.42     joerg 		obj->tlsinitsize = phtls->p_filesz;
    288  1.42     joerg 		tls_vaddr = phtls->p_vaddr;
    289  1.42     joerg 	}
    290  1.42     joerg #endif
    291  1.42     joerg 
    292  1.41     skrll 	obj->phdr_loaded = false;
    293  1.41     skrll 	for (i = 0; i < nsegs; i++) {
    294  1.41     skrll 		if (phdr_vaddr != EA_UNDEF &&
    295  1.41     skrll 		    segs[i]->p_vaddr <= phdr_vaddr &&
    296  1.41     skrll 		    segs[i]->p_memsz >= phdr_memsz) {
    297  1.41     skrll 			obj->phdr_loaded = true;
    298  1.41     skrll 			break;
    299  1.41     skrll 		}
    300  1.41     skrll 		if (segs[i]->p_offset <= ehdr->e_phoff &&
    301  1.41     skrll 		    segs[i]->p_memsz >= phsize) {
    302  1.41     skrll 			phdr_vaddr = segs[i]->p_vaddr + ehdr->e_phoff;
    303  1.41     skrll 			phdr_memsz = phsize;
    304  1.41     skrll 			obj->phdr_loaded = true;
    305  1.41     skrll 			break;
    306  1.41     skrll 		}
    307  1.41     skrll 	}
    308  1.41     skrll 	if (obj->phdr_loaded) {
    309  1.41     skrll 		obj->phdr = (void *)(uintptr_t)phdr_vaddr;
    310  1.41     skrll 		obj->phsize = phdr_memsz;
    311  1.41     skrll 	} else {
    312  1.41     skrll 		Elf_Phdr *buf;
    313  1.41     skrll 		buf = xmalloc(phsize);
    314  1.41     skrll 		if (buf == NULL) {
    315  1.41     skrll 			_rtld_error("%s: cannot allocate program header", path);
    316  1.41     skrll 			goto bad;
    317  1.41     skrll 		}
    318  1.41     skrll 		memcpy(buf, phdr, phsize);
    319  1.41     skrll 		obj->phdr = buf;
    320  1.41     skrll 		obj->phsize = phsize;
    321  1.41     skrll 	}
    322  1.41     skrll 	dbg(("%s: phdr %p phsize %zu (%s)", obj->path, obj->phdr, obj->phsize,
    323  1.41     skrll 	     obj->phdr_loaded ? "loaded" : "allocated"));
    324  1.41     skrll 
    325  1.36        ad 	/* Unmap header if it overlaps the first load section. */
    326  1.36        ad 	if (base_offset < _rtld_pagesz) {
    327  1.36        ad 		munmap(ehdr, _rtld_pagesz);
    328  1.36        ad 		obj->ehdr = MAP_FAILED;
    329  1.36        ad 	}
    330  1.11       chs 
    331  1.27      matt 	/*
    332  1.27      matt 	 * Calculate log2 of the base section alignment.
    333  1.27      matt 	 */
    334  1.27      matt 	mapflags = 0;
    335  1.27      matt 	if (base_alignment > _rtld_pagesz) {
    336  1.27      matt 		unsigned int log2 = 0;
    337  1.27      matt 		for (; base_alignment > 1; base_alignment >>= 1)
    338  1.27      matt 			log2++;
    339  1.27      matt 		mapflags = MAP_ALIGNED(log2);
    340  1.27      matt 	}
    341  1.27      matt 
    342  1.60     joerg 	base_addr = NULL;
    343   1.1       cgd #ifdef RTLD_LOADER
    344  1.60     joerg 	if (!obj->isdynamic) {
    345  1.60     joerg 		mapflags |= MAP_TRYFIXED;
    346  1.60     joerg 		base_addr = (void *)(uintptr_t)base_vaddr;
    347  1.60     joerg 	}
    348   1.1       cgd #endif
    349  1.22   mycroft 	mapsize = base_vlimit - base_vaddr;
    350  1.27      matt 	mapbase = mmap(base_addr, mapsize, text_flags,
    351  1.27      matt 	    mapflags | MAP_FILE | MAP_PRIVATE, fd, base_offset);
    352   1.5   thorpej 	if (mapbase == MAP_FAILED) {
    353   1.4  christos 		_rtld_error("mmap of entire address space failed: %s",
    354   1.4  christos 		    xstrerror(errno));
    355  1.16   mycroft 		goto bad;
    356   1.4  christos 	}
    357  1.60     joerg #ifdef RTLD_LOADER
    358  1.60     joerg 	if (!obj->isdynamic && mapbase != base_addr) {
    359  1.60     joerg 		_rtld_error("mmap of executable at correct address failed");
    360  1.60     joerg 		goto bad;
    361  1.60     joerg 	}
    362  1.60     joerg #endif
    363  1.11       chs 
    364   1.4  christos 	/* Overlay the data segment onto the proper region. */
    365   1.4  christos 	data_addr = mapbase + (data_vaddr - base_vaddr);
    366  1.57      maya 	if (mmap(data_addr, data_vlimit - data_vaddr, data_flags,
    367  1.22   mycroft 	    MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) ==
    368  1.22   mycroft 	    MAP_FAILED) {
    369   1.4  christos 		_rtld_error("mmap of data failed: %s", xstrerror(errno));
    370  1.22   mycroft 		goto bad;
    371  1.11       chs 	}
    372  1.11       chs 
    373  1.11       chs 	/* Overlay the bss segment onto the proper region. */
    374  1.62   hannken 	if (base_vlimit > data_vlimit) {
    375  1.62   hannken 		if (mmap(mapbase + data_vlimit - base_vaddr,
    376  1.62   hannken 		    base_vlimit - data_vlimit, data_flags,
    377  1.62   hannken 		    MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) == MAP_FAILED) {
    378  1.62   hannken 			_rtld_error("mmap of bss failed: %s", xstrerror(errno));
    379  1.62   hannken 			goto bad;
    380  1.62   hannken 		}
    381   1.4  christos 	}
    382   1.5   thorpej 
    383   1.5   thorpej 	/* Unmap the gap between the text and data. */
    384  1.22   mycroft 	gap_addr = mapbase + round_up(text_vlimit - base_vaddr);
    385   1.5   thorpej 	gap_size = data_addr - gap_addr;
    386  1.57      maya 	if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) {
    387  1.57      maya 		_rtld_error("mprotect of text -> data gap failed: %s",
    388   1.5   thorpej 		    xstrerror(errno));
    389  1.22   mycroft 		goto bad;
    390   1.5   thorpej 	}
    391   1.5   thorpej 
    392   1.1       cgd #ifdef RTLD_LOADER
    393   1.4  christos 	/* Clear any BSS in the last page of the data segment. */
    394   1.4  christos 	clear_addr = mapbase + (clear_vaddr - base_vaddr);
    395   1.4  christos 	if ((nclear = data_vlimit - clear_vaddr) > 0)
    396   1.4  christos 		memset(clear_addr, 0, nclear);
    397   1.4  christos 
    398   1.5   thorpej 	/* Non-file portion of BSS mapped above. */
    399   1.1       cgd #endif
    400   1.1       cgd 
    401  1.42     joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    402  1.42     joerg 	if (phtls != NULL)
    403  1.42     joerg 		obj->tlsinit = mapbase + tls_vaddr;
    404  1.42     joerg #endif
    405  1.42     joerg 
    406   1.4  christos 	obj->mapbase = mapbase;
    407   1.4  christos 	obj->mapsize = mapsize;
    408   1.4  christos 	obj->relocbase = mapbase - base_vaddr;
    409  1.10   mycroft 
    410  1.55  christos #ifdef GNU_RELRO
    411  1.61   thorpej 	/* rounding happens later. */
    412  1.61   thorpej 	obj->relro_page = obj->relocbase + relro_page;
    413  1.61   thorpej 	obj->relro_size = relro_size;
    414  1.55  christos #endif
    415  1.55  christos 
    416  1.22   mycroft 	if (obj->dynamic)
    417  1.37       mrg 		obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->dynamic);
    418  1.22   mycroft 	if (obj->entry)
    419  1.37       mrg 		obj->entry = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->entry);
    420  1.22   mycroft 	if (obj->interp)
    421  1.37       mrg 		obj->interp = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->interp);
    422  1.41     skrll 	if (obj->phdr_loaded)
    423  1.41     skrll 		obj->phdr =  (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->phdr);
    424  1.47     skrll #ifdef __ARM_EABI__
    425  1.47     skrll 	if (obj->exidx_start)
    426  1.47     skrll 		obj->exidx_start = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->exidx_start);
    427  1.47     skrll #endif
    428  1.22   mycroft 
    429  1.10   mycroft 	return obj;
    430  1.16   mycroft 
    431  1.16   mycroft bad:
    432  1.36        ad 	if (obj->ehdr != MAP_FAILED)
    433  1.36        ad 		munmap(obj->ehdr, _rtld_pagesz);
    434  1.22   mycroft 	if (mapbase != MAP_FAILED)
    435  1.22   mycroft 		munmap(mapbase, mapsize);
    436  1.22   mycroft 	_rtld_obj_free(obj);
    437  1.16   mycroft 	return NULL;
    438  1.10   mycroft }
    439  1.10   mycroft 
    440  1.10   mycroft void
    441  1.30     skrll _rtld_obj_free(Obj_Entry *obj)
    442  1.10   mycroft {
    443  1.10   mycroft 	Objlist_Entry *elm;
    444  1.51  christos 	Name_Entry *entry;
    445  1.10   mycroft 
    446  1.42     joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    447  1.42     joerg 	if (obj->tls_done)
    448  1.42     joerg 		_rtld_tls_offset_free(obj);
    449  1.42     joerg #endif
    450  1.35        ad 	xfree(obj->path);
    451  1.10   mycroft 	while (obj->needed != NULL) {
    452  1.10   mycroft 		Needed_Entry *needed = obj->needed;
    453  1.10   mycroft 		obj->needed = needed->next;
    454  1.35        ad 		xfree(needed);
    455  1.10   mycroft 	}
    456  1.51  christos 	while ((entry = SIMPLEQ_FIRST(&obj->names)) != NULL) {
    457  1.51  christos 		SIMPLEQ_REMOVE_HEAD(&obj->names, link);
    458  1.50  christos 		xfree(entry);
    459  1.49  christos 	}
    460  1.13     lukem 	while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
    461  1.13     lukem 		SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
    462  1.35        ad 		xfree(elm);
    463  1.10   mycroft 	}
    464  1.13     lukem 	while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
    465  1.13     lukem 		SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
    466  1.35        ad 		xfree(elm);
    467  1.10   mycroft 	}
    468  1.41     skrll 	if (!obj->phdr_loaded)
    469  1.41     skrll 		xfree((void *)(uintptr_t)obj->phdr);
    470  1.53    martin 	xfree(obj);
    471  1.10   mycroft }
    472  1.10   mycroft 
    473  1.10   mycroft Obj_Entry *
    474  1.10   mycroft _rtld_obj_new(void)
    475  1.10   mycroft {
    476  1.10   mycroft 	Obj_Entry *obj;
    477  1.10   mycroft 
    478  1.10   mycroft 	obj = CNEW(Obj_Entry);
    479  1.51  christos 	SIMPLEQ_INIT(&obj->names);
    480  1.10   mycroft 	SIMPLEQ_INIT(&obj->dldags);
    481  1.10   mycroft 	SIMPLEQ_INIT(&obj->dagmembers);
    482   1.4  christos 	return obj;
    483   1.1       cgd }
    484   1.1       cgd 
    485   1.1       cgd /*
    486   1.1       cgd  * Given a set of ELF protection flags, return the corresponding protection
    487   1.1       cgd  * flags for MMAP.
    488   1.1       cgd  */
    489   1.1       cgd static int
    490  1.30     skrll protflags(int elfflags)
    491   1.1       cgd {
    492   1.4  christos 	int prot = 0;
    493  1.29    simonb 
    494   1.8    kleink 	if (elfflags & PF_R)
    495   1.4  christos 		prot |= PROT_READ;
    496   1.1       cgd #ifdef RTLD_LOADER
    497   1.8    kleink 	if (elfflags & PF_W)
    498   1.4  christos 		prot |= PROT_WRITE;
    499   1.1       cgd #endif
    500   1.8    kleink 	if (elfflags & PF_X)
    501   1.4  christos 		prot |= PROT_EXEC;
    502   1.4  christos 	return prot;
    503   1.1       cgd }
    504