- Implement ProtocolResetComplete
[reactos.git] / dll / ntdll / ldr / elf.c
1 /* $Id$
2 */
3
4 /*
5 * REACTOS ELF LOADER
6 *
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
9 */
10
11 /*-
12 * Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra.
13 * Copyright 2003 Alexander Kabaev <kan@FreeBSD.ORG>.
14 * All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
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.
24 *
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.
35 *
36 * $FreeBSD: src/libexec/rtld-elf/rtld.c,v 1.101 2004/11/02 09:42:21 ssouhlal Exp $
37 */
38
39 /*
40 * Dynamic linker for ELF.
41 *
42 * John Polstra <jdp@polstra.com>.
43 */
44
45 #include <ntdll.h>
46 #define NDEBUG
47 #include <debug.h>
48
49 #if 0
50
51 #ifndef __GNUC__
52 #error "GCC is needed to compile this file"
53 #endif
54
55 #include <sys/param.h>
56 #include <sys/mman.h>
57 #include <sys/stat.h>
58
59 #include <dlfcn.h>
60 #include <err.h>
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <stdarg.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68
69 #include "debug.h"
70 #include "rtld.h"
71 #include "libmap.h"
72 #include "rtld_tls.h"
73
74 #ifndef COMPAT_32BIT
75 #define PATH_RTLD "/libexec/ld-elf.so.1"
76 #else
77 #define PATH_RTLD "/libexec/ld-elf32.so.1"
78 #endif
79
80 /* Types. */
81 typedef void (*func_ptr_type)();
82 typedef void * (*path_enum_proc) (const char *path, size_t len, void *arg);
83
84 /*
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.
87 */
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 */
92 } DoneList;
93
94 /*
95 * Function declarations.
96 */
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,
114 Objlist *list);
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 *);
147
148 void r_debug_state(struct r_debug*, struct link_map*);
149
150 /*
151 * Data declarations.
152 */
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
161 load first */
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 */
168
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);
175
176 static Elf_Sym sym_zero; /* For resolving undefined weak refs. */
177
178 #define GDB_STATE(s,m) r_debug.r_state = s; r_debug_state(&r_debug,m);
179
180 extern Elf_Dyn _DYNAMIC;
181 #pragma weak _DYNAMIC
182 #ifndef RTLD_IS_DYNAMIC
183 #define RTLD_IS_DYNAMIC() (&_DYNAMIC != NULL)
184 #endif
185
186 /*
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.
190 */
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,
201 #ifdef __i386__
202 (func_ptr_type) &___tls_get_addr,
203 #endif
204 (func_ptr_type) &__tls_get_addr,
205 (func_ptr_type) &_rtld_allocate_tls,
206 (func_ptr_type) &_rtld_free_tls,
207 NULL
208 };
209
210 /*
211 * Global declarations normally provided by crt1. The dynamic linker is
212 * not built with crt1, so we have to provide them ourselves.
213 */
214 char *__progname;
215 char **environ;
216
217 /*
218 * Globals to control TLS allocation.
219 */
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 */
225
226 /*
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.
230 */
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, \
235 (dlp)->num_used = 0)
236
237 /*
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.
246 *
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
249 * program's object.
250 *
251 * The return value is the main program's entry point.
252 */
253 func_ptr_type
254 _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
255 {
256 Elf_Auxinfo *aux_info[AT_COUNT];
257 int i;
258 int argc;
259 char **argv;
260 char **env;
261 Elf_Auxinfo *aux;
262 Elf_Auxinfo *auxp;
263 const char *argv0;
264 Objlist_Entry *entry;
265 Obj_Entry *obj;
266 Obj_Entry **preload_tail;
267 Objlist initlist;
268 int lockstate;
269
270 /*
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.
275 */
276
277 /* Find the auxiliary vector on the stack. */
278 argc = *sp++;
279 argv = (char **) sp;
280 sp += argc + 1; /* Skip over arguments and NULL terminator */
281 env = (char **) sp;
282 while (*sp++ != 0) /* Skip over environment, and NULL terminator */
283 ;
284 aux = (Elf_Auxinfo *) sp;
285
286 /* Digest the auxiliary vector. */
287 for (i = 0; i < AT_COUNT; i++)
288 aux_info[i] = NULL;
289 for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
290 if (auxp->a_type < AT_COUNT)
291 aux_info[auxp->a_type] = auxp;
292 }
293
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);
297
298 __progname = obj_rtld.path;
299 argv0 = argv[0] != NULL ? argv[0] : "(null)";
300 environ = env;
301
302 trust = !issetugid();
303
304 ld_bind_now = getenv(LD_ "BIND_NOW");
305 if (trust) {
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");
310 }
311 ld_tracing = getenv(LD_ "TRACE_LOADED_OBJECTS");
312
313 if (ld_debug != NULL && *ld_debug != '\0')
314 debug = 1;
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);
319
320 /*
321 * Load the main program, or process its program header if it is
322 * already loaded.
323 */
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);
328 close(fd);
329 if (obj_main == NULL)
330 die();
331 } else { /* Main program already loaded. */
332 const Elf_Phdr *phdr;
333 int phnum;
334 caddr_t entry;
335
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)
346 die();
347 }
348
349 obj_main->path = xstrdup(argv0);
350 obj_main->mainprog = true;
351
352 /*
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
356 * one is being used.
357 */
358 if (obj_main->interp != NULL &&
359 strcmp(obj_main->interp, obj_rtld.path) != 0) {
360 free(obj_rtld.path);
361 obj_rtld.path = xstrdup(obj_main->interp);
362 __progname = obj_rtld.path;
363 }
364
365 digest_dynamic(obj_main, 0);
366
367 linkmap_add(obj_main);
368 linkmap_add(&obj_rtld);
369
370 /* Link the main program into the list of objects. */
371 *obj_tail = obj_main;
372 obj_tail = &obj_main->next;
373 obj_count++;
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;
376
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;
380
381 if (!libmap_disable)
382 libmap_disable = (bool)lm_init();
383
384 dbg("loading LD_PRELOAD libraries");
385 if (load_preload_objects() == -1)
386 die();
387 preload_tail = obj_tail;
388
389 dbg("loading needed objects");
390 if (load_needed_objects(obj_main) == -1)
391 die();
392
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);
396 obj->refcount++;
397 }
398
399 if (ld_tracing) { /* We're done */
400 trace_loaded_objects(obj_main);
401 exit(0);
402 }
403
404 if (getenv(LD_ "DUMP_REL_PRE") != NULL) {
405 dump_relocations(obj_main);
406 exit (0);
407 }
408
409 /* setup TLS for main thread */
410 dbg("initializing initial thread local storage");
411 STAILQ_FOREACH(entry, &list_main, link) {
412 /*
413 * Allocate all the initial objects out of the static TLS
414 * block even if they didn't ask for it.
415 */
416 allocate_tls_offset(entry->obj);
417 }
418 allocate_initial_tls(obj_list);
419
420 if (relocate_objects(obj_main,
421 ld_bind_now != NULL && *ld_bind_now != '\0', &obj_rtld) == -1)
422 die();
423
424 dbg("doing copy relocations");
425 if (do_copy_relocations(obj_main) == -1)
426 die();
427
428 if (getenv(LD_ "DUMP_REL_POST") != NULL) {
429 dump_relocations(obj_main);
430 exit (0);
431 }
432
433 dbg("initializing key program variables");
434 set_program_var("__progname", argv[0] != NULL ? basename(argv[0]) : "");
435 set_program_var("environ", env);
436
437 dbg("initializing thread locks");
438 lockdflt_init();
439
440 /* Make a list of init functions to call. */
441 objlist_init(&initlist);
442 initlist_add_objects(obj_list, preload_tail, &initlist);
443
444 r_debug_state(NULL, &obj_main->linkmap); /* say hello to gdb! */
445
446 objlist_call_init(&initlist);
447 lockstate = wlock_acquire(rtld_bind_lock);
448 objlist_clear(&initlist);
449 wlock_release(rtld_bind_lock, lockstate);
450
451 dbg("transferring control to program entry point = %p", obj_main->entry);
452
453 /* Return the exit procedure and the program entry point. */
454 *exit_proc = rtld_exit;
455 *objp = obj_main;
456 return (func_ptr_type) obj_main->entry;
457 }
458
459 #endif /* 0 */
460
461 Elf_Addr
462 _rtld_bind(Obj_Entry *obj, Elf_Word reloff)
463 {
464 const Elf_Rel *rel;
465 const Elf_Sym *def;
466 const Obj_Entry *defobj;
467 Elf_Addr *where;
468 Elf_Addr target;
469 int lockstate;
470
471 lockstate = rlock_acquire(rtld_bind_lock);
472 if (obj->pltrel)
473 rel = (const Elf_Rel *) ((caddr_t) obj->pltrel + reloff);
474 else
475 rel = (const Elf_Rel *) ((caddr_t) obj->pltrela + reloff);
476
477 where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
478 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
479 if (def == NULL)
480 die();
481
482 target = (Elf_Addr)(defobj->relocbase + def->st_value);
483
484 dbg("\"%s\" in \"%s\" ==> %p in \"%s\"",
485 defobj->strtab + def->st_name, basename(obj->path),
486 (void *)target, basename(defobj->path));
487
488 /*
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.
494 */
495 target = reloc_jmpslot(where, target, defobj, obj, rel);
496 rlock_release(rtld_bind_lock, lockstate);
497 return target;
498 }
499
500 #if 0
501
502 /*
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.
506 */
507 void
508 _rtld_error(const char *fmt, ...)
509 {
510 static char buf[512];
511 va_list ap;
512
513 va_start(ap, fmt);
514 vsnprintf(buf, sizeof buf, fmt, ap);
515 error_message = buf;
516 va_end(ap);
517 }
518
519 /*
520 * Return a dynamically-allocated copy of the current error message, if any.
521 */
522 static char *
523 errmsg_save(void)
524 {
525 return error_message == NULL ? NULL : xstrdup(error_message);
526 }
527
528 /*
529 * Restore the current error message from a copy which was previously saved
530 * by errmsg_save(). The copy is freed.
531 */
532 static void
533 errmsg_restore(char *saved_msg)
534 {
535 if (saved_msg == NULL)
536 error_message = NULL;
537 else {
538 _rtld_error("%s", saved_msg);
539 free(saved_msg);
540 }
541 }
542
543 static const char *
544 basename(const char *name)
545 {
546 const char *p = strrchr(name, '/');
547 return p != NULL ? p + 1 : name;
548 }
549
550 static void
551 die(void)
552 {
553 const char *msg = dlerror();
554
555 if (msg == NULL)
556 msg = "Fatal error";
557 errx(1, "%s", msg);
558 }
559
560 #endif /* 0 */
561
562 /*
563 * Process a shared object's DYNAMIC section, and save the important
564 * information in its Obj_Entry structure.
565 */
566 static void
567 digest_dynamic(Obj_Entry *obj, int early)
568 {
569 const Elf_Dyn *dynp;
570 Needed_Entry **needed_tail = &obj->needed;
571 const Elf_Dyn *dyn_rpath = NULL;
572 int plttype = DT_REL;
573
574 obj->bind_now = false;
575 for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; dynp++) {
576 switch (dynp->d_tag) {
577
578 case DT_REL:
579 obj->rel = (const Elf_Rel *) (obj->relocbase + dynp->d_un.d_ptr);
580 break;
581
582 case DT_RELSZ:
583 obj->relsize = dynp->d_un.d_val;
584 break;
585
586 case DT_RELENT:
587 assert(dynp->d_un.d_val == sizeof(Elf_Rel));
588 break;
589
590 case DT_JMPREL:
591 obj->pltrel = (const Elf_Rel *)
592 (obj->relocbase + dynp->d_un.d_ptr);
593 break;
594
595 case DT_PLTRELSZ:
596 obj->pltrelsize = dynp->d_un.d_val;
597 break;
598
599 case DT_RELA:
600 obj->rela = (const Elf_Rela *) (obj->relocbase + dynp->d_un.d_ptr);
601 break;
602
603 case DT_RELASZ:
604 obj->relasize = dynp->d_un.d_val;
605 break;
606
607 case DT_RELAENT:
608 assert(dynp->d_un.d_val == sizeof(Elf_Rela));
609 break;
610
611 case DT_PLTREL:
612 plttype = dynp->d_un.d_val;
613 assert(dynp->d_un.d_val == DT_REL || plttype == DT_RELA);
614 break;
615
616 case DT_SYMTAB:
617 obj->symtab = (const Elf_Sym *)
618 (obj->relocbase + dynp->d_un.d_ptr);
619 break;
620
621 case DT_SYMENT:
622 assert(dynp->d_un.d_val == sizeof(Elf_Sym));
623 break;
624
625 case DT_STRTAB:
626 obj->strtab = (const char *) (obj->relocbase + dynp->d_un.d_ptr);
627 break;
628
629 case DT_STRSZ:
630 obj->strsize = dynp->d_un.d_val;
631 break;
632
633 case DT_HASH:
634 {
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;
641 }
642 break;
643
644 case DT_NEEDED:
645 if (!obj->rtld) {
646 Needed_Entry *nep = NEW(Needed_Entry);
647 nep->name = dynp->d_un.d_val;
648 nep->obj = NULL;
649 nep->next = NULL;
650
651 *needed_tail = nep;
652 needed_tail = &nep->next;
653 }
654 break;
655
656 case DT_PLTGOT:
657 obj->pltgot = (Elf_Addr *) (obj->relocbase + dynp->d_un.d_ptr);
658 break;
659
660 case DT_TEXTREL:
661 obj->textrel = true;
662 break;
663
664 case DT_SYMBOLIC:
665 obj->symbolic = true;
666 break;
667
668 case DT_RPATH:
669 case DT_RUNPATH: /* XXX: process separately */
670 /*
671 * We have to wait until later to process this, because we
672 * might not have gotten the address of the string table yet.
673 */
674 dyn_rpath = dynp;
675 break;
676
677 case DT_SONAME:
678 /* Not used by the dynamic linker. */
679 break;
680
681 case DT_INIT:
682 obj->init = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr);
683 break;
684
685 case DT_FINI:
686 obj->fini = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr);
687 break;
688
689 case DT_DEBUG:
690 /* XXX - not implemented yet */
691 if (!early)
692 dbg("Filling in DT_DEBUG entry");
693 ((Elf_Dyn*)dynp)->d_un.d_ptr = (Elf_Addr) &r_debug;
694 break;
695
696 case DT_FLAGS:
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)
700 die();
701 }
702 if (dynp->d_un.d_val & DF_SYMBOLIC)
703 obj->symbolic = true;
704 if (dynp->d_un.d_val & DF_TEXTREL)
705 obj->textrel = true;
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)
709 ;
710 break;
711
712 default:
713 if (!early) {
714 dbg("Ignoring d_tag %ld = %#lx", (long)dynp->d_tag,
715 (long)dynp->d_tag);
716 }
717 break;
718 }
719 }
720
721 obj->traced = false;
722
723 if (plttype == DT_RELA) {
724 obj->pltrela = (const Elf_Rela *) obj->pltrel;
725 obj->pltrel = NULL;
726 obj->pltrelasize = obj->pltrelsize;
727 obj->pltrelsize = 0;
728 }
729
730 if (dyn_rpath != NULL)
731 obj->rpath = obj->strtab + dyn_rpath->d_un.d_val;
732 }
733
734 /*
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.
739 */
740 static Obj_Entry *
741 digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry, const char *path)
742 {
743 Obj_Entry *obj;
744 const Elf_Phdr *phlimit = phdr + phnum;
745 const Elf_Phdr *ph;
746 int nsegs = 0;
747
748 obj = obj_new();
749 for (ph = phdr; ph < phlimit; ph++) {
750 switch (ph->p_type) {
751
752 case PT_PHDR:
753 if ((const Elf_Phdr *)ph->p_vaddr != phdr) {
754 _rtld_error("%s: invalid PT_PHDR", path);
755 return NULL;
756 }
757 obj->phdr = (const Elf_Phdr *) ph->p_vaddr;
758 obj->phsize = ph->p_memsz;
759 break;
760
761 case PT_INTERP:
762 obj->interp = (const char *) ph->p_vaddr;
763 break;
764
765 case PT_LOAD:
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) -
771 obj->vaddrbase;
772 } else { /* Last load segment */
773 obj->mapsize = round_page(ph->p_vaddr + ph->p_memsz) -
774 obj->vaddrbase;
775 }
776 nsegs++;
777 break;
778
779 case PT_DYNAMIC:
780 obj->dynamic = (const Elf_Dyn *) ph->p_vaddr;
781 break;
782
783 case PT_TLS:
784 obj->tlsindex = 1;
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;
789 break;
790 }
791 }
792 if (nsegs < 1) {
793 _rtld_error("%s: too few PT_LOAD segments", path);
794 return NULL;
795 }
796
797 obj->entry = entry;
798 return obj;
799 }
800
801 #if 0
802
803 static Obj_Entry *
804 dlcheck(void *handle)
805 {
806 Obj_Entry *obj;
807
808 for (obj = obj_list; obj != NULL; obj = obj->next)
809 if (obj == (Obj_Entry *) handle)
810 break;
811
812 if (obj == NULL || obj->refcount == 0 || obj->dl_refcount == 0) {
813 _rtld_error("Invalid shared object handle %p", handle);
814 return NULL;
815 }
816 return obj;
817 }
818
819 /*
820 * If the given object is already in the donelist, return true. Otherwise
821 * add the object to the list and return false.
822 */
823 static bool
824 donelist_check(DoneList *dlp, const Obj_Entry *obj)
825 {
826 unsigned int i;
827
828 for (i = 0; i < dlp->num_used; i++)
829 if (dlp->objs[i] == obj)
830 return true;
831 /*
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.
836 */
837 if (dlp->num_used < dlp->num_alloc)
838 dlp->objs[dlp->num_used++] = obj;
839 return false;
840 }
841
842 #endif /* 0 */
843
844 /*
845 * Hash function for symbol table lookup. Don't even think about changing
846 * this. It is specified by the System V ABI.
847 */
848 unsigned long
849 elf_hash(const char *name)
850 {
851 const unsigned char *p = (const unsigned char *) name;
852 unsigned long h = 0;
853 unsigned long g;
854
855 while (*p != '\0') {
856 h = (h << 4) + *p++;
857 if ((g = h & 0xf0000000) != 0)
858 h ^= g >> 24;
859 h &= ~g;
860 }
861 return h;
862 }
863
864 #if 0
865
866 /*
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.
870 *
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.
873 *
874 * The search order is:
875 * LD_LIBRARY_PATH
876 * rpath in the referencing file
877 * ldconfig hints
878 * /lib:/usr/lib
879 */
880 static char *
881 find_library(const char *xname, const Obj_Entry *refobj)
882 {
883 char *pathname;
884 char *name;
885
886 if (strchr(xname, '/') != NULL) { /* Hard coded pathname */
887 if (xname[0] != '/' && !trust) {
888 _rtld_error("Absolute pathname required for shared object \"%s\"",
889 xname);
890 return NULL;
891 }
892 return xstrdup(xname);
893 }
894
895 if (libmap_disable || (refobj == NULL) ||
896 (name = lm_find(refobj->path, xname)) == NULL)
897 name = (char *)xname;
898
899 dbg(" Searching for \"%s\"", name);
900
901 if ((pathname = search_library_path(name, ld_library_path)) != NULL ||
902 (refobj != 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)
906 return pathname;
907
908 if(refobj != NULL && refobj->path != NULL) {
909 _rtld_error("Shared object \"%s\" not found, required by \"%s\"",
910 name, basename(refobj->path));
911 } else {
912 _rtld_error("Shared object \"%s\" not found", name);
913 }
914 return NULL;
915 }
916
917 #endif /* 0 */
918
919 /*
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.
924 */
925 const Elf_Sym *
926 find_symdef(unsigned long symnum, const Obj_Entry *refobj,
927 const Obj_Entry **defobj_out, bool in_plt, SymCache *cache)
928 {
929 const Elf_Sym *ref;
930 const Elf_Sym *def;
931 const Obj_Entry *defobj;
932 const char *name;
933 unsigned long hash;
934
935 /*
936 * If we have already found this symbol, get the information from
937 * the cache.
938 */
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;
944 }
945
946 ref = refobj->symtab + symnum;
947 name = refobj->strtab + ref->st_name;
948 defobj = NULL;
949
950 /*
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.
957 */
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,
961 symnum);
962 }
963 hash = elf_hash(name);
964 def = symlook_default(name, hash, refobj, &defobj, in_plt);
965 } else {
966 def = ref;
967 defobj = refobj;
968 }
969
970 /*
971 * If we found no definition and the reference is weak, treat the
972 * symbol as having the value zero.
973 */
974 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
975 def = &sym_zero;
976 defobj = obj_main;
977 }
978
979 if (def != NULL) {
980 *defobj_out = defobj;
981 /* Record the information in the cache to avoid subsequent lookups. */
982 if (cache != NULL) {
983 cache[symnum].sym = def;
984 cache[symnum].obj = defobj;
985 }
986 } else {
987 if (refobj != &obj_rtld)
988 _rtld_error("%s: Undefined symbol \"%s\"", refobj->path, name);
989 }
990 return def;
991 }
992
993 #if 0
994
995 /*
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.
999 */
1000 static const char *
1001 gethints(void)
1002 {
1003 static char *hints;
1004
1005 if (hints == NULL) {
1006 int fd;
1007 struct elfhints_hdr hdr;
1008 char *p;
1009
1010 /* Keep from trying again in case the hints file is bad. */
1011 hints = "";
1012
1013 if ((fd = open(_PATH_ELF_HINTS, O_RDONLY)) == -1)
1014 return NULL;
1015 if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
1016 hdr.magic != ELFHINTS_MAGIC ||
1017 hdr.version != 1) {
1018 close(fd);
1019 return NULL;
1020 }
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) {
1024 free(p);
1025 close(fd);
1026 return NULL;
1027 }
1028 hints = p;
1029 close(fd);
1030 }
1031 return hints[0] != '\0' ? hints : NULL;
1032 }
1033
1034 static void
1035 init_dag(Obj_Entry *root)
1036 {
1037 DoneList donelist;
1038
1039 donelist_init(&donelist);
1040 init_dag1(root, root, &donelist);
1041 }
1042
1043 static void
1044 init_dag1(Obj_Entry *root, Obj_Entry *obj, DoneList *dlp)
1045 {
1046 const Needed_Entry *needed;
1047
1048 if (donelist_check(dlp, obj))
1049 return;
1050
1051 obj->refcount++;
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);
1057 }
1058
1059 /*
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.
1063 */
1064 static void
1065 init_rtld(caddr_t mapbase)
1066 {
1067 Obj_Entry objtmp; /* Temporary rtld object */
1068
1069 /*
1070 * Conjure up an Obj_Entry structure for the dynamic linker.
1071 *
1072 * The "path" member can't be initialized yet because string constatns
1073 * cannot yet be acessed. Below we will set it correctly.
1074 */
1075 memset(&objtmp, 0, sizeof(objtmp));
1076 objtmp.path = NULL;
1077 objtmp.rtld = true;
1078 objtmp.mapbase = mapbase;
1079 #ifdef PIC
1080 objtmp.relocbase = mapbase;
1081 #endif
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);
1087
1088 /*
1089 * Temporarily put the dynamic linker entry into the object list, so
1090 * that symbols can be found.
1091 */
1092
1093 relocate_objects(&objtmp, true, &objtmp);
1094 }
1095
1096 /* Initialize the object list. */
1097 obj_tail = &obj_list;
1098
1099 /* Now that non-local variables can be accesses, copy out obj_rtld. */
1100 memcpy(&obj_rtld, &objtmp, sizeof(obj_rtld));
1101
1102 /* Replace the path with a dynamically allocated copy. */
1103 obj_rtld.path = xstrdup(PATH_RTLD);
1104
1105 r_debug.r_brk = r_debug_state;
1106 r_debug.r_state = RT_CONSISTENT;
1107 }
1108
1109 /*
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.
1114 */
1115 static void
1116 initlist_add_neededs(Needed_Entry *needed, Objlist *list)
1117 {
1118 /* Recursively process the successor needed objects. */
1119 if (needed->next != NULL)
1120 initlist_add_neededs(needed->next, list);
1121
1122 /* Process the current needed object. */
1123 if (needed->obj != NULL)
1124 initlist_add_objects(needed->obj, &needed->obj->next, list);
1125 }
1126
1127 /*
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.
1135 */
1136 static void
1137 initlist_add_objects(Obj_Entry *obj, Obj_Entry **tail, Objlist *list)
1138 {
1139 if (obj->init_done)
1140 return;
1141 obj->init_done = true;
1142
1143 /* Recursively process the successor objects. */
1144 if (&obj->next != tail)
1145 initlist_add_objects(obj->next, tail, list);
1146
1147 /* Recursively process the needed objects. */
1148 if (obj->needed != NULL)
1149 initlist_add_neededs(obj->needed, list);
1150
1151 /* Add the object to the init list. */
1152 if (obj->init != (Elf_Addr)NULL)
1153 objlist_push_tail(list, obj);
1154
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);
1158 }
1159
1160 #ifndef FPTR_TARGET
1161 #define FPTR_TARGET(f) ((Elf_Addr) (f))
1162 #endif
1163
1164 static bool
1165 is_exported(const Elf_Sym *def)
1166 {
1167 Elf_Addr value;
1168 const func_ptr_type *p;
1169
1170 value = (Elf_Addr)(obj_rtld.relocbase + def->st_value);
1171 for (p = exports; *p != NULL; p++)
1172 if (FPTR_TARGET(*p) == value)
1173 return true;
1174 return false;
1175 }
1176
1177 /*
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.
1181 */
1182 static int
1183 load_needed_objects(Obj_Entry *first)
1184 {
1185 Obj_Entry *obj;
1186
1187 for (obj = first; obj != NULL; obj = obj->next) {
1188 Needed_Entry *needed;
1189
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);
1193
1194 needed->obj = NULL;
1195 if (path == NULL && !ld_tracing)
1196 return -1;
1197
1198 if (path) {
1199 needed->obj = load_object(path);
1200 if (needed->obj == NULL && !ld_tracing)
1201 return -1; /* XXX - cleanup */
1202 }
1203 }
1204 }
1205
1206 return 0;
1207 }
1208
1209 static int
1210 load_preload_objects(void)
1211 {
1212 char *p = ld_preload;
1213 static const char delim[] = " \t:;";
1214
1215 if (p == NULL)
1216 return 0;
1217
1218 p += strspn(p, delim);
1219 while (*p != '\0') {
1220 size_t len = strcspn(p, delim);
1221 char *path;
1222 char savech;
1223
1224 savech = p[len];
1225 p[len] = '\0';
1226 if ((path = find_library(p, NULL)) == NULL)
1227 return -1;
1228 if (load_object(path) == NULL)
1229 return -1; /* XXX - cleanup */
1230 p[len] = savech;
1231 p += len;
1232 p += strspn(p, delim);
1233 }
1234 return 0;
1235 }
1236
1237 /*
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.
1241 *
1242 * Returns a pointer to the Obj_Entry for the object. Returns NULL
1243 * on failure.
1244 */
1245 static Obj_Entry *
1246 load_object(char *path)
1247 {
1248 Obj_Entry *obj;
1249 int fd = -1;
1250 struct stat sb;
1251
1252 for (obj = obj_list->next; obj != NULL; obj = obj->next)
1253 if (strcmp(obj->path, path) == 0)
1254 break;
1255
1256 /*
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.
1260 *
1261 * To avoid a race, we open the file and use fstat() rather than
1262 * using stat().
1263 */
1264 if (obj == NULL) {
1265 if ((fd = open(path, O_RDONLY)) == -1) {
1266 _rtld_error("Cannot open \"%s\"", path);
1267 return NULL;
1268 }
1269 if (fstat(fd, &sb) == -1) {
1270 _rtld_error("Cannot fstat \"%s\"", path);
1271 close(fd);
1272 return NULL;
1273 }
1274 for (obj = obj_list->next; obj != NULL; obj = obj->next) {
1275 if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
1276 close(fd);
1277 break;
1278 }
1279 }
1280 }
1281
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);
1285 close(fd);
1286 if (obj == NULL) {
1287 free(path);
1288 return NULL;
1289 }
1290
1291 obj->path = path;
1292 digest_dynamic(obj, 0);
1293
1294 *obj_tail = obj;
1295 obj_tail = &obj->next;
1296 obj_count++;
1297 linkmap_add(obj); /* for GDB & dlinfo() */
1298
1299 dbg(" %p .. %p: %s", obj->mapbase,
1300 obj->mapbase + obj->mapsize - 1, obj->path);
1301 if (obj->textrel)
1302 dbg(" WARNING: %s has impure text", obj->path);
1303 } else
1304 free(path);
1305
1306 return obj;
1307 }
1308
1309 static Obj_Entry *
1310 obj_from_addr(const void *addr)
1311 {
1312 Obj_Entry *obj;
1313
1314 for (obj = obj_list; obj != NULL; obj = obj->next) {
1315 if (addr < (void *) obj->mapbase)
1316 continue;
1317 if (addr < (void *) (obj->mapbase + obj->mapsize))
1318 return obj;
1319 }
1320 return NULL;
1321 }
1322
1323 /*
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.
1327 */
1328 static void
1329 objlist_call_fini(Objlist *list)
1330 {
1331 Objlist_Entry *elm;
1332 char *saved_msg;
1333
1334 /*
1335 * Preserve the current error message since a fini function might
1336 * call into the dynamic linker and overwrite it.
1337 */
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);
1344 }
1345 }
1346 errmsg_restore(saved_msg);
1347 }
1348
1349 /*
1350 * Call the initialization functions for each of the objects in
1351 * "list". All of the objects are expected to have non-NULL init
1352 * functions.
1353 */
1354 static void
1355 objlist_call_init(Objlist *list)
1356 {
1357 Objlist_Entry *elm;
1358 char *saved_msg;
1359
1360 /*
1361 * Preserve the current error message since an init function might
1362 * call into the dynamic linker and overwrite it.
1363 */
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);
1369 }
1370 errmsg_restore(saved_msg);
1371 }
1372
1373 static void
1374 objlist_clear(Objlist *list)
1375 {
1376 Objlist_Entry *elm;
1377
1378 while (!STAILQ_EMPTY(list)) {
1379 elm = STAILQ_FIRST(list);
1380 STAILQ_REMOVE_HEAD(list, link);
1381 free(elm);
1382 }
1383 }
1384
1385 static Objlist_Entry *
1386 objlist_find(Objlist *list, const Obj_Entry *obj)
1387 {
1388 Objlist_Entry *elm;
1389
1390 STAILQ_FOREACH(elm, list, link)
1391 if (elm->obj == obj)
1392 return elm;
1393 return NULL;
1394 }
1395
1396 static void
1397 objlist_init(Objlist *list)
1398 {
1399 STAILQ_INIT(list);
1400 }
1401
1402 static void
1403 objlist_push_head(Objlist *list, Obj_Entry *obj)
1404 {
1405 Objlist_Entry *elm;
1406
1407 elm = NEW(Objlist_Entry);
1408 elm->obj = obj;
1409 STAILQ_INSERT_HEAD(list, elm, link);
1410 }
1411
1412 static void
1413 objlist_push_tail(Objlist *list, Obj_Entry *obj)
1414 {
1415 Objlist_Entry *elm;
1416
1417 elm = NEW(Objlist_Entry);
1418 elm->obj = obj;
1419 STAILQ_INSERT_TAIL(list, elm, link);
1420 }
1421
1422 static void
1423 objlist_remove(Objlist *list, Obj_Entry *obj)
1424 {
1425 Objlist_Entry *elm;
1426
1427 if ((elm = objlist_find(list, obj)) != NULL) {
1428 STAILQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
1429 free(elm);
1430 }
1431 }
1432
1433 /*
1434 * Remove all of the unreferenced objects from "list".
1435 */
1436 static void
1437 objlist_remove_unref(Objlist *list)
1438 {
1439 Objlist newlist;
1440 Objlist_Entry *elm;
1441
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)
1447 free(elm);
1448 else
1449 STAILQ_INSERT_TAIL(&newlist, elm, link);
1450 }
1451 *list = newlist;
1452 }
1453
1454 #endif /* 0 */
1455
1456 /*
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,
1460 * or -1 on failure.
1461 */
1462 static int
1463 relocate_objects(Obj_Entry *first, bool bind_now, Obj_Entry *rtldobj)
1464 {
1465 Obj_Entry *obj;
1466
1467 for (obj = first; obj != NULL; obj = obj->next) {
1468 if (obj != rtldobj)
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",
1473 obj->path);
1474 return -1;
1475 }
1476
1477 if (obj->textrel) {
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));
1483 return -1;
1484 }
1485 }
1486
1487 /* Process the non-PLT relocations. */
1488 if (reloc_non_plt(obj, rtldobj))
1489 return -1;
1490
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));
1496 return -1;
1497 }
1498 }
1499
1500 /* Process the PLT relocations. */
1501 if (reloc_plt(obj) == -1)
1502 return -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)
1506 return -1;
1507
1508
1509 /*
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.
1513 */
1514 obj->magic = RTLD_MAGIC;
1515 obj->version = RTLD_VERSION;
1516
1517 /* Set the special PLT or GOT entries. */
1518 init_pltgot(obj);
1519 }
1520
1521 return 0;
1522 }
1523
1524 #if 0
1525
1526 /*
1527 * Cleanup procedure. It will be called (by the atexit mechanism) just
1528 * before the process exits.
1529 */
1530 static void
1531 rtld_exit(void)
1532 {
1533 Obj_Entry *obj;
1534
1535 dbg("rtld_exit()");
1536 /* Clear all the reference counts so the fini functions will be called. */
1537 for (obj = obj_list; obj != NULL; obj = obj->next)
1538 obj->refcount = 0;
1539 objlist_call_fini(&list_fini);
1540 /* No need to remove the items from the list, since we are exiting. */
1541 if (!libmap_disable)
1542 lm_fini();
1543 }
1544
1545 static void *
1546 path_enumerate(const char *path, path_enum_proc callback, void *arg)
1547 {
1548 #ifdef COMPAT_32BIT
1549 const char *trans;
1550 #endif
1551 if (path == NULL)
1552 return (NULL);
1553
1554 path += strspn(path, ":;");
1555 while (*path != '\0') {
1556 size_t len;
1557 char *res;
1558
1559 len = strcspn(path, ":;");
1560 #ifdef COMPAT_32BIT
1561 trans = lm_findn(NULL, path, len);
1562 if (trans)
1563 res = callback(trans, strlen(trans), arg);
1564 else
1565 #endif
1566 res = callback(path, len, arg);
1567
1568 if (res != NULL)
1569 return (res);
1570
1571 path += len;
1572 path += strspn(path, ":;");
1573 }
1574
1575 return (NULL);
1576 }
1577
1578 struct try_library_args {
1579 const char *name;
1580 size_t namelen;
1581 char *buffer;
1582 size_t buflen;
1583 };
1584
1585 static void *
1586 try_library_path(const char *dir, size_t dirlen, void *param)
1587 {
1588 struct try_library_args *arg;
1589
1590 arg = param;
1591 if (*dir == '/' || trust) {
1592 char *pathname;
1593
1594 if (dirlen + 1 + arg->namelen + 1 > arg->buflen)
1595 return (NULL);
1596
1597 pathname = arg->buffer;
1598 strncpy(pathname, dir, dirlen);
1599 pathname[dirlen] = '/';
1600 strcpy(pathname + dirlen + 1, arg->name);
1601
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);
1606 return (pathname);
1607 }
1608 }
1609 return (NULL);
1610 }
1611
1612 static char *
1613 search_library_path(const char *name, const char *path)
1614 {
1615 char *p;
1616 struct try_library_args arg;
1617
1618 if (path == NULL)
1619 return NULL;
1620
1621 arg.name = name;
1622 arg.namelen = strlen(name);
1623 arg.buffer = xmalloc(PATH_MAX);
1624 arg.buflen = PATH_MAX;
1625
1626 p = path_enumerate(path, try_library_path, &arg);
1627
1628 free(arg.buffer);
1629
1630 return (p);
1631 }
1632
1633 int
1634 dlclose(void *handle)
1635 {
1636 Obj_Entry *root;
1637 int lockstate;
1638
1639 lockstate = wlock_acquire(rtld_bind_lock);
1640 root = dlcheck(handle);
1641 if (root == NULL) {
1642 wlock_release(rtld_bind_lock, lockstate);
1643 return -1;
1644 }
1645
1646 /* Unreference the object and its dependencies. */
1647 root->dl_refcount--;
1648
1649 unref_dag(root);
1650
1651 if (root->refcount == 0) {
1652 /*
1653 * The object is no longer referenced, so we must unload it.
1654 * First, call the fini functions with no locks held.
1655 */
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);
1660
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);
1665 }
1666 wlock_release(rtld_bind_lock, lockstate);
1667 return 0;
1668 }
1669
1670 const char *
1671 dlerror(void)
1672 {
1673 char *msg = error_message;
1674 error_message = NULL;
1675 return msg;
1676 }
1677
1678 /*
1679 * This function is deprecated and has no effect.
1680 */
1681 void
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))
1689 {
1690 static void *cur_context;
1691 static void (*cur_context_destroy)(void *);
1692
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;
1698 }
1699
1700 void *
1701 dlopen(const char *name, int mode)
1702 {
1703 Obj_Entry **old_obj_tail;
1704 Obj_Entry *obj;
1705 Objlist initlist;
1706 int result, lockstate;
1707
1708 ld_tracing = (mode & RTLD_TRACE) == 0 ? NULL : "1";
1709 if (ld_tracing != NULL)
1710 environ = (char **)*get_program_var_addr("environ");
1711
1712 objlist_init(&initlist);
1713
1714 lockstate = wlock_acquire(rtld_bind_lock);
1715 GDB_STATE(RT_ADD,NULL);
1716
1717 old_obj_tail = obj_tail;
1718 obj = NULL;
1719 if (name == NULL) {
1720 obj = obj_main;
1721 obj->refcount++;
1722 } else {
1723 char *path = find_library(name, obj_main);
1724 if (path != NULL)
1725 obj = load_object(path);
1726 }
1727
1728 if (obj) {
1729 obj->dl_refcount++;
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);
1735
1736 result = load_needed_objects(obj);
1737 if (result != -1 && ld_tracing)
1738 goto trace;
1739
1740 if (result == -1 ||
1741 (init_dag(obj), relocate_objects(obj, mode == RTLD_NOW,
1742 &obj_rtld)) == -1) {
1743 obj->dl_refcount--;
1744 unref_dag(obj);
1745 if (obj->refcount == 0)
1746 unload_object(obj);
1747 obj = NULL;
1748 } else {
1749 /* Make list of init functions to call. */
1750 initlist_add_objects(obj, &obj->next, &initlist);
1751 }
1752 } else {
1753
1754 /* Bump the reference counts for objects on this DAG. */
1755 ref_dag(obj);
1756
1757 if (ld_tracing)
1758 goto trace;
1759 }
1760 }
1761
1762 GDB_STATE(RT_CONSISTENT,obj ? &obj->linkmap : NULL);
1763
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);
1770 return obj;
1771 trace:
1772 trace_loaded_objects(obj);
1773 wlock_release(rtld_bind_lock, lockstate);
1774 exit(0);
1775 }
1776
1777 void *
1778 dlsym(void *handle, const char *name)
1779 {
1780 const Obj_Entry *obj;
1781 unsigned long hash;
1782 const Elf_Sym *def;
1783 const Obj_Entry *defobj;
1784 int lockstate;
1785
1786 hash = elf_hash(name);
1787 def = NULL;
1788 defobj = NULL;
1789
1790 lockstate = rlock_acquire(rtld_bind_lock);
1791 if (handle == NULL || handle == RTLD_NEXT ||
1792 handle == RTLD_DEFAULT || handle == RTLD_SELF) {
1793 void *retaddr;
1794
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);
1799 return NULL;
1800 }
1801 if (handle == NULL) { /* Just the caller's shared object. */
1802 def = symlook_obj(name, hash, obj, true);
1803 defobj = obj;
1804 } else if (handle == RTLD_NEXT || /* Objects after caller's */
1805 handle == RTLD_SELF) { /* ... caller included */
1806 if (handle == RTLD_NEXT)
1807 obj = obj->next;
1808 for (; obj != NULL; obj = obj->next) {
1809 if ((def = symlook_obj(name, hash, obj, true)) != NULL) {
1810 defobj = obj;
1811 break;
1812 }
1813 }
1814 } else {
1815 assert(handle == RTLD_DEFAULT);
1816 def = symlook_default(name, hash, obj, &defobj, true);
1817 }
1818 } else {
1819 if ((obj = dlcheck(handle)) == NULL) {
1820 rlock_release(rtld_bind_lock, lockstate);
1821 return NULL;
1822 }
1823
1824 if (obj->mainprog) {
1825 DoneList donelist;
1826
1827 /* Search main program and all libraries loaded by it. */
1828 donelist_init(&donelist);
1829 def = symlook_list(name, hash, &list_main, &defobj, true,
1830 &donelist);
1831 } else {
1832 /*
1833 * XXX - This isn't correct. The search should include the whole
1834 * DAG rooted at the given object.
1835 */
1836 def = symlook_obj(name, hash, obj, true);
1837 defobj = obj;
1838 }
1839 }
1840
1841 if (def != NULL) {
1842 rlock_release(rtld_bind_lock, lockstate);
1843
1844 /*
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.
1851 */
1852 if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
1853 return make_function_pointer(def, defobj);
1854 else
1855 return defobj->relocbase + def->st_value;
1856 }
1857
1858 _rtld_error("Undefined symbol \"%s\"", name);
1859 rlock_release(rtld_bind_lock, lockstate);
1860 return NULL;
1861 }
1862
1863 int
1864 dladdr(const void *addr, Dl_info *info)
1865 {
1866 const Obj_Entry *obj;
1867 const Elf_Sym *def;
1868 void *symbol_addr;
1869 unsigned long symoffset;
1870 int lockstate;
1871
1872 lockstate = rlock_acquire(rtld_bind_lock);
1873 obj = obj_from_addr(addr);
1874 if (obj == NULL) {
1875 _rtld_error("No shared object contains address");
1876 rlock_release(rtld_bind_lock, lockstate);
1877 return 0;
1878 }
1879 info->dli_fname = obj->path;
1880 info->dli_fbase = obj->mapbase;
1881 info->dli_saddr = (void *)0;
1882 info->dli_sname = NULL;
1883
1884 /*
1885 * Walk the symbol list looking for the symbol whose address is
1886 * closest to the address sent in.
1887 */
1888 for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
1889 def = obj->symtab + symoffset;
1890
1891 /*
1892 * For skip the symbol if st_shndx is either SHN_UNDEF or
1893 * SHN_COMMON.
1894 */
1895 if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
1896 continue;
1897
1898 /*
1899 * If the symbol is greater than the specified address, or if it
1900 * is further away from addr than the current nearest symbol,
1901 * then reject it.
1902 */
1903 symbol_addr = obj->relocbase + def->st_value;
1904 if (symbol_addr > addr || symbol_addr < info->dli_saddr)
1905 continue;
1906
1907 /* Update our idea of the nearest symbol. */
1908 info->dli_sname = obj->strtab + def->st_name;
1909 info->dli_saddr = symbol_addr;
1910
1911 /* Exact match? */
1912 if (info->dli_saddr == addr)
1913 break;
1914 }
1915 rlock_release(rtld_bind_lock, lockstate);
1916 return 1;
1917 }
1918
1919 int
1920 dlinfo(void *handle, int request, void *p)
1921 {
1922 const Obj_Entry *obj;
1923 int error, lockstate;
1924
1925 lockstate = rlock_acquire(rtld_bind_lock);
1926
1927 if (handle == NULL || handle == RTLD_SELF) {
1928 void *retaddr;
1929
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");
1933 } else
1934 obj = dlcheck(handle);
1935
1936 if (obj == NULL) {
1937 rlock_release(rtld_bind_lock, lockstate);
1938 return (-1);
1939 }
1940
1941 error = 0;
1942 switch (request) {
1943 case RTLD_DI_LINKMAP:
1944 *((struct link_map const **)p) = &obj->linkmap;
1945 break;
1946 case RTLD_DI_ORIGIN:
1947 error = rtld_dirname(obj->path, p);
1948 break;
1949
1950 case RTLD_DI_SERINFOSIZE:
1951 case RTLD_DI_SERINFO:
1952 error = do_search_info(obj, request, (struct dl_serinfo *)p);
1953 break;
1954
1955 default:
1956 _rtld_error("Invalid request %d passed to dlinfo()", request);
1957 error = -1;
1958 }
1959
1960 rlock_release(rtld_bind_lock, lockstate);
1961
1962 return (error);
1963 }
1964
1965 struct fill_search_info_args {
1966 int request;
1967 unsigned int flags;
1968 Dl_serinfo *serinfo;
1969 Dl_serpath *serpath;
1970 char *strspace;
1971 };
1972
1973 static void *
1974 fill_search_info(const char *dir, size_t dirlen, void *param)
1975 {
1976 struct fill_search_info_args *arg;
1977
1978 arg = param;
1979
1980 if (arg->request == RTLD_DI_SERINFOSIZE) {
1981 arg->serinfo->dls_cnt ++;
1982 arg->serinfo->dls_size += dirlen + 1;
1983 } else {
1984 struct dl_serpath *s_entry;
1985
1986 s_entry = arg->serpath;
1987 s_entry->dls_name = arg->strspace;
1988 s_entry->dls_flags = arg->flags;
1989
1990 strncpy(arg->strspace, dir, dirlen);
1991 arg->strspace[dirlen] = '\0';
1992
1993 arg->strspace += dirlen + 1;
1994 arg->serpath++;
1995 }
1996
1997 return (NULL);
1998 }
1999
2000 static int
2001 do_search_info(const Obj_Entry *obj, int request, struct dl_serinfo *info)
2002 {
2003 struct dl_serinfo _info;
2004 struct fill_search_info_args args;
2005
2006 args.request = RTLD_DI_SERINFOSIZE;
2007 args.serinfo = &_info;
2008
2009 _info.dls_size = __offsetof(struct dl_serinfo, dls_serpath);
2010 _info.dls_cnt = 0;
2011
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);
2016
2017
2018 if (request == RTLD_DI_SERINFOSIZE) {
2019 info->dls_size = _info.dls_size;
2020 info->dls_cnt = _info.dls_cnt;
2021 return (0);
2022 }
2023
2024 if (info->dls_cnt != _info.dls_cnt || info->dls_size != _info.dls_size) {
2025 _rtld_error("Uninitialized Dl_serinfo struct passed to dlinfo()");
2026 return (-1);
2027 }
2028
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];
2033
2034 args.flags = LA_SER_LIBPATH;
2035 if (path_enumerate(ld_library_path, fill_search_info, &args) != NULL)
2036 return (-1);
2037
2038 args.flags = LA_SER_RUNPATH;
2039 if (path_enumerate(obj->rpath, fill_search_info, &args) != NULL)
2040 return (-1);
2041
2042 args.flags = LA_SER_CONFIG;
2043 if (path_enumerate(gethints(), fill_search_info, &args) != NULL)
2044 return (-1);
2045
2046 args.flags = LA_SER_DEFAULT;
2047 if (path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args) != NULL)
2048 return (-1);
2049 return (0);
2050 }
2051
2052 static int
2053 rtld_dirname(const char *path, char *bname)
2054 {
2055 const char *endp;
2056
2057 /* Empty or NULL string gets treated as "." */
2058 if (path == NULL || *path == '\0') {
2059 bname[0] = '.';
2060 bname[1] = '\0';
2061 return (0);
2062 }
2063
2064 /* Strip trailing slashes */
2065 endp = path + strlen(path) - 1;
2066 while (endp > path && *endp == '/')
2067 endp--;
2068
2069 /* Find the start of the dir */
2070 while (endp > path && *endp != '/')
2071 endp--;
2072
2073 /* Either the dir is "/" or there are no slashes */
2074 if (endp == path) {
2075 bname[0] = *endp == '/' ? '/' : '.';
2076 bname[1] = '\0';
2077 return (0);
2078 } else {
2079 do {
2080 endp--;
2081 } while (endp > path && *endp == '/');
2082 }
2083
2084 if (endp - path + 2 > PATH_MAX)
2085 {
2086 _rtld_error("Filename is too long: %s", path);
2087 return(-1);
2088 }
2089
2090 strncpy(bname, path, endp - path + 1);
2091 bname[endp - path + 1] = '\0';
2092 return (0);
2093 }
2094
2095 static void
2096 linkmap_add(Obj_Entry *obj)
2097 {
2098 struct link_map *l = &obj->linkmap;
2099 struct link_map *prev;
2100
2101 obj->linkmap.l_name = obj->path;
2102 obj->linkmap.l_addr = obj->mapbase;
2103 obj->linkmap.l_ld = obj->dynamic;
2104 #ifdef __mips__
2105 /* GDB needs load offset on MIPS to use the symbols */
2106 obj->linkmap.l_offs = obj->relocbase;
2107 #endif
2108
2109 if (r_debug.r_map == NULL) {
2110 r_debug.r_map = l;
2111 return;
2112 }
2113
2114 /*
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.
2117 */
2118 for (prev = r_debug.r_map;
2119 prev->l_next != NULL && prev->l_next != &obj_rtld.linkmap;
2120 prev = prev->l_next)
2121 ;
2122
2123 /* Link in the new entry. */
2124 l->l_prev = prev;
2125 l->l_next = prev->l_next;
2126 if (l->l_next != NULL)
2127 l->l_next->l_prev = l;
2128 prev->l_next = l;
2129 }
2130
2131 static void
2132 linkmap_delete(Obj_Entry *obj)
2133 {
2134 struct link_map *l = &obj->linkmap;
2135
2136 if (l->l_prev == NULL) {
2137 if ((r_debug.r_map = l->l_next) != NULL)
2138 l->l_next->l_prev = NULL;
2139 return;
2140 }
2141
2142 if ((l->l_prev->l_next = l->l_next) != NULL)
2143 l->l_next->l_prev = l->l_prev;
2144 }
2145
2146 /*
2147 * Function for the debugger to set a breakpoint on to gain control.
2148 *
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.
2151 *
2152 * When the loadhook trap is hit (r_debug_state, set at program
2153 * initialization), the arguments can be found on the stack:
2154 *
2155 * +8 struct link_map *m
2156 * +4 struct r_debug *rd
2157 * +0 RetAddr
2158 */
2159 void
2160 r_debug_state(struct r_debug* rd, struct link_map *m)
2161 {
2162 }
2163
2164 /*
2165 * Get address of the pointer variable in the main program.
2166 */
2167 static const void **
2168 get_program_var_addr(const char *name)
2169 {
2170 const Obj_Entry *obj;
2171 unsigned long hash;
2172
2173 hash = elf_hash(name);
2174 for (obj = obj_main; obj != NULL; obj = obj->next) {
2175 const Elf_Sym *def;
2176
2177 if ((def = symlook_obj(name, hash, obj, false)) != NULL) {
2178 const void **addr;
2179
2180 addr = (const void **)(obj->relocbase + def->st_value);
2181 return addr;
2182 }
2183 }
2184 return NULL;
2185 }
2186
2187 /*
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.
2191 */
2192 static void
2193 set_program_var(const char *name, const void *value)
2194 {
2195 const void **addr;
2196
2197 if ((addr = get_program_var_addr(name)) != NULL) {
2198 dbg("\"%s\": *%p <-- %p", name, addr, value);
2199 *addr = value;
2200 }
2201 }
2202
2203 /*
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.
2208 */
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)
2212 {
2213 DoneList donelist;
2214 const Elf_Sym *def;
2215 const Elf_Sym *symp;
2216 const Obj_Entry *obj;
2217 const Obj_Entry *defobj;
2218 const Objlist_Entry *elm;
2219 def = NULL;
2220 defobj = NULL;
2221 donelist_init(&donelist);
2222
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);
2226 if (symp != NULL) {
2227 def = symp;
2228 defobj = refobj;
2229 }
2230 }
2231
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);
2235 if (symp != NULL &&
2236 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2237 def = symp;
2238 defobj = obj;
2239 }
2240 }
2241
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)
2245 break;
2246 symp = symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt,
2247 &donelist);
2248 if (symp != NULL &&
2249 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2250 def = symp;
2251 defobj = obj;
2252 }
2253 }
2254
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)
2258 break;
2259 symp = symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt,
2260 &donelist);
2261 if (symp != NULL &&
2262 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2263 def = symp;
2264 defobj = obj;
2265 }
2266 }
2267
2268 /*
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.
2273 */
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)) {
2277 def = symp;
2278 defobj = &obj_rtld;
2279 }
2280 }
2281
2282 if (def != NULL)
2283 *defobj_out = defobj;
2284 return def;
2285 }
2286
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)
2290 {
2291 const Elf_Sym *symp;
2292 const Elf_Sym *def;
2293 const Obj_Entry *defobj;
2294 const Objlist_Entry *elm;
2295
2296 def = NULL;
2297 defobj = NULL;
2298 STAILQ_FOREACH(elm, objlist, link) {
2299 if (donelist_check(dlp, elm->obj))
2300 continue;
2301 if ((symp = symlook_obj(name, hash, elm->obj, in_plt)) != NULL) {
2302 if (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK) {
2303 def = symp;
2304 defobj = elm->obj;
2305 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
2306 break;
2307 }
2308 }
2309 }
2310 if (def != NULL)
2311 *defobj_out = defobj;
2312 return def;
2313 }
2314
2315 #endif /* 0 */
2316
2317 /*
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.
2321 *
2322 * The symbol's hash value is passed in for efficiency reasons; that
2323 * eliminates many recomputations of the hash value.
2324 */
2325 const Elf_Sym *
2326 symlook_obj(const char *name, unsigned long hash, const Obj_Entry *obj,
2327 bool in_plt)
2328 {
2329 if (obj->buckets != NULL) {
2330 unsigned long symnum = obj->buckets[hash % obj->nbuckets];
2331
2332 while (symnum != STN_UNDEF) {
2333 const Elf_Sym *symp;
2334 const char *strp;
2335
2336 if (symnum >= obj->nchains)
2337 return NULL; /* Bad object */
2338 symp = obj->symtab + symnum;
2339 strp = obj->strtab + symp->st_name;
2340
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;
2345
2346 symnum = obj->chains[symnum];
2347 }
2348 }
2349 return NULL;
2350 }
2351
2352 #if 0
2353
2354 static void
2355 trace_loaded_objects(Obj_Entry *obj)
2356 {
2357 char *fmt1, *fmt2, *fmt, *main_local, *list_containers;
2358 int c;
2359
2360 if ((main_local = getenv(LD_ "TRACE_LOADED_OBJECTS_PROGNAME")) == NULL)
2361 main_local = "";
2362
2363 if ((fmt1 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT1")) == NULL)
2364 fmt1 = "\t%o => %p (%x)\n";
2365
2366 if ((fmt2 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT2")) == NULL)
2367 fmt2 = "\t%o (%x)\n";
2368
2369 list_containers = getenv(LD_ "TRACE_LOADED_OBJECTS_ALL");
2370
2371 for (; obj; obj = obj->next) {
2372 Needed_Entry *needed;
2373 char *name, *path;
2374 bool is_lib;
2375
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)
2381 continue;
2382 needed->obj->traced = true;
2383 path = needed->obj->path;
2384 } else
2385 path = "not found";
2386
2387 name = (char *)obj->strtab + needed->name;
2388 is_lib = strncmp(name, "lib", 3) == 0; /* XXX - bogus */
2389
2390 fmt = is_lib ? fmt1 : fmt2;
2391 while ((c = *fmt++) != '\0') {
2392 switch (c) {
2393 default:
2394 putchar(c);
2395 continue;
2396 case '\\':
2397 switch (c = *fmt) {
2398 case '\0':
2399 continue;
2400 case 'n':
2401 putchar('\n');
2402 break;
2403 case 't':
2404 putchar('\t');
2405 break;
2406 }
2407 break;
2408 case '%':
2409 switch (c = *fmt) {
2410 case '\0':
2411 continue;
2412 case '%':
2413 default:
2414 putchar(c);
2415 break;
2416 case 'A':
2417 printf("%s", main_local);
2418 break;
2419 case 'a':
2420 printf("%s", obj_main->path);
2421 break;
2422 case 'o':
2423 printf("%s", name);
2424 break;
2425 #if 0
2426 case 'm':
2427 printf("%d", sodp->sod_major);
2428 break;
2429 case 'n':
2430 printf("%d", sodp->sod_minor);
2431 break;
2432 #endif
2433 case 'p':
2434 printf("%s", path);
2435 break;
2436 case 'x':
2437 printf("%p", needed->obj ? needed->obj->mapbase : 0);
2438 break;
2439 }
2440 break;
2441 }
2442 ++fmt;
2443 }
2444 }
2445 }
2446 }
2447
2448 /*
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.
2453 */
2454 static void
2455 unload_object(Obj_Entry *root)
2456 {
2457 Obj_Entry *obj;
2458 Obj_Entry **linkp;
2459
2460 assert(root->refcount == 0);
2461
2462 /*
2463 * Pass over the DAG removing unreferenced objects from
2464 * appropriate lists.
2465 */
2466 unlink_object(root);
2467
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);
2475 *linkp = obj->next;
2476 obj_count--;
2477 obj_free(obj);
2478 } else
2479 linkp = &obj->next;
2480 }
2481 obj_tail = linkp;
2482 }
2483
2484 static void
2485 unlink_object(Obj_Entry *root)
2486 {
2487 Objlist_Entry *elm;
2488
2489 if (root->refcount == 0) {
2490 /* Remove the object from the RTLD_GLOBAL list. */
2491 objlist_remove(&list_global, root);
2492
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);
2498 }
2499 }
2500 }
2501
2502 static void
2503 ref_dag(Obj_Entry *root)
2504 {
2505 Objlist_Entry *elm;
2506
2507 STAILQ_FOREACH(elm, &root->dagmembers , link)
2508 elm->obj->refcount++;
2509 }
2510
2511 static void
2512 unref_dag(Obj_Entry *root)
2513 {
2514 Objlist_Entry *elm;
2515
2516 STAILQ_FOREACH(elm, &root->dagmembers , link)
2517 elm->obj->refcount--;
2518 }
2519
2520 /*
2521 * Common code for MD __tls_get_addr().
2522 */
2523 void *
2524 tls_get_addr_common(Elf_Addr** dtvp, int index, size_t offset)
2525 {
2526 Elf_Addr* dtv = *dtvp;
2527
2528 /* Check dtv generation in case new modules have arrived */
2529 if (dtv[0] != tls_dtv_generation) {
2530 Elf_Addr* newdtv;
2531 int to_copy;
2532
2533 newdtv = calloc(1, (tls_max_index + 2) * sizeof(Elf_Addr));
2534 to_copy = dtv[1];
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;
2540 free(dtv);
2541 *dtvp = newdtv;
2542 }
2543
2544 /* Dynamically allocate module TLS if necessary */
2545 if (!dtv[index + 1])
2546 dtv[index + 1] = (Elf_Addr)allocate_module_tls(index);
2547
2548 return (void*) (dtv[index + 1] + offset);
2549 }
2550
2551 /* XXX not sure what variants to use for arm. */
2552
2553 #if defined(__ia64__) || defined(__alpha__) || defined(__powerpc__)
2554
2555 /*
2556 * Allocate Static TLS using the Variant I method.
2557 */
2558 void *
2559 allocate_tls(Obj_Entry *objs, void *oldtls, size_t tcbsize, size_t tcbalign)
2560 {
2561 Obj_Entry *obj;
2562 size_t size;
2563 char *tls;
2564 Elf_Addr *dtv, *olddtv;
2565 Elf_Addr addr;
2566 int i;
2567
2568 size = tls_static_space;
2569
2570 tls = malloc(size);
2571 dtv = malloc((tls_max_index + 2) * sizeof(Elf_Addr));
2572
2573 *(Elf_Addr**) tls = dtv;
2574
2575 dtv[0] = tls_dtv_generation;
2576 dtv[1] = tls_max_index;
2577
2578 if (oldtls) {
2579 /*
2580 * Copy the static TLS block over whole.
2581 */
2582 memcpy(tls + tcbsize, oldtls + tcbsize, tls_static_space - tcbsize);
2583
2584 /*
2585 * If any dynamic TLS blocks have been created tls_get_addr(),
2586 * move them over.
2587 */
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];
2593 olddtv[i+2] = 0;
2594 }
2595 }
2596
2597 /*
2598 * We assume that all tls blocks are allocated with the same
2599 * size and alignment.
2600 */
2601 free_tls(oldtls, tcbsize, tcbalign);
2602 } else {
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);
2608 if (obj->tlsinit)
2609 memcpy((void*) addr, obj->tlsinit,
2610 obj->tlsinitsize);
2611 dtv[obj->tlsindex + 1] = addr;
2612 } else if (obj->tlsindex) {
2613 dtv[obj->tlsindex + 1] = 0;
2614 }
2615 }
2616 }
2617
2618 return tls;
2619 }
2620
2621 void
2622 free_tls(void *tls, size_t tcbsize, size_t tcbalign)
2623 {
2624 size_t size;
2625 Elf_Addr* dtv;
2626 int dtvsize, i;
2627 Elf_Addr tlsstart, tlsend;
2628
2629 /*
2630 * Figure out the size of the initial TLS block so that we can
2631 * find stuff which __tls_get_addr() allocated dynamically.
2632 */
2633 size = tls_static_space;
2634
2635 dtv = ((Elf_Addr**)tls)[0];
2636 dtvsize = dtv[1];
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]);
2642 }
2643 }
2644
2645 free((void*) tlsstart);
2646 }
2647
2648 #endif
2649
2650 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \
2651 defined(__arm__)
2652
2653 /*
2654 * Allocate Static TLS using the Variant II method.
2655 */
2656 void *
2657 allocate_tls(Obj_Entry *objs, void *oldtls, size_t tcbsize, size_t tcbalign)
2658 {
2659 Obj_Entry *obj;
2660 size_t size;
2661 char *tls;
2662 Elf_Addr *dtv, *olddtv;
2663 Elf_Addr segbase, oldsegbase, addr;
2664 int i;
2665
2666 size = round(tls_static_space, tcbalign);
2667
2668 assert(tcbsize >= 2*sizeof(Elf_Addr));
2669 tls = malloc(size + tcbsize);
2670 dtv = malloc((tls_max_index + 2) * sizeof(Elf_Addr));
2671
2672 segbase = (Elf_Addr)(tls + size);
2673 ((Elf_Addr*)segbase)[0] = segbase;
2674 ((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv;
2675
2676 dtv[0] = tls_dtv_generation;
2677 dtv[1] = tls_max_index;
2678
2679 if (oldtls) {
2680 /*
2681 * Copy the static TLS block over whole.
2682 */
2683 oldsegbase = (Elf_Addr) oldtls;
2684 memcpy((void *)(segbase - tls_static_space),
2685 (const void *)(oldsegbase - tls_static_space),
2686 tls_static_space);
2687
2688 /*
2689 * If any dynamic TLS blocks have been created tls_get_addr(),
2690 * move them over.
2691 */
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];
2696 olddtv[i+2] = 0;
2697 }
2698 }
2699
2700 /*
2701 * We assume that this block was the one we created with
2702 * allocate_initial_tls().
2703 */
2704 free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr));
2705 } else {
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);
2711 if (obj->tlsinit)
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;
2716 }
2717 }
2718 }
2719
2720 return (void*) segbase;
2721 }
2722
2723 void
2724 free_tls(void *tls, size_t tcbsize, size_t tcbalign)
2725 {
2726 size_t size;
2727 Elf_Addr* dtv;
2728 int dtvsize, i;
2729 Elf_Addr tlsstart, tlsend;
2730
2731 /*
2732 * Figure out the size of the initial TLS block so that we can
2733 * find stuff which ___tls_get_addr() allocated dynamically.
2734 */
2735 size = round(tls_static_space, tcbalign);
2736
2737 dtv = ((Elf_Addr**)tls)[1];
2738 dtvsize = dtv[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]);
2744 }
2745 }
2746
2747 free((void*) tlsstart);
2748 }
2749
2750 #endif
2751
2752 /*
2753 * Allocate TLS block for module with given index.
2754 */
2755 void *
2756 allocate_module_tls(int index)
2757 {
2758 Obj_Entry* obj;
2759 char* p;
2760
2761 for (obj = obj_list; obj; obj = obj->next) {
2762 if (obj->tlsindex == index)
2763 break;
2764 }
2765 if (!obj) {
2766 _rtld_error("Can't find module with TLS index %d", index);
2767 die();
2768 }
2769
2770 p = malloc(obj->tlssize);
2771 memcpy(p, obj->tlsinit, obj->tlsinitsize);
2772 memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize);
2773
2774 return p;
2775 }
2776
2777 bool
2778 allocate_tls_offset(Obj_Entry *obj)
2779 {
2780 size_t off;
2781
2782 if (obj->tls_done)
2783 return true;
2784
2785 if (obj->tlssize == 0) {
2786 obj->tls_done = true;
2787 return true;
2788 }
2789
2790 if (obj->tlsindex == 1)
2791 off = calculate_first_tls_offset(obj->tlssize, obj->tlsalign);
2792 else
2793 off = calculate_tls_offset(tls_last_offset, tls_last_size,
2794 obj->tlssize, obj->tlsalign);
2795
2796 /*
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.
2801 */
2802 if (tls_static_space) {
2803 if (calculate_tls_end(off, obj->tlssize) > tls_static_space)
2804 return false;
2805 }
2806
2807 tls_last_offset = obj->tlsoffset = off;
2808 tls_last_size = obj->tlssize;
2809 obj->tls_done = true;
2810
2811 return true;
2812 }
2813
2814 void *
2815 _rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
2816 {
2817 return allocate_tls(obj_list, oldtls, tcbsize, tcbalign);
2818 }
2819
2820 void
2821 _rtld_free_tls(void *tcb, size_t tcbsize, size_t tcbalign)
2822 {
2823 free_tls(tcb, tcbsize, tcbalign);
2824 }
2825
2826 #endif /* 0 */