xref: /netbsd-src/libexec/ld.elf_so/headers.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: headers.c,v 1.52 2013/08/03 13:17:05 skrll Exp $	 */
2 
3 /*
4  * Copyright 1996 John D. Polstra.
5  * Copyright 1996 Matt Thomas <matt@3am-software.com>
6  * Copyright 2002 Charles M. Hannum <root@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@polstra.com>.
39  */
40 
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: headers.c,v 1.52 2013/08/03 13:17:05 skrll 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 =
236 			    (fptr_t *)(obj->relocbase + dynp->d_un.d_ptr);
237 			dbg(("headers: DT_INIT_ARRAY at %p",
238 			    obj->init_array));
239 			break;
240 
241 		case DT_INIT_ARRAYSZ:
242 			obj->init_arraysz = dynp->d_un.d_val / sizeof(fptr_t);
243 			dbg(("headers: DT_INIT_ARRAYZ %zu",
244 			    obj->init_arraysz));
245 			break;
246 #endif
247 
248 		case DT_FINI:
249 			fini = dynp->d_un.d_ptr;
250 			break;
251 
252 #ifdef HAVE_INITFINI_ARRAY
253 		case DT_FINI_ARRAY:
254 			obj->fini_array =
255 			    (fptr_t *)(obj->relocbase + dynp->d_un.d_ptr);
256 			dbg(("headers: DT_FINI_ARRAY at %p",
257 			    obj->fini_array));
258 			break;
259 
260 		case DT_FINI_ARRAYSZ:
261 			obj->fini_arraysz = dynp->d_un.d_val / sizeof(fptr_t);
262 			dbg(("headers: DT_FINI_ARRAYZ %zu",
263 			    obj->fini_arraysz));
264 			break;
265 #endif
266 
267 		/*
268 		 * Don't process DT_DEBUG on MIPS as the dynamic section
269 		 * is mapped read-only. DT_MIPS_RLD_MAP is used instead.
270 		 * XXX: n32/n64 may use DT_DEBUG, not sure yet.
271 		 */
272 #ifndef __mips__
273 		case DT_DEBUG:
274 #ifdef RTLD_LOADER
275 			dynp->d_un.d_ptr = (Elf_Addr)&_rtld_debug;
276 #endif
277 			break;
278 #endif
279 
280 #ifdef __mips__
281 		case DT_MIPS_LOCAL_GOTNO:
282 			obj->local_gotno = dynp->d_un.d_val;
283 			break;
284 
285 		case DT_MIPS_SYMTABNO:
286 			obj->symtabno = dynp->d_un.d_val;
287 			break;
288 
289 		case DT_MIPS_GOTSYM:
290 			obj->gotsym = dynp->d_un.d_val;
291 			break;
292 
293 		case DT_MIPS_RLD_MAP:
294 #ifdef RTLD_LOADER
295 			*((Elf_Addr *)(dynp->d_un.d_ptr)) = (Elf_Addr)
296 			    &_rtld_debug;
297 #endif
298 			break;
299 #endif
300 #ifdef __powerpc__
301 		case DT_PPC_GOT:
302 			obj->gotptr = (Elf_Addr *)(obj->relocbase + dynp->d_un.d_ptr);
303 			break;
304 #endif
305 		case DT_FLAGS_1:
306 			obj->z_now =
307 			    ((dynp->d_un.d_val & DF_1_BIND_NOW) != 0);
308 			obj->z_nodelete =
309 			    ((dynp->d_un.d_val & DF_1_NODELETE) != 0);
310 			obj->z_initfirst =
311 			    ((dynp->d_un.d_val & DF_1_INITFIRST) != 0);
312 			obj->z_noopen =
313 			    ((dynp->d_un.d_val & DF_1_NOOPEN) != 0);
314 			break;
315 		}
316 	}
317 
318 	obj->rellim = (const Elf_Rel *)((const uint8_t *)obj->rel + relsz);
319 	obj->relalim = (const Elf_Rela *)((const uint8_t *)obj->rela + relasz);
320 	if (use_pltrel) {
321 		obj->pltrel = (const Elf_Rel *)(obj->relocbase + pltrel);
322 		obj->pltrellim = (const Elf_Rel *)(obj->relocbase + pltrel + pltrelsz);
323 		obj->pltrelalim = 0;
324 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
325 		   Trim rel(a)lim to save time later. */
326 		if (obj->rellim && obj->pltrel &&
327 		    obj->rellim > obj->pltrel &&
328 		    obj->rellim <= obj->pltrellim)
329 			obj->rellim = obj->pltrel;
330 	} else if (use_pltrela) {
331 		obj->pltrela = (const Elf_Rela *)(obj->relocbase + pltrel);
332 		obj->pltrellim = 0;
333 		obj->pltrelalim = (const Elf_Rela *)(obj->relocbase + pltrel + pltrelsz);
334 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
335 		   Trim rel(a)lim to save time later. */
336 		if (obj->relalim && obj->pltrela &&
337 		    obj->relalim > obj->pltrela &&
338 		    obj->relalim <= obj->pltrelalim)
339 			obj->relalim = obj->pltrela;
340 	}
341 
342 #if defined(RTLD_LOADER) && defined(__HAVE_FUNCTION_DESCRIPTORS)
343 	if (init != 0)
344 		obj->init = (void (*)(void))
345 		    _rtld_function_descriptor_alloc(obj, NULL, init);
346 	if (fini != 0)
347 		obj->fini = (void (*)(void))
348 		    _rtld_function_descriptor_alloc(obj, NULL, fini);
349 #else
350 	if (init != 0)
351 		obj->init = (void (*)(void))
352 		    (obj->relocbase + init);
353 	if (fini != 0)
354 		obj->fini = (void (*)(void))
355 		    (obj->relocbase + fini);
356 #endif
357 
358 	if (dyn_rpath != NULL) {
359 		_rtld_add_paths(execname, &obj->rpaths, obj->strtab +
360 		    dyn_rpath->d_un.d_val);
361 	}
362 	if (dyn_soname != NULL) {
363 		_rtld_object_add_name(obj, obj->strtab +
364 		    dyn_soname->d_un.d_val);
365 	}
366 }
367 
368 /*
369  * Process a shared object's program header.  This is used only for the
370  * main program, when the kernel has already loaded the main program
371  * into memory before calling the dynamic linker.  It creates and
372  * returns an Obj_Entry structure.
373  */
374 Obj_Entry *
375 _rtld_digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry)
376 {
377 	Obj_Entry      *obj;
378 	const Elf_Phdr *phlimit = phdr + phnum;
379 	const Elf_Phdr *ph;
380 	int             nsegs = 0;
381 	Elf_Addr	vaddr;
382 
383 	obj = _rtld_obj_new();
384 
385 	for (ph = phdr; ph < phlimit; ++ph) {
386 		if (ph->p_type != PT_PHDR)
387 			continue;
388 
389 		obj->phdr = (void *)(uintptr_t)ph->p_vaddr;
390 		obj->phsize = ph->p_memsz;
391 		obj->relocbase = (caddr_t)((uintptr_t)phdr - (uintptr_t)ph->p_vaddr);
392 		dbg(("headers: phdr %p (%p) phsize %zu relocbase %p",
393 		    obj->phdr, phdr, obj->phsize, obj->relocbase));
394 		break;
395 	}
396 
397 	for (ph = phdr; ph < phlimit; ++ph) {
398 		vaddr = (Elf_Addr)(uintptr_t)(obj->relocbase + ph->p_vaddr);
399 		switch (ph->p_type) {
400 
401 		case PT_INTERP:
402 			obj->interp = (const char *)(uintptr_t)vaddr;
403 			dbg(("headers: %s %p phsize %" PRImemsz,
404 			    "PT_INTERP", (void *)(uintptr_t)vaddr,
405 			     ph->p_memsz));
406 			break;
407 
408 		case PT_LOAD:
409 			assert(nsegs < 2);
410 			if (nsegs == 0) {	/* First load segment */
411 				obj->vaddrbase = round_down(vaddr);
412 				obj->mapbase = (caddr_t)(uintptr_t)obj->vaddrbase;
413 				obj->textsize = round_up(vaddr + ph->p_memsz) -
414 				    obj->vaddrbase;
415 			} else {		/* Last load segment */
416 				obj->mapsize = round_up(vaddr + ph->p_memsz) -
417 				    obj->vaddrbase;
418 			}
419 			++nsegs;
420 			dbg(("headers: %s %p phsize %" PRImemsz,
421 			    "PT_LOAD", (void *)(uintptr_t)vaddr,
422 			     ph->p_memsz));
423 			break;
424 
425 		case PT_DYNAMIC:
426 			obj->dynamic = (Elf_Dyn *)(uintptr_t)vaddr;
427 			dbg(("headers: %s %p phsize %" PRImemsz,
428 			    "PT_DYNAMIC", (void *)(uintptr_t)vaddr,
429 			     ph->p_memsz));
430 			break;
431 
432 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
433 		case PT_TLS:
434 			obj->tlsindex = 1;
435 			obj->tlssize = ph->p_memsz;
436 			obj->tlsalign = ph->p_align;
437 			obj->tlsinitsize = ph->p_filesz;
438 			obj->tlsinit = (void *)(uintptr_t)ph->p_vaddr;
439 			dbg(("headers: %s %p phsize %" PRImemsz,
440 			    "PT_TLS", (void *)(uintptr_t)vaddr,
441 			     ph->p_memsz));
442 			break;
443 #endif
444 #ifdef __ARM_EABI__
445 		case PT_ARM_EXIDX:
446 			obj->exidx_start = (void *)(uintptr_t)vaddr;
447 			obj->exidx_sz = ph->p_memsz;
448 			dbg(("headers: %s %p phsize %" PRImemsz,
449 			    "PT_ARM_EXIDX", (void *)(uintptr_t)vaddr,
450 			     ph->p_memsz));
451 			break;
452 #endif
453 		}
454 	}
455 	assert(nsegs == 2);
456 
457 	obj->entry = entry;
458 	return obj;
459 }
460