xref: /netbsd/libexec/ld.elf_so/rtld.c (revision dcbe31f9)
1 /*	$NetBSD: rtld.c,v 1.214 2023/06/04 23:42:38 riastradh 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.214 2023/06/04 23:42:38 riastradh Exp $");
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/atomic.h>
48 #include <sys/mman.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <lwp.h>
53 #include <stdarg.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <dirent.h>
59 
60 #include <ctype.h>
61 
62 #include <dlfcn.h>
63 #include "debug.h"
64 #include "rtld.h"
65 
66 #if !defined(lint)
67 #include "sysident.h"
68 #endif
69 
70 /*
71  * Function declarations.
72  */
73 static void     _rtld_init(caddr_t, caddr_t, const char *);
74 static void     _rtld_exit(void);
75 
76 Elf_Addr        _rtld(Elf_Addr *, Elf_Addr);
77 
78 
79 /*
80  * Data declarations.
81  */
82 static char    *error_message;	/* Message for dlopen(), or NULL */
83 
84 struct r_debug  _rtld_debug;	/* The SVR4 interface for the debugger */
85 bool            _rtld_trust;	/* False for setuid and setgid programs */
86 Obj_Entry      *_rtld_objlist;	/* Head of linked list of shared objects */
87 Obj_Entry     **_rtld_objtail;	/* Link field of last object in list */
88 Obj_Entry      *_rtld_objmain;	/* The main program shared object */
89 Obj_Entry       _rtld_objself;	/* The dynamic linker shared object */
90 u_int		_rtld_objcount;	/* Number of objects in _rtld_objlist */
91 u_int		_rtld_objloads;	/* Number of objects loaded in _rtld_objlist */
92 u_int		_rtld_objgen;	/* Generation count for _rtld_objlist */
93 const char	_rtld_path[] = _PATH_RTLD;
94 
95 /* Initialize a fake symbol for resolving undefined weak references. */
96 Elf_Sym		_rtld_sym_zero = {
97     .st_info	= ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
98     .st_shndx	= SHN_ABS,
99 };
100 size_t	_rtld_pagesz;	/* Page size, as provided by kernel */
101 
102 Search_Path    *_rtld_default_paths;
103 Search_Path    *_rtld_paths;
104 
105 Library_Xform  *_rtld_xforms;
106 static void    *auxinfo;
107 
108 /*
109  * Global declarations normally provided by crt0.
110  */
111 char           *__progname;
112 char          **environ;
113 
114 static volatile bool _rtld_mutex_may_recurse;
115 
116 #if defined(RTLD_DEBUG)
117 #ifndef __sh__
118 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
119 #else  /* 32-bit SuperH */
120 register Elf_Addr *_GLOBAL_OFFSET_TABLE_ asm("r12");
121 #endif
122 #endif /* RTLD_DEBUG */
123 extern Elf_Dyn  _DYNAMIC;
124 
125 static void _rtld_call_fini_functions(sigset_t *, int);
126 static void _rtld_call_init_functions(sigset_t *);
127 static void _rtld_initlist_visit(Objlist *, Obj_Entry *, int);
128 static void _rtld_initlist_tsort(Objlist *, int);
129 static Obj_Entry *_rtld_dlcheck(void *);
130 static void _rtld_init_dag(Obj_Entry *);
131 static void _rtld_init_dag1(Obj_Entry *, Obj_Entry *);
132 static void _rtld_objlist_remove(Objlist *, Obj_Entry *);
133 static void _rtld_objlist_clear(Objlist *);
134 static void _rtld_unload_object(sigset_t *, Obj_Entry *, bool);
135 static void _rtld_unref_dag(Obj_Entry *);
136 static Obj_Entry *_rtld_obj_from_addr(const void *);
137 static void _rtld_fill_dl_phdr_info(const Obj_Entry *, struct dl_phdr_info *);
138 
139 static inline void
_rtld_call_initfini_function(fptr_t func,sigset_t * mask)140 _rtld_call_initfini_function(fptr_t func, sigset_t *mask)
141 {
142 	_rtld_exclusive_exit(mask);
143 	(*func)();
144 	_rtld_exclusive_enter(mask);
145 }
146 
147 static void
_rtld_call_fini_function(Obj_Entry * obj,sigset_t * mask,u_int cur_objgen)148 _rtld_call_fini_function(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
149 {
150 	if (obj->fini_arraysz == 0 && (obj->fini == NULL || obj->fini_called))
151 		return;
152 
153 	if (obj->fini != NULL && !obj->fini_called) {
154 		dbg (("calling fini function %s at %p%s", obj->path,
155 		    (void *)obj->fini,
156 		    obj->z_initfirst ? " (DF_1_INITFIRST)" : ""));
157 		obj->fini_called = 1;
158 		_rtld_call_initfini_function(obj->fini, mask);
159 	}
160 #ifdef HAVE_INITFINI_ARRAY
161 	/*
162 	 * Now process the fini_array if it exists.  Simply go from
163 	 * start to end.  We need to make restartable so just advance
164 	 * the array pointer and decrement the size each time through
165 	 * the loop.
166 	 */
167 	while (obj->fini_arraysz > 0 && _rtld_objgen == cur_objgen) {
168 		fptr_t fini = *obj->fini_array++;
169 		obj->fini_arraysz--;
170 		dbg (("calling fini array function %s at %p%s", obj->path,
171 		    (void *)fini,
172 		    obj->z_initfirst ? " (DF_1_INITFIRST)" : ""));
173 		_rtld_call_initfini_function(fini, mask);
174 	}
175 #endif /* HAVE_INITFINI_ARRAY */
176 }
177 
178 static void
_rtld_call_fini_functions(sigset_t * mask,int force)179 _rtld_call_fini_functions(sigset_t *mask, int force)
180 {
181 	Objlist_Entry *elm;
182 	Objlist finilist;
183 	u_int cur_objgen;
184 
185 	dbg(("_rtld_call_fini_functions(%d)", force));
186 
187 restart:
188 	cur_objgen = ++_rtld_objgen;
189 	SIMPLEQ_INIT(&finilist);
190 	_rtld_initlist_tsort(&finilist, 1);
191 
192 	/* First pass: objects _not_ marked with DF_1_INITFIRST. */
193 	SIMPLEQ_FOREACH(elm, &finilist, link) {
194 		Obj_Entry * const obj = elm->obj;
195 		if (!obj->z_initfirst) {
196 			if (obj->refcount > 0 && !force) {
197 				continue;
198 			}
199 			/*
200 			 * XXX This can race against a concurrent dlclose().
201 			 * XXX In that case, the object could be unmapped before
202 			 * XXX the fini() call or the fini_array has completed.
203 			 */
204 			_rtld_call_fini_function(obj, mask, cur_objgen);
205 			if (_rtld_objgen != cur_objgen) {
206 				dbg(("restarting fini iteration"));
207 				_rtld_objlist_clear(&finilist);
208 				goto restart;
209 		}
210 		}
211 	}
212 
213 	/* Second pass: objects marked with DF_1_INITFIRST. */
214 	SIMPLEQ_FOREACH(elm, &finilist, link) {
215 		Obj_Entry * const obj = elm->obj;
216 		if (obj->refcount > 0 && !force) {
217 			continue;
218 		}
219 		/* XXX See above for the race condition here */
220 		_rtld_call_fini_function(obj, mask, cur_objgen);
221 		if (_rtld_objgen != cur_objgen) {
222 			dbg(("restarting fini iteration"));
223 			_rtld_objlist_clear(&finilist);
224 			goto restart;
225 		}
226 	}
227 
228         _rtld_objlist_clear(&finilist);
229 }
230 
231 static void
_rtld_call_init_function(Obj_Entry * obj,sigset_t * mask,u_int cur_objgen)232 _rtld_call_init_function(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
233 {
234 	if (obj->init_arraysz == 0 && (obj->init_called || obj->init == NULL))
235 		return;
236 
237 	if (!obj->init_called && obj->init != NULL) {
238 		dbg (("calling init function %s at %p%s",
239 		    obj->path, (void *)obj->init,
240 		    obj->z_initfirst ? " (DF_1_INITFIRST)" : ""));
241 		obj->init_called = 1;
242 		_rtld_call_initfini_function(obj->init, mask);
243 	}
244 
245 #ifdef HAVE_INITFINI_ARRAY
246 	/*
247 	 * Now process the init_array if it exists.  Simply go from
248 	 * start to end.  We need to make restartable so just advance
249 	 * the array pointer and decrement the size each time through
250 	 * the loop.
251 	 */
252 	while (obj->init_arraysz > 0 && _rtld_objgen == cur_objgen) {
253 		fptr_t init = *obj->init_array++;
254 		obj->init_arraysz--;
255 		dbg (("calling init_array function %s at %p%s",
256 		    obj->path, (void *)init,
257 		    obj->z_initfirst ? " (DF_1_INITFIRST)" : ""));
258 		_rtld_call_initfini_function(init, mask);
259 	}
260 #endif /* HAVE_INITFINI_ARRAY */
261 }
262 
263 static bool
_rtld_call_ifunc_functions(sigset_t * mask,Obj_Entry * obj,u_int cur_objgen)264 _rtld_call_ifunc_functions(sigset_t *mask, Obj_Entry *obj, u_int cur_objgen)
265 {
266 	if (obj->ifunc_remaining
267 #if defined(IFUNC_NONPLT)
268 	    || obj->ifunc_remaining_nonplt
269 #endif
270 	) {
271 		_rtld_call_ifunc(obj, mask, cur_objgen);
272 		if (_rtld_objgen != cur_objgen) {
273 			return true;
274 		}
275 	}
276 	return false;
277 }
278 
279 static void
_rtld_call_init_functions(sigset_t * mask)280 _rtld_call_init_functions(sigset_t *mask)
281 {
282 	Objlist_Entry *elm;
283 	Objlist initlist;
284 	u_int cur_objgen;
285 
286 	dbg(("_rtld_call_init_functions()"));
287 
288 restart:
289 	cur_objgen = ++_rtld_objgen;
290 	SIMPLEQ_INIT(&initlist);
291 	_rtld_initlist_tsort(&initlist, 0);
292 
293 	/* First pass: objects with IRELATIVE relocations. */
294 	SIMPLEQ_FOREACH(elm, &initlist, link) {
295 		if (_rtld_call_ifunc_functions(mask, elm->obj, cur_objgen)) {
296 			dbg(("restarting init iteration"));
297 			_rtld_objlist_clear(&initlist);
298 			goto restart;
299 		}
300 	}
301 	/*
302 	 * XXX: For historic reasons, init/fini of the main object are called
303 	 * from crt0. Don't introduce that mistake for ifunc, so look at
304 	 * the head of _rtld_objlist that _rtld_initlist_tsort skipped.
305 	 */
306 	if (_rtld_call_ifunc_functions(mask, _rtld_objlist, cur_objgen)) {
307 		dbg(("restarting init iteration"));
308 		_rtld_objlist_clear(&initlist);
309 		goto restart;
310 	}
311 
312 	/* Second pass: objects marked with DF_1_INITFIRST. */
313 	SIMPLEQ_FOREACH(elm, &initlist, link) {
314 		Obj_Entry * const obj = elm->obj;
315 		if (obj->z_initfirst) {
316 			_rtld_call_init_function(obj, mask, cur_objgen);
317 			if (_rtld_objgen != cur_objgen) {
318 				dbg(("restarting init iteration"));
319 				_rtld_objlist_clear(&initlist);
320 				goto restart;
321 			}
322 		}
323 	}
324 
325 	/* Third pass: all other objects. */
326 	SIMPLEQ_FOREACH(elm, &initlist, link) {
327 		_rtld_call_init_function(elm->obj, mask, cur_objgen);
328 		if (_rtld_objgen != cur_objgen) {
329 			dbg(("restarting init iteration"));
330 			_rtld_objlist_clear(&initlist);
331 			goto restart;
332 		}
333 	}
334 
335         _rtld_objlist_clear(&initlist);
336 }
337 
338 /*
339  * Initialize the dynamic linker.  The argument is the address at which
340  * the dynamic linker has been mapped into memory.  The primary task of
341  * this function is to create an Obj_Entry for the dynamic linker and
342  * to resolve the PLT relocation for platforms that need it (those that
343  * define __HAVE_FUNCTION_DESCRIPTORS
344  */
345 static void
_rtld_init(caddr_t mapbase,caddr_t relocbase,const char * execname)346 _rtld_init(caddr_t mapbase, caddr_t relocbase, const char *execname)
347 {
348 	const Elf_Ehdr *ehdr;
349 
350 	/* Conjure up an Obj_Entry structure for the dynamic linker. */
351 	_rtld_objself.path = __UNCONST(_rtld_path);
352 	_rtld_objself.pathlen = sizeof(_rtld_path)-1;
353 	_rtld_objself.rtld = true;
354 	_rtld_objself.mapbase = mapbase;
355 	_rtld_objself.relocbase = relocbase;
356 	_rtld_objself.dynamic = (Elf_Dyn *) &_DYNAMIC;
357 	_rtld_objself.strtab = "_rtld_sym_zero";
358 
359 	/*
360 	 * Set value to -relocbase so that
361 	 *
362 	 *     _rtld_objself.relocbase + _rtld_sym_zero.st_value == 0
363 	 *
364 	 * This allows unresolved references to weak symbols to be computed
365 	 * to a value of 0.
366 	 */
367 	_rtld_sym_zero.st_value = -(uintptr_t)relocbase;
368 
369 	_rtld_digest_dynamic(_rtld_path, &_rtld_objself);
370 	assert(!_rtld_objself.needed);
371 #if !defined(__hppa__)
372 	assert(!_rtld_objself.pltrel && !_rtld_objself.pltrela);
373 #else
374 	_rtld_relocate_plt_objects(&_rtld_objself);
375 #endif
376 #if !defined(__mips__) && !defined(__hppa__)
377 	assert(!_rtld_objself.pltgot);
378 #endif
379 #if !defined(__arm__) && !defined(__mips__) && !defined(__sh__)
380 	/* ARM, MIPS and SH{3,5} have a bogus DT_TEXTREL. */
381 	assert(!_rtld_objself.textrel);
382 #endif
383 
384 	_rtld_add_paths(execname, &_rtld_default_paths,
385 	    RTLD_DEFAULT_LIBRARY_PATH);
386 
387 #ifdef RTLD_ARCH_SUBDIR
388 	_rtld_add_paths(execname, &_rtld_default_paths,
389 	    RTLD_DEFAULT_LIBRARY_PATH "/" RTLD_ARCH_SUBDIR);
390 #endif
391 
392 	/* Make the object list empty. */
393 	_rtld_objlist = NULL;
394 	_rtld_objtail = &_rtld_objlist;
395 	_rtld_objcount = 0;
396 
397 	_rtld_debug.r_version = R_DEBUG_VERSION;
398 	_rtld_debug.r_brk = _rtld_debug_state;
399 	_rtld_debug.r_state = RT_CONSISTENT;
400 	_rtld_debug.r_ldbase = _rtld_objself.relocbase;
401 
402 	ehdr = (Elf_Ehdr *)mapbase;
403 	_rtld_objself.phdr = (Elf_Phdr *)((char *)mapbase + ehdr->e_phoff);
404 	_rtld_objself.phsize = ehdr->e_phnum * sizeof(_rtld_objself.phdr[0]);
405 }
406 
407 /*
408  * Cleanup procedure.  It will be called (by the atexit() mechanism) just
409  * before the process exits.
410  */
411 static void
_rtld_exit(void)412 _rtld_exit(void)
413 {
414 	sigset_t mask;
415 
416 	dbg(("rtld_exit()"));
417 
418 	_rtld_exclusive_enter(&mask);
419 
420 	_rtld_call_fini_functions(&mask, 1);
421 
422 	_rtld_exclusive_exit(&mask);
423 }
424 
425 __dso_public void *
_dlauxinfo(void)426 _dlauxinfo(void)
427 {
428 	return auxinfo;
429 }
430 
431 /*
432  * Main entry point for dynamic linking.  The argument is the stack
433  * pointer.  The stack is expected to be laid out as described in the
434  * SVR4 ABI specification, Intel 386 Processor Supplement.  Specifically,
435  * the stack pointer points to a word containing ARGC.  Following that
436  * in the stack is a null-terminated sequence of pointers to argument
437  * strings.  Then comes a null-terminated sequence of pointers to
438  * environment strings.  Finally, there is a sequence of "auxiliary
439  * vector" entries.
440  *
441  * This function returns the entry point for the main program, the dynamic
442  * linker's exit procedure in sp[0], and a pointer to the main object in
443  * sp[1].
444  */
445 Elf_Addr
_rtld(Elf_Addr * sp,Elf_Addr relocbase)446 _rtld(Elf_Addr *sp, Elf_Addr relocbase)
447 {
448 	const AuxInfo  *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
449 	               *pAUX_phent, *pAUX_phnum, *pAUX_euid, *pAUX_egid,
450 		       *pAUX_ruid, *pAUX_rgid;
451 	const AuxInfo  *pAUX_pagesz;
452 	char          **env, **oenvp;
453 	const AuxInfo  *auxp;
454 	Obj_Entry      *obj;
455 	Elf_Addr       *const osp = sp;
456 	bool            bind_now = 0;
457 	const char     *ld_bind_now, *ld_preload, *ld_library_path;
458 	const char    **argv;
459 	const char     *execname;
460 	long		argc;
461 	const char **real___progname;
462 	const Obj_Entry **real___mainprog_obj;
463 	char ***real_environ;
464 	sigset_t        mask;
465 #ifdef DEBUG
466 	const char     *ld_debug;
467 #endif
468 #ifdef RTLD_DEBUG
469 	int i = 0;
470 #endif
471 
472 	/*
473          * On entry, the dynamic linker itself has not been relocated yet.
474          * Be very careful not to reference any global data until after
475          * _rtld_init has returned.  It is OK to reference file-scope statics
476          * and string constants, and to call static and global functions.
477          */
478 	/* Find the auxiliary vector on the stack. */
479 	/* first Elf_Word reserved to address of exit routine */
480 #if defined(RTLD_DEBUG)
481 	debug = 1;
482 	dbg(("sp = %p, argc = %ld, argv = %p <%s> relocbase %p", sp,
483 	    (long)sp[2], &sp[3], (char *) sp[3], (void *)relocbase));
484 #ifndef __x86_64__
485 	dbg(("got is at %p, dynamic is at %p", _GLOBAL_OFFSET_TABLE_,
486 	    &_DYNAMIC));
487 #endif
488 #endif
489 
490 	sp += 2;		/* skip over return argument space */
491 	argv = (const char **) &sp[1];
492 	argc = *(long *)sp;
493 	sp += 2 + argc;		/* Skip over argc, arguments, and NULL
494 				 * terminator */
495 	env = (char **) sp;
496 	while (*sp++ != 0) {	/* Skip over environment, and NULL terminator */
497 #if defined(RTLD_DEBUG)
498 		dbg(("env[%d] = %p %s", i++, (void *)sp[-1], (char *)sp[-1]));
499 #endif
500 	}
501 	auxinfo = (AuxInfo *) sp;
502 
503 	pAUX_base = pAUX_entry = pAUX_execfd = NULL;
504 	pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
505 	pAUX_euid = pAUX_ruid = pAUX_egid = pAUX_rgid = NULL;
506 	pAUX_pagesz = NULL;
507 
508 	execname = NULL;
509 
510 	/* Digest the auxiliary vector. */
511 	for (auxp = auxinfo; auxp->a_type != AT_NULL; ++auxp) {
512 		switch (auxp->a_type) {
513 		case AT_BASE:
514 			pAUX_base = auxp;
515 			break;
516 		case AT_ENTRY:
517 			pAUX_entry = auxp;
518 			break;
519 		case AT_EXECFD:
520 			pAUX_execfd = auxp;
521 			break;
522 		case AT_PHDR:
523 			pAUX_phdr = auxp;
524 			break;
525 		case AT_PHENT:
526 			pAUX_phent = auxp;
527 			break;
528 		case AT_PHNUM:
529 			pAUX_phnum = auxp;
530 			break;
531 #ifdef AT_EUID
532 		case AT_EUID:
533 			pAUX_euid = auxp;
534 			break;
535 		case AT_RUID:
536 			pAUX_ruid = auxp;
537 			break;
538 		case AT_EGID:
539 			pAUX_egid = auxp;
540 			break;
541 		case AT_RGID:
542 			pAUX_rgid = auxp;
543 			break;
544 #endif
545 #ifdef AT_SUN_EXECNAME
546 		case AT_SUN_EXECNAME:
547 			execname = (const char *)(const void *)auxp->a_v;
548 			break;
549 #endif
550 		case AT_PAGESZ:
551 			pAUX_pagesz = auxp;
552 			break;
553 		}
554 	}
555 
556 	/* Initialize and relocate ourselves. */
557 	if (pAUX_base == NULL) {
558 		_rtld_error("Bad pAUX_base");
559 		_rtld_die();
560 	}
561 	assert(pAUX_pagesz != NULL);
562 	_rtld_pagesz = (int)pAUX_pagesz->a_v;
563 	_rtld_init((caddr_t)pAUX_base->a_v, (caddr_t)relocbase, execname);
564 
565 	__progname = _rtld_objself.path;
566 	environ = env;
567 
568 	_rtld_trust = ((pAUX_euid ? (uid_t)pAUX_euid->a_v : geteuid()) ==
569 	    (pAUX_ruid ? (uid_t)pAUX_ruid->a_v : getuid())) &&
570 	    ((pAUX_egid ? (gid_t)pAUX_egid->a_v : getegid()) ==
571 	    (pAUX_rgid ? (gid_t)pAUX_rgid->a_v : getgid()));
572 
573 #ifdef DEBUG
574 	ld_debug = NULL;
575 #endif
576 	ld_bind_now = NULL;
577 	ld_library_path = NULL;
578 	ld_preload = NULL;
579 	/*
580 	 * Inline avoid using normal getenv/unsetenv here as the libc
581 	 * code is quite a bit more complicated.
582 	 */
583 	for (oenvp = env; *env != NULL; ++env) {
584 		static const char bind_var[] = "LD_BIND_NOW=";
585 		static const char debug_var[] =  "LD_DEBUG=";
586 		static const char path_var[] = "LD_LIBRARY_PATH=";
587 		static const char preload_var[] = "LD_PRELOAD=";
588 #define LEN(x)	(sizeof(x) - 1)
589 
590 		if ((*env)[0] != 'L' || (*env)[1] != 'D') {
591 			/*
592 			 * Special case to skip most entries without
593 			 * the more expensive calls to strncmp.
594 			 */
595 			*oenvp++ = *env;
596 		} else if (strncmp(*env, debug_var, LEN(debug_var)) == 0) {
597 			if (_rtld_trust) {
598 #ifdef DEBUG
599 				ld_debug = *env + LEN(debug_var);
600 #endif
601 				*oenvp++ = *env;
602 			}
603 		} else if (strncmp(*env, bind_var, LEN(bind_var)) == 0) {
604 			if (_rtld_trust) {
605 				ld_bind_now = *env + LEN(bind_var);
606 				*oenvp++ = *env;
607 			}
608 		} else if (strncmp(*env, path_var, LEN(path_var)) == 0) {
609 			if (_rtld_trust) {
610 				ld_library_path = *env + LEN(path_var);
611 				*oenvp++ = *env;
612 			}
613 		} else if (strncmp(*env, preload_var, LEN(preload_var)) == 0) {
614 			if (_rtld_trust) {
615 				ld_preload = *env + LEN(preload_var);
616 				*oenvp++ = *env;
617 			}
618 		} else {
619 			*oenvp++ = *env;
620 		}
621 #undef LEN
622 	}
623 	*oenvp++ = NULL;
624 
625 	if (ld_bind_now != NULL && *ld_bind_now != '\0')
626 		bind_now = true;
627 	if (_rtld_trust) {
628 #ifdef DEBUG
629 #ifdef RTLD_DEBUG
630 		debug = 0;
631 #endif
632 		if (ld_debug != NULL && *ld_debug != '\0')
633 			debug = 1;
634 #endif
635 		_rtld_add_paths(execname, &_rtld_paths, ld_library_path);
636 	} else {
637 		execname = NULL;
638 	}
639 	_rtld_process_hints(execname, &_rtld_paths, &_rtld_xforms,
640 	    _PATH_LD_HINTS);
641 	dbg(("dynamic linker is initialized, mapbase=%p, relocbase=%p",
642 	     _rtld_objself.mapbase, _rtld_objself.relocbase));
643 
644 	/*
645          * Load the main program, or process its program header if it is
646          * already loaded.
647          */
648 	if (pAUX_execfd != NULL) {	/* Load the main program. */
649 		int             fd = pAUX_execfd->a_v;
650 		const char *obj_name = argv[0] ? argv[0] : "main program";
651 		dbg(("loading main program"));
652 		_rtld_objmain = _rtld_map_object(obj_name, fd, NULL);
653 		close(fd);
654 		if (_rtld_objmain == NULL)
655 			_rtld_die();
656 	} else {		/* Main program already loaded. */
657 		const Elf_Phdr *phdr;
658 		int             phnum;
659 		caddr_t         entry;
660 
661 		dbg(("processing main program's program header"));
662 		assert(pAUX_phdr != NULL);
663 		phdr = (const Elf_Phdr *) pAUX_phdr->a_v;
664 		assert(pAUX_phnum != NULL);
665 		phnum = pAUX_phnum->a_v;
666 		assert(pAUX_phent != NULL);
667 		assert(pAUX_phent->a_v == sizeof(Elf_Phdr));
668 		assert(pAUX_entry != NULL);
669 		entry = (caddr_t) pAUX_entry->a_v;
670 		_rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
671 		_rtld_objmain->path = xstrdup(argv[0] ? argv[0] :
672 		    "main program");
673 		_rtld_objmain->pathlen = strlen(_rtld_objmain->path);
674 	}
675 
676 	_rtld_objmain->mainprog = true;
677 
678 	/*
679 	 * Get the actual dynamic linker pathname from the executable if
680 	 * possible.  (It should always be possible.)  That ensures that
681 	 * the debugger will find the right dynamic linker even if a
682 	 * non-standard one is being used.
683 	 */
684 	if (_rtld_objmain->interp != NULL &&
685 	    strcmp(_rtld_objmain->interp, _rtld_objself.path) != 0) {
686 		_rtld_objself.path = xstrdup(_rtld_objmain->interp);
687 		_rtld_objself.pathlen = strlen(_rtld_objself.path);
688 	}
689 	dbg(("actual dynamic linker is %s", _rtld_objself.path));
690 
691 	_rtld_digest_dynamic(execname, _rtld_objmain);
692 
693 	/* Link the main program into the list of objects. */
694 	*_rtld_objtail = _rtld_objmain;
695 	_rtld_objtail = &_rtld_objmain->next;
696 	_rtld_objcount++;
697 	_rtld_objloads++;
698 
699 	_rtld_linkmap_add(_rtld_objmain);
700 	_rtld_objself.path = xstrdup(_rtld_objself.path);
701 	_rtld_linkmap_add(&_rtld_objself);
702 
703 	++_rtld_objmain->refcount;
704 	_rtld_objmain->mainref = 1;
705 	_rtld_objlist_push_tail(&_rtld_list_main, _rtld_objmain);
706 
707 	if (ld_preload) {
708 		/*
709 		 * Pre-load user-specified objects after the main program
710 		 * but before any shared object dependencies.
711 		 */
712 		dbg(("preloading objects"));
713 		if (_rtld_preload(ld_preload) == -1)
714 			_rtld_die();
715 	}
716 
717 	dbg(("loading needed objects"));
718 	if (_rtld_load_needed_objects(_rtld_objmain, _RTLD_MAIN) == -1)
719 		_rtld_die();
720 
721 	dbg(("checking for required versions"));
722 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
723 		if (_rtld_verify_object_versions(obj) == -1)
724 			_rtld_die();
725 	}
726 
727 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
728 	dbg(("initializing initial Thread Local Storage offsets"));
729 	/*
730 	 * All initial objects get the TLS space from the static block.
731 	 */
732 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
733 		_rtld_tls_offset_allocate(obj);
734 #endif
735 
736 	dbg(("relocating objects"));
737 	if (_rtld_relocate_objects(_rtld_objmain, bind_now) == -1)
738 		_rtld_die();
739 
740 	dbg(("doing copy relocations"));
741 	if (_rtld_do_copy_relocations(_rtld_objmain) == -1)
742 		_rtld_die();
743 
744 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
745 	dbg(("initializing Thread Local Storage for main thread"));
746 	/*
747 	 * Set up TLS area for the main thread.
748 	 * This has to be done after all relocations are processed,
749 	 * since .tdata may contain relocations.
750 	 */
751 	_rtld_tls_initial_allocation();
752 #endif
753 
754 	/*
755 	 * Set the __progname,  environ and, __mainprog_obj before
756 	 * calling anything that might use them.
757 	 */
758 	real___progname = _rtld_objmain_sym("__progname");
759 	if (real___progname) {
760 		if (argv[0] != NULL) {
761 			if ((*real___progname = strrchr(argv[0], '/')) == NULL)
762 				(*real___progname) = argv[0];
763 			else
764 				(*real___progname)++;
765 		} else {
766 			(*real___progname) = NULL;
767 		}
768 	}
769 	real_environ = _rtld_objmain_sym("environ");
770 	if (real_environ)
771 		*real_environ = environ;
772 	/*
773 	 * Set __mainprog_obj for old binaries.
774 	 */
775 	real___mainprog_obj = _rtld_objmain_sym("__mainprog_obj");
776 	if (real___mainprog_obj)
777 		*real___mainprog_obj = _rtld_objmain;
778 
779 	_rtld_debug_state();	/* say hello to the debugger! */
780 
781 	_rtld_exclusive_enter(&mask);
782 
783 	dbg(("calling _init functions"));
784 	_rtld_call_init_functions(&mask);
785 
786 	dbg(("control at program entry point = %p, obj = %p, exit = %p",
787 	     _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
788 
789 	_rtld_exclusive_exit(&mask);
790 
791 	/*
792 	 * Return with the entry point and the exit procedure in at the top
793 	 * of stack.
794 	 */
795 
796 	((void **) osp)[0] = _rtld_exit;
797 	((void **) osp)[1] = __UNCONST(_rtld_compat_obj);
798 	return (Elf_Addr) _rtld_objmain->entry;
799 }
800 
801 void
_rtld_die(void)802 _rtld_die(void)
803 {
804 	const char *msg = dlerror();
805 
806 	if (msg == NULL)
807 		msg = "Fatal error";
808 	xerrx(1, "%s", msg);
809 }
810 
811 static Obj_Entry *
_rtld_dlcheck(void * handle)812 _rtld_dlcheck(void *handle)
813 {
814 	Obj_Entry *obj;
815 
816 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
817 		if (obj == (Obj_Entry *) handle)
818 			break;
819 
820 	if (obj == NULL || obj->dl_refcount == 0) {
821 		_rtld_error("Invalid shared object handle %p", handle);
822 		return NULL;
823 	}
824 	return obj;
825 }
826 
827 static void
_rtld_initlist_visit(Objlist * list,Obj_Entry * obj,int rev)828 _rtld_initlist_visit(Objlist* list, Obj_Entry *obj, int rev)
829 {
830 	Needed_Entry* elm;
831 
832 	/* dbg(("_rtld_initlist_visit(%s)", obj->path)); */
833 
834 	if (obj->init_done)
835 		return;
836 	obj->init_done = 1;
837 
838 	for (elm = obj->needed; elm != NULL; elm = elm->next) {
839 		if (elm->obj != NULL) {
840 			_rtld_initlist_visit(list, elm->obj, rev);
841 		}
842 	}
843 
844 	if (rev) {
845 		_rtld_objlist_push_head(list, obj);
846 	} else {
847 		_rtld_objlist_push_tail(list, obj);
848 	}
849 }
850 
851 static void
_rtld_initlist_tsort(Objlist * list,int rev)852 _rtld_initlist_tsort(Objlist* list, int rev)
853 {
854 	dbg(("_rtld_initlist_tsort"));
855 
856 	Obj_Entry* obj;
857 
858 	/*
859 	 * We don't include objmain here (starting from next)
860 	 * because csu handles it
861 	 */
862 	for (obj = _rtld_objlist->next; obj; obj = obj->next) {
863 		obj->init_done = 0;
864 	}
865 
866 	for (obj = _rtld_objlist->next; obj; obj = obj->next) {
867 		_rtld_initlist_visit(list, obj, rev);
868 	}
869 }
870 
871 static void
_rtld_init_dag(Obj_Entry * root)872 _rtld_init_dag(Obj_Entry *root)
873 {
874 
875 	_rtld_init_dag1(root, root);
876 }
877 
878 static void
_rtld_init_dag1(Obj_Entry * root,Obj_Entry * obj)879 _rtld_init_dag1(Obj_Entry *root, Obj_Entry *obj)
880 {
881 	const Needed_Entry *needed;
882 
883 	if (!obj->mainref) {
884 		if (_rtld_objlist_find(&obj->dldags, root))
885 			return;
886 		dbg(("add %p (%s) to %p (%s) DAG", obj, obj->path, root,
887 		    root->path));
888 		_rtld_objlist_push_tail(&obj->dldags, root);
889 		_rtld_objlist_push_tail(&root->dagmembers, obj);
890 	}
891 	for (needed = obj->needed; needed != NULL; needed = needed->next)
892 		if (needed->obj != NULL)
893 			_rtld_init_dag1(root, needed->obj);
894 }
895 
896 /*
897  * Note, this is called only for objects loaded by dlopen().
898  */
899 static void
_rtld_unload_object(sigset_t * mask,Obj_Entry * root,bool do_fini_funcs)900 _rtld_unload_object(sigset_t *mask, Obj_Entry *root, bool do_fini_funcs)
901 {
902 
903 	_rtld_unref_dag(root);
904 	if (root->refcount == 0) { /* We are finished with some objects. */
905 		Obj_Entry *obj;
906 		Obj_Entry **linkp;
907 		Objlist_Entry *elm;
908 
909 		/* Finalize objects that are about to be unmapped. */
910 		if (do_fini_funcs)
911 			_rtld_call_fini_functions(mask, 0);
912 
913 		/* Remove the DAG from all objects' DAG lists. */
914 		SIMPLEQ_FOREACH(elm, &root->dagmembers, link)
915 			_rtld_objlist_remove(&elm->obj->dldags, root);
916 
917 		/* Remove the DAG from the RTLD_GLOBAL list. */
918 		if (root->globalref) {
919 			root->globalref = 0;
920 			_rtld_objlist_remove(&_rtld_list_global, root);
921 		}
922 
923 		/* Unmap all objects that are no longer referenced. */
924 		linkp = &_rtld_objlist->next;
925 		while ((obj = *linkp) != NULL) {
926 			if (obj->refcount == 0) {
927 				dbg(("unloading \"%s\"", obj->path));
928 				if (obj->ehdr != MAP_FAILED)
929 					munmap(obj->ehdr, _rtld_pagesz);
930 				munmap(obj->mapbase, obj->mapsize);
931 				_rtld_objlist_remove(&_rtld_list_global, obj);
932 				_rtld_linkmap_delete(obj);
933 				*linkp = obj->next;
934 				_rtld_objcount--;
935 				_rtld_obj_free(obj);
936 			} else
937 				linkp = &obj->next;
938 		}
939 		_rtld_objtail = linkp;
940 	}
941 }
942 
943 void
_rtld_ref_dag(Obj_Entry * root)944 _rtld_ref_dag(Obj_Entry *root)
945 {
946 	const Needed_Entry *needed;
947 
948 	assert(root);
949 
950 	++root->refcount;
951 
952 	dbg(("incremented reference on \"%s\" (%d)", root->path,
953 	    root->refcount));
954 	for (needed = root->needed; needed != NULL;
955 	     needed = needed->next) {
956 		if (needed->obj != NULL)
957 			_rtld_ref_dag(needed->obj);
958 	}
959 }
960 
961 static void
_rtld_unref_dag(Obj_Entry * root)962 _rtld_unref_dag(Obj_Entry *root)
963 {
964 
965 	assert(root);
966 	assert(root->refcount != 0);
967 
968 	--root->refcount;
969 	dbg(("decremented reference on \"%s\" (%d)", root->path,
970 	    root->refcount));
971 
972 	if (root->refcount == 0) {
973 		const Needed_Entry *needed;
974 
975 		for (needed = root->needed; needed != NULL;
976 		     needed = needed->next) {
977 			if (needed->obj != NULL)
978 				_rtld_unref_dag(needed->obj);
979 		}
980 	}
981 }
982 
__strong_alias(__dlclose,dlclose)983 __strong_alias(__dlclose,dlclose)
984 int
985 dlclose(void *handle)
986 {
987 	Obj_Entry *root;
988 	sigset_t mask;
989 
990 	dbg(("dlclose of %p", handle));
991 
992 	_rtld_exclusive_enter(&mask);
993 
994 	root = _rtld_dlcheck(handle);
995 
996 	if (root == NULL) {
997 		_rtld_exclusive_exit(&mask);
998 		return -1;
999 	}
1000 
1001 	_rtld_debug.r_state = RT_DELETE;
1002 	_rtld_debug_state();
1003 
1004 	--root->dl_refcount;
1005 	_rtld_unload_object(&mask, root, true);
1006 
1007 	_rtld_debug.r_state = RT_CONSISTENT;
1008 	_rtld_debug_state();
1009 
1010 	_rtld_exclusive_exit(&mask);
1011 
1012 	return 0;
1013 }
1014 
__strong_alias(__dlerror,dlerror)1015 __strong_alias(__dlerror,dlerror)
1016 char *
1017 dlerror(void)
1018 {
1019 	char *msg = error_message;
1020 
1021 	error_message = NULL;
1022 	return msg;
1023 }
1024 
__strong_alias(__dlopen,dlopen)1025 __strong_alias(__dlopen,dlopen)
1026 void *
1027 dlopen(const char *name, int mode)
1028 {
1029 	Obj_Entry **old_obj_tail;
1030 	Obj_Entry *obj = NULL;
1031 	int flags = _RTLD_DLOPEN;
1032 	bool nodelete;
1033 	bool now;
1034 	sigset_t mask;
1035 	int result;
1036 
1037 	dbg(("dlopen of %s 0x%x", name, mode));
1038 
1039 	_rtld_exclusive_enter(&mask);
1040 
1041 	old_obj_tail = _rtld_objtail;
1042 
1043 	flags |= (mode & RTLD_GLOBAL) ? _RTLD_GLOBAL : 0;
1044 	flags |= (mode & RTLD_NOLOAD) ? _RTLD_NOLOAD : 0;
1045 
1046 	nodelete = (mode & RTLD_NODELETE) ? true : false;
1047 	now = ((mode & RTLD_MODEMASK) == RTLD_NOW) ? true : false;
1048 
1049 	_rtld_debug.r_state = RT_ADD;
1050 	_rtld_debug_state();
1051 
1052 	if (name == NULL) {
1053 		obj = _rtld_objmain;
1054 		obj->refcount++;
1055 	} else
1056 		obj = _rtld_load_library(name, _rtld_objmain, flags);
1057 
1058 
1059 	if (obj != NULL) {
1060 		++obj->dl_refcount;
1061 		if (*old_obj_tail != NULL) {	/* We loaded something new. */
1062 			assert(*old_obj_tail == obj);
1063 
1064 			result = _rtld_load_needed_objects(obj, flags);
1065 			if (result != -1) {
1066 				Objlist_Entry *entry;
1067 				_rtld_init_dag(obj);
1068 				SIMPLEQ_FOREACH(entry, &obj->dagmembers, link) {
1069 					result = _rtld_verify_object_versions(entry->obj);
1070 					if (result == -1)
1071 						break;
1072 				}
1073 			}
1074 			if (result == -1 || _rtld_relocate_objects(obj,
1075 			    (now || obj->z_now)) == -1) {
1076 				_rtld_unload_object(&mask, obj, false);
1077 				obj->dl_refcount--;
1078 				obj = NULL;
1079 			} else {
1080 				_rtld_call_init_functions(&mask);
1081 			}
1082 		}
1083 		if (obj != NULL) {
1084 			if ((nodelete || obj->z_nodelete) && !obj->ref_nodel) {
1085 				dbg(("dlopen obj %s nodelete", obj->path));
1086 				_rtld_ref_dag(obj);
1087 				obj->z_nodelete = obj->ref_nodel = true;
1088 			}
1089 		}
1090 	}
1091 	_rtld_debug.r_state = RT_CONSISTENT;
1092 	_rtld_debug_state();
1093 
1094 	dbg(("dlopen of %s 0x%x returned %p%s%s%s", name, mode, obj,
1095 	    obj ? "" : " (", obj ? "" : error_message, obj ? "" : ")"));
1096 
1097 	_rtld_exclusive_exit(&mask);
1098 
1099 	return obj;
1100 }
1101 
1102 /*
1103  * Find a symbol in the main program.
1104  */
1105 void *
_rtld_objmain_sym(const char * name)1106 _rtld_objmain_sym(const char *name)
1107 {
1108 	Elf_Hash hash;
1109 	const Elf_Sym *def;
1110 	const Obj_Entry *obj;
1111 	DoneList donelist;
1112 
1113 	hash.sysv = _rtld_sysv_hash(name);
1114 	hash.gnu = _rtld_gnu_hash(name);
1115 	obj = _rtld_objmain;
1116 	_rtld_donelist_init(&donelist);
1117 
1118 	def = _rtld_symlook_list(name, &hash, &_rtld_list_main, &obj, 0,
1119 	    NULL, &donelist);
1120 
1121 	if (def != NULL)
1122 		return obj->relocbase + def->st_value;
1123 	return NULL;
1124 }
1125 
1126 #if defined(__powerpc__) && !defined(__clang__)
1127 static __noinline void *
hackish_return_address(void)1128 hackish_return_address(void)
1129 {
1130 #if __GNUC_PREREQ__(6,0)
1131 #pragma GCC diagnostic push
1132 #pragma GCC diagnostic ignored "-Wframe-address"
1133 #endif
1134 	return __builtin_return_address(1);
1135 #if __GNUC_PREREQ__(6,0)
1136 #pragma GCC diagnostic pop
1137 #endif
1138 }
1139 #endif
1140 
1141 #ifdef __HAVE_FUNCTION_DESCRIPTORS
1142 #define	lookup_mutex_enter()	_rtld_exclusive_enter(&mask)
1143 #define	lookup_mutex_exit()	_rtld_exclusive_exit(&mask)
1144 #else
1145 #define	lookup_mutex_enter()	_rtld_shared_enter()
1146 #define	lookup_mutex_exit()	_rtld_shared_exit()
1147 #endif
1148 
1149 static void *
do_dlsym(void * handle,const char * name,const Ver_Entry * ventry,void * retaddr)1150 do_dlsym(void *handle, const char *name, const Ver_Entry *ventry, void *retaddr)
1151 {
1152 	const Obj_Entry *obj;
1153 	Elf_Hash hash;
1154 	const Elf_Sym *def;
1155 	const Obj_Entry *defobj;
1156 	DoneList donelist;
1157 	const u_int flags = SYMLOOK_DLSYM | SYMLOOK_IN_PLT;
1158 #ifdef __HAVE_FUNCTION_DESCRIPTORS
1159 	sigset_t mask;
1160 #endif
1161 
1162 	lookup_mutex_enter();
1163 
1164 	hash.sysv = _rtld_sysv_hash(name);
1165 	hash.gnu = _rtld_gnu_hash(name);
1166 	def = NULL;
1167 	defobj = NULL;
1168 
1169 	switch ((intptr_t)handle) {
1170 	case (intptr_t)NULL:
1171 	case (intptr_t)RTLD_NEXT:
1172 	case (intptr_t)RTLD_DEFAULT:
1173 	case (intptr_t)RTLD_SELF:
1174 		if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
1175 			_rtld_error("Cannot determine caller's shared object");
1176 			lookup_mutex_exit();
1177 			return NULL;
1178 		}
1179 
1180 		switch ((intptr_t)handle) {
1181 		case (intptr_t)NULL:	 /* Just the caller's shared object. */
1182 			def = _rtld_symlook_obj(name, &hash, obj, flags, ventry);
1183 			defobj = obj;
1184 			break;
1185 
1186 		case (intptr_t)RTLD_NEXT:	/* Objects after callers */
1187 			obj = obj->next;
1188 			/*FALLTHROUGH*/
1189 
1190 		case (intptr_t)RTLD_SELF:	/* Caller included */
1191 			for (; obj; obj = obj->next) {
1192 				if ((def = _rtld_symlook_obj(name, &hash, obj,
1193 				    flags, ventry)) != NULL) {
1194 					defobj = obj;
1195 					break;
1196 				}
1197 			}
1198 			/*
1199 			 * Search the dynamic linker itself, and possibly
1200 			 * resolve the symbol from there if it is not defined
1201 			 * already or weak. This is how the application links
1202 			 * to dynamic linker services such as dlopen.
1203 			 */
1204 			if (!def || ELF_ST_BIND(def->st_info) == STB_WEAK) {
1205 				const Elf_Sym *symp = _rtld_symlook_obj(name,
1206 				    &hash, &_rtld_objself, flags, ventry);
1207 				if (symp != NULL) {
1208 					def = symp;
1209 					defobj = &_rtld_objself;
1210 				}
1211 			}
1212 			break;
1213 
1214 		case (intptr_t)RTLD_DEFAULT:
1215 			def = _rtld_symlook_default(name, &hash, obj, &defobj,
1216 			    flags, ventry);
1217 			break;
1218 
1219 		default:
1220 			abort();
1221 		}
1222 		break;
1223 
1224 	default:
1225 		if ((obj = _rtld_dlcheck(handle)) == NULL) {
1226 			lookup_mutex_exit();
1227 			return NULL;
1228 		}
1229 
1230 		_rtld_donelist_init(&donelist);
1231 
1232 		if (obj->mainprog) {
1233 			/* Search main program and all libraries loaded by it */
1234 			def = _rtld_symlook_list(name, &hash, &_rtld_list_main,
1235 			    &defobj, flags, ventry, &donelist);
1236 		} else {
1237 			Needed_Entry fake;
1238 			DoneList depth;
1239 
1240 			/* Search the object and all the libraries loaded by it. */
1241 			fake.next = NULL;
1242 			fake.obj = __UNCONST(obj);
1243 			fake.name = 0;
1244 
1245 			_rtld_donelist_init(&depth);
1246 			def = _rtld_symlook_needed(name, &hash, &fake, &defobj,
1247 			    flags, ventry, &donelist, &depth);
1248 		}
1249 
1250 		break;
1251 	}
1252 
1253 	if (def != NULL) {
1254 		void *p;
1255 
1256 		if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
1257 #ifdef __HAVE_FUNCTION_DESCRIPTORS
1258 			lookup_mutex_exit();
1259 			_rtld_shared_enter();
1260 #endif
1261 			p = (void *)_rtld_resolve_ifunc(defobj, def);
1262 			_rtld_shared_exit();
1263 			return p;
1264 		}
1265 
1266 #ifdef __HAVE_FUNCTION_DESCRIPTORS
1267 		if (ELF_ST_TYPE(def->st_info) == STT_FUNC) {
1268 			p = (void *)_rtld_function_descriptor_alloc(defobj,
1269 			    def, 0);
1270 			lookup_mutex_exit();
1271 			return p;
1272 		}
1273 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
1274 		p = defobj->relocbase + def->st_value;
1275 		lookup_mutex_exit();
1276 		return p;
1277 	}
1278 
1279 	_rtld_error("Undefined symbol \"%s\"", name);
1280 	lookup_mutex_exit();
1281 	return NULL;
1282 }
1283 
__strong_alias(__dlsym,dlsym)1284 __strong_alias(__dlsym,dlsym)
1285 void *
1286 dlsym(void *handle, const char *name)
1287 {
1288 	void *retaddr;
1289 
1290 	dbg(("dlsym of %s in %p", name, handle));
1291 
1292 #if defined(__powerpc__) && !defined(__clang__)
1293 	retaddr = hackish_return_address();
1294 #else
1295 	retaddr = __builtin_return_address(0);
1296 #endif
1297 	return do_dlsym(handle, name, NULL, retaddr);
1298 }
1299 
__strong_alias(__dlvsym,dlvsym)1300 __strong_alias(__dlvsym,dlvsym)
1301 void *
1302 dlvsym(void *handle, const char *name, const char *version)
1303 {
1304 	Ver_Entry *ventry = NULL;
1305 	Ver_Entry ver_entry;
1306 	void *retaddr;
1307 
1308 	dbg(("dlvsym of %s@%s in %p", name, version ? version : NULL, handle));
1309 
1310 	if (version != NULL) {
1311 		ver_entry.name = version;
1312 		ver_entry.file = NULL;
1313 		ver_entry.hash = _rtld_sysv_hash(version);
1314 		ver_entry.flags = 0;
1315 		ventry = &ver_entry;
1316 	}
1317 #if defined(__powerpc__) && !defined(__clang__)
1318 	retaddr = hackish_return_address();
1319 #else
1320 	retaddr = __builtin_return_address(0);
1321 #endif
1322 	return do_dlsym(handle, name, ventry, retaddr);
1323 }
1324 
__strong_alias(__dladdr,dladdr)1325 __strong_alias(__dladdr,dladdr)
1326 int
1327 dladdr(const void *addr, Dl_info *info)
1328 {
1329 	const Obj_Entry *obj;
1330 	const Elf_Sym *def, *best_def;
1331 	void *symbol_addr;
1332 	unsigned long symoffset;
1333 #ifdef __HAVE_FUNCTION_DESCRIPTORS
1334 	sigset_t mask;
1335 #endif
1336 
1337 	dbg(("dladdr of %p", addr));
1338 
1339 	lookup_mutex_enter();
1340 
1341 #ifdef __HAVE_FUNCTION_DESCRIPTORS
1342 	addr = _rtld_function_descriptor_function(addr);
1343 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
1344 
1345 	obj = _rtld_obj_from_addr(addr);
1346 	if (obj == NULL) {
1347 		_rtld_error("No shared object contains address");
1348 		lookup_mutex_exit();
1349 		return 0;
1350 	}
1351 	info->dli_fname = obj->path;
1352 	info->dli_fbase = obj->mapbase;
1353 	info->dli_saddr = (void *)0;
1354 	info->dli_sname = NULL;
1355 
1356 	/*
1357 	 * Walk the symbol list looking for the symbol whose address is
1358 	 * closest to the address sent in.
1359 	 */
1360 	best_def = NULL;
1361 	for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
1362 		def = obj->symtab + symoffset;
1363 
1364 		/*
1365 		 * For skip the symbol if st_shndx is either SHN_UNDEF or
1366 		 * SHN_COMMON.
1367 		 */
1368 		if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
1369 			continue;
1370 
1371 		/*
1372 		 * If the symbol is greater than the specified address, or if it
1373 		 * is further away from addr than the current nearest symbol,
1374 		 * then reject it.
1375 		 */
1376 		symbol_addr = obj->relocbase + def->st_value;
1377 		if (symbol_addr > addr || symbol_addr < info->dli_saddr)
1378 			continue;
1379 
1380 		/* Update our idea of the nearest symbol. */
1381 		info->dli_sname = obj->strtab + def->st_name;
1382 		info->dli_saddr = symbol_addr;
1383 		best_def = def;
1384 
1385 
1386 		/* Exact match? */
1387 		if (info->dli_saddr == addr)
1388 			break;
1389 	}
1390 
1391 #ifdef __HAVE_FUNCTION_DESCRIPTORS
1392 	if (best_def != NULL && ELF_ST_TYPE(best_def->st_info) == STT_FUNC)
1393 		info->dli_saddr = (void *)_rtld_function_descriptor_alloc(obj,
1394 		    best_def, 0);
1395 #else
1396 	__USE(best_def);
1397 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
1398 
1399 	lookup_mutex_exit();
1400 	return 1;
1401 }
1402 
__strong_alias(__dlinfo,dlinfo)1403 __strong_alias(__dlinfo,dlinfo)
1404 int
1405 dlinfo(void *handle, int req, void *v)
1406 {
1407 	const Obj_Entry *obj;
1408 	void *retaddr;
1409 
1410 	dbg(("dlinfo for %p %d", handle, req));
1411 
1412 	_rtld_shared_enter();
1413 
1414 	if (handle == RTLD_SELF) {
1415 #if defined(__powerpc__) && !defined(__clang__)
1416 		retaddr = hackish_return_address();
1417 #else
1418 		retaddr = __builtin_return_address(0);
1419 #endif
1420 		if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
1421 			_rtld_error("Cannot determine caller's shared object");
1422 			_rtld_shared_exit();
1423 			return -1;
1424 		}
1425 	} else {
1426 		if ((obj = _rtld_dlcheck(handle)) == NULL) {
1427 			_rtld_shared_exit();
1428 			return -1;
1429 		}
1430 	}
1431 
1432 	switch (req) {
1433 	case RTLD_DI_LINKMAP:
1434 		{
1435 		const struct link_map **map = v;
1436 
1437 		*map = &obj->linkmap;
1438 		break;
1439 		}
1440 
1441 	default:
1442 		_rtld_error("Invalid request");
1443 		_rtld_shared_exit();
1444 		return -1;
1445 	}
1446 
1447 	_rtld_shared_exit();
1448 	return 0;
1449 }
1450 
1451 static void
_rtld_fill_dl_phdr_info(const Obj_Entry * obj,struct dl_phdr_info * phdr_info)1452 _rtld_fill_dl_phdr_info(const Obj_Entry *obj, struct dl_phdr_info *phdr_info)
1453 {
1454 
1455 	phdr_info->dlpi_addr = (Elf_Addr)obj->relocbase;
1456 	/* XXX: wrong but not fixing it yet */
1457 	phdr_info->dlpi_name = obj->path;
1458 	phdr_info->dlpi_phdr = obj->phdr;
1459 	phdr_info->dlpi_phnum = obj->phsize / sizeof(obj->phdr[0]);
1460 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
1461 	phdr_info->dlpi_tls_modid = obj->tlsindex;
1462 	phdr_info->dlpi_tls_data = obj->tlsinit;
1463 #else
1464 	phdr_info->dlpi_tls_modid = 0;
1465 	phdr_info->dlpi_tls_data = 0;
1466 #endif
1467 	phdr_info->dlpi_adds = _rtld_objloads;
1468 	phdr_info->dlpi_subs = _rtld_objloads - _rtld_objcount;
1469 }
1470 
1471 __strong_alias(__dl_iterate_phdr,dl_iterate_phdr);
1472 int
dl_iterate_phdr(int (* callback)(struct dl_phdr_info *,size_t,void *),void * param)1473 dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *), void *param)
1474 {
1475 	struct dl_phdr_info phdr_info;
1476 	const Obj_Entry *obj;
1477 	int error = 0;
1478 
1479 	dbg(("dl_iterate_phdr"));
1480 
1481 	_rtld_shared_enter();
1482 
1483 	for (obj = _rtld_objlist;  obj != NULL;  obj = obj->next) {
1484 		_rtld_fill_dl_phdr_info(obj, &phdr_info);
1485 
1486 		/* XXXlocking: exit point */
1487 		error = callback(&phdr_info, sizeof(phdr_info), param);
1488 		if (error)
1489 			break;
1490 	}
1491 
1492 	if (error == 0) {
1493 		_rtld_fill_dl_phdr_info(&_rtld_objself, &phdr_info);
1494 
1495 		/* XXXlocking: exit point */
1496 		error = callback(&phdr_info, sizeof(phdr_info), param);
1497 	}
1498 
1499 	_rtld_shared_exit();
1500 	return error;
1501 }
1502 
1503 void
__dl_cxa_refcount(void * addr,ssize_t delta)1504 __dl_cxa_refcount(void *addr, ssize_t delta)
1505 {
1506 	sigset_t mask;
1507 	Obj_Entry *obj;
1508 
1509 	if (delta == 0)
1510 		return;
1511 
1512 	dbg(("__dl_cxa_refcount of %p with %zd", addr, delta));
1513 
1514 	_rtld_exclusive_enter(&mask);
1515 	obj = _rtld_obj_from_addr(addr);
1516 
1517 	if (obj == NULL) {
1518 		dbg(("__dl_cxa_refcont: address not found"));
1519 		_rtld_error("No shared object contains address");
1520 		_rtld_exclusive_exit(&mask);
1521 		return;
1522 	}
1523 	if (delta > 0 && obj->cxa_refcount > SIZE_MAX - delta)
1524 		_rtld_error("Reference count overflow");
1525 	else if (delta < 0 && obj->cxa_refcount < -1 + (size_t)-(delta + 1))
1526 		_rtld_error("Reference count underflow");
1527 	else {
1528 		if (obj->cxa_refcount == 0)
1529 			++obj->refcount;
1530 		obj->cxa_refcount += delta;
1531 		dbg(("new reference count: %zu", obj->cxa_refcount));
1532 		if (obj->cxa_refcount == 0) {
1533 			--obj->refcount;
1534 			if (obj->refcount == 0)
1535 				_rtld_unload_object(&mask, obj, true);
1536 		}
1537 	}
1538 
1539 	_rtld_exclusive_exit(&mask);
1540 }
1541 
1542 pid_t __fork(void);
1543 
1544 __dso_public pid_t
__locked_fork(int * my_errno)1545 __locked_fork(int *my_errno)
1546 {
1547 	pid_t result;
1548 
1549 	_rtld_shared_enter();
1550 	result = __fork();
1551 	if (result == -1)
1552 		*my_errno = errno;
1553 	_rtld_shared_exit();
1554 
1555 	return result;
1556 }
1557 
1558 /*
1559  * Error reporting function.  Use it like printf.  If formats the message
1560  * into a buffer, and sets things up so that the next call to dlerror()
1561  * will return the message.
1562  */
1563 void
_rtld_error(const char * fmt,...)1564 _rtld_error(const char *fmt,...)
1565 {
1566 	static char     buf[512];
1567 	va_list         ap;
1568 
1569 	va_start(ap, fmt);
1570 	xvsnprintf(buf, sizeof buf, fmt, ap);
1571 	dbg(("%s: %s", __func__, buf));
1572 	error_message = buf;
1573 	va_end(ap);
1574 }
1575 
1576 void
_rtld_debug_state(void)1577 _rtld_debug_state(void)
1578 {
1579 #if defined(__hppa__)
1580 	__asm volatile("nop" ::: "memory");
1581 #endif
1582 
1583 	/* Prevent optimizer from removing calls to this function */
1584 	__insn_barrier();
1585 }
1586 
1587 void
_rtld_linkmap_add(Obj_Entry * obj)1588 _rtld_linkmap_add(Obj_Entry *obj)
1589 {
1590 	struct link_map *l = &obj->linkmap;
1591 	struct link_map *prev;
1592 
1593 	obj->linkmap.l_name = obj->path;
1594 	obj->linkmap.l_addr = obj->relocbase;
1595 	obj->linkmap.l_ld = obj->dynamic;
1596 #ifdef __mips__
1597 	/* XXX This field is not standard and will be removed eventually. */
1598 	obj->linkmap.l_offs = obj->relocbase;
1599 #endif
1600 
1601 	if (_rtld_debug.r_map == NULL) {
1602 		_rtld_debug.r_map = l;
1603 		return;
1604 	}
1605 
1606 	/*
1607 	 * Scan to the end of the list, but not past the entry for the
1608 	 * dynamic linker, which we want to keep at the very end.
1609 	 */
1610 	for (prev = _rtld_debug.r_map;
1611 	    prev->l_next != NULL && prev->l_next != &_rtld_objself.linkmap;
1612 	    prev = prev->l_next);
1613 
1614 	l->l_prev = prev;
1615 	l->l_next = prev->l_next;
1616 	if (l->l_next != NULL)
1617 		l->l_next->l_prev = l;
1618 	prev->l_next = l;
1619 }
1620 
1621 void
_rtld_linkmap_delete(Obj_Entry * obj)1622 _rtld_linkmap_delete(Obj_Entry *obj)
1623 {
1624 	struct link_map *l = &obj->linkmap;
1625 
1626 	if (l->l_prev == NULL) {
1627 		if ((_rtld_debug.r_map = l->l_next) != NULL)
1628 			l->l_next->l_prev = NULL;
1629 		return;
1630 	}
1631 	if ((l->l_prev->l_next = l->l_next) != NULL)
1632 		l->l_next->l_prev = l->l_prev;
1633 }
1634 
1635 static Obj_Entry *
_rtld_obj_from_addr(const void * addr)1636 _rtld_obj_from_addr(const void *addr)
1637 {
1638 	Obj_Entry *obj;
1639 
1640 	for (obj = _rtld_objlist;  obj != NULL;  obj = obj->next) {
1641 		if (addr < (void *) obj->mapbase)
1642 			continue;
1643 		if (addr < (void *) (obj->mapbase + obj->mapsize))
1644 			return obj;
1645 	}
1646 	return NULL;
1647 }
1648 
1649 static void
_rtld_objlist_clear(Objlist * list)1650 _rtld_objlist_clear(Objlist *list)
1651 {
1652 	while (!SIMPLEQ_EMPTY(list)) {
1653 		Objlist_Entry* elm = SIMPLEQ_FIRST(list);
1654 		SIMPLEQ_REMOVE_HEAD(list, link);
1655 		xfree(elm);
1656 	}
1657 }
1658 
1659 static void
_rtld_objlist_remove(Objlist * list,Obj_Entry * obj)1660 _rtld_objlist_remove(Objlist *list, Obj_Entry *obj)
1661 {
1662 	Objlist_Entry *elm;
1663 
1664 	if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
1665 		SIMPLEQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
1666 		xfree(elm);
1667 	}
1668 }
1669 
1670 #define	RTLD_EXCLUSIVE_MASK	0x80000000U
1671 static volatile unsigned int _rtld_mutex;
1672 static volatile unsigned int _rtld_waiter_exclusive;
1673 static volatile unsigned int _rtld_waiter_shared;
1674 
1675 void
_rtld_shared_enter(void)1676 _rtld_shared_enter(void)
1677 {
1678 	unsigned int cur;
1679 	lwpid_t waiter, self = 0;
1680 
1681 	for (;;) {
1682 		cur = _rtld_mutex;
1683 		/*
1684 		 * First check if we are currently not exclusively locked.
1685 		 */
1686 		if ((cur & RTLD_EXCLUSIVE_MASK) == 0) {
1687 			/* Yes, so increment use counter */
1688 			if (atomic_cas_uint(&_rtld_mutex, cur, cur + 1) != cur)
1689 				continue;
1690 			membar_acquire();
1691 			return;
1692 		}
1693 		/*
1694 		 * Someone has an exclusive lock.  Puts us on the waiter list.
1695 		 */
1696 		if (!self)
1697 			self = _lwp_self();
1698 		if (cur == (self | RTLD_EXCLUSIVE_MASK)) {
1699 			if (_rtld_mutex_may_recurse)
1700 				return;
1701 			_rtld_error("%s: dead lock detected", __func__);
1702 			_rtld_die();
1703 		}
1704 		waiter = atomic_swap_uint(&_rtld_waiter_shared, self);
1705 		/*
1706 		 * Check for race against _rtld_exclusive_exit before sleeping.
1707 		 */
1708 		membar_sync();
1709 		if ((_rtld_mutex & RTLD_EXCLUSIVE_MASK) ||
1710 		    _rtld_waiter_exclusive)
1711 			_lwp_park(CLOCK_REALTIME, 0, NULL, 0,
1712 			    __UNVOLATILE(&_rtld_mutex), NULL);
1713 		/* Try to remove us from the waiter list. */
1714 		atomic_cas_uint(&_rtld_waiter_shared, self, 0);
1715 		if (waiter)
1716 			_lwp_unpark(waiter, __UNVOLATILE(&_rtld_mutex));
1717 	}
1718 }
1719 
1720 void
_rtld_shared_exit(void)1721 _rtld_shared_exit(void)
1722 {
1723 	lwpid_t waiter;
1724 
1725 	/*
1726 	 * Shared lock taken after an exclusive lock.
1727 	 * Just assume this is a partial recursion.
1728 	 */
1729 	if (_rtld_mutex & RTLD_EXCLUSIVE_MASK)
1730 		return;
1731 
1732 	/*
1733 	 * Wakeup LWPs waiting for an exclusive lock if this is the last
1734 	 * LWP on the shared lock.
1735 	 */
1736 	membar_release();
1737 	if (atomic_dec_uint_nv(&_rtld_mutex))
1738 		return;
1739 	membar_sync();
1740 	if ((waiter = _rtld_waiter_exclusive) != 0)
1741 		_lwp_unpark(waiter, __UNVOLATILE(&_rtld_mutex));
1742 }
1743 
1744 void
_rtld_exclusive_enter(sigset_t * mask)1745 _rtld_exclusive_enter(sigset_t *mask)
1746 {
1747 	lwpid_t waiter, self = _lwp_self();
1748 	unsigned int locked_value = (unsigned int)self | RTLD_EXCLUSIVE_MASK;
1749 	unsigned int cur;
1750 	sigset_t blockmask;
1751 
1752 	sigfillset(&blockmask);
1753 	sigdelset(&blockmask, SIGTRAP);	/* Allow the debugger */
1754 	sigprocmask(SIG_BLOCK, &blockmask, mask);
1755 
1756 	for (;;) {
1757 		if (atomic_cas_uint(&_rtld_mutex, 0, locked_value) == 0) {
1758 			membar_acquire();
1759 			break;
1760 		}
1761 		waiter = atomic_swap_uint(&_rtld_waiter_exclusive, self);
1762 		membar_sync();
1763 		cur = _rtld_mutex;
1764 		if (cur == locked_value) {
1765 			_rtld_error("%s: dead lock detected", __func__);
1766 			_rtld_die();
1767 		}
1768 		if (cur)
1769 			_lwp_park(CLOCK_REALTIME, 0, NULL, 0,
1770 			    __UNVOLATILE(&_rtld_mutex), NULL);
1771 		atomic_cas_uint(&_rtld_waiter_exclusive, self, 0);
1772 		if (waiter)
1773 			_lwp_unpark(waiter, __UNVOLATILE(&_rtld_mutex));
1774 	}
1775 }
1776 
1777 void
_rtld_exclusive_exit(sigset_t * mask)1778 _rtld_exclusive_exit(sigset_t *mask)
1779 {
1780 	lwpid_t waiter;
1781 
1782 	membar_release();
1783 	_rtld_mutex = 0;
1784 	membar_sync();
1785 	if ((waiter = _rtld_waiter_exclusive) != 0)
1786 		_lwp_unpark(waiter, __UNVOLATILE(&_rtld_mutex));
1787 
1788 	if ((waiter = _rtld_waiter_shared) != 0)
1789 		_lwp_unpark(waiter, __UNVOLATILE(&_rtld_mutex));
1790 
1791 	sigprocmask(SIG_SETMASK, mask, NULL);
1792 }
1793 
1794 int
_rtld_relro(const Obj_Entry * obj,bool wantmain)1795 _rtld_relro(const Obj_Entry *obj, bool wantmain)
1796 {
1797 #ifdef GNU_RELRO
1798 	/*
1799 	 * If our VM page size is larger than the page size used by the
1800 	 * linker when laying out the object, we could end up making data
1801 	 * read-only that is unintended.  Detect and avoid this situation.
1802 	 * It may mean we are unable to protect everything we'd like, but
1803 	 * it's better than crashing.
1804 	 */
1805 	uintptr_t relro_end = (uintptr_t)obj->relro_page + obj->relro_size;
1806 	uintptr_t relro_start = round_down((uintptr_t)obj->relro_page);
1807 	assert(relro_end >= relro_start);
1808 	size_t relro_size = round_down(relro_end) - relro_start;
1809 
1810 	if (relro_size == 0)
1811 		return 0;
1812 	if (wantmain != (obj ==_rtld_objmain))
1813 		return 0;
1814 
1815 	dbg(("RELRO %s %p %zx\n", obj->path, (void *)relro_start, relro_size));
1816 	if (mprotect((void *)relro_start, relro_size, PROT_READ) == -1) {
1817 		_rtld_error("%s: Cannot enforce relro " "protection: %s",
1818 		    obj->path, xstrerror(errno));
1819 		return -1;
1820 	}
1821 #endif
1822 	return 0;
1823 }
1824