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, 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, 0);
55   else
56     {
57       if (handle != RTLD_NEXT)
58         {
59           /* Search the scope of the given object.  */
60           struct link_map *map = handle;
61
62           result = _dl_lookup_symbol (name, match, &ref, map->l_local_scope,
63                                       0, 1);
64         }
65       else
66         {
67           if (__builtin_expect (match == GL(dl_loaded), 0))
68             {
69               if (! GL(dl_loaded)
70                   || caller < GL(dl_loaded)->l_map_start
71                   || caller >= GL(dl_loaded)->l_map_end)
72                 _dl_signal_error (0, NULL, NULL, N_("\
73 RTLD_NEXT used in code not dynamically loaded"));
74             }
75
76           l = match;
77           while (l->l_loader != NULL)
78             l = l->l_loader;
79
80           result = _dl_lookup_symbol_skip (name, l, &ref, l->l_local_scope,
81                                            match);
82         }
83     }
84
85   if (ref != NULL)
86     return DL_SYMBOL_ADDRESS (result, ref);
87
88   return NULL;
89 }
90
91 void *
92 internal_function
93 _dl_vsym (void *handle, const char *name, const char *version, void *who)
94 {
95   const ElfW(Sym) *ref = NULL;
96   struct r_found_version vers;
97   lookup_t result;
98   ElfW(Addr) caller = (ElfW(Addr)) who;
99   struct link_map *match;
100   struct link_map *l;
101
102   /* Compute hash value to the version string.  */
103   vers.name = version;
104   vers.hidden = 1;
105   vers.hash = _dl_elf_hash (version);
106   /* We don't have a specific file where the symbol can be found.  */
107   vers.filename = NULL;
108
109   /* If the address is not recognized the call comes from the main
110      program (we hope).  */
111   match = GL(dl_loaded);
112
113   /* Find the highest-addressed object that CALLER is not below.  */
114   for (l = GL(dl_loaded); l != NULL; l = l->l_next)
115     if (caller >= l->l_map_start && caller < l->l_map_end)
116       {
117         /* There must be exactly one DSO for the range of the virtual
118            memory.  Otherwise something is really broken.  */
119         match = l;
120         break;
121       }
122
123   if (handle == RTLD_DEFAULT)
124     /* Search the global scope.  */
125     result = _dl_lookup_versioned_symbol (name, match, &ref, match->l_scope,
126                                           &vers, 0, 0);
127   else if (handle == RTLD_NEXT)
128     {
129       if (__builtin_expect (match == GL(dl_loaded), 0))
130         {
131           if (! GL(dl_loaded)
132               || caller < GL(dl_loaded)->l_map_start
133               || caller >= GL(dl_loaded)->l_map_end)
134             _dl_signal_error (0, NULL, NULL, N_("\
135 RTLD_NEXT used in code not dynamically loaded"));
136         }
137
138       l = match;
139       while (l->l_loader != NULL)
140         l = l->l_loader;
141
142       result = _dl_lookup_versioned_symbol_skip (name, l, &ref,
143                                                  l->l_local_scope,
144                                                  &vers, match);
145     }
146   else
147     {
148       /* Search the scope of the given object.  */
149       struct link_map *map = handle;
150       result = _dl_lookup_versioned_symbol (name, map, &ref,
151                                             map->l_local_scope, &vers, 0, 1);
152     }
153
154   if (ref != NULL)
155     return DL_SYMBOL_ADDRESS (result, ref);
156
157   return NULL;
158 }