* sysdeps/unix/sysv/linux/alpha/kernel_stat.h (kernel_stat64): New. ...
[glibc.git] / elf / dl-sym.c
blob6c84d79ba897c8630ca0fcf30d6b9fb8a905ebe7
1 /* Look up a symbol in a shared object loaded by `dlopen'.
2 Copyright (C) 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
20 #include <stddef.h>
21 #include <setjmp.h>
22 #include <libintl.h>
24 #include <dlfcn.h>
25 #include <ldsodefs.h>
26 #include <dl-hash.h>
27 #ifdef USE_TLS
28 # include <dl-tls.h>
29 #endif
32 #if defined USE_TLS && defined SHARED
33 /* Systems which do not have tls_index also probably have to define
34 DONT_USE_TLS_INDEX. */
36 # ifndef __TLS_GET_ADDR
37 # define __TLS_GET_ADDR __tls_get_addr
38 # endif
40 /* Return the symbol address given the map of the module it is in and
41 the symbol record. This is used in dl-sym.c. */
42 static void *
43 internal_function
44 _dl_tls_symaddr (struct link_map *map, const ElfW(Sym) *ref)
46 # ifndef DONT_USE_TLS_INDEX
47 tls_index tmp =
49 .ti_module = map->l_tls_modid,
50 .ti_offset = ref->st_value
53 return __TLS_GET_ADDR (&tmp);
54 # else
55 return __TLS_GET_ADDR (map->l_tls_modid, ref->st_value);
56 # endif
58 #endif
61 void *
62 internal_function
63 _dl_sym (void *handle, const char *name, void *who)
65 const ElfW(Sym) *ref = NULL;
66 lookup_t result;
67 ElfW(Addr) caller = (ElfW(Addr)) who;
68 struct link_map *match;
69 struct link_map *l;
71 /* If the address is not recognized the call comes from the main
72 program (we hope). */
73 match = GL(dl_loaded);
75 /* Find the highest-addressed object that CALLER is not below. */
76 for (l = GL(dl_loaded); l != NULL; l = l->l_next)
77 if (caller >= l->l_map_start && caller < l->l_map_end)
79 /* There must be exactly one DSO for the range of the virtual
80 memory. Otherwise something is really broken. */
81 match = l;
82 break;
85 if (handle == RTLD_DEFAULT)
86 /* Search the global scope as seen in the caller object. */
87 result = GLRO(dl_lookup_symbol_x) (name, match, &ref, match->l_scope, NULL,
88 0, (DL_LOOKUP_RETURN_NEWEST
89 | DL_LOOKUP_ADD_DEPENDENCY), NULL);
90 else
92 if (handle != RTLD_NEXT)
94 /* Search the scope of the given object. */
95 struct link_map *map = handle;
97 result = GLRO(dl_lookup_symbol_x) (name, match, &ref,
98 map->l_local_scope, NULL, 0,
99 DL_LOOKUP_RETURN_NEWEST, NULL);
101 else
103 if (__builtin_expect (match == GL(dl_loaded), 0))
105 if (! GL(dl_loaded)
106 || caller < GL(dl_loaded)->l_map_start
107 || caller >= GL(dl_loaded)->l_map_end)
108 GLRO(dl_signal_error) (0, NULL, NULL, N_("\
109 RTLD_NEXT used in code not dynamically loaded"));
112 l = match;
113 while (l->l_loader != NULL)
114 l = l->l_loader;
116 result = GLRO(dl_lookup_symbol_x) (name, l, &ref, l->l_local_scope,
117 NULL, 0, 0, match);
121 if (ref != NULL)
123 #if defined USE_TLS && defined SHARED
124 if (ELFW(ST_TYPE) (ref->st_info) == STT_TLS)
125 /* The found symbol is a thread-local storage variable.
126 Return the address for the current thread. */
127 return _dl_tls_symaddr (result, ref);
128 #endif
130 return DL_SYMBOL_ADDRESS (result, ref);
133 return NULL;
136 void *
137 internal_function
138 _dl_vsym (void *handle, const char *name, const char *version, void *who)
140 const ElfW(Sym) *ref = NULL;
141 struct r_found_version vers;
142 lookup_t result;
143 ElfW(Addr) caller = (ElfW(Addr)) who;
144 struct link_map *match;
145 struct link_map *l;
147 /* Compute hash value to the version string. */
148 vers.name = version;
149 vers.hidden = 1;
150 vers.hash = _dl_elf_hash (version);
151 /* We don't have a specific file where the symbol can be found. */
152 vers.filename = NULL;
154 /* If the address is not recognized the call comes from the main
155 program (we hope). */
156 match = GL(dl_loaded);
158 /* Find the highest-addressed object that CALLER is not below. */
159 for (l = GL(dl_loaded); l != NULL; l = l->l_next)
160 if (caller >= l->l_map_start && caller < l->l_map_end)
162 /* There must be exactly one DSO for the range of the virtual
163 memory. Otherwise something is really broken. */
164 match = l;
165 break;
168 if (handle == RTLD_DEFAULT)
169 /* Search the global scope. */
170 result = GLRO(dl_lookup_symbol_x) (name, match, &ref, match->l_scope,
171 &vers, 0, DL_LOOKUP_ADD_DEPENDENCY,
172 NULL);
173 else if (handle == RTLD_NEXT)
175 if (__builtin_expect (match == GL(dl_loaded), 0))
177 if (! GL(dl_loaded)
178 || caller < GL(dl_loaded)->l_map_start
179 || caller >= GL(dl_loaded)->l_map_end)
180 GLRO(dl_signal_error) (0, NULL, NULL, N_("\
181 RTLD_NEXT used in code not dynamically loaded"));
184 l = match;
185 while (l->l_loader != NULL)
186 l = l->l_loader;
188 result = GLRO(dl_lookup_symbol_x) (name, l, &ref, l->l_local_scope,
189 &vers, 0, 0, match);
191 else
193 /* Search the scope of the given object. */
194 struct link_map *map = handle;
195 result = GLRO(dl_lookup_symbol_x) (name, map, &ref, map->l_local_scope,
196 &vers, 0, 0, NULL);
199 if (ref != NULL)
201 #if defined USE_TLS && defined SHARED
202 if (ELFW(ST_TYPE) (ref->st_info) == STT_TLS)
203 /* The found symbol is a thread-local storage variable.
204 Return the address for to the current thread. */
205 return _dl_tls_symaddr (result, ref);
206 #endif
208 return DL_SYMBOL_ADDRESS (result, ref);
211 return NULL;