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