xref: /minix3/libexec/ld.elf_so/headers.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: headers.c,v 1.59 2014/08/26 21:20:05 joerg Exp $	 */
2e83f7ba2SBen Gras 
3e83f7ba2SBen Gras /*
4e83f7ba2SBen Gras  * Copyright 1996 John D. Polstra.
5e83f7ba2SBen Gras  * Copyright 1996 Matt Thomas <matt@3am-software.com>
6e83f7ba2SBen Gras  * Copyright 2002 Charles M. Hannum <root@ihack.net>
7e83f7ba2SBen Gras  * All rights reserved.
8e83f7ba2SBen Gras  *
9e83f7ba2SBen Gras  * Redistribution and use in source and binary forms, with or without
10e83f7ba2SBen Gras  * modification, are permitted provided that the following conditions
11e83f7ba2SBen Gras  * are met:
12e83f7ba2SBen Gras  * 1. Redistributions of source code must retain the above copyright
13e83f7ba2SBen Gras  *    notice, this list of conditions and the following disclaimer.
14e83f7ba2SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
15e83f7ba2SBen Gras  *    notice, this list of conditions and the following disclaimer in the
16e83f7ba2SBen Gras  *    documentation and/or other materials provided with the distribution.
17e83f7ba2SBen Gras  * 3. All advertising materials mentioning features or use of this software
18e83f7ba2SBen Gras  *    must display the following acknowledgement:
19e83f7ba2SBen Gras  *      This product includes software developed by John Polstra.
20e83f7ba2SBen Gras  * 4. The name of the author may not be used to endorse or promote products
21e83f7ba2SBen Gras  *    derived from this software without specific prior written permission.
22e83f7ba2SBen Gras  *
23e83f7ba2SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24e83f7ba2SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25e83f7ba2SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26e83f7ba2SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27e83f7ba2SBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28e83f7ba2SBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29e83f7ba2SBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30e83f7ba2SBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31e83f7ba2SBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32e83f7ba2SBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33e83f7ba2SBen Gras  */
34e83f7ba2SBen Gras 
35e83f7ba2SBen Gras /*
36e83f7ba2SBen Gras  * Dynamic linker for ELF.
37e83f7ba2SBen Gras  *
38e83f7ba2SBen Gras  * John Polstra <jdp@polstra.com>.
39e83f7ba2SBen Gras  */
40e83f7ba2SBen Gras 
41e83f7ba2SBen Gras #include <sys/cdefs.h>
42e83f7ba2SBen Gras #ifndef lint
43*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: headers.c,v 1.59 2014/08/26 21:20:05 joerg Exp $");
44e83f7ba2SBen Gras #endif /* not lint */
45e83f7ba2SBen Gras 
46e83f7ba2SBen Gras #include <err.h>
47e83f7ba2SBen Gras #include <errno.h>
48e83f7ba2SBen Gras #include <fcntl.h>
49e83f7ba2SBen Gras #include <stdarg.h>
50e83f7ba2SBen Gras #include <stdio.h>
51e83f7ba2SBen Gras #include <stdlib.h>
52e83f7ba2SBen Gras #include <string.h>
53e83f7ba2SBen Gras #include <unistd.h>
54e83f7ba2SBen Gras #include <sys/types.h>
55e83f7ba2SBen Gras #include <sys/mman.h>
56e83f7ba2SBen Gras #include <sys/bitops.h>
57e83f7ba2SBen Gras #include <dirent.h>
58e83f7ba2SBen Gras 
59e83f7ba2SBen Gras #include "debug.h"
60e83f7ba2SBen Gras #include "rtld.h"
61e83f7ba2SBen Gras 
62e83f7ba2SBen Gras /*
63e83f7ba2SBen Gras  * Process a shared object's DYNAMIC section, and save the important
64e83f7ba2SBen Gras  * information in its Obj_Entry structure.
65e83f7ba2SBen Gras  */
66e83f7ba2SBen Gras void
_rtld_digest_dynamic(const char * execname,Obj_Entry * obj)67e83f7ba2SBen Gras _rtld_digest_dynamic(const char *execname, Obj_Entry *obj)
68e83f7ba2SBen Gras {
69e83f7ba2SBen Gras 	Elf_Dyn        *dynp;
70e83f7ba2SBen Gras 	Needed_Entry  **needed_tail = &obj->needed;
7184d9c625SLionel Sambuc 	const Elf_Dyn  *dyn_soname = NULL;
72e83f7ba2SBen Gras 	const Elf_Dyn  *dyn_rpath = NULL;
73e83f7ba2SBen Gras 	bool		use_pltrel = false;
74e83f7ba2SBen Gras 	bool		use_pltrela = false;
75e83f7ba2SBen Gras 	Elf_Addr        relsz = 0, relasz = 0;
76e83f7ba2SBen Gras 	Elf_Addr	pltrel = 0, pltrelsz = 0;
77*0a6a1f1dSLionel Sambuc #ifdef RTLD_LOADER
78e83f7ba2SBen Gras 	Elf_Addr	init = 0, fini = 0;
79*0a6a1f1dSLionel Sambuc #endif
80e83f7ba2SBen Gras 
8184d9c625SLionel Sambuc 	dbg(("headers: digesting PT_DYNAMIC at %p", obj->dynamic));
82e83f7ba2SBen Gras 	for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; ++dynp) {
8384d9c625SLionel Sambuc 		dbg(("  d_tag %ld at %p", (long)dynp->d_tag, dynp));
84e83f7ba2SBen Gras 		switch (dynp->d_tag) {
85e83f7ba2SBen Gras 
86e83f7ba2SBen Gras 		case DT_REL:
87e83f7ba2SBen Gras 			obj->rel = (const Elf_Rel *)
88e83f7ba2SBen Gras 			    (obj->relocbase + dynp->d_un.d_ptr);
89e83f7ba2SBen Gras 			break;
90e83f7ba2SBen Gras 
91e83f7ba2SBen Gras 		case DT_RELSZ:
92e83f7ba2SBen Gras 			relsz = dynp->d_un.d_val;
93e83f7ba2SBen Gras 			break;
94e83f7ba2SBen Gras 
95e83f7ba2SBen Gras 		case DT_RELENT:
96e83f7ba2SBen Gras 			assert(dynp->d_un.d_val == sizeof(Elf_Rel));
97e83f7ba2SBen Gras 			break;
98e83f7ba2SBen Gras 
99e83f7ba2SBen Gras 		case DT_JMPREL:
100e83f7ba2SBen Gras 			pltrel = dynp->d_un.d_ptr;
101e83f7ba2SBen Gras 			break;
102e83f7ba2SBen Gras 
103e83f7ba2SBen Gras 		case DT_PLTRELSZ:
104e83f7ba2SBen Gras 			pltrelsz = dynp->d_un.d_val;
105e83f7ba2SBen Gras 			break;
106e83f7ba2SBen Gras 
107e83f7ba2SBen Gras 		case DT_RELA:
108e83f7ba2SBen Gras 			obj->rela = (const Elf_Rela *)
109e83f7ba2SBen Gras 			    (obj->relocbase + dynp->d_un.d_ptr);
110e83f7ba2SBen Gras 			break;
111e83f7ba2SBen Gras 
112e83f7ba2SBen Gras 		case DT_RELASZ:
113e83f7ba2SBen Gras 			relasz = dynp->d_un.d_val;
114e83f7ba2SBen Gras 			break;
115e83f7ba2SBen Gras 
116e83f7ba2SBen Gras 		case DT_RELAENT:
117e83f7ba2SBen Gras 			assert(dynp->d_un.d_val == sizeof(Elf_Rela));
118e83f7ba2SBen Gras 			break;
119e83f7ba2SBen Gras 
120e83f7ba2SBen Gras 		case DT_PLTREL:
121e83f7ba2SBen Gras 			use_pltrel = dynp->d_un.d_val == DT_REL;
122e83f7ba2SBen Gras 			use_pltrela = dynp->d_un.d_val == DT_RELA;
123e83f7ba2SBen Gras 			assert(use_pltrel || use_pltrela);
124e83f7ba2SBen Gras 			break;
125e83f7ba2SBen Gras 
126e83f7ba2SBen Gras 		case DT_SYMTAB:
127e83f7ba2SBen Gras 			obj->symtab = (const Elf_Sym *)
128e83f7ba2SBen Gras 				(obj->relocbase + dynp->d_un.d_ptr);
129e83f7ba2SBen Gras 			break;
130e83f7ba2SBen Gras 
131e83f7ba2SBen Gras 		case DT_SYMENT:
132e83f7ba2SBen Gras 			assert(dynp->d_un.d_val == sizeof(Elf_Sym));
133e83f7ba2SBen Gras 			break;
134e83f7ba2SBen Gras 
135e83f7ba2SBen Gras 		case DT_STRTAB:
136e83f7ba2SBen Gras 			obj->strtab = (const char *)
137e83f7ba2SBen Gras 			    (obj->relocbase + dynp->d_un.d_ptr);
138e83f7ba2SBen Gras 			break;
139e83f7ba2SBen Gras 
140e83f7ba2SBen Gras 		case DT_STRSZ:
141e83f7ba2SBen Gras 			obj->strsize = dynp->d_un.d_val;
142e83f7ba2SBen Gras 			break;
143e83f7ba2SBen Gras 
144f14fb602SLionel Sambuc 		case DT_VERNEED:
145f14fb602SLionel Sambuc 			obj->verneed = (const Elf_Verneed *)
146f14fb602SLionel Sambuc 			    (obj->relocbase + dynp->d_un.d_ptr);
147f14fb602SLionel Sambuc 			break;
148f14fb602SLionel Sambuc 
149f14fb602SLionel Sambuc 		case DT_VERNEEDNUM:
150f14fb602SLionel Sambuc 			obj->verneednum = dynp->d_un.d_val;
151f14fb602SLionel Sambuc 			break;
152f14fb602SLionel Sambuc 
153f14fb602SLionel Sambuc 		case DT_VERDEF:
154f14fb602SLionel Sambuc 			obj->verdef = (const Elf_Verdef *)
155f14fb602SLionel Sambuc 			    (obj->relocbase + dynp->d_un.d_ptr);
156f14fb602SLionel Sambuc 			break;
157f14fb602SLionel Sambuc 
158f14fb602SLionel Sambuc 		case DT_VERDEFNUM:
159f14fb602SLionel Sambuc 			obj->verdefnum = dynp->d_un.d_val;
160f14fb602SLionel Sambuc 			break;
161f14fb602SLionel Sambuc 
162f14fb602SLionel Sambuc 		case DT_VERSYM:
163f14fb602SLionel Sambuc 			obj->versyms = (const Elf_Versym *)
164f14fb602SLionel Sambuc 			    (obj->relocbase + dynp->d_un.d_ptr);
165f14fb602SLionel Sambuc 			break;
166f14fb602SLionel Sambuc 
167e83f7ba2SBen Gras 		case DT_HASH:
168e83f7ba2SBen Gras 			{
169e83f7ba2SBen Gras 				const Elf_Symindx *hashtab = (const Elf_Symindx *)
170e83f7ba2SBen Gras 				    (obj->relocbase + dynp->d_un.d_ptr);
171e83f7ba2SBen Gras 
172e83f7ba2SBen Gras 				if (hashtab[0] > UINT32_MAX)
173e83f7ba2SBen Gras 					obj->nbuckets = UINT32_MAX;
174e83f7ba2SBen Gras 				else
175e83f7ba2SBen Gras 					obj->nbuckets = hashtab[0];
176e83f7ba2SBen Gras 				obj->nchains = hashtab[1];
177e83f7ba2SBen Gras 				obj->buckets = hashtab + 2;
178e83f7ba2SBen Gras 				obj->chains = obj->buckets + obj->nbuckets;
179e83f7ba2SBen Gras 				/*
180e83f7ba2SBen Gras 				 * Should really be in _rtld_relocate_objects,
181e83f7ba2SBen Gras 				 * but _rtld_symlook_obj might be used before.
182e83f7ba2SBen Gras 				 */
183e83f7ba2SBen Gras 				if (obj->nbuckets) {
184e83f7ba2SBen Gras 					fast_divide32_prepare(obj->nbuckets,
185e83f7ba2SBen Gras 					    &obj->nbuckets_m,
186e83f7ba2SBen Gras 					    &obj->nbuckets_s1,
187e83f7ba2SBen Gras 					    &obj->nbuckets_s2);
188e83f7ba2SBen Gras 				}
189e83f7ba2SBen Gras 			}
190e83f7ba2SBen Gras 			break;
191e83f7ba2SBen Gras 
192e83f7ba2SBen Gras 		case DT_NEEDED:
193e83f7ba2SBen Gras 			{
194e83f7ba2SBen Gras 				Needed_Entry *nep = NEW(Needed_Entry);
195e83f7ba2SBen Gras 
196e83f7ba2SBen Gras 				nep->name = dynp->d_un.d_val;
197e83f7ba2SBen Gras 				nep->obj = NULL;
198e83f7ba2SBen Gras 				nep->next = NULL;
199e83f7ba2SBen Gras 
200e83f7ba2SBen Gras 				*needed_tail = nep;
201e83f7ba2SBen Gras 				needed_tail = &nep->next;
202e83f7ba2SBen Gras 			}
203e83f7ba2SBen Gras 			break;
204e83f7ba2SBen Gras 
205e83f7ba2SBen Gras 		case DT_PLTGOT:
206e83f7ba2SBen Gras 			obj->pltgot = (Elf_Addr *)
207e83f7ba2SBen Gras 			    (obj->relocbase + dynp->d_un.d_ptr);
208e83f7ba2SBen Gras 			break;
209e83f7ba2SBen Gras 
210e83f7ba2SBen Gras 		case DT_TEXTREL:
211e83f7ba2SBen Gras 			obj->textrel = true;
212e83f7ba2SBen Gras 			break;
213e83f7ba2SBen Gras 
214e83f7ba2SBen Gras 		case DT_SYMBOLIC:
215e83f7ba2SBen Gras 			obj->symbolic = true;
216e83f7ba2SBen Gras 			break;
217e83f7ba2SBen Gras 
218e83f7ba2SBen Gras 		case DT_RPATH:
219e83f7ba2SBen Gras 			/*
220e83f7ba2SBen Gras 		         * We have to wait until later to process this, because
221e83f7ba2SBen Gras 			 * we might not have gotten the address of the string
222e83f7ba2SBen Gras 			 * table yet.
223e83f7ba2SBen Gras 		         */
224e83f7ba2SBen Gras 			dyn_rpath = dynp;
225e83f7ba2SBen Gras 			break;
226e83f7ba2SBen Gras 
227e83f7ba2SBen Gras 		case DT_SONAME:
22884d9c625SLionel Sambuc 			dyn_soname = dynp;
229e83f7ba2SBen Gras 			break;
230e83f7ba2SBen Gras 
231e83f7ba2SBen Gras 		case DT_INIT:
232*0a6a1f1dSLionel Sambuc #ifdef RTLD_LOADER
233e83f7ba2SBen Gras 			init = dynp->d_un.d_ptr;
234*0a6a1f1dSLionel Sambuc #endif
235e83f7ba2SBen Gras 			break;
236e83f7ba2SBen Gras 
237f14fb602SLionel Sambuc #ifdef HAVE_INITFINI_ARRAY
238f14fb602SLionel Sambuc 		case DT_INIT_ARRAY:
239f14fb602SLionel Sambuc 			obj->init_array =
240*0a6a1f1dSLionel Sambuc 			    (Elf_Addr *)(obj->relocbase + dynp->d_un.d_ptr);
24184d9c625SLionel Sambuc 			dbg(("headers: DT_INIT_ARRAY at %p",
24284d9c625SLionel Sambuc 			    obj->init_array));
243f14fb602SLionel Sambuc 			break;
244f14fb602SLionel Sambuc 
245f14fb602SLionel Sambuc 		case DT_INIT_ARRAYSZ:
246f14fb602SLionel Sambuc 			obj->init_arraysz = dynp->d_un.d_val / sizeof(fptr_t);
24784d9c625SLionel Sambuc 			dbg(("headers: DT_INIT_ARRAYZ %zu",
24884d9c625SLionel Sambuc 			    obj->init_arraysz));
249f14fb602SLionel Sambuc 			break;
250f14fb602SLionel Sambuc #endif
251f14fb602SLionel Sambuc 
252e83f7ba2SBen Gras 		case DT_FINI:
253*0a6a1f1dSLionel Sambuc #ifdef RTLD_LOADER
254e83f7ba2SBen Gras 			fini = dynp->d_un.d_ptr;
255*0a6a1f1dSLionel Sambuc #endif
256e83f7ba2SBen Gras 			break;
257e83f7ba2SBen Gras 
258f14fb602SLionel Sambuc #ifdef HAVE_INITFINI_ARRAY
259f14fb602SLionel Sambuc 		case DT_FINI_ARRAY:
260f14fb602SLionel Sambuc 			obj->fini_array =
261*0a6a1f1dSLionel Sambuc 			    (Elf_Addr *)(obj->relocbase + dynp->d_un.d_ptr);
26284d9c625SLionel Sambuc 			dbg(("headers: DT_FINI_ARRAY at %p",
26384d9c625SLionel Sambuc 			    obj->fini_array));
264f14fb602SLionel Sambuc 			break;
265f14fb602SLionel Sambuc 
266f14fb602SLionel Sambuc 		case DT_FINI_ARRAYSZ:
267f14fb602SLionel Sambuc 			obj->fini_arraysz = dynp->d_un.d_val / sizeof(fptr_t);
26884d9c625SLionel Sambuc 			dbg(("headers: DT_FINI_ARRAYZ %zu",
26984d9c625SLionel Sambuc 			    obj->fini_arraysz));
270f14fb602SLionel Sambuc 			break;
271f14fb602SLionel Sambuc #endif
272f14fb602SLionel Sambuc 
273e83f7ba2SBen Gras 		/*
274e83f7ba2SBen Gras 		 * Don't process DT_DEBUG on MIPS as the dynamic section
275e83f7ba2SBen Gras 		 * is mapped read-only. DT_MIPS_RLD_MAP is used instead.
276e83f7ba2SBen Gras 		 * XXX: n32/n64 may use DT_DEBUG, not sure yet.
277e83f7ba2SBen Gras 		 */
278e83f7ba2SBen Gras #ifndef __mips__
279e83f7ba2SBen Gras 		case DT_DEBUG:
280e83f7ba2SBen Gras #ifdef RTLD_LOADER
281e83f7ba2SBen Gras 			dynp->d_un.d_ptr = (Elf_Addr)&_rtld_debug;
282e83f7ba2SBen Gras #endif
283e83f7ba2SBen Gras 			break;
284e83f7ba2SBen Gras #endif
285e83f7ba2SBen Gras 
286e83f7ba2SBen Gras #ifdef __mips__
287e83f7ba2SBen Gras 		case DT_MIPS_LOCAL_GOTNO:
288e83f7ba2SBen Gras 			obj->local_gotno = dynp->d_un.d_val;
289e83f7ba2SBen Gras 			break;
290e83f7ba2SBen Gras 
291e83f7ba2SBen Gras 		case DT_MIPS_SYMTABNO:
292e83f7ba2SBen Gras 			obj->symtabno = dynp->d_un.d_val;
293e83f7ba2SBen Gras 			break;
294e83f7ba2SBen Gras 
295e83f7ba2SBen Gras 		case DT_MIPS_GOTSYM:
296e83f7ba2SBen Gras 			obj->gotsym = dynp->d_un.d_val;
297e83f7ba2SBen Gras 			break;
298e83f7ba2SBen Gras 
299e83f7ba2SBen Gras 		case DT_MIPS_RLD_MAP:
300e83f7ba2SBen Gras #ifdef RTLD_LOADER
301e83f7ba2SBen Gras 			*((Elf_Addr *)(dynp->d_un.d_ptr)) = (Elf_Addr)
302e83f7ba2SBen Gras 			    &_rtld_debug;
303e83f7ba2SBen Gras #endif
304e83f7ba2SBen Gras 			break;
305e83f7ba2SBen Gras #endif
306e83f7ba2SBen Gras #ifdef __powerpc__
307*0a6a1f1dSLionel Sambuc #ifdef _LP64
308*0a6a1f1dSLionel Sambuc 		case DT_PPC64_GLINK:
309*0a6a1f1dSLionel Sambuc 			obj->glink = (Elf_Addr)(uintptr_t)obj->relocbase + dynp->d_un.d_ptr;
310*0a6a1f1dSLionel Sambuc 			break;
311*0a6a1f1dSLionel Sambuc #else
312e83f7ba2SBen Gras 		case DT_PPC_GOT:
313e83f7ba2SBen Gras 			obj->gotptr = (Elf_Addr *)(obj->relocbase + dynp->d_un.d_ptr);
314e83f7ba2SBen Gras 			break;
315e83f7ba2SBen Gras #endif
316*0a6a1f1dSLionel Sambuc #endif
317e83f7ba2SBen Gras 		case DT_FLAGS_1:
318e83f7ba2SBen Gras 			obj->z_now =
319e83f7ba2SBen Gras 			    ((dynp->d_un.d_val & DF_1_BIND_NOW) != 0);
320e83f7ba2SBen Gras 			obj->z_nodelete =
321e83f7ba2SBen Gras 			    ((dynp->d_un.d_val & DF_1_NODELETE) != 0);
322e83f7ba2SBen Gras 			obj->z_initfirst =
323e83f7ba2SBen Gras 			    ((dynp->d_un.d_val & DF_1_INITFIRST) != 0);
324e83f7ba2SBen Gras 			obj->z_noopen =
325e83f7ba2SBen Gras 			    ((dynp->d_un.d_val & DF_1_NOOPEN) != 0);
326e83f7ba2SBen Gras 			break;
327e83f7ba2SBen Gras 		}
328e83f7ba2SBen Gras 	}
329e83f7ba2SBen Gras 
330e83f7ba2SBen Gras 	obj->rellim = (const Elf_Rel *)((const uint8_t *)obj->rel + relsz);
331e83f7ba2SBen Gras 	obj->relalim = (const Elf_Rela *)((const uint8_t *)obj->rela + relasz);
332e83f7ba2SBen Gras 	if (use_pltrel) {
333e83f7ba2SBen Gras 		obj->pltrel = (const Elf_Rel *)(obj->relocbase + pltrel);
334e83f7ba2SBen Gras 		obj->pltrellim = (const Elf_Rel *)(obj->relocbase + pltrel + pltrelsz);
335e83f7ba2SBen Gras 		obj->pltrelalim = 0;
336e83f7ba2SBen Gras 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
337e83f7ba2SBen Gras 		   Trim rel(a)lim to save time later. */
338e83f7ba2SBen Gras 		if (obj->rellim && obj->pltrel &&
339e83f7ba2SBen Gras 		    obj->rellim > obj->pltrel &&
340e83f7ba2SBen Gras 		    obj->rellim <= obj->pltrellim)
341e83f7ba2SBen Gras 			obj->rellim = obj->pltrel;
342e83f7ba2SBen Gras 	} else if (use_pltrela) {
343e83f7ba2SBen Gras 		obj->pltrela = (const Elf_Rela *)(obj->relocbase + pltrel);
344e83f7ba2SBen Gras 		obj->pltrellim = 0;
345e83f7ba2SBen Gras 		obj->pltrelalim = (const Elf_Rela *)(obj->relocbase + pltrel + pltrelsz);
346e83f7ba2SBen Gras 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
347e83f7ba2SBen Gras 		   Trim rel(a)lim to save time later. */
348e83f7ba2SBen Gras 		if (obj->relalim && obj->pltrela &&
349e83f7ba2SBen Gras 		    obj->relalim > obj->pltrela &&
350e83f7ba2SBen Gras 		    obj->relalim <= obj->pltrelalim)
351e83f7ba2SBen Gras 			obj->relalim = obj->pltrela;
352e83f7ba2SBen Gras 	}
353e83f7ba2SBen Gras 
354*0a6a1f1dSLionel Sambuc #ifdef RTLD_LOADER
355e83f7ba2SBen Gras 	if (init != 0)
356*0a6a1f1dSLionel Sambuc 		obj->init = (Elf_Addr) obj->relocbase + init;
357e83f7ba2SBen Gras 	if (fini != 0)
358*0a6a1f1dSLionel Sambuc 		obj->fini = (Elf_Addr) obj->relocbase + fini;
359e83f7ba2SBen Gras #endif
360e83f7ba2SBen Gras 
361e83f7ba2SBen Gras 	if (dyn_rpath != NULL) {
362e83f7ba2SBen Gras 		_rtld_add_paths(execname, &obj->rpaths, obj->strtab +
363e83f7ba2SBen Gras 		    dyn_rpath->d_un.d_val);
364e83f7ba2SBen Gras 	}
36584d9c625SLionel Sambuc 	if (dyn_soname != NULL) {
36684d9c625SLionel Sambuc 		_rtld_object_add_name(obj, obj->strtab +
36784d9c625SLionel Sambuc 		    dyn_soname->d_un.d_val);
36884d9c625SLionel Sambuc 	}
369e83f7ba2SBen Gras }
370e83f7ba2SBen Gras 
371e83f7ba2SBen Gras /*
372e83f7ba2SBen Gras  * Process a shared object's program header.  This is used only for the
373e83f7ba2SBen Gras  * main program, when the kernel has already loaded the main program
374e83f7ba2SBen Gras  * into memory before calling the dynamic linker.  It creates and
375e83f7ba2SBen Gras  * returns an Obj_Entry structure.
376e83f7ba2SBen Gras  */
377e83f7ba2SBen Gras Obj_Entry *
_rtld_digest_phdr(const Elf_Phdr * phdr,int phnum,caddr_t entry)378e83f7ba2SBen Gras _rtld_digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry)
379e83f7ba2SBen Gras {
380e83f7ba2SBen Gras 	Obj_Entry      *obj;
381e83f7ba2SBen Gras 	const Elf_Phdr *phlimit = phdr + phnum;
382e83f7ba2SBen Gras 	const Elf_Phdr *ph;
383e83f7ba2SBen Gras 	int             nsegs = 0;
384e83f7ba2SBen Gras 	Elf_Addr	vaddr;
385e83f7ba2SBen Gras 
386e83f7ba2SBen Gras 	obj = _rtld_obj_new();
387e83f7ba2SBen Gras 
388e83f7ba2SBen Gras 	for (ph = phdr; ph < phlimit; ++ph) {
389e83f7ba2SBen Gras 		if (ph->p_type != PT_PHDR)
390e83f7ba2SBen Gras 			continue;
391e83f7ba2SBen Gras 
39284d9c625SLionel Sambuc 		obj->phdr = (void *)(uintptr_t)ph->p_vaddr;
39384d9c625SLionel Sambuc 		obj->phsize = ph->p_memsz;
394e83f7ba2SBen Gras 		obj->relocbase = (caddr_t)((uintptr_t)phdr - (uintptr_t)ph->p_vaddr);
39584d9c625SLionel Sambuc 		dbg(("headers: phdr %p (%p) phsize %zu relocbase %p",
39684d9c625SLionel Sambuc 		    obj->phdr, phdr, obj->phsize, obj->relocbase));
397e83f7ba2SBen Gras 		break;
398e83f7ba2SBen Gras 	}
399e83f7ba2SBen Gras 
400e83f7ba2SBen Gras 	for (ph = phdr; ph < phlimit; ++ph) {
401e83f7ba2SBen Gras 		vaddr = (Elf_Addr)(uintptr_t)(obj->relocbase + ph->p_vaddr);
402e83f7ba2SBen Gras 		switch (ph->p_type) {
403e83f7ba2SBen Gras 
404e83f7ba2SBen Gras 		case PT_INTERP:
405e83f7ba2SBen Gras 			obj->interp = (const char *)(uintptr_t)vaddr;
40684d9c625SLionel Sambuc 			dbg(("headers: %s %p phsize %" PRImemsz,
40784d9c625SLionel Sambuc 			    "PT_INTERP", (void *)(uintptr_t)vaddr,
40884d9c625SLionel Sambuc 			     ph->p_memsz));
409e83f7ba2SBen Gras 			break;
410e83f7ba2SBen Gras 
411e83f7ba2SBen Gras 		case PT_LOAD:
412e83f7ba2SBen Gras 			assert(nsegs < 2);
413e83f7ba2SBen Gras 			if (nsegs == 0) {	/* First load segment */
414e83f7ba2SBen Gras 				obj->vaddrbase = round_down(vaddr);
415e83f7ba2SBen Gras 				obj->mapbase = (caddr_t)(uintptr_t)obj->vaddrbase;
416e83f7ba2SBen Gras 				obj->textsize = round_up(vaddr + ph->p_memsz) -
417e83f7ba2SBen Gras 				    obj->vaddrbase;
418e83f7ba2SBen Gras 			} else {		/* Last load segment */
419e83f7ba2SBen Gras 				obj->mapsize = round_up(vaddr + ph->p_memsz) -
420e83f7ba2SBen Gras 				    obj->vaddrbase;
421e83f7ba2SBen Gras 			}
422e83f7ba2SBen Gras 			++nsegs;
42384d9c625SLionel Sambuc 			dbg(("headers: %s %p phsize %" PRImemsz,
42484d9c625SLionel Sambuc 			    "PT_LOAD", (void *)(uintptr_t)vaddr,
42584d9c625SLionel Sambuc 			     ph->p_memsz));
426e83f7ba2SBen Gras 			break;
427e83f7ba2SBen Gras 
428e83f7ba2SBen Gras 		case PT_DYNAMIC:
429e83f7ba2SBen Gras 			obj->dynamic = (Elf_Dyn *)(uintptr_t)vaddr;
43084d9c625SLionel Sambuc 			dbg(("headers: %s %p phsize %" PRImemsz,
43184d9c625SLionel Sambuc 			    "PT_DYNAMIC", (void *)(uintptr_t)vaddr,
43284d9c625SLionel Sambuc 			     ph->p_memsz));
433e83f7ba2SBen Gras 			break;
434f14fb602SLionel Sambuc 
435f14fb602SLionel Sambuc #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
436f14fb602SLionel Sambuc 		case PT_TLS:
437f14fb602SLionel Sambuc 			obj->tlsindex = 1;
438f14fb602SLionel Sambuc 			obj->tlssize = ph->p_memsz;
439f14fb602SLionel Sambuc 			obj->tlsalign = ph->p_align;
440f14fb602SLionel Sambuc 			obj->tlsinitsize = ph->p_filesz;
441f14fb602SLionel Sambuc 			obj->tlsinit = (void *)(uintptr_t)ph->p_vaddr;
44284d9c625SLionel Sambuc 			dbg(("headers: %s %p phsize %" PRImemsz,
44384d9c625SLionel Sambuc 			    "PT_TLS", (void *)(uintptr_t)vaddr,
44484d9c625SLionel Sambuc 			     ph->p_memsz));
44584d9c625SLionel Sambuc 			break;
44684d9c625SLionel Sambuc #endif
44784d9c625SLionel Sambuc #ifdef __ARM_EABI__
44884d9c625SLionel Sambuc 		case PT_ARM_EXIDX:
44984d9c625SLionel Sambuc 			obj->exidx_start = (void *)(uintptr_t)vaddr;
45084d9c625SLionel Sambuc 			obj->exidx_sz = ph->p_memsz;
45184d9c625SLionel Sambuc 			dbg(("headers: %s %p phsize %" PRImemsz,
45284d9c625SLionel Sambuc 			    "PT_ARM_EXIDX", (void *)(uintptr_t)vaddr,
45384d9c625SLionel Sambuc 			     ph->p_memsz));
454f14fb602SLionel Sambuc 			break;
455f14fb602SLionel Sambuc #endif
456e83f7ba2SBen Gras 		}
457e83f7ba2SBen Gras 	}
458e83f7ba2SBen Gras 	assert(nsegs == 2);
459e83f7ba2SBen Gras 
460e83f7ba2SBen Gras 	obj->entry = entry;
461e83f7ba2SBen Gras 	return obj;
462e83f7ba2SBen Gras }
463