* elf/do-lookup.h (do_lookup_x): No need to compare SKIP to NULL
[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,2004,2006 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 #ifdef USE_TLS
28 # include <dl-tls.h>
29 #endif
30
31
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.  */
35
36 # ifndef __TLS_GET_ADDR
37 #  define __TLS_GET_ADDR __tls_get_addr
38 # endif
39
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)
45 {
46 # ifndef DONT_USE_TLS_INDEX
47   tls_index tmp =
48     {
49       .ti_module = map->l_tls_modid,
50       .ti_offset = ref->st_value
51     };
52
53   return __TLS_GET_ADDR (&tmp);
54 # else
55   return __TLS_GET_ADDR (map->l_tls_modid, ref->st_value);
56 # endif
57 }
58 #endif
59
60
61 static void *
62 internal_function
63 do_sym (void *handle, const char *name, void *who,
64         struct r_found_version *vers, int flags)
65 {
66   const ElfW(Sym) *ref = NULL;
67   lookup_t result;
68   ElfW(Addr) caller = (ElfW(Addr)) who;
69
70   /* If the address is not recognized the call comes from the main
71      program (we hope).  */
72   struct link_map *match = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
73
74   /* Find the highest-addressed object that CALLER is not below.  */
75   for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
76     for (struct link_map *l = GL(dl_ns)[ns]._ns_loaded; l != NULL;
77          l = l->l_next)
78       if (caller >= l->l_map_start && caller < l->l_map_end)
79         {
80           /* There must be exactly one DSO for the range of the virtual
81              memory.  Otherwise something is really broken.  */
82           match = l;
83           break;
84         }
85
86   if (handle == RTLD_DEFAULT)
87     /* Search the global scope.  */
88     result = GLRO(dl_lookup_symbol_x) (name, match, &ref, match->l_scope,
89                                        vers, 0, flags|DL_LOOKUP_ADD_DEPENDENCY,
90                                        NULL);
91   else if (handle == RTLD_NEXT)
92     {
93       if (__builtin_expect (match == GL(dl_ns)[LM_ID_BASE]._ns_loaded, 0))
94         {
95           if (match == NULL
96               || caller < match->l_map_start
97               || caller >= match->l_map_end)
98             GLRO(dl_signal_error) (0, NULL, NULL, N_("\
99 RTLD_NEXT used in code not dynamically loaded"));
100         }
101
102       struct link_map *l = match;
103       while (l->l_loader != NULL)
104         l = l->l_loader;
105
106       result = GLRO(dl_lookup_symbol_x) (name, match, &ref, l->l_local_scope,
107                                          vers, 0, 0, match);
108     }
109   else
110     {
111       /* Search the scope of the given object.  */
112       struct link_map *map = handle;
113       result = GLRO(dl_lookup_symbol_x) (name, map, &ref, map->l_local_scope,
114                                          vers, 0, flags, NULL);
115     }
116
117   if (ref != NULL)
118     {
119       void *value;
120
121 #if defined USE_TLS && defined SHARED
122       if (ELFW(ST_TYPE) (ref->st_info) == STT_TLS)
123         /* The found symbol is a thread-local storage variable.
124            Return the address for to the current thread.  */
125         value = _dl_tls_symaddr (result, ref);
126       else
127 #endif
128         value = DL_SYMBOL_ADDRESS (result, ref);
129
130 #ifdef SHARED
131       /* Auditing checkpoint: we have a new binding.  Provide the
132          auditing libraries the possibility to change the value and
133          tell us whether further auditing is wanted.  */
134       if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
135         {
136           const char *strtab = (const char *) D_PTR (result,
137                                                      l_info[DT_STRTAB]);
138           /* Compute index of the symbol entry in the symbol table of
139              the DSO with the definition.  */
140           unsigned int ndx = (ref - (ElfW(Sym) *) D_PTR (result,
141                                                          l_info[DT_SYMTAB]));
142
143           if ((match->l_audit_any_plt | result->l_audit_any_plt) != 0)
144             {
145               unsigned int altvalue = 0;
146               struct audit_ifaces *afct = GLRO(dl_audit);
147               /* Synthesize a symbol record where the st_value field is
148                  the result.  */
149               ElfW(Sym) sym = *ref;
150               sym.st_value = (ElfW(Addr)) value;
151
152               for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
153                 {
154                   if (afct->symbind != NULL
155                       && ((match->l_audit[cnt].bindflags & LA_FLG_BINDFROM)
156                           != 0
157                           || ((result->l_audit[cnt].bindflags & LA_FLG_BINDTO)
158                               != 0)))
159                     {
160                       unsigned int flags = altvalue | LA_SYMB_DLSYM;
161                       uintptr_t new_value
162                         = afct->symbind (&sym, ndx,
163                                          &match->l_audit[cnt].cookie,
164                                          &result->l_audit[cnt].cookie,
165                                          &flags, strtab + ref->st_name);
166                       if (new_value != (uintptr_t) sym.st_value)
167                         {
168                           altvalue = LA_SYMB_ALTVALUE;
169                           sym.st_value = new_value;
170                         }
171                     }
172
173                   afct = afct->next;
174                 }
175
176               value = (void *) sym.st_value;
177             }
178         }
179 #endif
180
181       return value;
182     }
183
184   return NULL;
185 }
186
187
188 void *
189 internal_function
190 _dl_vsym (void *handle, const char *name, const char *version, void *who)
191 {
192   struct r_found_version vers;
193
194   /* Compute hash value to the version string.  */
195   vers.name = version;
196   vers.hidden = 1;
197   vers.hash = _dl_elf_hash (version);
198   /* We don't have a specific file where the symbol can be found.  */
199   vers.filename = NULL;
200
201   return do_sym (handle, name, who, &vers, 0);
202 }
203
204
205 void *
206 internal_function
207 _dl_sym (void *handle, const char *name, void *who)
208 {
209   return do_sym (handle, name, who, NULL, DL_LOOKUP_RETURN_NEWEST);
210 }