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/ktrace.h>
62 #define PATH_RTLD "/libexec/ld-elf.so.1"
64 #define PATH_RTLD "/libexec/ld-elf32.so.1"
68 typedef void (*func_ptr_type
)();
69 typedef void * (*path_enum_proc
) (const char *path
, size_t len
, void *arg
);
72 * This structure provides a reentrant way to keep a list of objects and
73 * check which ones have already been processed in some way.
75 typedef struct Struct_DoneList
{
76 const Obj_Entry
**objs
; /* Array of object pointers */
77 unsigned int num_alloc
; /* Allocated size of the array */
78 unsigned int num_used
; /* Number of array slots used */
82 * Function declarations.
84 static const char *basename(const char *);
85 static void die(void) __dead2
;
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
*do_load_object(int, const char *, char *, struct stat
*);
90 static int do_search_info(const Obj_Entry
*obj
, int, struct dl_serinfo
*);
91 static bool donelist_check(DoneList
*, const Obj_Entry
*);
92 static void errmsg_restore(char *);
93 static char *errmsg_save(void);
94 static void *fill_search_info(const char *, size_t, void *);
95 static char *find_library(const char *, const Obj_Entry
*);
96 static const char *gethints(void);
97 static void init_dag(Obj_Entry
*);
98 static void init_dag1(Obj_Entry
*, Obj_Entry
*, DoneList
*);
99 static void init_rtld(caddr_t
);
100 static void initlist_add_neededs(Needed_Entry
*, Objlist
*);
101 static void initlist_add_objects(Obj_Entry
*, Obj_Entry
**, Objlist
*);
102 static bool is_exported(const Elf_Sym
*);
103 static void linkmap_add(Obj_Entry
*);
104 static void linkmap_delete(Obj_Entry
*);
105 static int load_needed_objects(Obj_Entry
*);
106 static int load_preload_objects(void);
107 static Obj_Entry
*load_object(const char *, const Obj_Entry
*);
108 static Obj_Entry
*obj_from_addr(const void *);
109 static void objlist_call_fini(Objlist
*, int *lockstate
);
110 static void objlist_call_init(Objlist
*, int *lockstate
);
111 static void objlist_clear(Objlist
*);
112 static Objlist_Entry
*objlist_find(Objlist
*, const Obj_Entry
*);
113 static void objlist_init(Objlist
*);
114 static void objlist_push_head(Objlist
*, Obj_Entry
*);
115 static void objlist_push_tail(Objlist
*, Obj_Entry
*);
116 static void objlist_remove(Objlist
*, Obj_Entry
*);
117 static void objlist_remove_unref(Objlist
*);
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 void rtld_exit(void);
122 static char *search_library_path(const char *, const char *);
123 static const void **get_program_var_addr(const char *);
124 static void set_program_var(const char *, const void *);
125 static const Elf_Sym
*symlook_default(const char *, unsigned long,
126 const Obj_Entry
*, const Obj_Entry
**, const Ver_Entry
*, int);
127 static const Elf_Sym
*symlook_list(const char *, unsigned long, const Objlist
*,
128 const Obj_Entry
**, const Ver_Entry
*, int, DoneList
*);
129 static const Elf_Sym
*symlook_needed(const char *, unsigned long,
130 const Needed_Entry
*, const Obj_Entry
**, const Ver_Entry
*,
132 static void trace_loaded_objects(Obj_Entry
*);
133 static void unlink_object(Obj_Entry
*);
134 static void unload_object(Obj_Entry
*);
135 static void unref_dag(Obj_Entry
*);
136 static void ref_dag(Obj_Entry
*);
137 static int rtld_verify_versions(const Objlist
*);
138 static int rtld_verify_object_versions(Obj_Entry
*);
139 static void object_add_name(Obj_Entry
*, const char *);
140 static int object_match_name(const Obj_Entry
*, const char *);
141 static void ld_utrace_log(int, void *, void *, size_t, int, const char *);
143 void r_debug_state(struct r_debug
*, struct link_map
*);
148 static char *error_message
; /* Message for dlerror(), or NULL */
149 struct r_debug r_debug
; /* for GDB; */
150 static bool libmap_disable
; /* Disable libmap */
151 static char *libmap_override
; /* Maps to use in addition to libmap.conf */
152 static bool trust
; /* False for setuid and setgid programs */
153 static bool dangerous_ld_env
; /* True if environment variables have been
154 used to affect the libraries loaded */
155 static char *ld_bind_now
; /* Environment variable for immediate binding */
156 static char *ld_debug
; /* Environment variable for debugging */
157 static char *ld_library_path
; /* Environment variable for search path */
158 static char *ld_preload
; /* Environment variable for libraries to
160 static char *ld_tracing
; /* Called from ldd to print libs */
161 static char *ld_utrace
; /* Use utrace() to log events. */
162 static Obj_Entry
*obj_list
; /* Head of linked list of shared objects */
163 static Obj_Entry
**obj_tail
; /* Link field of last object in list */
164 static Obj_Entry
*obj_main
; /* The main program shared object */
165 static Obj_Entry obj_rtld
; /* The dynamic linker shared object */
166 static unsigned int obj_count
; /* Number of objects in obj_list */
167 static unsigned int obj_loads
; /* Number of objects in obj_list */
169 static Objlist list_global
= /* Objects dlopened with RTLD_GLOBAL */
170 STAILQ_HEAD_INITIALIZER(list_global
);
171 static Objlist list_main
= /* Objects loaded at program startup */
172 STAILQ_HEAD_INITIALIZER(list_main
);
173 static Objlist list_fini
= /* Objects needing fini() calls */
174 STAILQ_HEAD_INITIALIZER(list_fini
);
176 static Elf_Sym sym_zero
; /* For resolving undefined weak refs. */
178 #define GDB_STATE(s,m) r_debug.r_state = s; r_debug_state(&r_debug,m);
180 extern Elf_Dyn _DYNAMIC
;
181 #pragma weak _DYNAMIC
182 #ifndef RTLD_IS_DYNAMIC
183 #define RTLD_IS_DYNAMIC() (&_DYNAMIC != NULL)
187 * These are the functions the dynamic linker exports to application
188 * programs. They are the only symbols the dynamic linker is willing
189 * to export from itself.
191 static func_ptr_type exports
[] = {
192 (func_ptr_type
) &_rtld_error
,
193 (func_ptr_type
) &dlclose
,
194 (func_ptr_type
) &dlerror
,
195 (func_ptr_type
) &dlopen
,
196 (func_ptr_type
) &dlsym
,
197 (func_ptr_type
) &dlvsym
,
198 (func_ptr_type
) &dladdr
,
199 (func_ptr_type
) &dllockinit
,
200 (func_ptr_type
) &dlinfo
,
201 (func_ptr_type
) &_rtld_thread_init
,
203 (func_ptr_type
) &___tls_get_addr
,
205 (func_ptr_type
) &__tls_get_addr
,
206 (func_ptr_type
) &_rtld_allocate_tls
,
207 (func_ptr_type
) &_rtld_free_tls
,
208 (func_ptr_type
) &dl_iterate_phdr
,
213 * Global declarations normally provided by crt1. The dynamic linker is
214 * not built with crt1, so we have to provide them ourselves.
220 * Globals to control TLS allocation.
222 size_t tls_last_offset
; /* Static TLS offset of last module */
223 size_t tls_last_size
; /* Static TLS size of last module */
224 size_t tls_static_space
; /* Static TLS space allocated */
225 int tls_dtv_generation
= 1; /* Used to detect when dtv size changes */
226 int tls_max_index
= 1; /* Largest module index allocated */
229 * Fill in a DoneList with an allocation large enough to hold all of
230 * the currently-loaded objects. Keep this as a macro since it calls
231 * alloca and we want that to occur within the scope of the caller.
233 #define donelist_init(dlp) \
234 ((dlp)->objs = alloca(obj_count * sizeof (dlp)->objs[0]), \
235 assert((dlp)->objs != NULL), \
236 (dlp)->num_alloc = obj_count, \
239 #define UTRACE_DLOPEN_START 1
240 #define UTRACE_DLOPEN_STOP 2
241 #define UTRACE_DLCLOSE_START 3
242 #define UTRACE_DLCLOSE_STOP 4
243 #define UTRACE_LOAD_OBJECT 5
244 #define UTRACE_UNLOAD_OBJECT 6
245 #define UTRACE_ADD_RUNDEP 7
246 #define UTRACE_PRELOAD_FINISHED 8
247 #define UTRACE_INIT_CALL 9
248 #define UTRACE_FINI_CALL 10
251 char sig
[4]; /* 'RTLD' */
254 void *mapbase
; /* Used for 'parent' and 'init/fini' */
256 int refcnt
; /* Used for 'mode' */
257 char name
[MAXPATHLEN
];
260 #define LD_UTRACE(e, h, mb, ms, r, n) do { \
261 if (ld_utrace != NULL) \
262 ld_utrace_log(e, h, mb, ms, r, n); \
266 ld_utrace_log(int event
, void *handle
, void *mapbase
, size_t mapsize
,
267 int refcnt
, const char *name
)
269 struct utrace_rtld ut
;
277 ut
.mapbase
= mapbase
;
278 ut
.mapsize
= mapsize
;
280 bzero(ut
.name
, sizeof(ut
.name
));
282 strlcpy(ut
.name
, name
, sizeof(ut
.name
));
283 utrace(&ut
, sizeof(ut
));
287 * Main entry point for dynamic linking. The first argument is the
288 * stack pointer. The stack is expected to be laid out as described
289 * in the SVR4 ABI specification, Intel 386 Processor Supplement.
290 * Specifically, the stack pointer points to a word containing
291 * ARGC. Following that in the stack is a null-terminated sequence
292 * of pointers to argument strings. Then comes a null-terminated
293 * sequence of pointers to environment strings. Finally, there is a
294 * sequence of "auxiliary vector" entries.
296 * The second argument points to a place to store the dynamic linker's
297 * exit procedure pointer and the third to a place to store the main
300 * The return value is the main program's entry point.
303 _rtld(Elf_Addr
*sp
, func_ptr_type
*exit_proc
, Obj_Entry
**objp
)
305 Elf_Auxinfo
*aux_info
[AT_COUNT
];
313 Objlist_Entry
*entry
;
315 Obj_Entry
**preload_tail
;
320 * On entry, the dynamic linker itself has not been relocated yet.
321 * Be very careful not to reference any global data until after
322 * init_rtld has returned. It is OK to reference file-scope statics
323 * and string constants, and to call static and global functions.
326 /* Find the auxiliary vector on the stack. */
329 sp
+= argc
+ 1; /* Skip over arguments and NULL terminator */
331 while (*sp
++ != 0) /* Skip over environment, and NULL terminator */
333 aux
= (Elf_Auxinfo
*) sp
;
335 /* Digest the auxiliary vector. */
336 for (i
= 0; i
< AT_COUNT
; i
++)
338 for (auxp
= aux
; auxp
->a_type
!= AT_NULL
; auxp
++) {
339 if (auxp
->a_type
< AT_COUNT
)
340 aux_info
[auxp
->a_type
] = auxp
;
343 /* Initialize and relocate ourselves. */
344 assert(aux_info
[AT_BASE
] != NULL
);
345 init_rtld((caddr_t
) aux_info
[AT_BASE
]->a_un
.a_ptr
);
347 __progname
= obj_rtld
.path
;
348 argv0
= argv
[0] != NULL
? argv
[0] : "(null)";
351 trust
= !issetugid();
353 ld_bind_now
= getenv(LD_
"BIND_NOW");
355 * If the process is tainted, then we un-set the dangerous environment
356 * variables. The process will be marked as tainted until setuid(2)
357 * is called. If any child process calls setuid(2) we do not want any
358 * future processes to honor the potentially un-safe variables.
361 unsetenv(LD_
"PRELOAD");
362 unsetenv(LD_
"LIBMAP");
363 unsetenv(LD_
"LIBRARY_PATH");
364 unsetenv(LD_
"LIBMAP_DISABLE");
365 unsetenv(LD_
"DEBUG");
367 ld_debug
= getenv(LD_
"DEBUG");
368 libmap_disable
= getenv(LD_
"LIBMAP_DISABLE") != NULL
;
369 libmap_override
= getenv(LD_
"LIBMAP");
370 ld_library_path
= getenv(LD_
"LIBRARY_PATH");
371 ld_preload
= getenv(LD_
"PRELOAD");
372 dangerous_ld_env
= libmap_disable
|| (libmap_override
!= NULL
) ||
373 (ld_library_path
!= NULL
) || (ld_preload
!= NULL
);
374 ld_tracing
= getenv(LD_
"TRACE_LOADED_OBJECTS");
375 ld_utrace
= getenv(LD_
"UTRACE");
377 if (ld_debug
!= NULL
&& *ld_debug
!= '\0')
379 dbg("%s is initialized, base address = %p", __progname
,
380 (caddr_t
) aux_info
[AT_BASE
]->a_un
.a_ptr
);
381 dbg("RTLD dynamic = %p", obj_rtld
.dynamic
);
382 dbg("RTLD pltgot = %p", obj_rtld
.pltgot
);
385 * Load the main program, or process its program header if it is
388 if (aux_info
[AT_EXECFD
] != NULL
) { /* Load the main program. */
389 int fd
= aux_info
[AT_EXECFD
]->a_un
.a_val
;
390 dbg("loading main program");
391 obj_main
= map_object(fd
, argv0
, NULL
);
393 if (obj_main
== NULL
)
395 } else { /* Main program already loaded. */
396 const Elf_Phdr
*phdr
;
400 dbg("processing main program's program header");
401 assert(aux_info
[AT_PHDR
] != NULL
);
402 phdr
= (const Elf_Phdr
*) aux_info
[AT_PHDR
]->a_un
.a_ptr
;
403 assert(aux_info
[AT_PHNUM
] != NULL
);
404 phnum
= aux_info
[AT_PHNUM
]->a_un
.a_val
;
405 assert(aux_info
[AT_PHENT
] != NULL
);
406 assert(aux_info
[AT_PHENT
]->a_un
.a_val
== sizeof(Elf_Phdr
));
407 assert(aux_info
[AT_ENTRY
] != NULL
);
408 entry
= (caddr_t
) aux_info
[AT_ENTRY
]->a_un
.a_ptr
;
409 if ((obj_main
= digest_phdr(phdr
, phnum
, entry
, argv0
)) == NULL
)
413 obj_main
->path
= xstrdup(argv0
);
414 obj_main
->mainprog
= true;
417 * Get the actual dynamic linker pathname from the executable if
418 * possible. (It should always be possible.) That ensures that
419 * gdb will find the right dynamic linker even if a non-standard
422 if (obj_main
->interp
!= NULL
&&
423 strcmp(obj_main
->interp
, obj_rtld
.path
) != 0) {
425 obj_rtld
.path
= xstrdup(obj_main
->interp
);
426 __progname
= obj_rtld
.path
;
429 digest_dynamic(obj_main
, 0);
431 linkmap_add(obj_main
);
432 linkmap_add(&obj_rtld
);
434 /* Link the main program into the list of objects. */
435 *obj_tail
= obj_main
;
436 obj_tail
= &obj_main
->next
;
439 /* Make sure we don't call the main program's init and fini functions. */
440 obj_main
->init
= obj_main
->fini
= (Elf_Addr
)NULL
;
442 /* Initialize a fake symbol for resolving undefined weak references. */
443 sym_zero
.st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_NOTYPE
);
444 sym_zero
.st_shndx
= SHN_UNDEF
;
447 libmap_disable
= (bool)lm_init(libmap_override
);
449 dbg("loading LD_PRELOAD libraries");
450 if (load_preload_objects() == -1)
452 preload_tail
= obj_tail
;
454 dbg("loading needed objects");
455 if (load_needed_objects(obj_main
) == -1)
458 /* Make a list of all objects loaded at startup. */
459 for (obj
= obj_list
; obj
!= NULL
; obj
= obj
->next
) {
460 objlist_push_tail(&list_main
, obj
);
464 dbg("checking for required versions");
465 if (rtld_verify_versions(&list_main
) == -1 && !ld_tracing
)
468 if (ld_tracing
) { /* We're done */
469 trace_loaded_objects(obj_main
);
473 if (getenv(LD_
"DUMP_REL_PRE") != NULL
) {
474 dump_relocations(obj_main
);
478 /* setup TLS for main thread */
479 dbg("initializing initial thread local storage");
480 STAILQ_FOREACH(entry
, &list_main
, link
) {
482 * Allocate all the initial objects out of the static TLS
483 * block even if they didn't ask for it.
485 allocate_tls_offset(entry
->obj
);
487 allocate_initial_tls(obj_list
);
489 if (relocate_objects(obj_main
,
490 ld_bind_now
!= NULL
&& *ld_bind_now
!= '\0', &obj_rtld
) == -1)
493 dbg("doing copy relocations");
494 if (do_copy_relocations(obj_main
) == -1)
497 if (getenv(LD_
"DUMP_REL_POST") != NULL
) {
498 dump_relocations(obj_main
);
502 dbg("initializing key program variables");
503 set_program_var("__progname", argv
[0] != NULL
? basename(argv
[0]) : "");
504 set_program_var("environ", env
);
506 dbg("initializing thread locks");
509 /* Make a list of init functions to call. */
510 objlist_init(&initlist
);
511 initlist_add_objects(obj_list
, preload_tail
, &initlist
);
513 r_debug_state(NULL
, &obj_main
->linkmap
); /* say hello to gdb! */
515 lockstate
= wlock_acquire(rtld_bind_lock
);
516 objlist_call_init(&initlist
, &lockstate
);
517 objlist_clear(&initlist
);
518 wlock_release(rtld_bind_lock
, lockstate
);
520 dbg("transferring control to program entry point = %p", obj_main
->entry
);
522 /* Return the exit procedure and the program entry point. */
523 *exit_proc
= rtld_exit
;
525 return (func_ptr_type
) obj_main
->entry
;
529 _rtld_bind(Obj_Entry
*obj
, Elf_Size reloff
)
533 const Obj_Entry
*defobj
;
538 lockstate
= rlock_acquire(rtld_bind_lock
);
540 rel
= (const Elf_Rel
*) ((caddr_t
) obj
->pltrel
+ reloff
);
542 rel
= (const Elf_Rel
*) ((caddr_t
) obj
->pltrela
+ reloff
);
544 where
= (Elf_Addr
*) (obj
->relocbase
+ rel
->r_offset
);
545 def
= find_symdef(ELF_R_SYM(rel
->r_info
), obj
, &defobj
, true, NULL
);
549 target
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
551 dbg("\"%s\" in \"%s\" ==> %p in \"%s\"",
552 defobj
->strtab
+ def
->st_name
, basename(obj
->path
),
553 (void *)target
, basename(defobj
->path
));
556 * Write the new contents for the jmpslot. Note that depending on
557 * architecture, the value which we need to return back to the
558 * lazy binding trampoline may or may not be the target
559 * address. The value returned from reloc_jmpslot() is the value
560 * that the trampoline needs.
562 target
= reloc_jmpslot(where
, target
, defobj
, obj
, rel
);
563 rlock_release(rtld_bind_lock
, lockstate
);
568 * Error reporting function. Use it like printf. If formats the message
569 * into a buffer, and sets things up so that the next call to dlerror()
570 * will return the message.
573 _rtld_error(const char *fmt
, ...)
575 static char buf
[512];
579 vsnprintf(buf
, sizeof buf
, fmt
, ap
);
585 * Return a dynamically-allocated copy of the current error message, if any.
590 return error_message
== NULL
? NULL
: xstrdup(error_message
);
594 * Restore the current error message from a copy which was previously saved
595 * by errmsg_save(). The copy is freed.
598 errmsg_restore(char *saved_msg
)
600 if (saved_msg
== NULL
)
601 error_message
= NULL
;
603 _rtld_error("%s", saved_msg
);
609 basename(const char *name
)
611 const char *p
= strrchr(name
, '/');
612 return p
!= NULL
? p
+ 1 : name
;
618 const char *msg
= dlerror();
626 * Process a shared object's DYNAMIC section, and save the important
627 * information in its Obj_Entry structure.
630 digest_dynamic(Obj_Entry
*obj
, int early
)
633 Needed_Entry
**needed_tail
= &obj
->needed
;
634 const Elf_Dyn
*dyn_rpath
= NULL
;
635 const Elf_Dyn
*dyn_soname
= NULL
;
636 int plttype
= DT_REL
;
638 obj
->bind_now
= false;
639 for (dynp
= obj
->dynamic
; dynp
->d_tag
!= DT_NULL
; dynp
++) {
640 switch (dynp
->d_tag
) {
643 obj
->rel
= (const Elf_Rel
*) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
647 obj
->relsize
= dynp
->d_un
.d_val
;
651 assert(dynp
->d_un
.d_val
== sizeof(Elf_Rel
));
655 obj
->pltrel
= (const Elf_Rel
*)
656 (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
660 obj
->pltrelsize
= dynp
->d_un
.d_val
;
664 obj
->rela
= (const Elf_Rela
*) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
668 obj
->relasize
= dynp
->d_un
.d_val
;
672 assert(dynp
->d_un
.d_val
== sizeof(Elf_Rela
));
676 plttype
= dynp
->d_un
.d_val
;
677 assert(dynp
->d_un
.d_val
== DT_REL
|| plttype
== DT_RELA
);
681 obj
->symtab
= (const Elf_Sym
*)
682 (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
686 assert(dynp
->d_un
.d_val
== sizeof(Elf_Sym
));
690 obj
->strtab
= (const char *) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
694 obj
->strsize
= dynp
->d_un
.d_val
;
698 obj
->verneed
= (const Elf_Verneed
*) (obj
->relocbase
+
703 obj
->verneednum
= dynp
->d_un
.d_val
;
707 obj
->verdef
= (const Elf_Verdef
*) (obj
->relocbase
+
712 obj
->verdefnum
= dynp
->d_un
.d_val
;
716 obj
->versyms
= (const Elf_Versym
*)(obj
->relocbase
+
722 const Elf_Hashelt
*hashtab
= (const Elf_Hashelt
*)
723 (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
724 obj
->nbuckets
= hashtab
[0];
725 obj
->nchains
= hashtab
[1];
726 obj
->buckets
= hashtab
+ 2;
727 obj
->chains
= obj
->buckets
+ obj
->nbuckets
;
733 Needed_Entry
*nep
= NEW(Needed_Entry
);
734 nep
->name
= dynp
->d_un
.d_val
;
739 needed_tail
= &nep
->next
;
744 obj
->pltgot
= (Elf_Addr
*) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
752 obj
->symbolic
= true;
756 case DT_RUNPATH
: /* XXX: process separately */
758 * We have to wait until later to process this, because we
759 * might not have gotten the address of the string table yet.
769 obj
->init
= (Elf_Addr
) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
773 obj
->fini
= (Elf_Addr
) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
777 * Don't process DT_DEBUG on MIPS as the dynamic section
778 * is mapped read-only. DT_MIPS_RLD_MAP is used instead.
783 /* XXX - not implemented yet */
785 dbg("Filling in DT_DEBUG entry");
786 ((Elf_Dyn
*)dynp
)->d_un
.d_ptr
= (Elf_Addr
) &r_debug
;
791 if (dynp
->d_un
.d_val
& DF_ORIGIN
) {
792 obj
->origin_path
= xmalloc(PATH_MAX
);
793 if (rtld_dirname(obj
->path
, obj
->origin_path
) == -1)
796 if (dynp
->d_un
.d_val
& DF_SYMBOLIC
)
797 obj
->symbolic
= true;
798 if (dynp
->d_un
.d_val
& DF_TEXTREL
)
800 if (dynp
->d_un
.d_val
& DF_BIND_NOW
)
801 obj
->bind_now
= true;
802 if (dynp
->d_un
.d_val
& DF_STATIC_TLS
)
806 case DT_MIPS_LOCAL_GOTNO
:
807 obj
->local_gotno
= dynp
->d_un
.d_val
;
810 case DT_MIPS_SYMTABNO
:
811 obj
->symtabno
= dynp
->d_un
.d_val
;
815 obj
->gotsym
= dynp
->d_un
.d_val
;
818 case DT_MIPS_RLD_MAP
:
821 dbg("Filling in DT_DEBUG entry");
822 ((Elf_Dyn
*)dynp
)->d_un
.d_ptr
= (Elf_Addr
) &r_debug
;
829 dbg("Ignoring d_tag %ld = %#lx", (long)dynp
->d_tag
,
838 if (plttype
== DT_RELA
) {
839 obj
->pltrela
= (const Elf_Rela
*) obj
->pltrel
;
841 obj
->pltrelasize
= obj
->pltrelsize
;
845 if (dyn_rpath
!= NULL
)
846 obj
->rpath
= obj
->strtab
+ dyn_rpath
->d_un
.d_val
;
848 if (dyn_soname
!= NULL
)
849 object_add_name(obj
, obj
->strtab
+ dyn_soname
->d_un
.d_val
);
853 * Process a shared object's program header. This is used only for the
854 * main program, when the kernel has already loaded the main program
855 * into memory before calling the dynamic linker. It creates and
856 * returns an Obj_Entry structure.
859 digest_phdr(const Elf_Phdr
*phdr
, int phnum
, caddr_t entry
, const char *path
)
862 const Elf_Phdr
*phlimit
= phdr
+ phnum
;
867 for (ph
= phdr
; ph
< phlimit
; ph
++) {
868 switch (ph
->p_type
) {
871 if ((const Elf_Phdr
*)ph
->p_vaddr
!= phdr
) {
872 _rtld_error("%s: invalid PT_PHDR", path
);
875 obj
->phdr
= (const Elf_Phdr
*) ph
->p_vaddr
;
876 obj
->phsize
= ph
->p_memsz
;
880 obj
->interp
= (const char *) ph
->p_vaddr
;
884 if (nsegs
== 0) { /* First load segment */
885 obj
->vaddrbase
= trunc_page(ph
->p_vaddr
);
886 obj
->mapbase
= (caddr_t
) obj
->vaddrbase
;
887 obj
->relocbase
= obj
->mapbase
- obj
->vaddrbase
;
888 obj
->textsize
= round_page(ph
->p_vaddr
+ ph
->p_memsz
) -
890 } else { /* Last load segment */
891 obj
->mapsize
= round_page(ph
->p_vaddr
+ ph
->p_memsz
) -
898 obj
->dynamic
= (const Elf_Dyn
*) ph
->p_vaddr
;
903 obj
->tlssize
= ph
->p_memsz
;
904 obj
->tlsalign
= ph
->p_align
;
905 obj
->tlsinitsize
= ph
->p_filesz
;
906 obj
->tlsinit
= (void*) ph
->p_vaddr
;
911 _rtld_error("%s: too few PT_LOAD segments", path
);
920 dlcheck(void *handle
)
924 for (obj
= obj_list
; obj
!= NULL
; obj
= obj
->next
)
925 if (obj
== (Obj_Entry
*) handle
)
928 if (obj
== NULL
|| obj
->refcount
== 0 || obj
->dl_refcount
== 0) {
929 _rtld_error("Invalid shared object handle %p", handle
);
936 * If the given object is already in the donelist, return true. Otherwise
937 * add the object to the list and return false.
940 donelist_check(DoneList
*dlp
, const Obj_Entry
*obj
)
944 for (i
= 0; i
< dlp
->num_used
; i
++)
945 if (dlp
->objs
[i
] == obj
)
948 * Our donelist allocation should always be sufficient. But if
949 * our threads locking isn't working properly, more shared objects
950 * could have been loaded since we allocated the list. That should
951 * never happen, but we'll handle it properly just in case it does.
953 if (dlp
->num_used
< dlp
->num_alloc
)
954 dlp
->objs
[dlp
->num_used
++] = obj
;
959 * Hash function for symbol table lookup. Don't even think about changing
960 * this. It is specified by the System V ABI.
963 elf_hash(const char *name
)
965 const unsigned char *p
= (const unsigned char *) name
;
971 if ((g
= h
& 0xf0000000) != 0)
979 * Find the library with the given name, and return its full pathname.
980 * The returned string is dynamically allocated. Generates an error
981 * message and returns NULL if the library cannot be found.
983 * If the second argument is non-NULL, then it refers to an already-
984 * loaded shared object, whose library search path will be searched.
986 * The search order is:
988 * rpath in the referencing file
993 find_library(const char *xname
, const Obj_Entry
*refobj
)
998 if (strchr(xname
, '/') != NULL
) { /* Hard coded pathname */
999 if (xname
[0] != '/' && !trust
) {
1000 _rtld_error("Absolute pathname required for shared object \"%s\"",
1004 return xstrdup(xname
);
1007 if (libmap_disable
|| (refobj
== NULL
) ||
1008 (name
= lm_find(refobj
->path
, xname
)) == NULL
)
1009 name
= (char *)xname
;
1011 dbg(" Searching for \"%s\"", name
);
1013 if ((pathname
= search_library_path(name
, ld_library_path
)) != NULL
||
1015 (pathname
= search_library_path(name
, refobj
->rpath
)) != NULL
) ||
1016 (pathname
= search_library_path(name
, gethints())) != NULL
||
1017 (pathname
= search_library_path(name
, STANDARD_LIBRARY_PATH
)) != NULL
)
1020 if(refobj
!= NULL
&& refobj
->path
!= NULL
) {
1021 _rtld_error("Shared object \"%s\" not found, required by \"%s\"",
1022 name
, basename(refobj
->path
));
1024 _rtld_error("Shared object \"%s\" not found", name
);
1030 * Given a symbol number in a referencing object, find the corresponding
1031 * definition of the symbol. Returns a pointer to the symbol, or NULL if
1032 * no definition was found. Returns a pointer to the Obj_Entry of the
1033 * defining object via the reference parameter DEFOBJ_OUT.
1036 find_symdef(unsigned long symnum
, const Obj_Entry
*refobj
,
1037 const Obj_Entry
**defobj_out
, int flags
, SymCache
*cache
)
1041 const Obj_Entry
*defobj
;
1042 const Ver_Entry
*ventry
;
1047 * If we have already found this symbol, get the information from
1050 if (symnum
>= refobj
->nchains
)
1051 return NULL
; /* Bad object */
1052 if (cache
!= NULL
&& cache
[symnum
].sym
!= NULL
) {
1053 *defobj_out
= cache
[symnum
].obj
;
1054 return cache
[symnum
].sym
;
1057 ref
= refobj
->symtab
+ symnum
;
1058 name
= refobj
->strtab
+ ref
->st_name
;
1062 * We don't have to do a full scale lookup if the symbol is local.
1063 * We know it will bind to the instance in this load module; to
1064 * which we already have a pointer (ie ref). By not doing a lookup,
1065 * we not only improve performance, but it also avoids unresolvable
1066 * symbols when local symbols are not in the hash table. This has
1067 * been seen with the ia64 toolchain.
1069 if (ELF_ST_BIND(ref
->st_info
) != STB_LOCAL
) {
1070 if (ELF_ST_TYPE(ref
->st_info
) == STT_SECTION
) {
1071 _rtld_error("%s: Bogus symbol table entry %lu", refobj
->path
,
1074 ventry
= fetch_ventry(refobj
, symnum
);
1075 hash
= elf_hash(name
);
1076 def
= symlook_default(name
, hash
, refobj
, &defobj
, ventry
, flags
);
1083 * If we found no definition and the reference is weak, treat the
1084 * symbol as having the value zero.
1086 if (def
== NULL
&& ELF_ST_BIND(ref
->st_info
) == STB_WEAK
) {
1092 *defobj_out
= defobj
;
1093 /* Record the information in the cache to avoid subsequent lookups. */
1094 if (cache
!= NULL
) {
1095 cache
[symnum
].sym
= def
;
1096 cache
[symnum
].obj
= defobj
;
1099 if (refobj
!= &obj_rtld
)
1100 _rtld_error("%s: Undefined symbol \"%s\"", refobj
->path
, name
);
1106 * Return the search path from the ldconfig hints file, reading it if
1107 * necessary. Returns NULL if there are problems with the hints file,
1108 * or if the search path there is empty.
1115 if (hints
== NULL
) {
1117 struct elfhints_hdr hdr
;
1120 /* Keep from trying again in case the hints file is bad. */
1123 if ((fd
= open(_PATH_ELF_HINTS
, O_RDONLY
)) == -1)
1125 if (read(fd
, &hdr
, sizeof hdr
) != sizeof hdr
||
1126 hdr
.magic
!= ELFHINTS_MAGIC
||
1131 p
= xmalloc(hdr
.dirlistlen
+ 1);
1132 if (lseek(fd
, hdr
.strtab
+ hdr
.dirlist
, SEEK_SET
) == -1 ||
1133 read(fd
, p
, hdr
.dirlistlen
+ 1) != (ssize_t
)hdr
.dirlistlen
+ 1) {
1141 return hints
[0] != '\0' ? hints
: NULL
;
1145 init_dag(Obj_Entry
*root
)
1149 donelist_init(&donelist
);
1150 init_dag1(root
, root
, &donelist
);
1154 init_dag1(Obj_Entry
*root
, Obj_Entry
*obj
, DoneList
*dlp
)
1156 const Needed_Entry
*needed
;
1158 if (donelist_check(dlp
, obj
))
1162 objlist_push_tail(&obj
->dldags
, root
);
1163 objlist_push_tail(&root
->dagmembers
, obj
);
1164 for (needed
= obj
->needed
; needed
!= NULL
; needed
= needed
->next
)
1165 if (needed
->obj
!= NULL
)
1166 init_dag1(root
, needed
->obj
, dlp
);
1170 * Initialize the dynamic linker. The argument is the address at which
1171 * the dynamic linker has been mapped into memory. The primary task of
1172 * this function is to relocate the dynamic linker.
1175 init_rtld(caddr_t mapbase
)
1177 Obj_Entry objtmp
; /* Temporary rtld object */
1180 * Conjure up an Obj_Entry structure for the dynamic linker.
1182 * The "path" member can't be initialized yet because string constatns
1183 * cannot yet be acessed. Below we will set it correctly.
1185 memset(&objtmp
, 0, sizeof(objtmp
));
1188 objtmp
.mapbase
= mapbase
;
1190 objtmp
.relocbase
= mapbase
;
1192 if (RTLD_IS_DYNAMIC()) {
1193 objtmp
.dynamic
= rtld_dynamic(&objtmp
);
1194 digest_dynamic(&objtmp
, 1);
1195 assert(objtmp
.needed
== NULL
);
1196 #if !defined(__mips__)
1197 /* MIPS and SH{3,5} have a bogus DT_TEXTREL. */
1198 assert(!objtmp
.textrel
);
1202 * Temporarily put the dynamic linker entry into the object list, so
1203 * that symbols can be found.
1206 relocate_objects(&objtmp
, true, &objtmp
);
1209 /* Initialize the object list. */
1210 obj_tail
= &obj_list
;
1212 /* Now that non-local variables can be accesses, copy out obj_rtld. */
1213 memcpy(&obj_rtld
, &objtmp
, sizeof(obj_rtld
));
1215 /* Replace the path with a dynamically allocated copy. */
1216 obj_rtld
.path
= xstrdup(PATH_RTLD
);
1218 r_debug
.r_brk
= r_debug_state
;
1219 r_debug
.r_state
= RT_CONSISTENT
;
1223 * Add the init functions from a needed object list (and its recursive
1224 * needed objects) to "list". This is not used directly; it is a helper
1225 * function for initlist_add_objects(). The write lock must be held
1226 * when this function is called.
1229 initlist_add_neededs(Needed_Entry
*needed
, Objlist
*list
)
1231 /* Recursively process the successor needed objects. */
1232 if (needed
->next
!= NULL
)
1233 initlist_add_neededs(needed
->next
, list
);
1235 /* Process the current needed object. */
1236 if (needed
->obj
!= NULL
)
1237 initlist_add_objects(needed
->obj
, &needed
->obj
->next
, list
);
1241 * Scan all of the DAGs rooted in the range of objects from "obj" to
1242 * "tail" and add their init functions to "list". This recurses over
1243 * the DAGs and ensure the proper init ordering such that each object's
1244 * needed libraries are initialized before the object itself. At the
1245 * same time, this function adds the objects to the global finalization
1246 * list "list_fini" in the opposite order. The write lock must be
1247 * held when this function is called.
1250 initlist_add_objects(Obj_Entry
*obj
, Obj_Entry
**tail
, Objlist
*list
)
1254 obj
->init_done
= true;
1256 /* Recursively process the successor objects. */
1257 if (&obj
->next
!= tail
)
1258 initlist_add_objects(obj
->next
, tail
, list
);
1260 /* Recursively process the needed objects. */
1261 if (obj
->needed
!= NULL
)
1262 initlist_add_neededs(obj
->needed
, list
);
1264 /* Add the object to the init list. */
1265 if (obj
->init
!= (Elf_Addr
)NULL
)
1266 objlist_push_tail(list
, obj
);
1268 /* Add the object to the global fini list in the reverse order. */
1269 if (obj
->fini
!= (Elf_Addr
)NULL
)
1270 objlist_push_head(&list_fini
, obj
);
1274 #define FPTR_TARGET(f) ((Elf_Addr) (f))
1278 is_exported(const Elf_Sym
*def
)
1281 const func_ptr_type
*p
;
1283 value
= (Elf_Addr
)(obj_rtld
.relocbase
+ def
->st_value
);
1284 for (p
= exports
; *p
!= NULL
; p
++)
1285 if (FPTR_TARGET(*p
) == value
)
1291 * Given a shared object, traverse its list of needed objects, and load
1292 * each of them. Returns 0 on success. Generates an error message and
1293 * returns -1 on failure.
1296 load_needed_objects(Obj_Entry
*first
)
1300 for (obj
= first
; obj
!= NULL
; obj
= obj
->next
) {
1301 Needed_Entry
*needed
;
1303 for (needed
= obj
->needed
; needed
!= NULL
; needed
= needed
->next
) {
1304 needed
->obj
= load_object(obj
->strtab
+ needed
->name
, obj
);
1305 if (needed
->obj
== NULL
&& !ld_tracing
)
1314 load_preload_objects(void)
1316 char *p
= ld_preload
;
1317 static const char delim
[] = " \t:;";
1322 p
+= strspn(p
, delim
);
1323 while (*p
!= '\0') {
1324 size_t len
= strcspn(p
, delim
);
1329 if (load_object(p
, NULL
) == NULL
)
1330 return -1; /* XXX - cleanup */
1333 p
+= strspn(p
, delim
);
1335 LD_UTRACE(UTRACE_PRELOAD_FINISHED
, NULL
, NULL
, 0, 0, NULL
);
1340 * Load a shared object into memory, if it is not already loaded.
1342 * Returns a pointer to the Obj_Entry for the object. Returns NULL
1346 load_object(const char *name
, const Obj_Entry
*refobj
)
1353 for (obj
= obj_list
->next
; obj
!= NULL
; obj
= obj
->next
)
1354 if (object_match_name(obj
, name
))
1357 path
= find_library(name
, refobj
);
1362 * If we didn't find a match by pathname, open the file and check
1363 * again by device and inode. This avoids false mismatches caused
1364 * by multiple links or ".." in pathnames.
1366 * To avoid a race, we open the file and use fstat() rather than
1369 if ((fd
= open(path
, O_RDONLY
)) == -1) {
1370 _rtld_error("Cannot open \"%s\"", path
);
1374 if (fstat(fd
, &sb
) == -1) {
1375 _rtld_error("Cannot fstat \"%s\"", path
);
1380 for (obj
= obj_list
->next
; obj
!= NULL
; obj
= obj
->next
) {
1381 if (obj
->ino
== sb
.st_ino
&& obj
->dev
== sb
.st_dev
) {
1387 object_add_name(obj
, name
);
1393 /* First use of this object, so we must map it in */
1394 obj
= do_load_object(fd
, name
, path
, &sb
);
1403 do_load_object(int fd
, const char *name
, char *path
, struct stat
*sbp
)
1409 * but first, make sure that environment variables haven't been
1410 * used to circumvent the noexec flag on a filesystem.
1412 if (dangerous_ld_env
) {
1413 if (fstatfs(fd
, &fs
) != 0) {
1414 _rtld_error("Cannot fstatfs \"%s\"", path
);
1417 if (fs
.f_flags
& MNT_NOEXEC
) {
1418 _rtld_error("Cannot execute objects on %s\n", fs
.f_mntonname
);
1422 dbg("loading \"%s\"", path
);
1423 obj
= map_object(fd
, path
, sbp
);
1427 object_add_name(obj
, name
);
1429 digest_dynamic(obj
, 0);
1432 obj_tail
= &obj
->next
;
1435 linkmap_add(obj
); /* for GDB & dlinfo() */
1437 dbg(" %p .. %p: %s", obj
->mapbase
,
1438 obj
->mapbase
+ obj
->mapsize
- 1, obj
->path
);
1440 dbg(" WARNING: %s has impure text", obj
->path
);
1441 LD_UTRACE(UTRACE_LOAD_OBJECT
, obj
, obj
->mapbase
, obj
->mapsize
, 0,
1448 obj_from_addr(const void *addr
)
1452 for (obj
= obj_list
; obj
!= NULL
; obj
= obj
->next
) {
1453 if (addr
< (void *) obj
->mapbase
)
1455 if (addr
< (void *) (obj
->mapbase
+ obj
->mapsize
))
1462 * Call the finalization functions for each of the objects in "list"
1463 * which are unreferenced. All of the objects are expected to have
1464 * non-NULL fini functions.
1467 objlist_call_fini(Objlist
*list
, int *lockstate
)
1473 * Preserve the current error message since a fini function might
1474 * call into the dynamic linker and overwrite it.
1476 saved_msg
= errmsg_save();
1477 wlock_release(rtld_bind_lock
, *lockstate
);
1478 STAILQ_FOREACH(elm
, list
, link
) {
1479 if (elm
->obj
->refcount
== 0) {
1480 dbg("calling fini function for %s at %p", elm
->obj
->path
,
1481 (void *)elm
->obj
->fini
);
1482 LD_UTRACE(UTRACE_FINI_CALL
, elm
->obj
, (void *)elm
->obj
->fini
, 0, 0,
1484 call_initfini_pointer(elm
->obj
, elm
->obj
->fini
);
1487 *lockstate
= wlock_acquire(rtld_bind_lock
);
1488 errmsg_restore(saved_msg
);
1492 * Call the initialization functions for each of the objects in
1493 * "list". All of the objects are expected to have non-NULL init
1497 objlist_call_init(Objlist
*list
, int *lockstate
)
1503 * Preserve the current error message since an init function might
1504 * call into the dynamic linker and overwrite it.
1506 saved_msg
= errmsg_save();
1507 wlock_release(rtld_bind_lock
, *lockstate
);
1508 STAILQ_FOREACH(elm
, list
, link
) {
1509 dbg("calling init function for %s at %p", elm
->obj
->path
,
1510 (void *)elm
->obj
->init
);
1511 LD_UTRACE(UTRACE_INIT_CALL
, elm
->obj
, (void *)elm
->obj
->init
, 0, 0,
1513 call_initfini_pointer(elm
->obj
, elm
->obj
->init
);
1515 *lockstate
= wlock_acquire(rtld_bind_lock
);
1516 errmsg_restore(saved_msg
);
1520 objlist_clear(Objlist
*list
)
1524 while (!STAILQ_EMPTY(list
)) {
1525 elm
= STAILQ_FIRST(list
);
1526 STAILQ_REMOVE_HEAD(list
, link
);
1531 static Objlist_Entry
*
1532 objlist_find(Objlist
*list
, const Obj_Entry
*obj
)
1536 STAILQ_FOREACH(elm
, list
, link
)
1537 if (elm
->obj
== obj
)
1543 objlist_init(Objlist
*list
)
1549 objlist_push_head(Objlist
*list
, Obj_Entry
*obj
)
1553 elm
= NEW(Objlist_Entry
);
1555 STAILQ_INSERT_HEAD(list
, elm
, link
);
1559 objlist_push_tail(Objlist
*list
, Obj_Entry
*obj
)
1563 elm
= NEW(Objlist_Entry
);
1565 STAILQ_INSERT_TAIL(list
, elm
, link
);
1569 objlist_remove(Objlist
*list
, Obj_Entry
*obj
)
1573 if ((elm
= objlist_find(list
, obj
)) != NULL
) {
1574 STAILQ_REMOVE(list
, elm
, Struct_Objlist_Entry
, link
);
1580 * Remove all of the unreferenced objects from "list".
1583 objlist_remove_unref(Objlist
*list
)
1588 STAILQ_INIT(&newlist
);
1589 while (!STAILQ_EMPTY(list
)) {
1590 elm
= STAILQ_FIRST(list
);
1591 STAILQ_REMOVE_HEAD(list
, link
);
1592 if (elm
->obj
->refcount
== 0)
1595 STAILQ_INSERT_TAIL(&newlist
, elm
, link
);
1601 * Relocate newly-loaded shared objects. The argument is a pointer to
1602 * the Obj_Entry for the first such object. All objects from the first
1603 * to the end of the list of objects are relocated. Returns 0 on success,
1607 relocate_objects(Obj_Entry
*first
, bool bind_now
, Obj_Entry
*rtldobj
)
1611 for (obj
= first
; obj
!= NULL
; obj
= obj
->next
) {
1613 dbg("relocating \"%s\"", obj
->path
);
1614 if (obj
->nbuckets
== 0 || obj
->nchains
== 0 || obj
->buckets
== NULL
||
1615 obj
->symtab
== NULL
|| obj
->strtab
== NULL
) {
1616 _rtld_error("%s: Shared object has no run-time symbol table",
1622 /* There are relocations to the write-protected text segment. */
1623 if (mprotect(obj
->mapbase
, obj
->textsize
,
1624 PROT_READ
|PROT_WRITE
|PROT_EXEC
) == -1) {
1625 _rtld_error("%s: Cannot write-enable text segment: %s",
1626 obj
->path
, strerror(errno
));
1631 /* Process the non-PLT relocations. */
1632 if (reloc_non_plt(obj
, rtldobj
))
1635 if (obj
->textrel
) { /* Re-protected the text segment. */
1636 if (mprotect(obj
->mapbase
, obj
->textsize
,
1637 PROT_READ
|PROT_EXEC
) == -1) {
1638 _rtld_error("%s: Cannot write-protect text segment: %s",
1639 obj
->path
, strerror(errno
));
1644 /* Process the PLT relocations. */
1645 if (reloc_plt(obj
) == -1)
1647 /* Relocate the jump slots if we are doing immediate binding. */
1648 if (obj
->bind_now
|| bind_now
)
1649 if (reloc_jmpslots(obj
) == -1)
1654 * Set up the magic number and version in the Obj_Entry. These
1655 * were checked in the crt1.o from the original ElfKit, so we
1656 * set them for backward compatibility.
1658 obj
->magic
= RTLD_MAGIC
;
1659 obj
->version
= RTLD_VERSION
;
1661 /* Set the special PLT or GOT entries. */
1669 * Cleanup procedure. It will be called (by the atexit mechanism) just
1670 * before the process exits.
1678 lockstate
= wlock_acquire(rtld_bind_lock
);
1680 /* Clear all the reference counts so the fini functions will be called. */
1681 for (obj
= obj_list
; obj
!= NULL
; obj
= obj
->next
)
1683 objlist_call_fini(&list_fini
, &lockstate
);
1684 /* No need to remove the items from the list, since we are exiting. */
1685 if (!libmap_disable
)
1687 wlock_release(rtld_bind_lock
, lockstate
);
1691 path_enumerate(const char *path
, path_enum_proc callback
, void *arg
)
1699 path
+= strspn(path
, ":;");
1700 while (*path
!= '\0') {
1704 len
= strcspn(path
, ":;");
1706 trans
= lm_findn(NULL
, path
, len
);
1708 res
= callback(trans
, strlen(trans
), arg
);
1711 res
= callback(path
, len
, arg
);
1717 path
+= strspn(path
, ":;");
1723 struct try_library_args
{
1731 try_library_path(const char *dir
, size_t dirlen
, void *param
)
1733 struct try_library_args
*arg
;
1736 if (*dir
== '/' || trust
) {
1739 if (dirlen
+ 1 + arg
->namelen
+ 1 > arg
->buflen
)
1742 pathname
= arg
->buffer
;
1743 strncpy(pathname
, dir
, dirlen
);
1744 pathname
[dirlen
] = '/';
1745 strcpy(pathname
+ dirlen
+ 1, arg
->name
);
1747 dbg(" Trying \"%s\"", pathname
);
1748 if (access(pathname
, F_OK
) == 0) { /* We found it */
1749 pathname
= xmalloc(dirlen
+ 1 + arg
->namelen
+ 1);
1750 strcpy(pathname
, arg
->buffer
);
1758 search_library_path(const char *name
, const char *path
)
1761 struct try_library_args arg
;
1767 arg
.namelen
= strlen(name
);
1768 arg
.buffer
= xmalloc(PATH_MAX
);
1769 arg
.buflen
= PATH_MAX
;
1771 p
= path_enumerate(path
, try_library_path
, &arg
);
1779 dlclose(void *handle
)
1784 lockstate
= wlock_acquire(rtld_bind_lock
);
1785 root
= dlcheck(handle
);
1787 wlock_release(rtld_bind_lock
, lockstate
);
1790 LD_UTRACE(UTRACE_DLCLOSE_START
, handle
, NULL
, 0, root
->dl_refcount
,
1793 /* Unreference the object and its dependencies. */
1794 root
->dl_refcount
--;
1798 if (root
->refcount
== 0) {
1800 * The object is no longer referenced, so we must unload it.
1801 * First, call the fini functions.
1803 objlist_call_fini(&list_fini
, &lockstate
);
1804 objlist_remove_unref(&list_fini
);
1806 /* Finish cleaning up the newly-unreferenced objects. */
1807 GDB_STATE(RT_DELETE
,&root
->linkmap
);
1808 unload_object(root
);
1809 GDB_STATE(RT_CONSISTENT
,NULL
);
1811 LD_UTRACE(UTRACE_DLCLOSE_STOP
, handle
, NULL
, 0, 0, NULL
);
1812 wlock_release(rtld_bind_lock
, lockstate
);
1819 char *msg
= error_message
;
1820 error_message
= NULL
;
1825 * This function is deprecated and has no effect.
1828 dllockinit(void *context
,
1829 void *(*lock_create
)(void *context
),
1830 void (*rlock_acquire
)(void *lock
),
1831 void (*wlock_acquire
)(void *lock
),
1832 void (*lock_release
)(void *lock
),
1833 void (*lock_destroy
)(void *lock
),
1834 void (*context_destroy
)(void *context
))
1836 static void *cur_context
;
1837 static void (*cur_context_destroy
)(void *);
1839 /* Just destroy the context from the previous call, if necessary. */
1840 if (cur_context_destroy
!= NULL
)
1841 cur_context_destroy(cur_context
);
1842 cur_context
= context
;
1843 cur_context_destroy
= context_destroy
;
1847 dlopen(const char *name
, int mode
)
1849 Obj_Entry
**old_obj_tail
;
1852 int result
, lockstate
;
1854 LD_UTRACE(UTRACE_DLOPEN_START
, NULL
, NULL
, 0, mode
, name
);
1855 ld_tracing
= (mode
& RTLD_TRACE
) == 0 ? NULL
: "1";
1856 if (ld_tracing
!= NULL
)
1857 environ
= (char **)*get_program_var_addr("environ");
1859 objlist_init(&initlist
);
1861 lockstate
= wlock_acquire(rtld_bind_lock
);
1862 GDB_STATE(RT_ADD
,NULL
);
1864 old_obj_tail
= obj_tail
;
1870 obj
= load_object(name
, obj_main
);
1875 if (mode
& RTLD_GLOBAL
&& objlist_find(&list_global
, obj
) == NULL
)
1876 objlist_push_tail(&list_global
, obj
);
1877 mode
&= RTLD_MODEMASK
;
1878 if (*old_obj_tail
!= NULL
) { /* We loaded something new. */
1879 assert(*old_obj_tail
== obj
);
1880 result
= load_needed_objects(obj
);
1883 result
= rtld_verify_versions(&obj
->dagmembers
);
1884 if (result
!= -1 && ld_tracing
)
1887 (relocate_objects(obj
, mode
== RTLD_NOW
, &obj_rtld
)) == -1) {
1890 if (obj
->refcount
== 0)
1894 /* Make list of init functions to call. */
1895 initlist_add_objects(obj
, &obj
->next
, &initlist
);
1899 /* Bump the reference counts for objects on this DAG. */
1907 LD_UTRACE(UTRACE_DLOPEN_STOP
, obj
, NULL
, 0, obj
? obj
->dl_refcount
: 0,
1909 GDB_STATE(RT_CONSISTENT
,obj
? &obj
->linkmap
: NULL
);
1911 /* Call the init functions. */
1912 objlist_call_init(&initlist
, &lockstate
);
1913 objlist_clear(&initlist
);
1914 wlock_release(rtld_bind_lock
, lockstate
);
1917 trace_loaded_objects(obj
);
1918 wlock_release(rtld_bind_lock
, lockstate
);
1923 do_dlsym(void *handle
, const char *name
, void *retaddr
, const Ver_Entry
*ve
,
1927 const Obj_Entry
*obj
, *defobj
;
1932 hash
= elf_hash(name
);
1935 flags
|= SYMLOOK_IN_PLT
;
1937 lockstate
= rlock_acquire(rtld_bind_lock
);
1938 if (handle
== NULL
|| handle
== RTLD_NEXT
||
1939 handle
== RTLD_DEFAULT
|| handle
== RTLD_SELF
) {
1941 if ((obj
= obj_from_addr(retaddr
)) == NULL
) {
1942 _rtld_error("Cannot determine caller's shared object");
1943 rlock_release(rtld_bind_lock
, lockstate
);
1946 if (handle
== NULL
) { /* Just the caller's shared object. */
1947 def
= symlook_obj(name
, hash
, obj
, ve
, flags
);
1949 } else if (handle
== RTLD_NEXT
|| /* Objects after caller's */
1950 handle
== RTLD_SELF
) { /* ... caller included */
1951 if (handle
== RTLD_NEXT
)
1953 for (; obj
!= NULL
; obj
= obj
->next
) {
1954 if ((def
= symlook_obj(name
, hash
, obj
, ve
, flags
)) != NULL
) {
1960 assert(handle
== RTLD_DEFAULT
);
1961 def
= symlook_default(name
, hash
, obj
, &defobj
, ve
, flags
);
1964 if ((obj
= dlcheck(handle
)) == NULL
) {
1965 rlock_release(rtld_bind_lock
, lockstate
);
1969 donelist_init(&donelist
);
1970 if (obj
->mainprog
) {
1971 /* Search main program and all libraries loaded by it. */
1972 def
= symlook_list(name
, hash
, &list_main
, &defobj
, ve
, flags
,
1977 /* Search the whole DAG rooted at the given object. */
1979 fake
.obj
= (Obj_Entry
*)obj
;
1981 def
= symlook_needed(name
, hash
, &fake
, &defobj
, ve
, flags
,
1987 rlock_release(rtld_bind_lock
, lockstate
);
1990 * The value required by the caller is derived from the value
1991 * of the symbol. For the ia64 architecture, we need to
1992 * construct a function descriptor which the caller can use to
1993 * call the function with the right 'gp' value. For other
1994 * architectures and for non-functions, the value is simply
1995 * the relocated value of the symbol.
1997 if (ELF_ST_TYPE(def
->st_info
) == STT_FUNC
)
1998 return make_function_pointer(def
, defobj
);
2000 return defobj
->relocbase
+ def
->st_value
;
2003 _rtld_error("Undefined symbol \"%s\"", name
);
2004 rlock_release(rtld_bind_lock
, lockstate
);
2009 dlsym(void *handle
, const char *name
)
2011 return do_dlsym(handle
, name
, __builtin_return_address(0), NULL
,
2016 dlvsym(void *handle
, const char *name
, const char *version
)
2020 ventry
.name
= version
;
2022 ventry
.hash
= elf_hash(version
);
2024 return do_dlsym(handle
, name
, __builtin_return_address(0), &ventry
,
2029 dladdr(const void *addr
, Dl_info
*info
)
2031 const Obj_Entry
*obj
;
2034 unsigned long symoffset
;
2037 lockstate
= rlock_acquire(rtld_bind_lock
);
2038 obj
= obj_from_addr(addr
);
2040 _rtld_error("No shared object contains address");
2041 rlock_release(rtld_bind_lock
, lockstate
);
2044 info
->dli_fname
= obj
->path
;
2045 info
->dli_fbase
= obj
->mapbase
;
2046 info
->dli_saddr
= (void *)0;
2047 info
->dli_sname
= NULL
;
2050 * Walk the symbol list looking for the symbol whose address is
2051 * closest to the address sent in.
2053 for (symoffset
= 0; symoffset
< obj
->nchains
; symoffset
++) {
2054 def
= obj
->symtab
+ symoffset
;
2057 * For skip the symbol if st_shndx is either SHN_UNDEF or
2060 if (def
->st_shndx
== SHN_UNDEF
|| def
->st_shndx
== SHN_COMMON
)
2064 * If the symbol is greater than the specified address, or if it
2065 * is further away from addr than the current nearest symbol,
2068 symbol_addr
= obj
->relocbase
+ def
->st_value
;
2069 if (symbol_addr
> addr
|| symbol_addr
< info
->dli_saddr
)
2072 /* Update our idea of the nearest symbol. */
2073 info
->dli_sname
= obj
->strtab
+ def
->st_name
;
2074 info
->dli_saddr
= symbol_addr
;
2077 if (info
->dli_saddr
== addr
)
2080 rlock_release(rtld_bind_lock
, lockstate
);
2085 dlinfo(void *handle
, int request
, void *p
)
2087 const Obj_Entry
*obj
;
2088 int error
, lockstate
;
2090 lockstate
= rlock_acquire(rtld_bind_lock
);
2092 if (handle
== NULL
|| handle
== RTLD_SELF
) {
2095 retaddr
= __builtin_return_address(0); /* __GNUC__ only */
2096 if ((obj
= obj_from_addr(retaddr
)) == NULL
)
2097 _rtld_error("Cannot determine caller's shared object");
2099 obj
= dlcheck(handle
);
2102 rlock_release(rtld_bind_lock
, lockstate
);
2108 case RTLD_DI_LINKMAP
:
2109 *((struct link_map
const **)p
) = &obj
->linkmap
;
2111 case RTLD_DI_ORIGIN
:
2112 error
= rtld_dirname(obj
->path
, p
);
2115 case RTLD_DI_SERINFOSIZE
:
2116 case RTLD_DI_SERINFO
:
2117 error
= do_search_info(obj
, request
, (struct dl_serinfo
*)p
);
2121 _rtld_error("Invalid request %d passed to dlinfo()", request
);
2125 rlock_release(rtld_bind_lock
, lockstate
);
2131 dl_iterate_phdr(__dl_iterate_hdr_callback callback
, void *param
)
2133 struct dl_phdr_info phdr_info
;
2134 const Obj_Entry
*obj
;
2135 int error
, bind_lockstate
, phdr_lockstate
;
2137 phdr_lockstate
= wlock_acquire(rtld_phdr_lock
);
2138 bind_lockstate
= rlock_acquire(rtld_bind_lock
);
2142 for (obj
= obj_list
; obj
!= NULL
; obj
= obj
->next
) {
2143 phdr_info
.dlpi_addr
= (Elf_Addr
)obj
->relocbase
;
2144 phdr_info
.dlpi_name
= STAILQ_FIRST(&obj
->names
) ?
2145 STAILQ_FIRST(&obj
->names
)->name
: obj
->path
;
2146 phdr_info
.dlpi_phdr
= obj
->phdr
;
2147 phdr_info
.dlpi_phnum
= obj
->phsize
/ sizeof(obj
->phdr
[0]);
2148 phdr_info
.dlpi_tls_modid
= obj
->tlsindex
;
2149 phdr_info
.dlpi_tls_data
= obj
->tlsinit
;
2150 phdr_info
.dlpi_adds
= obj_loads
;
2151 phdr_info
.dlpi_subs
= obj_loads
- obj_count
;
2153 if ((error
= callback(&phdr_info
, sizeof phdr_info
, param
)) != 0)
2157 rlock_release(rtld_bind_lock
, bind_lockstate
);
2158 wlock_release(rtld_phdr_lock
, phdr_lockstate
);
2163 struct fill_search_info_args
{
2166 Dl_serinfo
*serinfo
;
2167 Dl_serpath
*serpath
;
2172 fill_search_info(const char *dir
, size_t dirlen
, void *param
)
2174 struct fill_search_info_args
*arg
;
2178 if (arg
->request
== RTLD_DI_SERINFOSIZE
) {
2179 arg
->serinfo
->dls_cnt
++;
2180 arg
->serinfo
->dls_size
+= sizeof(Dl_serpath
) + dirlen
+ 1;
2182 struct dl_serpath
*s_entry
;
2184 s_entry
= arg
->serpath
;
2185 s_entry
->dls_name
= arg
->strspace
;
2186 s_entry
->dls_flags
= arg
->flags
;
2188 strncpy(arg
->strspace
, dir
, dirlen
);
2189 arg
->strspace
[dirlen
] = '\0';
2191 arg
->strspace
+= dirlen
+ 1;
2199 do_search_info(const Obj_Entry
*obj
, int request
, struct dl_serinfo
*info
)
2201 struct dl_serinfo _info
;
2202 struct fill_search_info_args args
;
2204 args
.request
= RTLD_DI_SERINFOSIZE
;
2205 args
.serinfo
= &_info
;
2207 _info
.dls_size
= __offsetof(struct dl_serinfo
, dls_serpath
);
2210 path_enumerate(ld_library_path
, fill_search_info
, &args
);
2211 path_enumerate(obj
->rpath
, fill_search_info
, &args
);
2212 path_enumerate(gethints(), fill_search_info
, &args
);
2213 path_enumerate(STANDARD_LIBRARY_PATH
, fill_search_info
, &args
);
2216 if (request
== RTLD_DI_SERINFOSIZE
) {
2217 info
->dls_size
= _info
.dls_size
;
2218 info
->dls_cnt
= _info
.dls_cnt
;
2222 if (info
->dls_cnt
!= _info
.dls_cnt
|| info
->dls_size
!= _info
.dls_size
) {
2223 _rtld_error("Uninitialized Dl_serinfo struct passed to dlinfo()");
2227 args
.request
= RTLD_DI_SERINFO
;
2228 args
.serinfo
= info
;
2229 args
.serpath
= &info
->dls_serpath
[0];
2230 args
.strspace
= (char *)&info
->dls_serpath
[_info
.dls_cnt
];
2232 args
.flags
= LA_SER_LIBPATH
;
2233 if (path_enumerate(ld_library_path
, fill_search_info
, &args
) != NULL
)
2236 args
.flags
= LA_SER_RUNPATH
;
2237 if (path_enumerate(obj
->rpath
, fill_search_info
, &args
) != NULL
)
2240 args
.flags
= LA_SER_CONFIG
;
2241 if (path_enumerate(gethints(), fill_search_info
, &args
) != NULL
)
2244 args
.flags
= LA_SER_DEFAULT
;
2245 if (path_enumerate(STANDARD_LIBRARY_PATH
, fill_search_info
, &args
) != NULL
)
2251 rtld_dirname(const char *path
, char *bname
)
2255 /* Empty or NULL string gets treated as "." */
2256 if (path
== NULL
|| *path
== '\0') {
2262 /* Strip trailing slashes */
2263 endp
= path
+ strlen(path
) - 1;
2264 while (endp
> path
&& *endp
== '/')
2267 /* Find the start of the dir */
2268 while (endp
> path
&& *endp
!= '/')
2271 /* Either the dir is "/" or there are no slashes */
2273 bname
[0] = *endp
== '/' ? '/' : '.';
2279 } while (endp
> path
&& *endp
== '/');
2282 if (endp
- path
+ 2 > PATH_MAX
)
2284 _rtld_error("Filename is too long: %s", path
);
2288 strncpy(bname
, path
, endp
- path
+ 1);
2289 bname
[endp
- path
+ 1] = '\0';
2294 linkmap_add(Obj_Entry
*obj
)
2296 struct link_map
*l
= &obj
->linkmap
;
2297 struct link_map
*prev
;
2299 obj
->linkmap
.l_name
= obj
->path
;
2300 obj
->linkmap
.l_addr
= obj
->mapbase
;
2301 obj
->linkmap
.l_ld
= obj
->dynamic
;
2303 /* GDB needs load offset on MIPS to use the symbols */
2304 obj
->linkmap
.l_offs
= obj
->relocbase
;
2307 if (r_debug
.r_map
== NULL
) {
2313 * Scan to the end of the list, but not past the entry for the
2314 * dynamic linker, which we want to keep at the very end.
2316 for (prev
= r_debug
.r_map
;
2317 prev
->l_next
!= NULL
&& prev
->l_next
!= &obj_rtld
.linkmap
;
2318 prev
= prev
->l_next
)
2321 /* Link in the new entry. */
2323 l
->l_next
= prev
->l_next
;
2324 if (l
->l_next
!= NULL
)
2325 l
->l_next
->l_prev
= l
;
2330 linkmap_delete(Obj_Entry
*obj
)
2332 struct link_map
*l
= &obj
->linkmap
;
2334 if (l
->l_prev
== NULL
) {
2335 if ((r_debug
.r_map
= l
->l_next
) != NULL
)
2336 l
->l_next
->l_prev
= NULL
;
2340 if ((l
->l_prev
->l_next
= l
->l_next
) != NULL
)
2341 l
->l_next
->l_prev
= l
->l_prev
;
2345 * Function for the debugger to set a breakpoint on to gain control.
2347 * The two parameters allow the debugger to easily find and determine
2348 * what the runtime loader is doing and to whom it is doing it.
2350 * When the loadhook trap is hit (r_debug_state, set at program
2351 * initialization), the arguments can be found on the stack:
2353 * +8 struct link_map *m
2354 * +4 struct r_debug *rd
2358 r_debug_state(struct r_debug
* rd
, struct link_map
*m
)
2363 * Get address of the pointer variable in the main program.
2365 static const void **
2366 get_program_var_addr(const char *name
)
2368 const Obj_Entry
*obj
;
2371 hash
= elf_hash(name
);
2372 for (obj
= obj_main
; obj
!= NULL
; obj
= obj
->next
) {
2375 if ((def
= symlook_obj(name
, hash
, obj
, NULL
, 0)) != NULL
) {
2378 addr
= (const void **)(obj
->relocbase
+ def
->st_value
);
2386 * Set a pointer variable in the main program to the given value. This
2387 * is used to set key variables such as "environ" before any of the
2388 * init functions are called.
2391 set_program_var(const char *name
, const void *value
)
2395 if ((addr
= get_program_var_addr(name
)) != NULL
) {
2396 dbg("\"%s\": *%p <-- %p", name
, addr
, value
);
2402 * Given a symbol name in a referencing object, find the corresponding
2403 * definition of the symbol. Returns a pointer to the symbol, or NULL if
2404 * no definition was found. Returns a pointer to the Obj_Entry of the
2405 * defining object via the reference parameter DEFOBJ_OUT.
2407 static const Elf_Sym
*
2408 symlook_default(const char *name
, unsigned long hash
, const Obj_Entry
*refobj
,
2409 const Obj_Entry
**defobj_out
, const Ver_Entry
*ventry
, int flags
)
2413 const Elf_Sym
*symp
;
2414 const Obj_Entry
*obj
;
2415 const Obj_Entry
*defobj
;
2416 const Objlist_Entry
*elm
;
2419 donelist_init(&donelist
);
2421 /* Look first in the referencing object if linked symbolically. */
2422 if (refobj
->symbolic
&& !donelist_check(&donelist
, refobj
)) {
2423 symp
= symlook_obj(name
, hash
, refobj
, ventry
, flags
);
2430 /* Search all objects loaded at program start up. */
2431 if (def
== NULL
|| ELF_ST_BIND(def
->st_info
) == STB_WEAK
) {
2432 symp
= symlook_list(name
, hash
, &list_main
, &obj
, ventry
, flags
,
2435 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
2441 /* Search all DAGs whose roots are RTLD_GLOBAL objects. */
2442 STAILQ_FOREACH(elm
, &list_global
, link
) {
2443 if (def
!= NULL
&& ELF_ST_BIND(def
->st_info
) != STB_WEAK
)
2445 symp
= symlook_list(name
, hash
, &elm
->obj
->dagmembers
, &obj
, ventry
,
2448 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
2454 /* Search all dlopened DAGs containing the referencing object. */
2455 STAILQ_FOREACH(elm
, &refobj
->dldags
, link
) {
2456 if (def
!= NULL
&& ELF_ST_BIND(def
->st_info
) != STB_WEAK
)
2458 symp
= symlook_list(name
, hash
, &elm
->obj
->dagmembers
, &obj
, ventry
,
2461 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
2468 * Search the dynamic linker itself, and possibly resolve the
2469 * symbol from there. This is how the application links to
2470 * dynamic linker services such as dlopen. Only the values listed
2471 * in the "exports" array can be resolved from the dynamic linker.
2473 if (def
== NULL
|| ELF_ST_BIND(def
->st_info
) == STB_WEAK
) {
2474 symp
= symlook_obj(name
, hash
, &obj_rtld
, ventry
, flags
);
2475 if (symp
!= NULL
&& is_exported(symp
)) {
2482 *defobj_out
= defobj
;
2486 static const Elf_Sym
*
2487 symlook_list(const char *name
, unsigned long hash
, const Objlist
*objlist
,
2488 const Obj_Entry
**defobj_out
, const Ver_Entry
*ventry
, int flags
,
2491 const Elf_Sym
*symp
;
2493 const Obj_Entry
*defobj
;
2494 const Objlist_Entry
*elm
;
2498 STAILQ_FOREACH(elm
, objlist
, link
) {
2499 if (donelist_check(dlp
, elm
->obj
))
2501 if ((symp
= symlook_obj(name
, hash
, elm
->obj
, ventry
, flags
)) != NULL
) {
2502 if (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
) {
2505 if (ELF_ST_BIND(def
->st_info
) != STB_WEAK
)
2511 *defobj_out
= defobj
;
2516 * Search the symbol table of a shared object and all objects needed
2517 * by it for a symbol of the given name. Search order is
2518 * breadth-first. Returns a pointer to the symbol, or NULL if no
2519 * definition was found.
2521 static const Elf_Sym
*
2522 symlook_needed(const char *name
, unsigned long hash
, const Needed_Entry
*needed
,
2523 const Obj_Entry
**defobj_out
, const Ver_Entry
*ventry
, int flags
,
2526 const Elf_Sym
*def
, *def_w
;
2527 const Needed_Entry
*n
;
2528 const Obj_Entry
*obj
, *defobj
, *defobj1
;
2532 for (n
= needed
; n
!= NULL
; n
= n
->next
) {
2533 if ((obj
= n
->obj
) == NULL
||
2534 donelist_check(dlp
, obj
) ||
2535 (def
= symlook_obj(name
, hash
, obj
, ventry
, flags
)) == NULL
)
2538 if (ELF_ST_BIND(def
->st_info
) != STB_WEAK
) {
2539 *defobj_out
= defobj
;
2544 * There we come when either symbol definition is not found in
2545 * directly needed objects, or found symbol is weak.
2547 for (n
= needed
; n
!= NULL
; n
= n
->next
) {
2548 if ((obj
= n
->obj
) == NULL
)
2550 def_w
= symlook_needed(name
, hash
, obj
->needed
, &defobj1
,
2551 ventry
, flags
, dlp
);
2554 if (def
== NULL
|| ELF_ST_BIND(def_w
->st_info
) != STB_WEAK
) {
2558 if (ELF_ST_BIND(def_w
->st_info
) != STB_WEAK
)
2562 *defobj_out
= defobj
;
2567 * Search the symbol table of a single shared object for a symbol of
2568 * the given name and version, if requested. Returns a pointer to the
2569 * symbol, or NULL if no definition was found.
2571 * The symbol's hash value is passed in for efficiency reasons; that
2572 * eliminates many recomputations of the hash value.
2575 symlook_obj(const char *name
, unsigned long hash
, const Obj_Entry
*obj
,
2576 const Ver_Entry
*ventry
, int flags
)
2578 unsigned long symnum
;
2579 const Elf_Sym
*vsymp
;
2583 if (obj
->buckets
== NULL
)
2588 symnum
= obj
->buckets
[hash
% obj
->nbuckets
];
2590 for (; symnum
!= STN_UNDEF
; symnum
= obj
->chains
[symnum
]) {
2591 const Elf_Sym
*symp
;
2594 if (symnum
>= obj
->nchains
)
2595 return NULL
; /* Bad object */
2597 symp
= obj
->symtab
+ symnum
;
2598 strp
= obj
->strtab
+ symp
->st_name
;
2600 switch (ELF_ST_TYPE(symp
->st_info
)) {
2604 if (symp
->st_value
== 0)
2608 if (symp
->st_shndx
!= SHN_UNDEF
)
2611 else if (((flags
& SYMLOOK_IN_PLT
) == 0) &&
2612 (ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
))
2619 if (name
[0] != strp
[0] || strcmp(name
, strp
) != 0)
2622 if (ventry
== NULL
) {
2623 if (obj
->versyms
!= NULL
) {
2624 verndx
= VER_NDX(obj
->versyms
[symnum
]);
2625 if (verndx
> obj
->vernum
) {
2626 _rtld_error("%s: symbol %s references wrong version %d",
2627 obj
->path
, obj
->strtab
+ symnum
, verndx
);
2631 * If we are not called from dlsym (i.e. this is a normal
2632 * relocation from unversioned binary, accept the symbol
2633 * immediately if it happens to have first version after
2634 * this shared object became versioned. Otherwise, if
2635 * symbol is versioned and not hidden, remember it. If it
2636 * is the only symbol with this name exported by the
2637 * shared object, it will be returned as a match at the
2638 * end of the function. If symbol is global (verndx < 2)
2639 * accept it unconditionally.
2641 if ((flags
& SYMLOOK_DLSYM
) == 0 && verndx
== VER_NDX_GIVEN
)
2643 else if (verndx
>= VER_NDX_GIVEN
) {
2644 if ((obj
->versyms
[symnum
] & VER_NDX_HIDDEN
) == 0) {
2654 if (obj
->versyms
== NULL
) {
2655 if (object_match_name(obj
, ventry
->name
)) {
2656 _rtld_error("%s: object %s should provide version %s for "
2657 "symbol %s", obj_rtld
.path
, obj
->path
, ventry
->name
,
2658 obj
->strtab
+ symnum
);
2662 verndx
= VER_NDX(obj
->versyms
[symnum
]);
2663 if (verndx
> obj
->vernum
) {
2664 _rtld_error("%s: symbol %s references wrong version %d",
2665 obj
->path
, obj
->strtab
+ symnum
, verndx
);
2668 if (obj
->vertab
[verndx
].hash
!= ventry
->hash
||
2669 strcmp(obj
->vertab
[verndx
].name
, ventry
->name
)) {
2671 * Version does not match. Look if this is a global symbol
2672 * and if it is not hidden. If global symbol (verndx < 2)
2673 * is available, use it. Do not return symbol if we are
2674 * called by dlvsym, because dlvsym looks for a specific
2675 * version and default one is not what dlvsym wants.
2677 if ((flags
& SYMLOOK_DLSYM
) ||
2678 (obj
->versyms
[symnum
] & VER_NDX_HIDDEN
) ||
2679 (verndx
>= VER_NDX_GIVEN
))
2686 return (vcount
== 1) ? vsymp
: NULL
;
2690 trace_loaded_objects(Obj_Entry
*obj
)
2692 char *fmt1
, *fmt2
, *fmt
, *main_local
, *list_containers
;
2695 if ((main_local
= getenv(LD_
"TRACE_LOADED_OBJECTS_PROGNAME")) == NULL
)
2698 if ((fmt1
= getenv(LD_
"TRACE_LOADED_OBJECTS_FMT1")) == NULL
)
2699 fmt1
= "\t%o => %p (%x)\n";
2701 if ((fmt2
= getenv(LD_
"TRACE_LOADED_OBJECTS_FMT2")) == NULL
)
2702 fmt2
= "\t%o (%x)\n";
2704 list_containers
= getenv(LD_
"TRACE_LOADED_OBJECTS_ALL");
2706 for (; obj
; obj
= obj
->next
) {
2707 Needed_Entry
*needed
;
2711 if (list_containers
&& obj
->needed
!= NULL
)
2712 printf("%s:\n", obj
->path
);
2713 for (needed
= obj
->needed
; needed
; needed
= needed
->next
) {
2714 if (needed
->obj
!= NULL
) {
2715 if (needed
->obj
->traced
&& !list_containers
)
2717 needed
->obj
->traced
= true;
2718 path
= needed
->obj
->path
;
2722 name
= (char *)obj
->strtab
+ needed
->name
;
2723 is_lib
= strncmp(name
, "lib", 3) == 0; /* XXX - bogus */
2725 fmt
= is_lib
? fmt1
: fmt2
;
2726 while ((c
= *fmt
++) != '\0') {
2752 printf("%s", main_local
);
2755 printf("%s", obj_main
->path
);
2762 printf("%d", sodp
->sod_major
);
2765 printf("%d", sodp
->sod_minor
);
2772 printf("%p", needed
->obj
? needed
->obj
->mapbase
: 0);
2784 * Unload a dlopened object and its dependencies from memory and from
2785 * our data structures. It is assumed that the DAG rooted in the
2786 * object has already been unreferenced, and that the object has a
2787 * reference count of 0.
2790 unload_object(Obj_Entry
*root
)
2795 assert(root
->refcount
== 0);
2798 * Pass over the DAG removing unreferenced objects from
2799 * appropriate lists.
2801 unlink_object(root
);
2803 /* Unmap all objects that are no longer referenced. */
2804 linkp
= &obj_list
->next
;
2805 while ((obj
= *linkp
) != NULL
) {
2806 if (obj
->refcount
== 0) {
2807 LD_UTRACE(UTRACE_UNLOAD_OBJECT
, obj
, obj
->mapbase
, obj
->mapsize
, 0,
2809 dbg("unloading \"%s\"", obj
->path
);
2810 munmap(obj
->mapbase
, obj
->mapsize
);
2811 linkmap_delete(obj
);
2822 unlink_object(Obj_Entry
*root
)
2826 if (root
->refcount
== 0) {
2827 /* Remove the object from the RTLD_GLOBAL list. */
2828 objlist_remove(&list_global
, root
);
2830 /* Remove the object from all objects' DAG lists. */
2831 STAILQ_FOREACH(elm
, &root
->dagmembers
, link
) {
2832 objlist_remove(&elm
->obj
->dldags
, root
);
2833 if (elm
->obj
!= root
)
2834 unlink_object(elm
->obj
);
2840 ref_dag(Obj_Entry
*root
)
2844 STAILQ_FOREACH(elm
, &root
->dagmembers
, link
)
2845 elm
->obj
->refcount
++;
2849 unref_dag(Obj_Entry
*root
)
2853 STAILQ_FOREACH(elm
, &root
->dagmembers
, link
)
2854 elm
->obj
->refcount
--;
2858 * Common code for MD __tls_get_addr().
2861 tls_get_addr_common(Elf_Addr
** dtvp
, int index
, size_t offset
)
2863 Elf_Addr
* dtv
= *dtvp
;
2866 /* Check dtv generation in case new modules have arrived */
2867 if (dtv
[0] != tls_dtv_generation
) {
2871 lockstate
= wlock_acquire(rtld_bind_lock
);
2872 newdtv
= calloc(1, (tls_max_index
+ 2) * sizeof(Elf_Addr
));
2874 if (to_copy
> tls_max_index
)
2875 to_copy
= tls_max_index
;
2876 memcpy(&newdtv
[2], &dtv
[2], to_copy
* sizeof(Elf_Addr
));
2877 newdtv
[0] = tls_dtv_generation
;
2878 newdtv
[1] = tls_max_index
;
2880 wlock_release(rtld_bind_lock
, lockstate
);
2884 /* Dynamically allocate module TLS if necessary */
2885 if (!dtv
[index
+ 1]) {
2886 /* Signal safe, wlock will block out signals. */
2887 lockstate
= wlock_acquire(rtld_bind_lock
);
2888 if (!dtv
[index
+ 1])
2889 dtv
[index
+ 1] = (Elf_Addr
)allocate_module_tls(index
);
2890 wlock_release(rtld_bind_lock
, lockstate
);
2892 return (void*) (dtv
[index
+ 1] + offset
);
2895 /* XXX not sure what variants to use for arm. */
2897 #if defined(__ia64__) || defined(__powerpc__)
2900 * Allocate Static TLS using the Variant I method.
2903 allocate_tls(Obj_Entry
*objs
, void *oldtcb
, size_t tcbsize
, size_t tcbalign
)
2912 if (oldtcb
!= NULL
&& tcbsize
== TLS_TCB_SIZE
)
2915 assert(tcbsize
>= TLS_TCB_SIZE
);
2916 tcb
= calloc(1, tls_static_space
- TLS_TCB_SIZE
+ tcbsize
);
2917 tls
= (Elf_Addr
**)(tcb
+ tcbsize
- TLS_TCB_SIZE
);
2919 if (oldtcb
!= NULL
) {
2920 memcpy(tls
, oldtcb
, tls_static_space
);
2923 /* Adjust the DTV. */
2925 for (i
= 0; i
< dtv
[1]; i
++) {
2926 if (dtv
[i
+2] >= (Elf_Addr
)oldtcb
&&
2927 dtv
[i
+2] < (Elf_Addr
)oldtcb
+ tls_static_space
) {
2928 dtv
[i
+2] = dtv
[i
+2] - (Elf_Addr
)oldtcb
+ (Elf_Addr
)tls
;
2932 dtv
= calloc(tls_max_index
+ 2, sizeof(Elf_Addr
));
2934 dtv
[0] = tls_dtv_generation
;
2935 dtv
[1] = tls_max_index
;
2937 for (obj
= objs
; obj
; obj
= obj
->next
) {
2938 if (obj
->tlsoffset
) {
2939 addr
= (Elf_Addr
)tls
+ obj
->tlsoffset
;
2940 memset((void*) (addr
+ obj
->tlsinitsize
),
2941 0, obj
->tlssize
- obj
->tlsinitsize
);
2943 memcpy((void*) addr
, obj
->tlsinit
,
2945 dtv
[obj
->tlsindex
+ 1] = addr
;
2954 free_tls(void *tcb
, size_t tcbsize
, size_t tcbalign
)
2957 Elf_Addr tlsstart
, tlsend
;
2960 assert(tcbsize
>= TLS_TCB_SIZE
);
2962 tlsstart
= (Elf_Addr
)tcb
+ tcbsize
- TLS_TCB_SIZE
;
2963 tlsend
= tlsstart
+ tls_static_space
;
2965 dtv
= *(Elf_Addr
**)tlsstart
;
2967 for (i
= 0; i
< dtvsize
; i
++) {
2968 if (dtv
[i
+2] && (dtv
[i
+2] < tlsstart
|| dtv
[i
+2] >= tlsend
)) {
2969 free((void*)dtv
[i
+2]);
2978 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \
2979 defined(__arm__) || defined(__mips__)
2982 * Allocate Static TLS using the Variant II method.
2985 allocate_tls(Obj_Entry
*objs
, void *oldtls
, size_t tcbsize
, size_t tcbalign
)
2990 Elf_Addr
*dtv
, *olddtv
;
2991 Elf_Addr segbase
, oldsegbase
, addr
;
2994 size
= round(tls_static_space
, tcbalign
);
2996 assert(tcbsize
>= 2*sizeof(Elf_Addr
));
2997 tls
= calloc(1, size
+ tcbsize
);
2998 dtv
= calloc(1, (tls_max_index
+ 2) * sizeof(Elf_Addr
));
3000 segbase
= (Elf_Addr
)(tls
+ size
);
3001 ((Elf_Addr
*)segbase
)[0] = segbase
;
3002 ((Elf_Addr
*)segbase
)[1] = (Elf_Addr
) dtv
;
3004 dtv
[0] = tls_dtv_generation
;
3005 dtv
[1] = tls_max_index
;
3009 * Copy the static TLS block over whole.
3011 oldsegbase
= (Elf_Addr
) oldtls
;
3012 memcpy((void *)(segbase
- tls_static_space
),
3013 (const void *)(oldsegbase
- tls_static_space
),
3017 * If any dynamic TLS blocks have been created tls_get_addr(),
3020 olddtv
= ((Elf_Addr
**)oldsegbase
)[1];
3021 for (i
= 0; i
< olddtv
[1]; i
++) {
3022 if (olddtv
[i
+2] < oldsegbase
- size
|| olddtv
[i
+2] > oldsegbase
) {
3023 dtv
[i
+2] = olddtv
[i
+2];
3029 * We assume that this block was the one we created with
3030 * allocate_initial_tls().
3032 free_tls(oldtls
, 2*sizeof(Elf_Addr
), sizeof(Elf_Addr
));
3034 for (obj
= objs
; obj
; obj
= obj
->next
) {
3035 if (obj
->tlsoffset
) {
3036 addr
= segbase
- obj
->tlsoffset
;
3037 memset((void*) (addr
+ obj
->tlsinitsize
),
3038 0, obj
->tlssize
- obj
->tlsinitsize
);
3040 memcpy((void*) addr
, obj
->tlsinit
, obj
->tlsinitsize
);
3041 dtv
[obj
->tlsindex
+ 1] = addr
;
3046 return (void*) segbase
;
3050 free_tls(void *tls
, size_t tcbsize
, size_t tcbalign
)
3055 Elf_Addr tlsstart
, tlsend
;
3058 * Figure out the size of the initial TLS block so that we can
3059 * find stuff which ___tls_get_addr() allocated dynamically.
3061 size
= round(tls_static_space
, tcbalign
);
3063 dtv
= ((Elf_Addr
**)tls
)[1];
3065 tlsend
= (Elf_Addr
) tls
;
3066 tlsstart
= tlsend
- size
;
3067 for (i
= 0; i
< dtvsize
; i
++) {
3068 if (dtv
[i
+2] && (dtv
[i
+2] < tlsstart
|| dtv
[i
+2] > tlsend
)) {
3069 free((void*) dtv
[i
+2]);
3073 free((void*) tlsstart
);
3080 * Allocate TLS block for module with given index.
3083 allocate_module_tls(int index
)
3088 for (obj
= obj_list
; obj
; obj
= obj
->next
) {
3089 if (obj
->tlsindex
== index
)
3093 _rtld_error("Can't find module with TLS index %d", index
);
3097 p
= malloc(obj
->tlssize
);
3098 memcpy(p
, obj
->tlsinit
, obj
->tlsinitsize
);
3099 memset(p
+ obj
->tlsinitsize
, 0, obj
->tlssize
- obj
->tlsinitsize
);
3105 allocate_tls_offset(Obj_Entry
*obj
)
3112 if (obj
->tlssize
== 0) {
3113 obj
->tls_done
= true;
3117 if (obj
->tlsindex
== 1)
3118 off
= calculate_first_tls_offset(obj
->tlssize
, obj
->tlsalign
);
3120 off
= calculate_tls_offset(tls_last_offset
, tls_last_size
,
3121 obj
->tlssize
, obj
->tlsalign
);
3124 * If we have already fixed the size of the static TLS block, we
3125 * must stay within that size. When allocating the static TLS, we
3126 * leave a small amount of space spare to be used for dynamically
3127 * loading modules which use static TLS.
3129 if (tls_static_space
) {
3130 if (calculate_tls_end(off
, obj
->tlssize
) > tls_static_space
)
3134 tls_last_offset
= obj
->tlsoffset
= off
;
3135 tls_last_size
= obj
->tlssize
;
3136 obj
->tls_done
= true;
3142 free_tls_offset(Obj_Entry
*obj
)
3144 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \
3145 defined(__arm__) || defined(__mips__)
3147 * If we were the last thing to allocate out of the static TLS
3148 * block, we give our space back to the 'allocator'. This is a
3149 * simplistic workaround to allow libGL.so.1 to be loaded and
3150 * unloaded multiple times. We only handle the Variant II
3151 * mechanism for now - this really needs a proper allocator.
3153 if (calculate_tls_end(obj
->tlsoffset
, obj
->tlssize
)
3154 == calculate_tls_end(tls_last_offset
, tls_last_size
)) {
3155 tls_last_offset
-= obj
->tlssize
;
3162 _rtld_allocate_tls(void *oldtls
, size_t tcbsize
, size_t tcbalign
)
3167 lockstate
= wlock_acquire(rtld_bind_lock
);
3168 ret
= allocate_tls(obj_list
, oldtls
, tcbsize
, tcbalign
);
3169 wlock_release(rtld_bind_lock
, lockstate
);
3174 _rtld_free_tls(void *tcb
, size_t tcbsize
, size_t tcbalign
)
3178 lockstate
= wlock_acquire(rtld_bind_lock
);
3179 free_tls(tcb
, tcbsize
, tcbalign
);
3180 wlock_release(rtld_bind_lock
, lockstate
);
3184 object_add_name(Obj_Entry
*obj
, const char *name
)
3190 entry
= malloc(sizeof(Name_Entry
) + len
);
3192 if (entry
!= NULL
) {
3193 strcpy(entry
->name
, name
);
3194 STAILQ_INSERT_TAIL(&obj
->names
, entry
, link
);
3199 object_match_name(const Obj_Entry
*obj
, const char *name
)
3203 STAILQ_FOREACH(entry
, &obj
->names
, link
) {
3204 if (strcmp(name
, entry
->name
) == 0)
3211 locate_dependency(const Obj_Entry
*obj
, const char *name
)
3213 const Objlist_Entry
*entry
;
3214 const Needed_Entry
*needed
;
3216 STAILQ_FOREACH(entry
, &list_main
, link
) {
3217 if (object_match_name(entry
->obj
, name
))
3221 for (needed
= obj
->needed
; needed
!= NULL
; needed
= needed
->next
) {
3222 if (needed
->obj
== NULL
)
3224 if (object_match_name(needed
->obj
, name
))
3227 _rtld_error("%s: Unexpected inconsistency: dependency %s not found",
3233 check_object_provided_version(Obj_Entry
*refobj
, const Obj_Entry
*depobj
,
3234 const Elf_Vernaux
*vna
)
3236 const Elf_Verdef
*vd
;
3237 const char *vername
;
3239 vername
= refobj
->strtab
+ vna
->vna_name
;
3240 vd
= depobj
->verdef
;
3242 _rtld_error("%s: version %s required by %s not defined",
3243 depobj
->path
, vername
, refobj
->path
);
3247 if (vd
->vd_version
!= VER_DEF_CURRENT
) {
3248 _rtld_error("%s: Unsupported version %d of Elf_Verdef entry",
3249 depobj
->path
, vd
->vd_version
);
3252 if (vna
->vna_hash
== vd
->vd_hash
) {
3253 const Elf_Verdaux
*aux
= (const Elf_Verdaux
*)
3254 ((char *)vd
+ vd
->vd_aux
);
3255 if (strcmp(vername
, depobj
->strtab
+ aux
->vda_name
) == 0)
3258 if (vd
->vd_next
== 0)
3260 vd
= (const Elf_Verdef
*) ((char *)vd
+ vd
->vd_next
);
3262 if (vna
->vna_flags
& VER_FLG_WEAK
)
3264 _rtld_error("%s: version %s required by %s not found",
3265 depobj
->path
, vername
, refobj
->path
);
3270 rtld_verify_object_versions(Obj_Entry
*obj
)
3272 const Elf_Verneed
*vn
;
3273 const Elf_Verdef
*vd
;
3274 const Elf_Verdaux
*vda
;
3275 const Elf_Vernaux
*vna
;
3276 const Obj_Entry
*depobj
;
3277 int maxvernum
, vernum
;
3281 * Walk over defined and required version records and figure out
3282 * max index used by any of them. Do very basic sanity checking
3286 while (vn
!= NULL
) {
3287 if (vn
->vn_version
!= VER_NEED_CURRENT
) {
3288 _rtld_error("%s: Unsupported version %d of Elf_Verneed entry",
3289 obj
->path
, vn
->vn_version
);
3292 vna
= (const Elf_Vernaux
*) ((char *)vn
+ vn
->vn_aux
);
3294 vernum
= VER_NEED_IDX(vna
->vna_other
);
3295 if (vernum
> maxvernum
)
3297 if (vna
->vna_next
== 0)
3299 vna
= (const Elf_Vernaux
*) ((char *)vna
+ vna
->vna_next
);
3301 if (vn
->vn_next
== 0)
3303 vn
= (const Elf_Verneed
*) ((char *)vn
+ vn
->vn_next
);
3307 while (vd
!= NULL
) {
3308 if (vd
->vd_version
!= VER_DEF_CURRENT
) {
3309 _rtld_error("%s: Unsupported version %d of Elf_Verdef entry",
3310 obj
->path
, vd
->vd_version
);
3313 vernum
= VER_DEF_IDX(vd
->vd_ndx
);
3314 if (vernum
> maxvernum
)
3316 if (vd
->vd_next
== 0)
3318 vd
= (const Elf_Verdef
*) ((char *)vd
+ vd
->vd_next
);
3325 * Store version information in array indexable by version index.
3326 * Verify that object version requirements are satisfied along the
3329 obj
->vernum
= maxvernum
+ 1;
3330 obj
->vertab
= calloc(obj
->vernum
, sizeof(Ver_Entry
));
3333 while (vd
!= NULL
) {
3334 if ((vd
->vd_flags
& VER_FLG_BASE
) == 0) {
3335 vernum
= VER_DEF_IDX(vd
->vd_ndx
);
3336 assert(vernum
<= maxvernum
);
3337 vda
= (const Elf_Verdaux
*)((char *)vd
+ vd
->vd_aux
);
3338 obj
->vertab
[vernum
].hash
= vd
->vd_hash
;
3339 obj
->vertab
[vernum
].name
= obj
->strtab
+ vda
->vda_name
;
3340 obj
->vertab
[vernum
].file
= NULL
;
3341 obj
->vertab
[vernum
].flags
= 0;
3343 if (vd
->vd_next
== 0)
3345 vd
= (const Elf_Verdef
*) ((char *)vd
+ vd
->vd_next
);
3349 while (vn
!= NULL
) {
3350 depobj
= locate_dependency(obj
, obj
->strtab
+ vn
->vn_file
);
3351 vna
= (const Elf_Vernaux
*) ((char *)vn
+ vn
->vn_aux
);
3353 if (check_object_provided_version(obj
, depobj
, vna
))
3355 vernum
= VER_NEED_IDX(vna
->vna_other
);
3356 assert(vernum
<= maxvernum
);
3357 obj
->vertab
[vernum
].hash
= vna
->vna_hash
;
3358 obj
->vertab
[vernum
].name
= obj
->strtab
+ vna
->vna_name
;
3359 obj
->vertab
[vernum
].file
= obj
->strtab
+ vn
->vn_file
;
3360 obj
->vertab
[vernum
].flags
= (vna
->vna_other
& VER_NEED_HIDDEN
) ?
3361 VER_INFO_HIDDEN
: 0;
3362 if (vna
->vna_next
== 0)
3364 vna
= (const Elf_Vernaux
*) ((char *)vna
+ vna
->vna_next
);
3366 if (vn
->vn_next
== 0)
3368 vn
= (const Elf_Verneed
*) ((char *)vn
+ vn
->vn_next
);
3374 rtld_verify_versions(const Objlist
*objlist
)
3376 Objlist_Entry
*entry
;
3380 STAILQ_FOREACH(entry
, objlist
, link
) {
3382 * Skip dummy objects or objects that have their version requirements
3385 if (entry
->obj
->strtab
== NULL
|| entry
->obj
->vertab
!= NULL
)
3387 if (rtld_verify_object_versions(entry
->obj
) == -1) {
3389 if (ld_tracing
== NULL
)
3393 if (rc
== 0 || ld_tracing
!= NULL
)
3394 rc
= rtld_verify_object_versions(&obj_rtld
);
3399 fetch_ventry(const Obj_Entry
*obj
, unsigned long symnum
)
3404 vernum
= VER_NDX(obj
->versyms
[symnum
]);
3405 if (vernum
>= obj
->vernum
) {
3406 _rtld_error("%s: symbol %s has wrong verneed value %d",
3407 obj
->path
, obj
->strtab
+ symnum
, vernum
);
3408 } else if (obj
->vertab
[vernum
].hash
!= 0) {
3409 return &obj
->vertab
[vernum
];