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
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
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. */
44 _dl_tls_symaddr (struct link_map
*map
, const ElfW(Sym
) *ref
)
46 # ifndef DONT_USE_TLS_INDEX
49 .ti_module
= map
->l_tls_modid
,
50 .ti_offset
= ref
->st_value
53 return __TLS_GET_ADDR (&tmp
);
55 return __TLS_GET_ADDR (map
->l_tls_modid
, ref
->st_value
);
63 do_sym (void *handle
, const char *name
, void *who
,
64 struct r_found_version
*vers
, int flags
)
66 const ElfW(Sym
) *ref
= NULL
;
68 ElfW(Addr
) caller
= (ElfW(Addr
)) who
;
70 /* If the address is not recognized the call comes from the main
72 struct link_map
*match
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
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
;
78 if (caller
>= l
->l_map_start
&& caller
< l
->l_map_end
)
80 /* There must be exactly one DSO for the range of the virtual
81 memory. Otherwise something is really broken. */
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
,
91 else if (handle
== RTLD_NEXT
)
93 if (__builtin_expect (match
== GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
, 0))
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"));
102 struct link_map
*l
= match
;
103 while (l
->l_loader
!= NULL
)
106 result
= GLRO(dl_lookup_symbol_x
) (name
, l
, &ref
, l
->l_local_scope
,
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
);
119 #if defined USE_TLS && defined SHARED
120 if (ELFW(ST_TYPE
) (ref
->st_info
) == STT_TLS
)
121 /* The found symbol is a thread-local storage variable.
122 Return the address for to the current thread. */
123 return _dl_tls_symaddr (result
, ref
);
126 return DL_SYMBOL_ADDRESS (result
, ref
);
135 _dl_vsym (void *handle
, const char *name
, const char *version
, void *who
)
137 struct r_found_version vers
;
139 /* Compute hash value to the version string. */
142 vers
.hash
= _dl_elf_hash (version
);
143 /* We don't have a specific file where the symbol can be found. */
144 vers
.filename
= NULL
;
146 return do_sym (handle
, name
, who
, &vers
, 0);
152 _dl_sym (void *handle
, const char *name
, void *who
)
154 return do_sym (handle
, name
, who
, NULL
, DL_LOOKUP_RETURN_NEWEST
);