xref: /netbsd-src/libexec/ld.elf_so/headers.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: headers.c,v 1.54 2014/03/07 01:27:14 matt 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.54 2014/03/07 01:27:14 matt 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 #ifdef _LP64
302 		case DT_PPC64_GLINK:
303 			obj->glink = (Elf_Addr)(uintptr_t)obj->relocbase + dynp->d_un.d_ptr;
304 			break;
305 #else
306 		case DT_PPC_GOT:
307 			obj->gotptr = (Elf_Addr *)(obj->relocbase + dynp->d_un.d_ptr);
308 			break;
309 #endif
310 #endif
311 		case DT_FLAGS_1:
312 			obj->z_now =
313 			    ((dynp->d_un.d_val & DF_1_BIND_NOW) != 0);
314 			obj->z_nodelete =
315 			    ((dynp->d_un.d_val & DF_1_NODELETE) != 0);
316 			obj->z_initfirst =
317 			    ((dynp->d_un.d_val & DF_1_INITFIRST) != 0);
318 			obj->z_noopen =
319 			    ((dynp->d_un.d_val & DF_1_NOOPEN) != 0);
320 			break;
321 		}
322 	}
323 
324 	obj->rellim = (const Elf_Rel *)((const uint8_t *)obj->rel + relsz);
325 	obj->relalim = (const Elf_Rela *)((const uint8_t *)obj->rela + relasz);
326 	if (use_pltrel) {
327 		obj->pltrel = (const Elf_Rel *)(obj->relocbase + pltrel);
328 		obj->pltrellim = (const Elf_Rel *)(obj->relocbase + pltrel + pltrelsz);
329 		obj->pltrelalim = 0;
330 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
331 		   Trim rel(a)lim to save time later. */
332 		if (obj->rellim && obj->pltrel &&
333 		    obj->rellim > obj->pltrel &&
334 		    obj->rellim <= obj->pltrellim)
335 			obj->rellim = obj->pltrel;
336 	} else if (use_pltrela) {
337 		obj->pltrela = (const Elf_Rela *)(obj->relocbase + pltrel);
338 		obj->pltrellim = 0;
339 		obj->pltrelalim = (const Elf_Rela *)(obj->relocbase + pltrel + pltrelsz);
340 		/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
341 		   Trim rel(a)lim to save time later. */
342 		if (obj->relalim && obj->pltrela &&
343 		    obj->relalim > obj->pltrela &&
344 		    obj->relalim <= obj->pltrelalim)
345 			obj->relalim = obj->pltrela;
346 	}
347 
348 #if defined(RTLD_LOADER) && defined(__HAVE_FUNCTION_DESCRIPTORS)
349 	if (init != 0)
350 		obj->init = (void (*)(void))
351 		    _rtld_function_descriptor_alloc(obj, NULL, init);
352 	if (fini != 0)
353 		obj->fini = (void (*)(void))
354 		    _rtld_function_descriptor_alloc(obj, NULL, fini);
355 #else
356 	if (init != 0)
357 		obj->init = (void (*)(void))
358 		    (obj->relocbase + init);
359 	if (fini != 0)
360 		obj->fini = (void (*)(void))
361 		    (obj->relocbase + fini);
362 #endif
363 
364 	if (dyn_rpath != NULL) {
365 		_rtld_add_paths(execname, &obj->rpaths, obj->strtab +
366 		    dyn_rpath->d_un.d_val);
367 	}
368 	if (dyn_soname != NULL) {
369 		_rtld_object_add_name(obj, obj->strtab +
370 		    dyn_soname->d_un.d_val);
371 	}
372 }
373 
374 /*
375  * Process a shared object's program header.  This is used only for the
376  * main program, when the kernel has already loaded the main program
377  * into memory before calling the dynamic linker.  It creates and
378  * returns an Obj_Entry structure.
379  */
380 Obj_Entry *
381 _rtld_digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry)
382 {
383 	Obj_Entry      *obj;
384 	const Elf_Phdr *phlimit = phdr + phnum;
385 	const Elf_Phdr *ph;
386 	int             nsegs = 0;
387 	Elf_Addr	vaddr;
388 
389 	obj = _rtld_obj_new();
390 
391 	for (ph = phdr; ph < phlimit; ++ph) {
392 		if (ph->p_type != PT_PHDR)
393 			continue;
394 
395 		obj->phdr = (void *)(uintptr_t)ph->p_vaddr;
396 		obj->phsize = ph->p_memsz;
397 		obj->relocbase = (caddr_t)((uintptr_t)phdr - (uintptr_t)ph->p_vaddr);
398 		dbg(("headers: phdr %p (%p) phsize %zu relocbase %p",
399 		    obj->phdr, phdr, obj->phsize, obj->relocbase));
400 		break;
401 	}
402 
403 	for (ph = phdr; ph < phlimit; ++ph) {
404 		vaddr = (Elf_Addr)(uintptr_t)(obj->relocbase + ph->p_vaddr);
405 		switch (ph->p_type) {
406 
407 		case PT_INTERP:
408 			obj->interp = (const char *)(uintptr_t)vaddr;
409 			dbg(("headers: %s %p phsize %" PRImemsz,
410 			    "PT_INTERP", (void *)(uintptr_t)vaddr,
411 			     ph->p_memsz));
412 			break;
413 
414 		case PT_LOAD:
415 			assert(nsegs < 2);
416 			if (nsegs == 0) {	/* First load segment */
417 				obj->vaddrbase = round_down(vaddr);
418 				obj->mapbase = (caddr_t)(uintptr_t)obj->vaddrbase;
419 				obj->textsize = round_up(vaddr + ph->p_memsz) -
420 				    obj->vaddrbase;
421 			} else {		/* Last load segment */
422 				obj->mapsize = round_up(vaddr + ph->p_memsz) -
423 				    obj->vaddrbase;
424 			}
425 			++nsegs;
426 			dbg(("headers: %s %p phsize %" PRImemsz,
427 			    "PT_LOAD", (void *)(uintptr_t)vaddr,
428 			     ph->p_memsz));
429 			break;
430 
431 		case PT_DYNAMIC:
432 			obj->dynamic = (Elf_Dyn *)(uintptr_t)vaddr;
433 			dbg(("headers: %s %p phsize %" PRImemsz,
434 			    "PT_DYNAMIC", (void *)(uintptr_t)vaddr,
435 			     ph->p_memsz));
436 			break;
437 
438 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
439 		case PT_TLS:
440 			obj->tlsindex = 1;
441 			obj->tlssize = ph->p_memsz;
442 			obj->tlsalign = ph->p_align;
443 			obj->tlsinitsize = ph->p_filesz;
444 			obj->tlsinit = (void *)(uintptr_t)ph->p_vaddr;
445 			dbg(("headers: %s %p phsize %" PRImemsz,
446 			    "PT_TLS", (void *)(uintptr_t)vaddr,
447 			     ph->p_memsz));
448 			break;
449 #endif
450 #ifdef __ARM_EABI__
451 		case PT_ARM_EXIDX:
452 			obj->exidx_start = (void *)(uintptr_t)vaddr;
453 			obj->exidx_sz = ph->p_memsz;
454 			dbg(("headers: %s %p phsize %" PRImemsz,
455 			    "PT_ARM_EXIDX", (void *)(uintptr_t)vaddr,
456 			     ph->p_memsz));
457 			break;
458 #endif
459 		}
460 	}
461 	assert(nsegs == 2);
462 
463 	obj->entry = entry;
464 	return obj;
465 }
466