Update.
[platform/upstream/glibc.git] / elf / dl-sym.c
1 /* Look up a symbol in a shared object loaded by `dlopen'.
2    Copyright (C) 1999 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <stddef.h>
21 #include <setjmp.h>
22 #include <libintl.h>
23
24 #include <dlfcn.h>
25 #include <ldsodefs.h>
26 #include <dl-hash.h>
27
28 void *
29 internal_function
30 _dl_sym (void *handle, const char *name, void *who)
31 {
32   ElfW(Addr) loadbase;
33   const ElfW(Sym) *ref = NULL;
34
35   if (handle == RTLD_DEFAULT)
36     /* Search the global scope.  */
37     loadbase = _dl_lookup_symbol (name, NULL, &ref, _dl_global_scope, 0);
38   else
39     {
40       struct link_map *l, *match;
41       ElfW(Addr) caller = (ElfW(Addr)) who;
42
43       /* Find the highest-addressed object that CALLER is not below.  */
44       match = NULL;
45       for (l = _dl_loaded; l; l = l->l_next)
46         if (caller >= l->l_addr && (!match || match->l_addr < l->l_addr))
47           match = l;
48
49       if (handle != RTLD_NEXT)
50         {
51           /* Search the scope of the given object.  */
52           struct link_map *map = handle;
53
54           if (match == NULL)
55             /* If the address is not recognized the call comes from the
56                main program (we hope).  */
57             match = _dl_loaded;
58
59           loadbase = _dl_lookup_symbol (name, match, &ref, map->l_local_scope,
60                                         0);
61         }
62       else
63         {
64           if (! match)
65             _dl_signal_error (0, NULL, _("\
66 RTLD_NEXT used in code not dynamically loaded"));
67
68           l = match;
69           while (l->l_loader)
70             l = l->l_loader;
71
72           loadbase = _dl_lookup_symbol_skip (name, l, &ref, l->l_local_scope,
73                                              match);
74         }
75     }
76
77   if (loadbase)
78     return (void *) (loadbase + ref->st_value);
79
80   return NULL;
81 }
82
83 void *
84 internal_function
85 _dl_vsym (void *handle, const char *name, const char *version, void *who)
86 {
87   ElfW(Addr) loadbase;
88   const ElfW(Sym) *ref = NULL;
89   struct r_found_version vers;
90
91   /* Compute hash value to the version string.  */
92   vers.name = version;
93   vers.hidden = 1;
94   vers.hash = _dl_elf_hash (version);
95   /* We don't have a specific file where the symbol can be found.  */
96   vers.filename = NULL;
97
98   if (handle == RTLD_DEFAULT)
99     /* Search the global scope.  */
100     loadbase = _dl_lookup_versioned_symbol (name, NULL, &ref, _dl_global_scope,
101                                             &vers, 0);
102   else if (handle == RTLD_NEXT)
103     {
104       struct link_map *l, *match;
105       ElfW(Addr) caller = (ElfW(Addr)) who;
106
107       /* Find the highest-addressed object that CALLER is not below.  */
108       match = NULL;
109       for (l = _dl_loaded; l; l = l->l_next)
110         if (caller >= l->l_addr && (!match || match->l_addr < l->l_addr))
111           match = l;
112
113       if (! match)
114         _dl_signal_error (0, NULL, _("\
115 RTLD_NEXT used in code not dynamically loaded"));
116
117       l = match;
118       while (l->l_loader)
119         l = l->l_loader;
120
121       loadbase = _dl_lookup_versioned_symbol_skip (name, l, &ref,
122                                                    l->l_local_scope,
123                                                    &vers, match);
124     }
125   else
126     {
127       /* Search the scope of the given object.  */
128       struct link_map *map = handle;
129       loadbase = _dl_lookup_versioned_symbol (name, map, &ref,
130                                               map->l_local_scope, &vers, 0);
131     }
132
133   if (loadbase)
134     return (void *) (loadbase + ref->st_value);
135
136   return NULL;
137 }