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