Home | History | Annotate | Line # | Download | only in ld.elf_so
headers.c revision 1.14
      1 /*	$NetBSD: headers.c,v 1.14 2002/09/25 06:43:46 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 /*
     35  * Dynamic linker for ELF.
     36  *
     37  * John Polstra <jdp (at) polstra.com>.
     38  */
     39 
     40 #include <err.h>
     41 #include <errno.h>
     42 #include <fcntl.h>
     43 #include <stdarg.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 #include <sys/types.h>
     49 #include <sys/mman.h>
     50 #include <dirent.h>
     51 
     52 #include "debug.h"
     53 #include "rtld.h"
     54 
     55 /*
     56  * Process a shared object's DYNAMIC section, and save the important
     57  * information in its Obj_Entry structure.
     58  */
     59 void
     60 _rtld_digest_dynamic(obj)
     61 	Obj_Entry *obj;
     62 {
     63 	Elf_Dyn        *dynp;
     64 	Needed_Entry  **needed_tail = &obj->needed;
     65 	const Elf_Dyn  *dyn_rpath = NULL;
     66 	Elf_Sword	plttype = DT_REL;
     67 	Elf_Addr        relsz = 0, relasz = 0;
     68 	Elf_Addr	pltrel = 0, pltrelsz = 0;
     69 	Elf_Addr	init = 0, fini = 0;
     70 
     71 	for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; ++dynp) {
     72 		switch (dynp->d_tag) {
     73 
     74 		case DT_REL:
     75 			obj->rel = (const Elf_Rel *)
     76 			    (obj->relocbase + dynp->d_un.d_ptr);
     77 			break;
     78 
     79 		case DT_RELSZ:
     80 			relsz = dynp->d_un.d_val;
     81 			break;
     82 
     83 		case DT_RELENT:
     84 			assert(dynp->d_un.d_val == sizeof(Elf_Rel));
     85 			break;
     86 
     87 		case DT_JMPREL:
     88 			pltrel = dynp->d_un.d_ptr;
     89 			break;
     90 
     91 		case DT_PLTRELSZ:
     92 			pltrelsz = dynp->d_un.d_val;
     93 			break;
     94 
     95 		case DT_RELA:
     96 			obj->rela = (const Elf_Rela *)
     97 			    (obj->relocbase + dynp->d_un.d_ptr);
     98 			break;
     99 
    100 		case DT_RELASZ:
    101 			relasz = dynp->d_un.d_val;
    102 			break;
    103 
    104 		case DT_RELAENT:
    105 			assert(dynp->d_un.d_val == sizeof(Elf_Rela));
    106 			break;
    107 
    108 		case DT_PLTREL:
    109 			plttype = dynp->d_un.d_val;
    110 			assert(plttype == DT_REL || plttype == DT_RELA);
    111 			break;
    112 
    113 		case DT_SYMTAB:
    114 			obj->symtab = (const Elf_Sym *)
    115 				(obj->relocbase + dynp->d_un.d_ptr);
    116 			break;
    117 
    118 		case DT_SYMENT:
    119 			assert(dynp->d_un.d_val == sizeof(Elf_Sym));
    120 			break;
    121 
    122 		case DT_STRTAB:
    123 			obj->strtab = (const char *)
    124 			    (obj->relocbase + dynp->d_un.d_ptr);
    125 			break;
    126 
    127 		case DT_STRSZ:
    128 			obj->strsize = dynp->d_un.d_val;
    129 			break;
    130 
    131 		case DT_HASH:
    132 			{
    133 				const Elf_Word *hashtab = (const Elf_Word *)
    134 				(obj->relocbase + dynp->d_un.d_ptr);
    135 
    136 				obj->nbuckets = hashtab[0];
    137 				obj->nchains = hashtab[1];
    138 				obj->buckets = hashtab + 2;
    139 				obj->chains = obj->buckets + obj->nbuckets;
    140 			}
    141 			break;
    142 
    143 		case DT_NEEDED:
    144 			assert(!obj->rtld);
    145 			{
    146 				Needed_Entry *nep = NEW(Needed_Entry);
    147 
    148 				nep->name = dynp->d_un.d_val;
    149 				nep->obj = NULL;
    150 				nep->next = NULL;
    151 
    152 				*needed_tail = nep;
    153 				needed_tail = &nep->next;
    154 			}
    155 			break;
    156 
    157 		case DT_PLTGOT:
    158 			obj->pltgot = (Elf_Addr *)
    159 			    (obj->relocbase + dynp->d_un.d_ptr);
    160 			break;
    161 
    162 		case DT_TEXTREL:
    163 			obj->textrel = true;
    164 			break;
    165 
    166 		case DT_SYMBOLIC:
    167 			obj->symbolic = true;
    168 			break;
    169 
    170 		case DT_RPATH:
    171 			/*
    172 		         * We have to wait until later to process this, because
    173 			 * we might not have gotten the address of the string
    174 			 * table yet.
    175 		         */
    176 			dyn_rpath = dynp;
    177 			break;
    178 
    179 		case DT_SONAME:
    180 			/* Not used by the dynamic linker. */
    181 			break;
    182 
    183 		case DT_INIT:
    184 			init = dynp->d_un.d_ptr;
    185 			break;
    186 
    187 		case DT_FINI:
    188 			fini = dynp->d_un.d_ptr;
    189 			break;
    190 
    191 		case DT_DEBUG:
    192 #ifdef RTLD_LOADER
    193 			dynp->d_un.d_ptr = (Elf_Addr)&_rtld_debug;
    194 #endif
    195 			break;
    196 
    197 #ifdef __mips__
    198 		case DT_MIPS_LOCAL_GOTNO:
    199 			obj->local_gotno = dynp->d_un.d_val;
    200 			break;
    201 
    202 		case DT_MIPS_SYMTABNO:
    203 			obj->symtabno = dynp->d_un.d_val;
    204 			break;
    205 
    206 		case DT_MIPS_GOTSYM:
    207 			obj->gotsym = dynp->d_un.d_val;
    208 			break;
    209 
    210 		case DT_MIPS_RLD_MAP:
    211 #ifdef RTLD_LOADER
    212 			*((Elf_Addr *)(dynp->d_un.d_ptr)) = (Elf_Addr)
    213 			    &_rtld_debug;
    214 #endif
    215 			break;
    216 #endif
    217 		}
    218 	}
    219 
    220 	obj->rellim = (const Elf_Rel *)((caddr_t)obj->rel + relsz);
    221 	obj->relalim = (const Elf_Rela *)((caddr_t)obj->rela + relasz);
    222 	if (plttype == DT_REL) {
    223 		obj->pltrel = (const Elf_Rel *)(obj->relocbase + pltrel);
    224 		obj->pltrellim = (const Elf_Rel *)(obj->relocbase + pltrel + pltrelsz);
    225 		obj->pltrelalim = 0;
    226 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
    227 		   Trim rel(a)lim to save time later. */
    228 		if (obj->rellim && obj->pltrel &&
    229 		    obj->rellim > obj->pltrel &&
    230 		    obj->rellim <= obj->pltrellim)
    231 			obj->rellim = obj->pltrel;
    232 	} else {
    233 		obj->pltrela = (const Elf_Rela *)(obj->relocbase + pltrel);
    234 		obj->pltrellim = 0;
    235 		obj->pltrelalim = (const Elf_Rela *)(obj->relocbase + pltrel + pltrelsz);
    236 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
    237 		   Trim rel(a)lim to save time later. */
    238 		if (obj->relalim && obj->pltrela &&
    239 		    obj->relalim > obj->pltrela &&
    240 		    obj->relalim <= obj->pltrelalim)
    241 			obj->relalim = obj->pltrela;
    242 	}
    243 
    244 #if defined(RTLD_LOADER) && defined(__HAVE_FUNCTION_DESCRIPTORS)
    245 	if (init != 0)
    246 		obj->init = (void (*) __P((void)))
    247 		    _rtld_function_descriptor_alloc(obj, NULL, init);
    248 	if (fini != 0)
    249 		obj->fini = (void (*) __P((void)))
    250 		    _rtld_function_descriptor_alloc(obj, NULL, fini);
    251 #else
    252 	if (init != 0)
    253 		obj->init = (void (*) __P((void)))
    254 		    (obj->relocbase + init);
    255 	if (fini != 0)
    256 		obj->fini = (void (*) __P((void)))
    257 		    (obj->relocbase + fini);
    258 #endif
    259 
    260 	if (dyn_rpath != NULL) {
    261 		_rtld_add_paths(&obj->rpaths, obj->strtab +
    262 		    dyn_rpath->d_un.d_val);
    263 	}
    264 }
    265 
    266 /*
    267  * Process a shared object's program header.  This is used only for the
    268  * main program, when the kernel has already loaded the main program
    269  * into memory before calling the dynamic linker.  It creates and
    270  * returns an Obj_Entry structure.
    271  */
    272 Obj_Entry *
    273 _rtld_digest_phdr(phdr, phnum, entry)
    274 	const Elf_Phdr *phdr;
    275 	int phnum;
    276 	caddr_t entry;
    277 {
    278 	Obj_Entry      *obj;
    279 	const Elf_Phdr *phlimit = phdr + phnum;
    280 	const Elf_Phdr *ph;
    281 	int             nsegs = 0;
    282 
    283 	obj = _rtld_obj_new();
    284 	for (ph = phdr; ph < phlimit; ++ph) {
    285 		switch (ph->p_type) {
    286 
    287 		case PT_PHDR:
    288 			assert((const Elf_Phdr *) ph->p_vaddr == phdr);
    289 			obj->phdr = (const Elf_Phdr *) ph->p_vaddr;
    290 			obj->phsize = ph->p_memsz;
    291 			break;
    292 
    293 		case PT_INTERP:
    294 			obj->interp = (const char *) ph->p_vaddr;
    295 			break;
    296 
    297 		case PT_LOAD:
    298 			assert(nsegs < 2);
    299 			if (nsegs == 0) {	/* First load segment */
    300 				obj->vaddrbase = round_down(ph->p_vaddr);
    301 				obj->mapbase = (caddr_t) obj->vaddrbase;
    302 				obj->relocbase = obj->mapbase - obj->vaddrbase;
    303 				obj->textsize = round_up(ph->p_vaddr +
    304 				    ph->p_memsz) - obj->vaddrbase;
    305 			} else {		/* Last load segment */
    306 				obj->mapsize = round_up(ph->p_vaddr +
    307 				    ph->p_memsz) - obj->vaddrbase;
    308 			}
    309 			++nsegs;
    310 			break;
    311 
    312 		case PT_DYNAMIC:
    313 			obj->dynamic = (Elf_Dyn *) ph->p_vaddr;
    314 			break;
    315 		}
    316 	}
    317 	assert(nsegs == 2);
    318 
    319 	obj->entry = entry;
    320 	return obj;
    321 }
    322