xref: /netbsd-src/libexec/ld.elf_so/rtld.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: rtld.c,v 1.123 2008/10/26 07:11:54 mrg 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: rtld.c,v 1.123 2008/10/26 07:11:54 mrg 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/param.h>
55 #include <sys/mman.h>
56 #include <dirent.h>
57 
58 #include <ctype.h>
59 
60 #include <dlfcn.h>
61 #include "debug.h"
62 #include "rtld.h"
63 
64 #if !defined(lint)
65 #include "sysident.h"
66 #endif
67 
68 /*
69  * Function declarations.
70  */
71 static void     _rtld_init(caddr_t, caddr_t, const char *);
72 static void     _rtld_exit(void);
73 
74 Elf_Addr        _rtld(Elf_Addr *, Elf_Addr);
75 
76 
77 /*
78  * Data declarations.
79  */
80 static char    *error_message;	/* Message for dlopen(), or NULL */
81 
82 struct r_debug  _rtld_debug;	/* for GDB; */
83 bool            _rtld_trust;	/* False for setuid and setgid programs */
84 Obj_Entry      *_rtld_objlist;	/* Head of linked list of shared objects */
85 Obj_Entry     **_rtld_objtail;	/* Link field of last object in list */
86 Obj_Entry      *_rtld_objmain;	/* The main program shared object */
87 Obj_Entry       _rtld_objself;	/* The dynamic linker shared object */
88 const char	_rtld_path[] = _PATH_RTLD;
89 
90 /* Initialize a fake symbol for resolving undefined weak references. */
91 Elf_Sym		_rtld_sym_zero = {
92     .st_info	= ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
93     .st_shndx	= SHN_ABS,
94 };
95 int		_rtld_pagesz;	/* Page size, as provided by kernel */
96 
97 Search_Path    *_rtld_default_paths;
98 Search_Path    *_rtld_paths;
99 
100 Library_Xform  *_rtld_xforms;
101 
102 /*
103  * Global declarations normally provided by crt0.
104  */
105 char           *__progname;
106 char          **environ;
107 
108 #if defined(RTLD_DEBUG)
109 #ifndef __sh__
110 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
111 #else  /* 32-bit SuperH */
112 register Elf_Addr *_GLOBAL_OFFSET_TABLE_ asm("r12");
113 #endif
114 #endif /* RTLD_DEBUG */
115 extern Elf_Dyn  _DYNAMIC;
116 
117 static void _rtld_call_fini_functions(int);
118 static void _rtld_call_init_functions(void);
119 static void _rtld_initlist_visit(Objlist *, Obj_Entry *, int);
120 static void _rtld_initlist_tsort(Objlist *, int);
121 static Obj_Entry *_rtld_dlcheck(void *);
122 static void _rtld_init_dag(Obj_Entry *);
123 static void _rtld_init_dag1(Obj_Entry *, Obj_Entry *);
124 static void _rtld_objlist_remove(Objlist *, Obj_Entry *);
125 static void _rtld_objlist_clear(Objlist *);
126 static void _rtld_unload_object(Obj_Entry *, bool);
127 static void _rtld_unref_dag(Obj_Entry *);
128 static Obj_Entry *_rtld_obj_from_addr(const void *);
129 
130 static void
131 _rtld_call_fini_functions(int force)
132 {
133 	Objlist_Entry *elm;
134 	Objlist finilist;
135 	Obj_Entry *obj;
136 
137 	dbg(("_rtld_call_fini_functions(%d)", force));
138 
139 	SIMPLEQ_INIT(&finilist);
140 	_rtld_initlist_tsort(&finilist, 1);
141 
142 	/* First pass: objects _not_ marked with DF_1_INITFIRST. */
143 	SIMPLEQ_FOREACH(elm, &finilist, link) {
144 		obj = elm->obj;
145 		if (obj->refcount > 0 && !force) {
146 			continue;
147 		}
148 		if (obj->fini == NULL || obj->fini_called || obj->initfirst) {
149 		    	continue;
150 		}
151 		dbg (("calling fini function %s at %p",  obj->path,
152 		    (void *)obj->fini));
153 		obj->fini_called = 1;
154 		(*obj->fini)();
155 	}
156 
157 	/* Second pass: objects marked with DF_1_INITFIRST. */
158 	SIMPLEQ_FOREACH(elm, &finilist, link) {
159 		obj = elm->obj;
160 		if (obj->refcount > 0 && !force) {
161 			continue;
162 		}
163 		if (obj->fini == NULL || obj->fini_called) {
164 		    	continue;
165 		}
166 		dbg (("calling fini function %s at %p (DF_1_INITFIRST)",
167 		    obj->path, (void *)obj->fini));
168 		obj->fini_called = 1;
169 		(*obj->fini)();
170 	}
171 
172         _rtld_objlist_clear(&finilist);
173 }
174 
175 static void
176 _rtld_call_init_functions()
177 {
178 	Objlist_Entry *elm;
179 	Objlist initlist;
180 	Obj_Entry *obj;
181 
182 	dbg(("_rtld_call_init_functions()"));
183 	SIMPLEQ_INIT(&initlist);
184 	_rtld_initlist_tsort(&initlist, 0);
185 
186 	/* First pass: objects marked with DF_1_INITFIRST. */
187 	SIMPLEQ_FOREACH(elm, &initlist, link) {
188 		obj = elm->obj;
189 		if (obj->init == NULL || obj->init_called || !obj->initfirst) {
190 			continue;
191 		}
192 		dbg (("calling init function %s at %p (DF_1_INITFIRST)",
193 		    obj->path, (void *)obj->init));
194 		obj->init_called = 1;
195 		(*obj->init)();
196 	}
197 
198 	/* Second pass: all other objects. */
199 	SIMPLEQ_FOREACH(elm, &initlist, link) {
200 		obj = elm->obj;
201 		if (obj->init == NULL || obj->init_called) {
202 			continue;
203 		}
204 		dbg (("calling init function %s at %p",  obj->path,
205 		    (void *)obj->init));
206 		obj->init_called = 1;
207 		(*obj->init)();
208 	}
209 
210         _rtld_objlist_clear(&initlist);
211 }
212 
213 /*
214  * Initialize the dynamic linker.  The argument is the address at which
215  * the dynamic linker has been mapped into memory.  The primary task of
216  * this function is to create an Obj_Entry for the dynamic linker and
217  * to resolve the PLT relocation for platforms that need it (those that
218  * define __HAVE_FUNCTION_DESCRIPTORS
219  */
220 static void
221 _rtld_init(caddr_t mapbase, caddr_t relocbase, const char *execname)
222 {
223 
224 	/* Conjure up an Obj_Entry structure for the dynamic linker. */
225 	_rtld_objself.path = __UNCONST(_rtld_path);
226 	_rtld_objself.pathlen = sizeof(_rtld_path)-1;
227 	_rtld_objself.rtld = true;
228 	_rtld_objself.mapbase = mapbase;
229 	_rtld_objself.relocbase = relocbase;
230 	_rtld_objself.dynamic = (Elf_Dyn *) &_DYNAMIC;
231 	_rtld_objself.strtab = "_rtld_sym_zero";
232 
233 	/*
234 	 * Set value to -relocabase so that
235 	 * _rtld_objself.relocbase + _rtld_smy_zero.st_value == 0
236 	 * This allows unresolved references to weak symbols to be computed
237 	 * to value a value of 0.
238 	 */
239 	_rtld_sym_zero.st_value = -(uintptr_t)relocbase;
240 
241 	_rtld_digest_dynamic(_rtld_path, &_rtld_objself);
242 	assert(!_rtld_objself.needed);
243 #if !defined(__hppa__)
244 	assert(!_rtld_objself.pltrel && !_rtld_objself.pltrela);
245 #else
246 	_rtld_relocate_plt_objects(&_rtld_objself);
247 #endif
248 #if !defined(__mips__) && !defined(__hppa__)
249 	assert(!_rtld_objself.pltgot);
250 #endif
251 #if !defined(__arm__) && !defined(__mips__) && !defined(__sh__)
252 	/* ARM, MIPS and SH{3,5} have a bogus DT_TEXTREL. */
253 	assert(!_rtld_objself.textrel);
254 #endif
255 
256 	_rtld_add_paths(execname, &_rtld_default_paths,
257 	    RTLD_DEFAULT_LIBRARY_PATH);
258 
259 #ifdef RTLD_ARCH_SUBDIR
260 	_rtld_add_paths(execname, &_rtld_default_paths,
261 	    RTLD_DEFAULT_LIBRARY_PATH "/" RTLD_ARCH_SUBDIR);
262 #endif
263 
264 	/*
265 	 * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
266 	 */
267 	_rtld_objlist = &_rtld_objself;
268 
269 	/* Make the object list empty again. */
270 	_rtld_objlist = NULL;
271 	_rtld_objtail = &_rtld_objlist;
272 
273 	_rtld_debug.r_brk = _rtld_debug_state;
274 	_rtld_debug.r_state = RT_CONSISTENT;
275 }
276 
277 /*
278  * Cleanup procedure.  It will be called (by the atexit() mechanism) just
279  * before the process exits.
280  */
281 static void
282 _rtld_exit(void)
283 {
284 	dbg(("rtld_exit()"));
285 
286 	_rtld_call_fini_functions(1);
287 }
288 
289 /*
290  * Main entry point for dynamic linking.  The argument is the stack
291  * pointer.  The stack is expected to be laid out as described in the
292  * SVR4 ABI specification, Intel 386 Processor Supplement.  Specifically,
293  * the stack pointer points to a word containing ARGC.  Following that
294  * in the stack is a null-terminated sequence of pointers to argument
295  * strings.  Then comes a null-terminated sequence of pointers to
296  * environment strings.  Finally, there is a sequence of "auxiliary
297  * vector" entries.
298  *
299  * This function returns the entry point for the main program, the dynamic
300  * linker's exit procedure in sp[0], and a pointer to the main object in
301  * sp[1].
302  */
303 Elf_Addr
304 _rtld(Elf_Addr *sp, Elf_Addr relocbase)
305 {
306 	const AuxInfo  *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
307 	               *pAUX_phent, *pAUX_phnum, *pAUX_euid, *pAUX_egid,
308 		       *pAUX_ruid, *pAUX_rgid;
309 	const AuxInfo  *pAUX_pagesz;
310 	char          **env;
311 	const AuxInfo  *aux;
312 	const AuxInfo  *auxp;
313 	Elf_Addr       *const osp = sp;
314 	bool            bind_now = 0;
315 	const char     *ld_bind_now;
316 	const char    **argv;
317 	const char     *execname;
318 	long		argc;
319 	const char **real___progname;
320 	const Obj_Entry **real___mainprog_obj;
321 	char ***real_environ;
322 #if defined(RTLD_DEBUG)
323 	int i = 0;
324 #endif
325 
326 	/*
327          * On entry, the dynamic linker itself has not been relocated yet.
328          * Be very careful not to reference any global data until after
329          * _rtld_init has returned.  It is OK to reference file-scope statics
330          * and string constants, and to call static and global functions.
331          */
332 	/* Find the auxiliary vector on the stack. */
333 	/* first Elf_Word reserved to address of exit routine */
334 #if defined(RTLD_DEBUG)
335 	debug = 1;
336 	dbg(("sp = %p, argc = %ld, argv = %p <%s> relocbase %p", sp,
337 	    (long)sp[2], &sp[3], (char *) sp[3], (void *)relocbase));
338 	dbg(("got is at %p, dynamic is at %p", _GLOBAL_OFFSET_TABLE_,
339 	    &_DYNAMIC));
340 	dbg(("_ctype_ is %p", _ctype_));
341 #endif
342 
343 	sp += 2;		/* skip over return argument space */
344 	argv = (const char **) &sp[1];
345 	argc = *(long *)sp;
346 	sp += 2 + argc;		/* Skip over argc, arguments, and NULL
347 				 * terminator */
348 	env = (char **) sp;
349 	while (*sp++ != 0) {	/* Skip over environment, and NULL terminator */
350 #if defined(RTLD_DEBUG)
351 		dbg(("env[%d] = %p %s", i++, (void *)sp[-1], (char *)sp[-1]));
352 #endif
353 	}
354 	aux = (const AuxInfo *) sp;
355 
356 	pAUX_base = pAUX_entry = pAUX_execfd = NULL;
357 	pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
358 	pAUX_euid = pAUX_ruid = pAUX_egid = pAUX_rgid = NULL;
359 	pAUX_pagesz = NULL;
360 
361 	execname = NULL;
362 
363 	/* Digest the auxiliary vector. */
364 	for (auxp = aux; auxp->a_type != AT_NULL; ++auxp) {
365 		switch (auxp->a_type) {
366 		case AT_BASE:
367 			pAUX_base = auxp;
368 			break;
369 		case AT_ENTRY:
370 			pAUX_entry = auxp;
371 			break;
372 		case AT_EXECFD:
373 			pAUX_execfd = auxp;
374 			break;
375 		case AT_PHDR:
376 			pAUX_phdr = auxp;
377 			break;
378 		case AT_PHENT:
379 			pAUX_phent = auxp;
380 			break;
381 		case AT_PHNUM:
382 			pAUX_phnum = auxp;
383 			break;
384 #ifdef AT_EUID
385 		case AT_EUID:
386 			pAUX_euid = auxp;
387 			break;
388 		case AT_RUID:
389 			pAUX_ruid = auxp;
390 			break;
391 		case AT_EGID:
392 			pAUX_egid = auxp;
393 			break;
394 		case AT_RGID:
395 			pAUX_rgid = auxp;
396 			break;
397 #endif
398 #ifdef AT_SUN_EXECNAME
399 		case AT_SUN_EXECNAME:
400 			execname = (const char *)(const void *)auxp->a_v;
401 			break;
402 #endif
403 		case AT_PAGESZ:
404 			pAUX_pagesz = auxp;
405 			break;
406 		}
407 	}
408 
409 	/* Initialize and relocate ourselves. */
410 	if (pAUX_base == NULL) {
411 		_rtld_error("Bad pAUX_base");
412 		_rtld_die();
413 	}
414 	assert(pAUX_pagesz != NULL);
415 	_rtld_pagesz = (int)pAUX_pagesz->a_v;
416 	_rtld_init((caddr_t)pAUX_base->a_v, (caddr_t)relocbase, execname);
417 
418 	__progname = _rtld_objself.path;
419 	environ = env;
420 
421 	_rtld_trust = ((pAUX_euid ? (uid_t)pAUX_euid->a_v : geteuid()) ==
422 	    (pAUX_ruid ? (uid_t)pAUX_ruid->a_v : getuid())) &&
423 	    ((pAUX_egid ? (gid_t)pAUX_egid->a_v : getegid()) ==
424 	    (pAUX_rgid ? (gid_t)pAUX_rgid->a_v : getgid()));
425 
426 	ld_bind_now = getenv("LD_BIND_NOW");
427 	if (ld_bind_now != NULL && *ld_bind_now != '\0')
428 		bind_now = true;
429 	if (_rtld_trust) {
430 #ifdef DEBUG
431 		const char     *ld_debug = getenv("LD_DEBUG");
432 #ifdef RTLD_DEBUG
433 		debug = 0;
434 #endif
435 		if (ld_debug != NULL && *ld_debug != '\0')
436 			debug = 1;
437 #endif
438 		_rtld_add_paths(execname, &_rtld_paths,
439 		    getenv("LD_LIBRARY_PATH"));
440 	} else {
441 		execname = NULL;
442 		unsetenv("LD_DEBUG");
443 		unsetenv("LD_LIBRARY_PATH");
444 	}
445 	_rtld_process_hints(execname, &_rtld_paths, &_rtld_xforms,
446 	    _PATH_LD_HINTS);
447 	dbg(("dynamic linker is initialized, mapbase=%p, relocbase=%p",
448 	     _rtld_objself.mapbase, _rtld_objself.relocbase));
449 
450 	/*
451          * Load the main program, or process its program header if it is
452          * already loaded.
453          */
454 	if (pAUX_execfd != NULL) {	/* Load the main program. */
455 		int             fd = pAUX_execfd->a_v;
456 		const char *obj_name = argv[0] ? argv[0] : "main program";
457 		dbg(("loading main program"));
458 		_rtld_objmain = _rtld_map_object(obj_name, fd, NULL);
459 		close(fd);
460 		if (_rtld_objmain == NULL)
461 			_rtld_die();
462 	} else {		/* Main program already loaded. */
463 		const Elf_Phdr *phdr;
464 		int             phnum;
465 		caddr_t         entry;
466 
467 		dbg(("processing main program's program header"));
468 		assert(pAUX_phdr != NULL);
469 		phdr = (const Elf_Phdr *) pAUX_phdr->a_v;
470 		assert(pAUX_phnum != NULL);
471 		phnum = pAUX_phnum->a_v;
472 		assert(pAUX_phent != NULL);
473 		assert(pAUX_phent->a_v == sizeof(Elf_Phdr));
474 		assert(pAUX_entry != NULL);
475 		entry = (caddr_t) pAUX_entry->a_v;
476 		_rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
477 		_rtld_objmain->path = xstrdup(argv[0] ? argv[0] :
478 		    "main program");
479 		_rtld_objmain->pathlen = strlen(_rtld_objmain->path);
480 	}
481 
482 	_rtld_objmain->mainprog = true;
483 
484 	/*
485 	 * Get the actual dynamic linker pathname from the executable if
486 	 * possible.  (It should always be possible.)  That ensures that
487 	 * gdb will find the right dynamic linker even if a non-standard
488 	 * one is being used.
489 	 */
490 	if (_rtld_objmain->interp != NULL &&
491 	    strcmp(_rtld_objmain->interp, _rtld_objself.path) != 0)
492 		_rtld_objself.path = xstrdup(_rtld_objmain->interp);
493 	dbg(("actual dynamic linker is %s", _rtld_objself.path));
494 
495 	_rtld_digest_dynamic(execname, _rtld_objmain);
496 
497 	/* Link the main program into the list of objects. */
498 	*_rtld_objtail = _rtld_objmain;
499 	_rtld_objtail = &_rtld_objmain->next;
500 
501 	_rtld_linkmap_add(_rtld_objmain);
502 	_rtld_linkmap_add(&_rtld_objself);
503 
504 	++_rtld_objmain->refcount;
505 	_rtld_objmain->mainref = 1;
506 	_rtld_objlist_push_tail(&_rtld_list_main, _rtld_objmain);
507 
508 	if (_rtld_trust) {
509 		/*
510 		 * Pre-load user-specified objects after the main program
511 		 * but before any shared object dependencies.
512 		 */
513 		dbg(("preloading objects"));
514 		if (_rtld_preload(getenv("LD_PRELOAD")) == -1)
515 			_rtld_die();
516 	} else
517 		unsetenv("LD_PRELOAD");
518 
519 	dbg(("loading needed objects"));
520 	if (_rtld_load_needed_objects(_rtld_objmain, RTLD_MAIN) == -1)
521 		_rtld_die();
522 
523 	dbg(("relocating objects"));
524 	if (_rtld_relocate_objects(_rtld_objmain, bind_now) == -1)
525 		_rtld_die();
526 
527 	dbg(("doing copy relocations"));
528 	if (_rtld_do_copy_relocations(_rtld_objmain) == -1)
529 		_rtld_die();
530 
531 	/*
532 	 * Set the __progname,  environ and, __mainprog_obj before
533 	 * calling anything that might use them.
534 	 */
535 	real___progname = _rtld_objmain_sym("__progname");
536 	if (real___progname) {
537 		if (argv[0] != NULL) {
538 			if ((*real___progname = strrchr(argv[0], '/')) == NULL)
539 				(*real___progname) = argv[0];
540 			else
541 				(*real___progname)++;
542 		} else {
543 			(*real___progname) = NULL;
544 		}
545 	}
546 	real_environ = _rtld_objmain_sym("environ");
547 	if (real_environ)
548 		*real_environ = environ;
549 	/*
550 	 * Set __mainprog_obj for old binaries.
551 	 */
552 	real___mainprog_obj = _rtld_objmain_sym("__mainprog_obj");
553 	if (real___mainprog_obj)
554 		*real___mainprog_obj = _rtld_objmain;
555 
556 	dbg(("calling _init functions"));
557 	_rtld_call_init_functions();
558 
559 	dbg(("control at program entry point = %p, obj = %p, exit = %p",
560 	     _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
561 
562 	/*
563 	 * Return with the entry point and the exit procedure in at the top
564 	 * of stack.
565 	 */
566 
567 	_rtld_debug_state();	/* say hello to gdb! */
568 
569 	((void **) osp)[0] = _rtld_exit;
570 	((void **) osp)[1] = _rtld_objmain;
571 	return (Elf_Addr) _rtld_objmain->entry;
572 }
573 
574 void
575 _rtld_die(void)
576 {
577 	const char *msg = dlerror();
578 
579 	if (msg == NULL)
580 		msg = "Fatal error";
581 	xerrx(1, "%s", msg);
582 }
583 
584 static Obj_Entry *
585 _rtld_dlcheck(void *handle)
586 {
587 	Obj_Entry *obj;
588 
589 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
590 		if (obj == (Obj_Entry *) handle)
591 			break;
592 
593 	if (obj == NULL || obj->dl_refcount == 0) {
594 		xwarnx("Invalid shared object handle %p", handle);
595 		return NULL;
596 	}
597 	return obj;
598 }
599 
600 static void
601 _rtld_initlist_visit(Objlist* list, Obj_Entry *obj, int rev)
602 {
603 	Needed_Entry* elm;
604 
605 	/* dbg(("_rtld_initlist_visit(%s)", obj->path)); */
606 
607 	if (obj->init_done)
608 		return;
609 	obj->init_done = 1;
610 
611 	for (elm = obj->needed; elm != NULL; elm = elm->next) {
612 		if (elm->obj != NULL) {
613 			_rtld_initlist_visit(list, elm->obj, rev);
614 		}
615 	}
616 
617 	if (rev) {
618 		_rtld_objlist_push_head(list, obj);
619 	} else {
620 		_rtld_objlist_push_tail(list, obj);
621 	}
622 }
623 
624 static void
625 _rtld_initlist_tsort(Objlist* list, int rev)
626 {
627 	dbg(("_rtld_initlist_tsort"));
628 
629 	Obj_Entry* obj;
630 
631 	for (obj = _rtld_objlist->next; obj; obj = obj->next) {
632 		obj->init_done = 0;
633 	}
634 
635 	for (obj = _rtld_objlist->next; obj; obj = obj->next) {
636 		_rtld_initlist_visit(list, obj, rev);
637 	}
638 }
639 
640 static void
641 _rtld_init_dag(Obj_Entry *root)
642 {
643 
644 	_rtld_init_dag1(root, root);
645 }
646 
647 static void
648 _rtld_init_dag1(Obj_Entry *root, Obj_Entry *obj)
649 {
650 	const Needed_Entry *needed;
651 
652 	if (!obj->mainref) {
653 		if (_rtld_objlist_find(&obj->dldags, root))
654 			return;
655 		rdbg(("add %p (%s) to %p (%s) DAG", obj, obj->path, root,
656 		    root->path));
657 		_rtld_objlist_push_tail(&obj->dldags, root);
658 		_rtld_objlist_push_tail(&root->dagmembers, obj);
659 	}
660 	for (needed = obj->needed; needed != NULL; needed = needed->next)
661 		if (needed->obj != NULL)
662 			_rtld_init_dag1(root, needed->obj);
663 }
664 
665 /*
666  * Note, this is called only for objects loaded by dlopen().
667  */
668 static void
669 _rtld_unload_object(Obj_Entry *root, bool do_fini_funcs)
670 {
671 
672 	_rtld_unref_dag(root);
673 	if (root->refcount == 0) { /* We are finished with some objects. */
674 		Obj_Entry *obj;
675 		Obj_Entry **linkp;
676 		Objlist_Entry *elm;
677 
678 		/* Finalize objects that are about to be unmapped. */
679 		if (do_fini_funcs)
680 			_rtld_call_fini_functions(0);
681 
682 		/* Remove the DAG from all objects' DAG lists. */
683 		SIMPLEQ_FOREACH(elm, &root->dagmembers, link)
684 			_rtld_objlist_remove(&elm->obj->dldags, root);
685 
686 		/* Remove the DAG from the RTLD_GLOBAL list. */
687 		if (root->globalref) {
688 			root->globalref = 0;
689 			_rtld_objlist_remove(&_rtld_list_global, root);
690 		}
691 
692 		/* Unmap all objects that are no longer referenced. */
693 		linkp = &_rtld_objlist->next;
694 		while ((obj = *linkp) != NULL) {
695 			if (obj->refcount == 0) {
696 #ifdef RTLD_DEBUG
697 				dbg(("unloading \"%s\"", obj->path));
698 #endif
699 				if (obj->ehdr != MAP_FAILED)
700 					munmap(obj->ehdr, _rtld_pagesz);
701 				munmap(obj->mapbase, obj->mapsize);
702 				_rtld_objlist_remove(&_rtld_list_global, obj);
703 				_rtld_linkmap_delete(obj);
704 				*linkp = obj->next;
705 				_rtld_obj_free(obj);
706 			} else
707 				linkp = &obj->next;
708 		}
709 		_rtld_objtail = linkp;
710 	}
711 }
712 
713 static void
714 _rtld_unref_dag(Obj_Entry *root)
715 {
716 
717 	assert(root);
718 	assert(root->refcount != 0);
719 	--root->refcount;
720 	if (root->refcount == 0) {
721 		const Needed_Entry *needed;
722 
723 		for (needed = root->needed; needed != NULL;
724 		     needed = needed->next) {
725 			if (needed->obj != NULL)
726 				_rtld_unref_dag(needed->obj);
727 		}
728 	}
729 }
730 
731 __strong_alias(__dlclose,dlclose)
732 int
733 dlclose(void *handle)
734 {
735 	Obj_Entry *root = _rtld_dlcheck(handle);
736 
737 	if (root == NULL)
738 		return -1;
739 
740 	_rtld_debug.r_state = RT_DELETE;
741 	_rtld_debug_state();
742 
743 	--root->dl_refcount;
744 	_rtld_unload_object(root, true);
745 
746 	_rtld_debug.r_state = RT_CONSISTENT;
747 	_rtld_debug_state();
748 
749 	return 0;
750 }
751 
752 __strong_alias(__dlerror,dlerror)
753 char *
754 dlerror(void)
755 {
756 	char *msg = error_message;
757 
758 	error_message = NULL;
759 	return msg;
760 }
761 
762 __strong_alias(__dlopen,dlopen)
763 void *
764 dlopen(const char *name, int mode)
765 {
766 	Obj_Entry **old_obj_tail = _rtld_objtail;
767 	Obj_Entry *obj = NULL;
768 
769 	_rtld_debug.r_state = RT_ADD;
770 	_rtld_debug_state();
771 
772 	if (name == NULL) {
773 		obj = _rtld_objmain;
774 		obj->refcount++;
775 	} else
776 		obj = _rtld_load_library(name, _rtld_objmain, mode);
777 
778 	if (obj != NULL) {
779 		++obj->dl_refcount;
780 		if (*old_obj_tail != NULL) {	/* We loaded something new. */
781 			assert(*old_obj_tail == obj);
782 
783 			if (_rtld_load_needed_objects(obj, mode) == -1 ||
784 			    (_rtld_init_dag(obj),
785 			    _rtld_relocate_objects(obj,
786 			    ((mode & 3) == RTLD_NOW))) == -1) {
787 				_rtld_unload_object(obj, false);
788 				obj->dl_refcount--;
789 				obj = NULL;
790 			} else {
791 				_rtld_call_init_functions();
792 			}
793 		}
794 	}
795 	_rtld_debug.r_state = RT_CONSISTENT;
796 	_rtld_debug_state();
797 
798 	return obj;
799 }
800 
801 /*
802  * Find a symbol in the main program.
803  */
804 void *
805 _rtld_objmain_sym(const char *name)
806 {
807 	unsigned long hash;
808 	const Elf_Sym *def;
809 	const Obj_Entry *obj;
810 
811 	hash = _rtld_elf_hash(name);
812 	obj = _rtld_objmain;
813 
814 	def = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, false);
815 
816 	if (def != NULL)
817 		return obj->relocbase + def->st_value;
818 	return(NULL);
819 }
820 
821 #ifdef __powerpc__
822 static void *
823 hackish_return_address(void)
824 {
825 	return __builtin_return_address(1);
826 }
827 #endif
828 
829 __strong_alias(__dlsym,dlsym)
830 void *
831 dlsym(void *handle, const char *name)
832 {
833 	const Obj_Entry *obj;
834 	unsigned long hash;
835 	const Elf_Sym *def;
836 	const Obj_Entry *defobj;
837 	void *retaddr;
838 
839 	hash = _rtld_elf_hash(name);
840 	def = NULL;
841 	defobj = NULL;
842 
843 	switch ((intptr_t)handle) {
844 	case (intptr_t)NULL:
845 	case (intptr_t)RTLD_NEXT:
846 	case (intptr_t)RTLD_DEFAULT:
847 	case (intptr_t)RTLD_SELF:
848 #ifdef __powerpc__
849 		retaddr = hackish_return_address();
850 #else
851 		retaddr = __builtin_return_address(0);
852 #endif
853 		if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
854 			_rtld_error("Cannot determine caller's shared object");
855 			return NULL;
856 		}
857 
858 		switch ((intptr_t)handle) {
859 		case (intptr_t)NULL:	 /* Just the caller's shared object. */
860 			def = _rtld_symlook_obj(name, hash, obj, false);
861 			defobj = obj;
862 			break;
863 
864 		case (intptr_t)RTLD_NEXT:	/* Objects after callers */
865 			obj = obj->next;
866 			/*FALLTHROUGH*/
867 
868 		case (intptr_t)RTLD_SELF:	/* Caller included */
869 			for (; obj; obj = obj->next) {
870 				if ((def = _rtld_symlook_obj(name, hash, obj,
871 				    false)) != NULL) {
872 					defobj = obj;
873 					break;
874 				}
875 			}
876 			break;
877 
878 		case (intptr_t)RTLD_DEFAULT:
879 			def = _rtld_symlook_default(name, hash, obj, &defobj,
880 			    false);
881 			break;
882 
883 		default:
884 			abort();
885 		}
886 		break;
887 
888 	default:
889 		if ((obj = _rtld_dlcheck(handle)) == NULL)
890 			return NULL;
891 
892 		if (obj->mainprog) {
893 			/* Search main program and all libraries loaded by it */
894 			def = _rtld_symlook_list(name, hash, &_rtld_list_main,
895 			    &defobj, false);
896 		} else {
897 			Needed_Entry fake;
898 
899 			/* Search the object and all the libraries loaded by it. */
900 			fake.next = NULL;
901 			fake.obj = (Obj_Entry *)obj;
902 			fake.name = 0;
903 			def = _rtld_symlook_needed(name, hash, &fake, &defobj,
904 			    false);
905 		}
906 		break;
907 	}
908 
909 	if (def != NULL) {
910 #ifdef __HAVE_FUNCTION_DESCRIPTORS
911 		if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
912 			return (void *)_rtld_function_descriptor_alloc(defobj,
913 			    def, 0);
914 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
915 		return defobj->relocbase + def->st_value;
916 	}
917 
918 	_rtld_error("Undefined symbol \"%s\"", name);
919 	return NULL;
920 }
921 
922 __strong_alias(__dladdr,dladdr)
923 int
924 dladdr(const void *addr, Dl_info *info)
925 {
926 	const Obj_Entry *obj;
927 	const Elf_Sym *def, *best_def;
928 	void *symbol_addr;
929 	unsigned long symoffset;
930 
931 #ifdef __HAVE_FUNCTION_DESCRIPTORS
932 	addr = _rtld_function_descriptor_function(addr);
933 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
934 
935 	obj = _rtld_obj_from_addr(addr);
936 	if (obj == NULL) {
937 		_rtld_error("No shared object contains address");
938 		return 0;
939 	}
940 	info->dli_fname = obj->path;
941 	info->dli_fbase = obj->mapbase;
942 	info->dli_saddr = (void *)0;
943 	info->dli_sname = NULL;
944 
945 	/*
946 	 * Walk the symbol list looking for the symbol whose address is
947 	 * closest to the address sent in.
948 	 */
949 	best_def = NULL;
950 	for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
951 		def = obj->symtab + symoffset;
952 
953 		/*
954 		 * For skip the symbol if st_shndx is either SHN_UNDEF or
955 		 * SHN_COMMON.
956 		 */
957 		if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
958 			continue;
959 
960 		/*
961 		 * If the symbol is greater than the specified address, or if it
962 		 * is further away from addr than the current nearest symbol,
963 		 * then reject it.
964 		 */
965 		symbol_addr = obj->relocbase + def->st_value;
966 		if (symbol_addr > addr || symbol_addr < info->dli_saddr)
967 			continue;
968 
969 		/* Update our idea of the nearest symbol. */
970 		info->dli_sname = obj->strtab + def->st_name;
971 		info->dli_saddr = symbol_addr;
972 		best_def = def;
973 
974 		/* Exact match? */
975 		if (info->dli_saddr == addr)
976 			break;
977 	}
978 
979 #ifdef __HAVE_FUNCTION_DESCRIPTORS
980 	if (best_def != NULL && ELF_ST_TYPE(best_def->st_info) == STT_FUNC)
981 		info->dli_saddr = (void *)_rtld_function_descriptor_alloc(obj,
982 		    best_def, 0);
983 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
984 
985 	return 1;
986 }
987 
988 /*
989  * Error reporting function.  Use it like printf.  If formats the message
990  * into a buffer, and sets things up so that the next call to dlerror()
991  * will return the message.
992  */
993 void
994 _rtld_error(const char *fmt,...)
995 {
996 	static char     buf[512];
997 	va_list         ap;
998 
999 	va_start(ap, fmt);
1000 	xvsnprintf(buf, sizeof buf, fmt, ap);
1001 	error_message = buf;
1002 	va_end(ap);
1003 }
1004 
1005 void
1006 _rtld_debug_state(void)
1007 {
1008 
1009 	/* do nothing */
1010 }
1011 
1012 void
1013 _rtld_linkmap_add(Obj_Entry *obj)
1014 {
1015 	struct link_map *l = &obj->linkmap;
1016 	struct link_map *prev;
1017 
1018 	obj->linkmap.l_name = obj->path;
1019 	obj->linkmap.l_addr = obj->relocbase;
1020 	obj->linkmap.l_ld = obj->dynamic;
1021 #ifdef __mips__
1022 	/* XXX This field is not standard and will be removed eventually. */
1023 	obj->linkmap.l_offs = obj->relocbase;
1024 #endif
1025 
1026 	if (_rtld_debug.r_map == NULL) {
1027 		_rtld_debug.r_map = l;
1028 		return;
1029 	}
1030 
1031 	/*
1032 	 * Scan to the end of the list, but not past the entry for the
1033 	 * dynamic linker, which we want to keep at the very end.
1034 	 */
1035 	for (prev = _rtld_debug.r_map;
1036 	    prev->l_next != NULL && prev->l_next != &_rtld_objself.linkmap;
1037 	    prev = prev->l_next);
1038 
1039 	l->l_prev = prev;
1040 	l->l_next = prev->l_next;
1041 	if (l->l_next != NULL)
1042 		l->l_next->l_prev = l;
1043 	prev->l_next = l;
1044 }
1045 
1046 void
1047 _rtld_linkmap_delete(Obj_Entry *obj)
1048 {
1049 	struct link_map *l = &obj->linkmap;
1050 
1051 	if (l->l_prev == NULL) {
1052 		if ((_rtld_debug.r_map = l->l_next) != NULL)
1053 			l->l_next->l_prev = NULL;
1054 		return;
1055 	}
1056 	if ((l->l_prev->l_next = l->l_next) != NULL)
1057 		l->l_next->l_prev = l->l_prev;
1058 }
1059 
1060 static Obj_Entry *
1061 _rtld_obj_from_addr(const void *addr)
1062 {
1063 	Obj_Entry *obj;
1064 
1065 	for (obj = _rtld_objlist;  obj != NULL;  obj = obj->next) {
1066 		if (addr < (void *) obj->mapbase)
1067 			continue;
1068 		if (addr < (void *) (obj->mapbase + obj->mapsize))
1069 			return obj;
1070 	}
1071 	return NULL;
1072 }
1073 
1074 static void
1075 _rtld_objlist_clear(Objlist *list)
1076 {
1077 	while (!SIMPLEQ_EMPTY(list)) {
1078 		Objlist_Entry* elm = SIMPLEQ_FIRST(list);
1079 		SIMPLEQ_REMOVE_HEAD(list, link);
1080 		xfree(elm);
1081 	}
1082 }
1083 
1084 static void
1085 _rtld_objlist_remove(Objlist *list, Obj_Entry *obj)
1086 {
1087 	Objlist_Entry *elm;
1088 
1089 	if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
1090 		SIMPLEQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
1091 		xfree(elm);
1092 	}
1093 }
1094