Update.
[glibc.git] / elf / dl-sym.c
blobe54ed808b2a39bacf282ee769314f68094157861
1 /* Look up a symbol in a shared object loaded by `dlopen'.
2 Copyright (C) 1999, 2000 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 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.
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.
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. */
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>
28 void *
29 internal_function
30 _dl_sym (void *handle, const char *name, void *who)
32 const ElfW(Sym) *ref = NULL;
33 lookup_t result;
35 if (handle == RTLD_DEFAULT)
36 /* Search the global scope. */
37 result = _dl_lookup_symbol (name, NULL, &ref, _dl_global_scope, 0);
38 else
40 struct link_map *l;
41 struct link_map *match;
42 ElfW(Addr) caller = (ElfW(Addr)) who;
44 /* Find the highest-addressed object that CALLER is not below. */
45 match = NULL;
46 for (l = _dl_loaded; l; l = l->l_next)
47 if (caller >= l->l_addr && (!match || match->l_addr < l->l_addr))
48 match = l;
50 if (handle != RTLD_NEXT)
52 /* Search the scope of the given object. */
53 struct link_map *map = handle;
55 if (match == NULL)
56 /* If the address is not recognized the call comes from the
57 main program (we hope). */
58 match = _dl_loaded;
60 result = _dl_lookup_symbol (name, match, &ref, map->l_local_scope,
61 0);
63 else
65 if (! match)
66 _dl_signal_error (0, NULL, N_("\
67 RTLD_NEXT used in code not dynamically loaded"));
69 l = match;
70 while (l->l_loader)
71 l = l->l_loader;
73 result = _dl_lookup_symbol_skip (name, l, &ref, l->l_local_scope,
74 match);
78 if (ref)
79 return DL_SYMBOL_ADDRESS (result, ref);
81 return NULL;
84 void *
85 internal_function
86 _dl_vsym (void *handle, const char *name, const char *version, void *who)
88 const ElfW(Sym) *ref = NULL;
89 struct r_found_version vers;
90 lookup_t result;
92 /* Compute hash value to the version string. */
93 vers.name = version;
94 vers.hidden = 1;
95 vers.hash = _dl_elf_hash (version);
96 /* We don't have a specific file where the symbol can be found. */
97 vers.filename = NULL;
99 if (handle == RTLD_DEFAULT)
100 /* Search the global scope. */
101 result = _dl_lookup_versioned_symbol (name, NULL, &ref, _dl_global_scope,
102 &vers, 0);
103 else if (handle == RTLD_NEXT)
105 struct link_map *l;
106 struct link_map *match;
107 ElfW(Addr) caller = (ElfW(Addr)) who;
109 /* Find the highest-addressed object that CALLER is not below. */
110 match = NULL;
111 for (l = _dl_loaded; l; l = l->l_next)
112 if (caller >= l->l_addr && (!match || match->l_addr < l->l_addr))
113 match = l;
115 if (! match)
116 _dl_signal_error (0, NULL, N_("\
117 RTLD_NEXT used in code not dynamically loaded"));
119 l = match;
120 while (l->l_loader)
121 l = l->l_loader;
123 result = _dl_lookup_versioned_symbol_skip (name, l, &ref,
124 l->l_local_scope,
125 &vers, match);
127 else
129 /* Search the scope of the given object. */
130 struct link_map *map = handle;
131 result = _dl_lookup_versioned_symbol (name, map, &ref,
132 map->l_local_scope, &vers, 0);
135 if (ref)
136 return DL_SYMBOL_ADDRESS (result, ref);
138 return NULL;