1 /*- 2 * Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra. 3 * Copyright 2003 Alexander Kabaev <kan@FreeBSD.ORG>. 4 * Copyright 2009, 2010, 2011 Konstantin Belousov <kib@FreeBSD.ORG>. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 /* 31 * Dynamic linker for ELF. 32 * 33 * John Polstra <jdp@polstra.com>. 34 */ 35 36 #ifndef __GNUC__ 37 #error "GCC is needed to compile this file" 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/mount.h> 42 #include <sys/mman.h> 43 #include <sys/stat.h> 44 #include <sys/sysctl.h> 45 #include <sys/uio.h> 46 #include <sys/utsname.h> 47 #include <sys/ktrace.h> 48 49 #include <dlfcn.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <stdarg.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <unistd.h> 58 59 #include "debug.h" 60 #include "rtld.h" 61 #include "libmap.h" 62 #include "rtld_tls.h" 63 #include "rtld_printf.h" 64 65 #ifndef COMPAT_32BIT 66 #define PATH_RTLD "/libexec/ld-elf.so.1" 67 #else 68 #define PATH_RTLD "/libexec/ld-elf32.so.1" 69 #endif 70 71 /* Types. */ 72 typedef void (*func_ptr_type)(); 73 typedef void * (*path_enum_proc) (const char *path, size_t len, void *arg); 74 75 /* 76 * Function declarations. 77 */ 78 static const char *basename(const char *); 79 static void die(void) __dead2; 80 static void digest_dynamic1(Obj_Entry *, int, const Elf_Dyn **, 81 const Elf_Dyn **); 82 static void digest_dynamic2(Obj_Entry *, const Elf_Dyn *, const Elf_Dyn *); 83 static void digest_dynamic(Obj_Entry *, int); 84 static Obj_Entry *digest_phdr(const Elf_Phdr *, int, caddr_t, const char *); 85 static Obj_Entry *dlcheck(void *); 86 static Obj_Entry *dlopen_object(const char *name, int fd, Obj_Entry *refobj, 87 int lo_flags, int mode); 88 static Obj_Entry *do_load_object(int, const char *, char *, struct stat *, int); 89 static int do_search_info(const Obj_Entry *obj, int, struct dl_serinfo *); 90 static bool donelist_check(DoneList *, const Obj_Entry *); 91 static void errmsg_restore(char *); 92 static char *errmsg_save(void); 93 static void *fill_search_info(const char *, size_t, void *); 94 static char *find_library(const char *, const Obj_Entry *); 95 static const char *gethints(void); 96 static void init_dag(Obj_Entry *); 97 static void init_rtld(caddr_t, Elf_Auxinfo **); 98 static void initlist_add_neededs(Needed_Entry *, Objlist *); 99 static void initlist_add_objects(Obj_Entry *, Obj_Entry **, Objlist *); 100 static void linkmap_add(Obj_Entry *); 101 static void linkmap_delete(Obj_Entry *); 102 static void load_filtees(Obj_Entry *, int flags, RtldLockState *); 103 static void unload_filtees(Obj_Entry *); 104 static int load_needed_objects(Obj_Entry *, int); 105 static int load_preload_objects(void); 106 static Obj_Entry *load_object(const char *, int fd, const Obj_Entry *, int); 107 static void map_stacks_exec(RtldLockState *); 108 static Obj_Entry *obj_from_addr(const void *); 109 static void objlist_call_fini(Objlist *, Obj_Entry *, RtldLockState *); 110 static void objlist_call_init(Objlist *, RtldLockState *); 111 static void objlist_clear(Objlist *); 112 static Objlist_Entry *objlist_find(Objlist *, const Obj_Entry *); 113 static void objlist_init(Objlist *); 114 static void objlist_push_head(Objlist *, Obj_Entry *); 115 static void objlist_push_tail(Objlist *, Obj_Entry *); 116 static void objlist_remove(Objlist *, Obj_Entry *); 117 static void *path_enumerate(const char *, path_enum_proc, void *); 118 static int relocate_objects(Obj_Entry *, bool, Obj_Entry *, RtldLockState *); 119 static int resolve_objects_ifunc(Obj_Entry *first, bool bind_now, 120 RtldLockState *lockstate); 121 static int rtld_dirname(const char *, char *); 122 static int rtld_dirname_abs(const char *, char *); 123 static void *rtld_dlopen(const char *name, int fd, int mode); 124 static void rtld_exit(void); 125 static char *search_library_path(const char *, const char *); 126 static const void **get_program_var_addr(const char *, RtldLockState *); 127 static void set_program_var(const char *, const void *); 128 static int symlook_default(SymLook *, const Obj_Entry *refobj); 129 static int symlook_global(SymLook *, DoneList *); 130 static void symlook_init_from_req(SymLook *, const SymLook *); 131 static int symlook_list(SymLook *, const Objlist *, DoneList *); 132 static int symlook_needed(SymLook *, const Needed_Entry *, DoneList *); 133 static int symlook_obj1(SymLook *, const Obj_Entry *); 134 static void trace_loaded_objects(Obj_Entry *); 135 static void unlink_object(Obj_Entry *); 136 static void unload_object(Obj_Entry *); 137 static void unref_dag(Obj_Entry *); 138 static void ref_dag(Obj_Entry *); 139 static int origin_subst_one(char **, const char *, const char *, 140 const char *, char *); 141 static char *origin_subst(const char *, const char *); 142 static int rtld_verify_versions(const Objlist *); 143 static int rtld_verify_object_versions(Obj_Entry *); 144 static void object_add_name(Obj_Entry *, const char *); 145 static int object_match_name(const Obj_Entry *, const char *); 146 static void ld_utrace_log(int, void *, void *, size_t, int, const char *); 147 static void rtld_fill_dl_phdr_info(const Obj_Entry *obj, 148 struct dl_phdr_info *phdr_info); 149 150 void r_debug_state(struct r_debug *, struct link_map *) __noinline; 151 152 /* 153 * Data declarations. 154 */ 155 static char *error_message; /* Message for dlerror(), or NULL */ 156 struct r_debug r_debug; /* for GDB; */ 157 static bool libmap_disable; /* Disable libmap */ 158 static bool ld_loadfltr; /* Immediate filters processing */ 159 static char *libmap_override; /* Maps to use in addition to libmap.conf */ 160 static bool trust; /* False for setuid and setgid programs */ 161 static bool dangerous_ld_env; /* True if environment variables have been 162 used to affect the libraries loaded */ 163 static char *ld_bind_now; /* Environment variable for immediate binding */ 164 static char *ld_debug; /* Environment variable for debugging */ 165 static char *ld_library_path; /* Environment variable for search path */ 166 static char *ld_preload; /* Environment variable for libraries to 167 load first */ 168 static char *ld_elf_hints_path; /* Environment variable for alternative hints path */ 169 static char *ld_tracing; /* Called from ldd to print libs */ 170 static char *ld_utrace; /* Use utrace() to log events. */ 171 static Obj_Entry *obj_list; /* Head of linked list of shared objects */ 172 static Obj_Entry **obj_tail; /* Link field of last object in list */ 173 static Obj_Entry *obj_main; /* The main program shared object */ 174 static Obj_Entry obj_rtld; /* The dynamic linker shared object */ 175 static unsigned int obj_count; /* Number of objects in obj_list */ 176 static unsigned int obj_loads; /* Number of objects in obj_list */ 177 178 static Objlist list_global = /* Objects dlopened with RTLD_GLOBAL */ 179 STAILQ_HEAD_INITIALIZER(list_global); 180 static Objlist list_main = /* Objects loaded at program startup */ 181 STAILQ_HEAD_INITIALIZER(list_main); 182 static Objlist list_fini = /* Objects needing fini() calls */ 183 STAILQ_HEAD_INITIALIZER(list_fini); 184 185 Elf_Sym sym_zero; /* For resolving undefined weak refs. */ 186 187 #define GDB_STATE(s,m) r_debug.r_state = s; r_debug_state(&r_debug,m); 188 189 extern Elf_Dyn _DYNAMIC; 190 #pragma weak _DYNAMIC 191 #ifndef RTLD_IS_DYNAMIC 192 #define RTLD_IS_DYNAMIC() (&_DYNAMIC != NULL) 193 #endif 194 195 int osreldate, pagesize; 196 197 static int stack_prot = PROT_READ | PROT_WRITE | RTLD_DEFAULT_STACK_EXEC; 198 static int max_stack_flags; 199 200 /* 201 * Global declarations normally provided by crt1. The dynamic linker is 202 * not built with crt1, so we have to provide them ourselves. 203 */ 204 char *__progname; 205 char **environ; 206 207 /* 208 * Globals to control TLS allocation. 209 */ 210 size_t tls_last_offset; /* Static TLS offset of last module */ 211 size_t tls_last_size; /* Static TLS size of last module */ 212 size_t tls_static_space; /* Static TLS space allocated */ 213 int tls_dtv_generation = 1; /* Used to detect when dtv size changes */ 214 int tls_max_index = 1; /* Largest module index allocated */ 215 216 /* 217 * Fill in a DoneList with an allocation large enough to hold all of 218 * the currently-loaded objects. Keep this as a macro since it calls 219 * alloca and we want that to occur within the scope of the caller. 220 */ 221 #define donelist_init(dlp) \ 222 ((dlp)->objs = alloca(obj_count * sizeof (dlp)->objs[0]), \ 223 assert((dlp)->objs != NULL), \ 224 (dlp)->num_alloc = obj_count, \ 225 (dlp)->num_used = 0) 226 227 #define UTRACE_DLOPEN_START 1 228 #define UTRACE_DLOPEN_STOP 2 229 #define UTRACE_DLCLOSE_START 3 230 #define UTRACE_DLCLOSE_STOP 4 231 #define UTRACE_LOAD_OBJECT 5 232 #define UTRACE_UNLOAD_OBJECT 6 233 #define UTRACE_ADD_RUNDEP 7 234 #define UTRACE_PRELOAD_FINISHED 8 235 #define UTRACE_INIT_CALL 9 236 #define UTRACE_FINI_CALL 10 237 238 struct utrace_rtld { 239 char sig[4]; /* 'RTLD' */ 240 int event; 241 void *handle; 242 void *mapbase; /* Used for 'parent' and 'init/fini' */ 243 size_t mapsize; 244 int refcnt; /* Used for 'mode' */ 245 char name[MAXPATHLEN]; 246 }; 247 248 #define LD_UTRACE(e, h, mb, ms, r, n) do { \ 249 if (ld_utrace != NULL) \ 250 ld_utrace_log(e, h, mb, ms, r, n); \ 251 } while (0) 252 253 static void 254 ld_utrace_log(int event, void *handle, void *mapbase, size_t mapsize, 255 int refcnt, const char *name) 256 { 257 struct utrace_rtld ut; 258 259 ut.sig[0] = 'R'; 260 ut.sig[1] = 'T'; 261 ut.sig[2] = 'L'; 262 ut.sig[3] = 'D'; 263 ut.event = event; 264 ut.handle = handle; 265 ut.mapbase = mapbase; 266 ut.mapsize = mapsize; 267 ut.refcnt = refcnt; 268 bzero(ut.name, sizeof(ut.name)); 269 if (name) 270 strlcpy(ut.name, name, sizeof(ut.name)); 271 utrace(&ut, sizeof(ut)); 272 } 273 274 /* 275 * Main entry point for dynamic linking. The first argument is the 276 * stack pointer. The stack is expected to be laid out as described 277 * in the SVR4 ABI specification, Intel 386 Processor Supplement. 278 * Specifically, the stack pointer points to a word containing 279 * ARGC. Following that in the stack is a null-terminated sequence 280 * of pointers to argument strings. Then comes a null-terminated 281 * sequence of pointers to environment strings. Finally, there is a 282 * sequence of "auxiliary vector" entries. 283 * 284 * The second argument points to a place to store the dynamic linker's 285 * exit procedure pointer and the third to a place to store the main 286 * program's object. 287 * 288 * The return value is the main program's entry point. 289 */ 290 func_ptr_type 291 _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) 292 { 293 Elf_Auxinfo *aux_info[AT_COUNT]; 294 int i; 295 int argc; 296 char **argv; 297 char **env; 298 Elf_Auxinfo *aux; 299 Elf_Auxinfo *auxp; 300 const char *argv0; 301 Objlist_Entry *entry; 302 Obj_Entry *obj; 303 Obj_Entry **preload_tail; 304 Objlist initlist; 305 RtldLockState lockstate; 306 307 /* 308 * On entry, the dynamic linker itself has not been relocated yet. 309 * Be very careful not to reference any global data until after 310 * init_rtld has returned. It is OK to reference file-scope statics 311 * and string constants, and to call static and global functions. 312 */ 313 314 /* Find the auxiliary vector on the stack. */ 315 argc = *sp++; 316 argv = (char **) sp; 317 sp += argc + 1; /* Skip over arguments and NULL terminator */ 318 env = (char **) sp; 319 while (*sp++ != 0) /* Skip over environment, and NULL terminator */ 320 ; 321 aux = (Elf_Auxinfo *) sp; 322 323 /* Digest the auxiliary vector. */ 324 for (i = 0; i < AT_COUNT; i++) 325 aux_info[i] = NULL; 326 for (auxp = aux; auxp->a_type != AT_NULL; auxp++) { 327 if (auxp->a_type < AT_COUNT) 328 aux_info[auxp->a_type] = auxp; 329 } 330 331 /* Initialize and relocate ourselves. */ 332 assert(aux_info[AT_BASE] != NULL); 333 init_rtld((caddr_t) aux_info[AT_BASE]->a_un.a_ptr, aux_info); 334 335 __progname = obj_rtld.path; 336 argv0 = argv[0] != NULL ? argv[0] : "(null)"; 337 environ = env; 338 339 trust = !issetugid(); 340 341 ld_bind_now = getenv(LD_ "BIND_NOW"); 342 /* 343 * If the process is tainted, then we un-set the dangerous environment 344 * variables. The process will be marked as tainted until setuid(2) 345 * is called. If any child process calls setuid(2) we do not want any 346 * future processes to honor the potentially un-safe variables. 347 */ 348 if (!trust) { 349 if (unsetenv(LD_ "PRELOAD") || unsetenv(LD_ "LIBMAP") || 350 unsetenv(LD_ "LIBRARY_PATH") || unsetenv(LD_ "LIBMAP_DISABLE") || 351 unsetenv(LD_ "DEBUG") || unsetenv(LD_ "ELF_HINTS_PATH") || 352 unsetenv(LD_ "LOADFLTR")) { 353 _rtld_error("environment corrupt; aborting"); 354 die(); 355 } 356 } 357 ld_debug = getenv(LD_ "DEBUG"); 358 libmap_disable = getenv(LD_ "LIBMAP_DISABLE") != NULL; 359 libmap_override = getenv(LD_ "LIBMAP"); 360 ld_library_path = getenv(LD_ "LIBRARY_PATH"); 361 ld_preload = getenv(LD_ "PRELOAD"); 362 ld_elf_hints_path = getenv(LD_ "ELF_HINTS_PATH"); 363 ld_loadfltr = getenv(LD_ "LOADFLTR") != NULL; 364 dangerous_ld_env = libmap_disable || (libmap_override != NULL) || 365 (ld_library_path != NULL) || (ld_preload != NULL) || 366 (ld_elf_hints_path != NULL) || ld_loadfltr; 367 ld_tracing = getenv(LD_ "TRACE_LOADED_OBJECTS"); 368 ld_utrace = getenv(LD_ "UTRACE"); 369 370 if ((ld_elf_hints_path == NULL) || strlen(ld_elf_hints_path) == 0) 371 ld_elf_hints_path = _PATH_ELF_HINTS; 372 373 if (ld_debug != NULL && *ld_debug != '\0') 374 debug = 1; 375 dbg("%s is initialized, base address = %p", __progname, 376 (caddr_t) aux_info[AT_BASE]->a_un.a_ptr); 377 dbg("RTLD dynamic = %p", obj_rtld.dynamic); 378 dbg("RTLD pltgot = %p", obj_rtld.pltgot); 379 380 dbg("initializing thread locks"); 381 lockdflt_init(); 382 383 /* 384 * Load the main program, or process its program header if it is 385 * already loaded. 386 */ 387 if (aux_info[AT_EXECFD] != NULL) { /* Load the main program. */ 388 int fd = aux_info[AT_EXECFD]->a_un.a_val; 389 dbg("loading main program"); 390 obj_main = map_object(fd, argv0, NULL); 391 close(fd); 392 if (obj_main == NULL) 393 die(); 394 max_stack_flags = obj->stack_flags; 395 } else { /* Main program already loaded. */ 396 const Elf_Phdr *phdr; 397 int phnum; 398 caddr_t entry; 399 400 dbg("processing main program's program header"); 401 assert(aux_info[AT_PHDR] != NULL); 402 phdr = (const Elf_Phdr *) aux_info[AT_PHDR]->a_un.a_ptr; 403 assert(aux_info[AT_PHNUM] != NULL); 404 phnum = aux_info[AT_PHNUM]->a_un.a_val; 405 assert(aux_info[AT_PHENT] != NULL); 406 assert(aux_info[AT_PHENT]->a_un.a_val == sizeof(Elf_Phdr)); 407 assert(aux_info[AT_ENTRY] != NULL); 408 entry = (caddr_t) aux_info[AT_ENTRY]->a_un.a_ptr; 409 if ((obj_main = digest_phdr(phdr, phnum, entry, argv0)) == NULL) 410 die(); 411 } 412 413 if (aux_info[AT_EXECPATH] != 0) { 414 char *kexecpath; 415 char buf[MAXPATHLEN]; 416 417 kexecpath = aux_info[AT_EXECPATH]->a_un.a_ptr; 418 dbg("AT_EXECPATH %p %s", kexecpath, kexecpath); 419 if (kexecpath[0] == '/') 420 obj_main->path = kexecpath; 421 else if (getcwd(buf, sizeof(buf)) == NULL || 422 strlcat(buf, "/", sizeof(buf)) >= sizeof(buf) || 423 strlcat(buf, kexecpath, sizeof(buf)) >= sizeof(buf)) 424 obj_main->path = xstrdup(argv0); 425 else 426 obj_main->path = xstrdup(buf); 427 } else { 428 dbg("No AT_EXECPATH"); 429 obj_main->path = xstrdup(argv0); 430 } 431 dbg("obj_main path %s", obj_main->path); 432 obj_main->mainprog = true; 433 434 if (aux_info[AT_STACKPROT] != NULL && 435 aux_info[AT_STACKPROT]->a_un.a_val != 0) 436 stack_prot = aux_info[AT_STACKPROT]->a_un.a_val; 437 438 /* 439 * Get the actual dynamic linker pathname from the executable if 440 * possible. (It should always be possible.) That ensures that 441 * gdb will find the right dynamic linker even if a non-standard 442 * one is being used. 443 */ 444 if (obj_main->interp != NULL && 445 strcmp(obj_main->interp, obj_rtld.path) != 0) { 446 free(obj_rtld.path); 447 obj_rtld.path = xstrdup(obj_main->interp); 448 __progname = obj_rtld.path; 449 } 450 451 digest_dynamic(obj_main, 0); 452 453 linkmap_add(obj_main); 454 linkmap_add(&obj_rtld); 455 456 /* Link the main program into the list of objects. */ 457 *obj_tail = obj_main; 458 obj_tail = &obj_main->next; 459 obj_count++; 460 obj_loads++; 461 /* Make sure we don't call the main program's init and fini functions. */ 462 obj_main->init = obj_main->fini = (Elf_Addr)NULL; 463 464 /* Initialize a fake symbol for resolving undefined weak references. */ 465 sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE); 466 sym_zero.st_shndx = SHN_UNDEF; 467 sym_zero.st_value = -(uintptr_t)obj_main->relocbase; 468 469 if (!libmap_disable) 470 libmap_disable = (bool)lm_init(libmap_override); 471 472 dbg("loading LD_PRELOAD libraries"); 473 if (load_preload_objects() == -1) 474 die(); 475 preload_tail = obj_tail; 476 477 dbg("loading needed objects"); 478 if (load_needed_objects(obj_main, 0) == -1) 479 die(); 480 481 /* Make a list of all objects loaded at startup. */ 482 for (obj = obj_list; obj != NULL; obj = obj->next) { 483 objlist_push_tail(&list_main, obj); 484 obj->refcount++; 485 } 486 487 dbg("checking for required versions"); 488 if (rtld_verify_versions(&list_main) == -1 && !ld_tracing) 489 die(); 490 491 if (ld_tracing) { /* We're done */ 492 trace_loaded_objects(obj_main); 493 exit(0); 494 } 495 496 if (getenv(LD_ "DUMP_REL_PRE") != NULL) { 497 dump_relocations(obj_main); 498 exit (0); 499 } 500 501 /* 502 * Processing tls relocations requires having the tls offsets 503 * initialized. Prepare offsets before starting initial 504 * relocation processing. 505 */ 506 dbg("initializing initial thread local storage offsets"); 507 STAILQ_FOREACH(entry, &list_main, link) { 508 /* 509 * Allocate all the initial objects out of the static TLS 510 * block even if they didn't ask for it. 511 */ 512 allocate_tls_offset(entry->obj); 513 } 514 515 if (relocate_objects(obj_main, 516 ld_bind_now != NULL && *ld_bind_now != '\0', &obj_rtld, NULL) == -1) 517 die(); 518 519 dbg("doing copy relocations"); 520 if (do_copy_relocations(obj_main) == -1) 521 die(); 522 523 if (getenv(LD_ "DUMP_REL_POST") != NULL) { 524 dump_relocations(obj_main); 525 exit (0); 526 } 527 528 /* 529 * Setup TLS for main thread. This must be done after the 530 * relocations are processed, since tls initialization section 531 * might be the subject for relocations. 532 */ 533 dbg("initializing initial thread local storage"); 534 allocate_initial_tls(obj_list); 535 536 dbg("initializing key program variables"); 537 set_program_var("__progname", argv[0] != NULL ? basename(argv[0]) : ""); 538 set_program_var("environ", env); 539 set_program_var("__elf_aux_vector", aux); 540 541 /* Make a list of init functions to call. */ 542 objlist_init(&initlist); 543 initlist_add_objects(obj_list, preload_tail, &initlist); 544 545 r_debug_state(NULL, &obj_main->linkmap); /* say hello to gdb! */ 546 547 map_stacks_exec(NULL); 548 549 dbg("resolving ifuncs"); 550 if (resolve_objects_ifunc(obj_main, 551 ld_bind_now != NULL && *ld_bind_now != '\0', NULL) == -1) 552 die(); 553 554 wlock_acquire(rtld_bind_lock, &lockstate); 555 objlist_call_init(&initlist, &lockstate); 556 objlist_clear(&initlist); 557 dbg("loading filtees"); 558 for (obj = obj_list->next; obj != NULL; obj = obj->next) { 559 if (ld_loadfltr || obj->z_loadfltr) 560 load_filtees(obj, 0, &lockstate); 561 } 562 lock_release(rtld_bind_lock, &lockstate); 563 564 dbg("transferring control to program entry point = %p", obj_main->entry); 565 566 /* Return the exit procedure and the program entry point. */ 567 *exit_proc = rtld_exit; 568 *objp = obj_main; 569 return (func_ptr_type) obj_main->entry; 570 } 571 572 void * 573 rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def) 574 { 575 void *ptr; 576 Elf_Addr target; 577 578 ptr = (void *)make_function_pointer(def, obj); 579 target = ((Elf_Addr (*)(void))ptr)(); 580 return ((void *)target); 581 } 582 583 Elf_Addr 584 _rtld_bind(Obj_Entry *obj, Elf_Size reloff) 585 { 586 const Elf_Rel *rel; 587 const Elf_Sym *def; 588 const Obj_Entry *defobj; 589 Elf_Addr *where; 590 Elf_Addr target; 591 RtldLockState lockstate; 592 593 rlock_acquire(rtld_bind_lock, &lockstate); 594 if (sigsetjmp(lockstate.env, 0) != 0) 595 lock_upgrade(rtld_bind_lock, &lockstate); 596 if (obj->pltrel) 597 rel = (const Elf_Rel *) ((caddr_t) obj->pltrel + reloff); 598 else 599 rel = (const Elf_Rel *) ((caddr_t) obj->pltrela + reloff); 600 601 where = (Elf_Addr *) (obj->relocbase + rel->r_offset); 602 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL, 603 &lockstate); 604 if (def == NULL) 605 die(); 606 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) 607 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def); 608 else 609 target = (Elf_Addr)(defobj->relocbase + def->st_value); 610 611 dbg("\"%s\" in \"%s\" ==> %p in \"%s\"", 612 defobj->strtab + def->st_name, basename(obj->path), 613 (void *)target, basename(defobj->path)); 614 615 /* 616 * Write the new contents for the jmpslot. Note that depending on 617 * architecture, the value which we need to return back to the 618 * lazy binding trampoline may or may not be the target 619 * address. The value returned from reloc_jmpslot() is the value 620 * that the trampoline needs. 621 */ 622 target = reloc_jmpslot(where, target, defobj, obj, rel); 623 lock_release(rtld_bind_lock, &lockstate); 624 return target; 625 } 626 627 /* 628 * Error reporting function. Use it like printf. If formats the message 629 * into a buffer, and sets things up so that the next call to dlerror() 630 * will return the message. 631 */ 632 void 633 _rtld_error(const char *fmt, ...) 634 { 635 static char buf[512]; 636 va_list ap; 637 638 va_start(ap, fmt); 639 rtld_vsnprintf(buf, sizeof buf, fmt, ap); 640 error_message = buf; 641 va_end(ap); 642 } 643 644 /* 645 * Return a dynamically-allocated copy of the current error message, if any. 646 */ 647 static char * 648 errmsg_save(void) 649 { 650 return error_message == NULL ? NULL : xstrdup(error_message); 651 } 652 653 /* 654 * Restore the current error message from a copy which was previously saved 655 * by errmsg_save(). The copy is freed. 656 */ 657 static void 658 errmsg_restore(char *saved_msg) 659 { 660 if (saved_msg == NULL) 661 error_message = NULL; 662 else { 663 _rtld_error("%s", saved_msg); 664 free(saved_msg); 665 } 666 } 667 668 static const char * 669 basename(const char *name) 670 { 671 const char *p = strrchr(name, '/'); 672 return p != NULL ? p + 1 : name; 673 } 674 675 static struct utsname uts; 676 677 static int 678 origin_subst_one(char **res, const char *real, const char *kw, const char *subst, 679 char *may_free) 680 { 681 const char *p, *p1; 682 char *res1; 683 int subst_len; 684 int kw_len; 685 686 res1 = *res = NULL; 687 p = real; 688 subst_len = kw_len = 0; 689 for (;;) { 690 p1 = strstr(p, kw); 691 if (p1 != NULL) { 692 if (subst_len == 0) { 693 subst_len = strlen(subst); 694 kw_len = strlen(kw); 695 } 696 if (*res == NULL) { 697 *res = xmalloc(PATH_MAX); 698 res1 = *res; 699 } 700 if ((res1 - *res) + subst_len + (p1 - p) >= PATH_MAX) { 701 _rtld_error("Substitution of %s in %s cannot be performed", 702 kw, real); 703 if (may_free != NULL) 704 free(may_free); 705 free(res); 706 return (false); 707 } 708 memcpy(res1, p, p1 - p); 709 res1 += p1 - p; 710 memcpy(res1, subst, subst_len); 711 res1 += subst_len; 712 p = p1 + kw_len; 713 } else { 714 if (*res == NULL) { 715 if (may_free != NULL) 716 *res = may_free; 717 else 718 *res = xstrdup(real); 719 return (true); 720 } 721 *res1 = '\0'; 722 if (may_free != NULL) 723 free(may_free); 724 if (strlcat(res1, p, PATH_MAX - (res1 - *res)) >= PATH_MAX) { 725 free(res); 726 return (false); 727 } 728 return (true); 729 } 730 } 731 } 732 733 static char * 734 origin_subst(const char *real, const char *origin_path) 735 { 736 char *res1, *res2, *res3, *res4; 737 738 if (uts.sysname[0] == '\0') { 739 if (uname(&uts) != 0) { 740 _rtld_error("utsname failed: %d", errno); 741 return (NULL); 742 } 743 } 744 if (!origin_subst_one(&res1, real, "$ORIGIN", origin_path, NULL) || 745 !origin_subst_one(&res2, res1, "$OSNAME", uts.sysname, res1) || 746 !origin_subst_one(&res3, res2, "$OSREL", uts.release, res2) || 747 !origin_subst_one(&res4, res3, "$PLATFORM", uts.machine, res3)) 748 return (NULL); 749 return (res4); 750 } 751 752 static void 753 die(void) 754 { 755 const char *msg = dlerror(); 756 757 if (msg == NULL) 758 msg = "Fatal error"; 759 rtld_fdputstr(STDERR_FILENO, msg); 760 rtld_fdputchar(STDERR_FILENO, '\n'); 761 _exit(1); 762 } 763 764 /* 765 * Process a shared object's DYNAMIC section, and save the important 766 * information in its Obj_Entry structure. 767 */ 768 static void 769 digest_dynamic1(Obj_Entry *obj, int early, const Elf_Dyn **dyn_rpath, 770 const Elf_Dyn **dyn_soname) 771 { 772 const Elf_Dyn *dynp; 773 Needed_Entry **needed_tail = &obj->needed; 774 Needed_Entry **needed_filtees_tail = &obj->needed_filtees; 775 Needed_Entry **needed_aux_filtees_tail = &obj->needed_aux_filtees; 776 int plttype = DT_REL; 777 778 *dyn_rpath = NULL; 779 *dyn_soname = NULL; 780 781 obj->bind_now = false; 782 for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; dynp++) { 783 switch (dynp->d_tag) { 784 785 case DT_REL: 786 obj->rel = (const Elf_Rel *) (obj->relocbase + dynp->d_un.d_ptr); 787 break; 788 789 case DT_RELSZ: 790 obj->relsize = dynp->d_un.d_val; 791 break; 792 793 case DT_RELENT: 794 assert(dynp->d_un.d_val == sizeof(Elf_Rel)); 795 break; 796 797 case DT_JMPREL: 798 obj->pltrel = (const Elf_Rel *) 799 (obj->relocbase + dynp->d_un.d_ptr); 800 break; 801 802 case DT_PLTRELSZ: 803 obj->pltrelsize = dynp->d_un.d_val; 804 break; 805 806 case DT_RELA: 807 obj->rela = (const Elf_Rela *) (obj->relocbase + dynp->d_un.d_ptr); 808 break; 809 810 case DT_RELASZ: 811 obj->relasize = dynp->d_un.d_val; 812 break; 813 814 case DT_RELAENT: 815 assert(dynp->d_un.d_val == sizeof(Elf_Rela)); 816 break; 817 818 case DT_PLTREL: 819 plttype = dynp->d_un.d_val; 820 assert(dynp->d_un.d_val == DT_REL || plttype == DT_RELA); 821 break; 822 823 case DT_SYMTAB: 824 obj->symtab = (const Elf_Sym *) 825 (obj->relocbase + dynp->d_un.d_ptr); 826 break; 827 828 case DT_SYMENT: 829 assert(dynp->d_un.d_val == sizeof(Elf_Sym)); 830 break; 831 832 case DT_STRTAB: 833 obj->strtab = (const char *) (obj->relocbase + dynp->d_un.d_ptr); 834 break; 835 836 case DT_STRSZ: 837 obj->strsize = dynp->d_un.d_val; 838 break; 839 840 case DT_VERNEED: 841 obj->verneed = (const Elf_Verneed *) (obj->relocbase + 842 dynp->d_un.d_val); 843 break; 844 845 case DT_VERNEEDNUM: 846 obj->verneednum = dynp->d_un.d_val; 847 break; 848 849 case DT_VERDEF: 850 obj->verdef = (const Elf_Verdef *) (obj->relocbase + 851 dynp->d_un.d_val); 852 break; 853 854 case DT_VERDEFNUM: 855 obj->verdefnum = dynp->d_un.d_val; 856 break; 857 858 case DT_VERSYM: 859 obj->versyms = (const Elf_Versym *)(obj->relocbase + 860 dynp->d_un.d_val); 861 break; 862 863 case DT_HASH: 864 { 865 const Elf_Hashelt *hashtab = (const Elf_Hashelt *) 866 (obj->relocbase + dynp->d_un.d_ptr); 867 obj->nbuckets = hashtab[0]; 868 obj->nchains = hashtab[1]; 869 obj->buckets = hashtab + 2; 870 obj->chains = obj->buckets + obj->nbuckets; 871 } 872 break; 873 874 case DT_NEEDED: 875 if (!obj->rtld) { 876 Needed_Entry *nep = NEW(Needed_Entry); 877 nep->name = dynp->d_un.d_val; 878 nep->obj = NULL; 879 nep->next = NULL; 880 881 *needed_tail = nep; 882 needed_tail = &nep->next; 883 } 884 break; 885 886 case DT_FILTER: 887 if (!obj->rtld) { 888 Needed_Entry *nep = NEW(Needed_Entry); 889 nep->name = dynp->d_un.d_val; 890 nep->obj = NULL; 891 nep->next = NULL; 892 893 *needed_filtees_tail = nep; 894 needed_filtees_tail = &nep->next; 895 } 896 break; 897 898 case DT_AUXILIARY: 899 if (!obj->rtld) { 900 Needed_Entry *nep = NEW(Needed_Entry); 901 nep->name = dynp->d_un.d_val; 902 nep->obj = NULL; 903 nep->next = NULL; 904 905 *needed_aux_filtees_tail = nep; 906 needed_aux_filtees_tail = &nep->next; 907 } 908 break; 909 910 case DT_PLTGOT: 911 obj->pltgot = (Elf_Addr *) (obj->relocbase + dynp->d_un.d_ptr); 912 break; 913 914 case DT_TEXTREL: 915 obj->textrel = true; 916 break; 917 918 case DT_SYMBOLIC: 919 obj->symbolic = true; 920 break; 921 922 case DT_RPATH: 923 case DT_RUNPATH: /* XXX: process separately */ 924 /* 925 * We have to wait until later to process this, because we 926 * might not have gotten the address of the string table yet. 927 */ 928 *dyn_rpath = dynp; 929 break; 930 931 case DT_SONAME: 932 *dyn_soname = dynp; 933 break; 934 935 case DT_INIT: 936 obj->init = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr); 937 break; 938 939 case DT_FINI: 940 obj->fini = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr); 941 break; 942 943 /* 944 * Don't process DT_DEBUG on MIPS as the dynamic section 945 * is mapped read-only. DT_MIPS_RLD_MAP is used instead. 946 */ 947 948 #ifndef __mips__ 949 case DT_DEBUG: 950 /* XXX - not implemented yet */ 951 if (!early) 952 dbg("Filling in DT_DEBUG entry"); 953 ((Elf_Dyn*)dynp)->d_un.d_ptr = (Elf_Addr) &r_debug; 954 break; 955 #endif 956 957 case DT_FLAGS: 958 if ((dynp->d_un.d_val & DF_ORIGIN) && trust) 959 obj->z_origin = true; 960 if (dynp->d_un.d_val & DF_SYMBOLIC) 961 obj->symbolic = true; 962 if (dynp->d_un.d_val & DF_TEXTREL) 963 obj->textrel = true; 964 if (dynp->d_un.d_val & DF_BIND_NOW) 965 obj->bind_now = true; 966 /*if (dynp->d_un.d_val & DF_STATIC_TLS) 967 ;*/ 968 break; 969 #ifdef __mips__ 970 case DT_MIPS_LOCAL_GOTNO: 971 obj->local_gotno = dynp->d_un.d_val; 972 break; 973 974 case DT_MIPS_SYMTABNO: 975 obj->symtabno = dynp->d_un.d_val; 976 break; 977 978 case DT_MIPS_GOTSYM: 979 obj->gotsym = dynp->d_un.d_val; 980 break; 981 982 case DT_MIPS_RLD_MAP: 983 #ifdef notyet 984 if (!early) 985 dbg("Filling in DT_DEBUG entry"); 986 ((Elf_Dyn*)dynp)->d_un.d_ptr = (Elf_Addr) &r_debug; 987 #endif 988 break; 989 #endif 990 991 case DT_FLAGS_1: 992 if (dynp->d_un.d_val & DF_1_NOOPEN) 993 obj->z_noopen = true; 994 if ((dynp->d_un.d_val & DF_1_ORIGIN) && trust) 995 obj->z_origin = true; 996 /*if (dynp->d_un.d_val & DF_1_GLOBAL) 997 XXX ;*/ 998 if (dynp->d_un.d_val & DF_1_BIND_NOW) 999 obj->bind_now = true; 1000 if (dynp->d_un.d_val & DF_1_NODELETE) 1001 obj->z_nodelete = true; 1002 if (dynp->d_un.d_val & DF_1_LOADFLTR) 1003 obj->z_loadfltr = true; 1004 break; 1005 1006 default: 1007 if (!early) { 1008 dbg("Ignoring d_tag %ld = %#lx", (long)dynp->d_tag, 1009 (long)dynp->d_tag); 1010 } 1011 break; 1012 } 1013 } 1014 1015 obj->traced = false; 1016 1017 if (plttype == DT_RELA) { 1018 obj->pltrela = (const Elf_Rela *) obj->pltrel; 1019 obj->pltrel = NULL; 1020 obj->pltrelasize = obj->pltrelsize; 1021 obj->pltrelsize = 0; 1022 } 1023 } 1024 1025 static void 1026 digest_dynamic2(Obj_Entry *obj, const Elf_Dyn *dyn_rpath, 1027 const Elf_Dyn *dyn_soname) 1028 { 1029 1030 if (obj->z_origin && obj->origin_path == NULL) { 1031 obj->origin_path = xmalloc(PATH_MAX); 1032 if (rtld_dirname_abs(obj->path, obj->origin_path) == -1) 1033 die(); 1034 } 1035 1036 if (dyn_rpath != NULL) { 1037 obj->rpath = (char *)obj->strtab + dyn_rpath->d_un.d_val; 1038 if (obj->z_origin) 1039 obj->rpath = origin_subst(obj->rpath, obj->origin_path); 1040 } 1041 1042 if (dyn_soname != NULL) 1043 object_add_name(obj, obj->strtab + dyn_soname->d_un.d_val); 1044 } 1045 1046 static void 1047 digest_dynamic(Obj_Entry *obj, int early) 1048 { 1049 const Elf_Dyn *dyn_rpath; 1050 const Elf_Dyn *dyn_soname; 1051 1052 digest_dynamic1(obj, early, &dyn_rpath, &dyn_soname); 1053 digest_dynamic2(obj, dyn_rpath, dyn_soname); 1054 } 1055 1056 /* 1057 * Process a shared object's program header. This is used only for the 1058 * main program, when the kernel has already loaded the main program 1059 * into memory before calling the dynamic linker. It creates and 1060 * returns an Obj_Entry structure. 1061 */ 1062 static Obj_Entry * 1063 digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry, const char *path) 1064 { 1065 Obj_Entry *obj; 1066 const Elf_Phdr *phlimit = phdr + phnum; 1067 const Elf_Phdr *ph; 1068 int nsegs = 0; 1069 1070 obj = obj_new(); 1071 for (ph = phdr; ph < phlimit; ph++) { 1072 if (ph->p_type != PT_PHDR) 1073 continue; 1074 1075 obj->phdr = phdr; 1076 obj->phsize = ph->p_memsz; 1077 obj->relocbase = (caddr_t)phdr - ph->p_vaddr; 1078 break; 1079 } 1080 1081 obj->stack_flags = PF_X | PF_R | PF_W; 1082 1083 for (ph = phdr; ph < phlimit; ph++) { 1084 switch (ph->p_type) { 1085 1086 case PT_INTERP: 1087 obj->interp = (const char *)(ph->p_vaddr + obj->relocbase); 1088 break; 1089 1090 case PT_LOAD: 1091 if (nsegs == 0) { /* First load segment */ 1092 obj->vaddrbase = trunc_page(ph->p_vaddr); 1093 obj->mapbase = obj->vaddrbase + obj->relocbase; 1094 obj->textsize = round_page(ph->p_vaddr + ph->p_memsz) - 1095 obj->vaddrbase; 1096 } else { /* Last load segment */ 1097 obj->mapsize = round_page(ph->p_vaddr + ph->p_memsz) - 1098 obj->vaddrbase; 1099 } 1100 nsegs++; 1101 break; 1102 1103 case PT_DYNAMIC: 1104 obj->dynamic = (const Elf_Dyn *)(ph->p_vaddr + obj->relocbase); 1105 break; 1106 1107 case PT_TLS: 1108 obj->tlsindex = 1; 1109 obj->tlssize = ph->p_memsz; 1110 obj->tlsalign = ph->p_align; 1111 obj->tlsinitsize = ph->p_filesz; 1112 obj->tlsinit = (void*)(ph->p_vaddr + obj->relocbase); 1113 break; 1114 1115 case PT_GNU_STACK: 1116 obj->stack_flags = ph->p_flags; 1117 break; 1118 1119 case PT_GNU_RELRO: 1120 obj->relro_page = obj->relocbase + trunc_page(ph->p_vaddr); 1121 obj->relro_size = round_page(ph->p_memsz); 1122 break; 1123 } 1124 } 1125 if (nsegs < 1) { 1126 _rtld_error("%s: too few PT_LOAD segments", path); 1127 return NULL; 1128 } 1129 1130 obj->entry = entry; 1131 return obj; 1132 } 1133 1134 static Obj_Entry * 1135 dlcheck(void *handle) 1136 { 1137 Obj_Entry *obj; 1138 1139 for (obj = obj_list; obj != NULL; obj = obj->next) 1140 if (obj == (Obj_Entry *) handle) 1141 break; 1142 1143 if (obj == NULL || obj->refcount == 0 || obj->dl_refcount == 0) { 1144 _rtld_error("Invalid shared object handle %p", handle); 1145 return NULL; 1146 } 1147 return obj; 1148 } 1149 1150 /* 1151 * If the given object is already in the donelist, return true. Otherwise 1152 * add the object to the list and return false. 1153 */ 1154 static bool 1155 donelist_check(DoneList *dlp, const Obj_Entry *obj) 1156 { 1157 unsigned int i; 1158 1159 for (i = 0; i < dlp->num_used; i++) 1160 if (dlp->objs[i] == obj) 1161 return true; 1162 /* 1163 * Our donelist allocation should always be sufficient. But if 1164 * our threads locking isn't working properly, more shared objects 1165 * could have been loaded since we allocated the list. That should 1166 * never happen, but we'll handle it properly just in case it does. 1167 */ 1168 if (dlp->num_used < dlp->num_alloc) 1169 dlp->objs[dlp->num_used++] = obj; 1170 return false; 1171 } 1172 1173 /* 1174 * Hash function for symbol table lookup. Don't even think about changing 1175 * this. It is specified by the System V ABI. 1176 */ 1177 unsigned long 1178 elf_hash(const char *name) 1179 { 1180 const unsigned char *p = (const unsigned char *) name; 1181 unsigned long h = 0; 1182 unsigned long g; 1183 1184 while (*p != '\0') { 1185 h = (h << 4) + *p++; 1186 if ((g = h & 0xf0000000) != 0) 1187 h ^= g >> 24; 1188 h &= ~g; 1189 } 1190 return h; 1191 } 1192 1193 /* 1194 * Find the library with the given name, and return its full pathname. 1195 * The returned string is dynamically allocated. Generates an error 1196 * message and returns NULL if the library cannot be found. 1197 * 1198 * If the second argument is non-NULL, then it refers to an already- 1199 * loaded shared object, whose library search path will be searched. 1200 * 1201 * The search order is: 1202 * LD_LIBRARY_PATH 1203 * rpath in the referencing file 1204 * ldconfig hints 1205 * /lib:/usr/lib 1206 */ 1207 static char * 1208 find_library(const char *xname, const Obj_Entry *refobj) 1209 { 1210 char *pathname; 1211 char *name; 1212 1213 if (strchr(xname, '/') != NULL) { /* Hard coded pathname */ 1214 if (xname[0] != '/' && !trust) { 1215 _rtld_error("Absolute pathname required for shared object \"%s\"", 1216 xname); 1217 return NULL; 1218 } 1219 if (refobj != NULL && refobj->z_origin) 1220 return origin_subst(xname, refobj->origin_path); 1221 else 1222 return xstrdup(xname); 1223 } 1224 1225 if (libmap_disable || (refobj == NULL) || 1226 (name = lm_find(refobj->path, xname)) == NULL) 1227 name = (char *)xname; 1228 1229 dbg(" Searching for \"%s\"", name); 1230 1231 if ((pathname = search_library_path(name, ld_library_path)) != NULL || 1232 (refobj != NULL && 1233 (pathname = search_library_path(name, refobj->rpath)) != NULL) || 1234 (pathname = search_library_path(name, gethints())) != NULL || 1235 (pathname = search_library_path(name, STANDARD_LIBRARY_PATH)) != NULL) 1236 return pathname; 1237 1238 if(refobj != NULL && refobj->path != NULL) { 1239 _rtld_error("Shared object \"%s\" not found, required by \"%s\"", 1240 name, basename(refobj->path)); 1241 } else { 1242 _rtld_error("Shared object \"%s\" not found", name); 1243 } 1244 return NULL; 1245 } 1246 1247 /* 1248 * Given a symbol number in a referencing object, find the corresponding 1249 * definition of the symbol. Returns a pointer to the symbol, or NULL if 1250 * no definition was found. Returns a pointer to the Obj_Entry of the 1251 * defining object via the reference parameter DEFOBJ_OUT. 1252 */ 1253 const Elf_Sym * 1254 find_symdef(unsigned long symnum, const Obj_Entry *refobj, 1255 const Obj_Entry **defobj_out, int flags, SymCache *cache, 1256 RtldLockState *lockstate) 1257 { 1258 const Elf_Sym *ref; 1259 const Elf_Sym *def; 1260 const Obj_Entry *defobj; 1261 SymLook req; 1262 const char *name; 1263 int res; 1264 1265 /* 1266 * If we have already found this symbol, get the information from 1267 * the cache. 1268 */ 1269 if (symnum >= refobj->nchains) 1270 return NULL; /* Bad object */ 1271 if (cache != NULL && cache[symnum].sym != NULL) { 1272 *defobj_out = cache[symnum].obj; 1273 return cache[symnum].sym; 1274 } 1275 1276 ref = refobj->symtab + symnum; 1277 name = refobj->strtab + ref->st_name; 1278 def = NULL; 1279 defobj = NULL; 1280 1281 /* 1282 * We don't have to do a full scale lookup if the symbol is local. 1283 * We know it will bind to the instance in this load module; to 1284 * which we already have a pointer (ie ref). By not doing a lookup, 1285 * we not only improve performance, but it also avoids unresolvable 1286 * symbols when local symbols are not in the hash table. This has 1287 * been seen with the ia64 toolchain. 1288 */ 1289 if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) { 1290 if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) { 1291 _rtld_error("%s: Bogus symbol table entry %lu", refobj->path, 1292 symnum); 1293 } 1294 symlook_init(&req, name); 1295 req.flags = flags; 1296 req.ventry = fetch_ventry(refobj, symnum); 1297 req.lockstate = lockstate; 1298 res = symlook_default(&req, refobj); 1299 if (res == 0) { 1300 def = req.sym_out; 1301 defobj = req.defobj_out; 1302 } 1303 } else { 1304 def = ref; 1305 defobj = refobj; 1306 } 1307 1308 /* 1309 * If we found no definition and the reference is weak, treat the 1310 * symbol as having the value zero. 1311 */ 1312 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) { 1313 def = &sym_zero; 1314 defobj = obj_main; 1315 } 1316 1317 if (def != NULL) { 1318 *defobj_out = defobj; 1319 /* Record the information in the cache to avoid subsequent lookups. */ 1320 if (cache != NULL) { 1321 cache[symnum].sym = def; 1322 cache[symnum].obj = defobj; 1323 } 1324 } else { 1325 if (refobj != &obj_rtld) 1326 _rtld_error("%s: Undefined symbol \"%s\"", refobj->path, name); 1327 } 1328 return def; 1329 } 1330 1331 /* 1332 * Return the search path from the ldconfig hints file, reading it if 1333 * necessary. Returns NULL if there are problems with the hints file, 1334 * or if the search path there is empty. 1335 */ 1336 static const char * 1337 gethints(void) 1338 { 1339 static char *hints; 1340 1341 if (hints == NULL) { 1342 int fd; 1343 struct elfhints_hdr hdr; 1344 char *p; 1345 1346 /* Keep from trying again in case the hints file is bad. */ 1347 hints = ""; 1348 1349 if ((fd = open(ld_elf_hints_path, O_RDONLY)) == -1) 1350 return NULL; 1351 if (read(fd, &hdr, sizeof hdr) != sizeof hdr || 1352 hdr.magic != ELFHINTS_MAGIC || 1353 hdr.version != 1) { 1354 close(fd); 1355 return NULL; 1356 } 1357 p = xmalloc(hdr.dirlistlen + 1); 1358 if (lseek(fd, hdr.strtab + hdr.dirlist, SEEK_SET) == -1 || 1359 read(fd, p, hdr.dirlistlen + 1) != (ssize_t)hdr.dirlistlen + 1) { 1360 free(p); 1361 close(fd); 1362 return NULL; 1363 } 1364 hints = p; 1365 close(fd); 1366 } 1367 return hints[0] != '\0' ? hints : NULL; 1368 } 1369 1370 static void 1371 init_dag(Obj_Entry *root) 1372 { 1373 const Needed_Entry *needed; 1374 const Objlist_Entry *elm; 1375 DoneList donelist; 1376 1377 if (root->dag_inited) 1378 return; 1379 donelist_init(&donelist); 1380 1381 /* Root object belongs to own DAG. */ 1382 objlist_push_tail(&root->dldags, root); 1383 objlist_push_tail(&root->dagmembers, root); 1384 donelist_check(&donelist, root); 1385 1386 /* 1387 * Add dependencies of root object to DAG in breadth order 1388 * by exploiting the fact that each new object get added 1389 * to the tail of the dagmembers list. 1390 */ 1391 STAILQ_FOREACH(elm, &root->dagmembers, link) { 1392 for (needed = elm->obj->needed; needed != NULL; needed = needed->next) { 1393 if (needed->obj == NULL || donelist_check(&donelist, needed->obj)) 1394 continue; 1395 objlist_push_tail(&needed->obj->dldags, root); 1396 objlist_push_tail(&root->dagmembers, needed->obj); 1397 } 1398 } 1399 root->dag_inited = true; 1400 } 1401 1402 /* 1403 * Initialize the dynamic linker. The argument is the address at which 1404 * the dynamic linker has been mapped into memory. The primary task of 1405 * this function is to relocate the dynamic linker. 1406 */ 1407 static void 1408 init_rtld(caddr_t mapbase, Elf_Auxinfo **aux_info) 1409 { 1410 Obj_Entry objtmp; /* Temporary rtld object */ 1411 const Elf_Dyn *dyn_rpath; 1412 const Elf_Dyn *dyn_soname; 1413 1414 /* 1415 * Conjure up an Obj_Entry structure for the dynamic linker. 1416 * 1417 * The "path" member can't be initialized yet because string constants 1418 * cannot yet be accessed. Below we will set it correctly. 1419 */ 1420 memset(&objtmp, 0, sizeof(objtmp)); 1421 objtmp.path = NULL; 1422 objtmp.rtld = true; 1423 objtmp.mapbase = mapbase; 1424 #ifdef PIC 1425 objtmp.relocbase = mapbase; 1426 #endif 1427 if (RTLD_IS_DYNAMIC()) { 1428 objtmp.dynamic = rtld_dynamic(&objtmp); 1429 digest_dynamic1(&objtmp, 1, &dyn_rpath, &dyn_soname); 1430 assert(objtmp.needed == NULL); 1431 #if !defined(__mips__) 1432 /* MIPS has a bogus DT_TEXTREL. */ 1433 assert(!objtmp.textrel); 1434 #endif 1435 1436 /* 1437 * Temporarily put the dynamic linker entry into the object list, so 1438 * that symbols can be found. 1439 */ 1440 1441 relocate_objects(&objtmp, true, &objtmp, NULL); 1442 } 1443 1444 /* Initialize the object list. */ 1445 obj_tail = &obj_list; 1446 1447 /* Now that non-local variables can be accesses, copy out obj_rtld. */ 1448 memcpy(&obj_rtld, &objtmp, sizeof(obj_rtld)); 1449 1450 if (aux_info[AT_PAGESZ] != NULL) 1451 pagesize = aux_info[AT_PAGESZ]->a_un.a_val; 1452 if (aux_info[AT_OSRELDATE] != NULL) 1453 osreldate = aux_info[AT_OSRELDATE]->a_un.a_val; 1454 1455 digest_dynamic2(&obj_rtld, dyn_rpath, dyn_soname); 1456 1457 /* Replace the path with a dynamically allocated copy. */ 1458 obj_rtld.path = xstrdup(PATH_RTLD); 1459 1460 r_debug.r_brk = r_debug_state; 1461 r_debug.r_state = RT_CONSISTENT; 1462 } 1463 1464 /* 1465 * Add the init functions from a needed object list (and its recursive 1466 * needed objects) to "list". This is not used directly; it is a helper 1467 * function for initlist_add_objects(). The write lock must be held 1468 * when this function is called. 1469 */ 1470 static void 1471 initlist_add_neededs(Needed_Entry *needed, Objlist *list) 1472 { 1473 /* Recursively process the successor needed objects. */ 1474 if (needed->next != NULL) 1475 initlist_add_neededs(needed->next, list); 1476 1477 /* Process the current needed object. */ 1478 if (needed->obj != NULL) 1479 initlist_add_objects(needed->obj, &needed->obj->next, list); 1480 } 1481 1482 /* 1483 * Scan all of the DAGs rooted in the range of objects from "obj" to 1484 * "tail" and add their init functions to "list". This recurses over 1485 * the DAGs and ensure the proper init ordering such that each object's 1486 * needed libraries are initialized before the object itself. At the 1487 * same time, this function adds the objects to the global finalization 1488 * list "list_fini" in the opposite order. The write lock must be 1489 * held when this function is called. 1490 */ 1491 static void 1492 initlist_add_objects(Obj_Entry *obj, Obj_Entry **tail, Objlist *list) 1493 { 1494 if (obj->init_scanned || obj->init_done) 1495 return; 1496 obj->init_scanned = true; 1497 1498 /* Recursively process the successor objects. */ 1499 if (&obj->next != tail) 1500 initlist_add_objects(obj->next, tail, list); 1501 1502 /* Recursively process the needed objects. */ 1503 if (obj->needed != NULL) 1504 initlist_add_neededs(obj->needed, list); 1505 1506 /* Add the object to the init list. */ 1507 if (obj->init != (Elf_Addr)NULL) 1508 objlist_push_tail(list, obj); 1509 1510 /* Add the object to the global fini list in the reverse order. */ 1511 if (obj->fini != (Elf_Addr)NULL && !obj->on_fini_list) { 1512 objlist_push_head(&list_fini, obj); 1513 obj->on_fini_list = true; 1514 } 1515 } 1516 1517 #ifndef FPTR_TARGET 1518 #define FPTR_TARGET(f) ((Elf_Addr) (f)) 1519 #endif 1520 1521 static void 1522 free_needed_filtees(Needed_Entry *n) 1523 { 1524 Needed_Entry *needed, *needed1; 1525 1526 for (needed = n; needed != NULL; needed = needed->next) { 1527 if (needed->obj != NULL) { 1528 dlclose(needed->obj); 1529 needed->obj = NULL; 1530 } 1531 } 1532 for (needed = n; needed != NULL; needed = needed1) { 1533 needed1 = needed->next; 1534 free(needed); 1535 } 1536 } 1537 1538 static void 1539 unload_filtees(Obj_Entry *obj) 1540 { 1541 1542 free_needed_filtees(obj->needed_filtees); 1543 obj->needed_filtees = NULL; 1544 free_needed_filtees(obj->needed_aux_filtees); 1545 obj->needed_aux_filtees = NULL; 1546 obj->filtees_loaded = false; 1547 } 1548 1549 static void 1550 load_filtee1(Obj_Entry *obj, Needed_Entry *needed, int flags) 1551 { 1552 1553 for (; needed != NULL; needed = needed->next) { 1554 needed->obj = dlopen_object(obj->strtab + needed->name, -1, obj, 1555 flags, ((ld_loadfltr || obj->z_loadfltr) ? RTLD_NOW : RTLD_LAZY) | 1556 RTLD_LOCAL); 1557 } 1558 } 1559 1560 static void 1561 load_filtees(Obj_Entry *obj, int flags, RtldLockState *lockstate) 1562 { 1563 1564 lock_restart_for_upgrade(lockstate); 1565 if (!obj->filtees_loaded) { 1566 load_filtee1(obj, obj->needed_filtees, flags); 1567 load_filtee1(obj, obj->needed_aux_filtees, flags); 1568 obj->filtees_loaded = true; 1569 } 1570 } 1571 1572 static int 1573 process_needed(Obj_Entry *obj, Needed_Entry *needed, int flags) 1574 { 1575 Obj_Entry *obj1; 1576 1577 for (; needed != NULL; needed = needed->next) { 1578 obj1 = needed->obj = load_object(obj->strtab + needed->name, -1, obj, 1579 flags & ~RTLD_LO_NOLOAD); 1580 if (obj1 == NULL && !ld_tracing && (flags & RTLD_LO_FILTEES) == 0) 1581 return (-1); 1582 if (obj1 != NULL && obj1->z_nodelete && !obj1->ref_nodel) { 1583 dbg("obj %s nodelete", obj1->path); 1584 init_dag(obj1); 1585 ref_dag(obj1); 1586 obj1->ref_nodel = true; 1587 } 1588 } 1589 return (0); 1590 } 1591 1592 /* 1593 * Given a shared object, traverse its list of needed objects, and load 1594 * each of them. Returns 0 on success. Generates an error message and 1595 * returns -1 on failure. 1596 */ 1597 static int 1598 load_needed_objects(Obj_Entry *first, int flags) 1599 { 1600 Obj_Entry *obj; 1601 1602 for (obj = first; obj != NULL; obj = obj->next) { 1603 if (process_needed(obj, obj->needed, flags) == -1) 1604 return (-1); 1605 } 1606 return (0); 1607 } 1608 1609 static int 1610 load_preload_objects(void) 1611 { 1612 char *p = ld_preload; 1613 static const char delim[] = " \t:;"; 1614 1615 if (p == NULL) 1616 return 0; 1617 1618 p += strspn(p, delim); 1619 while (*p != '\0') { 1620 size_t len = strcspn(p, delim); 1621 char savech; 1622 1623 savech = p[len]; 1624 p[len] = '\0'; 1625 if (load_object(p, -1, NULL, 0) == NULL) 1626 return -1; /* XXX - cleanup */ 1627 p[len] = savech; 1628 p += len; 1629 p += strspn(p, delim); 1630 } 1631 LD_UTRACE(UTRACE_PRELOAD_FINISHED, NULL, NULL, 0, 0, NULL); 1632 return 0; 1633 } 1634 1635 static const char * 1636 printable_path(const char *path) 1637 { 1638 1639 return (path == NULL ? "<unknown>" : path); 1640 } 1641 1642 /* 1643 * Load a shared object into memory, if it is not already loaded. The 1644 * object may be specified by name or by user-supplied file descriptor 1645 * fd_u. In the later case, the fd_u descriptor is not closed, but its 1646 * duplicate is. 1647 * 1648 * Returns a pointer to the Obj_Entry for the object. Returns NULL 1649 * on failure. 1650 */ 1651 static Obj_Entry * 1652 load_object(const char *name, int fd_u, const Obj_Entry *refobj, int flags) 1653 { 1654 Obj_Entry *obj; 1655 int fd; 1656 struct stat sb; 1657 char *path; 1658 1659 if (name != NULL) { 1660 for (obj = obj_list->next; obj != NULL; obj = obj->next) { 1661 if (object_match_name(obj, name)) 1662 return (obj); 1663 } 1664 1665 path = find_library(name, refobj); 1666 if (path == NULL) 1667 return (NULL); 1668 } else 1669 path = NULL; 1670 1671 /* 1672 * If we didn't find a match by pathname, or the name is not 1673 * supplied, open the file and check again by device and inode. 1674 * This avoids false mismatches caused by multiple links or ".." 1675 * in pathnames. 1676 * 1677 * To avoid a race, we open the file and use fstat() rather than 1678 * using stat(). 1679 */ 1680 fd = -1; 1681 if (fd_u == -1) { 1682 if ((fd = open(path, O_RDONLY)) == -1) { 1683 _rtld_error("Cannot open \"%s\"", path); 1684 free(path); 1685 return (NULL); 1686 } 1687 } else { 1688 fd = dup(fd_u); 1689 if (fd == -1) { 1690 _rtld_error("Cannot dup fd"); 1691 free(path); 1692 return (NULL); 1693 } 1694 } 1695 if (fstat(fd, &sb) == -1) { 1696 _rtld_error("Cannot fstat \"%s\"", printable_path(path)); 1697 close(fd); 1698 free(path); 1699 return NULL; 1700 } 1701 for (obj = obj_list->next; obj != NULL; obj = obj->next) 1702 if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) 1703 break; 1704 if (obj != NULL && name != NULL) { 1705 object_add_name(obj, name); 1706 free(path); 1707 close(fd); 1708 return obj; 1709 } 1710 if (flags & RTLD_LO_NOLOAD) { 1711 free(path); 1712 close(fd); 1713 return (NULL); 1714 } 1715 1716 /* First use of this object, so we must map it in */ 1717 obj = do_load_object(fd, name, path, &sb, flags); 1718 if (obj == NULL) 1719 free(path); 1720 close(fd); 1721 1722 return obj; 1723 } 1724 1725 static Obj_Entry * 1726 do_load_object(int fd, const char *name, char *path, struct stat *sbp, 1727 int flags) 1728 { 1729 Obj_Entry *obj; 1730 struct statfs fs; 1731 1732 /* 1733 * but first, make sure that environment variables haven't been 1734 * used to circumvent the noexec flag on a filesystem. 1735 */ 1736 if (dangerous_ld_env) { 1737 if (fstatfs(fd, &fs) != 0) { 1738 _rtld_error("Cannot fstatfs \"%s\"", printable_path(path)); 1739 return NULL; 1740 } 1741 if (fs.f_flags & MNT_NOEXEC) { 1742 _rtld_error("Cannot execute objects on %s\n", fs.f_mntonname); 1743 return NULL; 1744 } 1745 } 1746 dbg("loading \"%s\"", printable_path(path)); 1747 obj = map_object(fd, printable_path(path), sbp); 1748 if (obj == NULL) 1749 return NULL; 1750 1751 /* 1752 * If DT_SONAME is present in the object, digest_dynamic2 already 1753 * added it to the object names. 1754 */ 1755 if (name != NULL) 1756 object_add_name(obj, name); 1757 obj->path = path; 1758 digest_dynamic(obj, 0); 1759 if (obj->z_noopen && (flags & (RTLD_LO_DLOPEN | RTLD_LO_TRACE)) == 1760 RTLD_LO_DLOPEN) { 1761 dbg("refusing to load non-loadable \"%s\"", obj->path); 1762 _rtld_error("Cannot dlopen non-loadable %s", obj->path); 1763 munmap(obj->mapbase, obj->mapsize); 1764 obj_free(obj); 1765 return (NULL); 1766 } 1767 1768 *obj_tail = obj; 1769 obj_tail = &obj->next; 1770 obj_count++; 1771 obj_loads++; 1772 linkmap_add(obj); /* for GDB & dlinfo() */ 1773 max_stack_flags |= obj->stack_flags; 1774 1775 dbg(" %p .. %p: %s", obj->mapbase, 1776 obj->mapbase + obj->mapsize - 1, obj->path); 1777 if (obj->textrel) 1778 dbg(" WARNING: %s has impure text", obj->path); 1779 LD_UTRACE(UTRACE_LOAD_OBJECT, obj, obj->mapbase, obj->mapsize, 0, 1780 obj->path); 1781 1782 return obj; 1783 } 1784 1785 static Obj_Entry * 1786 obj_from_addr(const void *addr) 1787 { 1788 Obj_Entry *obj; 1789 1790 for (obj = obj_list; obj != NULL; obj = obj->next) { 1791 if (addr < (void *) obj->mapbase) 1792 continue; 1793 if (addr < (void *) (obj->mapbase + obj->mapsize)) 1794 return obj; 1795 } 1796 return NULL; 1797 } 1798 1799 /* 1800 * Call the finalization functions for each of the objects in "list" 1801 * belonging to the DAG of "root" and referenced once. If NULL "root" 1802 * is specified, every finalization function will be called regardless 1803 * of the reference count and the list elements won't be freed. All of 1804 * the objects are expected to have non-NULL fini functions. 1805 */ 1806 static void 1807 objlist_call_fini(Objlist *list, Obj_Entry *root, RtldLockState *lockstate) 1808 { 1809 Objlist_Entry *elm; 1810 char *saved_msg; 1811 1812 assert(root == NULL || root->refcount == 1); 1813 1814 /* 1815 * Preserve the current error message since a fini function might 1816 * call into the dynamic linker and overwrite it. 1817 */ 1818 saved_msg = errmsg_save(); 1819 do { 1820 STAILQ_FOREACH(elm, list, link) { 1821 if (root != NULL && (elm->obj->refcount != 1 || 1822 objlist_find(&root->dagmembers, elm->obj) == NULL)) 1823 continue; 1824 dbg("calling fini function for %s at %p", elm->obj->path, 1825 (void *)elm->obj->fini); 1826 LD_UTRACE(UTRACE_FINI_CALL, elm->obj, (void *)elm->obj->fini, 0, 0, 1827 elm->obj->path); 1828 /* Remove object from fini list to prevent recursive invocation. */ 1829 STAILQ_REMOVE(list, elm, Struct_Objlist_Entry, link); 1830 /* 1831 * XXX: If a dlopen() call references an object while the 1832 * fini function is in progress, we might end up trying to 1833 * unload the referenced object in dlclose() or the object 1834 * won't be unloaded although its fini function has been 1835 * called. 1836 */ 1837 lock_release(rtld_bind_lock, lockstate); 1838 call_initfini_pointer(elm->obj, elm->obj->fini); 1839 wlock_acquire(rtld_bind_lock, lockstate); 1840 /* No need to free anything if process is going down. */ 1841 if (root != NULL) 1842 free(elm); 1843 /* 1844 * We must restart the list traversal after every fini call 1845 * because a dlclose() call from the fini function or from 1846 * another thread might have modified the reference counts. 1847 */ 1848 break; 1849 } 1850 } while (elm != NULL); 1851 errmsg_restore(saved_msg); 1852 } 1853 1854 /* 1855 * Call the initialization functions for each of the objects in 1856 * "list". All of the objects are expected to have non-NULL init 1857 * functions. 1858 */ 1859 static void 1860 objlist_call_init(Objlist *list, RtldLockState *lockstate) 1861 { 1862 Objlist_Entry *elm; 1863 Obj_Entry *obj; 1864 char *saved_msg; 1865 1866 /* 1867 * Clean init_scanned flag so that objects can be rechecked and 1868 * possibly initialized earlier if any of vectors called below 1869 * cause the change by using dlopen. 1870 */ 1871 for (obj = obj_list; obj != NULL; obj = obj->next) 1872 obj->init_scanned = false; 1873 1874 /* 1875 * Preserve the current error message since an init function might 1876 * call into the dynamic linker and overwrite it. 1877 */ 1878 saved_msg = errmsg_save(); 1879 STAILQ_FOREACH(elm, list, link) { 1880 if (elm->obj->init_done) /* Initialized early. */ 1881 continue; 1882 dbg("calling init function for %s at %p", elm->obj->path, 1883 (void *)elm->obj->init); 1884 LD_UTRACE(UTRACE_INIT_CALL, elm->obj, (void *)elm->obj->init, 0, 0, 1885 elm->obj->path); 1886 /* 1887 * Race: other thread might try to use this object before current 1888 * one completes the initilization. Not much can be done here 1889 * without better locking. 1890 */ 1891 elm->obj->init_done = true; 1892 lock_release(rtld_bind_lock, lockstate); 1893 call_initfini_pointer(elm->obj, elm->obj->init); 1894 wlock_acquire(rtld_bind_lock, lockstate); 1895 } 1896 errmsg_restore(saved_msg); 1897 } 1898 1899 static void 1900 objlist_clear(Objlist *list) 1901 { 1902 Objlist_Entry *elm; 1903 1904 while (!STAILQ_EMPTY(list)) { 1905 elm = STAILQ_FIRST(list); 1906 STAILQ_REMOVE_HEAD(list, link); 1907 free(elm); 1908 } 1909 } 1910 1911 static Objlist_Entry * 1912 objlist_find(Objlist *list, const Obj_Entry *obj) 1913 { 1914 Objlist_Entry *elm; 1915 1916 STAILQ_FOREACH(elm, list, link) 1917 if (elm->obj == obj) 1918 return elm; 1919 return NULL; 1920 } 1921 1922 static void 1923 objlist_init(Objlist *list) 1924 { 1925 STAILQ_INIT(list); 1926 } 1927 1928 static void 1929 objlist_push_head(Objlist *list, Obj_Entry *obj) 1930 { 1931 Objlist_Entry *elm; 1932 1933 elm = NEW(Objlist_Entry); 1934 elm->obj = obj; 1935 STAILQ_INSERT_HEAD(list, elm, link); 1936 } 1937 1938 static void 1939 objlist_push_tail(Objlist *list, Obj_Entry *obj) 1940 { 1941 Objlist_Entry *elm; 1942 1943 elm = NEW(Objlist_Entry); 1944 elm->obj = obj; 1945 STAILQ_INSERT_TAIL(list, elm, link); 1946 } 1947 1948 static void 1949 objlist_remove(Objlist *list, Obj_Entry *obj) 1950 { 1951 Objlist_Entry *elm; 1952 1953 if ((elm = objlist_find(list, obj)) != NULL) { 1954 STAILQ_REMOVE(list, elm, Struct_Objlist_Entry, link); 1955 free(elm); 1956 } 1957 } 1958 1959 /* 1960 * Relocate newly-loaded shared objects. The argument is a pointer to 1961 * the Obj_Entry for the first such object. All objects from the first 1962 * to the end of the list of objects are relocated. Returns 0 on success, 1963 * or -1 on failure. 1964 */ 1965 static int 1966 relocate_objects(Obj_Entry *first, bool bind_now, Obj_Entry *rtldobj, 1967 RtldLockState *lockstate) 1968 { 1969 Obj_Entry *obj; 1970 1971 for (obj = first; obj != NULL; obj = obj->next) { 1972 if (obj != rtldobj) 1973 dbg("relocating \"%s\"", obj->path); 1974 if (obj->nbuckets == 0 || obj->nchains == 0 || obj->buckets == NULL || 1975 obj->symtab == NULL || obj->strtab == NULL) { 1976 _rtld_error("%s: Shared object has no run-time symbol table", 1977 obj->path); 1978 return -1; 1979 } 1980 1981 if (obj->textrel) { 1982 /* There are relocations to the write-protected text segment. */ 1983 if (mprotect(obj->mapbase, obj->textsize, 1984 PROT_READ|PROT_WRITE|PROT_EXEC) == -1) { 1985 _rtld_error("%s: Cannot write-enable text segment: %s", 1986 obj->path, strerror(errno)); 1987 return -1; 1988 } 1989 } 1990 1991 /* Process the non-PLT relocations. */ 1992 if (reloc_non_plt(obj, rtldobj, lockstate)) 1993 return -1; 1994 1995 if (obj->textrel) { /* Re-protected the text segment. */ 1996 if (mprotect(obj->mapbase, obj->textsize, 1997 PROT_READ|PROT_EXEC) == -1) { 1998 _rtld_error("%s: Cannot write-protect text segment: %s", 1999 obj->path, strerror(errno)); 2000 return -1; 2001 } 2002 } 2003 2004 2005 /* Set the special PLT or GOT entries. */ 2006 init_pltgot(obj); 2007 2008 /* Process the PLT relocations. */ 2009 if (reloc_plt(obj) == -1) 2010 return -1; 2011 /* Relocate the jump slots if we are doing immediate binding. */ 2012 if (obj->bind_now || bind_now) 2013 if (reloc_jmpslots(obj, lockstate) == -1) 2014 return -1; 2015 2016 if (obj->relro_size > 0) { 2017 if (mprotect(obj->relro_page, obj->relro_size, PROT_READ) == -1) { 2018 _rtld_error("%s: Cannot enforce relro protection: %s", 2019 obj->path, strerror(errno)); 2020 return -1; 2021 } 2022 } 2023 2024 /* 2025 * Set up the magic number and version in the Obj_Entry. These 2026 * were checked in the crt1.o from the original ElfKit, so we 2027 * set them for backward compatibility. 2028 */ 2029 obj->magic = RTLD_MAGIC; 2030 obj->version = RTLD_VERSION; 2031 } 2032 2033 return (0); 2034 } 2035 2036 /* 2037 * The handling of R_MACHINE_IRELATIVE relocations and jumpslots 2038 * referencing STT_GNU_IFUNC symbols is postponed till the other 2039 * relocations are done. The indirect functions specified as 2040 * ifunc are allowed to call other symbols, so we need to have 2041 * objects relocated before asking for resolution from indirects. 2042 * 2043 * The R_MACHINE_IRELATIVE slots are resolved in greedy fashion, 2044 * instead of the usual lazy handling of PLT slots. It is 2045 * consistent with how GNU does it. 2046 */ 2047 static int 2048 resolve_object_ifunc(Obj_Entry *obj, bool bind_now, RtldLockState *lockstate) 2049 { 2050 if (obj->irelative && reloc_iresolve(obj, lockstate) == -1) 2051 return (-1); 2052 if ((obj->bind_now || bind_now) && obj->gnu_ifunc && 2053 reloc_gnu_ifunc(obj, lockstate) == -1) 2054 return (-1); 2055 return (0); 2056 } 2057 2058 static int 2059 resolve_objects_ifunc(Obj_Entry *first, bool bind_now, RtldLockState *lockstate) 2060 { 2061 Obj_Entry *obj; 2062 2063 for (obj = first; obj != NULL; obj = obj->next) { 2064 if (resolve_object_ifunc(obj, bind_now, lockstate) == -1) 2065 return (-1); 2066 } 2067 return (0); 2068 } 2069 2070 static int 2071 initlist_objects_ifunc(Objlist *list, bool bind_now, RtldLockState *lockstate) 2072 { 2073 Objlist_Entry *elm; 2074 2075 STAILQ_FOREACH(elm, list, link) { 2076 if (resolve_object_ifunc(elm->obj, bind_now, lockstate) == -1) 2077 return (-1); 2078 } 2079 return (0); 2080 } 2081 2082 /* 2083 * Cleanup procedure. It will be called (by the atexit mechanism) just 2084 * before the process exits. 2085 */ 2086 static void 2087 rtld_exit(void) 2088 { 2089 RtldLockState lockstate; 2090 2091 wlock_acquire(rtld_bind_lock, &lockstate); 2092 dbg("rtld_exit()"); 2093 objlist_call_fini(&list_fini, NULL, &lockstate); 2094 /* No need to remove the items from the list, since we are exiting. */ 2095 if (!libmap_disable) 2096 lm_fini(); 2097 lock_release(rtld_bind_lock, &lockstate); 2098 } 2099 2100 static void * 2101 path_enumerate(const char *path, path_enum_proc callback, void *arg) 2102 { 2103 #ifdef COMPAT_32BIT 2104 const char *trans; 2105 #endif 2106 if (path == NULL) 2107 return (NULL); 2108 2109 path += strspn(path, ":;"); 2110 while (*path != '\0') { 2111 size_t len; 2112 char *res; 2113 2114 len = strcspn(path, ":;"); 2115 #ifdef COMPAT_32BIT 2116 trans = lm_findn(NULL, path, len); 2117 if (trans) 2118 res = callback(trans, strlen(trans), arg); 2119 else 2120 #endif 2121 res = callback(path, len, arg); 2122 2123 if (res != NULL) 2124 return (res); 2125 2126 path += len; 2127 path += strspn(path, ":;"); 2128 } 2129 2130 return (NULL); 2131 } 2132 2133 struct try_library_args { 2134 const char *name; 2135 size_t namelen; 2136 char *buffer; 2137 size_t buflen; 2138 }; 2139 2140 static void * 2141 try_library_path(const char *dir, size_t dirlen, void *param) 2142 { 2143 struct try_library_args *arg; 2144 2145 arg = param; 2146 if (*dir == '/' || trust) { 2147 char *pathname; 2148 2149 if (dirlen + 1 + arg->namelen + 1 > arg->buflen) 2150 return (NULL); 2151 2152 pathname = arg->buffer; 2153 strncpy(pathname, dir, dirlen); 2154 pathname[dirlen] = '/'; 2155 strcpy(pathname + dirlen + 1, arg->name); 2156 2157 dbg(" Trying \"%s\"", pathname); 2158 if (access(pathname, F_OK) == 0) { /* We found it */ 2159 pathname = xmalloc(dirlen + 1 + arg->namelen + 1); 2160 strcpy(pathname, arg->buffer); 2161 return (pathname); 2162 } 2163 } 2164 return (NULL); 2165 } 2166 2167 static char * 2168 search_library_path(const char *name, const char *path) 2169 { 2170 char *p; 2171 struct try_library_args arg; 2172 2173 if (path == NULL) 2174 return NULL; 2175 2176 arg.name = name; 2177 arg.namelen = strlen(name); 2178 arg.buffer = xmalloc(PATH_MAX); 2179 arg.buflen = PATH_MAX; 2180 2181 p = path_enumerate(path, try_library_path, &arg); 2182 2183 free(arg.buffer); 2184 2185 return (p); 2186 } 2187 2188 int 2189 dlclose(void *handle) 2190 { 2191 Obj_Entry *root; 2192 RtldLockState lockstate; 2193 2194 wlock_acquire(rtld_bind_lock, &lockstate); 2195 root = dlcheck(handle); 2196 if (root == NULL) { 2197 lock_release(rtld_bind_lock, &lockstate); 2198 return -1; 2199 } 2200 LD_UTRACE(UTRACE_DLCLOSE_START, handle, NULL, 0, root->dl_refcount, 2201 root->path); 2202 2203 /* Unreference the object and its dependencies. */ 2204 root->dl_refcount--; 2205 2206 if (root->refcount == 1) { 2207 /* 2208 * The object will be no longer referenced, so we must unload it. 2209 * First, call the fini functions. 2210 */ 2211 objlist_call_fini(&list_fini, root, &lockstate); 2212 2213 unref_dag(root); 2214 2215 /* Finish cleaning up the newly-unreferenced objects. */ 2216 GDB_STATE(RT_DELETE,&root->linkmap); 2217 unload_object(root); 2218 GDB_STATE(RT_CONSISTENT,NULL); 2219 } else 2220 unref_dag(root); 2221 2222 LD_UTRACE(UTRACE_DLCLOSE_STOP, handle, NULL, 0, 0, NULL); 2223 lock_release(rtld_bind_lock, &lockstate); 2224 return 0; 2225 } 2226 2227 char * 2228 dlerror(void) 2229 { 2230 char *msg = error_message; 2231 error_message = NULL; 2232 return msg; 2233 } 2234 2235 /* 2236 * This function is deprecated and has no effect. 2237 */ 2238 void 2239 dllockinit(void *context, 2240 void *(*lock_create)(void *context), 2241 void (*rlock_acquire)(void *lock), 2242 void (*wlock_acquire)(void *lock), 2243 void (*lock_release)(void *lock), 2244 void (*lock_destroy)(void *lock), 2245 void (*context_destroy)(void *context)) 2246 { 2247 static void *cur_context; 2248 static void (*cur_context_destroy)(void *); 2249 2250 /* Just destroy the context from the previous call, if necessary. */ 2251 if (cur_context_destroy != NULL) 2252 cur_context_destroy(cur_context); 2253 cur_context = context; 2254 cur_context_destroy = context_destroy; 2255 } 2256 2257 void * 2258 dlopen(const char *name, int mode) 2259 { 2260 2261 return (rtld_dlopen(name, -1, mode)); 2262 } 2263 2264 void * 2265 fdlopen(int fd, int mode) 2266 { 2267 2268 return (rtld_dlopen(NULL, fd, mode)); 2269 } 2270 2271 static void * 2272 rtld_dlopen(const char *name, int fd, int mode) 2273 { 2274 RtldLockState lockstate; 2275 int lo_flags; 2276 2277 LD_UTRACE(UTRACE_DLOPEN_START, NULL, NULL, 0, mode, name); 2278 ld_tracing = (mode & RTLD_TRACE) == 0 ? NULL : "1"; 2279 if (ld_tracing != NULL) { 2280 rlock_acquire(rtld_bind_lock, &lockstate); 2281 if (sigsetjmp(lockstate.env, 0) != 0) 2282 lock_upgrade(rtld_bind_lock, &lockstate); 2283 environ = (char **)*get_program_var_addr("environ", &lockstate); 2284 lock_release(rtld_bind_lock, &lockstate); 2285 } 2286 lo_flags = RTLD_LO_DLOPEN; 2287 if (mode & RTLD_NODELETE) 2288 lo_flags |= RTLD_LO_NODELETE; 2289 if (mode & RTLD_NOLOAD) 2290 lo_flags |= RTLD_LO_NOLOAD; 2291 if (ld_tracing != NULL) 2292 lo_flags |= RTLD_LO_TRACE; 2293 2294 return (dlopen_object(name, fd, obj_main, lo_flags, 2295 mode & (RTLD_MODEMASK | RTLD_GLOBAL))); 2296 } 2297 2298 static void 2299 dlopen_cleanup(Obj_Entry *obj) 2300 { 2301 2302 obj->dl_refcount--; 2303 unref_dag(obj); 2304 if (obj->refcount == 0) 2305 unload_object(obj); 2306 } 2307 2308 static Obj_Entry * 2309 dlopen_object(const char *name, int fd, Obj_Entry *refobj, int lo_flags, 2310 int mode) 2311 { 2312 Obj_Entry **old_obj_tail; 2313 Obj_Entry *obj; 2314 Objlist initlist; 2315 RtldLockState lockstate; 2316 int result; 2317 2318 objlist_init(&initlist); 2319 2320 wlock_acquire(rtld_bind_lock, &lockstate); 2321 GDB_STATE(RT_ADD,NULL); 2322 2323 old_obj_tail = obj_tail; 2324 obj = NULL; 2325 if (name == NULL && fd == -1) { 2326 obj = obj_main; 2327 obj->refcount++; 2328 } else { 2329 obj = load_object(name, fd, refobj, lo_flags); 2330 } 2331 2332 if (obj) { 2333 obj->dl_refcount++; 2334 if (mode & RTLD_GLOBAL && objlist_find(&list_global, obj) == NULL) 2335 objlist_push_tail(&list_global, obj); 2336 if (*old_obj_tail != NULL) { /* We loaded something new. */ 2337 assert(*old_obj_tail == obj); 2338 result = load_needed_objects(obj, lo_flags & RTLD_LO_DLOPEN); 2339 init_dag(obj); 2340 ref_dag(obj); 2341 if (result != -1) 2342 result = rtld_verify_versions(&obj->dagmembers); 2343 if (result != -1 && ld_tracing) 2344 goto trace; 2345 if (result == -1 || (relocate_objects(obj, (mode & RTLD_MODEMASK) 2346 == RTLD_NOW, &obj_rtld, &lockstate)) == -1) { 2347 dlopen_cleanup(obj); 2348 obj = NULL; 2349 } else { 2350 /* Make list of init functions to call. */ 2351 initlist_add_objects(obj, &obj->next, &initlist); 2352 } 2353 } else { 2354 2355 /* 2356 * Bump the reference counts for objects on this DAG. If 2357 * this is the first dlopen() call for the object that was 2358 * already loaded as a dependency, initialize the dag 2359 * starting at it. 2360 */ 2361 init_dag(obj); 2362 ref_dag(obj); 2363 2364 if ((lo_flags & RTLD_LO_TRACE) != 0) 2365 goto trace; 2366 } 2367 if (obj != NULL && ((lo_flags & RTLD_LO_NODELETE) != 0 || 2368 obj->z_nodelete) && !obj->ref_nodel) { 2369 dbg("obj %s nodelete", obj->path); 2370 ref_dag(obj); 2371 obj->z_nodelete = obj->ref_nodel = true; 2372 } 2373 } 2374 2375 LD_UTRACE(UTRACE_DLOPEN_STOP, obj, NULL, 0, obj ? obj->dl_refcount : 0, 2376 name); 2377 GDB_STATE(RT_CONSISTENT,obj ? &obj->linkmap : NULL); 2378 2379 map_stacks_exec(&lockstate); 2380 2381 if (initlist_objects_ifunc(&initlist, (mode & RTLD_MODEMASK) == RTLD_NOW, 2382 &lockstate) == -1) { 2383 objlist_clear(&initlist); 2384 dlopen_cleanup(obj); 2385 lock_release(rtld_bind_lock, &lockstate); 2386 return (NULL); 2387 } 2388 2389 /* Call the init functions. */ 2390 objlist_call_init(&initlist, &lockstate); 2391 objlist_clear(&initlist); 2392 lock_release(rtld_bind_lock, &lockstate); 2393 return obj; 2394 trace: 2395 trace_loaded_objects(obj); 2396 lock_release(rtld_bind_lock, &lockstate); 2397 exit(0); 2398 } 2399 2400 static void * 2401 do_dlsym(void *handle, const char *name, void *retaddr, const Ver_Entry *ve, 2402 int flags) 2403 { 2404 DoneList donelist; 2405 const Obj_Entry *obj, *defobj; 2406 const Elf_Sym *def; 2407 SymLook req; 2408 RtldLockState lockstate; 2409 int res; 2410 2411 def = NULL; 2412 defobj = NULL; 2413 symlook_init(&req, name); 2414 req.ventry = ve; 2415 req.flags = flags | SYMLOOK_IN_PLT; 2416 req.lockstate = &lockstate; 2417 2418 rlock_acquire(rtld_bind_lock, &lockstate); 2419 if (sigsetjmp(lockstate.env, 0) != 0) 2420 lock_upgrade(rtld_bind_lock, &lockstate); 2421 if (handle == NULL || handle == RTLD_NEXT || 2422 handle == RTLD_DEFAULT || handle == RTLD_SELF) { 2423 2424 if ((obj = obj_from_addr(retaddr)) == NULL) { 2425 _rtld_error("Cannot determine caller's shared object"); 2426 lock_release(rtld_bind_lock, &lockstate); 2427 return NULL; 2428 } 2429 if (handle == NULL) { /* Just the caller's shared object. */ 2430 res = symlook_obj(&req, obj); 2431 if (res == 0) { 2432 def = req.sym_out; 2433 defobj = req.defobj_out; 2434 } 2435 } else if (handle == RTLD_NEXT || /* Objects after caller's */ 2436 handle == RTLD_SELF) { /* ... caller included */ 2437 if (handle == RTLD_NEXT) 2438 obj = obj->next; 2439 for (; obj != NULL; obj = obj->next) { 2440 res = symlook_obj(&req, obj); 2441 if (res == 0) { 2442 if (def == NULL || 2443 ELF_ST_BIND(req.sym_out->st_info) != STB_WEAK) { 2444 def = req.sym_out; 2445 defobj = req.defobj_out; 2446 if (ELF_ST_BIND(def->st_info) != STB_WEAK) 2447 break; 2448 } 2449 } 2450 } 2451 /* 2452 * Search the dynamic linker itself, and possibly resolve the 2453 * symbol from there. This is how the application links to 2454 * dynamic linker services such as dlopen. 2455 */ 2456 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) { 2457 res = symlook_obj(&req, &obj_rtld); 2458 if (res == 0) { 2459 def = req.sym_out; 2460 defobj = req.defobj_out; 2461 } 2462 } 2463 } else { 2464 assert(handle == RTLD_DEFAULT); 2465 res = symlook_default(&req, obj); 2466 if (res == 0) { 2467 defobj = req.defobj_out; 2468 def = req.sym_out; 2469 } 2470 } 2471 } else { 2472 if ((obj = dlcheck(handle)) == NULL) { 2473 lock_release(rtld_bind_lock, &lockstate); 2474 return NULL; 2475 } 2476 2477 donelist_init(&donelist); 2478 if (obj->mainprog) { 2479 /* Handle obtained by dlopen(NULL, ...) implies global scope. */ 2480 res = symlook_global(&req, &donelist); 2481 if (res == 0) { 2482 def = req.sym_out; 2483 defobj = req.defobj_out; 2484 } 2485 /* 2486 * Search the dynamic linker itself, and possibly resolve the 2487 * symbol from there. This is how the application links to 2488 * dynamic linker services such as dlopen. 2489 */ 2490 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) { 2491 res = symlook_obj(&req, &obj_rtld); 2492 if (res == 0) { 2493 def = req.sym_out; 2494 defobj = req.defobj_out; 2495 } 2496 } 2497 } 2498 else { 2499 /* Search the whole DAG rooted at the given object. */ 2500 res = symlook_list(&req, &obj->dagmembers, &donelist); 2501 if (res == 0) { 2502 def = req.sym_out; 2503 defobj = req.defobj_out; 2504 } 2505 } 2506 } 2507 2508 if (def != NULL) { 2509 lock_release(rtld_bind_lock, &lockstate); 2510 2511 /* 2512 * The value required by the caller is derived from the value 2513 * of the symbol. For the ia64 architecture, we need to 2514 * construct a function descriptor which the caller can use to 2515 * call the function with the right 'gp' value. For other 2516 * architectures and for non-functions, the value is simply 2517 * the relocated value of the symbol. 2518 */ 2519 if (ELF_ST_TYPE(def->st_info) == STT_FUNC) 2520 return (make_function_pointer(def, defobj)); 2521 else if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) 2522 return (rtld_resolve_ifunc(defobj, def)); 2523 else 2524 return (defobj->relocbase + def->st_value); 2525 } 2526 2527 _rtld_error("Undefined symbol \"%s\"", name); 2528 lock_release(rtld_bind_lock, &lockstate); 2529 return NULL; 2530 } 2531 2532 void * 2533 dlsym(void *handle, const char *name) 2534 { 2535 return do_dlsym(handle, name, __builtin_return_address(0), NULL, 2536 SYMLOOK_DLSYM); 2537 } 2538 2539 dlfunc_t 2540 dlfunc(void *handle, const char *name) 2541 { 2542 union { 2543 void *d; 2544 dlfunc_t f; 2545 } rv; 2546 2547 rv.d = do_dlsym(handle, name, __builtin_return_address(0), NULL, 2548 SYMLOOK_DLSYM); 2549 return (rv.f); 2550 } 2551 2552 void * 2553 dlvsym(void *handle, const char *name, const char *version) 2554 { 2555 Ver_Entry ventry; 2556 2557 ventry.name = version; 2558 ventry.file = NULL; 2559 ventry.hash = elf_hash(version); 2560 ventry.flags= 0; 2561 return do_dlsym(handle, name, __builtin_return_address(0), &ventry, 2562 SYMLOOK_DLSYM); 2563 } 2564 2565 int 2566 _rtld_addr_phdr(const void *addr, struct dl_phdr_info *phdr_info) 2567 { 2568 const Obj_Entry *obj; 2569 RtldLockState lockstate; 2570 2571 rlock_acquire(rtld_bind_lock, &lockstate); 2572 obj = obj_from_addr(addr); 2573 if (obj == NULL) { 2574 _rtld_error("No shared object contains address"); 2575 lock_release(rtld_bind_lock, &lockstate); 2576 return (0); 2577 } 2578 rtld_fill_dl_phdr_info(obj, phdr_info); 2579 lock_release(rtld_bind_lock, &lockstate); 2580 return (1); 2581 } 2582 2583 int 2584 dladdr(const void *addr, Dl_info *info) 2585 { 2586 const Obj_Entry *obj; 2587 const Elf_Sym *def; 2588 void *symbol_addr; 2589 unsigned long symoffset; 2590 RtldLockState lockstate; 2591 2592 rlock_acquire(rtld_bind_lock, &lockstate); 2593 obj = obj_from_addr(addr); 2594 if (obj == NULL) { 2595 _rtld_error("No shared object contains address"); 2596 lock_release(rtld_bind_lock, &lockstate); 2597 return 0; 2598 } 2599 info->dli_fname = obj->path; 2600 info->dli_fbase = obj->mapbase; 2601 info->dli_saddr = (void *)0; 2602 info->dli_sname = NULL; 2603 2604 /* 2605 * Walk the symbol list looking for the symbol whose address is 2606 * closest to the address sent in. 2607 */ 2608 for (symoffset = 0; symoffset < obj->nchains; symoffset++) { 2609 def = obj->symtab + symoffset; 2610 2611 /* 2612 * For skip the symbol if st_shndx is either SHN_UNDEF or 2613 * SHN_COMMON. 2614 */ 2615 if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON) 2616 continue; 2617 2618 /* 2619 * If the symbol is greater than the specified address, or if it 2620 * is further away from addr than the current nearest symbol, 2621 * then reject it. 2622 */ 2623 symbol_addr = obj->relocbase + def->st_value; 2624 if (symbol_addr > addr || symbol_addr < info->dli_saddr) 2625 continue; 2626 2627 /* Update our idea of the nearest symbol. */ 2628 info->dli_sname = obj->strtab + def->st_name; 2629 info->dli_saddr = symbol_addr; 2630 2631 /* Exact match? */ 2632 if (info->dli_saddr == addr) 2633 break; 2634 } 2635 lock_release(rtld_bind_lock, &lockstate); 2636 return 1; 2637 } 2638 2639 int 2640 dlinfo(void *handle, int request, void *p) 2641 { 2642 const Obj_Entry *obj; 2643 RtldLockState lockstate; 2644 int error; 2645 2646 rlock_acquire(rtld_bind_lock, &lockstate); 2647 2648 if (handle == NULL || handle == RTLD_SELF) { 2649 void *retaddr; 2650 2651 retaddr = __builtin_return_address(0); /* __GNUC__ only */ 2652 if ((obj = obj_from_addr(retaddr)) == NULL) 2653 _rtld_error("Cannot determine caller's shared object"); 2654 } else 2655 obj = dlcheck(handle); 2656 2657 if (obj == NULL) { 2658 lock_release(rtld_bind_lock, &lockstate); 2659 return (-1); 2660 } 2661 2662 error = 0; 2663 switch (request) { 2664 case RTLD_DI_LINKMAP: 2665 *((struct link_map const **)p) = &obj->linkmap; 2666 break; 2667 case RTLD_DI_ORIGIN: 2668 error = rtld_dirname(obj->path, p); 2669 break; 2670 2671 case RTLD_DI_SERINFOSIZE: 2672 case RTLD_DI_SERINFO: 2673 error = do_search_info(obj, request, (struct dl_serinfo *)p); 2674 break; 2675 2676 default: 2677 _rtld_error("Invalid request %d passed to dlinfo()", request); 2678 error = -1; 2679 } 2680 2681 lock_release(rtld_bind_lock, &lockstate); 2682 2683 return (error); 2684 } 2685 2686 static void 2687 rtld_fill_dl_phdr_info(const Obj_Entry *obj, struct dl_phdr_info *phdr_info) 2688 { 2689 2690 phdr_info->dlpi_addr = (Elf_Addr)obj->relocbase; 2691 phdr_info->dlpi_name = STAILQ_FIRST(&obj->names) ? 2692 STAILQ_FIRST(&obj->names)->name : obj->path; 2693 phdr_info->dlpi_phdr = obj->phdr; 2694 phdr_info->dlpi_phnum = obj->phsize / sizeof(obj->phdr[0]); 2695 phdr_info->dlpi_tls_modid = obj->tlsindex; 2696 phdr_info->dlpi_tls_data = obj->tlsinit; 2697 phdr_info->dlpi_adds = obj_loads; 2698 phdr_info->dlpi_subs = obj_loads - obj_count; 2699 } 2700 2701 int 2702 dl_iterate_phdr(__dl_iterate_hdr_callback callback, void *param) 2703 { 2704 struct dl_phdr_info phdr_info; 2705 const Obj_Entry *obj; 2706 RtldLockState bind_lockstate, phdr_lockstate; 2707 int error; 2708 2709 wlock_acquire(rtld_phdr_lock, &phdr_lockstate); 2710 rlock_acquire(rtld_bind_lock, &bind_lockstate); 2711 2712 error = 0; 2713 2714 for (obj = obj_list; obj != NULL; obj = obj->next) { 2715 rtld_fill_dl_phdr_info(obj, &phdr_info); 2716 if ((error = callback(&phdr_info, sizeof phdr_info, param)) != 0) 2717 break; 2718 2719 } 2720 lock_release(rtld_bind_lock, &bind_lockstate); 2721 lock_release(rtld_phdr_lock, &phdr_lockstate); 2722 2723 return (error); 2724 } 2725 2726 struct fill_search_info_args { 2727 int request; 2728 unsigned int flags; 2729 Dl_serinfo *serinfo; 2730 Dl_serpath *serpath; 2731 char *strspace; 2732 }; 2733 2734 static void * 2735 fill_search_info(const char *dir, size_t dirlen, void *param) 2736 { 2737 struct fill_search_info_args *arg; 2738 2739 arg = param; 2740 2741 if (arg->request == RTLD_DI_SERINFOSIZE) { 2742 arg->serinfo->dls_cnt ++; 2743 arg->serinfo->dls_size += sizeof(Dl_serpath) + dirlen + 1; 2744 } else { 2745 struct dl_serpath *s_entry; 2746 2747 s_entry = arg->serpath; 2748 s_entry->dls_name = arg->strspace; 2749 s_entry->dls_flags = arg->flags; 2750 2751 strncpy(arg->strspace, dir, dirlen); 2752 arg->strspace[dirlen] = '\0'; 2753 2754 arg->strspace += dirlen + 1; 2755 arg->serpath++; 2756 } 2757 2758 return (NULL); 2759 } 2760 2761 static int 2762 do_search_info(const Obj_Entry *obj, int request, struct dl_serinfo *info) 2763 { 2764 struct dl_serinfo _info; 2765 struct fill_search_info_args args; 2766 2767 args.request = RTLD_DI_SERINFOSIZE; 2768 args.serinfo = &_info; 2769 2770 _info.dls_size = __offsetof(struct dl_serinfo, dls_serpath); 2771 _info.dls_cnt = 0; 2772 2773 path_enumerate(ld_library_path, fill_search_info, &args); 2774 path_enumerate(obj->rpath, fill_search_info, &args); 2775 path_enumerate(gethints(), fill_search_info, &args); 2776 path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args); 2777 2778 2779 if (request == RTLD_DI_SERINFOSIZE) { 2780 info->dls_size = _info.dls_size; 2781 info->dls_cnt = _info.dls_cnt; 2782 return (0); 2783 } 2784 2785 if (info->dls_cnt != _info.dls_cnt || info->dls_size != _info.dls_size) { 2786 _rtld_error("Uninitialized Dl_serinfo struct passed to dlinfo()"); 2787 return (-1); 2788 } 2789 2790 args.request = RTLD_DI_SERINFO; 2791 args.serinfo = info; 2792 args.serpath = &info->dls_serpath[0]; 2793 args.strspace = (char *)&info->dls_serpath[_info.dls_cnt]; 2794 2795 args.flags = LA_SER_LIBPATH; 2796 if (path_enumerate(ld_library_path, fill_search_info, &args) != NULL) 2797 return (-1); 2798 2799 args.flags = LA_SER_RUNPATH; 2800 if (path_enumerate(obj->rpath, fill_search_info, &args) != NULL) 2801 return (-1); 2802 2803 args.flags = LA_SER_CONFIG; 2804 if (path_enumerate(gethints(), fill_search_info, &args) != NULL) 2805 return (-1); 2806 2807 args.flags = LA_SER_DEFAULT; 2808 if (path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args) != NULL) 2809 return (-1); 2810 return (0); 2811 } 2812 2813 static int 2814 rtld_dirname(const char *path, char *bname) 2815 { 2816 const char *endp; 2817 2818 /* Empty or NULL string gets treated as "." */ 2819 if (path == NULL || *path == '\0') { 2820 bname[0] = '.'; 2821 bname[1] = '\0'; 2822 return (0); 2823 } 2824 2825 /* Strip trailing slashes */ 2826 endp = path + strlen(path) - 1; 2827 while (endp > path && *endp == '/') 2828 endp--; 2829 2830 /* Find the start of the dir */ 2831 while (endp > path && *endp != '/') 2832 endp--; 2833 2834 /* Either the dir is "/" or there are no slashes */ 2835 if (endp == path) { 2836 bname[0] = *endp == '/' ? '/' : '.'; 2837 bname[1] = '\0'; 2838 return (0); 2839 } else { 2840 do { 2841 endp--; 2842 } while (endp > path && *endp == '/'); 2843 } 2844 2845 if (endp - path + 2 > PATH_MAX) 2846 { 2847 _rtld_error("Filename is too long: %s", path); 2848 return(-1); 2849 } 2850 2851 strncpy(bname, path, endp - path + 1); 2852 bname[endp - path + 1] = '\0'; 2853 return (0); 2854 } 2855 2856 static int 2857 rtld_dirname_abs(const char *path, char *base) 2858 { 2859 char base_rel[PATH_MAX]; 2860 2861 if (rtld_dirname(path, base) == -1) 2862 return (-1); 2863 if (base[0] == '/') 2864 return (0); 2865 if (getcwd(base_rel, sizeof(base_rel)) == NULL || 2866 strlcat(base_rel, "/", sizeof(base_rel)) >= sizeof(base_rel) || 2867 strlcat(base_rel, base, sizeof(base_rel)) >= sizeof(base_rel)) 2868 return (-1); 2869 strcpy(base, base_rel); 2870 return (0); 2871 } 2872 2873 static void 2874 linkmap_add(Obj_Entry *obj) 2875 { 2876 struct link_map *l = &obj->linkmap; 2877 struct link_map *prev; 2878 2879 obj->linkmap.l_name = obj->path; 2880 obj->linkmap.l_addr = obj->mapbase; 2881 obj->linkmap.l_ld = obj->dynamic; 2882 #ifdef __mips__ 2883 /* GDB needs load offset on MIPS to use the symbols */ 2884 obj->linkmap.l_offs = obj->relocbase; 2885 #endif 2886 2887 if (r_debug.r_map == NULL) { 2888 r_debug.r_map = l; 2889 return; 2890 } 2891 2892 /* 2893 * Scan to the end of the list, but not past the entry for the 2894 * dynamic linker, which we want to keep at the very end. 2895 */ 2896 for (prev = r_debug.r_map; 2897 prev->l_next != NULL && prev->l_next != &obj_rtld.linkmap; 2898 prev = prev->l_next) 2899 ; 2900 2901 /* Link in the new entry. */ 2902 l->l_prev = prev; 2903 l->l_next = prev->l_next; 2904 if (l->l_next != NULL) 2905 l->l_next->l_prev = l; 2906 prev->l_next = l; 2907 } 2908 2909 static void 2910 linkmap_delete(Obj_Entry *obj) 2911 { 2912 struct link_map *l = &obj->linkmap; 2913 2914 if (l->l_prev == NULL) { 2915 if ((r_debug.r_map = l->l_next) != NULL) 2916 l->l_next->l_prev = NULL; 2917 return; 2918 } 2919 2920 if ((l->l_prev->l_next = l->l_next) != NULL) 2921 l->l_next->l_prev = l->l_prev; 2922 } 2923 2924 /* 2925 * Function for the debugger to set a breakpoint on to gain control. 2926 * 2927 * The two parameters allow the debugger to easily find and determine 2928 * what the runtime loader is doing and to whom it is doing it. 2929 * 2930 * When the loadhook trap is hit (r_debug_state, set at program 2931 * initialization), the arguments can be found on the stack: 2932 * 2933 * +8 struct link_map *m 2934 * +4 struct r_debug *rd 2935 * +0 RetAddr 2936 */ 2937 void 2938 r_debug_state(struct r_debug* rd, struct link_map *m) 2939 { 2940 /* 2941 * The following is a hack to force the compiler to emit calls to 2942 * this function, even when optimizing. If the function is empty, 2943 * the compiler is not obliged to emit any code for calls to it, 2944 * even when marked __noinline. However, gdb depends on those 2945 * calls being made. 2946 */ 2947 __asm __volatile("" : : : "memory"); 2948 } 2949 2950 /* 2951 * Get address of the pointer variable in the main program. 2952 * Prefer non-weak symbol over the weak one. 2953 */ 2954 static const void ** 2955 get_program_var_addr(const char *name, RtldLockState *lockstate) 2956 { 2957 SymLook req; 2958 DoneList donelist; 2959 2960 symlook_init(&req, name); 2961 req.lockstate = lockstate; 2962 donelist_init(&donelist); 2963 if (symlook_global(&req, &donelist) != 0) 2964 return (NULL); 2965 if (ELF_ST_TYPE(req.sym_out->st_info) == STT_FUNC) 2966 return ((const void **)make_function_pointer(req.sym_out, 2967 req.defobj_out)); 2968 else if (ELF_ST_TYPE(req.sym_out->st_info) == STT_GNU_IFUNC) 2969 return ((const void **)rtld_resolve_ifunc(req.defobj_out, req.sym_out)); 2970 else 2971 return ((const void **)(req.defobj_out->relocbase + 2972 req.sym_out->st_value)); 2973 } 2974 2975 /* 2976 * Set a pointer variable in the main program to the given value. This 2977 * is used to set key variables such as "environ" before any of the 2978 * init functions are called. 2979 */ 2980 static void 2981 set_program_var(const char *name, const void *value) 2982 { 2983 const void **addr; 2984 2985 if ((addr = get_program_var_addr(name, NULL)) != NULL) { 2986 dbg("\"%s\": *%p <-- %p", name, addr, value); 2987 *addr = value; 2988 } 2989 } 2990 2991 /* 2992 * Search the global objects, including dependencies and main object, 2993 * for the given symbol. 2994 */ 2995 static int 2996 symlook_global(SymLook *req, DoneList *donelist) 2997 { 2998 SymLook req1; 2999 const Objlist_Entry *elm; 3000 int res; 3001 3002 symlook_init_from_req(&req1, req); 3003 3004 /* Search all objects loaded at program start up. */ 3005 if (req->defobj_out == NULL || 3006 ELF_ST_BIND(req->sym_out->st_info) == STB_WEAK) { 3007 res = symlook_list(&req1, &list_main, donelist); 3008 if (res == 0 && (req->defobj_out == NULL || 3009 ELF_ST_BIND(req1.sym_out->st_info) != STB_WEAK)) { 3010 req->sym_out = req1.sym_out; 3011 req->defobj_out = req1.defobj_out; 3012 assert(req->defobj_out != NULL); 3013 } 3014 } 3015 3016 /* Search all DAGs whose roots are RTLD_GLOBAL objects. */ 3017 STAILQ_FOREACH(elm, &list_global, link) { 3018 if (req->defobj_out != NULL && 3019 ELF_ST_BIND(req->sym_out->st_info) != STB_WEAK) 3020 break; 3021 res = symlook_list(&req1, &elm->obj->dagmembers, donelist); 3022 if (res == 0 && (req->defobj_out == NULL || 3023 ELF_ST_BIND(req1.sym_out->st_info) != STB_WEAK)) { 3024 req->sym_out = req1.sym_out; 3025 req->defobj_out = req1.defobj_out; 3026 assert(req->defobj_out != NULL); 3027 } 3028 } 3029 3030 return (req->sym_out != NULL ? 0 : ESRCH); 3031 } 3032 3033 /* 3034 * Given a symbol name in a referencing object, find the corresponding 3035 * definition of the symbol. Returns a pointer to the symbol, or NULL if 3036 * no definition was found. Returns a pointer to the Obj_Entry of the 3037 * defining object via the reference parameter DEFOBJ_OUT. 3038 */ 3039 static int 3040 symlook_default(SymLook *req, const Obj_Entry *refobj) 3041 { 3042 DoneList donelist; 3043 const Objlist_Entry *elm; 3044 SymLook req1; 3045 int res; 3046 3047 donelist_init(&donelist); 3048 symlook_init_from_req(&req1, req); 3049 3050 /* Look first in the referencing object if linked symbolically. */ 3051 if (refobj->symbolic && !donelist_check(&donelist, refobj)) { 3052 res = symlook_obj(&req1, refobj); 3053 if (res == 0) { 3054 req->sym_out = req1.sym_out; 3055 req->defobj_out = req1.defobj_out; 3056 assert(req->defobj_out != NULL); 3057 } 3058 } 3059 3060 symlook_global(req, &donelist); 3061 3062 /* Search all dlopened DAGs containing the referencing object. */ 3063 STAILQ_FOREACH(elm, &refobj->dldags, link) { 3064 if (req->sym_out != NULL && 3065 ELF_ST_BIND(req->sym_out->st_info) != STB_WEAK) 3066 break; 3067 res = symlook_list(&req1, &elm->obj->dagmembers, &donelist); 3068 if (res == 0 && (req->sym_out == NULL || 3069 ELF_ST_BIND(req1.sym_out->st_info) != STB_WEAK)) { 3070 req->sym_out = req1.sym_out; 3071 req->defobj_out = req1.defobj_out; 3072 assert(req->defobj_out != NULL); 3073 } 3074 } 3075 3076 /* 3077 * Search the dynamic linker itself, and possibly resolve the 3078 * symbol from there. This is how the application links to 3079 * dynamic linker services such as dlopen. 3080 */ 3081 if (req->sym_out == NULL || 3082 ELF_ST_BIND(req->sym_out->st_info) == STB_WEAK) { 3083 res = symlook_obj(&req1, &obj_rtld); 3084 if (res == 0) { 3085 req->sym_out = req1.sym_out; 3086 req->defobj_out = req1.defobj_out; 3087 assert(req->defobj_out != NULL); 3088 } 3089 } 3090 3091 return (req->sym_out != NULL ? 0 : ESRCH); 3092 } 3093 3094 static int 3095 symlook_list(SymLook *req, const Objlist *objlist, DoneList *dlp) 3096 { 3097 const Elf_Sym *def; 3098 const Obj_Entry *defobj; 3099 const Objlist_Entry *elm; 3100 SymLook req1; 3101 int res; 3102 3103 def = NULL; 3104 defobj = NULL; 3105 STAILQ_FOREACH(elm, objlist, link) { 3106 if (donelist_check(dlp, elm->obj)) 3107 continue; 3108 symlook_init_from_req(&req1, req); 3109 if ((res = symlook_obj(&req1, elm->obj)) == 0) { 3110 if (def == NULL || ELF_ST_BIND(req1.sym_out->st_info) != STB_WEAK) { 3111 def = req1.sym_out; 3112 defobj = req1.defobj_out; 3113 if (ELF_ST_BIND(def->st_info) != STB_WEAK) 3114 break; 3115 } 3116 } 3117 } 3118 if (def != NULL) { 3119 req->sym_out = def; 3120 req->defobj_out = defobj; 3121 return (0); 3122 } 3123 return (ESRCH); 3124 } 3125 3126 /* 3127 * Search the chain of DAGS cointed to by the given Needed_Entry 3128 * for a symbol of the given name. Each DAG is scanned completely 3129 * before advancing to the next one. Returns a pointer to the symbol, 3130 * or NULL if no definition was found. 3131 */ 3132 static int 3133 symlook_needed(SymLook *req, const Needed_Entry *needed, DoneList *dlp) 3134 { 3135 const Elf_Sym *def; 3136 const Needed_Entry *n; 3137 const Obj_Entry *defobj; 3138 SymLook req1; 3139 int res; 3140 3141 def = NULL; 3142 defobj = NULL; 3143 symlook_init_from_req(&req1, req); 3144 for (n = needed; n != NULL; n = n->next) { 3145 if (n->obj == NULL || 3146 (res = symlook_list(&req1, &n->obj->dagmembers, dlp)) != 0) 3147 continue; 3148 if (def == NULL || ELF_ST_BIND(req1.sym_out->st_info) != STB_WEAK) { 3149 def = req1.sym_out; 3150 defobj = req1.defobj_out; 3151 if (ELF_ST_BIND(def->st_info) != STB_WEAK) 3152 break; 3153 } 3154 } 3155 if (def != NULL) { 3156 req->sym_out = def; 3157 req->defobj_out = defobj; 3158 return (0); 3159 } 3160 return (ESRCH); 3161 } 3162 3163 /* 3164 * Search the symbol table of a single shared object for a symbol of 3165 * the given name and version, if requested. Returns a pointer to the 3166 * symbol, or NULL if no definition was found. If the object is 3167 * filter, return filtered symbol from filtee. 3168 * 3169 * The symbol's hash value is passed in for efficiency reasons; that 3170 * eliminates many recomputations of the hash value. 3171 */ 3172 int 3173 symlook_obj(SymLook *req, const Obj_Entry *obj) 3174 { 3175 DoneList donelist; 3176 SymLook req1; 3177 int res, mres; 3178 3179 mres = symlook_obj1(req, obj); 3180 if (mres == 0) { 3181 if (obj->needed_filtees != NULL) { 3182 load_filtees(__DECONST(Obj_Entry *, obj), 0, req->lockstate); 3183 donelist_init(&donelist); 3184 symlook_init_from_req(&req1, req); 3185 res = symlook_needed(&req1, obj->needed_filtees, &donelist); 3186 if (res == 0) { 3187 req->sym_out = req1.sym_out; 3188 req->defobj_out = req1.defobj_out; 3189 } 3190 return (res); 3191 } 3192 if (obj->needed_aux_filtees != NULL) { 3193 load_filtees(__DECONST(Obj_Entry *, obj), 0, req->lockstate); 3194 donelist_init(&donelist); 3195 symlook_init_from_req(&req1, req); 3196 res = symlook_needed(&req1, obj->needed_aux_filtees, &donelist); 3197 if (res == 0) { 3198 req->sym_out = req1.sym_out; 3199 req->defobj_out = req1.defobj_out; 3200 return (res); 3201 } 3202 } 3203 } 3204 return (mres); 3205 } 3206 3207 static int 3208 symlook_obj1(SymLook *req, const Obj_Entry *obj) 3209 { 3210 unsigned long symnum; 3211 const Elf_Sym *vsymp; 3212 Elf_Versym verndx; 3213 int vcount; 3214 3215 if (obj->buckets == NULL) 3216 return (ESRCH); 3217 3218 vsymp = NULL; 3219 vcount = 0; 3220 symnum = obj->buckets[req->hash % obj->nbuckets]; 3221 3222 for (; symnum != STN_UNDEF; symnum = obj->chains[symnum]) { 3223 const Elf_Sym *symp; 3224 const char *strp; 3225 3226 if (symnum >= obj->nchains) 3227 return (ESRCH); /* Bad object */ 3228 3229 symp = obj->symtab + symnum; 3230 strp = obj->strtab + symp->st_name; 3231 3232 switch (ELF_ST_TYPE(symp->st_info)) { 3233 case STT_FUNC: 3234 case STT_NOTYPE: 3235 case STT_OBJECT: 3236 case STT_GNU_IFUNC: 3237 if (symp->st_value == 0) 3238 continue; 3239 /* fallthrough */ 3240 case STT_TLS: 3241 if (symp->st_shndx != SHN_UNDEF) 3242 break; 3243 #ifndef __mips__ 3244 else if (((req->flags & SYMLOOK_IN_PLT) == 0) && 3245 (ELF_ST_TYPE(symp->st_info) == STT_FUNC)) 3246 break; 3247 /* fallthrough */ 3248 #endif 3249 default: 3250 continue; 3251 } 3252 if (req->name[0] != strp[0] || strcmp(req->name, strp) != 0) 3253 continue; 3254 3255 if (req->ventry == NULL) { 3256 if (obj->versyms != NULL) { 3257 verndx = VER_NDX(obj->versyms[symnum]); 3258 if (verndx > obj->vernum) { 3259 _rtld_error("%s: symbol %s references wrong version %d", 3260 obj->path, obj->strtab + symnum, verndx); 3261 continue; 3262 } 3263 /* 3264 * If we are not called from dlsym (i.e. this is a normal 3265 * relocation from unversioned binary), accept the symbol 3266 * immediately if it happens to have first version after 3267 * this shared object became versioned. Otherwise, if 3268 * symbol is versioned and not hidden, remember it. If it 3269 * is the only symbol with this name exported by the 3270 * shared object, it will be returned as a match at the 3271 * end of the function. If symbol is global (verndx < 2) 3272 * accept it unconditionally. 3273 */ 3274 if ((req->flags & SYMLOOK_DLSYM) == 0 && 3275 verndx == VER_NDX_GIVEN) { 3276 req->sym_out = symp; 3277 req->defobj_out = obj; 3278 return (0); 3279 } 3280 else if (verndx >= VER_NDX_GIVEN) { 3281 if ((obj->versyms[symnum] & VER_NDX_HIDDEN) == 0) { 3282 if (vsymp == NULL) 3283 vsymp = symp; 3284 vcount ++; 3285 } 3286 continue; 3287 } 3288 } 3289 req->sym_out = symp; 3290 req->defobj_out = obj; 3291 return (0); 3292 } else { 3293 if (obj->versyms == NULL) { 3294 if (object_match_name(obj, req->ventry->name)) { 3295 _rtld_error("%s: object %s should provide version %s for " 3296 "symbol %s", obj_rtld.path, obj->path, 3297 req->ventry->name, obj->strtab + symnum); 3298 continue; 3299 } 3300 } else { 3301 verndx = VER_NDX(obj->versyms[symnum]); 3302 if (verndx > obj->vernum) { 3303 _rtld_error("%s: symbol %s references wrong version %d", 3304 obj->path, obj->strtab + symnum, verndx); 3305 continue; 3306 } 3307 if (obj->vertab[verndx].hash != req->ventry->hash || 3308 strcmp(obj->vertab[verndx].name, req->ventry->name)) { 3309 /* 3310 * Version does not match. Look if this is a global symbol 3311 * and if it is not hidden. If global symbol (verndx < 2) 3312 * is available, use it. Do not return symbol if we are 3313 * called by dlvsym, because dlvsym looks for a specific 3314 * version and default one is not what dlvsym wants. 3315 */ 3316 if ((req->flags & SYMLOOK_DLSYM) || 3317 (obj->versyms[symnum] & VER_NDX_HIDDEN) || 3318 (verndx >= VER_NDX_GIVEN)) 3319 continue; 3320 } 3321 } 3322 req->sym_out = symp; 3323 req->defobj_out = obj; 3324 return (0); 3325 } 3326 } 3327 if (vcount == 1) { 3328 req->sym_out = vsymp; 3329 req->defobj_out = obj; 3330 return (0); 3331 } 3332 return (ESRCH); 3333 } 3334 3335 static void 3336 trace_loaded_objects(Obj_Entry *obj) 3337 { 3338 char *fmt1, *fmt2, *fmt, *main_local, *list_containers; 3339 int c; 3340 3341 if ((main_local = getenv(LD_ "TRACE_LOADED_OBJECTS_PROGNAME")) == NULL) 3342 main_local = ""; 3343 3344 if ((fmt1 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT1")) == NULL) 3345 fmt1 = "\t%o => %p (%x)\n"; 3346 3347 if ((fmt2 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT2")) == NULL) 3348 fmt2 = "\t%o (%x)\n"; 3349 3350 list_containers = getenv(LD_ "TRACE_LOADED_OBJECTS_ALL"); 3351 3352 for (; obj; obj = obj->next) { 3353 Needed_Entry *needed; 3354 char *name, *path; 3355 bool is_lib; 3356 3357 if (list_containers && obj->needed != NULL) 3358 rtld_printf("%s:\n", obj->path); 3359 for (needed = obj->needed; needed; needed = needed->next) { 3360 if (needed->obj != NULL) { 3361 if (needed->obj->traced && !list_containers) 3362 continue; 3363 needed->obj->traced = true; 3364 path = needed->obj->path; 3365 } else 3366 path = "not found"; 3367 3368 name = (char *)obj->strtab + needed->name; 3369 is_lib = strncmp(name, "lib", 3) == 0; /* XXX - bogus */ 3370 3371 fmt = is_lib ? fmt1 : fmt2; 3372 while ((c = *fmt++) != '\0') { 3373 switch (c) { 3374 default: 3375 rtld_putchar(c); 3376 continue; 3377 case '\\': 3378 switch (c = *fmt) { 3379 case '\0': 3380 continue; 3381 case 'n': 3382 rtld_putchar('\n'); 3383 break; 3384 case 't': 3385 rtld_putchar('\t'); 3386 break; 3387 } 3388 break; 3389 case '%': 3390 switch (c = *fmt) { 3391 case '\0': 3392 continue; 3393 case '%': 3394 default: 3395 rtld_putchar(c); 3396 break; 3397 case 'A': 3398 rtld_putstr(main_local); 3399 break; 3400 case 'a': 3401 rtld_putstr(obj_main->path); 3402 break; 3403 case 'o': 3404 rtld_putstr(name); 3405 break; 3406 #if 0 3407 case 'm': 3408 rtld_printf("%d", sodp->sod_major); 3409 break; 3410 case 'n': 3411 rtld_printf("%d", sodp->sod_minor); 3412 break; 3413 #endif 3414 case 'p': 3415 rtld_putstr(path); 3416 break; 3417 case 'x': 3418 rtld_printf("%p", needed->obj ? needed->obj->mapbase : 3419 0); 3420 break; 3421 } 3422 break; 3423 } 3424 ++fmt; 3425 } 3426 } 3427 } 3428 } 3429 3430 /* 3431 * Unload a dlopened object and its dependencies from memory and from 3432 * our data structures. It is assumed that the DAG rooted in the 3433 * object has already been unreferenced, and that the object has a 3434 * reference count of 0. 3435 */ 3436 static void 3437 unload_object(Obj_Entry *root) 3438 { 3439 Obj_Entry *obj; 3440 Obj_Entry **linkp; 3441 3442 assert(root->refcount == 0); 3443 3444 /* 3445 * Pass over the DAG removing unreferenced objects from 3446 * appropriate lists. 3447 */ 3448 unlink_object(root); 3449 3450 /* Unmap all objects that are no longer referenced. */ 3451 linkp = &obj_list->next; 3452 while ((obj = *linkp) != NULL) { 3453 if (obj->refcount == 0) { 3454 LD_UTRACE(UTRACE_UNLOAD_OBJECT, obj, obj->mapbase, obj->mapsize, 0, 3455 obj->path); 3456 dbg("unloading \"%s\"", obj->path); 3457 unload_filtees(root); 3458 munmap(obj->mapbase, obj->mapsize); 3459 linkmap_delete(obj); 3460 *linkp = obj->next; 3461 obj_count--; 3462 obj_free(obj); 3463 } else 3464 linkp = &obj->next; 3465 } 3466 obj_tail = linkp; 3467 } 3468 3469 static void 3470 unlink_object(Obj_Entry *root) 3471 { 3472 Objlist_Entry *elm; 3473 3474 if (root->refcount == 0) { 3475 /* Remove the object from the RTLD_GLOBAL list. */ 3476 objlist_remove(&list_global, root); 3477 3478 /* Remove the object from all objects' DAG lists. */ 3479 STAILQ_FOREACH(elm, &root->dagmembers, link) { 3480 objlist_remove(&elm->obj->dldags, root); 3481 if (elm->obj != root) 3482 unlink_object(elm->obj); 3483 } 3484 } 3485 } 3486 3487 static void 3488 ref_dag(Obj_Entry *root) 3489 { 3490 Objlist_Entry *elm; 3491 3492 assert(root->dag_inited); 3493 STAILQ_FOREACH(elm, &root->dagmembers, link) 3494 elm->obj->refcount++; 3495 } 3496 3497 static void 3498 unref_dag(Obj_Entry *root) 3499 { 3500 Objlist_Entry *elm; 3501 3502 assert(root->dag_inited); 3503 STAILQ_FOREACH(elm, &root->dagmembers, link) 3504 elm->obj->refcount--; 3505 } 3506 3507 /* 3508 * Common code for MD __tls_get_addr(). 3509 */ 3510 void * 3511 tls_get_addr_common(Elf_Addr** dtvp, int index, size_t offset) 3512 { 3513 Elf_Addr* dtv = *dtvp; 3514 RtldLockState lockstate; 3515 3516 /* Check dtv generation in case new modules have arrived */ 3517 if (dtv[0] != tls_dtv_generation) { 3518 Elf_Addr* newdtv; 3519 int to_copy; 3520 3521 wlock_acquire(rtld_bind_lock, &lockstate); 3522 newdtv = calloc(1, (tls_max_index + 2) * sizeof(Elf_Addr)); 3523 to_copy = dtv[1]; 3524 if (to_copy > tls_max_index) 3525 to_copy = tls_max_index; 3526 memcpy(&newdtv[2], &dtv[2], to_copy * sizeof(Elf_Addr)); 3527 newdtv[0] = tls_dtv_generation; 3528 newdtv[1] = tls_max_index; 3529 free(dtv); 3530 lock_release(rtld_bind_lock, &lockstate); 3531 dtv = *dtvp = newdtv; 3532 } 3533 3534 /* Dynamically allocate module TLS if necessary */ 3535 if (!dtv[index + 1]) { 3536 /* Signal safe, wlock will block out signals. */ 3537 wlock_acquire(rtld_bind_lock, &lockstate); 3538 if (!dtv[index + 1]) 3539 dtv[index + 1] = (Elf_Addr)allocate_module_tls(index); 3540 lock_release(rtld_bind_lock, &lockstate); 3541 } 3542 return (void*) (dtv[index + 1] + offset); 3543 } 3544 3545 #if defined(__arm__) || defined(__ia64__) || defined(__mips__) || defined(__powerpc__) 3546 3547 /* 3548 * Allocate Static TLS using the Variant I method. 3549 */ 3550 void * 3551 allocate_tls(Obj_Entry *objs, void *oldtcb, size_t tcbsize, size_t tcbalign) 3552 { 3553 Obj_Entry *obj; 3554 char *tcb; 3555 Elf_Addr **tls; 3556 Elf_Addr *dtv; 3557 Elf_Addr addr; 3558 int i; 3559 3560 if (oldtcb != NULL && tcbsize == TLS_TCB_SIZE) 3561 return (oldtcb); 3562 3563 assert(tcbsize >= TLS_TCB_SIZE); 3564 tcb = calloc(1, tls_static_space - TLS_TCB_SIZE + tcbsize); 3565 tls = (Elf_Addr **)(tcb + tcbsize - TLS_TCB_SIZE); 3566 3567 if (oldtcb != NULL) { 3568 memcpy(tls, oldtcb, tls_static_space); 3569 free(oldtcb); 3570 3571 /* Adjust the DTV. */ 3572 dtv = tls[0]; 3573 for (i = 0; i < dtv[1]; i++) { 3574 if (dtv[i+2] >= (Elf_Addr)oldtcb && 3575 dtv[i+2] < (Elf_Addr)oldtcb + tls_static_space) { 3576 dtv[i+2] = dtv[i+2] - (Elf_Addr)oldtcb + (Elf_Addr)tls; 3577 } 3578 } 3579 } else { 3580 dtv = calloc(tls_max_index + 2, sizeof(Elf_Addr)); 3581 tls[0] = dtv; 3582 dtv[0] = tls_dtv_generation; 3583 dtv[1] = tls_max_index; 3584 3585 for (obj = objs; obj; obj = obj->next) { 3586 if (obj->tlsoffset > 0) { 3587 addr = (Elf_Addr)tls + obj->tlsoffset; 3588 if (obj->tlsinitsize > 0) 3589 memcpy((void*) addr, obj->tlsinit, obj->tlsinitsize); 3590 if (obj->tlssize > obj->tlsinitsize) 3591 memset((void*) (addr + obj->tlsinitsize), 0, 3592 obj->tlssize - obj->tlsinitsize); 3593 dtv[obj->tlsindex + 1] = addr; 3594 } 3595 } 3596 } 3597 3598 return (tcb); 3599 } 3600 3601 void 3602 free_tls(void *tcb, size_t tcbsize, size_t tcbalign) 3603 { 3604 Elf_Addr *dtv; 3605 Elf_Addr tlsstart, tlsend; 3606 int dtvsize, i; 3607 3608 assert(tcbsize >= TLS_TCB_SIZE); 3609 3610 tlsstart = (Elf_Addr)tcb + tcbsize - TLS_TCB_SIZE; 3611 tlsend = tlsstart + tls_static_space; 3612 3613 dtv = *(Elf_Addr **)tlsstart; 3614 dtvsize = dtv[1]; 3615 for (i = 0; i < dtvsize; i++) { 3616 if (dtv[i+2] && (dtv[i+2] < tlsstart || dtv[i+2] >= tlsend)) { 3617 free((void*)dtv[i+2]); 3618 } 3619 } 3620 free(dtv); 3621 free(tcb); 3622 } 3623 3624 #endif 3625 3626 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) 3627 3628 /* 3629 * Allocate Static TLS using the Variant II method. 3630 */ 3631 void * 3632 allocate_tls(Obj_Entry *objs, void *oldtls, size_t tcbsize, size_t tcbalign) 3633 { 3634 Obj_Entry *obj; 3635 size_t size; 3636 char *tls; 3637 Elf_Addr *dtv, *olddtv; 3638 Elf_Addr segbase, oldsegbase, addr; 3639 int i; 3640 3641 size = round(tls_static_space, tcbalign); 3642 3643 assert(tcbsize >= 2*sizeof(Elf_Addr)); 3644 tls = calloc(1, size + tcbsize); 3645 dtv = calloc(1, (tls_max_index + 2) * sizeof(Elf_Addr)); 3646 3647 segbase = (Elf_Addr)(tls + size); 3648 ((Elf_Addr*)segbase)[0] = segbase; 3649 ((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv; 3650 3651 dtv[0] = tls_dtv_generation; 3652 dtv[1] = tls_max_index; 3653 3654 if (oldtls) { 3655 /* 3656 * Copy the static TLS block over whole. 3657 */ 3658 oldsegbase = (Elf_Addr) oldtls; 3659 memcpy((void *)(segbase - tls_static_space), 3660 (const void *)(oldsegbase - tls_static_space), 3661 tls_static_space); 3662 3663 /* 3664 * If any dynamic TLS blocks have been created tls_get_addr(), 3665 * move them over. 3666 */ 3667 olddtv = ((Elf_Addr**)oldsegbase)[1]; 3668 for (i = 0; i < olddtv[1]; i++) { 3669 if (olddtv[i+2] < oldsegbase - size || olddtv[i+2] > oldsegbase) { 3670 dtv[i+2] = olddtv[i+2]; 3671 olddtv[i+2] = 0; 3672 } 3673 } 3674 3675 /* 3676 * We assume that this block was the one we created with 3677 * allocate_initial_tls(). 3678 */ 3679 free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr)); 3680 } else { 3681 for (obj = objs; obj; obj = obj->next) { 3682 if (obj->tlsoffset) { 3683 addr = segbase - obj->tlsoffset; 3684 memset((void*) (addr + obj->tlsinitsize), 3685 0, obj->tlssize - obj->tlsinitsize); 3686 if (obj->tlsinit) 3687 memcpy((void*) addr, obj->tlsinit, obj->tlsinitsize); 3688 dtv[obj->tlsindex + 1] = addr; 3689 } 3690 } 3691 } 3692 3693 return (void*) segbase; 3694 } 3695 3696 void 3697 free_tls(void *tls, size_t tcbsize, size_t tcbalign) 3698 { 3699 size_t size; 3700 Elf_Addr* dtv; 3701 int dtvsize, i; 3702 Elf_Addr tlsstart, tlsend; 3703 3704 /* 3705 * Figure out the size of the initial TLS block so that we can 3706 * find stuff which ___tls_get_addr() allocated dynamically. 3707 */ 3708 size = round(tls_static_space, tcbalign); 3709 3710 dtv = ((Elf_Addr**)tls)[1]; 3711 dtvsize = dtv[1]; 3712 tlsend = (Elf_Addr) tls; 3713 tlsstart = tlsend - size; 3714 for (i = 0; i < dtvsize; i++) { 3715 if (dtv[i+2] && (dtv[i+2] < tlsstart || dtv[i+2] > tlsend)) { 3716 free((void*) dtv[i+2]); 3717 } 3718 } 3719 3720 free((void*) tlsstart); 3721 free((void*) dtv); 3722 } 3723 3724 #endif 3725 3726 /* 3727 * Allocate TLS block for module with given index. 3728 */ 3729 void * 3730 allocate_module_tls(int index) 3731 { 3732 Obj_Entry* obj; 3733 char* p; 3734 3735 for (obj = obj_list; obj; obj = obj->next) { 3736 if (obj->tlsindex == index) 3737 break; 3738 } 3739 if (!obj) { 3740 _rtld_error("Can't find module with TLS index %d", index); 3741 die(); 3742 } 3743 3744 p = malloc(obj->tlssize); 3745 if (p == NULL) { 3746 _rtld_error("Cannot allocate TLS block for index %d", index); 3747 die(); 3748 } 3749 memcpy(p, obj->tlsinit, obj->tlsinitsize); 3750 memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize); 3751 3752 return p; 3753 } 3754 3755 bool 3756 allocate_tls_offset(Obj_Entry *obj) 3757 { 3758 size_t off; 3759 3760 if (obj->tls_done) 3761 return true; 3762 3763 if (obj->tlssize == 0) { 3764 obj->tls_done = true; 3765 return true; 3766 } 3767 3768 if (obj->tlsindex == 1) 3769 off = calculate_first_tls_offset(obj->tlssize, obj->tlsalign); 3770 else 3771 off = calculate_tls_offset(tls_last_offset, tls_last_size, 3772 obj->tlssize, obj->tlsalign); 3773 3774 /* 3775 * If we have already fixed the size of the static TLS block, we 3776 * must stay within that size. When allocating the static TLS, we 3777 * leave a small amount of space spare to be used for dynamically 3778 * loading modules which use static TLS. 3779 */ 3780 if (tls_static_space) { 3781 if (calculate_tls_end(off, obj->tlssize) > tls_static_space) 3782 return false; 3783 } 3784 3785 tls_last_offset = obj->tlsoffset = off; 3786 tls_last_size = obj->tlssize; 3787 obj->tls_done = true; 3788 3789 return true; 3790 } 3791 3792 void 3793 free_tls_offset(Obj_Entry *obj) 3794 { 3795 3796 /* 3797 * If we were the last thing to allocate out of the static TLS 3798 * block, we give our space back to the 'allocator'. This is a 3799 * simplistic workaround to allow libGL.so.1 to be loaded and 3800 * unloaded multiple times. 3801 */ 3802 if (calculate_tls_end(obj->tlsoffset, obj->tlssize) 3803 == calculate_tls_end(tls_last_offset, tls_last_size)) { 3804 tls_last_offset -= obj->tlssize; 3805 tls_last_size = 0; 3806 } 3807 } 3808 3809 void * 3810 _rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign) 3811 { 3812 void *ret; 3813 RtldLockState lockstate; 3814 3815 wlock_acquire(rtld_bind_lock, &lockstate); 3816 ret = allocate_tls(obj_list, oldtls, tcbsize, tcbalign); 3817 lock_release(rtld_bind_lock, &lockstate); 3818 return (ret); 3819 } 3820 3821 void 3822 _rtld_free_tls(void *tcb, size_t tcbsize, size_t tcbalign) 3823 { 3824 RtldLockState lockstate; 3825 3826 wlock_acquire(rtld_bind_lock, &lockstate); 3827 free_tls(tcb, tcbsize, tcbalign); 3828 lock_release(rtld_bind_lock, &lockstate); 3829 } 3830 3831 static void 3832 object_add_name(Obj_Entry *obj, const char *name) 3833 { 3834 Name_Entry *entry; 3835 size_t len; 3836 3837 len = strlen(name); 3838 entry = malloc(sizeof(Name_Entry) + len); 3839 3840 if (entry != NULL) { 3841 strcpy(entry->name, name); 3842 STAILQ_INSERT_TAIL(&obj->names, entry, link); 3843 } 3844 } 3845 3846 static int 3847 object_match_name(const Obj_Entry *obj, const char *name) 3848 { 3849 Name_Entry *entry; 3850 3851 STAILQ_FOREACH(entry, &obj->names, link) { 3852 if (strcmp(name, entry->name) == 0) 3853 return (1); 3854 } 3855 return (0); 3856 } 3857 3858 static Obj_Entry * 3859 locate_dependency(const Obj_Entry *obj, const char *name) 3860 { 3861 const Objlist_Entry *entry; 3862 const Needed_Entry *needed; 3863 3864 STAILQ_FOREACH(entry, &list_main, link) { 3865 if (object_match_name(entry->obj, name)) 3866 return entry->obj; 3867 } 3868 3869 for (needed = obj->needed; needed != NULL; needed = needed->next) { 3870 if (strcmp(obj->strtab + needed->name, name) == 0 || 3871 (needed->obj != NULL && object_match_name(needed->obj, name))) { 3872 /* 3873 * If there is DT_NEEDED for the name we are looking for, 3874 * we are all set. Note that object might not be found if 3875 * dependency was not loaded yet, so the function can 3876 * return NULL here. This is expected and handled 3877 * properly by the caller. 3878 */ 3879 return (needed->obj); 3880 } 3881 } 3882 _rtld_error("%s: Unexpected inconsistency: dependency %s not found", 3883 obj->path, name); 3884 die(); 3885 } 3886 3887 static int 3888 check_object_provided_version(Obj_Entry *refobj, const Obj_Entry *depobj, 3889 const Elf_Vernaux *vna) 3890 { 3891 const Elf_Verdef *vd; 3892 const char *vername; 3893 3894 vername = refobj->strtab + vna->vna_name; 3895 vd = depobj->verdef; 3896 if (vd == NULL) { 3897 _rtld_error("%s: version %s required by %s not defined", 3898 depobj->path, vername, refobj->path); 3899 return (-1); 3900 } 3901 for (;;) { 3902 if (vd->vd_version != VER_DEF_CURRENT) { 3903 _rtld_error("%s: Unsupported version %d of Elf_Verdef entry", 3904 depobj->path, vd->vd_version); 3905 return (-1); 3906 } 3907 if (vna->vna_hash == vd->vd_hash) { 3908 const Elf_Verdaux *aux = (const Elf_Verdaux *) 3909 ((char *)vd + vd->vd_aux); 3910 if (strcmp(vername, depobj->strtab + aux->vda_name) == 0) 3911 return (0); 3912 } 3913 if (vd->vd_next == 0) 3914 break; 3915 vd = (const Elf_Verdef *) ((char *)vd + vd->vd_next); 3916 } 3917 if (vna->vna_flags & VER_FLG_WEAK) 3918 return (0); 3919 _rtld_error("%s: version %s required by %s not found", 3920 depobj->path, vername, refobj->path); 3921 return (-1); 3922 } 3923 3924 static int 3925 rtld_verify_object_versions(Obj_Entry *obj) 3926 { 3927 const Elf_Verneed *vn; 3928 const Elf_Verdef *vd; 3929 const Elf_Verdaux *vda; 3930 const Elf_Vernaux *vna; 3931 const Obj_Entry *depobj; 3932 int maxvernum, vernum; 3933 3934 maxvernum = 0; 3935 /* 3936 * Walk over defined and required version records and figure out 3937 * max index used by any of them. Do very basic sanity checking 3938 * while there. 3939 */ 3940 vn = obj->verneed; 3941 while (vn != NULL) { 3942 if (vn->vn_version != VER_NEED_CURRENT) { 3943 _rtld_error("%s: Unsupported version %d of Elf_Verneed entry", 3944 obj->path, vn->vn_version); 3945 return (-1); 3946 } 3947 vna = (const Elf_Vernaux *) ((char *)vn + vn->vn_aux); 3948 for (;;) { 3949 vernum = VER_NEED_IDX(vna->vna_other); 3950 if (vernum > maxvernum) 3951 maxvernum = vernum; 3952 if (vna->vna_next == 0) 3953 break; 3954 vna = (const Elf_Vernaux *) ((char *)vna + vna->vna_next); 3955 } 3956 if (vn->vn_next == 0) 3957 break; 3958 vn = (const Elf_Verneed *) ((char *)vn + vn->vn_next); 3959 } 3960 3961 vd = obj->verdef; 3962 while (vd != NULL) { 3963 if (vd->vd_version != VER_DEF_CURRENT) { 3964 _rtld_error("%s: Unsupported version %d of Elf_Verdef entry", 3965 obj->path, vd->vd_version); 3966 return (-1); 3967 } 3968 vernum = VER_DEF_IDX(vd->vd_ndx); 3969 if (vernum > maxvernum) 3970 maxvernum = vernum; 3971 if (vd->vd_next == 0) 3972 break; 3973 vd = (const Elf_Verdef *) ((char *)vd + vd->vd_next); 3974 } 3975 3976 if (maxvernum == 0) 3977 return (0); 3978 3979 /* 3980 * Store version information in array indexable by version index. 3981 * Verify that object version requirements are satisfied along the 3982 * way. 3983 */ 3984 obj->vernum = maxvernum + 1; 3985 obj->vertab = calloc(obj->vernum, sizeof(Ver_Entry)); 3986 3987 vd = obj->verdef; 3988 while (vd != NULL) { 3989 if ((vd->vd_flags & VER_FLG_BASE) == 0) { 3990 vernum = VER_DEF_IDX(vd->vd_ndx); 3991 assert(vernum <= maxvernum); 3992 vda = (const Elf_Verdaux *)((char *)vd + vd->vd_aux); 3993 obj->vertab[vernum].hash = vd->vd_hash; 3994 obj->vertab[vernum].name = obj->strtab + vda->vda_name; 3995 obj->vertab[vernum].file = NULL; 3996 obj->vertab[vernum].flags = 0; 3997 } 3998 if (vd->vd_next == 0) 3999 break; 4000 vd = (const Elf_Verdef *) ((char *)vd + vd->vd_next); 4001 } 4002 4003 vn = obj->verneed; 4004 while (vn != NULL) { 4005 depobj = locate_dependency(obj, obj->strtab + vn->vn_file); 4006 if (depobj == NULL) 4007 return (-1); 4008 vna = (const Elf_Vernaux *) ((char *)vn + vn->vn_aux); 4009 for (;;) { 4010 if (check_object_provided_version(obj, depobj, vna)) 4011 return (-1); 4012 vernum = VER_NEED_IDX(vna->vna_other); 4013 assert(vernum <= maxvernum); 4014 obj->vertab[vernum].hash = vna->vna_hash; 4015 obj->vertab[vernum].name = obj->strtab + vna->vna_name; 4016 obj->vertab[vernum].file = obj->strtab + vn->vn_file; 4017 obj->vertab[vernum].flags = (vna->vna_other & VER_NEED_HIDDEN) ? 4018 VER_INFO_HIDDEN : 0; 4019 if (vna->vna_next == 0) 4020 break; 4021 vna = (const Elf_Vernaux *) ((char *)vna + vna->vna_next); 4022 } 4023 if (vn->vn_next == 0) 4024 break; 4025 vn = (const Elf_Verneed *) ((char *)vn + vn->vn_next); 4026 } 4027 return 0; 4028 } 4029 4030 static int 4031 rtld_verify_versions(const Objlist *objlist) 4032 { 4033 Objlist_Entry *entry; 4034 int rc; 4035 4036 rc = 0; 4037 STAILQ_FOREACH(entry, objlist, link) { 4038 /* 4039 * Skip dummy objects or objects that have their version requirements 4040 * already checked. 4041 */ 4042 if (entry->obj->strtab == NULL || entry->obj->vertab != NULL) 4043 continue; 4044 if (rtld_verify_object_versions(entry->obj) == -1) { 4045 rc = -1; 4046 if (ld_tracing == NULL) 4047 break; 4048 } 4049 } 4050 if (rc == 0 || ld_tracing != NULL) 4051 rc = rtld_verify_object_versions(&obj_rtld); 4052 return rc; 4053 } 4054 4055 const Ver_Entry * 4056 fetch_ventry(const Obj_Entry *obj, unsigned long symnum) 4057 { 4058 Elf_Versym vernum; 4059 4060 if (obj->vertab) { 4061 vernum = VER_NDX(obj->versyms[symnum]); 4062 if (vernum >= obj->vernum) { 4063 _rtld_error("%s: symbol %s has wrong verneed value %d", 4064 obj->path, obj->strtab + symnum, vernum); 4065 } else if (obj->vertab[vernum].hash != 0) { 4066 return &obj->vertab[vernum]; 4067 } 4068 } 4069 return NULL; 4070 } 4071 4072 int 4073 _rtld_get_stack_prot(void) 4074 { 4075 4076 return (stack_prot); 4077 } 4078 4079 static void 4080 map_stacks_exec(RtldLockState *lockstate) 4081 { 4082 void (*thr_map_stacks_exec)(void); 4083 4084 if ((max_stack_flags & PF_X) == 0 || (stack_prot & PROT_EXEC) != 0) 4085 return; 4086 thr_map_stacks_exec = (void (*)(void))(uintptr_t) 4087 get_program_var_addr("__pthread_map_stacks_exec", lockstate); 4088 if (thr_map_stacks_exec != NULL) { 4089 stack_prot |= PROT_EXEC; 4090 thr_map_stacks_exec(); 4091 } 4092 } 4093 4094 void 4095 symlook_init(SymLook *dst, const char *name) 4096 { 4097 4098 bzero(dst, sizeof(*dst)); 4099 dst->name = name; 4100 dst->hash = elf_hash(name); 4101 } 4102 4103 static void 4104 symlook_init_from_req(SymLook *dst, const SymLook *src) 4105 { 4106 4107 dst->name = src->name; 4108 dst->hash = src->hash; 4109 dst->ventry = src->ventry; 4110 dst->flags = src->flags; 4111 dst->defobj_out = NULL; 4112 dst->sym_out = NULL; 4113 dst->lockstate = src->lockstate; 4114 } 4115 4116 /* 4117 * Overrides for libc_pic-provided functions. 4118 */ 4119 4120 int 4121 __getosreldate(void) 4122 { 4123 size_t len; 4124 int oid[2]; 4125 int error, osrel; 4126 4127 if (osreldate != 0) 4128 return (osreldate); 4129 4130 oid[0] = CTL_KERN; 4131 oid[1] = KERN_OSRELDATE; 4132 osrel = 0; 4133 len = sizeof(osrel); 4134 error = sysctl(oid, 2, &osrel, &len, NULL, 0); 4135 if (error == 0 && osrel > 0 && len == sizeof(osrel)) 4136 osreldate = osrel; 4137 return (osreldate); 4138 } 4139 4140 /* 4141 * No unresolved symbols for rtld. 4142 */ 4143 void 4144 __pthread_cxa_finalize(struct dl_phdr_info *a) 4145 { 4146 } 4147