2 * Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra.
3 * Copyright 2003 Alexander Kabaev <kan@FreeBSD.ORG>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Dynamic linker for ELF.
32 * John Polstra <jdp@polstra.com>.
36 #error "GCC is needed to compile this file"
39 #include <sys/param.h>
40 #include <sys/mount.h>
44 #include <sys/utsname.h>
45 #include <sys/ktrace.h>
63 #define PATH_RTLD "/libexec/ld-elf.so.1"
65 #define PATH_RTLD "/libexec/ld-elf32.so.1"
69 typedef void (*func_ptr_type)();
70 typedef void * (*path_enum_proc) (const char *path, size_t len, void *arg);
73 * This structure provides a reentrant way to keep a list of objects and
74 * check which ones have already been processed in some way.
76 typedef struct Struct_DoneList {
77 const Obj_Entry **objs; /* Array of object pointers */
78 unsigned int num_alloc; /* Allocated size of the array */
79 unsigned int num_used; /* Number of array slots used */
83 * Function declarations.
85 static const char *basename(const char *);
86 static void die(void) __dead2;
87 static void digest_dynamic(Obj_Entry *, int);
88 static Obj_Entry *digest_phdr(const Elf_Phdr *, int, caddr_t, const char *);
89 static Obj_Entry *dlcheck(void *);
90 static Obj_Entry *do_load_object(int, const char *, char *, struct stat *, int);
91 static int do_search_info(const Obj_Entry *obj, int, struct dl_serinfo *);
92 static bool donelist_check(DoneList *, const Obj_Entry *);
93 static void errmsg_restore(char *);
94 static char *errmsg_save(void);
95 static void *fill_search_info(const char *, size_t, void *);
96 static char *find_library(const char *, const Obj_Entry *);
97 static const char *gethints(void);
98 static void init_dag(Obj_Entry *);
99 static void init_dag1(Obj_Entry *, Obj_Entry *, DoneList *);
100 static void init_rtld(caddr_t);
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 int load_needed_objects(Obj_Entry *, int);
107 static int load_preload_objects(void);
108 static Obj_Entry *load_object(const char *, const Obj_Entry *, int);
109 static Obj_Entry *obj_from_addr(const void *);
110 static void objlist_call_fini(Objlist *, Obj_Entry *, int *);
111 static void objlist_call_init(Objlist *, int *);
112 static void objlist_clear(Objlist *);
113 static Objlist_Entry *objlist_find(Objlist *, const Obj_Entry *);
114 static void objlist_init(Objlist *);
115 static void objlist_push_head(Objlist *, Obj_Entry *);
116 static void objlist_push_tail(Objlist *, Obj_Entry *);
117 static void objlist_remove(Objlist *, Obj_Entry *);
118 static void *path_enumerate(const char *, path_enum_proc, void *);
119 static int relocate_objects(Obj_Entry *, bool, Obj_Entry *);
120 static int rtld_dirname(const char *, char *);
121 static int rtld_dirname_abs(const char *, char *);
122 static void rtld_exit(void);
123 static char *search_library_path(const char *, const char *);
124 static const void **get_program_var_addr(const char *);
125 static void set_program_var(const char *, const void *);
126 static const Elf_Sym *symlook_default(const char *, unsigned long,
127 const Obj_Entry *, const Obj_Entry **, const Ver_Entry *, int);
128 static const Elf_Sym *symlook_list(const char *, unsigned long, const Objlist *,
129 const Obj_Entry **, const Ver_Entry *, int, DoneList *);
130 static const Elf_Sym *symlook_needed(const char *, unsigned long,
131 const Needed_Entry *, const Obj_Entry **, const Ver_Entry *,
133 static void trace_loaded_objects(Obj_Entry *);
134 static void unlink_object(Obj_Entry *);
135 static void unload_object(Obj_Entry *);
136 static void unref_dag(Obj_Entry *);
137 static void ref_dag(Obj_Entry *);
138 static int origin_subst_one(char **, const char *, const char *,
139 const char *, char *);
140 static char *origin_subst(const char *, const char *);
141 static int rtld_verify_versions(const Objlist *);
142 static int rtld_verify_object_versions(Obj_Entry *);
143 static void object_add_name(Obj_Entry *, const char *);
144 static int object_match_name(const Obj_Entry *, const char *);
145 static void ld_utrace_log(int, void *, void *, size_t, int, const char *);
147 void r_debug_state(struct r_debug *, struct link_map *);
152 static char *error_message; /* Message for dlerror(), or NULL */
153 struct r_debug r_debug; /* for GDB; */
154 static bool libmap_disable; /* Disable libmap */
155 static char *libmap_override; /* Maps to use in addition to libmap.conf */
156 static bool trust; /* False for setuid and setgid programs */
157 static bool dangerous_ld_env; /* True if environment variables have been
158 used to affect the libraries loaded */
159 static char *ld_bind_now; /* Environment variable for immediate binding */
160 static char *ld_debug; /* Environment variable for debugging */
161 static char *ld_library_path; /* Environment variable for search path */
162 static char *ld_preload; /* Environment variable for libraries to
164 static char *ld_elf_hints_path; /* Environment variable for alternative hints path */
165 static char *ld_tracing; /* Called from ldd to print libs */
166 static char *ld_utrace; /* Use utrace() to log events. */
167 static Obj_Entry *obj_list; /* Head of linked list of shared objects */
168 static Obj_Entry **obj_tail; /* Link field of last object in list */
169 static Obj_Entry *obj_main; /* The main program shared object */
170 static Obj_Entry obj_rtld; /* The dynamic linker shared object */
171 static unsigned int obj_count; /* Number of objects in obj_list */
172 static unsigned int obj_loads; /* Number of objects in obj_list */
174 static Objlist list_global = /* Objects dlopened with RTLD_GLOBAL */
175 STAILQ_HEAD_INITIALIZER(list_global);
176 static Objlist list_main = /* Objects loaded at program startup */
177 STAILQ_HEAD_INITIALIZER(list_main);
178 static Objlist list_fini = /* Objects needing fini() calls */
179 STAILQ_HEAD_INITIALIZER(list_fini);
181 static Elf_Sym sym_zero; /* For resolving undefined weak refs. */
183 #define GDB_STATE(s,m) r_debug.r_state = s; r_debug_state(&r_debug,m);
185 extern Elf_Dyn _DYNAMIC;
186 #pragma weak _DYNAMIC
187 #ifndef RTLD_IS_DYNAMIC
188 #define RTLD_IS_DYNAMIC() (&_DYNAMIC != NULL)
192 * These are the functions the dynamic linker exports to application
193 * programs. They are the only symbols the dynamic linker is willing
194 * to export from itself.
196 static func_ptr_type exports[] = {
197 (func_ptr_type) &_rtld_error,
198 (func_ptr_type) &dlclose,
199 (func_ptr_type) &dlerror,
200 (func_ptr_type) &dlopen,
201 (func_ptr_type) &dlsym,
202 (func_ptr_type) &dlfunc,
203 (func_ptr_type) &dlvsym,
204 (func_ptr_type) &dladdr,
205 (func_ptr_type) &dllockinit,
206 (func_ptr_type) &dlinfo,
207 (func_ptr_type) &_rtld_thread_init,
209 (func_ptr_type) &___tls_get_addr,
211 (func_ptr_type) &__tls_get_addr,
212 (func_ptr_type) &_rtld_allocate_tls,
213 (func_ptr_type) &_rtld_free_tls,
214 (func_ptr_type) &dl_iterate_phdr,
215 (func_ptr_type) &_rtld_atfork_pre,
216 (func_ptr_type) &_rtld_atfork_post,
221 * Global declarations normally provided by crt1. The dynamic linker is
222 * not built with crt1, so we have to provide them ourselves.
228 * Globals to control TLS allocation.
230 size_t tls_last_offset; /* Static TLS offset of last module */
231 size_t tls_last_size; /* Static TLS size of last module */
232 size_t tls_static_space; /* Static TLS space allocated */
233 int tls_dtv_generation = 1; /* Used to detect when dtv size changes */
234 int tls_max_index = 1; /* Largest module index allocated */
237 * Fill in a DoneList with an allocation large enough to hold all of
238 * the currently-loaded objects. Keep this as a macro since it calls
239 * alloca and we want that to occur within the scope of the caller.
241 #define donelist_init(dlp) \
242 ((dlp)->objs = alloca(obj_count * sizeof (dlp)->objs[0]), \
243 assert((dlp)->objs != NULL), \
244 (dlp)->num_alloc = obj_count, \
247 #define UTRACE_DLOPEN_START 1
248 #define UTRACE_DLOPEN_STOP 2
249 #define UTRACE_DLCLOSE_START 3
250 #define UTRACE_DLCLOSE_STOP 4
251 #define UTRACE_LOAD_OBJECT 5
252 #define UTRACE_UNLOAD_OBJECT 6
253 #define UTRACE_ADD_RUNDEP 7
254 #define UTRACE_PRELOAD_FINISHED 8
255 #define UTRACE_INIT_CALL 9
256 #define UTRACE_FINI_CALL 10
259 char sig[4]; /* 'RTLD' */
262 void *mapbase; /* Used for 'parent' and 'init/fini' */
264 int refcnt; /* Used for 'mode' */
265 char name[MAXPATHLEN];
268 #define LD_UTRACE(e, h, mb, ms, r, n) do { \
269 if (ld_utrace != NULL) \
270 ld_utrace_log(e, h, mb, ms, r, n); \
274 ld_utrace_log(int event, void *handle, void *mapbase, size_t mapsize,
275 int refcnt, const char *name)
277 struct utrace_rtld ut;
285 ut.mapbase = mapbase;
286 ut.mapsize = mapsize;
288 bzero(ut.name, sizeof(ut.name));
290 strlcpy(ut.name, name, sizeof(ut.name));
291 utrace(&ut, sizeof(ut));
295 * Main entry point for dynamic linking. The first argument is the
296 * stack pointer. The stack is expected to be laid out as described
297 * in the SVR4 ABI specification, Intel 386 Processor Supplement.
298 * Specifically, the stack pointer points to a word containing
299 * ARGC. Following that in the stack is a null-terminated sequence
300 * of pointers to argument strings. Then comes a null-terminated
301 * sequence of pointers to environment strings. Finally, there is a
302 * sequence of "auxiliary vector" entries.
304 * The second argument points to a place to store the dynamic linker's
305 * exit procedure pointer and the third to a place to store the main
308 * The return value is the main program's entry point.
311 _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
313 Elf_Auxinfo *aux_info[AT_COUNT];
321 Objlist_Entry *entry;
323 Obj_Entry **preload_tail;
328 * On entry, the dynamic linker itself has not been relocated yet.
329 * Be very careful not to reference any global data until after
330 * init_rtld has returned. It is OK to reference file-scope statics
331 * and string constants, and to call static and global functions.
334 /* Find the auxiliary vector on the stack. */
337 sp += argc + 1; /* Skip over arguments and NULL terminator */
339 while (*sp++ != 0) /* Skip over environment, and NULL terminator */
341 aux = (Elf_Auxinfo *) sp;
343 /* Digest the auxiliary vector. */
344 for (i = 0; i < AT_COUNT; i++)
346 for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
347 if (auxp->a_type < AT_COUNT)
348 aux_info[auxp->a_type] = auxp;
351 /* Initialize and relocate ourselves. */
352 assert(aux_info[AT_BASE] != NULL);
353 init_rtld((caddr_t) aux_info[AT_BASE]->a_un.a_ptr);
355 __progname = obj_rtld.path;
356 argv0 = argv[0] != NULL ? argv[0] : "(null)";
359 trust = !issetugid();
361 ld_bind_now = getenv(LD_ "BIND_NOW");
363 * If the process is tainted, then we un-set the dangerous environment
364 * variables. The process will be marked as tainted until setuid(2)
365 * is called. If any child process calls setuid(2) we do not want any
366 * future processes to honor the potentially un-safe variables.
369 if (unsetenv(LD_ "PRELOAD") || unsetenv(LD_ "LIBMAP") ||
370 unsetenv(LD_ "LIBRARY_PATH") || unsetenv(LD_ "LIBMAP_DISABLE") ||
371 unsetenv(LD_ "DEBUG") || unsetenv(LD_ "ELF_HINTS_PATH")) {
372 _rtld_error("environment corrupt; aborting");
376 ld_debug = getenv(LD_ "DEBUG");
377 libmap_disable = getenv(LD_ "LIBMAP_DISABLE") != NULL;
378 libmap_override = getenv(LD_ "LIBMAP");
379 ld_library_path = getenv(LD_ "LIBRARY_PATH");
380 ld_preload = getenv(LD_ "PRELOAD");
381 ld_elf_hints_path = getenv(LD_ "ELF_HINTS_PATH");
382 dangerous_ld_env = libmap_disable || (libmap_override != NULL) ||
383 (ld_library_path != NULL) || (ld_preload != NULL) ||
384 (ld_elf_hints_path != NULL);
385 ld_tracing = getenv(LD_ "TRACE_LOADED_OBJECTS");
386 ld_utrace = getenv(LD_ "UTRACE");
388 if ((ld_elf_hints_path == NULL) || strlen(ld_elf_hints_path) == 0)
389 ld_elf_hints_path = _PATH_ELF_HINTS;
391 if (ld_debug != NULL && *ld_debug != '\0')
393 dbg("%s is initialized, base address = %p", __progname,
394 (caddr_t) aux_info[AT_BASE]->a_un.a_ptr);
395 dbg("RTLD dynamic = %p", obj_rtld.dynamic);
396 dbg("RTLD pltgot = %p", obj_rtld.pltgot);
399 * Load the main program, or process its program header if it is
402 if (aux_info[AT_EXECFD] != NULL) { /* Load the main program. */
403 int fd = aux_info[AT_EXECFD]->a_un.a_val;
404 dbg("loading main program");
405 obj_main = map_object(fd, argv0, NULL);
407 if (obj_main == NULL)
409 } else { /* Main program already loaded. */
410 const Elf_Phdr *phdr;
414 dbg("processing main program's program header");
415 assert(aux_info[AT_PHDR] != NULL);
416 phdr = (const Elf_Phdr *) aux_info[AT_PHDR]->a_un.a_ptr;
417 assert(aux_info[AT_PHNUM] != NULL);
418 phnum = aux_info[AT_PHNUM]->a_un.a_val;
419 assert(aux_info[AT_PHENT] != NULL);
420 assert(aux_info[AT_PHENT]->a_un.a_val == sizeof(Elf_Phdr));
421 assert(aux_info[AT_ENTRY] != NULL);
422 entry = (caddr_t) aux_info[AT_ENTRY]->a_un.a_ptr;
423 if ((obj_main = digest_phdr(phdr, phnum, entry, argv0)) == NULL)
427 if (aux_info[AT_EXECPATH] != 0) {
429 char buf[MAXPATHLEN];
431 kexecpath = aux_info[AT_EXECPATH]->a_un.a_ptr;
432 dbg("AT_EXECPATH %p %s", kexecpath, kexecpath);
433 if (kexecpath[0] == '/')
434 obj_main->path = kexecpath;
435 else if (getcwd(buf, sizeof(buf)) == NULL ||
436 strlcat(buf, "/", sizeof(buf)) >= sizeof(buf) ||
437 strlcat(buf, kexecpath, sizeof(buf)) >= sizeof(buf))
438 obj_main->path = xstrdup(argv0);
440 obj_main->path = xstrdup(buf);
442 dbg("No AT_EXECPATH");
443 obj_main->path = xstrdup(argv0);
445 dbg("obj_main path %s", obj_main->path);
446 obj_main->mainprog = true;
449 * Get the actual dynamic linker pathname from the executable if
450 * possible. (It should always be possible.) That ensures that
451 * gdb will find the right dynamic linker even if a non-standard
454 if (obj_main->interp != NULL &&
455 strcmp(obj_main->interp, obj_rtld.path) != 0) {
457 obj_rtld.path = xstrdup(obj_main->interp);
458 __progname = obj_rtld.path;
461 digest_dynamic(obj_main, 0);
463 linkmap_add(obj_main);
464 linkmap_add(&obj_rtld);
466 /* Link the main program into the list of objects. */
467 *obj_tail = obj_main;
468 obj_tail = &obj_main->next;
471 /* Make sure we don't call the main program's init and fini functions. */
472 obj_main->init = obj_main->fini = (Elf_Addr)NULL;
474 /* Initialize a fake symbol for resolving undefined weak references. */
475 sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
476 sym_zero.st_shndx = SHN_UNDEF;
477 sym_zero.st_value = -(uintptr_t)obj_main->relocbase;
480 libmap_disable = (bool)lm_init(libmap_override);
482 dbg("loading LD_PRELOAD libraries");
483 if (load_preload_objects() == -1)
485 preload_tail = obj_tail;
487 dbg("loading needed objects");
488 if (load_needed_objects(obj_main, 0) == -1)
491 /* Make a list of all objects loaded at startup. */
492 for (obj = obj_list; obj != NULL; obj = obj->next) {
493 objlist_push_tail(&list_main, obj);
497 dbg("checking for required versions");
498 if (rtld_verify_versions(&list_main) == -1 && !ld_tracing)
501 if (ld_tracing) { /* We're done */
502 trace_loaded_objects(obj_main);
506 if (getenv(LD_ "DUMP_REL_PRE") != NULL) {
507 dump_relocations(obj_main);
512 * Processing tls relocations requires having the tls offsets
513 * initialized. Prepare offsets before starting initial
514 * relocation processing.
516 dbg("initializing initial thread local storage offsets");
517 STAILQ_FOREACH(entry, &list_main, link) {
519 * Allocate all the initial objects out of the static TLS
520 * block even if they didn't ask for it.
522 allocate_tls_offset(entry->obj);
525 if (relocate_objects(obj_main,
526 ld_bind_now != NULL && *ld_bind_now != '\0', &obj_rtld) == -1)
529 dbg("doing copy relocations");
530 if (do_copy_relocations(obj_main) == -1)
533 if (getenv(LD_ "DUMP_REL_POST") != NULL) {
534 dump_relocations(obj_main);
539 * Setup TLS for main thread. This must be done after the
540 * relocations are processed, since tls initialization section
541 * might be the subject for relocations.
543 dbg("initializing initial thread local storage");
544 allocate_initial_tls(obj_list);
546 dbg("initializing key program variables");
547 set_program_var("__progname", argv[0] != NULL ? basename(argv[0]) : "");
548 set_program_var("environ", env);
550 dbg("initializing thread locks");
553 /* Make a list of init functions to call. */
554 objlist_init(&initlist);
555 initlist_add_objects(obj_list, preload_tail, &initlist);
557 r_debug_state(NULL, &obj_main->linkmap); /* say hello to gdb! */
559 lockstate = wlock_acquire(rtld_bind_lock);
560 objlist_call_init(&initlist, &lockstate);
561 objlist_clear(&initlist);
562 wlock_release(rtld_bind_lock, lockstate);
564 dbg("transferring control to program entry point = %p", obj_main->entry);
566 /* Return the exit procedure and the program entry point. */
567 *exit_proc = rtld_exit;
569 return (func_ptr_type) obj_main->entry;
573 _rtld_bind(Obj_Entry *obj, Elf_Size reloff)
577 const Obj_Entry *defobj;
582 lockstate = rlock_acquire(rtld_bind_lock);
584 rel = (const Elf_Rel *) ((caddr_t) obj->pltrel + reloff);
586 rel = (const Elf_Rel *) ((caddr_t) obj->pltrela + reloff);
588 where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
589 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
593 target = (Elf_Addr)(defobj->relocbase + def->st_value);
595 dbg("\"%s\" in \"%s\" ==> %p in \"%s\"",
596 defobj->strtab + def->st_name, basename(obj->path),
597 (void *)target, basename(defobj->path));
600 * Write the new contents for the jmpslot. Note that depending on
601 * architecture, the value which we need to return back to the
602 * lazy binding trampoline may or may not be the target
603 * address. The value returned from reloc_jmpslot() is the value
604 * that the trampoline needs.
606 target = reloc_jmpslot(where, target, defobj, obj, rel);
607 rlock_release(rtld_bind_lock, lockstate);
612 * Error reporting function. Use it like printf. If formats the message
613 * into a buffer, and sets things up so that the next call to dlerror()
614 * will return the message.
617 _rtld_error(const char *fmt, ...)
619 static char buf[512];
623 vsnprintf(buf, sizeof buf, fmt, ap);
629 * Return a dynamically-allocated copy of the current error message, if any.
634 return error_message == NULL ? NULL : xstrdup(error_message);
638 * Restore the current error message from a copy which was previously saved
639 * by errmsg_save(). The copy is freed.
642 errmsg_restore(char *saved_msg)
644 if (saved_msg == NULL)
645 error_message = NULL;
647 _rtld_error("%s", saved_msg);
653 basename(const char *name)
655 const char *p = strrchr(name, '/');
656 return p != NULL ? p + 1 : name;
659 static struct utsname uts;
662 origin_subst_one(char **res, const char *real, const char *kw, const char *subst,
672 subst_len = kw_len = 0;
676 if (subst_len == 0) {
677 subst_len = strlen(subst);
681 *res = xmalloc(PATH_MAX);
684 if ((res1 - *res) + subst_len + (p1 - p) >= PATH_MAX) {
685 _rtld_error("Substitution of %s in %s cannot be performed",
687 if (may_free != NULL)
692 memcpy(res1, p, p1 - p);
694 memcpy(res1, subst, subst_len);
699 if (may_free != NULL)
702 *res = xstrdup(real);
706 if (may_free != NULL)
708 if (strlcat(res1, p, PATH_MAX - (res1 - *res)) >= PATH_MAX) {
718 origin_subst(const char *real, const char *origin_path)
720 char *res1, *res2, *res3, *res4;
722 if (uts.sysname[0] == '\0') {
723 if (uname(&uts) != 0) {
724 _rtld_error("utsname failed: %d", errno);
728 if (!origin_subst_one(&res1, real, "$ORIGIN", origin_path, NULL) ||
729 !origin_subst_one(&res2, res1, "$OSNAME", uts.sysname, res1) ||
730 !origin_subst_one(&res3, res2, "$OSREL", uts.release, res2) ||
731 !origin_subst_one(&res4, res3, "$PLATFORM", uts.machine, res3))
739 const char *msg = dlerror();
747 * Process a shared object's DYNAMIC section, and save the important
748 * information in its Obj_Entry structure.
751 digest_dynamic(Obj_Entry *obj, int early)
754 Needed_Entry **needed_tail = &obj->needed;
755 const Elf_Dyn *dyn_rpath = NULL;
756 const Elf_Dyn *dyn_soname = NULL;
757 int plttype = DT_REL;
759 obj->bind_now = false;
760 for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; dynp++) {
761 switch (dynp->d_tag) {
764 obj->rel = (const Elf_Rel *) (obj->relocbase + dynp->d_un.d_ptr);
768 obj->relsize = dynp->d_un.d_val;
772 assert(dynp->d_un.d_val == sizeof(Elf_Rel));
776 obj->pltrel = (const Elf_Rel *)
777 (obj->relocbase + dynp->d_un.d_ptr);
781 obj->pltrelsize = dynp->d_un.d_val;
785 obj->rela = (const Elf_Rela *) (obj->relocbase + dynp->d_un.d_ptr);
789 obj->relasize = dynp->d_un.d_val;
793 assert(dynp->d_un.d_val == sizeof(Elf_Rela));
797 plttype = dynp->d_un.d_val;
798 assert(dynp->d_un.d_val == DT_REL || plttype == DT_RELA);
802 obj->symtab = (const Elf_Sym *)
803 (obj->relocbase + dynp->d_un.d_ptr);
807 assert(dynp->d_un.d_val == sizeof(Elf_Sym));
811 obj->strtab = (const char *) (obj->relocbase + dynp->d_un.d_ptr);
815 obj->strsize = dynp->d_un.d_val;
819 obj->verneed = (const Elf_Verneed *) (obj->relocbase +
824 obj->verneednum = dynp->d_un.d_val;
828 obj->verdef = (const Elf_Verdef *) (obj->relocbase +
833 obj->verdefnum = dynp->d_un.d_val;
837 obj->versyms = (const Elf_Versym *)(obj->relocbase +
843 const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
844 (obj->relocbase + dynp->d_un.d_ptr);
845 obj->nbuckets = hashtab[0];
846 obj->nchains = hashtab[1];
847 obj->buckets = hashtab + 2;
848 obj->chains = obj->buckets + obj->nbuckets;
854 Needed_Entry *nep = NEW(Needed_Entry);
855 nep->name = dynp->d_un.d_val;
860 needed_tail = &nep->next;
865 obj->pltgot = (Elf_Addr *) (obj->relocbase + dynp->d_un.d_ptr);
873 obj->symbolic = true;
877 case DT_RUNPATH: /* XXX: process separately */
879 * We have to wait until later to process this, because we
880 * might not have gotten the address of the string table yet.
890 obj->init = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr);
894 obj->fini = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr);
898 * Don't process DT_DEBUG on MIPS as the dynamic section
899 * is mapped read-only. DT_MIPS_RLD_MAP is used instead.
904 /* XXX - not implemented yet */
906 dbg("Filling in DT_DEBUG entry");
907 ((Elf_Dyn*)dynp)->d_un.d_ptr = (Elf_Addr) &r_debug;
912 if ((dynp->d_un.d_val & DF_ORIGIN) && trust)
913 obj->z_origin = true;
914 if (dynp->d_un.d_val & DF_SYMBOLIC)
915 obj->symbolic = true;
916 if (dynp->d_un.d_val & DF_TEXTREL)
918 if (dynp->d_un.d_val & DF_BIND_NOW)
919 obj->bind_now = true;
920 if (dynp->d_un.d_val & DF_STATIC_TLS)
924 case DT_MIPS_LOCAL_GOTNO:
925 obj->local_gotno = dynp->d_un.d_val;
928 case DT_MIPS_SYMTABNO:
929 obj->symtabno = dynp->d_un.d_val;
933 obj->gotsym = dynp->d_un.d_val;
936 case DT_MIPS_RLD_MAP:
939 dbg("Filling in DT_DEBUG entry");
940 ((Elf_Dyn*)dynp)->d_un.d_ptr = (Elf_Addr) &r_debug;
946 if (dynp->d_un.d_val & DF_1_NOOPEN)
947 obj->z_noopen = true;
948 if ((dynp->d_un.d_val & DF_1_ORIGIN) && trust)
949 obj->z_origin = true;
950 if (dynp->d_un.d_val & DF_1_GLOBAL)
952 if (dynp->d_un.d_val & DF_1_BIND_NOW)
953 obj->bind_now = true;
954 if (dynp->d_un.d_val & DF_1_NODELETE)
955 obj->z_nodelete = true;
960 dbg("Ignoring d_tag %ld = %#lx", (long)dynp->d_tag,
969 if (plttype == DT_RELA) {
970 obj->pltrela = (const Elf_Rela *) obj->pltrel;
972 obj->pltrelasize = obj->pltrelsize;
976 if (obj->z_origin && obj->origin_path == NULL) {
977 obj->origin_path = xmalloc(PATH_MAX);
978 if (rtld_dirname_abs(obj->path, obj->origin_path) == -1)
982 if (dyn_rpath != NULL) {
983 obj->rpath = (char *)obj->strtab + dyn_rpath->d_un.d_val;
985 obj->rpath = origin_subst(obj->rpath, obj->origin_path);
988 if (dyn_soname != NULL)
989 object_add_name(obj, obj->strtab + dyn_soname->d_un.d_val);
993 * Process a shared object's program header. This is used only for the
994 * main program, when the kernel has already loaded the main program
995 * into memory before calling the dynamic linker. It creates and
996 * returns an Obj_Entry structure.
999 digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry, const char *path)
1002 const Elf_Phdr *phlimit = phdr + phnum;
1007 for (ph = phdr; ph < phlimit; ph++) {
1008 if (ph->p_type != PT_PHDR)
1012 obj->phsize = ph->p_memsz;
1013 obj->relocbase = (caddr_t)phdr - ph->p_vaddr;
1017 for (ph = phdr; ph < phlimit; ph++) {
1018 switch (ph->p_type) {
1021 obj->interp = (const char *)(ph->p_vaddr + obj->relocbase);
1025 if (nsegs == 0) { /* First load segment */
1026 obj->vaddrbase = trunc_page(ph->p_vaddr);
1027 obj->mapbase = obj->vaddrbase + obj->relocbase;
1028 obj->textsize = round_page(ph->p_vaddr + ph->p_memsz) -
1030 } else { /* Last load segment */
1031 obj->mapsize = round_page(ph->p_vaddr + ph->p_memsz) -
1038 obj->dynamic = (const Elf_Dyn *)(ph->p_vaddr + obj->relocbase);
1043 obj->tlssize = ph->p_memsz;
1044 obj->tlsalign = ph->p_align;
1045 obj->tlsinitsize = ph->p_filesz;
1046 obj->tlsinit = (void*)(ph->p_vaddr + obj->relocbase);
1051 _rtld_error("%s: too few PT_LOAD segments", path);
1060 dlcheck(void *handle)
1064 for (obj = obj_list; obj != NULL; obj = obj->next)
1065 if (obj == (Obj_Entry *) handle)
1068 if (obj == NULL || obj->refcount == 0 || obj->dl_refcount == 0) {
1069 _rtld_error("Invalid shared object handle %p", handle);
1076 * If the given object is already in the donelist, return true. Otherwise
1077 * add the object to the list and return false.
1080 donelist_check(DoneList *dlp, const Obj_Entry *obj)
1084 for (i = 0; i < dlp->num_used; i++)
1085 if (dlp->objs[i] == obj)
1088 * Our donelist allocation should always be sufficient. But if
1089 * our threads locking isn't working properly, more shared objects
1090 * could have been loaded since we allocated the list. That should
1091 * never happen, but we'll handle it properly just in case it does.
1093 if (dlp->num_used < dlp->num_alloc)
1094 dlp->objs[dlp->num_used++] = obj;
1099 * Hash function for symbol table lookup. Don't even think about changing
1100 * this. It is specified by the System V ABI.
1103 elf_hash(const char *name)
1105 const unsigned char *p = (const unsigned char *) name;
1106 unsigned long h = 0;
1109 while (*p != '\0') {
1110 h = (h << 4) + *p++;
1111 if ((g = h & 0xf0000000) != 0)
1119 * Find the library with the given name, and return its full pathname.
1120 * The returned string is dynamically allocated. Generates an error
1121 * message and returns NULL if the library cannot be found.
1123 * If the second argument is non-NULL, then it refers to an already-
1124 * loaded shared object, whose library search path will be searched.
1126 * The search order is:
1128 * rpath in the referencing file
1133 find_library(const char *xname, const Obj_Entry *refobj)
1138 if (strchr(xname, '/') != NULL) { /* Hard coded pathname */
1139 if (xname[0] != '/' && !trust) {
1140 _rtld_error("Absolute pathname required for shared object \"%s\"",
1144 if (refobj != NULL && refobj->z_origin)
1145 return origin_subst(xname, refobj->origin_path);
1147 return xstrdup(xname);
1150 if (libmap_disable || (refobj == NULL) ||
1151 (name = lm_find(refobj->path, xname)) == NULL)
1152 name = (char *)xname;
1154 dbg(" Searching for \"%s\"", name);
1156 if ((pathname = search_library_path(name, ld_library_path)) != NULL ||
1158 (pathname = search_library_path(name, refobj->rpath)) != NULL) ||
1159 (pathname = search_library_path(name, gethints())) != NULL ||
1160 (pathname = search_library_path(name, STANDARD_LIBRARY_PATH)) != NULL)
1163 if(refobj != NULL && refobj->path != NULL) {
1164 _rtld_error("Shared object \"%s\" not found, required by \"%s\"",
1165 name, basename(refobj->path));
1167 _rtld_error("Shared object \"%s\" not found", name);
1173 * Given a symbol number in a referencing object, find the corresponding
1174 * definition of the symbol. Returns a pointer to the symbol, or NULL if
1175 * no definition was found. Returns a pointer to the Obj_Entry of the
1176 * defining object via the reference parameter DEFOBJ_OUT.
1179 find_symdef(unsigned long symnum, const Obj_Entry *refobj,
1180 const Obj_Entry **defobj_out, int flags, SymCache *cache)
1184 const Obj_Entry *defobj;
1185 const Ver_Entry *ventry;
1190 * If we have already found this symbol, get the information from
1193 if (symnum >= refobj->nchains)
1194 return NULL; /* Bad object */
1195 if (cache != NULL && cache[symnum].sym != NULL) {
1196 *defobj_out = cache[symnum].obj;
1197 return cache[symnum].sym;
1200 ref = refobj->symtab + symnum;
1201 name = refobj->strtab + ref->st_name;
1205 * We don't have to do a full scale lookup if the symbol is local.
1206 * We know it will bind to the instance in this load module; to
1207 * which we already have a pointer (ie ref). By not doing a lookup,
1208 * we not only improve performance, but it also avoids unresolvable
1209 * symbols when local symbols are not in the hash table. This has
1210 * been seen with the ia64 toolchain.
1212 if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
1213 if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
1214 _rtld_error("%s: Bogus symbol table entry %lu", refobj->path,
1217 ventry = fetch_ventry(refobj, symnum);
1218 hash = elf_hash(name);
1219 def = symlook_default(name, hash, refobj, &defobj, ventry, flags);
1226 * If we found no definition and the reference is weak, treat the
1227 * symbol as having the value zero.
1229 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
1235 *defobj_out = defobj;
1236 /* Record the information in the cache to avoid subsequent lookups. */
1237 if (cache != NULL) {
1238 cache[symnum].sym = def;
1239 cache[symnum].obj = defobj;
1242 if (refobj != &obj_rtld)
1243 _rtld_error("%s: Undefined symbol \"%s\"", refobj->path, name);
1249 * Return the search path from the ldconfig hints file, reading it if
1250 * necessary. Returns NULL if there are problems with the hints file,
1251 * or if the search path there is empty.
1258 if (hints == NULL) {
1260 struct elfhints_hdr hdr;
1263 /* Keep from trying again in case the hints file is bad. */
1266 if ((fd = open(ld_elf_hints_path, O_RDONLY)) == -1)
1268 if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
1269 hdr.magic != ELFHINTS_MAGIC ||
1274 p = xmalloc(hdr.dirlistlen + 1);
1275 if (lseek(fd, hdr.strtab + hdr.dirlist, SEEK_SET) == -1 ||
1276 read(fd, p, hdr.dirlistlen + 1) != (ssize_t)hdr.dirlistlen + 1) {
1284 return hints[0] != '\0' ? hints : NULL;
1288 init_dag(Obj_Entry *root)
1292 if (root->dag_inited)
1294 donelist_init(&donelist);
1295 init_dag1(root, root, &donelist);
1296 root->dag_inited = true;
1300 init_dag1(Obj_Entry *root, Obj_Entry *obj, DoneList *dlp)
1302 const Needed_Entry *needed;
1304 if (donelist_check(dlp, obj))
1307 objlist_push_tail(&obj->dldags, root);
1308 objlist_push_tail(&root->dagmembers, obj);
1309 for (needed = obj->needed; needed != NULL; needed = needed->next)
1310 if (needed->obj != NULL)
1311 init_dag1(root, needed->obj, dlp);
1315 * Initialize the dynamic linker. The argument is the address at which
1316 * the dynamic linker has been mapped into memory. The primary task of
1317 * this function is to relocate the dynamic linker.
1320 init_rtld(caddr_t mapbase)
1322 Obj_Entry objtmp; /* Temporary rtld object */
1325 * Conjure up an Obj_Entry structure for the dynamic linker.
1327 * The "path" member can't be initialized yet because string constants
1328 * cannot yet be accessed. Below we will set it correctly.
1330 memset(&objtmp, 0, sizeof(objtmp));
1333 objtmp.mapbase = mapbase;
1335 objtmp.relocbase = mapbase;
1337 if (RTLD_IS_DYNAMIC()) {
1338 objtmp.dynamic = rtld_dynamic(&objtmp);
1339 digest_dynamic(&objtmp, 1);
1340 assert(objtmp.needed == NULL);
1341 #if !defined(__mips__)
1342 /* MIPS has a bogus DT_TEXTREL. */
1343 assert(!objtmp.textrel);
1347 * Temporarily put the dynamic linker entry into the object list, so
1348 * that symbols can be found.
1351 relocate_objects(&objtmp, true, &objtmp);
1354 /* Initialize the object list. */
1355 obj_tail = &obj_list;
1357 /* Now that non-local variables can be accesses, copy out obj_rtld. */
1358 memcpy(&obj_rtld, &objtmp, sizeof(obj_rtld));
1360 /* Replace the path with a dynamically allocated copy. */
1361 obj_rtld.path = xstrdup(PATH_RTLD);
1363 r_debug.r_brk = r_debug_state;
1364 r_debug.r_state = RT_CONSISTENT;
1368 * Add the init functions from a needed object list (and its recursive
1369 * needed objects) to "list". This is not used directly; it is a helper
1370 * function for initlist_add_objects(). The write lock must be held
1371 * when this function is called.
1374 initlist_add_neededs(Needed_Entry *needed, Objlist *list)
1376 /* Recursively process the successor needed objects. */
1377 if (needed->next != NULL)
1378 initlist_add_neededs(needed->next, list);
1380 /* Process the current needed object. */
1381 if (needed->obj != NULL)
1382 initlist_add_objects(needed->obj, &needed->obj->next, list);
1386 * Scan all of the DAGs rooted in the range of objects from "obj" to
1387 * "tail" and add their init functions to "list". This recurses over
1388 * the DAGs and ensure the proper init ordering such that each object's
1389 * needed libraries are initialized before the object itself. At the
1390 * same time, this function adds the objects to the global finalization
1391 * list "list_fini" in the opposite order. The write lock must be
1392 * held when this function is called.
1395 initlist_add_objects(Obj_Entry *obj, Obj_Entry **tail, Objlist *list)
1397 if (obj->init_scanned || obj->init_done)
1399 obj->init_scanned = true;
1401 /* Recursively process the successor objects. */
1402 if (&obj->next != tail)
1403 initlist_add_objects(obj->next, tail, list);
1405 /* Recursively process the needed objects. */
1406 if (obj->needed != NULL)
1407 initlist_add_neededs(obj->needed, list);
1409 /* Add the object to the init list. */
1410 if (obj->init != (Elf_Addr)NULL)
1411 objlist_push_tail(list, obj);
1413 /* Add the object to the global fini list in the reverse order. */
1414 if (obj->fini != (Elf_Addr)NULL && !obj->on_fini_list) {
1415 objlist_push_head(&list_fini, obj);
1416 obj->on_fini_list = true;
1421 #define FPTR_TARGET(f) ((Elf_Addr) (f))
1425 is_exported(const Elf_Sym *def)
1428 const func_ptr_type *p;
1430 value = (Elf_Addr)(obj_rtld.relocbase + def->st_value);
1431 for (p = exports; *p != NULL; p++)
1432 if (FPTR_TARGET(*p) == value)
1438 * Given a shared object, traverse its list of needed objects, and load
1439 * each of them. Returns 0 on success. Generates an error message and
1440 * returns -1 on failure.
1443 load_needed_objects(Obj_Entry *first, int flags)
1445 Obj_Entry *obj, *obj1;
1447 for (obj = first; obj != NULL; obj = obj->next) {
1448 Needed_Entry *needed;
1450 for (needed = obj->needed; needed != NULL; needed = needed->next) {
1451 obj1 = needed->obj = load_object(obj->strtab + needed->name, obj,
1452 flags & ~RTLD_LO_NOLOAD);
1453 if (obj1 == NULL && !ld_tracing)
1455 if (obj1 != NULL && obj1->z_nodelete && !obj1->ref_nodel) {
1456 dbg("obj %s nodelete", obj1->path);
1459 obj1->ref_nodel = true;
1468 load_preload_objects(void)
1470 char *p = ld_preload;
1471 static const char delim[] = " \t:;";
1476 p += strspn(p, delim);
1477 while (*p != '\0') {
1478 size_t len = strcspn(p, delim);
1483 if (load_object(p, NULL, 0) == NULL)
1484 return -1; /* XXX - cleanup */
1487 p += strspn(p, delim);
1489 LD_UTRACE(UTRACE_PRELOAD_FINISHED, NULL, NULL, 0, 0, NULL);
1494 * Load a shared object into memory, if it is not already loaded.
1496 * Returns a pointer to the Obj_Entry for the object. Returns NULL
1500 load_object(const char *name, const Obj_Entry *refobj, int flags)
1507 for (obj = obj_list->next; obj != NULL; obj = obj->next)
1508 if (object_match_name(obj, name))
1511 path = find_library(name, refobj);
1516 * If we didn't find a match by pathname, open the file and check
1517 * again by device and inode. This avoids false mismatches caused
1518 * by multiple links or ".." in pathnames.
1520 * To avoid a race, we open the file and use fstat() rather than
1523 if ((fd = open(path, O_RDONLY)) == -1) {
1524 _rtld_error("Cannot open \"%s\"", path);
1528 if (fstat(fd, &sb) == -1) {
1529 _rtld_error("Cannot fstat \"%s\"", path);
1534 for (obj = obj_list->next; obj != NULL; obj = obj->next)
1535 if (obj->ino == sb.st_ino && obj->dev == sb.st_dev)
1538 object_add_name(obj, name);
1543 if (flags & RTLD_LO_NOLOAD) {
1548 /* First use of this object, so we must map it in */
1549 obj = do_load_object(fd, name, path, &sb, flags);
1558 do_load_object(int fd, const char *name, char *path, struct stat *sbp,
1565 * but first, make sure that environment variables haven't been
1566 * used to circumvent the noexec flag on a filesystem.
1568 if (dangerous_ld_env) {
1569 if (fstatfs(fd, &fs) != 0) {
1570 _rtld_error("Cannot fstatfs \"%s\"", path);
1573 if (fs.f_flags & MNT_NOEXEC) {
1574 _rtld_error("Cannot execute objects on %s\n", fs.f_mntonname);
1578 dbg("loading \"%s\"", path);
1579 obj = map_object(fd, path, sbp);
1583 object_add_name(obj, name);
1585 digest_dynamic(obj, 0);
1586 if (obj->z_noopen && (flags & (RTLD_LO_DLOPEN | RTLD_LO_TRACE)) ==
1588 dbg("refusing to load non-loadable \"%s\"", obj->path);
1589 _rtld_error("Cannot dlopen non-loadable %s", obj->path);
1590 munmap(obj->mapbase, obj->mapsize);
1596 obj_tail = &obj->next;
1599 linkmap_add(obj); /* for GDB & dlinfo() */
1601 dbg(" %p .. %p: %s", obj->mapbase,
1602 obj->mapbase + obj->mapsize - 1, obj->path);
1604 dbg(" WARNING: %s has impure text", obj->path);
1605 LD_UTRACE(UTRACE_LOAD_OBJECT, obj, obj->mapbase, obj->mapsize, 0,
1612 obj_from_addr(const void *addr)
1616 for (obj = obj_list; obj != NULL; obj = obj->next) {
1617 if (addr < (void *) obj->mapbase)
1619 if (addr < (void *) (obj->mapbase + obj->mapsize))
1626 * Call the finalization functions for each of the objects in "list"
1627 * belonging to the DAG of "root" and referenced once. If NULL "root"
1628 * is specified, every finalization function will be called regardless
1629 * of the reference count and the list elements won't be freed. All of
1630 * the objects are expected to have non-NULL fini functions.
1633 objlist_call_fini(Objlist *list, Obj_Entry *root, int *lockstate)
1638 assert(root == NULL || root->refcount == 1);
1641 * Preserve the current error message since a fini function might
1642 * call into the dynamic linker and overwrite it.
1644 saved_msg = errmsg_save();
1646 STAILQ_FOREACH(elm, list, link) {
1647 if (root != NULL && (elm->obj->refcount != 1 ||
1648 objlist_find(&root->dagmembers, elm->obj) == NULL))
1650 dbg("calling fini function for %s at %p", elm->obj->path,
1651 (void *)elm->obj->fini);
1652 LD_UTRACE(UTRACE_FINI_CALL, elm->obj, (void *)elm->obj->fini, 0, 0,
1654 /* Remove object from fini list to prevent recursive invocation. */
1655 STAILQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
1657 * XXX: If a dlopen() call references an object while the
1658 * fini function is in progress, we might end up trying to
1659 * unload the referenced object in dlclose() or the object
1660 * won't be unloaded although its fini function has been
1663 wlock_release(rtld_bind_lock, *lockstate);
1664 call_initfini_pointer(elm->obj, elm->obj->fini);
1665 *lockstate = wlock_acquire(rtld_bind_lock);
1666 /* No need to free anything if process is going down. */
1670 * We must restart the list traversal after every fini call
1671 * because a dlclose() call from the fini function or from
1672 * another thread might have modified the reference counts.
1676 } while (elm != NULL);
1677 errmsg_restore(saved_msg);
1681 * Call the initialization functions for each of the objects in
1682 * "list". All of the objects are expected to have non-NULL init
1686 objlist_call_init(Objlist *list, int *lockstate)
1693 * Clean init_scanned flag so that objects can be rechecked and
1694 * possibly initialized earlier if any of vectors called below
1695 * cause the change by using dlopen.
1697 for (obj = obj_list; obj != NULL; obj = obj->next)
1698 obj->init_scanned = false;
1701 * Preserve the current error message since an init function might
1702 * call into the dynamic linker and overwrite it.
1704 saved_msg = errmsg_save();
1705 STAILQ_FOREACH(elm, list, link) {
1706 if (elm->obj->init_done) /* Initialized early. */
1708 dbg("calling init function for %s at %p", elm->obj->path,
1709 (void *)elm->obj->init);
1710 LD_UTRACE(UTRACE_INIT_CALL, elm->obj, (void *)elm->obj->init, 0, 0,
1713 * Race: other thread might try to use this object before current
1714 * one completes the initilization. Not much can be done here
1715 * without better locking.
1717 elm->obj->init_done = true;
1718 wlock_release(rtld_bind_lock, *lockstate);
1719 call_initfini_pointer(elm->obj, elm->obj->init);
1720 *lockstate = wlock_acquire(rtld_bind_lock);
1722 errmsg_restore(saved_msg);
1726 objlist_clear(Objlist *list)
1730 while (!STAILQ_EMPTY(list)) {
1731 elm = STAILQ_FIRST(list);
1732 STAILQ_REMOVE_HEAD(list, link);
1737 static Objlist_Entry *
1738 objlist_find(Objlist *list, const Obj_Entry *obj)
1742 STAILQ_FOREACH(elm, list, link)
1743 if (elm->obj == obj)
1749 objlist_init(Objlist *list)
1755 objlist_push_head(Objlist *list, Obj_Entry *obj)
1759 elm = NEW(Objlist_Entry);
1761 STAILQ_INSERT_HEAD(list, elm, link);
1765 objlist_push_tail(Objlist *list, Obj_Entry *obj)
1769 elm = NEW(Objlist_Entry);
1771 STAILQ_INSERT_TAIL(list, elm, link);
1775 objlist_remove(Objlist *list, Obj_Entry *obj)
1779 if ((elm = objlist_find(list, obj)) != NULL) {
1780 STAILQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
1786 * Relocate newly-loaded shared objects. The argument is a pointer to
1787 * the Obj_Entry for the first such object. All objects from the first
1788 * to the end of the list of objects are relocated. Returns 0 on success,
1792 relocate_objects(Obj_Entry *first, bool bind_now, Obj_Entry *rtldobj)
1796 for (obj = first; obj != NULL; obj = obj->next) {
1798 dbg("relocating \"%s\"", obj->path);
1799 if (obj->nbuckets == 0 || obj->nchains == 0 || obj->buckets == NULL ||
1800 obj->symtab == NULL || obj->strtab == NULL) {
1801 _rtld_error("%s: Shared object has no run-time symbol table",
1807 /* There are relocations to the write-protected text segment. */
1808 if (mprotect(obj->mapbase, obj->textsize,
1809 PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
1810 _rtld_error("%s: Cannot write-enable text segment: %s",
1811 obj->path, strerror(errno));
1816 /* Process the non-PLT relocations. */
1817 if (reloc_non_plt(obj, rtldobj))
1820 if (obj->textrel) { /* Re-protected the text segment. */
1821 if (mprotect(obj->mapbase, obj->textsize,
1822 PROT_READ|PROT_EXEC) == -1) {
1823 _rtld_error("%s: Cannot write-protect text segment: %s",
1824 obj->path, strerror(errno));
1829 /* Process the PLT relocations. */
1830 if (reloc_plt(obj) == -1)
1832 /* Relocate the jump slots if we are doing immediate binding. */
1833 if (obj->bind_now || bind_now)
1834 if (reloc_jmpslots(obj) == -1)
1839 * Set up the magic number and version in the Obj_Entry. These
1840 * were checked in the crt1.o from the original ElfKit, so we
1841 * set them for backward compatibility.
1843 obj->magic = RTLD_MAGIC;
1844 obj->version = RTLD_VERSION;
1846 /* Set the special PLT or GOT entries. */
1854 * Cleanup procedure. It will be called (by the atexit mechanism) just
1855 * before the process exits.
1862 lockstate = wlock_acquire(rtld_bind_lock);
1864 objlist_call_fini(&list_fini, NULL, &lockstate);
1865 /* No need to remove the items from the list, since we are exiting. */
1866 if (!libmap_disable)
1868 wlock_release(rtld_bind_lock, lockstate);
1872 path_enumerate(const char *path, path_enum_proc callback, void *arg)
1880 path += strspn(path, ":;");
1881 while (*path != '\0') {
1885 len = strcspn(path, ":;");
1887 trans = lm_findn(NULL, path, len);
1889 res = callback(trans, strlen(trans), arg);
1892 res = callback(path, len, arg);
1898 path += strspn(path, ":;");
1904 struct try_library_args {
1912 try_library_path(const char *dir, size_t dirlen, void *param)
1914 struct try_library_args *arg;
1917 if (*dir == '/' || trust) {
1920 if (dirlen + 1 + arg->namelen + 1 > arg->buflen)
1923 pathname = arg->buffer;
1924 strncpy(pathname, dir, dirlen);
1925 pathname[dirlen] = '/';
1926 strcpy(pathname + dirlen + 1, arg->name);
1928 dbg(" Trying \"%s\"", pathname);
1929 if (access(pathname, F_OK) == 0) { /* We found it */
1930 pathname = xmalloc(dirlen + 1 + arg->namelen + 1);
1931 strcpy(pathname, arg->buffer);
1939 search_library_path(const char *name, const char *path)
1942 struct try_library_args arg;
1948 arg.namelen = strlen(name);
1949 arg.buffer = xmalloc(PATH_MAX);
1950 arg.buflen = PATH_MAX;
1952 p = path_enumerate(path, try_library_path, &arg);
1960 dlclose(void *handle)
1965 lockstate = wlock_acquire(rtld_bind_lock);
1966 root = dlcheck(handle);
1968 wlock_release(rtld_bind_lock, lockstate);
1971 LD_UTRACE(UTRACE_DLCLOSE_START, handle, NULL, 0, root->dl_refcount,
1974 /* Unreference the object and its dependencies. */
1975 root->dl_refcount--;
1977 if (root->refcount == 1) {
1979 * The object will be no longer referenced, so we must unload it.
1980 * First, call the fini functions.
1982 objlist_call_fini(&list_fini, root, &lockstate);
1986 /* Finish cleaning up the newly-unreferenced objects. */
1987 GDB_STATE(RT_DELETE,&root->linkmap);
1988 unload_object(root);
1989 GDB_STATE(RT_CONSISTENT,NULL);
1993 LD_UTRACE(UTRACE_DLCLOSE_STOP, handle, NULL, 0, 0, NULL);
1994 wlock_release(rtld_bind_lock, lockstate);
2001 char *msg = error_message;
2002 error_message = NULL;
2007 * This function is deprecated and has no effect.
2010 dllockinit(void *context,
2011 void *(*lock_create)(void *context),
2012 void (*rlock_acquire)(void *lock),
2013 void (*wlock_acquire)(void *lock),
2014 void (*lock_release)(void *lock),
2015 void (*lock_destroy)(void *lock),
2016 void (*context_destroy)(void *context))
2018 static void *cur_context;
2019 static void (*cur_context_destroy)(void *);
2021 /* Just destroy the context from the previous call, if necessary. */
2022 if (cur_context_destroy != NULL)
2023 cur_context_destroy(cur_context);
2024 cur_context = context;
2025 cur_context_destroy = context_destroy;
2029 dlopen(const char *name, int mode)
2031 Obj_Entry **old_obj_tail;
2034 int result, lockstate, nodelete, lo_flags;
2036 LD_UTRACE(UTRACE_DLOPEN_START, NULL, NULL, 0, mode, name);
2037 ld_tracing = (mode & RTLD_TRACE) == 0 ? NULL : "1";
2038 if (ld_tracing != NULL)
2039 environ = (char **)*get_program_var_addr("environ");
2040 nodelete = mode & RTLD_NODELETE;
2041 lo_flags = RTLD_LO_DLOPEN;
2042 if (mode & RTLD_NOLOAD)
2043 lo_flags |= RTLD_LO_NOLOAD;
2044 if (ld_tracing != NULL)
2045 lo_flags |= RTLD_LO_TRACE;
2047 objlist_init(&initlist);
2049 lockstate = wlock_acquire(rtld_bind_lock);
2050 GDB_STATE(RT_ADD,NULL);
2052 old_obj_tail = obj_tail;
2058 obj = load_object(name, obj_main, lo_flags);
2063 if (mode & RTLD_GLOBAL && objlist_find(&list_global, obj) == NULL)
2064 objlist_push_tail(&list_global, obj);
2065 mode &= RTLD_MODEMASK;
2066 if (*old_obj_tail != NULL) { /* We loaded something new. */
2067 assert(*old_obj_tail == obj);
2068 result = load_needed_objects(obj, RTLD_LO_DLOPEN);
2072 result = rtld_verify_versions(&obj->dagmembers);
2073 if (result != -1 && ld_tracing)
2076 (relocate_objects(obj, mode == RTLD_NOW, &obj_rtld)) == -1) {
2079 if (obj->refcount == 0)
2083 /* Make list of init functions to call. */
2084 initlist_add_objects(obj, &obj->next, &initlist);
2089 * Bump the reference counts for objects on this DAG. If
2090 * this is the first dlopen() call for the object that was
2091 * already loaded as a dependency, initialize the dag
2100 if (obj != NULL && (nodelete || obj->z_nodelete) && !obj->ref_nodel) {
2101 dbg("obj %s nodelete", obj->path);
2103 obj->z_nodelete = obj->ref_nodel = true;
2107 LD_UTRACE(UTRACE_DLOPEN_STOP, obj, NULL, 0, obj ? obj->dl_refcount : 0,
2109 GDB_STATE(RT_CONSISTENT,obj ? &obj->linkmap : NULL);
2111 /* Call the init functions. */
2112 objlist_call_init(&initlist, &lockstate);
2113 objlist_clear(&initlist);
2114 wlock_release(rtld_bind_lock, lockstate);
2117 trace_loaded_objects(obj);
2118 wlock_release(rtld_bind_lock, lockstate);
2123 do_dlsym(void *handle, const char *name, void *retaddr, const Ver_Entry *ve,
2127 const Obj_Entry *obj, *defobj;
2128 const Elf_Sym *def, *symp;
2132 hash = elf_hash(name);
2135 flags |= SYMLOOK_IN_PLT;
2137 lockstate = rlock_acquire(rtld_bind_lock);
2138 if (handle == NULL || handle == RTLD_NEXT ||
2139 handle == RTLD_DEFAULT || handle == RTLD_SELF) {
2141 if ((obj = obj_from_addr(retaddr)) == NULL) {
2142 _rtld_error("Cannot determine caller's shared object");
2143 rlock_release(rtld_bind_lock, lockstate);
2146 if (handle == NULL) { /* Just the caller's shared object. */
2147 def = symlook_obj(name, hash, obj, ve, flags);
2149 } else if (handle == RTLD_NEXT || /* Objects after caller's */
2150 handle == RTLD_SELF) { /* ... caller included */
2151 if (handle == RTLD_NEXT)
2153 for (; obj != NULL; obj = obj->next) {
2154 if ((symp = symlook_obj(name, hash, obj, ve, flags)) != NULL) {
2155 if (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK) {
2158 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
2164 * Search the dynamic linker itself, and possibly resolve the
2165 * symbol from there. This is how the application links to
2166 * dynamic linker services such as dlopen. Only the values listed
2167 * in the "exports" array can be resolved from the dynamic linker.
2169 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
2170 symp = symlook_obj(name, hash, &obj_rtld, ve, flags);
2171 if (symp != NULL && is_exported(symp)) {
2177 assert(handle == RTLD_DEFAULT);
2178 def = symlook_default(name, hash, obj, &defobj, ve, flags);
2181 if ((obj = dlcheck(handle)) == NULL) {
2182 rlock_release(rtld_bind_lock, lockstate);
2186 donelist_init(&donelist);
2187 if (obj->mainprog) {
2188 /* Search main program and all libraries loaded by it. */
2189 def = symlook_list(name, hash, &list_main, &defobj, ve, flags,
2193 * We do not distinguish between 'main' object and global scope.
2194 * If symbol is not defined by objects loaded at startup, continue
2195 * search among dynamically loaded objects with RTLD_GLOBAL
2199 def = symlook_list(name, hash, &list_global, &defobj, ve,
2204 /* Search the whole DAG rooted at the given object. */
2206 fake.obj = (Obj_Entry *)obj;
2208 def = symlook_needed(name, hash, &fake, &defobj, ve, flags,
2214 rlock_release(rtld_bind_lock, lockstate);
2217 * The value required by the caller is derived from the value
2218 * of the symbol. For the ia64 architecture, we need to
2219 * construct a function descriptor which the caller can use to
2220 * call the function with the right 'gp' value. For other
2221 * architectures and for non-functions, the value is simply
2222 * the relocated value of the symbol.
2224 if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
2225 return make_function_pointer(def, defobj);
2227 return defobj->relocbase + def->st_value;
2230 _rtld_error("Undefined symbol \"%s\"", name);
2231 rlock_release(rtld_bind_lock, lockstate);
2236 dlsym(void *handle, const char *name)
2238 return do_dlsym(handle, name, __builtin_return_address(0), NULL,
2243 dlfunc(void *handle, const char *name)
2250 rv.d = do_dlsym(handle, name, __builtin_return_address(0), NULL,
2256 dlvsym(void *handle, const char *name, const char *version)
2260 ventry.name = version;
2262 ventry.hash = elf_hash(version);
2264 return do_dlsym(handle, name, __builtin_return_address(0), &ventry,
2269 dladdr(const void *addr, Dl_info *info)
2271 const Obj_Entry *obj;
2274 unsigned long symoffset;
2277 lockstate = rlock_acquire(rtld_bind_lock);
2278 obj = obj_from_addr(addr);
2280 _rtld_error("No shared object contains address");
2281 rlock_release(rtld_bind_lock, lockstate);
2284 info->dli_fname = obj->path;
2285 info->dli_fbase = obj->mapbase;
2286 info->dli_saddr = (void *)0;
2287 info->dli_sname = NULL;
2290 * Walk the symbol list looking for the symbol whose address is
2291 * closest to the address sent in.
2293 for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
2294 def = obj->symtab + symoffset;
2297 * For skip the symbol if st_shndx is either SHN_UNDEF or
2300 if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
2304 * If the symbol is greater than the specified address, or if it
2305 * is further away from addr than the current nearest symbol,
2308 symbol_addr = obj->relocbase + def->st_value;
2309 if (symbol_addr > addr || symbol_addr < info->dli_saddr)
2312 /* Update our idea of the nearest symbol. */
2313 info->dli_sname = obj->strtab + def->st_name;
2314 info->dli_saddr = symbol_addr;
2317 if (info->dli_saddr == addr)
2320 rlock_release(rtld_bind_lock, lockstate);
2325 dlinfo(void *handle, int request, void *p)
2327 const Obj_Entry *obj;
2328 int error, lockstate;
2330 lockstate = rlock_acquire(rtld_bind_lock);
2332 if (handle == NULL || handle == RTLD_SELF) {
2335 retaddr = __builtin_return_address(0); /* __GNUC__ only */
2336 if ((obj = obj_from_addr(retaddr)) == NULL)
2337 _rtld_error("Cannot determine caller's shared object");
2339 obj = dlcheck(handle);
2342 rlock_release(rtld_bind_lock, lockstate);
2348 case RTLD_DI_LINKMAP:
2349 *((struct link_map const **)p) = &obj->linkmap;
2351 case RTLD_DI_ORIGIN:
2352 error = rtld_dirname(obj->path, p);
2355 case RTLD_DI_SERINFOSIZE:
2356 case RTLD_DI_SERINFO:
2357 error = do_search_info(obj, request, (struct dl_serinfo *)p);
2361 _rtld_error("Invalid request %d passed to dlinfo()", request);
2365 rlock_release(rtld_bind_lock, lockstate);
2371 dl_iterate_phdr(__dl_iterate_hdr_callback callback, void *param)
2373 struct dl_phdr_info phdr_info;
2374 const Obj_Entry *obj;
2375 int error, bind_lockstate, phdr_lockstate;
2377 phdr_lockstate = wlock_acquire(rtld_phdr_lock);
2378 bind_lockstate = rlock_acquire(rtld_bind_lock);
2382 for (obj = obj_list; obj != NULL; obj = obj->next) {
2383 phdr_info.dlpi_addr = (Elf_Addr)obj->relocbase;
2384 phdr_info.dlpi_name = STAILQ_FIRST(&obj->names) ?
2385 STAILQ_FIRST(&obj->names)->name : obj->path;
2386 phdr_info.dlpi_phdr = obj->phdr;
2387 phdr_info.dlpi_phnum = obj->phsize / sizeof(obj->phdr[0]);
2388 phdr_info.dlpi_tls_modid = obj->tlsindex;
2389 phdr_info.dlpi_tls_data = obj->tlsinit;
2390 phdr_info.dlpi_adds = obj_loads;
2391 phdr_info.dlpi_subs = obj_loads - obj_count;
2393 if ((error = callback(&phdr_info, sizeof phdr_info, param)) != 0)
2397 rlock_release(rtld_bind_lock, bind_lockstate);
2398 wlock_release(rtld_phdr_lock, phdr_lockstate);
2403 struct fill_search_info_args {
2406 Dl_serinfo *serinfo;
2407 Dl_serpath *serpath;
2412 fill_search_info(const char *dir, size_t dirlen, void *param)
2414 struct fill_search_info_args *arg;
2418 if (arg->request == RTLD_DI_SERINFOSIZE) {
2419 arg->serinfo->dls_cnt ++;
2420 arg->serinfo->dls_size += sizeof(Dl_serpath) + dirlen + 1;
2422 struct dl_serpath *s_entry;
2424 s_entry = arg->serpath;
2425 s_entry->dls_name = arg->strspace;
2426 s_entry->dls_flags = arg->flags;
2428 strncpy(arg->strspace, dir, dirlen);
2429 arg->strspace[dirlen] = '\0';
2431 arg->strspace += dirlen + 1;
2439 do_search_info(const Obj_Entry *obj, int request, struct dl_serinfo *info)
2441 struct dl_serinfo _info;
2442 struct fill_search_info_args args;
2444 args.request = RTLD_DI_SERINFOSIZE;
2445 args.serinfo = &_info;
2447 _info.dls_size = __offsetof(struct dl_serinfo, dls_serpath);
2450 path_enumerate(ld_library_path, fill_search_info, &args);
2451 path_enumerate(obj->rpath, fill_search_info, &args);
2452 path_enumerate(gethints(), fill_search_info, &args);
2453 path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args);
2456 if (request == RTLD_DI_SERINFOSIZE) {
2457 info->dls_size = _info.dls_size;
2458 info->dls_cnt = _info.dls_cnt;
2462 if (info->dls_cnt != _info.dls_cnt || info->dls_size != _info.dls_size) {
2463 _rtld_error("Uninitialized Dl_serinfo struct passed to dlinfo()");
2467 args.request = RTLD_DI_SERINFO;
2468 args.serinfo = info;
2469 args.serpath = &info->dls_serpath[0];
2470 args.strspace = (char *)&info->dls_serpath[_info.dls_cnt];
2472 args.flags = LA_SER_LIBPATH;
2473 if (path_enumerate(ld_library_path, fill_search_info, &args) != NULL)
2476 args.flags = LA_SER_RUNPATH;
2477 if (path_enumerate(obj->rpath, fill_search_info, &args) != NULL)
2480 args.flags = LA_SER_CONFIG;
2481 if (path_enumerate(gethints(), fill_search_info, &args) != NULL)
2484 args.flags = LA_SER_DEFAULT;
2485 if (path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args) != NULL)
2491 rtld_dirname(const char *path, char *bname)
2495 /* Empty or NULL string gets treated as "." */
2496 if (path == NULL || *path == '\0') {
2502 /* Strip trailing slashes */
2503 endp = path + strlen(path) - 1;
2504 while (endp > path && *endp == '/')
2507 /* Find the start of the dir */
2508 while (endp > path && *endp != '/')
2511 /* Either the dir is "/" or there are no slashes */
2513 bname[0] = *endp == '/' ? '/' : '.';
2519 } while (endp > path && *endp == '/');
2522 if (endp - path + 2 > PATH_MAX)
2524 _rtld_error("Filename is too long: %s", path);
2528 strncpy(bname, path, endp - path + 1);
2529 bname[endp - path + 1] = '\0';
2534 rtld_dirname_abs(const char *path, char *base)
2536 char base_rel[PATH_MAX];
2538 if (rtld_dirname(path, base) == -1)
2542 if (getcwd(base_rel, sizeof(base_rel)) == NULL ||
2543 strlcat(base_rel, "/", sizeof(base_rel)) >= sizeof(base_rel) ||
2544 strlcat(base_rel, base, sizeof(base_rel)) >= sizeof(base_rel))
2546 strcpy(base, base_rel);
2551 linkmap_add(Obj_Entry *obj)
2553 struct link_map *l = &obj->linkmap;
2554 struct link_map *prev;
2556 obj->linkmap.l_name = obj->path;
2557 obj->linkmap.l_addr = obj->mapbase;
2558 obj->linkmap.l_ld = obj->dynamic;
2560 /* GDB needs load offset on MIPS to use the symbols */
2561 obj->linkmap.l_offs = obj->relocbase;
2564 if (r_debug.r_map == NULL) {
2570 * Scan to the end of the list, but not past the entry for the
2571 * dynamic linker, which we want to keep at the very end.
2573 for (prev = r_debug.r_map;
2574 prev->l_next != NULL && prev->l_next != &obj_rtld.linkmap;
2575 prev = prev->l_next)
2578 /* Link in the new entry. */
2580 l->l_next = prev->l_next;
2581 if (l->l_next != NULL)
2582 l->l_next->l_prev = l;
2587 linkmap_delete(Obj_Entry *obj)
2589 struct link_map *l = &obj->linkmap;
2591 if (l->l_prev == NULL) {
2592 if ((r_debug.r_map = l->l_next) != NULL)
2593 l->l_next->l_prev = NULL;
2597 if ((l->l_prev->l_next = l->l_next) != NULL)
2598 l->l_next->l_prev = l->l_prev;
2602 * Function for the debugger to set a breakpoint on to gain control.
2604 * The two parameters allow the debugger to easily find and determine
2605 * what the runtime loader is doing and to whom it is doing it.
2607 * When the loadhook trap is hit (r_debug_state, set at program
2608 * initialization), the arguments can be found on the stack:
2610 * +8 struct link_map *m
2611 * +4 struct r_debug *rd
2615 r_debug_state(struct r_debug* rd, struct link_map *m)
2620 * Get address of the pointer variable in the main program.
2622 static const void **
2623 get_program_var_addr(const char *name)
2625 const Obj_Entry *obj;
2628 hash = elf_hash(name);
2629 for (obj = obj_main; obj != NULL; obj = obj->next) {
2632 if ((def = symlook_obj(name, hash, obj, NULL, 0)) != NULL) {
2635 addr = (const void **)(obj->relocbase + def->st_value);
2643 * Set a pointer variable in the main program to the given value. This
2644 * is used to set key variables such as "environ" before any of the
2645 * init functions are called.
2648 set_program_var(const char *name, const void *value)
2652 if ((addr = get_program_var_addr(name)) != NULL) {
2653 dbg("\"%s\": *%p <-- %p", name, addr, value);
2659 * Given a symbol name in a referencing object, find the corresponding
2660 * definition of the symbol. Returns a pointer to the symbol, or NULL if
2661 * no definition was found. Returns a pointer to the Obj_Entry of the
2662 * defining object via the reference parameter DEFOBJ_OUT.
2664 static const Elf_Sym *
2665 symlook_default(const char *name, unsigned long hash, const Obj_Entry *refobj,
2666 const Obj_Entry **defobj_out, const Ver_Entry *ventry, int flags)
2670 const Elf_Sym *symp;
2671 const Obj_Entry *obj;
2672 const Obj_Entry *defobj;
2673 const Objlist_Entry *elm;
2676 donelist_init(&donelist);
2678 /* Look first in the referencing object if linked symbolically. */
2679 if (refobj->symbolic && !donelist_check(&donelist, refobj)) {
2680 symp = symlook_obj(name, hash, refobj, ventry, flags);
2687 /* Search all objects loaded at program start up. */
2688 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
2689 symp = symlook_list(name, hash, &list_main, &obj, ventry, flags,
2692 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2698 /* Search all DAGs whose roots are RTLD_GLOBAL objects. */
2699 STAILQ_FOREACH(elm, &list_global, link) {
2700 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
2702 symp = symlook_list(name, hash, &elm->obj->dagmembers, &obj, ventry,
2705 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2711 /* Search all dlopened DAGs containing the referencing object. */
2712 STAILQ_FOREACH(elm, &refobj->dldags, link) {
2713 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
2715 symp = symlook_list(name, hash, &elm->obj->dagmembers, &obj, ventry,
2718 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2725 * Search the dynamic linker itself, and possibly resolve the
2726 * symbol from there. This is how the application links to
2727 * dynamic linker services such as dlopen. Only the values listed
2728 * in the "exports" array can be resolved from the dynamic linker.
2730 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
2731 symp = symlook_obj(name, hash, &obj_rtld, ventry, flags);
2732 if (symp != NULL && is_exported(symp)) {
2739 *defobj_out = defobj;
2743 static const Elf_Sym *
2744 symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
2745 const Obj_Entry **defobj_out, const Ver_Entry *ventry, int flags,
2748 const Elf_Sym *symp;
2750 const Obj_Entry *defobj;
2751 const Objlist_Entry *elm;
2755 STAILQ_FOREACH(elm, objlist, link) {
2756 if (donelist_check(dlp, elm->obj))
2758 if ((symp = symlook_obj(name, hash, elm->obj, ventry, flags)) != NULL) {
2759 if (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK) {
2762 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
2768 *defobj_out = defobj;
2773 * Search the symbol table of a shared object and all objects needed
2774 * by it for a symbol of the given name. Search order is
2775 * breadth-first. Returns a pointer to the symbol, or NULL if no
2776 * definition was found.
2778 static const Elf_Sym *
2779 symlook_needed(const char *name, unsigned long hash, const Needed_Entry *needed,
2780 const Obj_Entry **defobj_out, const Ver_Entry *ventry, int flags,
2783 const Elf_Sym *def, *def_w;
2784 const Needed_Entry *n;
2785 const Obj_Entry *obj, *defobj, *defobj1;
2789 for (n = needed; n != NULL; n = n->next) {
2790 if ((obj = n->obj) == NULL ||
2791 donelist_check(dlp, obj) ||
2792 (def = symlook_obj(name, hash, obj, ventry, flags)) == NULL)
2795 if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
2796 *defobj_out = defobj;
2801 * There we come when either symbol definition is not found in
2802 * directly needed objects, or found symbol is weak.
2804 for (n = needed; n != NULL; n = n->next) {
2805 if ((obj = n->obj) == NULL)
2807 def_w = symlook_needed(name, hash, obj->needed, &defobj1,
2808 ventry, flags, dlp);
2811 if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
2815 if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
2819 *defobj_out = defobj;
2824 * Search the symbol table of a single shared object for a symbol of
2825 * the given name and version, if requested. Returns a pointer to the
2826 * symbol, or NULL if no definition was found.
2828 * The symbol's hash value is passed in for efficiency reasons; that
2829 * eliminates many recomputations of the hash value.
2832 symlook_obj(const char *name, unsigned long hash, const Obj_Entry *obj,
2833 const Ver_Entry *ventry, int flags)
2835 unsigned long symnum;
2836 const Elf_Sym *vsymp;
2840 if (obj->buckets == NULL)
2845 symnum = obj->buckets[hash % obj->nbuckets];
2847 for (; symnum != STN_UNDEF; symnum = obj->chains[symnum]) {
2848 const Elf_Sym *symp;
2851 if (symnum >= obj->nchains)
2852 return NULL; /* Bad object */
2854 symp = obj->symtab + symnum;
2855 strp = obj->strtab + symp->st_name;
2857 switch (ELF_ST_TYPE(symp->st_info)) {
2861 if (symp->st_value == 0)
2865 if (symp->st_shndx != SHN_UNDEF)
2868 else if (((flags & SYMLOOK_IN_PLT) == 0) &&
2869 (ELF_ST_TYPE(symp->st_info) == STT_FUNC))
2876 if (name[0] != strp[0] || strcmp(name, strp) != 0)
2879 if (ventry == NULL) {
2880 if (obj->versyms != NULL) {
2881 verndx = VER_NDX(obj->versyms[symnum]);
2882 if (verndx > obj->vernum) {
2883 _rtld_error("%s: symbol %s references wrong version %d",
2884 obj->path, obj->strtab + symnum, verndx);
2888 * If we are not called from dlsym (i.e. this is a normal
2889 * relocation from unversioned binary, accept the symbol
2890 * immediately if it happens to have first version after
2891 * this shared object became versioned. Otherwise, if
2892 * symbol is versioned and not hidden, remember it. If it
2893 * is the only symbol with this name exported by the
2894 * shared object, it will be returned as a match at the
2895 * end of the function. If symbol is global (verndx < 2)
2896 * accept it unconditionally.
2898 if ((flags & SYMLOOK_DLSYM) == 0 && verndx == VER_NDX_GIVEN)
2900 else if (verndx >= VER_NDX_GIVEN) {
2901 if ((obj->versyms[symnum] & VER_NDX_HIDDEN) == 0) {
2911 if (obj->versyms == NULL) {
2912 if (object_match_name(obj, ventry->name)) {
2913 _rtld_error("%s: object %s should provide version %s for "
2914 "symbol %s", obj_rtld.path, obj->path, ventry->name,
2915 obj->strtab + symnum);
2919 verndx = VER_NDX(obj->versyms[symnum]);
2920 if (verndx > obj->vernum) {
2921 _rtld_error("%s: symbol %s references wrong version %d",
2922 obj->path, obj->strtab + symnum, verndx);
2925 if (obj->vertab[verndx].hash != ventry->hash ||
2926 strcmp(obj->vertab[verndx].name, ventry->name)) {
2928 * Version does not match. Look if this is a global symbol
2929 * and if it is not hidden. If global symbol (verndx < 2)
2930 * is available, use it. Do not return symbol if we are
2931 * called by dlvsym, because dlvsym looks for a specific
2932 * version and default one is not what dlvsym wants.
2934 if ((flags & SYMLOOK_DLSYM) ||
2935 (obj->versyms[symnum] & VER_NDX_HIDDEN) ||
2936 (verndx >= VER_NDX_GIVEN))
2943 return (vcount == 1) ? vsymp : NULL;
2947 trace_loaded_objects(Obj_Entry *obj)
2949 char *fmt1, *fmt2, *fmt, *main_local, *list_containers;
2952 if ((main_local = getenv(LD_ "TRACE_LOADED_OBJECTS_PROGNAME")) == NULL)
2955 if ((fmt1 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT1")) == NULL)
2956 fmt1 = "\t%o => %p (%x)\n";
2958 if ((fmt2 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT2")) == NULL)
2959 fmt2 = "\t%o (%x)\n";
2961 list_containers = getenv(LD_ "TRACE_LOADED_OBJECTS_ALL");
2963 for (; obj; obj = obj->next) {
2964 Needed_Entry *needed;
2968 if (list_containers && obj->needed != NULL)
2969 printf("%s:\n", obj->path);
2970 for (needed = obj->needed; needed; needed = needed->next) {
2971 if (needed->obj != NULL) {
2972 if (needed->obj->traced && !list_containers)
2974 needed->obj->traced = true;
2975 path = needed->obj->path;
2979 name = (char *)obj->strtab + needed->name;
2980 is_lib = strncmp(name, "lib", 3) == 0; /* XXX - bogus */
2982 fmt = is_lib ? fmt1 : fmt2;
2983 while ((c = *fmt++) != '\0') {
3009 printf("%s", main_local);
3012 printf("%s", obj_main->path);
3019 printf("%d", sodp->sod_major);
3022 printf("%d", sodp->sod_minor);
3029 printf("%p", needed->obj ? needed->obj->mapbase : 0);
3041 * Unload a dlopened object and its dependencies from memory and from
3042 * our data structures. It is assumed that the DAG rooted in the
3043 * object has already been unreferenced, and that the object has a
3044 * reference count of 0.
3047 unload_object(Obj_Entry *root)
3052 assert(root->refcount == 0);
3055 * Pass over the DAG removing unreferenced objects from
3056 * appropriate lists.
3058 unlink_object(root);
3060 /* Unmap all objects that are no longer referenced. */
3061 linkp = &obj_list->next;
3062 while ((obj = *linkp) != NULL) {
3063 if (obj->refcount == 0) {
3064 LD_UTRACE(UTRACE_UNLOAD_OBJECT, obj, obj->mapbase, obj->mapsize, 0,
3066 dbg("unloading \"%s\"", obj->path);
3067 munmap(obj->mapbase, obj->mapsize);
3068 linkmap_delete(obj);
3079 unlink_object(Obj_Entry *root)
3083 if (root->refcount == 0) {
3084 /* Remove the object from the RTLD_GLOBAL list. */
3085 objlist_remove(&list_global, root);
3087 /* Remove the object from all objects' DAG lists. */
3088 STAILQ_FOREACH(elm, &root->dagmembers, link) {
3089 objlist_remove(&elm->obj->dldags, root);
3090 if (elm->obj != root)
3091 unlink_object(elm->obj);
3097 ref_dag(Obj_Entry *root)
3101 assert(root->dag_inited);
3102 STAILQ_FOREACH(elm, &root->dagmembers, link)
3103 elm->obj->refcount++;
3107 unref_dag(Obj_Entry *root)
3111 assert(root->dag_inited);
3112 STAILQ_FOREACH(elm, &root->dagmembers, link)
3113 elm->obj->refcount--;
3117 * Common code for MD __tls_get_addr().
3120 tls_get_addr_common(Elf_Addr** dtvp, int index, size_t offset)
3122 Elf_Addr* dtv = *dtvp;
3125 /* Check dtv generation in case new modules have arrived */
3126 if (dtv[0] != tls_dtv_generation) {
3130 lockstate = wlock_acquire(rtld_bind_lock);
3131 newdtv = calloc(1, (tls_max_index + 2) * sizeof(Elf_Addr));
3133 if (to_copy > tls_max_index)
3134 to_copy = tls_max_index;
3135 memcpy(&newdtv[2], &dtv[2], to_copy * sizeof(Elf_Addr));
3136 newdtv[0] = tls_dtv_generation;
3137 newdtv[1] = tls_max_index;
3139 wlock_release(rtld_bind_lock, lockstate);
3140 dtv = *dtvp = newdtv;
3143 /* Dynamically allocate module TLS if necessary */
3144 if (!dtv[index + 1]) {
3145 /* Signal safe, wlock will block out signals. */
3146 lockstate = wlock_acquire(rtld_bind_lock);
3147 if (!dtv[index + 1])
3148 dtv[index + 1] = (Elf_Addr)allocate_module_tls(index);
3149 wlock_release(rtld_bind_lock, lockstate);
3151 return (void*) (dtv[index + 1] + offset);
3154 /* XXX not sure what variants to use for arm. */
3156 #if defined(__ia64__) || defined(__powerpc__)
3159 * Allocate Static TLS using the Variant I method.
3162 allocate_tls(Obj_Entry *objs, void *oldtcb, size_t tcbsize, size_t tcbalign)
3171 if (oldtcb != NULL && tcbsize == TLS_TCB_SIZE)
3174 assert(tcbsize >= TLS_TCB_SIZE);
3175 tcb = calloc(1, tls_static_space - TLS_TCB_SIZE + tcbsize);
3176 tls = (Elf_Addr **)(tcb + tcbsize - TLS_TCB_SIZE);
3178 if (oldtcb != NULL) {
3179 memcpy(tls, oldtcb, tls_static_space);
3182 /* Adjust the DTV. */
3184 for (i = 0; i < dtv[1]; i++) {
3185 if (dtv[i+2] >= (Elf_Addr)oldtcb &&
3186 dtv[i+2] < (Elf_Addr)oldtcb + tls_static_space) {
3187 dtv[i+2] = dtv[i+2] - (Elf_Addr)oldtcb + (Elf_Addr)tls;
3191 dtv = calloc(tls_max_index + 2, sizeof(Elf_Addr));
3193 dtv[0] = tls_dtv_generation;
3194 dtv[1] = tls_max_index;
3196 for (obj = objs; obj; obj = obj->next) {
3197 if (obj->tlsoffset) {
3198 addr = (Elf_Addr)tls + obj->tlsoffset;
3199 memset((void*) (addr + obj->tlsinitsize),
3200 0, obj->tlssize - obj->tlsinitsize);
3202 memcpy((void*) addr, obj->tlsinit,
3204 dtv[obj->tlsindex + 1] = addr;
3213 free_tls(void *tcb, size_t tcbsize, size_t tcbalign)
3216 Elf_Addr tlsstart, tlsend;
3219 assert(tcbsize >= TLS_TCB_SIZE);
3221 tlsstart = (Elf_Addr)tcb + tcbsize - TLS_TCB_SIZE;
3222 tlsend = tlsstart + tls_static_space;
3224 dtv = *(Elf_Addr **)tlsstart;
3226 for (i = 0; i < dtvsize; i++) {
3227 if (dtv[i+2] && (dtv[i+2] < tlsstart || dtv[i+2] >= tlsend)) {
3228 free((void*)dtv[i+2]);
3237 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \
3238 defined(__arm__) || defined(__mips__)
3241 * Allocate Static TLS using the Variant II method.
3244 allocate_tls(Obj_Entry *objs, void *oldtls, size_t tcbsize, size_t tcbalign)
3249 Elf_Addr *dtv, *olddtv;
3250 Elf_Addr segbase, oldsegbase, addr;
3253 size = round(tls_static_space, tcbalign);
3255 assert(tcbsize >= 2*sizeof(Elf_Addr));
3256 tls = calloc(1, size + tcbsize);
3257 dtv = calloc(1, (tls_max_index + 2) * sizeof(Elf_Addr));
3259 segbase = (Elf_Addr)(tls + size);
3260 ((Elf_Addr*)segbase)[0] = segbase;
3261 ((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv;
3263 dtv[0] = tls_dtv_generation;
3264 dtv[1] = tls_max_index;
3268 * Copy the static TLS block over whole.
3270 oldsegbase = (Elf_Addr) oldtls;
3271 memcpy((void *)(segbase - tls_static_space),
3272 (const void *)(oldsegbase - tls_static_space),
3276 * If any dynamic TLS blocks have been created tls_get_addr(),
3279 olddtv = ((Elf_Addr**)oldsegbase)[1];
3280 for (i = 0; i < olddtv[1]; i++) {
3281 if (olddtv[i+2] < oldsegbase - size || olddtv[i+2] > oldsegbase) {
3282 dtv[i+2] = olddtv[i+2];
3288 * We assume that this block was the one we created with
3289 * allocate_initial_tls().
3291 free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr));
3293 for (obj = objs; obj; obj = obj->next) {
3294 if (obj->tlsoffset) {
3295 addr = segbase - obj->tlsoffset;
3296 memset((void*) (addr + obj->tlsinitsize),
3297 0, obj->tlssize - obj->tlsinitsize);
3299 memcpy((void*) addr, obj->tlsinit, obj->tlsinitsize);
3300 dtv[obj->tlsindex + 1] = addr;
3305 return (void*) segbase;
3309 free_tls(void *tls, size_t tcbsize, size_t tcbalign)
3314 Elf_Addr tlsstart, tlsend;
3317 * Figure out the size of the initial TLS block so that we can
3318 * find stuff which ___tls_get_addr() allocated dynamically.
3320 size = round(tls_static_space, tcbalign);
3322 dtv = ((Elf_Addr**)tls)[1];
3324 tlsend = (Elf_Addr) tls;
3325 tlsstart = tlsend - size;
3326 for (i = 0; i < dtvsize; i++) {
3327 if (dtv[i+2] && (dtv[i+2] < tlsstart || dtv[i+2] > tlsend)) {
3328 free((void*) dtv[i+2]);
3332 free((void*) tlsstart);
3339 * Allocate TLS block for module with given index.
3342 allocate_module_tls(int index)
3347 for (obj = obj_list; obj; obj = obj->next) {
3348 if (obj->tlsindex == index)
3352 _rtld_error("Can't find module with TLS index %d", index);
3356 p = malloc(obj->tlssize);
3357 memcpy(p, obj->tlsinit, obj->tlsinitsize);
3358 memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize);
3364 allocate_tls_offset(Obj_Entry *obj)
3371 if (obj->tlssize == 0) {
3372 obj->tls_done = true;
3376 if (obj->tlsindex == 1)
3377 off = calculate_first_tls_offset(obj->tlssize, obj->tlsalign);
3379 off = calculate_tls_offset(tls_last_offset, tls_last_size,
3380 obj->tlssize, obj->tlsalign);
3383 * If we have already fixed the size of the static TLS block, we
3384 * must stay within that size. When allocating the static TLS, we
3385 * leave a small amount of space spare to be used for dynamically
3386 * loading modules which use static TLS.
3388 if (tls_static_space) {
3389 if (calculate_tls_end(off, obj->tlssize) > tls_static_space)
3393 tls_last_offset = obj->tlsoffset = off;
3394 tls_last_size = obj->tlssize;
3395 obj->tls_done = true;
3401 free_tls_offset(Obj_Entry *obj)
3403 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \
3404 defined(__arm__) || defined(__mips__)
3406 * If we were the last thing to allocate out of the static TLS
3407 * block, we give our space back to the 'allocator'. This is a
3408 * simplistic workaround to allow libGL.so.1 to be loaded and
3409 * unloaded multiple times. We only handle the Variant II
3410 * mechanism for now - this really needs a proper allocator.
3412 if (calculate_tls_end(obj->tlsoffset, obj->tlssize)
3413 == calculate_tls_end(tls_last_offset, tls_last_size)) {
3414 tls_last_offset -= obj->tlssize;
3421 _rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
3426 lockstate = wlock_acquire(rtld_bind_lock);
3427 ret = allocate_tls(obj_list, oldtls, tcbsize, tcbalign);
3428 wlock_release(rtld_bind_lock, lockstate);
3433 _rtld_free_tls(void *tcb, size_t tcbsize, size_t tcbalign)
3437 lockstate = wlock_acquire(rtld_bind_lock);
3438 free_tls(tcb, tcbsize, tcbalign);
3439 wlock_release(rtld_bind_lock, lockstate);
3443 object_add_name(Obj_Entry *obj, const char *name)
3449 entry = malloc(sizeof(Name_Entry) + len);
3451 if (entry != NULL) {
3452 strcpy(entry->name, name);
3453 STAILQ_INSERT_TAIL(&obj->names, entry, link);
3458 object_match_name(const Obj_Entry *obj, const char *name)
3462 STAILQ_FOREACH(entry, &obj->names, link) {
3463 if (strcmp(name, entry->name) == 0)
3470 locate_dependency(const Obj_Entry *obj, const char *name)
3472 const Objlist_Entry *entry;
3473 const Needed_Entry *needed;
3475 STAILQ_FOREACH(entry, &list_main, link) {
3476 if (object_match_name(entry->obj, name))
3480 for (needed = obj->needed; needed != NULL; needed = needed->next) {
3481 if (strcmp(obj->strtab + needed->name, name) == 0 ||
3482 (needed->obj != NULL && object_match_name(needed->obj, name))) {
3484 * If there is DT_NEEDED for the name we are looking for,
3485 * we are all set. Note that object might not be found if
3486 * dependency was not loaded yet, so the function can
3487 * return NULL here. This is expected and handled
3488 * properly by the caller.
3490 return (needed->obj);
3493 _rtld_error("%s: Unexpected inconsistency: dependency %s not found",
3499 check_object_provided_version(Obj_Entry *refobj, const Obj_Entry *depobj,
3500 const Elf_Vernaux *vna)
3502 const Elf_Verdef *vd;
3503 const char *vername;
3505 vername = refobj->strtab + vna->vna_name;
3506 vd = depobj->verdef;
3508 _rtld_error("%s: version %s required by %s not defined",
3509 depobj->path, vername, refobj->path);
3513 if (vd->vd_version != VER_DEF_CURRENT) {
3514 _rtld_error("%s: Unsupported version %d of Elf_Verdef entry",
3515 depobj->path, vd->vd_version);
3518 if (vna->vna_hash == vd->vd_hash) {
3519 const Elf_Verdaux *aux = (const Elf_Verdaux *)
3520 ((char *)vd + vd->vd_aux);
3521 if (strcmp(vername, depobj->strtab + aux->vda_name) == 0)
3524 if (vd->vd_next == 0)
3526 vd = (const Elf_Verdef *) ((char *)vd + vd->vd_next);
3528 if (vna->vna_flags & VER_FLG_WEAK)
3530 _rtld_error("%s: version %s required by %s not found",
3531 depobj->path, vername, refobj->path);
3536 rtld_verify_object_versions(Obj_Entry *obj)
3538 const Elf_Verneed *vn;
3539 const Elf_Verdef *vd;
3540 const Elf_Verdaux *vda;
3541 const Elf_Vernaux *vna;
3542 const Obj_Entry *depobj;
3543 int maxvernum, vernum;
3547 * Walk over defined and required version records and figure out
3548 * max index used by any of them. Do very basic sanity checking
3552 while (vn != NULL) {
3553 if (vn->vn_version != VER_NEED_CURRENT) {
3554 _rtld_error("%s: Unsupported version %d of Elf_Verneed entry",
3555 obj->path, vn->vn_version);
3558 vna = (const Elf_Vernaux *) ((char *)vn + vn->vn_aux);
3560 vernum = VER_NEED_IDX(vna->vna_other);
3561 if (vernum > maxvernum)
3563 if (vna->vna_next == 0)
3565 vna = (const Elf_Vernaux *) ((char *)vna + vna->vna_next);
3567 if (vn->vn_next == 0)
3569 vn = (const Elf_Verneed *) ((char *)vn + vn->vn_next);
3573 while (vd != NULL) {
3574 if (vd->vd_version != VER_DEF_CURRENT) {
3575 _rtld_error("%s: Unsupported version %d of Elf_Verdef entry",
3576 obj->path, vd->vd_version);
3579 vernum = VER_DEF_IDX(vd->vd_ndx);
3580 if (vernum > maxvernum)
3582 if (vd->vd_next == 0)
3584 vd = (const Elf_Verdef *) ((char *)vd + vd->vd_next);
3591 * Store version information in array indexable by version index.
3592 * Verify that object version requirements are satisfied along the
3595 obj->vernum = maxvernum + 1;
3596 obj->vertab = calloc(obj->vernum, sizeof(Ver_Entry));
3599 while (vd != NULL) {
3600 if ((vd->vd_flags & VER_FLG_BASE) == 0) {
3601 vernum = VER_DEF_IDX(vd->vd_ndx);
3602 assert(vernum <= maxvernum);
3603 vda = (const Elf_Verdaux *)((char *)vd + vd->vd_aux);
3604 obj->vertab[vernum].hash = vd->vd_hash;
3605 obj->vertab[vernum].name = obj->strtab + vda->vda_name;
3606 obj->vertab[vernum].file = NULL;
3607 obj->vertab[vernum].flags = 0;
3609 if (vd->vd_next == 0)
3611 vd = (const Elf_Verdef *) ((char *)vd + vd->vd_next);
3615 while (vn != NULL) {
3616 depobj = locate_dependency(obj, obj->strtab + vn->vn_file);
3619 vna = (const Elf_Vernaux *) ((char *)vn + vn->vn_aux);
3621 if (check_object_provided_version(obj, depobj, vna))
3623 vernum = VER_NEED_IDX(vna->vna_other);
3624 assert(vernum <= maxvernum);
3625 obj->vertab[vernum].hash = vna->vna_hash;
3626 obj->vertab[vernum].name = obj->strtab + vna->vna_name;
3627 obj->vertab[vernum].file = obj->strtab + vn->vn_file;
3628 obj->vertab[vernum].flags = (vna->vna_other & VER_NEED_HIDDEN) ?
3629 VER_INFO_HIDDEN : 0;
3630 if (vna->vna_next == 0)
3632 vna = (const Elf_Vernaux *) ((char *)vna + vna->vna_next);
3634 if (vn->vn_next == 0)
3636 vn = (const Elf_Verneed *) ((char *)vn + vn->vn_next);
3642 rtld_verify_versions(const Objlist *objlist)
3644 Objlist_Entry *entry;
3648 STAILQ_FOREACH(entry, objlist, link) {
3650 * Skip dummy objects or objects that have their version requirements
3653 if (entry->obj->strtab == NULL || entry->obj->vertab != NULL)
3655 if (rtld_verify_object_versions(entry->obj) == -1) {
3657 if (ld_tracing == NULL)
3661 if (rc == 0 || ld_tracing != NULL)
3662 rc = rtld_verify_object_versions(&obj_rtld);
3667 fetch_ventry(const Obj_Entry *obj, unsigned long symnum)
3672 vernum = VER_NDX(obj->versyms[symnum]);
3673 if (vernum >= obj->vernum) {
3674 _rtld_error("%s: symbol %s has wrong verneed value %d",
3675 obj->path, obj->strtab + symnum, vernum);
3676 } else if (obj->vertab[vernum].hash != 0) {
3677 return &obj->vertab[vernum];