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