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