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