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