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