2000-10-13 Michael Fedrowitz <michael@fedrowitz.de>
[glibc.git] / elf / dl-sym.c
blob873865698c1f8887605737843249682dcdce8af6
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;
34 ElfW(Addr) caller = (ElfW(Addr)) who;
35 struct link_map *match;
36 struct link_map *l;
38 /* Find the highest-addressed object that CALLER is not below. */
39 match = NULL;
40 for (l = _dl_loaded; l; l = l->l_next)
41 if (caller >= l->l_addr && (!match || match->l_addr < l->l_addr))
42 match = l;
44 if (handle == RTLD_DEFAULT)
45 /* Search the global scope. */
46 result = _dl_lookup_symbol (name, match, &ref, _dl_global_scope, 0, 0);
47 else
49 if (handle != RTLD_NEXT)
51 /* Search the scope of the given object. */
52 struct link_map *map = handle;
54 if (match == NULL)
55 /* If the address is not recognized the call comes from the
56 main program (we hope). */
57 match = _dl_loaded;
59 result = _dl_lookup_symbol (name, match, &ref, map->l_local_scope,
60 0, 1);
62 else
64 if (! match)
65 _dl_signal_error (0, NULL, N_("\
66 RTLD_NEXT used in code not dynamically loaded"));
68 l = match;
69 while (l->l_loader)
70 l = l->l_loader;
72 result = _dl_lookup_symbol_skip (name, l, &ref, l->l_local_scope,
73 match);
77 if (ref)
78 return DL_SYMBOL_ADDRESS (result, ref);
80 return NULL;
83 void *
84 internal_function
85 _dl_vsym (void *handle, const char *name, const char *version, void *who)
87 const ElfW(Sym) *ref = NULL;
88 struct r_found_version vers;
89 lookup_t result;
90 ElfW(Addr) caller = (ElfW(Addr)) who;
91 struct link_map *match;
92 struct link_map *l;
94 /* Compute hash value to the version string. */
95 vers.name = version;
96 vers.hidden = 1;
97 vers.hash = _dl_elf_hash (version);
98 /* We don't have a specific file where the symbol can be found. */
99 vers.filename = NULL;
101 /* Find the highest-addressed object that CALLER is not below. */
102 match = NULL;
103 for (l = _dl_loaded; l; l = l->l_next)
104 if (caller >= l->l_addr && (!match || match->l_addr < l->l_addr))
105 match = l;
107 if (handle == RTLD_DEFAULT)
108 /* Search the global scope. */
109 result = _dl_lookup_versioned_symbol (name, match, &ref, _dl_global_scope,
110 &vers, 0, 0);
111 else if (handle == RTLD_NEXT)
113 if (! match)
114 _dl_signal_error (0, NULL, N_("\
115 RTLD_NEXT used in code not dynamically loaded"));
117 l = match;
118 while (l->l_loader)
119 l = l->l_loader;
121 result = _dl_lookup_versioned_symbol_skip (name, l, &ref,
122 l->l_local_scope,
123 &vers, match);
125 else
127 /* Search the scope of the given object. */
128 struct link_map *map = handle;
129 result = _dl_lookup_versioned_symbol (name, map, &ref,
130 map->l_local_scope, &vers, 0, 1);
133 if (ref)
134 return DL_SYMBOL_ADDRESS (result, ref);
136 return NULL;