rtld.c revision 1.113 1 /* $NetBSD: rtld.c,v 1.113 2007/05/18 21:44:08 christos Exp $ */
2
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root (at) ihack.net>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by John Polstra.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * Dynamic linker for ELF.
37 *
38 * John Polstra <jdp (at) polstra.com>.
39 */
40
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: rtld.c,v 1.113 2007/05/18 21:44:08 christos Exp $");
44 #endif /* not lint */
45
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <sys/param.h>
55 #include <sys/mman.h>
56 #include <dirent.h>
57
58 #include <ctype.h>
59
60 #include <dlfcn.h>
61 #include "debug.h"
62 #include "rtld.h"
63
64 #if !defined(lint)
65 #include "sysident.h"
66 #endif
67
68 /*
69 * Function declarations.
70 */
71 static void _rtld_init(caddr_t, caddr_t, const char *);
72 static void _rtld_exit(void);
73
74 Elf_Addr _rtld(Elf_Addr *, Elf_Addr);
75
76
77 /*
78 * Data declarations.
79 */
80 static char *error_message; /* Message for dlopen(), or NULL */
81
82 struct r_debug _rtld_debug; /* for GDB; */
83 bool _rtld_trust; /* False for setuid and setgid programs */
84 Obj_Entry *_rtld_objlist; /* Head of linked list of shared objects */
85 Obj_Entry **_rtld_objtail; /* Link field of last object in list */
86 Obj_Entry *_rtld_objmain; /* The main program shared object */
87 Obj_Entry _rtld_objself; /* The dynamic linker shared object */
88 char *_rtld_path = _PATH_RTLD;
89 Elf_Sym _rtld_sym_zero; /* For resolving undefined weak refs. */
90 int _rtld_pagesz; /* Page size, as provided by kernel */
91
92 Search_Path *_rtld_default_paths;
93 Search_Path *_rtld_paths;
94
95 Library_Xform *_rtld_xforms;
96
97 /*
98 * Global declarations normally provided by crt0.
99 */
100 char *__progname;
101 char **environ;
102
103 #if defined(RTLD_DEBUG)
104 #ifndef __sh__
105 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
106 #else /* 32-bit SuperH */
107 register Elf_Addr *_GLOBAL_OFFSET_TABLE_ asm("r12");
108 #endif
109 #endif /* RTLD_DEBUG */
110 extern Elf_Dyn _DYNAMIC;
111
112 static void _rtld_call_fini_functions(Obj_Entry *);
113 static void _rtld_call_init_functions(Obj_Entry *);
114 static Obj_Entry *_rtld_dlcheck(void *);
115 static void _rtld_init_dag(Obj_Entry *);
116 static void _rtld_init_dag1(Obj_Entry *, Obj_Entry *);
117 static void _rtld_objlist_remove(Objlist *, Obj_Entry *);
118 static void _rtld_unload_object(Obj_Entry *, bool);
119 static void _rtld_unref_dag(Obj_Entry *);
120 static Obj_Entry *_rtld_obj_from_addr(const void *);
121
122 static void
123 _rtld_call_fini_functions(Obj_Entry *first)
124 {
125 Obj_Entry *obj;
126
127 for (obj = first; obj != NULL; obj = obj->next)
128 if (obj->fini != NULL)
129 (*obj->fini)();
130 }
131
132 static void
133 _rtld_call_init_functions(Obj_Entry *first)
134 {
135
136 if (first != NULL) {
137 _rtld_call_init_functions(first->next);
138 if (first->init != NULL)
139 (*first->init)();
140 }
141 }
142
143 /*
144 * Initialize the dynamic linker. The argument is the address at which
145 * the dynamic linker has been mapped into memory. The primary task of
146 * this function is to create an Obj_Entry for the dynamic linker and
147 * to resolve the PLT relocation for platforms that need it (those that
148 * define __HAVE_FUNCTION_DESCRIPTORS
149 */
150 static void
151 _rtld_init(caddr_t mapbase, caddr_t relocbase, const char *argv0)
152 {
153
154 /* Conjure up an Obj_Entry structure for the dynamic linker. */
155 _rtld_objself.path = _rtld_path;
156 _rtld_objself.pathlen = strlen(_rtld_path);
157 _rtld_objself.rtld = true;
158 _rtld_objself.mapbase = mapbase;
159 _rtld_objself.relocbase = relocbase;
160 _rtld_objself.dynamic = (Elf_Dyn *) &_DYNAMIC;
161
162 _rtld_digest_dynamic(_rtld_path, &_rtld_objself);
163 assert(!_rtld_objself.needed);
164 #if !defined(__hppa__)
165 assert(!_rtld_objself.pltrel && !_rtld_objself.pltrela);
166 #else
167 _rtld_relocate_plt_objects(&_rtld_objself);
168 #endif
169 #if !defined(__mips__) && !defined(__hppa__)
170 assert(!_rtld_objself.pltgot);
171 #endif
172 #if !defined(__arm__) && !defined(__mips__) && !defined(__sh__)
173 /* ARM, MIPS and SH{3,5} have a bogus DT_TEXTREL. */
174 assert(!_rtld_objself.textrel);
175 #endif
176
177 _rtld_add_paths(argv0, &_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH);
178
179 /*
180 * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
181 */
182 _rtld_objlist = &_rtld_objself;
183
184 /* Make the object list empty again. */
185 _rtld_objlist = NULL;
186 _rtld_objtail = &_rtld_objlist;
187
188 _rtld_debug.r_brk = _rtld_debug_state;
189 _rtld_debug.r_state = RT_CONSISTENT;
190 }
191
192 /*
193 * Cleanup procedure. It will be called (by the atexit() mechanism) just
194 * before the process exits.
195 */
196 static void
197 _rtld_exit(void)
198 {
199
200 dbg(("rtld_exit()"));
201
202 _rtld_call_fini_functions(_rtld_objlist->next);
203 }
204
205 /*
206 * Main entry point for dynamic linking. The argument is the stack
207 * pointer. The stack is expected to be laid out as described in the
208 * SVR4 ABI specification, Intel 386 Processor Supplement. Specifically,
209 * the stack pointer points to a word containing ARGC. Following that
210 * in the stack is a null-terminated sequence of pointers to argument
211 * strings. Then comes a null-terminated sequence of pointers to
212 * environment strings. Finally, there is a sequence of "auxiliary
213 * vector" entries.
214 *
215 * This function returns the entry point for the main program, the dynamic
216 * linker's exit procedure in sp[0], and a pointer to the main object in
217 * sp[1].
218 */
219 Elf_Addr
220 _rtld(Elf_Addr *sp, Elf_Addr relocbase)
221 {
222 const AuxInfo *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
223 *pAUX_phent, *pAUX_phnum, *pAUX_euid, *pAUX_egid,
224 *pAUX_ruid, *pAUX_rgid;
225 const AuxInfo *pAUX_pagesz;
226 char **env;
227 const AuxInfo *aux;
228 const AuxInfo *auxp;
229 Elf_Addr *const osp = sp;
230 bool bind_now = 0;
231 const char *ld_bind_now;
232 const char **argv;
233 long argc;
234 const char **real___progname;
235 const Obj_Entry **real___mainprog_obj;
236 char ***real_environ;
237 #if defined(RTLD_DEBUG)
238 int i = 0;
239 #endif
240
241 /*
242 * On entry, the dynamic linker itself has not been relocated yet.
243 * Be very careful not to reference any global data until after
244 * _rtld_init has returned. It is OK to reference file-scope statics
245 * and string constants, and to call static and global functions.
246 */
247 /* Find the auxiliary vector on the stack. */
248 /* first Elf_Word reserved to address of exit routine */
249 #if defined(RTLD_DEBUG)
250 debug = 1;
251 dbg(("sp = %p, argc = %ld, argv = %p <%s> relocbase %p", sp,
252 (long)sp[2], &sp[3], (char *) sp[3], (void *)relocbase));
253 dbg(("got is at %p, dynamic is at %p", _GLOBAL_OFFSET_TABLE_,
254 &_DYNAMIC));
255 dbg(("_ctype_ is %p", _ctype_));
256 #endif
257
258 sp += 2; /* skip over return argument space */
259 argv = (const char **) &sp[1];
260 argc = *(long *)sp;
261 sp += 2 + argc; /* Skip over argc, arguments, and NULL
262 * terminator */
263 env = (char **) sp;
264 while (*sp++ != 0) { /* Skip over environment, and NULL terminator */
265 #if defined(RTLD_DEBUG)
266 dbg(("env[%d] = %p %s", i++, (void *)sp[-1], (char *)sp[-1]));
267 #endif
268 }
269 aux = (const AuxInfo *) sp;
270
271 pAUX_base = pAUX_entry = pAUX_execfd = NULL;
272 pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
273 pAUX_euid = pAUX_ruid = pAUX_egid = pAUX_rgid = NULL;
274 pAUX_pagesz = NULL;
275
276 /* Digest the auxiliary vector. */
277 for (auxp = aux; auxp->a_type != AT_NULL; ++auxp) {
278 switch (auxp->a_type) {
279 case AT_BASE:
280 pAUX_base = auxp;
281 break;
282 case AT_ENTRY:
283 pAUX_entry = auxp;
284 break;
285 case AT_EXECFD:
286 pAUX_execfd = auxp;
287 break;
288 case AT_PHDR:
289 pAUX_phdr = auxp;
290 break;
291 case AT_PHENT:
292 pAUX_phent = auxp;
293 break;
294 case AT_PHNUM:
295 pAUX_phnum = auxp;
296 break;
297 #ifdef AT_EUID
298 case AT_EUID:
299 pAUX_euid = auxp;
300 break;
301 case AT_RUID:
302 pAUX_ruid = auxp;
303 break;
304 case AT_EGID:
305 pAUX_egid = auxp;
306 break;
307 case AT_RGID:
308 pAUX_rgid = auxp;
309 break;
310 #endif
311 case AT_PAGESZ:
312 pAUX_pagesz = auxp;
313 break;
314 }
315 }
316
317 /* Initialize and relocate ourselves. */
318 if (pAUX_base == NULL) {
319 _rtld_error("Bad pAUX_base");
320 _rtld_die();
321 }
322 assert(pAUX_pagesz != NULL);
323 _rtld_pagesz = (int)pAUX_pagesz->a_v;
324 _rtld_init((caddr_t)pAUX_base->a_v, (caddr_t)relocbase, argv[0]);
325
326 __progname = _rtld_objself.path;
327 environ = env;
328
329 _rtld_trust = ((pAUX_euid ? (uid_t)pAUX_euid->a_v : geteuid()) ==
330 (pAUX_ruid ? (uid_t)pAUX_ruid->a_v : getuid())) &&
331 ((pAUX_egid ? (gid_t)pAUX_egid->a_v : getegid()) ==
332 (pAUX_rgid ? (gid_t)pAUX_rgid->a_v : getgid()));
333
334 ld_bind_now = getenv("LD_BIND_NOW");
335 if (ld_bind_now != NULL && *ld_bind_now != '\0')
336 bind_now = true;
337 if (_rtld_trust) {
338 #ifdef DEBUG
339 const char *ld_debug = getenv("LD_DEBUG");
340 #ifdef RTLD_DEBUG
341 debug = 0;
342 #endif
343 if (ld_debug != NULL && *ld_debug != '\0')
344 debug = 1;
345 #endif
346 _rtld_add_paths(argv[0], &_rtld_paths, getenv("LD_LIBRARY_PATH"));
347 } else {
348 unsetenv("LD_DEBUG");
349 unsetenv("LD_LIBRARY_PATH");
350 }
351 _rtld_process_hints(argv[0], &_rtld_paths, &_rtld_xforms,
352 _PATH_LD_HINTS);
353 dbg(("dynamic linker is initialized, mapbase=%p, relocbase=%p",
354 _rtld_objself.mapbase, _rtld_objself.relocbase));
355
356 /*
357 * Load the main program, or process its program header if it is
358 * already loaded.
359 */
360 if (pAUX_execfd != NULL) { /* Load the main program. */
361 int fd = pAUX_execfd->a_v;
362 const char *obj_name = argv[0] ? argv[0] : "main program";
363 dbg(("loading main program"));
364 _rtld_objmain = _rtld_map_object(obj_name, fd, NULL);
365 close(fd);
366 if (_rtld_objmain == NULL)
367 _rtld_die();
368 } else { /* Main program already loaded. */
369 const Elf_Phdr *phdr;
370 int phnum;
371 caddr_t entry;
372
373 dbg(("processing main program's program header"));
374 assert(pAUX_phdr != NULL);
375 phdr = (const Elf_Phdr *) pAUX_phdr->a_v;
376 assert(pAUX_phnum != NULL);
377 phnum = pAUX_phnum->a_v;
378 assert(pAUX_phent != NULL);
379 assert(pAUX_phent->a_v == sizeof(Elf_Phdr));
380 assert(pAUX_entry != NULL);
381 entry = (caddr_t) pAUX_entry->a_v;
382 _rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
383 _rtld_objmain->path = xstrdup(argv[0] ? argv[0] :
384 "main program");
385 _rtld_objmain->pathlen = strlen(_rtld_objmain->path);
386 }
387
388 _rtld_objmain->mainprog = true;
389
390 /*
391 * Get the actual dynamic linker pathname from the executable if
392 * possible. (It should always be possible.) That ensures that
393 * gdb will find the right dynamic linker even if a non-standard
394 * one is being used.
395 */
396 if (_rtld_objmain->interp != NULL &&
397 strcmp(_rtld_objmain->interp, _rtld_objself.path) != 0)
398 _rtld_objself.path = xstrdup(_rtld_objmain->interp);
399 dbg(("actual dynamic linker is %s", _rtld_objself.path));
400
401 _rtld_digest_dynamic(argv[0], _rtld_objmain);
402
403 /* Link the main program into the list of objects. */
404 *_rtld_objtail = _rtld_objmain;
405 _rtld_objtail = &_rtld_objmain->next;
406
407 _rtld_linkmap_add(_rtld_objmain);
408 _rtld_linkmap_add(&_rtld_objself);
409
410 ++_rtld_objmain->refcount;
411 _rtld_objmain->mainref = 1;
412 _rtld_objlist_add(&_rtld_list_main, _rtld_objmain);
413
414 /* Initialize a fake symbol for resolving undefined weak references. */
415 _rtld_sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
416 _rtld_sym_zero.st_shndx = SHN_ABS;
417
418 if (_rtld_trust) {
419 /*
420 * Pre-load user-specified objects after the main program
421 * but before any shared object dependencies.
422 */
423 dbg(("preloading objects"));
424 if (_rtld_preload(getenv("LD_PRELOAD")) == -1)
425 _rtld_die();
426 } else
427 unsetenv("LD_PRELOAD");
428
429 dbg(("loading needed objects"));
430 if (_rtld_load_needed_objects(_rtld_objmain, RTLD_MAIN) == -1)
431 _rtld_die();
432
433 dbg(("relocating objects"));
434 if (_rtld_relocate_objects(_rtld_objmain, bind_now) == -1)
435 _rtld_die();
436
437 dbg(("doing copy relocations"));
438 if (_rtld_do_copy_relocations(_rtld_objmain) == -1)
439 _rtld_die();
440
441 /*
442 * Set the __progname, environ and, __mainprog_obj before
443 * calling anything that might use them.
444 */
445 real___progname = _rtld_objmain_sym("__progname");
446 if (real___progname) {
447 if (argv[0] != NULL) {
448 if ((*real___progname = strrchr(argv[0], '/')) == NULL)
449 (*real___progname) = argv[0];
450 else
451 (*real___progname)++;
452 } else {
453 (*real___progname) = NULL;
454 }
455 }
456 real_environ = _rtld_objmain_sym("environ");
457 if (real_environ)
458 *real_environ = environ;
459 /*
460 * Set __mainprog_obj for old binaries.
461 */
462 real___mainprog_obj = _rtld_objmain_sym("__mainprog_obj");
463 if (real___mainprog_obj)
464 *real___mainprog_obj = _rtld_objmain;
465
466 dbg(("calling _init functions"));
467 _rtld_call_init_functions(_rtld_objmain->next);
468
469 dbg(("control at program entry point = %p, obj = %p, exit = %p",
470 _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
471
472 /*
473 * Return with the entry point and the exit procedure in at the top
474 * of stack.
475 */
476
477 _rtld_debug_state(); /* say hello to gdb! */
478
479 ((void **) osp)[0] = _rtld_exit;
480 ((void **) osp)[1] = _rtld_objmain;
481 return (Elf_Addr) _rtld_objmain->entry;
482 }
483
484 void
485 _rtld_die(void)
486 {
487 const char *msg = dlerror();
488
489 if (msg == NULL)
490 msg = "Fatal error";
491 xerrx(1, "%s", msg);
492 }
493
494 static Obj_Entry *
495 _rtld_dlcheck(void *handle)
496 {
497 Obj_Entry *obj;
498
499 for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
500 if (obj == (Obj_Entry *) handle)
501 break;
502
503 if (obj == NULL || obj->dl_refcount == 0) {
504 xwarnx("Invalid shared object handle %p", handle);
505 return NULL;
506 }
507 return obj;
508 }
509
510 static void
511 _rtld_init_dag(Obj_Entry *root)
512 {
513
514 _rtld_init_dag1(root, root);
515 }
516
517 static void
518 _rtld_init_dag1(Obj_Entry *root, Obj_Entry *obj)
519 {
520 const Needed_Entry *needed;
521
522 if (!obj->mainref) {
523 if (_rtld_objlist_find(&obj->dldags, root))
524 return;
525 rdbg(("add %p (%s) to %p (%s) DAG", obj, obj->path, root,
526 root->path));
527 _rtld_objlist_add(&obj->dldags, root);
528 _rtld_objlist_add(&root->dagmembers, obj);
529 }
530 for (needed = obj->needed; needed != NULL; needed = needed->next)
531 if (needed->obj != NULL)
532 _rtld_init_dag1(root, needed->obj);
533 }
534
535 /*
536 * Note, this is called only for objects loaded by dlopen().
537 */
538 static void
539 _rtld_unload_object(Obj_Entry *root, bool do_fini_funcs)
540 {
541
542 _rtld_unref_dag(root);
543 if (root->refcount == 0) { /* We are finished with some objects. */
544 Obj_Entry *obj;
545 Obj_Entry **linkp;
546 Objlist_Entry *elm;
547
548 /* Finalize objects that are about to be unmapped. */
549 if (do_fini_funcs)
550 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
551 if (obj->refcount == 0 && obj->fini != NULL)
552 (*obj->fini)();
553
554 /* Remove the DAG from all objects' DAG lists. */
555 SIMPLEQ_FOREACH(elm, &root->dagmembers, link)
556 _rtld_objlist_remove(&elm->obj->dldags, root);
557
558 /* Remove the DAG from the RTLD_GLOBAL list. */
559 if (root->globalref) {
560 root->globalref = 0;
561 _rtld_objlist_remove(&_rtld_list_global, root);
562 }
563
564 /* Unmap all objects that are no longer referenced. */
565 linkp = &_rtld_objlist->next;
566 while ((obj = *linkp) != NULL) {
567 if (obj->refcount == 0) {
568 #ifdef RTLD_DEBUG
569 dbg(("unloading \"%s\"", obj->path));
570 #endif
571 munmap(obj->mapbase, obj->mapsize);
572 _rtld_objlist_remove(&_rtld_list_global, obj);
573 _rtld_linkmap_delete(obj);
574 *linkp = obj->next;
575 _rtld_obj_free(obj);
576 } else
577 linkp = &obj->next;
578 }
579 _rtld_objtail = linkp;
580 }
581 }
582
583 static void
584 _rtld_unref_dag(Obj_Entry *root)
585 {
586
587 assert(root);
588 assert(root->refcount != 0);
589 --root->refcount;
590 if (root->refcount == 0) {
591 const Needed_Entry *needed;
592
593 for (needed = root->needed; needed != NULL;
594 needed = needed->next) {
595 if (needed->obj != NULL)
596 _rtld_unref_dag(needed->obj);
597 }
598 }
599 }
600
601 __strong_alias(__dlclose,dlclose)
602 int
603 dlclose(void *handle)
604 {
605 Obj_Entry *root = _rtld_dlcheck(handle);
606
607 if (root == NULL)
608 return -1;
609
610 _rtld_debug.r_state = RT_DELETE;
611 _rtld_debug_state();
612
613 --root->dl_refcount;
614 _rtld_unload_object(root, true);
615
616 _rtld_debug.r_state = RT_CONSISTENT;
617 _rtld_debug_state();
618
619 return 0;
620 }
621
622 __strong_alias(__dlerror,dlerror)
623 char *
624 dlerror(void)
625 {
626 char *msg = error_message;
627
628 error_message = NULL;
629 return msg;
630 }
631
632 __strong_alias(__dlopen,dlopen)
633 void *
634 dlopen(const char *name, int mode)
635 {
636 Obj_Entry **old_obj_tail = _rtld_objtail;
637 Obj_Entry *obj = NULL;
638
639 _rtld_debug.r_state = RT_ADD;
640 _rtld_debug_state();
641
642 if (name == NULL) {
643 obj = _rtld_objmain;
644 obj->refcount++;
645 } else
646 obj = _rtld_load_library(name, _rtld_objmain, mode);
647
648 if (obj != NULL) {
649 ++obj->dl_refcount;
650 if (*old_obj_tail != NULL) { /* We loaded something new. */
651 assert(*old_obj_tail == obj);
652
653 if (_rtld_load_needed_objects(obj, mode) == -1 ||
654 (_rtld_init_dag(obj),
655 _rtld_relocate_objects(obj,
656 ((mode & 3) == RTLD_NOW))) == -1) {
657 _rtld_unload_object(obj, false);
658 obj->dl_refcount--;
659 obj = NULL;
660 } else
661 _rtld_call_init_functions(obj);
662 }
663 }
664 _rtld_debug.r_state = RT_CONSISTENT;
665 _rtld_debug_state();
666
667 return obj;
668 }
669
670 /*
671 * Find a symbol in the main program.
672 */
673 void *
674 _rtld_objmain_sym(const char *name)
675 {
676 unsigned long hash;
677 const Elf_Sym *def;
678 const Obj_Entry *obj;
679
680 hash = _rtld_elf_hash(name);
681 obj = _rtld_objmain;
682
683 def = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, false);
684
685 if (def != NULL)
686 return obj->relocbase + def->st_value;
687 return(NULL);
688 }
689
690 __strong_alias(__dlsym,dlsym)
691 void *
692 dlsym(void *handle, const char *name)
693 {
694 const Obj_Entry *obj;
695 unsigned long hash;
696 const Elf_Sym *def;
697 const Obj_Entry *defobj;
698 void *retaddr;
699
700 hash = _rtld_elf_hash(name);
701 def = NULL;
702 defobj = NULL;
703
704 switch ((intptr_t)handle) {
705 case (intptr_t)NULL:
706 case (intptr_t)RTLD_NEXT:
707 case (intptr_t)RTLD_DEFAULT:
708 case (intptr_t)RTLD_SELF:
709 retaddr = __builtin_return_address(0); /* __GNUC__ only */
710 if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
711 _rtld_error("Cannot determine caller's shared object");
712 return NULL;
713 }
714
715 switch ((intptr_t)handle) {
716 case (intptr_t)NULL: /* Just the caller's shared object. */
717 def = _rtld_symlook_obj(name, hash, obj, false);
718 defobj = obj;
719 break;
720
721 case (intptr_t)RTLD_NEXT: /* Objects after callers */
722 obj = obj->next;
723 /*FALLTHROUGH*/
724
725 case (intptr_t)RTLD_SELF: /* Caller included */
726 for (; obj; obj = obj->next) {
727 if ((def = _rtld_symlook_obj(name, hash, obj,
728 false)) != NULL) {
729 defobj = obj;
730 break;
731 }
732 }
733 break;
734
735 case (intptr_t)RTLD_DEFAULT:
736 def = _rtld_symlook_default(name, hash, obj, &defobj,
737 false);
738 break;
739
740 default:
741 abort();
742 }
743 break;
744
745 default:
746 if ((obj = _rtld_dlcheck(handle)) == NULL)
747 return NULL;
748
749 if (obj->mainprog) {
750 /* Search main program and all libraries loaded by it */
751 def = _rtld_symlook_list(name, hash, &_rtld_list_main,
752 &defobj, false);
753 } else {
754 /*
755 * XXX - This isn't correct. The search should include
756 * the whole DAG rooted at the given object.
757 */
758 def = _rtld_symlook_obj(name, hash, obj, false);
759 defobj = obj;
760 }
761 break;
762 }
763
764 if (def != NULL) {
765 #ifdef __HAVE_FUNCTION_DESCRIPTORS
766 if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
767 return (void *)_rtld_function_descriptor_alloc(defobj,
768 def, 0);
769 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
770 return defobj->relocbase + def->st_value;
771 }
772
773 _rtld_error("Undefined symbol \"%s\"", name);
774 return NULL;
775 }
776
777 __strong_alias(__dladdr,dladdr)
778 int
779 dladdr(const void *addr, Dl_info *info)
780 {
781 const Obj_Entry *obj;
782 const Elf_Sym *def, *best_def;
783 void *symbol_addr;
784 unsigned long symoffset;
785
786 #ifdef __HAVE_FUNCTION_DESCRIPTORS
787 addr = _rtld_function_descriptor_function(addr);
788 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
789
790 obj = _rtld_obj_from_addr(addr);
791 if (obj == NULL) {
792 _rtld_error("No shared object contains address");
793 return 0;
794 }
795 info->dli_fname = obj->path;
796 info->dli_fbase = obj->mapbase;
797 info->dli_saddr = (void *)0;
798 info->dli_sname = NULL;
799
800 /*
801 * Walk the symbol list looking for the symbol whose address is
802 * closest to the address sent in.
803 */
804 best_def = NULL;
805 for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
806 def = obj->symtab + symoffset;
807
808 /*
809 * For skip the symbol if st_shndx is either SHN_UNDEF or
810 * SHN_COMMON.
811 */
812 if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
813 continue;
814
815 /*
816 * If the symbol is greater than the specified address, or if it
817 * is further away from addr than the current nearest symbol,
818 * then reject it.
819 */
820 symbol_addr = obj->relocbase + def->st_value;
821 if (symbol_addr > addr || symbol_addr < info->dli_saddr)
822 continue;
823
824 /* Update our idea of the nearest symbol. */
825 info->dli_sname = obj->strtab + def->st_name;
826 info->dli_saddr = symbol_addr;
827 best_def = def;
828
829 /* Exact match? */
830 if (info->dli_saddr == addr)
831 break;
832 }
833
834 #ifdef __HAVE_FUNCTION_DESCRIPTORS
835 if (best_def != NULL && ELF_ST_TYPE(best_def->st_info) == STT_FUNC)
836 info->dli_saddr = (void *)_rtld_function_descriptor_alloc(obj,
837 best_def, 0);
838 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
839
840 return 1;
841 }
842
843 /*
844 * Error reporting function. Use it like printf. If formats the message
845 * into a buffer, and sets things up so that the next call to dlerror()
846 * will return the message.
847 */
848 void
849 _rtld_error(const char *fmt,...)
850 {
851 static char buf[512];
852 va_list ap;
853
854 va_start(ap, fmt);
855 xvsnprintf(buf, sizeof buf, fmt, ap);
856 error_message = buf;
857 va_end(ap);
858 }
859
860 void
861 _rtld_debug_state(void)
862 {
863
864 /* do nothing */
865 }
866
867 void
868 _rtld_linkmap_add(Obj_Entry *obj)
869 {
870 struct link_map *l = &obj->linkmap;
871 struct link_map *prev;
872
873 obj->linkmap.l_name = obj->path;
874 obj->linkmap.l_addr = obj->relocbase;
875 obj->linkmap.l_ld = obj->dynamic;
876 #ifdef __mips__
877 /* XXX This field is not standard and will be removed eventually. */
878 obj->linkmap.l_offs = obj->relocbase;
879 #endif
880
881 if (_rtld_debug.r_map == NULL) {
882 _rtld_debug.r_map = l;
883 return;
884 }
885
886 /*
887 * Scan to the end of the list, but not past the entry for the
888 * dynamic linker, which we want to keep at the very end.
889 */
890 for (prev = _rtld_debug.r_map;
891 prev->l_next != NULL && prev->l_next != &_rtld_objself.linkmap;
892 prev = prev->l_next);
893
894 l->l_prev = prev;
895 l->l_next = prev->l_next;
896 if (l->l_next != NULL)
897 l->l_next->l_prev = l;
898 prev->l_next = l;
899 }
900
901 void
902 _rtld_linkmap_delete(Obj_Entry *obj)
903 {
904 struct link_map *l = &obj->linkmap;
905
906 if (l->l_prev == NULL) {
907 if ((_rtld_debug.r_map = l->l_next) != NULL)
908 l->l_next->l_prev = NULL;
909 return;
910 }
911 if ((l->l_prev->l_next = l->l_next) != NULL)
912 l->l_next->l_prev = l->l_prev;
913 }
914
915 static Obj_Entry *
916 _rtld_obj_from_addr(const void *addr)
917 {
918 Obj_Entry *obj;
919
920 for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
921 if (addr < (void *) obj->mapbase)
922 continue;
923 if (addr < (void *) (obj->mapbase + obj->mapsize))
924 return obj;
925 }
926 return NULL;
927 }
928
929 static void
930 _rtld_objlist_remove(Objlist *list, Obj_Entry *obj)
931 {
932 Objlist_Entry *elm;
933
934 if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
935 SIMPLEQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
936 free(elm);
937 }
938 }
939