- Sync with trunk up to r46941.
[reactos.git] / dll / ntdll / ldr / elf.c
1 /*
2 * REACTOS ELF LOADER
3 *
4 * ELF run-time linker, ported from FreeBSD by KJK::Hyperion as part of the ELF
5 * support initiative. Original copyright, licensing and disclaimers follow
6 */
7
8 /*-
9 * Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra.
10 * Copyright 2003 Alexander Kabaev <kan@FreeBSD.ORG>.
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $FreeBSD: src/libexec/rtld-elf/rtld.c,v 1.101 2004/11/02 09:42:21 ssouhlal Exp $
34 */
35
36
37 /*
38 * Dynamic linker for ELF.
39 *
40 * John Polstra <jdp@polstra.com>.
41 */
42
43 #include <ntdll.h>
44 #define NDEBUG
45 #include <debug.h>
46
47 #if 0
48
49 #ifndef __GNUC__
50 #error "GCC is needed to compile this file"
51 #endif
52
53 #include <sys/param.h>
54 #include <sys/mman.h>
55 #include <sys/stat.h>
56
57 #include <dlfcn.h>
58 #include <err.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <stdarg.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66
67 #include "debug.h"
68 #include "rtld.h"
69 #include "libmap.h"
70 #include "rtld_tls.h"
71
72 #ifndef COMPAT_32BIT
73 #define PATH_RTLD "/libexec/ld-elf.so.1"
74 #else
75 #define PATH_RTLD "/libexec/ld-elf32.so.1"
76 #endif
77
78 /* Types. */
79 typedef void (*func_ptr_type)();
80 typedef void * (*path_enum_proc) (const char *path, size_t len, void *arg);
81
82 /*
83 * This structure provides a reentrant way to keep a list of objects and
84 * check which ones have already been processed in some way.
85 */
86 typedef struct Struct_DoneList {
87 const Obj_Entry **objs; /* Array of object pointers */
88 unsigned int num_alloc; /* Allocated size of the array */
89 unsigned int num_used; /* Number of array slots used */
90 } DoneList;
91
92 /*
93 * Function declarations.
94 */
95 static const char *basename(const char *);
96 static void die(void);
97 static void digest_dynamic(Obj_Entry *, int);
98 static Obj_Entry *digest_phdr(const Elf_Phdr *, int, caddr_t, const char *);
99 static Obj_Entry *dlcheck(void *);
100 static int do_search_info(const Obj_Entry *obj, int, struct dl_serinfo *);
101 static bool donelist_check(DoneList *, const Obj_Entry *);
102 static void errmsg_restore(char *);
103 static char *errmsg_save(void);
104 static void *fill_search_info(const char *, size_t, void *);
105 static char *find_library(const char *, const Obj_Entry *);
106 static const char *gethints(void);
107 static void init_dag(Obj_Entry *);
108 static void init_dag1(Obj_Entry *root, Obj_Entry *obj, DoneList *);
109 static void init_rtld(caddr_t);
110 static void initlist_add_neededs(Needed_Entry *needed, Objlist *list);
111 static void initlist_add_objects(Obj_Entry *obj, Obj_Entry **tail,
112 Objlist *list);
113 static bool is_exported(const Elf_Sym *);
114 static void linkmap_add(Obj_Entry *);
115 static void linkmap_delete(Obj_Entry *);
116 static int load_needed_objects(Obj_Entry *);
117 static int load_preload_objects(void);
118 static Obj_Entry *load_object(char *);
119 static Obj_Entry *obj_from_addr(const void *);
120 static void objlist_call_fini(Objlist *);
121 static void objlist_call_init(Objlist *);
122 static void objlist_clear(Objlist *);
123 static Objlist_Entry *objlist_find(Objlist *, const Obj_Entry *);
124 static void objlist_init(Objlist *);
125 static void objlist_push_head(Objlist *, Obj_Entry *);
126 static void objlist_push_tail(Objlist *, Obj_Entry *);
127 static void objlist_remove(Objlist *, Obj_Entry *);
128 static void objlist_remove_unref(Objlist *);
129 static void *path_enumerate(const char *, path_enum_proc, void *);
130 static int relocate_objects(Obj_Entry *, bool, Obj_Entry *);
131 static int rtld_dirname(const char *, char *);
132 static void rtld_exit(void);
133 static char *search_library_path(const char *, const char *);
134 static const void **get_program_var_addr(const char *name);
135 static void set_program_var(const char *, const void *);
136 static const Elf_Sym *symlook_default(const char *, unsigned long hash,
137 const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt);
138 static const Elf_Sym *symlook_list(const char *, unsigned long,
139 Objlist *, const Obj_Entry **, bool in_plt, DoneList *);
140 static void trace_loaded_objects(Obj_Entry *obj);
141 static void unlink_object(Obj_Entry *);
142 static void unload_object(Obj_Entry *);
143 static void unref_dag(Obj_Entry *);
144 static void ref_dag(Obj_Entry *);
145
146 void r_debug_state(struct r_debug*, struct link_map*);
147
148 /*
149 * Data declarations.
150 */
151 static char *error_message; /* Message for dlerror(), or NULL */
152 struct r_debug r_debug; /* for GDB; */
153 static bool libmap_disable; /* Disable libmap */
154 static bool trust; /* False for setuid and setgid programs */
155 static char *ld_bind_now; /* Environment variable for immediate binding */
156 static char *ld_debug; /* Environment variable for debugging */
157 static char *ld_library_path; /* Environment variable for search path */
158 static char *ld_preload; /* Environment variable for libraries to
159 load first */
160 static char *ld_tracing; /* Called from ldd to print libs */
161 static Obj_Entry *obj_list; /* Head of linked list of shared objects */
162 static Obj_Entry **obj_tail; /* Link field of last object in list */
163 static Obj_Entry *obj_main; /* The main program shared object */
164 static Obj_Entry obj_rtld; /* The dynamic linker shared object */
165 static unsigned int obj_count; /* Number of objects in obj_list */
166
167 static Objlist list_global = /* Objects dlopened with RTLD_GLOBAL */
168 STAILQ_HEAD_INITIALIZER(list_global);
169 static Objlist list_main = /* Objects loaded at program startup */
170 STAILQ_HEAD_INITIALIZER(list_main);
171 static Objlist list_fini = /* Objects needing fini() calls */
172 STAILQ_HEAD_INITIALIZER(list_fini);
173
174 static Elf_Sym sym_zero; /* For resolving undefined weak refs. */
175
176 #define GDB_STATE(s,m) r_debug.r_state = s; r_debug_state(&r_debug,m);
177
178 extern Elf_Dyn _DYNAMIC;
179 #pragma weak _DYNAMIC
180 #ifndef RTLD_IS_DYNAMIC
181 #define RTLD_IS_DYNAMIC() (&_DYNAMIC != NULL)
182 #endif
183
184 /*
185 * These are the functions the dynamic linker exports to application
186 * programs. They are the only symbols the dynamic linker is willing
187 * to export from itself.
188 */
189 static func_ptr_type exports[] = {
190 (func_ptr_type) &_rtld_error,
191 (func_ptr_type) &dlclose,
192 (func_ptr_type) &dlerror,
193 (func_ptr_type) &dlopen,
194 (func_ptr_type) &dlsym,
195 (func_ptr_type) &dladdr,
196 (func_ptr_type) &dllockinit,
197 (func_ptr_type) &dlinfo,
198 (func_ptr_type) &_rtld_thread_init,
199 #ifdef __i386__
200 (func_ptr_type) &___tls_get_addr,
201 #endif
202 (func_ptr_type) &__tls_get_addr,
203 (func_ptr_type) &_rtld_allocate_tls,
204 (func_ptr_type) &_rtld_free_tls,
205 NULL
206 };
207
208 /*
209 * Global declarations normally provided by crt1. The dynamic linker is
210 * not built with crt1, so we have to provide them ourselves.
211 */
212 char *__progname;
213 char **environ;
214
215 /*
216 * Globals to control TLS allocation.
217 */
218 size_t tls_last_offset; /* Static TLS offset of last module */
219 size_t tls_last_size; /* Static TLS size of last module */
220 size_t tls_static_space; /* Static TLS space allocated */
221 int tls_dtv_generation = 1; /* Used to detect when dtv size changes */
222 int tls_max_index = 1; /* Largest module index allocated */
223
224 /*
225 * Fill in a DoneList with an allocation large enough to hold all of
226 * the currently-loaded objects. Keep this as a macro since it calls
227 * alloca and we want that to occur within the scope of the caller.
228 */
229 #define donelist_init(dlp) \
230 ((dlp)->objs = alloca(obj_count * sizeof (dlp)->objs[0]), \
231 assert((dlp)->objs != NULL), \
232 (dlp)->num_alloc = obj_count, \
233 (dlp)->num_used = 0)
234
235 /*
236 * Main entry point for dynamic linking. The first argument is the
237 * stack pointer. The stack is expected to be laid out as described
238 * in the SVR4 ABI specification, Intel 386 Processor Supplement.
239 * Specifically, the stack pointer points to a word containing
240 * ARGC. Following that in the stack is a null-terminated sequence
241 * of pointers to argument strings. Then comes a null-terminated
242 * sequence of pointers to environment strings. Finally, there is a
243 * sequence of "auxiliary vector" entries.
244 *
245 * The second argument points to a place to store the dynamic linker's
246 * exit procedure pointer and the third to a place to store the main
247 * program's object.
248 *
249 * The return value is the main program's entry point.
250 */
251 func_ptr_type
252 _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
253 {
254 Elf_Auxinfo *aux_info[AT_COUNT];
255 int i;
256 int argc;
257 char **argv;
258 char **env;
259 Elf_Auxinfo *aux;
260 Elf_Auxinfo *auxp;
261 const char *argv0;
262 Objlist_Entry *entry;
263 Obj_Entry *obj;
264 Obj_Entry **preload_tail;
265 Objlist initlist;
266 int lockstate;
267
268 /*
269 * On entry, the dynamic linker itself has not been relocated yet.
270 * Be very careful not to reference any global data until after
271 * init_rtld has returned. It is OK to reference file-scope statics
272 * and string constants, and to call static and global functions.
273 */
274
275 /* Find the auxiliary vector on the stack. */
276 argc = *sp++;
277 argv = (char **) sp;
278 sp += argc + 1; /* Skip over arguments and NULL terminator */
279 env = (char **) sp;
280 while (*sp++ != 0) /* Skip over environment, and NULL terminator */
281 ;
282 aux = (Elf_Auxinfo *) sp;
283
284 /* Digest the auxiliary vector. */
285 for (i = 0; i < AT_COUNT; i++)
286 aux_info[i] = NULL;
287 for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
288 if (auxp->a_type < AT_COUNT)
289 aux_info[auxp->a_type] = auxp;
290 }
291
292 /* Initialize and relocate ourselves. */
293 assert(aux_info[AT_BASE] != NULL);
294 init_rtld((caddr_t) aux_info[AT_BASE]->a_un.a_ptr);
295
296 __progname = obj_rtld.path;
297 argv0 = argv[0] != NULL ? argv[0] : "(null)";
298 environ = env;
299
300 trust = !issetugid();
301
302 ld_bind_now = getenv(LD_ "BIND_NOW");
303 if (trust) {
304 ld_debug = getenv(LD_ "DEBUG");
305 libmap_disable = getenv(LD_ "LIBMAP_DISABLE") != NULL;
306 ld_library_path = getenv(LD_ "LIBRARY_PATH");
307 ld_preload = getenv(LD_ "PRELOAD");
308 }
309 ld_tracing = getenv(LD_ "TRACE_LOADED_OBJECTS");
310
311 if (ld_debug != NULL && *ld_debug != '\0')
312 debug = 1;
313 dbg("%s is initialized, base address = %p", __progname,
314 (caddr_t) aux_info[AT_BASE]->a_un.a_ptr);
315 dbg("RTLD dynamic = %p", obj_rtld.dynamic);
316 dbg("RTLD pltgot = %p", obj_rtld.pltgot);
317
318 /*
319 * Load the main program, or process its program header if it is
320 * already loaded.
321 */
322 if (aux_info[AT_EXECFD] != NULL) { /* Load the main program. */
323 int fd = aux_info[AT_EXECFD]->a_un.a_val;
324 dbg("loading main program");
325 obj_main = map_object(fd, argv0, NULL);
326 close(fd);
327 if (obj_main == NULL)
328 die();
329 } else { /* Main program already loaded. */
330 const Elf_Phdr *phdr;
331 int phnum;
332 caddr_t entry;
333
334 dbg("processing main program's program header");
335 assert(aux_info[AT_PHDR] != NULL);
336 phdr = (const Elf_Phdr *) aux_info[AT_PHDR]->a_un.a_ptr;
337 assert(aux_info[AT_PHNUM] != NULL);
338 phnum = aux_info[AT_PHNUM]->a_un.a_val;
339 assert(aux_info[AT_PHENT] != NULL);
340 assert(aux_info[AT_PHENT]->a_un.a_val == sizeof(Elf_Phdr));
341 assert(aux_info[AT_ENTRY] != NULL);
342 entry = (caddr_t) aux_info[AT_ENTRY]->a_un.a_ptr;
343 if ((obj_main = digest_phdr(phdr, phnum, entry, argv0)) == NULL)
344 die();
345 }
346
347 obj_main->path = xstrdup(argv0);
348 obj_main->mainprog = true;
349
350 /*
351 * Get the actual dynamic linker pathname from the executable if
352 * possible. (It should always be possible.) That ensures that
353 * gdb will find the right dynamic linker even if a non-standard
354 * one is being used.
355 */
356 if (obj_main->interp != NULL &&
357 strcmp(obj_main->interp, obj_rtld.path) != 0) {
358 free(obj_rtld.path);
359 obj_rtld.path = xstrdup(obj_main->interp);
360 __progname = obj_rtld.path;
361 }
362
363 digest_dynamic(obj_main, 0);
364
365 linkmap_add(obj_main);
366 linkmap_add(&obj_rtld);
367
368 /* Link the main program into the list of objects. */
369 *obj_tail = obj_main;
370 obj_tail = &obj_main->next;
371 obj_count++;
372 /* Make sure we don't call the main program's init and fini functions. */
373 obj_main->init = obj_main->fini = (Elf_Addr)NULL;
374
375 /* Initialize a fake symbol for resolving undefined weak references. */
376 sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
377 sym_zero.st_shndx = SHN_UNDEF;
378
379 if (!libmap_disable)
380 libmap_disable = (bool)lm_init();
381
382 dbg("loading LD_PRELOAD libraries");
383 if (load_preload_objects() == -1)
384 die();
385 preload_tail = obj_tail;
386
387 dbg("loading needed objects");
388 if (load_needed_objects(obj_main) == -1)
389 die();
390
391 /* Make a list of all objects loaded at startup. */
392 for (obj = obj_list; obj != NULL; obj = obj->next) {
393 objlist_push_tail(&list_main, obj);
394 obj->refcount++;
395 }
396
397 if (ld_tracing) { /* We're done */
398 trace_loaded_objects(obj_main);
399 exit(0);
400 }
401
402 if (getenv(LD_ "DUMP_REL_PRE") != NULL) {
403 dump_relocations(obj_main);
404 exit (0);
405 }
406
407 /* setup TLS for main thread */
408 dbg("initializing initial thread local storage");
409 STAILQ_FOREACH(entry, &list_main, link) {
410 /*
411 * Allocate all the initial objects out of the static TLS
412 * block even if they didn't ask for it.
413 */
414 allocate_tls_offset(entry->obj);
415 }
416 allocate_initial_tls(obj_list);
417
418 if (relocate_objects(obj_main,
419 ld_bind_now != NULL && *ld_bind_now != '\0', &obj_rtld) == -1)
420 die();
421
422 dbg("doing copy relocations");
423 if (do_copy_relocations(obj_main) == -1)
424 die();
425
426 if (getenv(LD_ "DUMP_REL_POST") != NULL) {
427 dump_relocations(obj_main);
428 exit (0);
429 }
430
431 dbg("initializing key program variables");
432 set_program_var("__progname", argv[0] != NULL ? basename(argv[0]) : "");
433 set_program_var("environ", env);
434
435 dbg("initializing thread locks");
436 lockdflt_init();
437
438 /* Make a list of init functions to call. */
439 objlist_init(&initlist);
440 initlist_add_objects(obj_list, preload_tail, &initlist);
441
442 r_debug_state(NULL, &obj_main->linkmap); /* say hello to gdb! */
443
444 objlist_call_init(&initlist);
445 lockstate = wlock_acquire(rtld_bind_lock);
446 objlist_clear(&initlist);
447 wlock_release(rtld_bind_lock, lockstate);
448
449 dbg("transferring control to program entry point = %p", obj_main->entry);
450
451 /* Return the exit procedure and the program entry point. */
452 *exit_proc = rtld_exit;
453 *objp = obj_main;
454 return (func_ptr_type) obj_main->entry;
455 }
456
457 #endif /* 0 */
458
459 Elf_Addr
460 _rtld_bind(Obj_Entry *obj, Elf_Word reloff)
461 {
462 const Elf_Rel *rel;
463 const Elf_Sym *def;
464 const Obj_Entry *defobj;
465 Elf_Addr *where;
466 Elf_Addr target;
467 int lockstate;
468
469 lockstate = rlock_acquire(rtld_bind_lock);
470 if (obj->pltrel)
471 rel = (const Elf_Rel *) ((caddr_t) obj->pltrel + reloff);
472 else
473 rel = (const Elf_Rel *) ((caddr_t) obj->pltrela + reloff);
474
475 where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
476 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
477 if (def == NULL)
478 die();
479
480 target = (Elf_Addr)(defobj->relocbase + def->st_value);
481
482 dbg("\"%s\" in \"%s\" ==> %p in \"%s\"",
483 defobj->strtab + def->st_name, basename(obj->path),
484 (void *)target, basename(defobj->path));
485
486 /*
487 * Write the new contents for the jmpslot. Note that depending on
488 * architecture, the value which we need to return back to the
489 * lazy binding trampoline may or may not be the target
490 * address. The value returned from reloc_jmpslot() is the value
491 * that the trampoline needs.
492 */
493 target = reloc_jmpslot(where, target, defobj, obj, rel);
494 rlock_release(rtld_bind_lock, lockstate);
495 return target;
496 }
497
498 #if 0
499
500 /*
501 * Error reporting function. Use it like printf. If formats the message
502 * into a buffer, and sets things up so that the next call to dlerror()
503 * will return the message.
504 */
505 void
506 _rtld_error(const char *fmt, ...)
507 {
508 static char buf[512];
509 va_list ap;
510
511 va_start(ap, fmt);
512 vsnprintf(buf, sizeof buf, fmt, ap);
513 error_message = buf;
514 va_end(ap);
515 }
516
517 /*
518 * Return a dynamically-allocated copy of the current error message, if any.
519 */
520 static char *
521 errmsg_save(void)
522 {
523 return error_message == NULL ? NULL : xstrdup(error_message);
524 }
525
526 /*
527 * Restore the current error message from a copy which was previously saved
528 * by errmsg_save(). The copy is freed.
529 */
530 static void
531 errmsg_restore(char *saved_msg)
532 {
533 if (saved_msg == NULL)
534 error_message = NULL;
535 else {
536 _rtld_error("%s", saved_msg);
537 free(saved_msg);
538 }
539 }
540
541 static const char *
542 basename(const char *name)
543 {
544 const char *p = strrchr(name, '/');
545 return p != NULL ? p + 1 : name;
546 }
547
548 static void
549 die(void)
550 {
551 const char *msg = dlerror();
552
553 if (msg == NULL)
554 msg = "Fatal error";
555 errx(1, "%s", msg);
556 }
557
558 #endif /* 0 */
559
560 /*
561 * Process a shared object's DYNAMIC section, and save the important
562 * information in its Obj_Entry structure.
563 */
564 static void
565 digest_dynamic(Obj_Entry *obj, int early)
566 {
567 const Elf_Dyn *dynp;
568 Needed_Entry **needed_tail = &obj->needed;
569 const Elf_Dyn *dyn_rpath = NULL;
570 int plttype = DT_REL;
571
572 obj->bind_now = false;
573 for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; dynp++) {
574 switch (dynp->d_tag) {
575
576 case DT_REL:
577 obj->rel = (const Elf_Rel *) (obj->relocbase + dynp->d_un.d_ptr);
578 break;
579
580 case DT_RELSZ:
581 obj->relsize = dynp->d_un.d_val;
582 break;
583
584 case DT_RELENT:
585 assert(dynp->d_un.d_val == sizeof(Elf_Rel));
586 break;
587
588 case DT_JMPREL:
589 obj->pltrel = (const Elf_Rel *)
590 (obj->relocbase + dynp->d_un.d_ptr);
591 break;
592
593 case DT_PLTRELSZ:
594 obj->pltrelsize = dynp->d_un.d_val;
595 break;
596
597 case DT_RELA:
598 obj->rela = (const Elf_Rela *) (obj->relocbase + dynp->d_un.d_ptr);
599 break;
600
601 case DT_RELASZ:
602 obj->relasize = dynp->d_un.d_val;
603 break;
604
605 case DT_RELAENT:
606 assert(dynp->d_un.d_val == sizeof(Elf_Rela));
607 break;
608
609 case DT_PLTREL:
610 plttype = dynp->d_un.d_val;
611 assert(dynp->d_un.d_val == DT_REL || plttype == DT_RELA);
612 break;
613
614 case DT_SYMTAB:
615 obj->symtab = (const Elf_Sym *)
616 (obj->relocbase + dynp->d_un.d_ptr);
617 break;
618
619 case DT_SYMENT:
620 assert(dynp->d_un.d_val == sizeof(Elf_Sym));
621 break;
622
623 case DT_STRTAB:
624 obj->strtab = (const char *) (obj->relocbase + dynp->d_un.d_ptr);
625 break;
626
627 case DT_STRSZ:
628 obj->strsize = dynp->d_un.d_val;
629 break;
630
631 case DT_HASH:
632 {
633 const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
634 (obj->relocbase + dynp->d_un.d_ptr);
635 obj->nbuckets = hashtab[0];
636 obj->nchains = hashtab[1];
637 obj->buckets = hashtab + 2;
638 obj->chains = obj->buckets + obj->nbuckets;
639 }
640 break;
641
642 case DT_NEEDED:
643 if (!obj->rtld) {
644 Needed_Entry *nep = NEW(Needed_Entry);
645 nep->name = dynp->d_un.d_val;
646 nep->obj = NULL;
647 nep->next = NULL;
648
649 *needed_tail = nep;
650 needed_tail = &nep->next;
651 }
652 break;
653
654 case DT_PLTGOT:
655 obj->pltgot = (Elf_Addr *) (obj->relocbase + dynp->d_un.d_ptr);
656 break;
657
658 case DT_TEXTREL:
659 obj->textrel = true;
660 break;
661
662 case DT_SYMBOLIC:
663 obj->symbolic = true;
664 break;
665
666 case DT_RPATH:
667 case DT_RUNPATH: /* XXX: process separately */
668 /*
669 * We have to wait until later to process this, because we
670 * might not have gotten the address of the string table yet.
671 */
672 dyn_rpath = dynp;
673 break;
674
675 case DT_SONAME:
676 /* Not used by the dynamic linker. */
677 break;
678
679 case DT_INIT:
680 obj->init = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr);
681 break;
682
683 case DT_FINI:
684 obj->fini = (Elf_Addr) (obj->relocbase + dynp->d_un.d_ptr);
685 break;
686
687 case DT_DEBUG:
688 /* XXX - not implemented yet */
689 if (!early)
690 dbg("Filling in DT_DEBUG entry");
691 ((Elf_Dyn*)dynp)->d_un.d_ptr = (Elf_Addr) &r_debug;
692 break;
693
694 case DT_FLAGS:
695 if (dynp->d_un.d_val & DF_ORIGIN) {
696 obj->origin_path = xmalloc(PATH_MAX);
697 if (rtld_dirname(obj->path, obj->origin_path) == -1)
698 die();
699 }
700 if (dynp->d_un.d_val & DF_SYMBOLIC)
701 obj->symbolic = true;
702 if (dynp->d_un.d_val & DF_TEXTREL)
703 obj->textrel = true;
704 if (dynp->d_un.d_val & DF_BIND_NOW)
705 obj->bind_now = true;
706 if (dynp->d_un.d_val & DF_STATIC_TLS)
707 ;
708 break;
709
710 default:
711 if (!early) {
712 dbg("Ignoring d_tag %ld = %#lx", (long)dynp->d_tag,
713 (long)dynp->d_tag);
714 }
715 break;
716 }
717 }
718
719 obj->traced = false;
720
721 if (plttype == DT_RELA) {
722 obj->pltrela = (const Elf_Rela *) obj->pltrel;
723 obj->pltrel = NULL;
724 obj->pltrelasize = obj->pltrelsize;
725 obj->pltrelsize = 0;
726 }
727
728 if (dyn_rpath != NULL)
729 obj->rpath = obj->strtab + dyn_rpath->d_un.d_val;
730 }
731
732 /*
733 * Process a shared object's program header. This is used only for the
734 * main program, when the kernel has already loaded the main program
735 * into memory before calling the dynamic linker. It creates and
736 * returns an Obj_Entry structure.
737 */
738 static Obj_Entry *
739 digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry, const char *path)
740 {
741 Obj_Entry *obj;
742 const Elf_Phdr *phlimit = phdr + phnum;
743 const Elf_Phdr *ph;
744 int nsegs = 0;
745
746 obj = obj_new();
747 for (ph = phdr; ph < phlimit; ph++) {
748 switch (ph->p_type) {
749
750 case PT_PHDR:
751 if ((const Elf_Phdr *)ph->p_vaddr != phdr) {
752 _rtld_error("%s: invalid PT_PHDR", path);
753 return NULL;
754 }
755 obj->phdr = (const Elf_Phdr *) ph->p_vaddr;
756 obj->phsize = ph->p_memsz;
757 break;
758
759 case PT_INTERP:
760 obj->interp = (const char *) ph->p_vaddr;
761 break;
762
763 case PT_LOAD:
764 if (nsegs == 0) { /* First load segment */
765 obj->vaddrbase = trunc_page(ph->p_vaddr);
766 obj->mapbase = (caddr_t) obj->vaddrbase;
767 obj->relocbase = obj->mapbase - obj->vaddrbase;
768 obj->textsize = round_page(ph->p_vaddr + ph->p_memsz) -
769 obj->vaddrbase;
770 } else { /* Last load segment */
771 obj->mapsize = round_page(ph->p_vaddr + ph->p_memsz) -
772 obj->vaddrbase;
773 }
774 nsegs++;
775 break;
776
777 case PT_DYNAMIC:
778 obj->dynamic = (const Elf_Dyn *) ph->p_vaddr;
779 break;
780
781 case PT_TLS:
782 obj->tlsindex = 1;
783 obj->tlssize = ph->p_memsz;
784 obj->tlsalign = ph->p_align;
785 obj->tlsinitsize = ph->p_filesz;
786 obj->tlsinit = (void*) ph->p_vaddr;
787 break;
788 }
789 }
790 if (nsegs < 1) {
791 _rtld_error("%s: too few PT_LOAD segments", path);
792 return NULL;
793 }
794
795 obj->entry = entry;
796 return obj;
797 }
798
799 #if 0
800
801 static Obj_Entry *
802 dlcheck(void *handle)
803 {
804 Obj_Entry *obj;
805
806 for (obj = obj_list; obj != NULL; obj = obj->next)
807 if (obj == (Obj_Entry *) handle)
808 break;
809
810 if (obj == NULL || obj->refcount == 0 || obj->dl_refcount == 0) {
811 _rtld_error("Invalid shared object handle %p", handle);
812 return NULL;
813 }
814 return obj;
815 }
816
817 /*
818 * If the given object is already in the donelist, return true. Otherwise
819 * add the object to the list and return false.
820 */
821 static bool
822 donelist_check(DoneList *dlp, const Obj_Entry *obj)
823 {
824 unsigned int i;
825
826 for (i = 0; i < dlp->num_used; i++)
827 if (dlp->objs[i] == obj)
828 return true;
829 /*
830 * Our donelist allocation should always be sufficient. But if
831 * our threads locking isn't working properly, more shared objects
832 * could have been loaded since we allocated the list. That should
833 * never happen, but we'll handle it properly just in case it does.
834 */
835 if (dlp->num_used < dlp->num_alloc)
836 dlp->objs[dlp->num_used++] = obj;
837 return false;
838 }
839
840 #endif /* 0 */
841
842 /*
843 * Hash function for symbol table lookup. Don't even think about changing
844 * this. It is specified by the System V ABI.
845 */
846 unsigned long
847 elf_hash(const char *name)
848 {
849 const unsigned char *p = (const unsigned char *) name;
850 unsigned long h = 0;
851 unsigned long g;
852
853 while (*p != '\0') {
854 h = (h << 4) + *p++;
855 if ((g = h & 0xf0000000) != 0)
856 h ^= g >> 24;
857 h &= ~g;
858 }
859 return h;
860 }
861
862 #if 0
863
864 /*
865 * Find the library with the given name, and return its full pathname.
866 * The returned string is dynamically allocated. Generates an error
867 * message and returns NULL if the library cannot be found.
868 *
869 * If the second argument is non-NULL, then it refers to an already-
870 * loaded shared object, whose library search path will be searched.
871 *
872 * The search order is:
873 * LD_LIBRARY_PATH
874 * rpath in the referencing file
875 * ldconfig hints
876 * /lib:/usr/lib
877 */
878 static char *
879 find_library(const char *xname, const Obj_Entry *refobj)
880 {
881 char *pathname;
882 char *name;
883
884 if (strchr(xname, '/') != NULL) { /* Hard coded pathname */
885 if (xname[0] != '/' && !trust) {
886 _rtld_error("Absolute pathname required for shared object \"%s\"",
887 xname);
888 return NULL;
889 }
890 return xstrdup(xname);
891 }
892
893 if (libmap_disable || (refobj == NULL) ||
894 (name = lm_find(refobj->path, xname)) == NULL)
895 name = (char *)xname;
896
897 dbg(" Searching for \"%s\"", name);
898
899 if ((pathname = search_library_path(name, ld_library_path)) != NULL ||
900 (refobj != NULL &&
901 (pathname = search_library_path(name, refobj->rpath)) != NULL) ||
902 (pathname = search_library_path(name, gethints())) != NULL ||
903 (pathname = search_library_path(name, STANDARD_LIBRARY_PATH)) != NULL)
904 return pathname;
905
906 if(refobj != NULL && refobj->path != NULL) {
907 _rtld_error("Shared object \"%s\" not found, required by \"%s\"",
908 name, basename(refobj->path));
909 } else {
910 _rtld_error("Shared object \"%s\" not found", name);
911 }
912 return NULL;
913 }
914
915 #endif /* 0 */
916
917 /*
918 * Given a symbol number in a referencing object, find the corresponding
919 * definition of the symbol. Returns a pointer to the symbol, or NULL if
920 * no definition was found. Returns a pointer to the Obj_Entry of the
921 * defining object via the reference parameter DEFOBJ_OUT.
922 */
923 const Elf_Sym *
924 find_symdef(unsigned long symnum, const Obj_Entry *refobj,
925 const Obj_Entry **defobj_out, bool in_plt, SymCache *cache)
926 {
927 const Elf_Sym *ref;
928 const Elf_Sym *def;
929 const Obj_Entry *defobj;
930 const char *name;
931 unsigned long hash;
932
933 /*
934 * If we have already found this symbol, get the information from
935 * the cache.
936 */
937 if (symnum >= refobj->nchains)
938 return NULL; /* Bad object */
939 if (cache != NULL && cache[symnum].sym != NULL) {
940 *defobj_out = cache[symnum].obj;
941 return cache[symnum].sym;
942 }
943
944 ref = refobj->symtab + symnum;
945 name = refobj->strtab + ref->st_name;
946 defobj = NULL;
947
948 /*
949 * We don't have to do a full scale lookup if the symbol is local.
950 * We know it will bind to the instance in this load module; to
951 * which we already have a pointer (ie ref). By not doing a lookup,
952 * we not only improve performance, but it also avoids unresolvable
953 * symbols when local symbols are not in the hash table. This has
954 * been seen with the ia64 toolchain.
955 */
956 if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
957 if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
958 _rtld_error("%s: Bogus symbol table entry %lu", refobj->path,
959 symnum);
960 }
961 hash = elf_hash(name);
962 def = symlook_default(name, hash, refobj, &defobj, in_plt);
963 } else {
964 def = ref;
965 defobj = refobj;
966 }
967
968 /*
969 * If we found no definition and the reference is weak, treat the
970 * symbol as having the value zero.
971 */
972 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
973 def = &sym_zero;
974 defobj = obj_main;
975 }
976
977 if (def != NULL) {
978 *defobj_out = defobj;
979 /* Record the information in the cache to avoid subsequent lookups. */
980 if (cache != NULL) {
981 cache[symnum].sym = def;
982 cache[symnum].obj = defobj;
983 }
984 } else {
985 if (refobj != &obj_rtld)
986 _rtld_error("%s: Undefined symbol \"%s\"", refobj->path, name);
987 }
988 return def;
989 }
990
991 #if 0
992
993 /*
994 * Return the search path from the ldconfig hints file, reading it if
995 * necessary. Returns NULL if there are problems with the hints file,
996 * or if the search path there is empty.
997 */
998 static const char *
999 gethints(void)
1000 {
1001 static char *hints;
1002
1003 if (hints == NULL) {
1004 int fd;
1005 struct elfhints_hdr hdr;
1006 char *p;
1007
1008 /* Keep from trying again in case the hints file is bad. */
1009 hints = "";
1010
1011 if ((fd = open(_PATH_ELF_HINTS, O_RDONLY)) == -1)
1012 return NULL;
1013 if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
1014 hdr.magic != ELFHINTS_MAGIC ||
1015 hdr.version != 1) {
1016 close(fd);
1017 return NULL;
1018 }
1019 p = xmalloc(hdr.dirlistlen + 1);
1020 if (lseek(fd, hdr.strtab + hdr.dirlist, SEEK_SET) == -1 ||
1021 read(fd, p, hdr.dirlistlen + 1) != (ssize_t)hdr.dirlistlen + 1) {
1022 free(p);
1023 close(fd);
1024 return NULL;
1025 }
1026 hints = p;
1027 close(fd);
1028 }
1029 return hints[0] != '\0' ? hints : NULL;
1030 }
1031
1032 static void
1033 init_dag(Obj_Entry *root)
1034 {
1035 DoneList donelist;
1036
1037 donelist_init(&donelist);
1038 init_dag1(root, root, &donelist);
1039 }
1040
1041 static void
1042 init_dag1(Obj_Entry *root, Obj_Entry *obj, DoneList *dlp)
1043 {
1044 const Needed_Entry *needed;
1045
1046 if (donelist_check(dlp, obj))
1047 return;
1048
1049 obj->refcount++;
1050 objlist_push_tail(&obj->dldags, root);
1051 objlist_push_tail(&root->dagmembers, obj);
1052 for (needed = obj->needed; needed != NULL; needed = needed->next)
1053 if (needed->obj != NULL)
1054 init_dag1(root, needed->obj, dlp);
1055 }
1056
1057 /*
1058 * Initialize the dynamic linker. The argument is the address at which
1059 * the dynamic linker has been mapped into memory. The primary task of
1060 * this function is to relocate the dynamic linker.
1061 */
1062 static void
1063 init_rtld(caddr_t mapbase)
1064 {
1065 Obj_Entry objtmp; /* Temporary rtld object */
1066
1067 /*
1068 * Conjure up an Obj_Entry structure for the dynamic linker.
1069 *
1070 * The "path" member can't be initialized yet because string constatns
1071 * cannot yet be acessed. Below we will set it correctly.
1072 */
1073 memset(&objtmp, 0, sizeof(objtmp));
1074 objtmp.path = NULL;
1075 objtmp.rtld = true;
1076 objtmp.mapbase = mapbase;
1077 #ifdef PIC
1078 objtmp.relocbase = mapbase;
1079 #endif
1080 if (RTLD_IS_DYNAMIC()) {
1081 objtmp.dynamic = rtld_dynamic(&objtmp);
1082 digest_dynamic(&objtmp, 1);
1083 assert(objtmp.needed == NULL);
1084 assert(!objtmp.textrel);
1085
1086 /*
1087 * Temporarily put the dynamic linker entry into the object list, so
1088 * that symbols can be found.
1089 */
1090
1091 relocate_objects(&objtmp, true, &objtmp);
1092 }
1093
1094 /* Initialize the object list. */
1095 obj_tail = &obj_list;
1096
1097 /* Now that non-local variables can be accesses, copy out obj_rtld. */
1098 memcpy(&obj_rtld, &objtmp, sizeof(obj_rtld));
1099
1100 /* Replace the path with a dynamically allocated copy. */
1101 obj_rtld.path = xstrdup(PATH_RTLD);
1102
1103 r_debug.r_brk = r_debug_state;
1104 r_debug.r_state = RT_CONSISTENT;
1105 }
1106
1107 /*
1108 * Add the init functions from a needed object list (and its recursive
1109 * needed objects) to "list". This is not used directly; it is a helper
1110 * function for initlist_add_objects(). The write lock must be held
1111 * when this function is called.
1112 */
1113 static void
1114 initlist_add_neededs(Needed_Entry *needed, Objlist *list)
1115 {
1116 /* Recursively process the successor needed objects. */
1117 if (needed->next != NULL)
1118 initlist_add_neededs(needed->next, list);
1119
1120 /* Process the current needed object. */
1121 if (needed->obj != NULL)
1122 initlist_add_objects(needed->obj, &needed->obj->next, list);
1123 }
1124
1125 /*
1126 * Scan all of the DAGs rooted in the range of objects from "obj" to
1127 * "tail" and add their init functions to "list". This recurses over
1128 * the DAGs and ensure the proper init ordering such that each object's
1129 * needed libraries are initialized before the object itself. At the
1130 * same time, this function adds the objects to the global finalization
1131 * list "list_fini" in the opposite order. The write lock must be
1132 * held when this function is called.
1133 */
1134 static void
1135 initlist_add_objects(Obj_Entry *obj, Obj_Entry **tail, Objlist *list)
1136 {
1137 if (obj->init_done)
1138 return;
1139 obj->init_done = true;
1140
1141 /* Recursively process the successor objects. */
1142 if (&obj->next != tail)
1143 initlist_add_objects(obj->next, tail, list);
1144
1145 /* Recursively process the needed objects. */
1146 if (obj->needed != NULL)
1147 initlist_add_neededs(obj->needed, list);
1148
1149 /* Add the object to the init list. */
1150 if (obj->init != (Elf_Addr)NULL)
1151 objlist_push_tail(list, obj);
1152
1153 /* Add the object to the global fini list in the reverse order. */
1154 if (obj->fini != (Elf_Addr)NULL)
1155 objlist_push_head(&list_fini, obj);
1156 }
1157
1158 #ifndef FPTR_TARGET
1159 #define FPTR_TARGET(f) ((Elf_Addr) (f))
1160 #endif
1161
1162 static bool
1163 is_exported(const Elf_Sym *def)
1164 {
1165 Elf_Addr value;
1166 const func_ptr_type *p;
1167
1168 value = (Elf_Addr)(obj_rtld.relocbase + def->st_value);
1169 for (p = exports; *p != NULL; p++)
1170 if (FPTR_TARGET(*p) == value)
1171 return true;
1172 return false;
1173 }
1174
1175 /*
1176 * Given a shared object, traverse its list of needed objects, and load
1177 * each of them. Returns 0 on success. Generates an error message and
1178 * returns -1 on failure.
1179 */
1180 static int
1181 load_needed_objects(Obj_Entry *first)
1182 {
1183 Obj_Entry *obj;
1184
1185 for (obj = first; obj != NULL; obj = obj->next) {
1186 Needed_Entry *needed;
1187
1188 for (needed = obj->needed; needed != NULL; needed = needed->next) {
1189 const char *name = obj->strtab + needed->name;
1190 char *path = find_library(name, obj);
1191
1192 needed->obj = NULL;
1193 if (path == NULL && !ld_tracing)
1194 return -1;
1195
1196 if (path) {
1197 needed->obj = load_object(path);
1198 if (needed->obj == NULL && !ld_tracing)
1199 return -1; /* XXX - cleanup */
1200 }
1201 }
1202 }
1203
1204 return 0;
1205 }
1206
1207 static int
1208 load_preload_objects(void)
1209 {
1210 char *p = ld_preload;
1211 static const char delim[] = " \t:;";
1212
1213 if (p == NULL)
1214 return 0;
1215
1216 p += strspn(p, delim);
1217 while (*p != '\0') {
1218 size_t len = strcspn(p, delim);
1219 char *path;
1220 char savech;
1221
1222 savech = p[len];
1223 p[len] = '\0';
1224 if ((path = find_library(p, NULL)) == NULL)
1225 return -1;
1226 if (load_object(path) == NULL)
1227 return -1; /* XXX - cleanup */
1228 p[len] = savech;
1229 p += len;
1230 p += strspn(p, delim);
1231 }
1232 return 0;
1233 }
1234
1235 /*
1236 * Load a shared object into memory, if it is not already loaded. The
1237 * argument must be a string allocated on the heap. This function assumes
1238 * responsibility for freeing it when necessary.
1239 *
1240 * Returns a pointer to the Obj_Entry for the object. Returns NULL
1241 * on failure.
1242 */
1243 static Obj_Entry *
1244 load_object(char *path)
1245 {
1246 Obj_Entry *obj;
1247 int fd = -1;
1248 struct stat sb;
1249
1250 for (obj = obj_list->next; obj != NULL; obj = obj->next)
1251 if (strcmp(obj->path, path) == 0)
1252 break;
1253
1254 /*
1255 * If we didn't find a match by pathname, open the file and check
1256 * again by device and inode. This avoids false mismatches caused
1257 * by multiple links or ".." in pathnames.
1258 *
1259 * To avoid a race, we open the file and use fstat() rather than
1260 * using stat().
1261 */
1262 if (obj == NULL) {
1263 if ((fd = open(path, O_RDONLY)) == -1) {
1264 _rtld_error("Cannot open \"%s\"", path);
1265 return NULL;
1266 }
1267 if (fstat(fd, &sb) == -1) {
1268 _rtld_error("Cannot fstat \"%s\"", path);
1269 close(fd);
1270 return NULL;
1271 }
1272 for (obj = obj_list->next; obj != NULL; obj = obj->next) {
1273 if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
1274 close(fd);
1275 break;
1276 }
1277 }
1278 }
1279
1280 if (obj == NULL) { /* First use of this object, so we must map it in */
1281 dbg("loading \"%s\"", path);
1282 obj = map_object(fd, path, &sb);
1283 close(fd);
1284 if (obj == NULL) {
1285 free(path);
1286 return NULL;
1287 }
1288
1289 obj->path = path;
1290 digest_dynamic(obj, 0);
1291
1292 *obj_tail = obj;
1293 obj_tail = &obj->next;
1294 obj_count++;
1295 linkmap_add(obj); /* for GDB & dlinfo() */
1296
1297 dbg(" %p .. %p: %s", obj->mapbase,
1298 obj->mapbase + obj->mapsize - 1, obj->path);
1299 if (obj->textrel)
1300 dbg(" WARNING: %s has impure text", obj->path);
1301 } else
1302 free(path);
1303
1304 return obj;
1305 }
1306
1307 static Obj_Entry *
1308 obj_from_addr(const void *addr)
1309 {
1310 Obj_Entry *obj;
1311
1312 for (obj = obj_list; obj != NULL; obj = obj->next) {
1313 if (addr < (void *) obj->mapbase)
1314 continue;
1315 if (addr < (void *) (obj->mapbase + obj->mapsize))
1316 return obj;
1317 }
1318 return NULL;
1319 }
1320
1321 /*
1322 * Call the finalization functions for each of the objects in "list"
1323 * which are unreferenced. All of the objects are expected to have
1324 * non-NULL fini functions.
1325 */
1326 static void
1327 objlist_call_fini(Objlist *list)
1328 {
1329 Objlist_Entry *elm;
1330 char *saved_msg;
1331
1332 /*
1333 * Preserve the current error message since a fini function might
1334 * call into the dynamic linker and overwrite it.
1335 */
1336 saved_msg = errmsg_save();
1337 STAILQ_FOREACH(elm, list, link) {
1338 if (elm->obj->refcount == 0) {
1339 dbg("calling fini function for %s at %p", elm->obj->path,
1340 (void *)elm->obj->fini);
1341 call_initfini_pointer(elm->obj, elm->obj->fini);
1342 }
1343 }
1344 errmsg_restore(saved_msg);
1345 }
1346
1347 /*
1348 * Call the initialization functions for each of the objects in
1349 * "list". All of the objects are expected to have non-NULL init
1350 * functions.
1351 */
1352 static void
1353 objlist_call_init(Objlist *list)
1354 {
1355 Objlist_Entry *elm;
1356 char *saved_msg;
1357
1358 /*
1359 * Preserve the current error message since an init function might
1360 * call into the dynamic linker and overwrite it.
1361 */
1362 saved_msg = errmsg_save();
1363 STAILQ_FOREACH(elm, list, link) {
1364 dbg("calling init function for %s at %p", elm->obj->path,
1365 (void *)elm->obj->init);
1366 call_initfini_pointer(elm->obj, elm->obj->init);
1367 }
1368 errmsg_restore(saved_msg);
1369 }
1370
1371 static void
1372 objlist_clear(Objlist *list)
1373 {
1374 Objlist_Entry *elm;
1375
1376 while (!STAILQ_EMPTY(list)) {
1377 elm = STAILQ_FIRST(list);
1378 STAILQ_REMOVE_HEAD(list, link);
1379 free(elm);
1380 }
1381 }
1382
1383 static Objlist_Entry *
1384 objlist_find(Objlist *list, const Obj_Entry *obj)
1385 {
1386 Objlist_Entry *elm;
1387
1388 STAILQ_FOREACH(elm, list, link)
1389 if (elm->obj == obj)
1390 return elm;
1391 return NULL;
1392 }
1393
1394 static void
1395 objlist_init(Objlist *list)
1396 {
1397 STAILQ_INIT(list);
1398 }
1399
1400 static void
1401 objlist_push_head(Objlist *list, Obj_Entry *obj)
1402 {
1403 Objlist_Entry *elm;
1404
1405 elm = NEW(Objlist_Entry);
1406 elm->obj = obj;
1407 STAILQ_INSERT_HEAD(list, elm, link);
1408 }
1409
1410 static void
1411 objlist_push_tail(Objlist *list, Obj_Entry *obj)
1412 {
1413 Objlist_Entry *elm;
1414
1415 elm = NEW(Objlist_Entry);
1416 elm->obj = obj;
1417 STAILQ_INSERT_TAIL(list, elm, link);
1418 }
1419
1420 static void
1421 objlist_remove(Objlist *list, Obj_Entry *obj)
1422 {
1423 Objlist_Entry *elm;
1424
1425 if ((elm = objlist_find(list, obj)) != NULL) {
1426 STAILQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
1427 free(elm);
1428 }
1429 }
1430
1431 /*
1432 * Remove all of the unreferenced objects from "list".
1433 */
1434 static void
1435 objlist_remove_unref(Objlist *list)
1436 {
1437 Objlist newlist;
1438 Objlist_Entry *elm;
1439
1440 STAILQ_INIT(&newlist);
1441 while (!STAILQ_EMPTY(list)) {
1442 elm = STAILQ_FIRST(list);
1443 STAILQ_REMOVE_HEAD(list, link);
1444 if (elm->obj->refcount == 0)
1445 free(elm);
1446 else
1447 STAILQ_INSERT_TAIL(&newlist, elm, link);
1448 }
1449 *list = newlist;
1450 }
1451
1452 #endif /* 0 */
1453
1454 /*
1455 * Relocate newly-loaded shared objects. The argument is a pointer to
1456 * the Obj_Entry for the first such object. All objects from the first
1457 * to the end of the list of objects are relocated. Returns 0 on success,
1458 * or -1 on failure.
1459 */
1460 static int
1461 relocate_objects(Obj_Entry *first, bool bind_now, Obj_Entry *rtldobj)
1462 {
1463 Obj_Entry *obj;
1464
1465 for (obj = first; obj != NULL; obj = obj->next) {
1466 if (obj != rtldobj)
1467 dbg("relocating \"%s\"", obj->path);
1468 if (obj->nbuckets == 0 || obj->nchains == 0 || obj->buckets == NULL ||
1469 obj->symtab == NULL || obj->strtab == NULL) {
1470 _rtld_error("%s: Shared object has no run-time symbol table",
1471 obj->path);
1472 return -1;
1473 }
1474
1475 if (obj->textrel) {
1476 /* There are relocations to the write-protected text segment. */
1477 if (mprotect(obj->mapbase, obj->textsize,
1478 PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
1479 _rtld_error("%s: Cannot write-enable text segment: %s",
1480 obj->path, strerror(errno));
1481 return -1;
1482 }
1483 }
1484
1485 /* Process the non-PLT relocations. */
1486 if (reloc_non_plt(obj, rtldobj))
1487 return -1;
1488
1489 if (obj->textrel) { /* Re-protected the text segment. */
1490 if (mprotect(obj->mapbase, obj->textsize,
1491 PROT_READ|PROT_EXEC) == -1) {
1492 _rtld_error("%s: Cannot write-protect text segment: %s",
1493 obj->path, strerror(errno));
1494 return -1;
1495 }
1496 }
1497
1498 /* Process the PLT relocations. */
1499 if (reloc_plt(obj) == -1)
1500 return -1;
1501 /* Relocate the jump slots if we are doing immediate binding. */
1502 if (obj->bind_now || bind_now)
1503 if (reloc_jmpslots(obj) == -1)
1504 return -1;
1505
1506
1507 /*
1508 * Set up the magic number and version in the Obj_Entry. These
1509 * were checked in the crt1.o from the original ElfKit, so we
1510 * set them for backward compatibility.
1511 */
1512 obj->magic = RTLD_MAGIC;
1513 obj->version = RTLD_VERSION;
1514
1515 /* Set the special PLT or GOT entries. */
1516 init_pltgot(obj);
1517 }
1518
1519 return 0;
1520 }
1521
1522 #if 0
1523
1524 /*
1525 * Cleanup procedure. It will be called (by the atexit mechanism) just
1526 * before the process exits.
1527 */
1528 static void
1529 rtld_exit(void)
1530 {
1531 Obj_Entry *obj;
1532
1533 dbg("rtld_exit()");
1534 /* Clear all the reference counts so the fini functions will be called. */
1535 for (obj = obj_list; obj != NULL; obj = obj->next)
1536 obj->refcount = 0;
1537 objlist_call_fini(&list_fini);
1538 /* No need to remove the items from the list, since we are exiting. */
1539 if (!libmap_disable)
1540 lm_fini();
1541 }
1542
1543 static void *
1544 path_enumerate(const char *path, path_enum_proc callback, void *arg)
1545 {
1546 #ifdef COMPAT_32BIT
1547 const char *trans;
1548 #endif
1549 if (path == NULL)
1550 return (NULL);
1551
1552 path += strspn(path, ":;");
1553 while (*path != '\0') {
1554 size_t len;
1555 char *res;
1556
1557 len = strcspn(path, ":;");
1558 #ifdef COMPAT_32BIT
1559 trans = lm_findn(NULL, path, len);
1560 if (trans)
1561 res = callback(trans, strlen(trans), arg);
1562 else
1563 #endif
1564 res = callback(path, len, arg);
1565
1566 if (res != NULL)
1567 return (res);
1568
1569 path += len;
1570 path += strspn(path, ":;");
1571 }
1572
1573 return (NULL);
1574 }
1575
1576 struct try_library_args {
1577 const char *name;
1578 size_t namelen;
1579 char *buffer;
1580 size_t buflen;
1581 };
1582
1583 static void *
1584 try_library_path(const char *dir, size_t dirlen, void *param)
1585 {
1586 struct try_library_args *arg;
1587
1588 arg = param;
1589 if (*dir == '/' || trust) {
1590 char *pathname;
1591
1592 if (dirlen + 1 + arg->namelen + 1 > arg->buflen)
1593 return (NULL);
1594
1595 pathname = arg->buffer;
1596 strncpy(pathname, dir, dirlen);
1597 pathname[dirlen] = '/';
1598 strcpy(pathname + dirlen + 1, arg->name);
1599
1600 dbg(" Trying \"%s\"", pathname);
1601 if (access(pathname, F_OK) == 0) { /* We found it */
1602 pathname = xmalloc(dirlen + 1 + arg->namelen + 1);
1603 strcpy(pathname, arg->buffer);
1604 return (pathname);
1605 }
1606 }
1607 return (NULL);
1608 }
1609
1610 static char *
1611 search_library_path(const char *name, const char *path)
1612 {
1613 char *p;
1614 struct try_library_args arg;
1615
1616 if (path == NULL)
1617 return NULL;
1618
1619 arg.name = name;
1620 arg.namelen = strlen(name);
1621 arg.buffer = xmalloc(PATH_MAX);
1622 arg.buflen = PATH_MAX;
1623
1624 p = path_enumerate(path, try_library_path, &arg);
1625
1626 free(arg.buffer);
1627
1628 return (p);
1629 }
1630
1631 int
1632 dlclose(void *handle)
1633 {
1634 Obj_Entry *root;
1635 int lockstate;
1636
1637 lockstate = wlock_acquire(rtld_bind_lock);
1638 root = dlcheck(handle);
1639 if (root == NULL) {
1640 wlock_release(rtld_bind_lock, lockstate);
1641 return -1;
1642 }
1643
1644 /* Unreference the object and its dependencies. */
1645 root->dl_refcount--;
1646
1647 unref_dag(root);
1648
1649 if (root->refcount == 0) {
1650 /*
1651 * The object is no longer referenced, so we must unload it.
1652 * First, call the fini functions with no locks held.
1653 */
1654 wlock_release(rtld_bind_lock, lockstate);
1655 objlist_call_fini(&list_fini);
1656 lockstate = wlock_acquire(rtld_bind_lock);
1657 objlist_remove_unref(&list_fini);
1658
1659 /* Finish cleaning up the newly-unreferenced objects. */
1660 GDB_STATE(RT_DELETE,&root->linkmap);
1661 unload_object(root);
1662 GDB_STATE(RT_CONSISTENT,NULL);
1663 }
1664 wlock_release(rtld_bind_lock, lockstate);
1665 return 0;
1666 }
1667
1668 const char *
1669 dlerror(void)
1670 {
1671 char *msg = error_message;
1672 error_message = NULL;
1673 return msg;
1674 }
1675
1676 /*
1677 * This function is deprecated and has no effect.
1678 */
1679 void
1680 dllockinit(void *context,
1681 void *(*lock_create)(void *context),
1682 void (*rlock_acquire)(void *lock),
1683 void (*wlock_acquire)(void *lock),
1684 void (*lock_release)(void *lock),
1685 void (*lock_destroy)(void *lock),
1686 void (*context_destroy)(void *context))
1687 {
1688 static void *cur_context;
1689 static void (*cur_context_destroy)(void *);
1690
1691 /* Just destroy the context from the previous call, if necessary. */
1692 if (cur_context_destroy != NULL)
1693 cur_context_destroy(cur_context);
1694 cur_context = context;
1695 cur_context_destroy = context_destroy;
1696 }
1697
1698 void *
1699 dlopen(const char *name, int mode)
1700 {
1701 Obj_Entry **old_obj_tail;
1702 Obj_Entry *obj;
1703 Objlist initlist;
1704 int result, lockstate;
1705
1706 ld_tracing = (mode & RTLD_TRACE) == 0 ? NULL : "1";
1707 if (ld_tracing != NULL)
1708 environ = (char **)*get_program_var_addr("environ");
1709
1710 objlist_init(&initlist);
1711
1712 lockstate = wlock_acquire(rtld_bind_lock);
1713 GDB_STATE(RT_ADD,NULL);
1714
1715 old_obj_tail = obj_tail;
1716 obj = NULL;
1717 if (name == NULL) {
1718 obj = obj_main;
1719 obj->refcount++;
1720 } else {
1721 char *path = find_library(name, obj_main);
1722 if (path != NULL)
1723 obj = load_object(path);
1724 }
1725
1726 if (obj) {
1727 obj->dl_refcount++;
1728 if (mode & RTLD_GLOBAL && objlist_find(&list_global, obj) == NULL)
1729 objlist_push_tail(&list_global, obj);
1730 mode &= RTLD_MODEMASK;
1731 if (*old_obj_tail != NULL) { /* We loaded something new. */
1732 assert(*old_obj_tail == obj);
1733
1734 result = load_needed_objects(obj);
1735 if (result != -1 && ld_tracing)
1736 goto trace;
1737
1738 if (result == -1 ||
1739 (init_dag(obj), relocate_objects(obj, mode == RTLD_NOW,
1740 &obj_rtld)) == -1) {
1741 obj->dl_refcount--;
1742 unref_dag(obj);
1743 if (obj->refcount == 0)
1744 unload_object(obj);
1745 obj = NULL;
1746 } else {
1747 /* Make list of init functions to call. */
1748 initlist_add_objects(obj, &obj->next, &initlist);
1749 }
1750 } else {
1751
1752 /* Bump the reference counts for objects on this DAG. */
1753 ref_dag(obj);
1754
1755 if (ld_tracing)
1756 goto trace;
1757 }
1758 }
1759
1760 GDB_STATE(RT_CONSISTENT,obj ? &obj->linkmap : NULL);
1761
1762 /* Call the init functions with no locks held. */
1763 wlock_release(rtld_bind_lock, lockstate);
1764 objlist_call_init(&initlist);
1765 lockstate = wlock_acquire(rtld_bind_lock);
1766 objlist_clear(&initlist);
1767 wlock_release(rtld_bind_lock, lockstate);
1768 return obj;
1769 trace:
1770 trace_loaded_objects(obj);
1771 wlock_release(rtld_bind_lock, lockstate);
1772 exit(0);
1773 }
1774
1775 void *
1776 dlsym(void *handle, const char *name)
1777 {
1778 const Obj_Entry *obj;
1779 unsigned long hash;
1780 const Elf_Sym *def;
1781 const Obj_Entry *defobj;
1782 int lockstate;
1783
1784 hash = elf_hash(name);
1785 def = NULL;
1786 defobj = NULL;
1787
1788 lockstate = rlock_acquire(rtld_bind_lock);
1789 if (handle == NULL || handle == RTLD_NEXT ||
1790 handle == RTLD_DEFAULT || handle == RTLD_SELF) {
1791 void *retaddr;
1792
1793 retaddr = __builtin_return_address(0); /* __GNUC__ only */
1794 if ((obj = obj_from_addr(retaddr)) == NULL) {
1795 _rtld_error("Cannot determine caller's shared object");
1796 rlock_release(rtld_bind_lock, lockstate);
1797 return NULL;
1798 }
1799 if (handle == NULL) { /* Just the caller's shared object. */
1800 def = symlook_obj(name, hash, obj, true);
1801 defobj = obj;
1802 } else if (handle == RTLD_NEXT || /* Objects after caller's */
1803 handle == RTLD_SELF) { /* ... caller included */
1804 if (handle == RTLD_NEXT)
1805 obj = obj->next;
1806 for (; obj != NULL; obj = obj->next) {
1807 if ((def = symlook_obj(name, hash, obj, true)) != NULL) {
1808 defobj = obj;
1809 break;
1810 }
1811 }
1812 } else {
1813 assert(handle == RTLD_DEFAULT);
1814 def = symlook_default(name, hash, obj, &defobj, true);
1815 }
1816 } else {
1817 if ((obj = dlcheck(handle)) == NULL) {
1818 rlock_release(rtld_bind_lock, lockstate);
1819 return NULL;
1820 }
1821
1822 if (obj->mainprog) {
1823 DoneList donelist;
1824
1825 /* Search main program and all libraries loaded by it. */
1826 donelist_init(&donelist);
1827 def = symlook_list(name, hash, &list_main, &defobj, true,
1828 &donelist);
1829 } else {
1830 /*
1831 * XXX - This isn't correct. The search should include the whole
1832 * DAG rooted at the given object.
1833 */
1834 def = symlook_obj(name, hash, obj, true);
1835 defobj = obj;
1836 }
1837 }
1838
1839 if (def != NULL) {
1840 rlock_release(rtld_bind_lock, lockstate);
1841
1842 /*
1843 * The value required by the caller is derived from the value
1844 * of the symbol. For the ia64 architecture, we need to
1845 * construct a function descriptor which the caller can use to
1846 * call the function with the right 'gp' value. For other
1847 * architectures and for non-functions, the value is simply
1848 * the relocated value of the symbol.
1849 */
1850 if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
1851 return make_function_pointer(def, defobj);
1852 else
1853 return defobj->relocbase + def->st_value;
1854 }
1855
1856 _rtld_error("Undefined symbol \"%s\"", name);
1857 rlock_release(rtld_bind_lock, lockstate);
1858 return NULL;
1859 }
1860
1861 int
1862 dladdr(const void *addr, Dl_info *info)
1863 {
1864 const Obj_Entry *obj;
1865 const Elf_Sym *def;
1866 void *symbol_addr;
1867 unsigned long symoffset;
1868 int lockstate;
1869
1870 lockstate = rlock_acquire(rtld_bind_lock);
1871 obj = obj_from_addr(addr);
1872 if (obj == NULL) {
1873 _rtld_error("No shared object contains address");
1874 rlock_release(rtld_bind_lock, lockstate);
1875 return 0;
1876 }
1877 info->dli_fname = obj->path;
1878 info->dli_fbase = obj->mapbase;
1879 info->dli_saddr = (void *)0;
1880 info->dli_sname = NULL;
1881
1882 /*
1883 * Walk the symbol list looking for the symbol whose address is
1884 * closest to the address sent in.
1885 */
1886 for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
1887 def = obj->symtab + symoffset;
1888
1889 /*
1890 * For skip the symbol if st_shndx is either SHN_UNDEF or
1891 * SHN_COMMON.
1892 */
1893 if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
1894 continue;
1895
1896 /*
1897 * If the symbol is greater than the specified address, or if it
1898 * is further away from addr than the current nearest symbol,
1899 * then reject it.
1900 */
1901 symbol_addr = obj->relocbase + def->st_value;
1902 if (symbol_addr > addr || symbol_addr < info->dli_saddr)
1903 continue;
1904
1905 /* Update our idea of the nearest symbol. */
1906 info->dli_sname = obj->strtab + def->st_name;
1907 info->dli_saddr = symbol_addr;
1908
1909 /* Exact match? */
1910 if (info->dli_saddr == addr)
1911 break;
1912 }
1913 rlock_release(rtld_bind_lock, lockstate);
1914 return 1;
1915 }
1916
1917 int
1918 dlinfo(void *handle, int request, void *p)
1919 {
1920 const Obj_Entry *obj;
1921 int error, lockstate;
1922
1923 lockstate = rlock_acquire(rtld_bind_lock);
1924
1925 if (handle == NULL || handle == RTLD_SELF) {
1926 void *retaddr;
1927
1928 retaddr = __builtin_return_address(0); /* __GNUC__ only */
1929 if ((obj = obj_from_addr(retaddr)) == NULL)
1930 _rtld_error("Cannot determine caller's shared object");
1931 } else
1932 obj = dlcheck(handle);
1933
1934 if (obj == NULL) {
1935 rlock_release(rtld_bind_lock, lockstate);
1936 return (-1);
1937 }
1938
1939 error = 0;
1940 switch (request) {
1941 case RTLD_DI_LINKMAP:
1942 *((struct link_map const **)p) = &obj->linkmap;
1943 break;
1944 case RTLD_DI_ORIGIN:
1945 error = rtld_dirname(obj->path, p);
1946 break;
1947
1948 case RTLD_DI_SERINFOSIZE:
1949 case RTLD_DI_SERINFO:
1950 error = do_search_info(obj, request, (struct dl_serinfo *)p);
1951 break;
1952
1953 default:
1954 _rtld_error("Invalid request %d passed to dlinfo()", request);
1955 error = -1;
1956 }
1957
1958 rlock_release(rtld_bind_lock, lockstate);
1959
1960 return (error);
1961 }
1962
1963 struct fill_search_info_args {
1964 int request;
1965 unsigned int flags;
1966 Dl_serinfo *serinfo;
1967 Dl_serpath *serpath;
1968 char *strspace;
1969 };
1970
1971 static void *
1972 fill_search_info(const char *dir, size_t dirlen, void *param)
1973 {
1974 struct fill_search_info_args *arg;
1975
1976 arg = param;
1977
1978 if (arg->request == RTLD_DI_SERINFOSIZE) {
1979 arg->serinfo->dls_cnt ++;
1980 arg->serinfo->dls_size += dirlen + 1;
1981 } else {
1982 struct dl_serpath *s_entry;
1983
1984 s_entry = arg->serpath;
1985 s_entry->dls_name = arg->strspace;
1986 s_entry->dls_flags = arg->flags;
1987
1988 strncpy(arg->strspace, dir, dirlen);
1989 arg->strspace[dirlen] = '\0';
1990
1991 arg->strspace += dirlen + 1;
1992 arg->serpath++;
1993 }
1994
1995 return (NULL);
1996 }
1997
1998 static int
1999 do_search_info(const Obj_Entry *obj, int request, struct dl_serinfo *info)
2000 {
2001 struct dl_serinfo _info;
2002 struct fill_search_info_args args;
2003
2004 args.request = RTLD_DI_SERINFOSIZE;
2005 args.serinfo = &_info;
2006
2007 _info.dls_size = __offsetof(struct dl_serinfo, dls_serpath);
2008 _info.dls_cnt = 0;
2009
2010 path_enumerate(ld_library_path, fill_search_info, &args);
2011 path_enumerate(obj->rpath, fill_search_info, &args);
2012 path_enumerate(gethints(), fill_search_info, &args);
2013 path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args);
2014
2015
2016 if (request == RTLD_DI_SERINFOSIZE) {
2017 info->dls_size = _info.dls_size;
2018 info->dls_cnt = _info.dls_cnt;
2019 return (0);
2020 }
2021
2022 if (info->dls_cnt != _info.dls_cnt || info->dls_size != _info.dls_size) {
2023 _rtld_error("Uninitialized Dl_serinfo struct passed to dlinfo()");
2024 return (-1);
2025 }
2026
2027 args.request = RTLD_DI_SERINFO;
2028 args.serinfo = info;
2029 args.serpath = &info->dls_serpath[0];
2030 args.strspace = (char *)&info->dls_serpath[_info.dls_cnt];
2031
2032 args.flags = LA_SER_LIBPATH;
2033 if (path_enumerate(ld_library_path, fill_search_info, &args) != NULL)
2034 return (-1);
2035
2036 args.flags = LA_SER_RUNPATH;
2037 if (path_enumerate(obj->rpath, fill_search_info, &args) != NULL)
2038 return (-1);
2039
2040 args.flags = LA_SER_CONFIG;
2041 if (path_enumerate(gethints(), fill_search_info, &args) != NULL)
2042 return (-1);
2043
2044 args.flags = LA_SER_DEFAULT;
2045 if (path_enumerate(STANDARD_LIBRARY_PATH, fill_search_info, &args) != NULL)
2046 return (-1);
2047 return (0);
2048 }
2049
2050 static int
2051 rtld_dirname(const char *path, char *bname)
2052 {
2053 const char *endp;
2054
2055 /* Empty or NULL string gets treated as "." */
2056 if (path == NULL || *path == '\0') {
2057 bname[0] = '.';
2058 bname[1] = '\0';
2059 return (0);
2060 }
2061
2062 /* Strip trailing slashes */
2063 endp = path + strlen(path) - 1;
2064 while (endp > path && *endp == '/')
2065 endp--;
2066
2067 /* Find the start of the dir */
2068 while (endp > path && *endp != '/')
2069 endp--;
2070
2071 /* Either the dir is "/" or there are no slashes */
2072 if (endp == path) {
2073 bname[0] = *endp == '/' ? '/' : '.';
2074 bname[1] = '\0';
2075 return (0);
2076 } else {
2077 do {
2078 endp--;
2079 } while (endp > path && *endp == '/');
2080 }
2081
2082 if (endp - path + 2 > PATH_MAX)
2083 {
2084 _rtld_error("Filename is too long: %s", path);
2085 return(-1);
2086 }
2087
2088 strncpy(bname, path, endp - path + 1);
2089 bname[endp - path + 1] = '\0';
2090 return (0);
2091 }
2092
2093 static void
2094 linkmap_add(Obj_Entry *obj)
2095 {
2096 struct link_map *l = &obj->linkmap;
2097 struct link_map *prev;
2098
2099 obj->linkmap.l_name = obj->path;
2100 obj->linkmap.l_addr = obj->mapbase;
2101 obj->linkmap.l_ld = obj->dynamic;
2102 #ifdef __mips__
2103 /* GDB needs load offset on MIPS to use the symbols */
2104 obj->linkmap.l_offs = obj->relocbase;
2105 #endif
2106
2107 if (r_debug.r_map == NULL) {
2108 r_debug.r_map = l;
2109 return;
2110 }
2111
2112 /*
2113 * Scan to the end of the list, but not past the entry for the
2114 * dynamic linker, which we want to keep at the very end.
2115 */
2116 for (prev = r_debug.r_map;
2117 prev->l_next != NULL && prev->l_next != &obj_rtld.linkmap;
2118 prev = prev->l_next)
2119 ;
2120
2121 /* Link in the new entry. */
2122 l->l_prev = prev;
2123 l->l_next = prev->l_next;
2124 if (l->l_next != NULL)
2125 l->l_next->l_prev = l;
2126 prev->l_next = l;
2127 }
2128
2129 static void
2130 linkmap_delete(Obj_Entry *obj)
2131 {
2132 struct link_map *l = &obj->linkmap;
2133
2134 if (l->l_prev == NULL) {
2135 if ((r_debug.r_map = l->l_next) != NULL)
2136 l->l_next->l_prev = NULL;
2137 return;
2138 }
2139
2140 if ((l->l_prev->l_next = l->l_next) != NULL)
2141 l->l_next->l_prev = l->l_prev;
2142 }
2143
2144 /*
2145 * Function for the debugger to set a breakpoint on to gain control.
2146 *
2147 * The two parameters allow the debugger to easily find and determine
2148 * what the runtime loader is doing and to whom it is doing it.
2149 *
2150 * When the loadhook trap is hit (r_debug_state, set at program
2151 * initialization), the arguments can be found on the stack:
2152 *
2153 * +8 struct link_map *m
2154 * +4 struct r_debug *rd
2155 * +0 RetAddr
2156 */
2157 void
2158 r_debug_state(struct r_debug* rd, struct link_map *m)
2159 {
2160 }
2161
2162 /*
2163 * Get address of the pointer variable in the main program.
2164 */
2165 static const void **
2166 get_program_var_addr(const char *name)
2167 {
2168 const Obj_Entry *obj;
2169 unsigned long hash;
2170
2171 hash = elf_hash(name);
2172 for (obj = obj_main; obj != NULL; obj = obj->next) {
2173 const Elf_Sym *def;
2174
2175 if ((def = symlook_obj(name, hash, obj, false)) != NULL) {
2176 const void **addr;
2177
2178 addr = (const void **)(obj->relocbase + def->st_value);
2179 return addr;
2180 }
2181 }
2182 return NULL;
2183 }
2184
2185 /*
2186 * Set a pointer variable in the main program to the given value. This
2187 * is used to set key variables such as "environ" before any of the
2188 * init functions are called.
2189 */
2190 static void
2191 set_program_var(const char *name, const void *value)
2192 {
2193 const void **addr;
2194
2195 if ((addr = get_program_var_addr(name)) != NULL) {
2196 dbg("\"%s\": *%p <-- %p", name, addr, value);
2197 *addr = value;
2198 }
2199 }
2200
2201 /*
2202 * Given a symbol name in a referencing object, find the corresponding
2203 * definition of the symbol. Returns a pointer to the symbol, or NULL if
2204 * no definition was found. Returns a pointer to the Obj_Entry of the
2205 * defining object via the reference parameter DEFOBJ_OUT.
2206 */
2207 static const Elf_Sym *
2208 symlook_default(const char *name, unsigned long hash,
2209 const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
2210 {
2211 DoneList donelist;
2212 const Elf_Sym *def;
2213 const Elf_Sym *symp;
2214 const Obj_Entry *obj;
2215 const Obj_Entry *defobj;
2216 const Objlist_Entry *elm;
2217 def = NULL;
2218 defobj = NULL;
2219 donelist_init(&donelist);
2220
2221 /* Look first in the referencing object if linked symbolically. */
2222 if (refobj->symbolic && !donelist_check(&donelist, refobj)) {
2223 symp = symlook_obj(name, hash, refobj, in_plt);
2224 if (symp != NULL) {
2225 def = symp;
2226 defobj = refobj;
2227 }
2228 }
2229
2230 /* Search all objects loaded at program start up. */
2231 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
2232 symp = symlook_list(name, hash, &list_main, &obj, in_plt, &donelist);
2233 if (symp != NULL &&
2234 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2235 def = symp;
2236 defobj = obj;
2237 }
2238 }
2239
2240 /* Search all DAGs whose roots are RTLD_GLOBAL objects. */
2241 STAILQ_FOREACH(elm, &list_global, link) {
2242 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
2243 break;
2244 symp = symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt,
2245 &donelist);
2246 if (symp != NULL &&
2247 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2248 def = symp;
2249 defobj = obj;
2250 }
2251 }
2252
2253 /* Search all dlopened DAGs containing the referencing object. */
2254 STAILQ_FOREACH(elm, &refobj->dldags, link) {
2255 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
2256 break;
2257 symp = symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt,
2258 &donelist);
2259 if (symp != NULL &&
2260 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
2261 def = symp;
2262 defobj = obj;
2263 }
2264 }
2265
2266 /*
2267 * Search the dynamic linker itself, and possibly resolve the
2268 * symbol from there. This is how the application links to
2269 * dynamic linker services such as dlopen. Only the values listed
2270 * in the "exports" array can be resolved from the dynamic linker.
2271 */
2272 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
2273 symp = symlook_obj(name, hash, &obj_rtld, in_plt);
2274 if (symp != NULL && is_exported(symp)) {
2275 def = symp;
2276 defobj = &obj_rtld;
2277 }
2278 }
2279
2280 if (def != NULL)
2281 *defobj_out = defobj;
2282 return def;
2283 }
2284
2285 static const Elf_Sym *
2286 symlook_list(const char *name, unsigned long hash, Objlist *objlist,
2287 const Obj_Entry **defobj_out, bool in_plt, DoneList *dlp)
2288 {
2289 const Elf_Sym *symp;
2290 const Elf_Sym *def;
2291 const Obj_Entry *defobj;
2292 const Objlist_Entry *elm;
2293
2294 def = NULL;
2295 defobj = NULL;
2296 STAILQ_FOREACH(elm, objlist, link) {
2297 if (donelist_check(dlp, elm->obj))
2298 continue;
2299 if ((symp = symlook_obj(name, hash, elm->obj, in_plt)) != NULL) {
2300 if (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK) {
2301 def = symp;
2302 defobj = elm->obj;
2303 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
2304 break;
2305 }
2306 }
2307 }
2308 if (def != NULL)
2309 *defobj_out = defobj;
2310 return def;
2311 }
2312
2313 #endif /* 0 */
2314
2315 /*
2316 * Search the symbol table of a single shared object for a symbol of
2317 * the given name. Returns a pointer to the symbol, or NULL if no
2318 * definition was found.
2319 *
2320 * The symbol's hash value is passed in for efficiency reasons; that
2321 * eliminates many recomputations of the hash value.
2322 */
2323 const Elf_Sym *
2324 symlook_obj(const char *name, unsigned long hash, const Obj_Entry *obj,
2325 bool in_plt)
2326 {
2327 if (obj->buckets != NULL) {
2328 unsigned long symnum = obj->buckets[hash % obj->nbuckets];
2329
2330 while (symnum != STN_UNDEF) {
2331 const Elf_Sym *symp;
2332 const char *strp;
2333
2334 if (symnum >= obj->nchains)
2335 return NULL; /* Bad object */
2336 symp = obj->symtab + symnum;
2337 strp = obj->strtab + symp->st_name;
2338
2339 if (name[0] == strp[0] && strcmp(name, strp) == 0)
2340 return symp->st_shndx != SHN_UNDEF ||
2341 (!in_plt && symp->st_value != 0 &&
2342 ELF_ST_TYPE(symp->st_info) == STT_FUNC) ? symp : NULL;
2343
2344 symnum = obj->chains[symnum];
2345 }
2346 }
2347 return NULL;
2348 }
2349
2350 #if 0
2351
2352 static void
2353 trace_loaded_objects(Obj_Entry *obj)
2354 {
2355 char *fmt1, *fmt2, *fmt, *main_local, *list_containers;
2356 int c;
2357
2358 if ((main_local = getenv(LD_ "TRACE_LOADED_OBJECTS_PROGNAME")) == NULL)
2359 main_local = "";
2360
2361 if ((fmt1 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT1")) == NULL)
2362 fmt1 = "\t%o => %p (%x)\n";
2363
2364 if ((fmt2 = getenv(LD_ "TRACE_LOADED_OBJECTS_FMT2")) == NULL)
2365 fmt2 = "\t%o (%x)\n";
2366
2367 list_containers = getenv(LD_ "TRACE_LOADED_OBJECTS_ALL");
2368
2369 for (; obj; obj = obj->next) {
2370 Needed_Entry *needed;
2371 char *name, *path;
2372 bool is_lib;
2373
2374 if (list_containers && obj->needed != NULL)
2375 printf("%s:\n", obj->path);
2376 for (needed = obj->needed; needed; needed = needed->next) {
2377 if (needed->obj != NULL) {
2378 if (needed->obj->traced && !list_containers)
2379 continue;
2380 needed->obj->traced = true;
2381 path = needed->obj->path;
2382 } else
2383 path = "not found";
2384
2385 name = (char *)obj->strtab + needed->name;
2386 is_lib = strncmp(name, "lib", 3) == 0; /* XXX - bogus */
2387
2388 fmt = is_lib ? fmt1 : fmt2;
2389 while ((c = *fmt++) != '\0') {
2390 switch (c) {
2391 default:
2392 putchar(c);
2393 continue;
2394 case '\\':
2395 switch (c = *fmt) {
2396 case '\0':
2397 continue;
2398 case 'n':
2399 putchar('\n');
2400 break;
2401 case 't':
2402 putchar('\t');
2403 break;
2404 }
2405 break;
2406 case '%':
2407 switch (c = *fmt) {
2408 case '\0':
2409 continue;
2410 case '%':
2411 default:
2412 putchar(c);
2413 break;
2414 case 'A':
2415 printf("%s", main_local);
2416 break;
2417 case 'a':
2418 printf("%s", obj_main->path);
2419 break;
2420 case 'o':
2421 printf("%s", name);
2422 break;
2423 #if 0
2424 case 'm':
2425 printf("%d", sodp->sod_major);
2426 break;
2427 case 'n':
2428 printf("%d", sodp->sod_minor);
2429 break;
2430 #endif
2431 case 'p':
2432 printf("%s", path);
2433 break;
2434 case 'x':
2435 printf("%p", needed->obj ? needed->obj->mapbase : 0);
2436 break;
2437 }
2438 break;
2439 }
2440 ++fmt;
2441 }
2442 }
2443 }
2444 }
2445
2446 /*
2447 * Unload a dlopened object and its dependencies from memory and from
2448 * our data structures. It is assumed that the DAG rooted in the
2449 * object has already been unreferenced, and that the object has a
2450 * reference count of 0.
2451 */
2452 static void
2453 unload_object(Obj_Entry *root)
2454 {
2455 Obj_Entry *obj;
2456 Obj_Entry **linkp;
2457
2458 assert(root->refcount == 0);
2459
2460 /*
2461 * Pass over the DAG removing unreferenced objects from
2462 * appropriate lists.
2463 */
2464 unlink_object(root);
2465
2466 /* Unmap all objects that are no longer referenced. */
2467 linkp = &obj_list->next;
2468 while ((obj = *linkp) != NULL) {
2469 if (obj->refcount == 0) {
2470 dbg("unloading \"%s\"", obj->path);
2471 munmap(obj->mapbase, obj->mapsize);
2472 linkmap_delete(obj);
2473 *linkp = obj->next;
2474 obj_count--;
2475 obj_free(obj);
2476 } else
2477 linkp = &obj->next;
2478 }
2479 obj_tail = linkp;
2480 }
2481
2482 static void
2483 unlink_object(Obj_Entry *root)
2484 {
2485 Objlist_Entry *elm;
2486
2487 if (root->refcount == 0) {
2488 /* Remove the object from the RTLD_GLOBAL list. */
2489 objlist_remove(&list_global, root);
2490
2491 /* Remove the object from all objects' DAG lists. */
2492 STAILQ_FOREACH(elm, &root->dagmembers , link) {
2493 objlist_remove(&elm->obj->dldags, root);
2494 if (elm->obj != root)
2495 unlink_object(elm->obj);
2496 }
2497 }
2498 }
2499
2500 static void
2501 ref_dag(Obj_Entry *root)
2502 {
2503 Objlist_Entry *elm;
2504
2505 STAILQ_FOREACH(elm, &root->dagmembers , link)
2506 elm->obj->refcount++;
2507 }
2508
2509 static void
2510 unref_dag(Obj_Entry *root)
2511 {
2512 Objlist_Entry *elm;
2513
2514 STAILQ_FOREACH(elm, &root->dagmembers , link)
2515 elm->obj->refcount--;
2516 }
2517
2518 /*
2519 * Common code for MD __tls_get_addr().
2520 */
2521 void *
2522 tls_get_addr_common(Elf_Addr** dtvp, int index, size_t offset)
2523 {
2524 Elf_Addr* dtv = *dtvp;
2525
2526 /* Check dtv generation in case new modules have arrived */
2527 if (dtv[0] != tls_dtv_generation) {
2528 Elf_Addr* newdtv;
2529 int to_copy;
2530
2531 newdtv = calloc(1, (tls_max_index + 2) * sizeof(Elf_Addr));
2532 to_copy = dtv[1];
2533 if (to_copy > tls_max_index)
2534 to_copy = tls_max_index;
2535 memcpy(&newdtv[2], &dtv[2], to_copy * sizeof(Elf_Addr));
2536 newdtv[0] = tls_dtv_generation;
2537 newdtv[1] = tls_max_index;
2538 free(dtv);
2539 *dtvp = newdtv;
2540 }
2541
2542 /* Dynamically allocate module TLS if necessary */
2543 if (!dtv[index + 1])
2544 dtv[index + 1] = (Elf_Addr)allocate_module_tls(index);
2545
2546 return (void*) (dtv[index + 1] + offset);
2547 }
2548
2549 /* XXX not sure what variants to use for arm. */
2550
2551 #if defined(__ia64__) || defined(__alpha__) || defined(__powerpc__)
2552
2553 /*
2554 * Allocate Static TLS using the Variant I method.
2555 */
2556 void *
2557 allocate_tls(Obj_Entry *objs, void *oldtls, size_t tcbsize, size_t tcbalign)
2558 {
2559 Obj_Entry *obj;
2560 size_t size;
2561 char *tls;
2562 Elf_Addr *dtv, *olddtv;
2563 Elf_Addr addr;
2564 int i;
2565
2566 size = tls_static_space;
2567
2568 tls = malloc(size);
2569 dtv = malloc((tls_max_index + 2) * sizeof(Elf_Addr));
2570
2571 *(Elf_Addr**) tls = dtv;
2572
2573 dtv[0] = tls_dtv_generation;
2574 dtv[1] = tls_max_index;
2575
2576 if (oldtls) {
2577 /*
2578 * Copy the static TLS block over whole.
2579 */
2580 memcpy(tls + tcbsize, oldtls + tcbsize, tls_static_space - tcbsize);
2581
2582 /*
2583 * If any dynamic TLS blocks have been created tls_get_addr(),
2584 * move them over.
2585 */
2586 olddtv = *(Elf_Addr**) oldtls;
2587 for (i = 0; i < olddtv[1]; i++) {
2588 if (olddtv[i+2] < (Elf_Addr)oldtls ||
2589 olddtv[i+2] > (Elf_Addr)oldtls + tls_static_space) {
2590 dtv[i+2] = olddtv[i+2];
2591 olddtv[i+2] = 0;
2592 }
2593 }
2594
2595 /*
2596 * We assume that all tls blocks are allocated with the same
2597 * size and alignment.
2598 */
2599 free_tls(oldtls, tcbsize, tcbalign);
2600 } else {
2601 for (obj = objs; obj; obj = obj->next) {
2602 if (obj->tlsoffset) {
2603 addr = (Elf_Addr)tls + obj->tlsoffset;
2604 memset((void*) (addr + obj->tlsinitsize),
2605 0, obj->tlssize - obj->tlsinitsize);
2606 if (obj->tlsinit)
2607 memcpy((void*) addr, obj->tlsinit,
2608 obj->tlsinitsize);
2609 dtv[obj->tlsindex + 1] = addr;
2610 } else if (obj->tlsindex) {
2611 dtv[obj->tlsindex + 1] = 0;
2612 }
2613 }
2614 }
2615
2616 return tls;
2617 }
2618
2619 void
2620 free_tls(void *tls, size_t tcbsize, size_t tcbalign)
2621 {
2622 size_t size;
2623 Elf_Addr* dtv;
2624 int dtvsize, i;
2625 Elf_Addr tlsstart, tlsend;
2626
2627 /*
2628 * Figure out the size of the initial TLS block so that we can
2629 * find stuff which __tls_get_addr() allocated dynamically.
2630 */
2631 size = tls_static_space;
2632
2633 dtv = ((Elf_Addr**)tls)[0];
2634 dtvsize = dtv[1];
2635 tlsstart = (Elf_Addr) tls;
2636 tlsend = tlsstart + size;
2637 for (i = 0; i < dtvsize; i++) {
2638 if (dtv[i+2] < tlsstart || dtv[i+2] > tlsend) {
2639 free((void*) dtv[i+2]);
2640 }
2641 }
2642
2643 free((void*) tlsstart);
2644 }
2645
2646 #endif
2647
2648 #if defined(__i386__) || defined(__amd64__) || defined(__sparc64__) || \
2649 defined(__arm__)
2650
2651 /*
2652 * Allocate Static TLS using the Variant II method.
2653 */
2654 void *
2655 allocate_tls(Obj_Entry *objs, void *oldtls, size_t tcbsize, size_t tcbalign)
2656 {
2657 Obj_Entry *obj;
2658 size_t size;
2659 char *tls;
2660 Elf_Addr *dtv, *olddtv;
2661 Elf_Addr segbase, oldsegbase, addr;
2662 int i;
2663
2664 size = round(tls_static_space, tcbalign);
2665
2666 assert(tcbsize >= 2*sizeof(Elf_Addr));
2667 tls = malloc(size + tcbsize);
2668 dtv = malloc((tls_max_index + 2) * sizeof(Elf_Addr));
2669
2670 segbase = (Elf_Addr)(tls + size);
2671 ((Elf_Addr*)segbase)[0] = segbase;
2672 ((Elf_Addr*)segbase)[1] = (Elf_Addr) dtv;
2673
2674 dtv[0] = tls_dtv_generation;
2675 dtv[1] = tls_max_index;
2676
2677 if (oldtls) {
2678 /*
2679 * Copy the static TLS block over whole.
2680 */
2681 oldsegbase = (Elf_Addr) oldtls;
2682 memcpy((void *)(segbase - tls_static_space),
2683 (const void *)(oldsegbase - tls_static_space),
2684 tls_static_space);
2685
2686 /*
2687 * If any dynamic TLS blocks have been created tls_get_addr(),
2688 * move them over.
2689 */
2690 olddtv = ((Elf_Addr**)oldsegbase)[1];
2691 for (i = 0; i < olddtv[1]; i++) {
2692 if (olddtv[i+2] < oldsegbase - size || olddtv[i+2] > oldsegbase) {
2693 dtv[i+2] = olddtv[i+2];
2694 olddtv[i+2] = 0;
2695 }
2696 }
2697
2698 /*
2699 * We assume that this block was the one we created with
2700 * allocate_initial_tls().
2701 */
2702 free_tls(oldtls, 2*sizeof(Elf_Addr), sizeof(Elf_Addr));
2703 } else {
2704 for (obj = objs; obj; obj = obj->next) {
2705 if (obj->tlsoffset) {
2706 addr = segbase - obj->tlsoffset;
2707 memset((void*) (addr + obj->tlsinitsize),
2708 0, obj->tlssize - obj->tlsinitsize);
2709 if (obj->tlsinit)
2710 memcpy((void*) addr, obj->tlsinit, obj->tlsinitsize);
2711 dtv[obj->tlsindex + 1] = addr;
2712 } else if (obj->tlsindex) {
2713 dtv[obj->tlsindex + 1] = 0;
2714 }
2715 }
2716 }
2717
2718 return (void*) segbase;
2719 }
2720
2721 void
2722 free_tls(void *tls, size_t tcbsize, size_t tcbalign)
2723 {
2724 size_t size;
2725 Elf_Addr* dtv;
2726 int dtvsize, i;
2727 Elf_Addr tlsstart, tlsend;
2728
2729 /*
2730 * Figure out the size of the initial TLS block so that we can
2731 * find stuff which ___tls_get_addr() allocated dynamically.
2732 */
2733 size = round(tls_static_space, tcbalign);
2734
2735 dtv = ((Elf_Addr**)tls)[1];
2736 dtvsize = dtv[1];
2737 tlsend = (Elf_Addr) tls;
2738 tlsstart = tlsend - size;
2739 for (i = 0; i < dtvsize; i++) {
2740 if (dtv[i+2] < tlsstart || dtv[i+2] > tlsend) {
2741 free((void*) dtv[i+2]);
2742 }
2743 }
2744
2745 free((void*) tlsstart);
2746 }
2747
2748 #endif
2749
2750 /*
2751 * Allocate TLS block for module with given index.
2752 */
2753 void *
2754 allocate_module_tls(int index)
2755 {
2756 Obj_Entry* obj;
2757 char* p;
2758
2759 for (obj = obj_list; obj; obj = obj->next) {
2760 if (obj->tlsindex == index)
2761 break;
2762 }
2763 if (!obj) {
2764 _rtld_error("Can't find module with TLS index %d", index);
2765 die();
2766 }
2767
2768 p = malloc(obj->tlssize);
2769 memcpy(p, obj->tlsinit, obj->tlsinitsize);
2770 memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize);
2771
2772 return p;
2773 }
2774
2775 bool
2776 allocate_tls_offset(Obj_Entry *obj)
2777 {
2778 size_t off;
2779
2780 if (obj->tls_done)
2781 return true;
2782
2783 if (obj->tlssize == 0) {
2784 obj->tls_done = true;
2785 return true;
2786 }
2787
2788 if (obj->tlsindex == 1)
2789 off = calculate_first_tls_offset(obj->tlssize, obj->tlsalign);
2790 else
2791 off = calculate_tls_offset(tls_last_offset, tls_last_size,
2792 obj->tlssize, obj->tlsalign);
2793
2794 /*
2795 * If we have already fixed the size of the static TLS block, we
2796 * must stay within that size. When allocating the static TLS, we
2797 * leave a small amount of space spare to be used for dynamically
2798 * loading modules which use static TLS.
2799 */
2800 if (tls_static_space) {
2801 if (calculate_tls_end(off, obj->tlssize) > tls_static_space)
2802 return false;
2803 }
2804
2805 tls_last_offset = obj->tlsoffset = off;
2806 tls_last_size = obj->tlssize;
2807 obj->tls_done = true;
2808
2809 return true;
2810 }
2811
2812 void *
2813 _rtld_allocate_tls(void *oldtls, size_t tcbsize, size_t tcbalign)
2814 {
2815 return allocate_tls(obj_list, oldtls, tcbsize, tcbalign);
2816 }
2817
2818 void
2819 _rtld_free_tls(void *tcb, size_t tcbsize, size_t tcbalign)
2820 {
2821 free_tls(tcb, tcbsize, tcbalign);
2822 }
2823
2824 #endif /* 0 */