3bf8104a4a23e9e03b067614e0d4e84516a70256
[platform/upstream/glibc.git] / elf / dl-sym.c
1 /* Look up a symbol in a shared object loaded by `dlopen'.
2    Copyright (C) 1999, 2000, 2001, 2002 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 Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the 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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    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   const ElfW(Sym) *ref = NULL;
33   lookup_t result;
34   ElfW(Addr) caller = (ElfW(Addr)) who;
35   struct link_map *match;
36   struct link_map *l;
37
38   /* If the address is not recognized the call comes from the main
39      program (we hope).  */
40   match = GL(dl_loaded);
41
42   /* Find the highest-addressed object that CALLER is not below.  */
43   for (l = GL(dl_loaded); l != NULL; l = l->l_next)
44     if (caller >= l->l_map_start && caller < l->l_map_end)
45       {
46         /* There must be exactly one DSO for the range of the virtual
47            memory.  Otherwise something is really broken.  */
48         match = l;
49         break;
50       }
51
52   if (handle == RTLD_DEFAULT)
53     /* Search the global scope as seen in the caller object.  */
54     result = _dl_lookup_symbol (name, match, &ref, match->l_scope, 0,
55                                 DL_LOOKUP_RETURN_NEWEST
56                                 | DL_LOOKUP_ADD_DEPENDENCY);
57   else
58     {
59       if (handle != RTLD_NEXT)
60         {
61           /* Search the scope of the given object.  */
62           struct link_map *map = handle;
63
64           result = _dl_lookup_symbol (name, match, &ref, map->l_local_scope,
65                                       0, DL_LOOKUP_RETURN_NEWEST);
66         }
67       else
68         {
69           if (__builtin_expect (match == GL(dl_loaded), 0))
70             {
71               if (! GL(dl_loaded)
72                   || caller < GL(dl_loaded)->l_map_start
73                   || caller >= GL(dl_loaded)->l_map_end)
74                 _dl_signal_error (0, NULL, NULL, N_("\
75 RTLD_NEXT used in code not dynamically loaded"));
76             }
77
78           l = match;
79           while (l->l_loader != NULL)
80             l = l->l_loader;
81
82           result = _dl_lookup_symbol_skip (name, l, &ref, l->l_local_scope,
83                                            match);
84         }
85     }
86
87   if (ref != NULL)
88     {
89 #if defined USE_TLS && defined SHARED
90       if (ELFW(ST_TYPE) (ref->st_info) == STT_TLS)
91         /* The found symbol is a thread-local storage variable.
92            Return the address for to the current thread.  */
93         return _dl_tls_symaddr (result, ref);
94 #endif
95
96       return DL_SYMBOL_ADDRESS (result, ref);
97     }
98
99   return NULL;
100 }
101
102 void *
103 internal_function
104 _dl_vsym (void *handle, const char *name, const char *version, void *who)
105 {
106   const ElfW(Sym) *ref = NULL;
107   struct r_found_version vers;
108   lookup_t result;
109   ElfW(Addr) caller = (ElfW(Addr)) who;
110   struct link_map *match;
111   struct link_map *l;
112
113   /* Compute hash value to the version string.  */
114   vers.name = version;
115   vers.hidden = 1;
116   vers.hash = _dl_elf_hash (version);
117   /* We don't have a specific file where the symbol can be found.  */
118   vers.filename = NULL;
119
120   /* If the address is not recognized the call comes from the main
121      program (we hope).  */
122   match = GL(dl_loaded);
123
124   /* Find the highest-addressed object that CALLER is not below.  */
125   for (l = GL(dl_loaded); l != NULL; l = l->l_next)
126     if (caller >= l->l_map_start && caller < l->l_map_end)
127       {
128         /* There must be exactly one DSO for the range of the virtual
129            memory.  Otherwise something is really broken.  */
130         match = l;
131         break;
132       }
133
134   if (handle == RTLD_DEFAULT)
135     /* Search the global scope.  */
136     result = _dl_lookup_versioned_symbol (name, match, &ref, match->l_scope,
137                                           &vers, 0, DL_LOOKUP_ADD_DEPENDENCY);
138   else if (handle == RTLD_NEXT)
139     {
140       if (__builtin_expect (match == GL(dl_loaded), 0))
141         {
142           if (! GL(dl_loaded)
143               || caller < GL(dl_loaded)->l_map_start
144               || caller >= GL(dl_loaded)->l_map_end)
145             _dl_signal_error (0, NULL, NULL, N_("\
146 RTLD_NEXT used in code not dynamically loaded"));
147         }
148
149       l = match;
150       while (l->l_loader != NULL)
151         l = l->l_loader;
152
153       result = _dl_lookup_versioned_symbol_skip (name, l, &ref,
154                                                  l->l_local_scope,
155                                                  &vers, match);
156     }
157   else
158     {
159       /* Search the scope of the given object.  */
160       struct link_map *map = handle;
161       result = _dl_lookup_versioned_symbol (name, map, &ref,
162                                             map->l_local_scope, &vers, 0, 0);
163     }
164
165   if (ref != NULL)
166     {
167 #if defined USE_TLS && defined SHARED
168       if (ELFW(ST_TYPE) (ref->st_info) == STT_TLS)
169         /* The found symbol is a thread-local storage variable.
170            Return the address for to the current thread.  */
171         return _dl_tls_symaddr (result, ref);
172 #endif
173
174       return DL_SYMBOL_ADDRESS (result, ref);
175     }
176
177   return NULL;
178 }