7 * ELF run-time linker, ported from FreeBSD by KJK::Hyperion as part of the ELF
8 * support initiative. Original copyright, licensing and disclaimers follow
12 * Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra.
13 * Copyright 2003 Alexander Kabaev <kan@FreeBSD.ORG>.
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * $FreeBSD: src/libexec/rtld-elf/rtld.c,v 1.101 2004/11/02 09:42:21 ssouhlal Exp $
40 * Dynamic linker for ELF.
42 * John Polstra <jdp@polstra.com>.
52 #error "GCC is needed to compile this file"
55 #include <sys/param.h>
75 #define PATH_RTLD "/libexec/ld-elf.so.1"
77 #define PATH_RTLD "/libexec/ld-elf32.so.1"
81 typedef void (*func_ptr_type
)();
82 typedef void * (*path_enum_proc
) (const char *path
, size_t len
, void *arg
);
85 * This structure provides a reentrant way to keep a list of objects and
86 * check which ones have already been processed in some way.
88 typedef struct Struct_DoneList
{
89 const Obj_Entry
**objs
; /* Array of object pointers */
90 unsigned int num_alloc
; /* Allocated size of the array */
91 unsigned int num_used
; /* Number of array slots used */
95 * Function declarations.
97 static const char *basename(const char *);
98 static void die(void);
99 static void digest_dynamic(Obj_Entry
*, int);
100 static Obj_Entry
*digest_phdr(const Elf_Phdr
*, int, caddr_t
, const char *);
101 static Obj_Entry
*dlcheck(void *);
102 static int do_search_info(const Obj_Entry
*obj
, int, struct dl_serinfo
*);
103 static bool donelist_check(DoneList
*, const Obj_Entry
*);
104 static void errmsg_restore(char *);
105 static char *errmsg_save(void);
106 static void *fill_search_info(const char *, size_t, void *);
107 static char *find_library(const char *, const Obj_Entry
*);
108 static const char *gethints(void);
109 static void init_dag(Obj_Entry
*);
110 static void init_dag1(Obj_Entry
*root
, Obj_Entry
*obj
, DoneList
*);
111 static void init_rtld(caddr_t
);
112 static void initlist_add_neededs(Needed_Entry
*needed
, Objlist
*list
);
113 static void initlist_add_objects(Obj_Entry
*obj
, Obj_Entry
**tail
,
115 static bool is_exported(const Elf_Sym
*);
116 static void linkmap_add(Obj_Entry
*);
117 static void linkmap_delete(Obj_Entry
*);
118 static int load_needed_objects(Obj_Entry
*);
119 static int load_preload_objects(void);
120 static Obj_Entry
*load_object(char *);
121 static Obj_Entry
*obj_from_addr(const void *);
122 static void objlist_call_fini(Objlist
*);
123 static void objlist_call_init(Objlist
*);
124 static void objlist_clear(Objlist
*);
125 static Objlist_Entry
*objlist_find(Objlist
*, const Obj_Entry
*);
126 static void objlist_init(Objlist
*);
127 static void objlist_push_head(Objlist
*, Obj_Entry
*);
128 static void objlist_push_tail(Objlist
*, Obj_Entry
*);
129 static void objlist_remove(Objlist
*, Obj_Entry
*);
130 static void objlist_remove_unref(Objlist
*);
131 static void *path_enumerate(const char *, path_enum_proc
, void *);
132 static int relocate_objects(Obj_Entry
*, bool, Obj_Entry
*);
133 static int rtld_dirname(const char *, char *);
134 static void rtld_exit(void);
135 static char *search_library_path(const char *, const char *);
136 static const void **get_program_var_addr(const char *name
);
137 static void set_program_var(const char *, const void *);
138 static const Elf_Sym
*symlook_default(const char *, unsigned long hash
,
139 const Obj_Entry
*refobj
, const Obj_Entry
**defobj_out
, bool in_plt
);
140 static const Elf_Sym
*symlook_list(const char *, unsigned long,
141 Objlist
*, const Obj_Entry
**, bool in_plt
, DoneList
*);
142 static void trace_loaded_objects(Obj_Entry
*obj
);
143 static void unlink_object(Obj_Entry
*);
144 static void unload_object(Obj_Entry
*);
145 static void unref_dag(Obj_Entry
*);
146 static void ref_dag(Obj_Entry
*);
148 void r_debug_state(struct r_debug
*, struct link_map
*);
153 static char *error_message
; /* Message for dlerror(), or NULL */
154 struct r_debug r_debug
; /* for GDB; */
155 static bool libmap_disable
; /* Disable libmap */
156 static bool trust
; /* False for setuid and setgid programs */
157 static char *ld_bind_now
; /* Environment variable for immediate binding */
158 static char *ld_debug
; /* Environment variable for debugging */
159 static char *ld_library_path
; /* Environment variable for search path */
160 static char *ld_preload
; /* Environment variable for libraries to
162 static char *ld_tracing
; /* Called from ldd to print libs */
163 static Obj_Entry
*obj_list
; /* Head of linked list of shared objects */
164 static Obj_Entry
**obj_tail
; /* Link field of last object in list */
165 static Obj_Entry
*obj_main
; /* The main program shared object */
166 static Obj_Entry obj_rtld
; /* The dynamic linker shared object */
167 static unsigned int obj_count
; /* 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
) &dladdr
,
198 (func_ptr_type
) &dllockinit
,
199 (func_ptr_type
) &dlinfo
,
200 (func_ptr_type
) &_rtld_thread_init
,
202 (func_ptr_type
) &___tls_get_addr
,
204 (func_ptr_type
) &__tls_get_addr
,
205 (func_ptr_type
) &_rtld_allocate_tls
,
206 (func_ptr_type
) &_rtld_free_tls
,
211 * Global declarations normally provided by crt1. The dynamic linker is
212 * not built with crt1, so we have to provide them ourselves.
218 * Globals to control TLS allocation.
220 size_t tls_last_offset
; /* Static TLS offset of last module */
221 size_t tls_last_size
; /* Static TLS size of last module */
222 size_t tls_static_space
; /* Static TLS space allocated */
223 int tls_dtv_generation
= 1; /* Used to detect when dtv size changes */
224 int tls_max_index
= 1; /* Largest module index allocated */
227 * Fill in a DoneList with an allocation large enough to hold all of
228 * the currently-loaded objects. Keep this as a macro since it calls
229 * alloca and we want that to occur within the scope of the caller.
231 #define donelist_init(dlp) \
232 ((dlp)->objs = alloca(obj_count * sizeof (dlp)->objs[0]), \
233 assert((dlp)->objs != NULL), \
234 (dlp)->num_alloc = obj_count, \
238 * Main entry point for dynamic linking. The first argument is the
239 * stack pointer. The stack is expected to be laid out as described
240 * in the SVR4 ABI specification, Intel 386 Processor Supplement.
241 * Specifically, the stack pointer points to a word containing
242 * ARGC. Following that in the stack is a null-terminated sequence
243 * of pointers to argument strings. Then comes a null-terminated
244 * sequence of pointers to environment strings. Finally, there is a
245 * sequence of "auxiliary vector" entries.
247 * The second argument points to a place to store the dynamic linker's
248 * exit procedure pointer and the third to a place to store the main
251 * The return value is the main program's entry point.
254 _rtld(Elf_Addr
*sp
, func_ptr_type
*exit_proc
, Obj_Entry
**objp
)
256 Elf_Auxinfo
*aux_info
[AT_COUNT
];
264 Objlist_Entry
*entry
;
266 Obj_Entry
**preload_tail
;
271 * On entry, the dynamic linker itself has not been relocated yet.
272 * Be very careful not to reference any global data until after
273 * init_rtld has returned. It is OK to reference file-scope statics
274 * and string constants, and to call static and global functions.
277 /* Find the auxiliary vector on the stack. */
280 sp
+= argc
+ 1; /* Skip over arguments and NULL terminator */
282 while (*sp
++ != 0) /* Skip over environment, and NULL terminator */
284 aux
= (Elf_Auxinfo
*) sp
;
286 /* Digest the auxiliary vector. */
287 for (i
= 0; i
< AT_COUNT
; i
++)
289 for (auxp
= aux
; auxp
->a_type
!= AT_NULL
; auxp
++) {
290 if (auxp
->a_type
< AT_COUNT
)
291 aux_info
[auxp
->a_type
] = auxp
;
294 /* Initialize and relocate ourselves. */
295 assert(aux_info
[AT_BASE
] != NULL
);
296 init_rtld((caddr_t
) aux_info
[AT_BASE
]->a_un
.a_ptr
);
298 __progname
= obj_rtld
.path
;
299 argv0
= argv
[0] != NULL
? argv
[0] : "(null)";
302 trust
= !issetugid();
304 ld_bind_now
= getenv(LD_
"BIND_NOW");
306 ld_debug
= getenv(LD_
"DEBUG");
307 libmap_disable
= getenv(LD_
"LIBMAP_DISABLE") != NULL
;
308 ld_library_path
= getenv(LD_
"LIBRARY_PATH");
309 ld_preload
= getenv(LD_
"PRELOAD");
311 ld_tracing
= getenv(LD_
"TRACE_LOADED_OBJECTS");
313 if (ld_debug
!= NULL
&& *ld_debug
!= '\0')
315 dbg("%s is initialized, base address = %p", __progname
,
316 (caddr_t
) aux_info
[AT_BASE
]->a_un
.a_ptr
);
317 dbg("RTLD dynamic = %p", obj_rtld
.dynamic
);
318 dbg("RTLD pltgot = %p", obj_rtld
.pltgot
);
321 * Load the main program, or process its program header if it is
324 if (aux_info
[AT_EXECFD
] != NULL
) { /* Load the main program. */
325 int fd
= aux_info
[AT_EXECFD
]->a_un
.a_val
;
326 dbg("loading main program");
327 obj_main
= map_object(fd
, argv0
, NULL
);
329 if (obj_main
== NULL
)
331 } else { /* Main program already loaded. */
332 const Elf_Phdr
*phdr
;
336 dbg("processing main program's program header");
337 assert(aux_info
[AT_PHDR
] != NULL
);
338 phdr
= (const Elf_Phdr
*) aux_info
[AT_PHDR
]->a_un
.a_ptr
;
339 assert(aux_info
[AT_PHNUM
] != NULL
);
340 phnum
= aux_info
[AT_PHNUM
]->a_un
.a_val
;
341 assert(aux_info
[AT_PHENT
] != NULL
);
342 assert(aux_info
[AT_PHENT
]->a_un
.a_val
== sizeof(Elf_Phdr
));
343 assert(aux_info
[AT_ENTRY
] != NULL
);
344 entry
= (caddr_t
) aux_info
[AT_ENTRY
]->a_un
.a_ptr
;
345 if ((obj_main
= digest_phdr(phdr
, phnum
, entry
, argv0
)) == NULL
)
349 obj_main
->path
= xstrdup(argv0
);
350 obj_main
->mainprog
= true;
353 * Get the actual dynamic linker pathname from the executable if
354 * possible. (It should always be possible.) That ensures that
355 * gdb will find the right dynamic linker even if a non-standard
358 if (obj_main
->interp
!= NULL
&&
359 strcmp(obj_main
->interp
, obj_rtld
.path
) != 0) {
361 obj_rtld
.path
= xstrdup(obj_main
->interp
);
362 __progname
= obj_rtld
.path
;
365 digest_dynamic(obj_main
, 0);
367 linkmap_add(obj_main
);
368 linkmap_add(&obj_rtld
);
370 /* Link the main program into the list of objects. */
371 *obj_tail
= obj_main
;
372 obj_tail
= &obj_main
->next
;
374 /* Make sure we don't call the main program's init and fini functions. */
375 obj_main
->init
= obj_main
->fini
= (Elf_Addr
)NULL
;
377 /* Initialize a fake symbol for resolving undefined weak references. */
378 sym_zero
.st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_NOTYPE
);
379 sym_zero
.st_shndx
= SHN_UNDEF
;
382 libmap_disable
= (bool)lm_init();
384 dbg("loading LD_PRELOAD libraries");
385 if (load_preload_objects() == -1)
387 preload_tail
= obj_tail
;
389 dbg("loading needed objects");
390 if (load_needed_objects(obj_main
) == -1)
393 /* Make a list of all objects loaded at startup. */
394 for (obj
= obj_list
; obj
!= NULL
; obj
= obj
->next
) {
395 objlist_push_tail(&list_main
, obj
);
399 if (ld_tracing
) { /* We're done */
400 trace_loaded_objects(obj_main
);
404 if (getenv(LD_
"DUMP_REL_PRE") != NULL
) {
405 dump_relocations(obj_main
);
409 /* setup TLS for main thread */
410 dbg("initializing initial thread local storage");
411 STAILQ_FOREACH(entry
, &list_main
, link
) {
413 * Allocate all the initial objects out of the static TLS
414 * block even if they didn't ask for it.
416 allocate_tls_offset(entry
->obj
);
418 allocate_initial_tls(obj_list
);
420 if (relocate_objects(obj_main
,
421 ld_bind_now
!= NULL
&& *ld_bind_now
!= '\0', &obj_rtld
) == -1)
424 dbg("doing copy relocations");
425 if (do_copy_relocations(obj_main
) == -1)
428 if (getenv(LD_
"DUMP_REL_POST") != NULL
) {
429 dump_relocations(obj_main
);
433 dbg("initializing key program variables");
434 set_program_var("__progname", argv
[0] != NULL
? basename(argv
[0]) : "");
435 set_program_var("environ", env
);
437 dbg("initializing thread locks");
440 /* Make a list of init functions to call. */
441 objlist_init(&initlist
);
442 initlist_add_objects(obj_list
, preload_tail
, &initlist
);
444 r_debug_state(NULL
, &obj_main
->linkmap
); /* say hello to gdb! */
446 objlist_call_init(&initlist
);
447 lockstate
= wlock_acquire(rtld_bind_lock
);
448 objlist_clear(&initlist
);
449 wlock_release(rtld_bind_lock
, lockstate
);
451 dbg("transferring control to program entry point = %p", obj_main
->entry
);
453 /* Return the exit procedure and the program entry point. */
454 *exit_proc
= rtld_exit
;
456 return (func_ptr_type
) obj_main
->entry
;
462 _rtld_bind(Obj_Entry
*obj
, Elf_Word reloff
)
466 const Obj_Entry
*defobj
;
471 lockstate
= rlock_acquire(rtld_bind_lock
);
473 rel
= (const Elf_Rel
*) ((caddr_t
) obj
->pltrel
+ reloff
);
475 rel
= (const Elf_Rel
*) ((caddr_t
) obj
->pltrela
+ reloff
);
477 where
= (Elf_Addr
*) (obj
->relocbase
+ rel
->r_offset
);
478 def
= find_symdef(ELF_R_SYM(rel
->r_info
), obj
, &defobj
, true, NULL
);
482 target
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
484 dbg("\"%s\" in \"%s\" ==> %p in \"%s\"",
485 defobj
->strtab
+ def
->st_name
, basename(obj
->path
),
486 (void *)target
, basename(defobj
->path
));
489 * Write the new contents for the jmpslot. Note that depending on
490 * architecture, the value which we need to return back to the
491 * lazy binding trampoline may or may not be the target
492 * address. The value returned from reloc_jmpslot() is the value
493 * that the trampoline needs.
495 target
= reloc_jmpslot(where
, target
, defobj
, obj
, rel
);
496 rlock_release(rtld_bind_lock
, lockstate
);
503 * Error reporting function. Use it like printf. If formats the message
504 * into a buffer, and sets things up so that the next call to dlerror()
505 * will return the message.
508 _rtld_error(const char *fmt
, ...)
510 static char buf
[512];
514 vsnprintf(buf
, sizeof buf
, fmt
, ap
);
520 * Return a dynamically-allocated copy of the current error message, if any.
525 return error_message
== NULL
? NULL
: xstrdup(error_message
);
529 * Restore the current error message from a copy which was previously saved
530 * by errmsg_save(). The copy is freed.
533 errmsg_restore(char *saved_msg
)
535 if (saved_msg
== NULL
)
536 error_message
= NULL
;
538 _rtld_error("%s", saved_msg
);
544 basename(const char *name
)
546 const char *p
= strrchr(name
, '/');
547 return p
!= NULL
? p
+ 1 : name
;
553 const char *msg
= dlerror();
563 * Process a shared object's DYNAMIC section, and save the important
564 * information in its Obj_Entry structure.
567 digest_dynamic(Obj_Entry
*obj
, int early
)
570 Needed_Entry
**needed_tail
= &obj
->needed
;
571 const Elf_Dyn
*dyn_rpath
= NULL
;
572 int plttype
= DT_REL
;
574 obj
->bind_now
= false;
575 for (dynp
= obj
->dynamic
; dynp
->d_tag
!= DT_NULL
; dynp
++) {
576 switch (dynp
->d_tag
) {
579 obj
->rel
= (const Elf_Rel
*) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
583 obj
->relsize
= dynp
->d_un
.d_val
;
587 assert(dynp
->d_un
.d_val
== sizeof(Elf_Rel
));
591 obj
->pltrel
= (const Elf_Rel
*)
592 (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
596 obj
->pltrelsize
= dynp
->d_un
.d_val
;
600 obj
->rela
= (const Elf_Rela
*) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
604 obj
->relasize
= dynp
->d_un
.d_val
;
608 assert(dynp
->d_un
.d_val
== sizeof(Elf_Rela
));
612 plttype
= dynp
->d_un
.d_val
;
613 assert(dynp
->d_un
.d_val
== DT_REL
|| plttype
== DT_RELA
);
617 obj
->symtab
= (const Elf_Sym
*)
618 (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
622 assert(dynp
->d_un
.d_val
== sizeof(Elf_Sym
));
626 obj
->strtab
= (const char *) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
630 obj
->strsize
= dynp
->d_un
.d_val
;
635 const Elf_Hashelt
*hashtab
= (const Elf_Hashelt
*)
636 (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
637 obj
->nbuckets
= hashtab
[0];
638 obj
->nchains
= hashtab
[1];
639 obj
->buckets
= hashtab
+ 2;
640 obj
->chains
= obj
->buckets
+ obj
->nbuckets
;
646 Needed_Entry
*nep
= NEW(Needed_Entry
);
647 nep
->name
= dynp
->d_un
.d_val
;
652 needed_tail
= &nep
->next
;
657 obj
->pltgot
= (Elf_Addr
*) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
665 obj
->symbolic
= true;
669 case DT_RUNPATH
: /* XXX: process separately */
671 * We have to wait until later to process this, because we
672 * might not have gotten the address of the string table yet.
678 /* Not used by the dynamic linker. */
682 obj
->init
= (Elf_Addr
) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
686 obj
->fini
= (Elf_Addr
) (obj
->relocbase
+ dynp
->d_un
.d_ptr
);
690 /* XXX - not implemented yet */
692 dbg("Filling in DT_DEBUG entry");
693 ((Elf_Dyn
*)dynp
)->d_un
.d_ptr
= (Elf_Addr
) &r_debug
;
697 if (dynp
->d_un
.d_val
& DF_ORIGIN
) {
698 obj
->origin_path
= xmalloc(PATH_MAX
);
699 if (rtld_dirname(obj
->path
, obj
->origin_path
) == -1)
702 if (dynp
->d_un
.d_val
& DF_SYMBOLIC
)
703 obj
->symbolic
= true;
704 if (dynp
->d_un
.d_val
& DF_TEXTREL
)
706 if (dynp
->d_un
.d_val
& DF_BIND_NOW
)
707 obj
->bind_now
= true;
708 if (dynp
->d_un
.d_val
& DF_STATIC_TLS
)
714 dbg("Ignoring d_tag %ld = %#lx", (long)dynp
->d_tag
,
723 if (plttype
== DT_RELA
) {
724 obj
->pltrela
= (const Elf_Rela
*) obj
->pltrel
;
726 obj
->pltrelasize
= obj
->pltrelsize
;
730 if (dyn_rpath
!= NULL
)
731 obj
->rpath
= obj
->strtab
+ dyn_rpath
->d_un
.d_val
;
735 * Process a shared object's program header. This is used only for the
736 * main program, when the kernel has already loaded the main program
737 * into memory before calling the dynamic linker. It creates and
738 * returns an Obj_Entry structure.
741 digest_phdr(const Elf_Phdr
*phdr
, int phnum
, caddr_t entry
, const char *path
)
744 const Elf_Phdr
*phlimit
= phdr
+ phnum
;
749 for (ph
= phdr
; ph
< phlimit
; ph
++) {
750 switch (ph
->p_type
) {
753 if ((const Elf_Phdr
*)ph
->p_vaddr
!= phdr
) {
754 _rtld_error("%s: invalid PT_PHDR", path
);
757 obj
->phdr
= (const Elf_Phdr
*) ph
->p_vaddr
;
758 obj
->phsize
= ph
->p_memsz
;
762 obj
->interp
= (const char *) ph
->p_vaddr
;
766 if (nsegs
== 0) { /* First load segment */
767 obj
->vaddrbase
= trunc_page(ph
->p_vaddr
);
768 obj
->mapbase
= (caddr_t
) obj
->vaddrbase
;
769 obj
->relocbase
= obj
->mapbase
- obj
->vaddrbase
;
770 obj
->textsize
= round_page(ph
->p_vaddr
+ ph
->p_memsz
) -
772 } else { /* Last load segment */
773 obj
->mapsize
= round_page(ph
->p_vaddr
+ ph
->p_memsz
) -
780 obj
->dynamic
= (const Elf_Dyn
*) ph
->p_vaddr
;
785 obj
->tlssize
= ph
->p_memsz
;
786 obj
->tlsalign
= ph
->p_align
;
787 obj
->tlsinitsize
= ph
->p_filesz
;
788 obj
->tlsinit
= (void*) ph
->p_vaddr
;
793 _rtld_error("%s: too few PT_LOAD segments", path
);
804 dlcheck(void *handle
)
808 for (obj
= obj_list
; obj
!= NULL
; obj
= obj
->next
)
809 if (obj
== (Obj_Entry
*) handle
)
812 if (obj
== NULL
|| obj
->refcount
== 0 || obj
->dl_refcount
== 0) {
813 _rtld_error("Invalid shared object handle %p", handle
);
820 * If the given object is already in the donelist, return true. Otherwise
821 * add the object to the list and return false.
824 donelist_check(DoneList
*dlp
, const Obj_Entry
*obj
)
828 for (i
= 0; i
< dlp
->num_used
; i
++)
829 if (dlp
->objs
[i
] == obj
)
832 * Our donelist allocation should always be sufficient. But if
833 * our threads locking isn't working properly, more shared objects
834 * could have been loaded since we allocated the list. That should
835 * never happen, but we'll handle it properly just in case it does.
837 if (dlp
->num_used
< dlp
->num_alloc
)
838 dlp
->objs
[dlp
->num_used
++] = obj
;
845 * Hash function for symbol table lookup. Don't even think about changing
846 * this. It is specified by the System V ABI.
849 elf_hash(const char *name
)
851 const unsigned char *p
= (const unsigned char *) name
;
857 if ((g
= h
& 0xf0000000) != 0)
867 * Find the library with the given name, and return its full pathname.
868 * The returned string is dynamically allocated. Generates an error
869 * message and returns NULL if the library cannot be found.
871 * If the second argument is non-NULL, then it refers to an already-
872 * loaded shared object, whose library search path will be searched.
874 * The search order is:
876 * rpath in the referencing file
881 find_library(const char *xname
, const Obj_Entry
*refobj
)
886 if (strchr(xname
, '/') != NULL
) { /* Hard coded pathname */
887 if (xname
[0] != '/' && !trust
) {
888 _rtld_error("Absolute pathname required for shared object \"%s\"",
892 return xstrdup(xname
);
895 if (libmap_disable
|| (refobj
== NULL
) ||
896 (name
= lm_find(refobj
->path
, xname
)) == NULL
)
897 name
= (char *)xname
;
899 dbg(" Searching for \"%s\"", name
);
901 if ((pathname
= search_library_path(name
, ld_library_path
)) != NULL
||
903 (pathname
= search_library_path(name
, refobj
->rpath
)) != NULL
) ||
904 (pathname
= search_library_path(name
, gethints())) != NULL
||
905 (pathname
= search_library_path(name
, STANDARD_LIBRARY_PATH
)) != NULL
)
908 if(refobj
!= NULL
&& refobj
->path
!= NULL
) {
909 _rtld_error("Shared object \"%s\" not found, required by \"%s\"",
910 name
, basename(refobj
->path
));
912 _rtld_error("Shared object \"%s\" not found", name
);
920 * Given a symbol number in a referencing object, find the corresponding
921 * definition of the symbol. Returns a pointer to the symbol, or NULL if
922 * no definition was found. Returns a pointer to the Obj_Entry of the
923 * defining object via the reference parameter DEFOBJ_OUT.
926 find_symdef(unsigned long symnum
, const Obj_Entry
*refobj
,
927 const Obj_Entry
**defobj_out
, bool in_plt
, SymCache
*cache
)
931 const Obj_Entry
*defobj
;
936 * If we have already found this symbol, get the information from
939 if (symnum
>= refobj
->nchains
)
940 return NULL
; /* Bad object */
941 if (cache
!= NULL
&& cache
[symnum
].sym
!= NULL
) {
942 *defobj_out
= cache
[symnum
].obj
;
943 return cache
[symnum
].sym
;
946 ref
= refobj
->symtab
+ symnum
;
947 name
= refobj
->strtab
+ ref
->st_name
;
951 * We don't have to do a full scale lookup if the symbol is local.
952 * We know it will bind to the instance in this load module; to
953 * which we already have a pointer (ie ref). By not doing a lookup,
954 * we not only improve performance, but it also avoids unresolvable
955 * symbols when local symbols are not in the hash table. This has
956 * been seen with the ia64 toolchain.
958 if (ELF_ST_BIND(ref
->st_info
) != STB_LOCAL
) {
959 if (ELF_ST_TYPE(ref
->st_info
) == STT_SECTION
) {
960 _rtld_error("%s: Bogus symbol table entry %lu", refobj
->path
,
963 hash
= elf_hash(name
);
964 def
= symlook_default(name
, hash
, refobj
, &defobj
, in_plt
);
971 * If we found no definition and the reference is weak, treat the
972 * symbol as having the value zero.
974 if (def
== NULL
&& ELF_ST_BIND(ref
->st_info
) == STB_WEAK
) {
980 *defobj_out
= defobj
;
981 /* Record the information in the cache to avoid subsequent lookups. */
983 cache
[symnum
].sym
= def
;
984 cache
[symnum
].obj
= defobj
;
987 if (refobj
!= &obj_rtld
)
988 _rtld_error("%s: Undefined symbol \"%s\"", refobj
->path
, name
);
996 * Return the search path from the ldconfig hints file, reading it if
997 * necessary. Returns NULL if there are problems with the hints file,
998 * or if the search path there is empty.
1005 if (hints
== NULL
) {
1007 struct elfhints_hdr hdr
;
1010 /* Keep from trying again in case the hints file is bad. */
1013 if ((fd
= open(_PATH_ELF_HINTS
, O_RDONLY
)) == -1)
1015 if (read(fd
, &hdr
, sizeof hdr
) != sizeof hdr
||
1016 hdr
.magic
!= ELFHINTS_MAGIC
||
1021 p
= xmalloc(hdr
.dirlistlen
+ 1);
1022 if (lseek(fd
, hdr
.strtab
+ hdr
.dirlist
, SEEK_SET
) == -1 ||
1023 read(fd
, p
, hdr
.dirlistlen
+ 1) != (ssize_t
)hdr
.dirlistlen
+ 1) {
1031 return hints
[0] != '\0' ? hints
: NULL
;
1035 init_dag(Obj_Entry
*root
)
1039 donelist_init(&donelist
);
1040 init_dag1(root
, root
, &donelist
);
1044 init_dag1(Obj_Entry
*root
, Obj_Entry
*obj
, DoneList
*dlp
)
1046 const Needed_Entry
*needed
;
1048 if (donelist_check(dlp
, obj
))
1052 objlist_push_tail(&obj
->dldags
, root
);
1053 objlist_push_tail(&root
->dagmembers
, obj
);
1054 for (needed
= obj
->needed
; needed
!= NULL
; needed
= needed
->next
)
1055 if (needed
->obj
!= NULL
)
1056 init_dag1(root
, needed
->obj
, dlp
);
1060 * Initialize the dynamic linker. The argument is the address at which
1061 * the dynamic linker has been mapped into memory. The primary task of
1062 * this function is to relocate the dynamic linker.
1065 init_rtld(caddr_t mapbase
)
1067 Obj_Entry objtmp
; /* Temporary rtld object */
1070 * Conjure up an Obj_Entry structure for the dynamic linker.
1072 * The "path" member can't be initialized yet because string constatns
1073 * cannot yet be acessed. Below we will set it correctly.
1075 memset(&objtmp
, 0, sizeof(objtmp
));
1078 objtmp
.mapbase
= mapbase
;
1080 objtmp
.relocbase
= mapbase
;
1082 if (RTLD_IS_DYNAMIC()) {
1083 objtmp
.dynamic
= rtld_dynamic(&objtmp
);
1084 digest_dynamic(&objtmp
, 1);
1085 assert(objtmp
.needed
== NULL
);
1086 assert(!objtmp
.textrel
);
1089 * Temporarily put the dynamic linker entry into the object list, so
1090 * that symbols can be found.
1093 relocate_objects(&objtmp
, true, &objtmp
);
1096 /* Initialize the object list. */
1097 obj_tail
= &obj_list
;
1099 /* Now that non-local variables can be accesses, copy out obj_rtld. */
1100 memcpy(&obj_rtld
, &objtmp
, sizeof(obj_rtld
));
1102 /* Replace the path with a dynamically allocated copy. */
1103 obj_rtld
.path
= xstrdup(PATH_RTLD
);
1105 r_debug
.r_brk
= r_debug_state
;
1106 r_debug
.r_state
= RT_CONSISTENT
;
1110 * Add the init functions from a needed object list (and its recursive
1111 * needed objects) to "list". This is not used directly; it is a helper
1112 * function for initlist_add_objects(). The write lock must be held
1113 * when this function is called.
1116 initlist_add_neededs(Needed_Entry
*needed
, Objlist
*list
)
1118 /* Recursively process the successor needed objects. */
1119 if (needed
->next
!= NULL
)
1120 initlist_add_neededs(needed
->next
, list
);
1122 /* Process the current needed object. */
1123 if (needed
->obj
!= NULL
)
1124 initlist_add_objects(needed
->obj
, &needed
->obj
->next
, list
);
1128 * Scan all of the DAGs rooted in the range of objects from "obj" to
1129 * "tail" and add their init functions to "list". This recurses over
1130 * the DAGs and ensure the proper init ordering such that each object's
1131 * needed libraries are initialized before the object itself. At the
1132 * same time, this function adds the objects to the global finalization
1133 * list "list_fini" in the opposite order. The write lock must be
1134 * held when this function is called.
1137 initlist_add_objects(Obj_Entry
*obj
, Obj_Entry
**tail
, Objlist
*list
)
1141 obj
->init_done
= true;
1143 /* Recursively process the successor objects. */
1144 if (&obj
->next
!= tail
)
1145 initlist_add_objects(obj
->next
, tail
, list
);
1147 /* Recursively process the needed objects. */
1148 if (obj
->needed
!= NULL
)
1149 initlist_add_neededs(obj
->needed
, list
);
1151 /* Add the object to the init list. */
1152 if (obj
->init
!= (Elf_Addr
)NULL
)
1153 objlist_push_tail(list
, obj
);
1155 /* Add the object to the global fini list in the reverse order. */
1156 if (obj
->fini
!= (Elf_Addr
)NULL
)
1157 objlist_push_head(&list_fini
, obj
);
1161 #define FPTR_TARGET(f) ((Elf_Addr) (f))
1165 is_exported(const Elf_Sym
*def
)
1168 const func_ptr_type
*p
;
1170 value
= (Elf_Addr
)(obj_rtld
.relocbase
+ def
->st_value
);
1171 for (p
= exports
; *p
!= NULL
; p
++)
1172 if (FPTR_TARGET(*p
) == value
)
1178 * Given a shared object, traverse its list of needed objects, and load
1179 * each of them. Returns 0 on success. Generates an error message and
1180 * returns -1 on failure.
1183 load_needed_objects(Obj_Entry
*first
)
1187 for (obj
= first
; obj
!= NULL
; obj
= obj
->next
) {
1188 Needed_Entry
*needed
;
1190 for (needed
= obj
->needed
; needed
!= NULL
; needed
= needed
->next
) {
1191 const char *name
= obj
->strtab
+ needed
->name
;
1192 char *path
= find_library(name
, obj
);
1195 if (path
== NULL
&& !ld_tracing
)
1199 needed
->obj
= load_object(path
);
1200 if (needed
->obj
== NULL
&& !ld_tracing
)
1201 return -1; /* XXX - cleanup */
1210 load_preload_objects(void)
1212 char *p
= ld_preload
;
1213 static const char delim
[] = " \t:;";
1218 p
+= strspn(p
, delim
);
1219 while (*p
!= '\0') {
1220 size_t len
= strcspn(p
, delim
);
1226 if ((path
= find_library(p
, NULL
)) == NULL
)
1228 if (load_object(path
) == NULL
)
1229 return -1; /* XXX - cleanup */
1232 p
+= strspn(p
, delim
);
1238 * Load a shared object into memory, if it is not already loaded. The
1239 * argument must be a string allocated on the heap. This function assumes
1240 * responsibility for freeing it when necessary.
1242 * Returns a pointer to the Obj_Entry for the object. Returns NULL
1246 load_object(char *path
)
1252 for (obj
= obj_list
->next
; obj
!= NULL
; obj
= obj
->next
)
1253 if (strcmp(obj
->path
, path
) == 0)
1257 * If we didn't find a match by pathname, open the file and check
1258 * again by device and inode. This avoids false mismatches caused
1259 * by multiple links or ".." in pathnames.
1261 * To avoid a race, we open the file and use fstat() rather than
1265 if ((fd
= open(path
, O_RDONLY
)) == -1) {
1266 _rtld_error("Cannot open \"%s\"", path
);
1269 if (fstat(fd
, &sb
) == -1) {
1270 _rtld_error("Cannot fstat \"%s\"", path
);
1274 for (obj
= obj_list
->next
; obj
!= NULL
; obj
= obj
->next
) {
1275 if (obj
->ino
== sb
.st_ino
&& obj
->dev
== sb
.st_dev
) {
1282 if (obj
== NULL
) { /* First use of this object, so we must map it in */
1283 dbg("loading \"%s\"", path
);
1284 obj
= map_object(fd
, path
, &sb
);
1292 digest_dynamic(obj
, 0);
1295 obj_tail
= &obj
->next
;
1297 linkmap_add(obj
); /* for GDB & dlinfo() */
1299 dbg(" %p .. %p: %s", obj
->mapbase
,
1300 obj
->mapbase
+ obj
->mapsize
- 1, obj
->path
);
1302 dbg(" WARNING: %s has impure text", obj
->path
);
1310 obj_from_addr(const void *addr
)
1314 for (obj
= obj_list
; obj
!= NULL
; obj
= obj
->next
) {
1315 if (addr
< (void *) obj
->mapbase
)
1317 if (addr
< (void *) (obj
->mapbase
+ obj
->mapsize
))
1324 * Call the finalization functions for each of the objects in "list"
1325 * which are unreferenced. All of the objects are expected to have
1326 * non-NULL fini functions.
1329 objlist_call_fini(Objlist
*list
)
1335 * Preserve the current error message since a fini function might
1336 * call into the dynamic linker and overwrite it.
1338 saved_msg
= errmsg_save();
1339 STAILQ_FOREACH(elm
, list
, link
) {
1340 if (elm
->obj
->refcount
== 0) {
1341 dbg("calling fini function for %s at %p", elm
->obj
->path
,
1342 (void *)elm
->obj
->fini
);
1343 call_initfini_pointer(elm
->obj
, elm
->obj
->fini
);
1346 errmsg_restore(saved_msg
);
1350 * Call the initialization functions for each of the objects in
1351 * "list". All of the objects are expected to have non-NULL init
1355 objlist_call_init(Objlist
*list
)
1361 * Preserve the current error message since an init function might
1362 * call into the dynamic linker and overwrite it.
1364 saved_msg
= errmsg_save();
1365 STAILQ_FOREACH(elm
, list
, link
) {
1366 dbg("calling init function for %s at %p", elm
->obj
->path
,
1367 (void *)elm
->obj
->init
);
1368 call_initfini_pointer(elm
->obj
, elm
->obj
->init
);
1370 errmsg_restore(saved_msg
);
1374 objlist_clear(Objlist
*list
)
1378 while (!STAILQ_EMPTY(list
)) {
1379 elm
= STAILQ_FIRST(list
);
1380 STAILQ_REMOVE_HEAD(list
, link
);
1385 static Objlist_Entry
*
1386 objlist_find(Objlist
*list
, const Obj_Entry
*obj
)
1390 STAILQ_FOREACH(elm
, list
, link
)
1391 if (elm
->obj
== obj
)
1397 objlist_init(Objlist
*list
)
1403 objlist_push_head(Objlist
*list
, Obj_Entry
*obj
)
1407 elm
= NEW(Objlist_Entry
);
1409 STAILQ_INSERT_HEAD(list
, elm
, link
);
1413 objlist_push_tail(Objlist
*list
, Obj_Entry
*obj
)
1417 elm
= NEW(Objlist_Entry
);
1419 STAILQ_INSERT_TAIL(list
, elm
, link
);
1423 objlist_remove(Objlist
*list
, Obj_Entry
*obj
)
1427 if ((elm
= objlist_find(list
, obj
)) != NULL
) {
1428 STAILQ_REMOVE(list
, elm
, Struct_Objlist_Entry
, link
);
1434 * Remove all of the unreferenced objects from "list".
1437 objlist_remove_unref(Objlist
*list
)
1442 STAILQ_INIT(&newlist
);
1443 while (!STAILQ_EMPTY(list
)) {
1444 elm
= STAILQ_FIRST(list
);
1445 STAILQ_REMOVE_HEAD(list
, link
);
1446 if (elm
->obj
->refcount
== 0)
1449 STAILQ_INSERT_TAIL(&newlist
, elm
, link
);
1457 * Relocate newly-loaded shared objects. The argument is a pointer to
1458 * the Obj_Entry for the first such object. All objects from the first
1459 * to the end of the list of objects are relocated. Returns 0 on success,
1463 relocate_objects(Obj_Entry
*first
, bool bind_now
, Obj_Entry
*rtldobj
)
1467 for (obj
= first
; obj
!= NULL
; obj
= obj
->next
) {
1469 dbg("relocating \"%s\"", obj
->path
);
1470 if (obj
->nbuckets
== 0 || obj
->nchains
== 0 || obj
->buckets
== NULL
||
1471 obj
->symtab
== NULL
|| obj
->strtab
== NULL
) {
1472 _rtld_error("%s: Shared object has no run-time symbol table",
1478 /* There are relocations to the write-protected text segment. */
1479 if (mprotect(obj
->mapbase
, obj
->textsize
,
1480 PROT_READ
|PROT_WRITE
|PROT_EXEC
) == -1) {
1481 _rtld_error("%s: Cannot write-enable text segment: %s",
1482 obj
->path
, strerror(errno
));
1487 /* Process the non-PLT relocations. */
1488 if (reloc_non_plt(obj
, rtldobj
))
1491 if (obj
->textrel
) { /* Re-protected the text segment. */
1492 if (mprotect(obj
->mapbase
, obj
->textsize
,
1493 PROT_READ
|PROT_EXEC
) == -1) {
1494 _rtld_error("%s: Cannot write-protect text segment: %s",
1495 obj
->path
, strerror(errno
));
1500 /* Process the PLT relocations. */
1501 if (reloc_plt(obj
) == -1)
1503 /* Relocate the jump slots if we are doing immediate binding. */
1504 if (obj
->bind_now
|| bind_now
)
1505 if (reloc_jmpslots(obj
) == -1)
1510 * Set up the magic number and version in the Obj_Entry. These
1511 * were checked in the crt1.o from the original ElfKit, so we
1512 * set them for backward compatibility.
1514 obj
->magic
= RTLD_MAGIC
;
1515 obj
->version
= RTLD_VERSION
;
1517 /* Set the special PLT or GOT entries. */
1527 * Cleanup procedure. It will be called (by the atexit mechanism) just
1528 * before the process exits.
1536 /* Clear all the reference counts so the fini functions will be called. */
1537 for (obj
= obj_list
; obj
!= NULL
; obj
= obj
->next
)
1539 objlist_call_fini(&list_fini
);
1540 /* No need to remove the items from the list, since we are exiting. */
1541 if (!libmap_disable
)
1546 path_enumerate(const char *path
, path_enum_proc callback
, void *arg
)
1554 path
+= strspn(path
, ":;");
1555 while (*path
!= '\0') {
1559 len
= strcspn(path
, ":;");
1561 trans
= lm_findn(NULL
, path
, len
);
1563 res
= callback(trans
, strlen(trans
), arg
);
1566 res
= callback(path
, len
, arg
);
1572 path
+= strspn(path
, ":;");
1578 struct try_library_args
{
1586 try_library_path(const char *dir
, size_t dirlen
, void *param
)
1588 struct try_library_args
*arg
;
1591 if (*dir
== '/' || trust
) {
1594 if (dirlen
+ 1 + arg
->namelen
+ 1 > arg
->buflen
)
1597 pathname
= arg
->buffer
;
1598 strncpy(pathname
, dir
, dirlen
);
1599 pathname
[dirlen
] = '/';
1600 strcpy(pathname
+ dirlen
+ 1, arg
->name
);
1602 dbg(" Trying \"%s\"", pathname
);
1603 if (access(pathname
, F_OK
) == 0) { /* We found it */
1604 pathname
= xmalloc(dirlen
+ 1 + arg
->namelen
+ 1);
1605 strcpy(pathname
, arg
->buffer
);
1613 search_library_path(const char *name
, const char *path
)
1616 struct try_library_args arg
;
1622 arg
.namelen
= strlen(name
);
1623 arg
.buffer
= xmalloc(PATH_MAX
);
1624 arg
.buflen
= PATH_MAX
;
1626 p
= path_enumerate(path
, try_library_path
, &arg
);
1634 dlclose(void *handle
)
1639 lockstate
= wlock_acquire(rtld_bind_lock
);
1640 root
= dlcheck(handle
);
1642 wlock_release(rtld_bind_lock
, lockstate
);
1646 /* Unreference the object and its dependencies. */
1647 root
->dl_refcount
--;
1651 if (root
->refcount
== 0) {
1653 * The object is no longer referenced, so we must unload it.
1654 * First, call the fini functions with no locks held.
1656 wlock_release(rtld_bind_lock
, lockstate
);
1657 objlist_call_fini(&list_fini
);
1658 lockstate
= wlock_acquire(rtld_bind_lock
);
1659 objlist_remove_unref(&list_fini
);
1661 /* Finish cleaning up the newly-unreferenced objects. */
1662 GDB_STATE(RT_DELETE
,&root
->linkmap
);
1663 unload_object(root
);
1664 GDB_STATE(RT_CONSISTENT
,NULL
);
1666 wlock_release(rtld_bind_lock
, lockstate
);
1673 char *msg
= error_message
;
1674 error_message
= NULL
;
1679 * This function is deprecated and has no effect.
1682 dllockinit(void *context
,
1683 void *(*lock_create
)(void *context
),
1684 void (*rlock_acquire
)(void *lock
),
1685 void (*wlock_acquire
)(void *lock
),
1686 void (*lock_release
)(void *lock
),
1687 void (*lock_destroy
)(void *lock
),
1688 void (*context_destroy
)(void *context
))
1690 static void *cur_context
;
1691 static void (*cur_context_destroy
)(void *);
1693 /* Just destroy the context from the previous call, if necessary. */
1694 if (cur_context_destroy
!= NULL
)
1695 cur_context_destroy(cur_context
);
1696 cur_context
= context
;
1697 cur_context_destroy
= context_destroy
;
1701 dlopen(const char *name
, int mode
)
1703 Obj_Entry
**old_obj_tail
;
1706 int result
, lockstate
;
1708 ld_tracing
= (mode
& RTLD_TRACE
) == 0 ? NULL
: "1";
1709 if (ld_tracing
!= NULL
)
1710 environ
= (char **)*get_program_var_addr("environ");
1712 objlist_init(&initlist
);
1714 lockstate
= wlock_acquire(rtld_bind_lock
);
1715 GDB_STATE(RT_ADD
,NULL
);
1717 old_obj_tail
= obj_tail
;
1723 char *path
= find_library(name
, obj_main
);
1725 obj
= load_object(path
);
1730 if (mode
& RTLD_GLOBAL
&& objlist_find(&list_global
, obj
) == NULL
)
1731 objlist_push_tail(&list_global
, obj
);
1732 mode
&= RTLD_MODEMASK
;
1733 if (*old_obj_tail
!= NULL
) { /* We loaded something new. */
1734 assert(*old_obj_tail
== obj
);
1736 result
= load_needed_objects(obj
);
1737 if (result
!= -1 && ld_tracing
)
1741 (init_dag(obj
), relocate_objects(obj
, mode
== RTLD_NOW
,
1742 &obj_rtld
)) == -1) {
1745 if (obj
->refcount
== 0)
1749 /* Make list of init functions to call. */
1750 initlist_add_objects(obj
, &obj
->next
, &initlist
);
1754 /* Bump the reference counts for objects on this DAG. */
1762 GDB_STATE(RT_CONSISTENT
,obj
? &obj
->linkmap
: NULL
);
1764 /* Call the init functions with no locks held. */
1765 wlock_release(rtld_bind_lock
, lockstate
);
1766 objlist_call_init(&initlist
);
1767 lockstate
= wlock_acquire(rtld_bind_lock
);
1768 objlist_clear(&initlist
);
1769 wlock_release(rtld_bind_lock
, lockstate
);
1772 trace_loaded_objects(obj
);
1773 wlock_release(rtld_bind_lock
, lockstate
);
1778 dlsym(void *handle
, const char *name
)
1780 const Obj_Entry
*obj
;
1783 const Obj_Entry
*defobj
;
1786 hash
= elf_hash(name
);
1790 lockstate
= rlock_acquire(rtld_bind_lock
);
1791 if (handle
== NULL
|| handle
== RTLD_NEXT
||
1792 handle
== RTLD_DEFAULT
|| handle
== RTLD_SELF
) {
1795 retaddr
= __builtin_return_address(0); /* __GNUC__ only */
1796 if ((obj
= obj_from_addr(retaddr
)) == NULL
) {
1797 _rtld_error("Cannot determine caller's shared object");
1798 rlock_release(rtld_bind_lock
, lockstate
);
1801 if (handle
== NULL
) { /* Just the caller's shared object. */
1802 def
= symlook_obj(name
, hash
, obj
, true);
1804 } else if (handle
== RTLD_NEXT
|| /* Objects after caller's */
1805 handle
== RTLD_SELF
) { /* ... caller included */
1806 if (handle
== RTLD_NEXT
)
1808 for (; obj
!= NULL
; obj
= obj
->next
) {
1809 if ((def
= symlook_obj(name
, hash
, obj
, true)) != NULL
) {
1815 assert(handle
== RTLD_DEFAULT
);
1816 def
= symlook_default(name
, hash
, obj
, &defobj
, true);
1819 if ((obj
= dlcheck(handle
)) == NULL
) {
1820 rlock_release(rtld_bind_lock
, lockstate
);
1824 if (obj
->mainprog
) {
1827 /* Search main program and all libraries loaded by it. */
1828 donelist_init(&donelist
);
1829 def
= symlook_list(name
, hash
, &list_main
, &defobj
, true,
1833 * XXX - This isn't correct. The search should include the whole
1834 * DAG rooted at the given object.
1836 def
= symlook_obj(name
, hash
, obj
, true);
1842 rlock_release(rtld_bind_lock
, lockstate
);
1845 * The value required by the caller is derived from the value
1846 * of the symbol. For the ia64 architecture, we need to
1847 * construct a function descriptor which the caller can use to
1848 * call the function with the right 'gp' value. For other
1849 * architectures and for non-functions, the value is simply
1850 * the relocated value of the symbol.
1852 if (ELF_ST_TYPE(def
->st_info
) == STT_FUNC
)
1853 return make_function_pointer(def
, defobj
);
1855 return defobj
->relocbase
+ def
->st_value
;
1858 _rtld_error("Undefined symbol \"%s\"", name
);
1859 rlock_release(rtld_bind_lock
, lockstate
);
1864 dladdr(const void *addr
, Dl_info
*info
)
1866 const Obj_Entry
*obj
;
1869 unsigned long symoffset
;
1872 lockstate
= rlock_acquire(rtld_bind_lock
);
1873 obj
= obj_from_addr(addr
);
1875 _rtld_error("No shared object contains address");
1876 rlock_release(rtld_bind_lock
, lockstate
);
1879 info
->dli_fname
= obj
->path
;
1880 info
->dli_fbase
= obj
->mapbase
;
1881 info
->dli_saddr
= (void *)0;
1882 info
->dli_sname
= NULL
;
1885 * Walk the symbol list looking for the symbol whose address is
1886 * closest to the address sent in.
1888 for (symoffset
= 0; symoffset
< obj
->nchains
; symoffset
++) {
1889 def
= obj
->symtab
+ symoffset
;
1892 * For skip the symbol if st_shndx is either SHN_UNDEF or
1895 if (def
->st_shndx
== SHN_UNDEF
|| def
->st_shndx
== SHN_COMMON
)
1899 * If the symbol is greater than the specified address, or if it
1900 * is further away from addr than the current nearest symbol,
1903 symbol_addr
= obj
->relocbase
+ def
->st_value
;
1904 if (symbol_addr
> addr
|| symbol_addr
< info
->dli_saddr
)
1907 /* Update our idea of the nearest symbol. */
1908 info
->dli_sname
= obj
->strtab
+ def
->st_name
;
1909 info
->dli_saddr
= symbol_addr
;
1912 if (info
->dli_saddr
== addr
)
1915 rlock_release(rtld_bind_lock
, lockstate
);
1920 dlinfo(void *handle
, int request
, void *p
)
1922 const Obj_Entry
*obj
;
1923 int error
, lockstate
;
1925 lockstate
= rlock_acquire(rtld_bind_lock
);
1927 if (handle
== NULL
|| handle
== RTLD_SELF
) {
1930 retaddr
= __builtin_return_address(0); /* __GNUC__ only */
1931 if ((obj
= obj_from_addr(retaddr
)) == NULL
)
1932 _rtld_error("Cannot determine caller's shared object");
1934 obj
= dlcheck(handle
);
1937 rlock_release(rtld_bind_lock
, lockstate
);
1943 case RTLD_DI_LINKMAP
:
1944 *((struct link_map
const **)p
) = &obj
->linkmap
;
1946 case RTLD_DI_ORIGIN
:
1947 error
= rtld_dirname(obj
->path
, p
);
1950 case RTLD_DI_SERINFOSIZE
:
1951 case RTLD_DI_SERINFO
:
1952 error
= do_search_info(obj
, request
, (struct dl_serinfo
*)p
);
1956 _rtld_error("Invalid request %d passed to dlinfo()", request
);
1960 rlock_release(rtld_bind_lock
, lockstate
);
1965 struct fill_search_info_args
{
1968 Dl_serinfo
*serinfo
;
1969 Dl_serpath
*serpath
;
1974 fill_search_info(const char *dir
, size_t dirlen
, void *param
)
1976 struct fill_search_info_args
*arg
;
1980 if (arg
->request
== RTLD_DI_SERINFOSIZE
) {
1981 arg
->serinfo
->dls_cnt
++;
1982 arg
->serinfo
->dls_size
+= dirlen
+ 1;
1984 struct dl_serpath
*s_entry
;
1986 s_entry
= arg
->serpath
;
1987 s_entry
->dls_name
= arg
->strspace
;
1988 s_entry
->dls_flags
= arg
->flags
;
1990 strncpy(arg
->strspace
, dir
, dirlen
);
1991 arg
->strspace
[dirlen
] = '\0';
1993 arg
->strspace
+= dirlen
+ 1;
2001 do_search_info(const Obj_Entry
*obj
, int request
, struct dl_serinfo
*info
)
2003 struct dl_serinfo _info
;
2004 struct fill_search_info_args args
;
2006 args
.request
= RTLD_DI_SERINFOSIZE
;
2007 args
.serinfo
= &_info
;
2009 _info
.dls_size
= __offsetof(struct dl_serinfo
, dls_serpath
);
2012 path_enumerate(ld_library_path
, fill_search_info
, &args
);
2013 path_enumerate(obj
->rpath
, fill_search_info
, &args
);
2014 path_enumerate(gethints(), fill_search_info
, &args
);
2015 path_enumerate(STANDARD_LIBRARY_PATH
, fill_search_info
, &args
);
2018 if (request
== RTLD_DI_SERINFOSIZE
) {
2019 info
->dls_size
= _info
.dls_size
;
2020 info
->dls_cnt
= _info
.dls_cnt
;
2024 if (info
->dls_cnt
!= _info
.dls_cnt
|| info
->dls_size
!= _info
.dls_size
) {
2025 _rtld_error("Uninitialized Dl_serinfo struct passed to dlinfo()");
2029 args
.request
= RTLD_DI_SERINFO
;
2030 args
.serinfo
= info
;
2031 args
.serpath
= &info
->dls_serpath
[0];
2032 args
.strspace
= (char *)&info
->dls_serpath
[_info
.dls_cnt
];
2034 args
.flags
= LA_SER_LIBPATH
;
2035 if (path_enumerate(ld_library_path
, fill_search_info
, &args
) != NULL
)
2038 args
.flags
= LA_SER_RUNPATH
;
2039 if (path_enumerate(obj
->rpath
, fill_search_info
, &args
) != NULL
)
2042 args
.flags
= LA_SER_CONFIG
;
2043 if (path_enumerate(gethints(), fill_search_info
, &args
) != NULL
)
2046 args
.flags
= LA_SER_DEFAULT
;
2047 if (path_enumerate(STANDARD_LIBRARY_PATH
, fill_search_info
, &args
) != NULL
)
2053 rtld_dirname(const char *path
, char *bname
)
2057 /* Empty or NULL string gets treated as "." */
2058 if (path
== NULL
|| *path
== '\0') {
2064 /* Strip trailing slashes */
2065 endp
= path
+ strlen(path
) - 1;
2066 while (endp
> path
&& *endp
== '/')
2069 /* Find the start of the dir */
2070 while (endp
> path
&& *endp
!= '/')
2073 /* Either the dir is "/" or there are no slashes */
2075 bname
[0] = *endp
== '/' ? '/' : '.';
2081 } while (endp
> path
&& *endp
== '/');
2084 if (endp
- path
+ 2 > PATH_MAX
)
2086 _rtld_error("Filename is too long: %s", path
);
2090 strncpy(bname
, path
, endp
- path
+ 1);
2091 bname
[endp
- path
+ 1] = '\0';
2096 linkmap_add(Obj_Entry
*obj
)
2098 struct link_map
*l
= &obj
->linkmap
;
2099 struct link_map
*prev
;
2101 obj
->linkmap
.l_name
= obj
->path
;
2102 obj
->linkmap
.l_addr
= obj
->mapbase
;
2103 obj
->linkmap
.l_ld
= obj
->dynamic
;
2105 /* GDB needs load offset on MIPS to use the symbols */
2106 obj
->linkmap
.l_offs
= obj
->relocbase
;
2109 if (r_debug
.r_map
== NULL
) {
2115 * Scan to the end of the list, but not past the entry for the
2116 * dynamic linker, which we want to keep at the very end.
2118 for (prev
= r_debug
.r_map
;
2119 prev
->l_next
!= NULL
&& prev
->l_next
!= &obj_rtld
.linkmap
;
2120 prev
= prev
->l_next
)
2123 /* Link in the new entry. */
2125 l
->l_next
= prev
->l_next
;
2126 if (l
->l_next
!= NULL
)
2127 l
->l_next
->l_prev
= l
;
2132 linkmap_delete(Obj_Entry
*obj
)
2134 struct link_map
*l
= &obj
->linkmap
;
2136 if (l
->l_prev
== NULL
) {
2137 if ((r_debug
.r_map
= l
->l_next
) != NULL
)
2138 l
->l_next
->l_prev
= NULL
;
2142 if ((l
->l_prev
->l_next
= l
->l_next
) != NULL
)
2143 l
->l_next
->l_prev
= l
->l_prev
;
2147 * Function for the debugger to set a breakpoint on to gain control.
2149 * The two parameters allow the debugger to easily find and determine
2150 * what the runtime loader is doing and to whom it is doing it.
2152 * When the loadhook trap is hit (r_debug_state, set at program
2153 * initialization), the arguments can be found on the stack:
2155 * +8 struct link_map *m
2156 * +4 struct r_debug *rd
2160 r_debug_state(struct r_debug
* rd
, struct link_map
*m
)
2165 * Get address of the pointer variable in the main program.
2167 static const void **
2168 get_program_var_addr(const char *name
)
2170 const Obj_Entry
*obj
;
2173 hash
= elf_hash(name
);
2174 for (obj
= obj_main
; obj
!= NULL
; obj
= obj
->next
) {
2177 if ((def
= symlook_obj(name
, hash
, obj
, false)) != NULL
) {
2180 addr
= (const void **)(obj
->relocbase
+ def
->st_value
);
2188 * Set a pointer variable in the main program to the given value. This
2189 * is used to set key variables such as "environ" before any of the
2190 * init functions are called.
2193 set_program_var(const char *name
, const void *value
)
2197 if ((addr
= get_program_var_addr(name
)) != NULL
) {
2198 dbg("\"%s\": *%p <-- %p", name
, addr
, value
);
2204 * Given a symbol name in a referencing object, find the corresponding
2205 * definition of the symbol. Returns a pointer to the symbol, or NULL if
2206 * no definition was found. Returns a pointer to the Obj_Entry of the
2207 * defining object via the reference parameter DEFOBJ_OUT.
2209 static const Elf_Sym
*
2210 symlook_default(const char *name
, unsigned long hash
,
2211 const Obj_Entry
*refobj
, const Obj_Entry
**defobj_out
, bool in_plt
)
2215 const Elf_Sym
*symp
;
2216 const Obj_Entry
*obj
;
2217 const Obj_Entry
*defobj
;
2218 const Objlist_Entry
*elm
;
2221 donelist_init(&donelist
);
2223 /* Look first in the referencing object if linked symbolically. */
2224 if (refobj
->symbolic
&& !donelist_check(&donelist
, refobj
)) {
2225 symp
= symlook_obj(name
, hash
, refobj
, in_plt
);
2232 /* Search all objects loaded at program start up. */
2233 if (def
== NULL
|| ELF_ST_BIND(def
->st_info
) == STB_WEAK
) {
2234 symp
= symlook_list(name
, hash
, &list_main
, &obj
, in_plt
, &donelist
);
2236 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
2242 /* Search all DAGs whose roots are RTLD_GLOBAL objects. */
2243 STAILQ_FOREACH(elm
, &list_global
, link
) {
2244 if (def
!= NULL
&& ELF_ST_BIND(def
->st_info
) != STB_WEAK
)
2246 symp
= symlook_list(name
, hash
, &elm
->obj
->dagmembers
, &obj
, in_plt
,
2249 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
2255 /* Search all dlopened DAGs containing the referencing object. */
2256 STAILQ_FOREACH(elm
, &refobj
->dldags
, link
) {
2257 if (def
!= NULL
&& ELF_ST_BIND(def
->st_info
) != STB_WEAK
)
2259 symp
= symlook_list(name
, hash
, &elm
->obj
->dagmembers
, &obj
, in_plt
,
2262 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
2269 * Search the dynamic linker itself, and possibly resolve the
2270 * symbol from there. This is how the application links to
2271 * dynamic linker services such as dlopen. Only the values listed
2272 * in the "exports" array can be resolved from the dynamic linker.
2274 if (def
== NULL
|| ELF_ST_BIND(def
->st_info
) == STB_WEAK
) {
2275 symp
= symlook_obj(name
, hash
, &obj_rtld
, in_plt
);
2276 if (symp
!= NULL
&& is_exported(symp
)) {
2283 *defobj_out
= defobj
;
2287 static const Elf_Sym
*
2288 symlook_list(const char *name
, unsigned long hash
, Objlist
*objlist
,
2289 const Obj_Entry
**defobj_out
, bool in_plt
, DoneList
*dlp
)
2291 const Elf_Sym
*symp
;
2293 const Obj_Entry
*defobj
;
2294 const Objlist_Entry
*elm
;
2298 STAILQ_FOREACH(elm
, objlist
, link
) {
2299 if (donelist_check(dlp
, elm
->obj
))
2301 if ((symp
= symlook_obj(name
, hash
, elm
->obj
, in_plt
)) != NULL
) {
2302 if (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
) {
2305 if (ELF_ST_BIND(def
->st_info
) != STB_WEAK
)
2311 *defobj_out
= defobj
;
2318 * Search the symbol table of a single shared object for a symbol of
2319 * the given name. Returns a pointer to the symbol, or NULL if no
2320 * definition was found.
2322 * The symbol's hash value is passed in for efficiency reasons; that
2323 * eliminates many recomputations of the hash value.
2326 symlook_obj(const char *name
, unsigned long hash
, const Obj_Entry
*obj
,
2329 if (obj
->buckets
!= NULL
) {
2330 unsigned long symnum
= obj
->buckets
[hash
% obj
->nbuckets
];
2332 while (symnum
!= STN_UNDEF
) {
2333 const Elf_Sym
*symp
;
2336 if (symnum
>= obj
->nchains
)
2337 return NULL
; /* Bad object */
2338 symp
= obj
->symtab
+ symnum
;
2339 strp
= obj
->strtab
+ symp
->st_name
;
2341 if (name
[0] == strp
[0] && strcmp(name
, strp
) == 0)
2342 return symp
->st_shndx
!= SHN_UNDEF
||
2343 (!in_plt
&& symp
->st_value
!= 0 &&
2344 ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
) ? symp
: NULL
;
2346 symnum
= obj
->chains
[symnum
];
2355 trace_loaded_objects(Obj_Entry
*obj
)
2357 char *fmt1
, *fmt2
, *fmt
, *main_local
, *list_containers
;
2360 if ((main_local
= getenv(LD_
"TRACE_LOADED_OBJECTS_PROGNAME")) == NULL
)
2363 if ((fmt1
= getenv(LD_
"TRACE_LOADED_OBJECTS_FMT1")) == NULL
)
2364 fmt1
= "\t%o => %p (%x)\n";
2366 if ((fmt2
= getenv(LD_
"TRACE_LOADED_OBJECTS_FMT2")) == NULL
)
2367 fmt2
= "\t%o (%x)\n";
2369 list_containers
= getenv(LD_
"TRACE_LOADED_OBJECTS_ALL");
2371 for (; obj
; obj
= obj
->next
) {
2372 Needed_Entry
*needed
;
2376 if (list_containers
&& obj
->needed
!= NULL
)
2377 printf("%s:\n", obj
->path
);
2378 for (needed
= obj
->needed
; needed
; needed
= needed
->next
) {
2379 if (needed
->obj
!= NULL
) {
2380 if (needed
->obj
->traced
&& !list_containers
)
2382 needed
->obj
->traced
= true;
2383 path
= needed
->obj
->path
;
2387 name
= (char *)obj
->strtab
+ needed
->name
;
2388 is_lib
= strncmp(name
, "lib", 3) == 0; /* XXX - bogus */
2390 fmt
= is_lib
? fmt1
: fmt2
;
2391 while ((c
= *fmt
++) != '\0') {
2417 printf("%s", main_local
);
2420 printf("%s", obj_main
->path
);
2427 printf("%d", sodp
->sod_major
);
2430 printf("%d", sodp
->sod_minor
);
2437 printf("%p", needed
->obj
? needed
->obj
->mapbase
: 0);
2449 * Unload a dlopened object and its dependencies from memory and from
2450 * our data structures. It is assumed that the DAG rooted in the
2451 * object has already been unreferenced, and that the object has a
2452 * reference count of 0.
2455 unload_object(Obj_Entry
*root
)
2460 assert(root
->refcount
== 0);
2463 * Pass over the DAG removing unreferenced objects from
2464 * appropriate lists.
2466 unlink_object(root
);
2468 /* Unmap all objects that are no longer referenced. */
2469 linkp
= &obj_list
->next
;
2470 while ((obj
= *linkp
) != NULL
) {
2471 if (obj
->refcount
== 0) {
2472 dbg("unloading \"%s\"", obj
->path
);
2473 munmap(obj
->mapbase
, obj
->mapsize
);
2474 linkmap_delete(obj
);
2485 unlink_object(Obj_Entry
*root
)
2489 if (root
->refcount
== 0) {
2490 /* Remove the object from the RTLD_GLOBAL list. */
2491 objlist_remove(&list_global
, root
);
2493 /* Remove the object from all objects' DAG lists. */
2494 STAILQ_FOREACH(elm
, &root
->dagmembers
, link
) {
2495 objlist_remove(&elm
->obj
->dldags
, root
);
2496 if (elm
->obj
!= root
)
2497 unlink_object(elm
->obj
);
2503 ref_dag(Obj_Entry
*root
)
2507 STAILQ_FOREACH(elm
, &root
->dagmembers
, link
)
2508 elm
->obj
->refcount
++;
2512 unref_dag(Obj_Entry
*root
)
2516 STAILQ_FOREACH(elm
, &root
->dagmembers
, link
)
2517 elm
->obj
->refcount
--;
2521 * Common code for MD __tls_get_addr().
2524 tls_get_addr_common(Elf_Addr
** dtvp
, int index
, size_t offset
)
2526 Elf_Addr
* dtv
= *dtvp
;
2528 /* Check dtv generation in case new modules have arrived */
2529 if (dtv
[0] != tls_dtv_generation
) {
2533 newdtv
= calloc(1, (tls_max_index
+ 2) * sizeof(Elf_Addr
));
2535 if (to_copy
> tls_max_index
)
2536 to_copy
= tls_max_index
;
2537 memcpy(&newdtv
[2], &dtv
[2], to_copy
* sizeof(Elf_Addr
));
2538 newdtv
[0] = tls_dtv_generation
;
2539 newdtv
[1] = tls_max_index
;
2544 /* Dynamically allocate module TLS if necessary */
2545 if (!dtv
[index
+ 1])
2546 dtv
[index
+ 1] = (Elf_Addr
)allocate_module_tls(index
);
2548 return (void*) (dtv
[index
+ 1] + offset
);
2551 /* XXX not sure what variants to use for arm. */
2553 #if defined(__ia64__) || defined(__alpha__) || defined(__powerpc__)
2556 * Allocate Static TLS using the Variant I method.
2559 allocate_tls(Obj_Entry
*objs
, void *oldtls
, size_t tcbsize
, size_t tcbalign
)
2564 Elf_Addr
*dtv
, *olddtv
;
2568 size
= tls_static_space
;
2571 dtv
= malloc((tls_max_index
+ 2) * sizeof(Elf_Addr
));
2573 *(Elf_Addr
**) tls
= dtv
;
2575 dtv
[0] = tls_dtv_generation
;
2576 dtv
[1] = tls_max_index
;
2580 * Copy the static TLS block over whole.
2582 memcpy(tls
+ tcbsize
, oldtls
+ tcbsize
, tls_static_space
- tcbsize
);
2585 * If any dynamic TLS blocks have been created tls_get_addr(),
2588 olddtv
= *(Elf_Addr
**) oldtls
;
2589 for (i
= 0; i
< olddtv
[1]; i
++) {
2590 if (olddtv
[i
+2] < (Elf_Addr
)oldtls
||
2591 olddtv
[i
+2] > (Elf_Addr
)oldtls
+ tls_static_space
) {
2592 dtv
[i
+2] = olddtv
[i
+2];
2598 * We assume that all tls blocks are allocated with the same
2599 * size and alignment.
2601 free_tls(oldtls
, tcbsize
, tcbalign
);
2603 for (obj
= objs
; obj
; obj
= obj
->next
) {
2604 if (obj
->tlsoffset
) {
2605 addr
= (Elf_Addr
)tls
+ obj
->tlsoffset
;
2606 memset((void*) (addr
+ obj
->tlsinitsize
),
2607 0, obj
->tlssize
- obj
->tlsinitsize
);
2609 memcpy((void*) addr
, obj
->tlsinit
,
2611 dtv
[obj
->tlsindex
+ 1] = addr
;
2612 } else if (obj
->tlsindex
) {
2613 dtv
[obj
->tlsindex
+ 1] = 0;
2622 free_tls(void *tls
, size_t tcbsize
, size_t tcbalign
)
2627 Elf_Addr tlsstart
, tlsend
;
2630 * Figure out the size of the initial TLS block so that we can
2631 * find stuff which __tls_get_addr() allocated dynamically.
2633 size
= tls_static_space
;
2635 dtv
= ((Elf_Addr
**)tls
)[0];
2637 tlsstart
= (Elf_Addr
) tls
;
2638 tlsend
= tlsstart
+ size
;
2639 for (i
= 0; i
< dtvsize
; i
++) {
2640 if (dtv
[i
+2] < tlsstart
|| dtv
[i
+2] > tlsend
) {
2641 free((void*) dtv
[i
+2]);
2645 free((void*) tlsstart
);
2650 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \
2654 * Allocate Static TLS using the Variant II method.
2657 allocate_tls(Obj_Entry
*objs
, void *oldtls
, size_t tcbsize
, size_t tcbalign
)
2662 Elf_Addr
*dtv
, *olddtv
;
2663 Elf_Addr segbase
, oldsegbase
, addr
;
2666 size
= round(tls_static_space
, tcbalign
);
2668 assert(tcbsize
>= 2*sizeof(Elf_Addr
));
2669 tls
= malloc(size
+ tcbsize
);
2670 dtv
= malloc((tls_max_index
+ 2) * sizeof(Elf_Addr
));
2672 segbase
= (Elf_Addr
)(tls
+ size
);
2673 ((Elf_Addr
*)segbase
)[0] = segbase
;
2674 ((Elf_Addr
*)segbase
)[1] = (Elf_Addr
) dtv
;
2676 dtv
[0] = tls_dtv_generation
;
2677 dtv
[1] = tls_max_index
;
2681 * Copy the static TLS block over whole.
2683 oldsegbase
= (Elf_Addr
) oldtls
;
2684 memcpy((void *)(segbase
- tls_static_space
),
2685 (const void *)(oldsegbase
- tls_static_space
),
2689 * If any dynamic TLS blocks have been created tls_get_addr(),
2692 olddtv
= ((Elf_Addr
**)oldsegbase
)[1];
2693 for (i
= 0; i
< olddtv
[1]; i
++) {
2694 if (olddtv
[i
+2] < oldsegbase
- size
|| olddtv
[i
+2] > oldsegbase
) {
2695 dtv
[i
+2] = olddtv
[i
+2];
2701 * We assume that this block was the one we created with
2702 * allocate_initial_tls().
2704 free_tls(oldtls
, 2*sizeof(Elf_Addr
), sizeof(Elf_Addr
));
2706 for (obj
= objs
; obj
; obj
= obj
->next
) {
2707 if (obj
->tlsoffset
) {
2708 addr
= segbase
- obj
->tlsoffset
;
2709 memset((void*) (addr
+ obj
->tlsinitsize
),
2710 0, obj
->tlssize
- obj
->tlsinitsize
);
2712 memcpy((void*) addr
, obj
->tlsinit
, obj
->tlsinitsize
);
2713 dtv
[obj
->tlsindex
+ 1] = addr
;
2714 } else if (obj
->tlsindex
) {
2715 dtv
[obj
->tlsindex
+ 1] = 0;
2720 return (void*) segbase
;
2724 free_tls(void *tls
, size_t tcbsize
, size_t tcbalign
)
2729 Elf_Addr tlsstart
, tlsend
;
2732 * Figure out the size of the initial TLS block so that we can
2733 * find stuff which ___tls_get_addr() allocated dynamically.
2735 size
= round(tls_static_space
, tcbalign
);
2737 dtv
= ((Elf_Addr
**)tls
)[1];
2739 tlsend
= (Elf_Addr
) tls
;
2740 tlsstart
= tlsend
- size
;
2741 for (i
= 0; i
< dtvsize
; i
++) {
2742 if (dtv
[i
+2] < tlsstart
|| dtv
[i
+2] > tlsend
) {
2743 free((void*) dtv
[i
+2]);
2747 free((void*) tlsstart
);
2753 * Allocate TLS block for module with given index.
2756 allocate_module_tls(int index
)
2761 for (obj
= obj_list
; obj
; obj
= obj
->next
) {
2762 if (obj
->tlsindex
== index
)
2766 _rtld_error("Can't find module with TLS index %d", index
);
2770 p
= malloc(obj
->tlssize
);
2771 memcpy(p
, obj
->tlsinit
, obj
->tlsinitsize
);
2772 memset(p
+ obj
->tlsinitsize
, 0, obj
->tlssize
- obj
->tlsinitsize
);
2778 allocate_tls_offset(Obj_Entry
*obj
)
2785 if (obj
->tlssize
== 0) {
2786 obj
->tls_done
= true;
2790 if (obj
->tlsindex
== 1)
2791 off
= calculate_first_tls_offset(obj
->tlssize
, obj
->tlsalign
);
2793 off
= calculate_tls_offset(tls_last_offset
, tls_last_size
,
2794 obj
->tlssize
, obj
->tlsalign
);
2797 * If we have already fixed the size of the static TLS block, we
2798 * must stay within that size. When allocating the static TLS, we
2799 * leave a small amount of space spare to be used for dynamically
2800 * loading modules which use static TLS.
2802 if (tls_static_space
) {
2803 if (calculate_tls_end(off
, obj
->tlssize
) > tls_static_space
)
2807 tls_last_offset
= obj
->tlsoffset
= off
;
2808 tls_last_size
= obj
->tlssize
;
2809 obj
->tls_done
= true;
2815 _rtld_allocate_tls(void *oldtls
, size_t tcbsize
, size_t tcbalign
)
2817 return allocate_tls(obj_list
, oldtls
, tcbsize
, tcbalign
);
2821 _rtld_free_tls(void *tcb
, size_t tcbsize
, size_t tcbalign
)
2823 free_tls(tcb
, tcbsize
, tcbalign
);