Home | History | Annotate | Line # | Download | only in ld.elf_so
headers.c revision 1.48
      1 /*	$NetBSD: headers.c,v 1.48 2013/05/02 21:11:03 matt 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 /*
     36  * Dynamic linker for ELF.
     37  *
     38  * John Polstra <jdp (at) polstra.com>.
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 #ifndef lint
     43 __RCSID("$NetBSD: headers.c,v 1.48 2013/05/02 21:11:03 matt Exp $");
     44 #endif /* not lint */
     45 
     46 #include <err.h>
     47 #include <errno.h>
     48 #include <fcntl.h>
     49 #include <stdarg.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 #include <sys/types.h>
     55 #include <sys/mman.h>
     56 #include <sys/bitops.h>
     57 #include <dirent.h>
     58 
     59 #include "debug.h"
     60 #include "rtld.h"
     61 
     62 /*
     63  * Process a shared object's DYNAMIC section, and save the important
     64  * information in its Obj_Entry structure.
     65  */
     66 void
     67 _rtld_digest_dynamic(const char *execname, Obj_Entry *obj)
     68 {
     69 	Elf_Dyn        *dynp;
     70 	Needed_Entry  **needed_tail = &obj->needed;
     71 	const Elf_Dyn  *dyn_rpath = NULL;
     72 	bool		use_pltrel = false;
     73 	bool		use_pltrela = false;
     74 	Elf_Addr        relsz = 0, relasz = 0;
     75 	Elf_Addr	pltrel = 0, pltrelsz = 0;
     76 	Elf_Addr	init = 0, fini = 0;
     77 
     78 	for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; ++dynp) {
     79 		switch (dynp->d_tag) {
     80 
     81 		case DT_REL:
     82 			obj->rel = (const Elf_Rel *)
     83 			    (obj->relocbase + dynp->d_un.d_ptr);
     84 			break;
     85 
     86 		case DT_RELSZ:
     87 			relsz = dynp->d_un.d_val;
     88 			break;
     89 
     90 		case DT_RELENT:
     91 			assert(dynp->d_un.d_val == sizeof(Elf_Rel));
     92 			break;
     93 
     94 		case DT_JMPREL:
     95 			pltrel = dynp->d_un.d_ptr;
     96 			break;
     97 
     98 		case DT_PLTRELSZ:
     99 			pltrelsz = dynp->d_un.d_val;
    100 			break;
    101 
    102 		case DT_RELA:
    103 			obj->rela = (const Elf_Rela *)
    104 			    (obj->relocbase + dynp->d_un.d_ptr);
    105 			break;
    106 
    107 		case DT_RELASZ:
    108 			relasz = dynp->d_un.d_val;
    109 			break;
    110 
    111 		case DT_RELAENT:
    112 			assert(dynp->d_un.d_val == sizeof(Elf_Rela));
    113 			break;
    114 
    115 		case DT_PLTREL:
    116 			use_pltrel = dynp->d_un.d_val == DT_REL;
    117 			use_pltrela = dynp->d_un.d_val == DT_RELA;
    118 			assert(use_pltrel || use_pltrela);
    119 			break;
    120 
    121 		case DT_SYMTAB:
    122 			obj->symtab = (const Elf_Sym *)
    123 				(obj->relocbase + dynp->d_un.d_ptr);
    124 			break;
    125 
    126 		case DT_SYMENT:
    127 			assert(dynp->d_un.d_val == sizeof(Elf_Sym));
    128 			break;
    129 
    130 		case DT_STRTAB:
    131 			obj->strtab = (const char *)
    132 			    (obj->relocbase + dynp->d_un.d_ptr);
    133 			break;
    134 
    135 		case DT_STRSZ:
    136 			obj->strsize = dynp->d_un.d_val;
    137 			break;
    138 
    139 		case DT_VERNEED:
    140 			obj->verneed = (const Elf_Verneed *)
    141 			    (obj->relocbase + dynp->d_un.d_ptr);
    142 			break;
    143 
    144 		case DT_VERNEEDNUM:
    145 			obj->verneednum = dynp->d_un.d_val;
    146 			break;
    147 
    148 		case DT_VERDEF:
    149 			obj->verdef = (const Elf_Verdef *)
    150 			    (obj->relocbase + dynp->d_un.d_ptr);
    151 			break;
    152 
    153 		case DT_VERDEFNUM:
    154 			obj->verdefnum = dynp->d_un.d_val;
    155 			break;
    156 
    157 		case DT_VERSYM:
    158 			obj->versyms = (const Elf_Versym *)
    159 			    (obj->relocbase + dynp->d_un.d_ptr);
    160 			break;
    161 
    162 		case DT_HASH:
    163 			{
    164 				const Elf_Symindx *hashtab = (const Elf_Symindx *)
    165 				    (obj->relocbase + dynp->d_un.d_ptr);
    166 
    167 				if (hashtab[0] > UINT32_MAX)
    168 					obj->nbuckets = UINT32_MAX;
    169 				else
    170 					obj->nbuckets = hashtab[0];
    171 				obj->nchains = hashtab[1];
    172 				obj->buckets = hashtab + 2;
    173 				obj->chains = obj->buckets + obj->nbuckets;
    174 				/*
    175 				 * Should really be in _rtld_relocate_objects,
    176 				 * but _rtld_symlook_obj might be used before.
    177 				 */
    178 				if (obj->nbuckets) {
    179 					fast_divide32_prepare(obj->nbuckets,
    180 					    &obj->nbuckets_m,
    181 					    &obj->nbuckets_s1,
    182 					    &obj->nbuckets_s2);
    183 				}
    184 			}
    185 			break;
    186 
    187 		case DT_NEEDED:
    188 			{
    189 				Needed_Entry *nep = NEW(Needed_Entry);
    190 
    191 				nep->name = dynp->d_un.d_val;
    192 				nep->obj = NULL;
    193 				nep->next = NULL;
    194 
    195 				*needed_tail = nep;
    196 				needed_tail = &nep->next;
    197 			}
    198 			break;
    199 
    200 		case DT_PLTGOT:
    201 			obj->pltgot = (Elf_Addr *)
    202 			    (obj->relocbase + dynp->d_un.d_ptr);
    203 			break;
    204 
    205 		case DT_TEXTREL:
    206 			obj->textrel = true;
    207 			break;
    208 
    209 		case DT_SYMBOLIC:
    210 			obj->symbolic = true;
    211 			break;
    212 
    213 		case DT_RPATH:
    214 			/*
    215 		         * We have to wait until later to process this, because
    216 			 * we might not have gotten the address of the string
    217 			 * table yet.
    218 		         */
    219 			dyn_rpath = dynp;
    220 			break;
    221 
    222 		case DT_SONAME:
    223 			/* Not used by the dynamic linker. */
    224 			break;
    225 
    226 		case DT_INIT:
    227 			init = dynp->d_un.d_ptr;
    228 			break;
    229 
    230 #ifdef HAVE_INITFINI_ARRAY
    231 		case DT_INIT_ARRAY:
    232 			obj->init_array =
    233 			    (fptr_t *)(obj->relocbase + dynp->d_un.d_ptr);
    234 			break;
    235 
    236 		case DT_INIT_ARRAYSZ:
    237 			obj->init_arraysz = dynp->d_un.d_val / sizeof(fptr_t);
    238 			break;
    239 #endif
    240 
    241 		case DT_FINI:
    242 			fini = dynp->d_un.d_ptr;
    243 			break;
    244 
    245 #ifdef HAVE_INITFINI_ARRAY
    246 		case DT_FINI_ARRAY:
    247 			obj->fini_array =
    248 			    (fptr_t *)(obj->relocbase + dynp->d_un.d_ptr);
    249 			break;
    250 
    251 		case DT_FINI_ARRAYSZ:
    252 			obj->fini_arraysz = dynp->d_un.d_val / sizeof(fptr_t);
    253 			break;
    254 #endif
    255 
    256 		/*
    257 		 * Don't process DT_DEBUG on MIPS as the dynamic section
    258 		 * is mapped read-only. DT_MIPS_RLD_MAP is used instead.
    259 		 * XXX: n32/n64 may use DT_DEBUG, not sure yet.
    260 		 */
    261 #ifndef __mips__
    262 		case DT_DEBUG:
    263 #ifdef RTLD_LOADER
    264 			dynp->d_un.d_ptr = (Elf_Addr)&_rtld_debug;
    265 #endif
    266 			break;
    267 #endif
    268 
    269 #ifdef __mips__
    270 		case DT_MIPS_LOCAL_GOTNO:
    271 			obj->local_gotno = dynp->d_un.d_val;
    272 			break;
    273 
    274 		case DT_MIPS_SYMTABNO:
    275 			obj->symtabno = dynp->d_un.d_val;
    276 			break;
    277 
    278 		case DT_MIPS_GOTSYM:
    279 			obj->gotsym = dynp->d_un.d_val;
    280 			break;
    281 
    282 		case DT_MIPS_RLD_MAP:
    283 #ifdef RTLD_LOADER
    284 			*((Elf_Addr *)(dynp->d_un.d_ptr)) = (Elf_Addr)
    285 			    &_rtld_debug;
    286 #endif
    287 			break;
    288 #endif
    289 #ifdef __powerpc__
    290 		case DT_PPC_GOT:
    291 			obj->gotptr = (Elf_Addr *)(obj->relocbase + dynp->d_un.d_ptr);
    292 			break;
    293 #endif
    294 		case DT_FLAGS_1:
    295 			obj->z_now =
    296 			    ((dynp->d_un.d_val & DF_1_BIND_NOW) != 0);
    297 			obj->z_nodelete =
    298 			    ((dynp->d_un.d_val & DF_1_NODELETE) != 0);
    299 			obj->z_initfirst =
    300 			    ((dynp->d_un.d_val & DF_1_INITFIRST) != 0);
    301 			obj->z_noopen =
    302 			    ((dynp->d_un.d_val & DF_1_NOOPEN) != 0);
    303 			break;
    304 		}
    305 	}
    306 
    307 	obj->rellim = (const Elf_Rel *)((const uint8_t *)obj->rel + relsz);
    308 	obj->relalim = (const Elf_Rela *)((const uint8_t *)obj->rela + relasz);
    309 	if (use_pltrel) {
    310 		obj->pltrel = (const Elf_Rel *)(obj->relocbase + pltrel);
    311 		obj->pltrellim = (const Elf_Rel *)(obj->relocbase + pltrel + pltrelsz);
    312 		obj->pltrelalim = 0;
    313 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
    314 		   Trim rel(a)lim to save time later. */
    315 		if (obj->rellim && obj->pltrel &&
    316 		    obj->rellim > obj->pltrel &&
    317 		    obj->rellim <= obj->pltrellim)
    318 			obj->rellim = obj->pltrel;
    319 	} else if (use_pltrela) {
    320 		obj->pltrela = (const Elf_Rela *)(obj->relocbase + pltrel);
    321 		obj->pltrellim = 0;
    322 		obj->pltrelalim = (const Elf_Rela *)(obj->relocbase + pltrel + pltrelsz);
    323 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
    324 		   Trim rel(a)lim to save time later. */
    325 		if (obj->relalim && obj->pltrela &&
    326 		    obj->relalim > obj->pltrela &&
    327 		    obj->relalim <= obj->pltrelalim)
    328 			obj->relalim = obj->pltrela;
    329 	}
    330 
    331 #if defined(RTLD_LOADER) && defined(__HAVE_FUNCTION_DESCRIPTORS)
    332 	if (init != 0)
    333 		obj->init = (void (*)(void))
    334 		    _rtld_function_descriptor_alloc(obj, NULL, init);
    335 	if (fini != 0)
    336 		obj->fini = (void (*)(void))
    337 		    _rtld_function_descriptor_alloc(obj, NULL, fini);
    338 #else
    339 	if (init != 0)
    340 		obj->init = (void (*)(void))
    341 		    (obj->relocbase + init);
    342 	if (fini != 0)
    343 		obj->fini = (void (*)(void))
    344 		    (obj->relocbase + fini);
    345 #endif
    346 
    347 	if (dyn_rpath != NULL) {
    348 		_rtld_add_paths(execname, &obj->rpaths, obj->strtab +
    349 		    dyn_rpath->d_un.d_val);
    350 	}
    351 }
    352 
    353 /*
    354  * Process a shared object's program header.  This is used only for the
    355  * main program, when the kernel has already loaded the main program
    356  * into memory before calling the dynamic linker.  It creates and
    357  * returns an Obj_Entry structure.
    358  */
    359 Obj_Entry *
    360 _rtld_digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry)
    361 {
    362 	Obj_Entry      *obj;
    363 	const Elf_Phdr *phlimit = phdr + phnum;
    364 	const Elf_Phdr *ph;
    365 	int             nsegs = 0;
    366 	Elf_Addr	vaddr;
    367 
    368 	obj = _rtld_obj_new();
    369 
    370 	for (ph = phdr; ph < phlimit; ++ph) {
    371 		if (ph->p_type != PT_PHDR)
    372 			continue;
    373 
    374 		obj->phdr = (void *)(uintptr_t)ph->p_vaddr;
    375 		obj->phsize = ph->p_memsz;
    376 		obj->relocbase = (caddr_t)((uintptr_t)phdr - (uintptr_t)ph->p_vaddr);
    377 		dbg(("headers: phdr %p (%p) phsize %zu relocbase %lx",
    378 		    obj->phdr, phdr, obj->phsize, (long)obj->relocbase));
    379 		break;
    380 	}
    381 
    382 	for (ph = phdr; ph < phlimit; ++ph) {
    383 		vaddr = (Elf_Addr)(uintptr_t)(obj->relocbase + ph->p_vaddr);
    384 		switch (ph->p_type) {
    385 
    386 		case PT_INTERP:
    387 			obj->interp = (const char *)(uintptr_t)vaddr;
    388 			break;
    389 
    390 		case PT_LOAD:
    391 			assert(nsegs < 2);
    392 			if (nsegs == 0) {	/* First load segment */
    393 				obj->vaddrbase = round_down(vaddr);
    394 				obj->mapbase = (caddr_t)(uintptr_t)obj->vaddrbase;
    395 				obj->textsize = round_up(vaddr + ph->p_memsz) -
    396 				    obj->vaddrbase;
    397 			} else {		/* Last load segment */
    398 				obj->mapsize = round_up(vaddr + ph->p_memsz) -
    399 				    obj->vaddrbase;
    400 			}
    401 			++nsegs;
    402 			break;
    403 
    404 		case PT_DYNAMIC:
    405 			obj->dynamic = (Elf_Dyn *)(uintptr_t)vaddr;
    406 			dbg(("headers: PT_DYNAMIC %p phsize %zu",
    407 			    obj->dynamic, (size_t)ph->p_memsz));
    408 			break;
    409 
    410 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    411 		case PT_TLS:
    412 			obj->tlsindex = 1;
    413 			obj->tlssize = ph->p_memsz;
    414 			obj->tlsalign = ph->p_align;
    415 			obj->tlsinitsize = ph->p_filesz;
    416 			obj->tlsinit = (void *)(uintptr_t)ph->p_vaddr;
    417 			break;
    418 #endif
    419 #ifdef __ARM_EABI__
    420 		case PT_ARM_EXIDX:
    421 			obj->exidx_start = (void *)(uintptr_t)vaddr;
    422 			obj->exidx_sz = ph->p_memsz;
    423 			break;
    424 #endif
    425 		}
    426 	}
    427 	assert(nsegs == 2);
    428 
    429 	obj->entry = entry;
    430 	return obj;
    431 }
    432