2.5-18.1
[glibc.git] / elf / dl-addr.c
blob7dbf716cfa4f7e8b46d7d5c2f90f57786f0db7c2
1 /* Locate the shared object symbol nearest a given address.
2 Copyright (C) 1996-2004, 2005, 2006, 2007 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
18 02111-1307 USA. */
20 #include <dlfcn.h>
21 #include <stddef.h>
22 #include <ldsodefs.h>
25 static void
26 __attribute ((always_inline))
27 determine_info (const ElfW(Addr) addr, struct link_map *match, Dl_info *info,
28 struct link_map **mapp, const ElfW(Sym) **symbolp)
30 /* Now we know what object the address lies in. */
31 info->dli_fname = match->l_name;
32 info->dli_fbase = (void *) match->l_map_start;
34 /* If this is the main program the information is incomplete. */
35 if (__builtin_expect (match->l_name[0], 'a') == '\0'
36 && match->l_type == lt_executable)
37 info->dli_fname = _dl_argv[0];
39 const ElfW(Sym) *symtab
40 = (const ElfW(Sym) *) D_PTR (match, l_info[DT_SYMTAB]);
41 const char *strtab = (const char *) D_PTR (match, l_info[DT_STRTAB]);
43 ElfW(Word) strtabsize = match->l_info[DT_STRSZ]->d_un.d_val;
45 const ElfW(Sym) *matchsym = NULL;
46 if (match->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM + DT_THISPROCNUM
47 + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM] != NULL)
49 /* We look at all symbol table entries referenced by the hash
50 table. */
51 for (Elf_Symndx bucket = 0; bucket < match->l_nbuckets; ++bucket)
53 Elf32_Word symndx = match->l_gnu_buckets[bucket];
54 if (symndx != 0)
56 const Elf32_Word *hasharr = &match->l_gnu_chain_zero[symndx];
60 /* The hash table never references local symbols so
61 we can omit that test here. */
62 if ((symtab[symndx].st_shndx != SHN_UNDEF
63 || symtab[symndx].st_value != 0)
64 #ifdef USE_TLS
65 && ELFW(ST_TYPE) (symtab[symndx].st_info) != STT_TLS
66 #endif
67 && DL_ADDR_SYM_MATCH (match, &symtab[symndx],
68 matchsym, addr)
69 && symtab[symndx].st_name < strtabsize)
70 matchsym = (ElfW(Sym) *) &symtab[symndx];
72 ++symndx;
74 while ((*hasharr++ & 1u) == 0);
78 else
80 const ElfW(Sym) *symtabend;
81 if (match->l_info[DT_HASH] != NULL)
82 symtabend = (symtab
83 + ((Elf_Symndx *) D_PTR (match, l_info[DT_HASH]))[1]);
84 else
85 /* There is no direct way to determine the number of symbols in the
86 dynamic symbol table and no hash table is present. The ELF
87 binary is ill-formed but what shall we do? Use the beginning of
88 the string table which generally follows the symbol table. */
89 symtabend = (const ElfW(Sym) *) strtab;
91 for (; (void *) symtab < (void *) symtabend; ++symtab)
92 if ((ELFW(ST_BIND) (symtab->st_info) == STB_GLOBAL
93 || ELFW(ST_BIND) (symtab->st_info) == STB_WEAK)
94 #ifdef USE_TLS
95 && ELFW(ST_TYPE) (symtab->st_info) != STT_TLS
96 #endif
97 && (symtab->st_shndx != SHN_UNDEF
98 || symtab->st_value != 0)
99 && DL_ADDR_SYM_MATCH (match, symtab, matchsym, addr)
100 && symtab->st_name < strtabsize)
101 matchsym = (ElfW(Sym) *) symtab;
104 if (mapp)
105 *mapp = match;
106 if (symbolp)
107 *symbolp = matchsym;
109 if (matchsym)
111 /* We found a symbol close by. Fill in its name and exact
112 address. */
113 lookup_t matchl = LOOKUP_VALUE (match);
115 info->dli_sname = strtab + matchsym->st_name;
116 info->dli_saddr = DL_SYMBOL_ADDRESS (matchl, matchsym);
118 else
120 /* No symbol matches. We return only the containing object. */
121 info->dli_sname = NULL;
122 info->dli_saddr = NULL;
128 internal_function
129 _dl_addr (const void *address, Dl_info *info,
130 struct link_map **mapp, const ElfW(Sym) **symbolp)
132 const ElfW(Addr) addr = DL_LOOKUP_ADDRESS (address);
133 int result = 0;
135 /* Protect against concurrent loads and unloads. */
136 __rtld_lock_lock_recursive (GL(dl_load_lock));
138 /* Find the highest-addressed object that ADDRESS is not below. */
139 for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
140 for (struct link_map *l = GL(dl_ns)[ns]._ns_loaded; l; l = l->l_next)
141 if (addr >= l->l_map_start && addr < l->l_map_end
142 && (l->l_contiguous || _dl_addr_inside_object (l, addr)))
144 determine_info (addr, l, info, mapp, symbolp);
145 result = 1;
146 goto out;
149 out:
150 __rtld_lock_unlock_recursive (GL(dl_load_lock));
152 return result;
154 libc_hidden_def (_dl_addr)
156 /* Return non-zero if ADDR lies within one of L's segments. */
158 internal_function
159 _dl_addr_inside_object (struct link_map *l, const ElfW(Addr) addr)
161 int n = l->l_phnum;
162 const ElfW(Addr) reladdr = addr - l->l_addr;
164 while (--n >= 0)
165 if (l->l_phdr[n].p_type == PT_LOAD
166 && reladdr - l->l_phdr[n].p_vaddr >= 0
167 && reladdr - l->l_phdr[n].p_vaddr < l->l_phdr[n].p_memsz)
168 return 1;
169 return 0;