2.3.4-19
[glibc.git] / elf / dl-addr.c
blob6bd7f7b69c1770e0d42910bb18b5dff6c107b019
1 /* Locate the shared object symbol nearest a given address.
2 Copyright (C) 1996-2004, 2005 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 int
26 internal_function
27 _dl_addr (const void *address, Dl_info *info,
28 struct link_map **mapp, const ElfW(Sym) **symbolp)
30 const ElfW(Addr) addr = DL_LOOKUP_ADDRESS (address);
32 /* Protect against concurrent loads and unloads. */
33 __rtld_lock_lock_recursive (GL(dl_load_lock));
35 /* Find the highest-addressed object that ADDRESS is not below. */
36 struct link_map *match = NULL;
37 for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
38 for (struct link_map *l = GL(dl_ns)[ns]._ns_loaded; l; l = l->l_next)
39 if (addr >= l->l_map_start && addr < l->l_map_end)
41 /* We know ADDRESS lies within L if in any shared object.
42 Make sure it isn't past the end of L's segments. */
43 size_t n = l->l_phnum;
44 if (n > 0)
47 --n;
48 while (l->l_phdr[n].p_type != PT_LOAD);
49 if (addr >= (l->l_addr +
50 l->l_phdr[n].p_vaddr + l->l_phdr[n].p_memsz))
51 /* Off the end of the highest-addressed shared object. */
52 continue;
55 match = l;
56 break;
59 int result = 0;
60 if (match != NULL)
62 /* Now we know what object the address lies in. */
63 info->dli_fname = match->l_name;
64 info->dli_fbase = (void *) match->l_map_start;
66 /* If this is the main program the information is incomplete. */
67 if (__builtin_expect (match->l_name[0], 'a') == '\0'
68 && match->l_type == lt_executable)
69 info->dli_fname = _dl_argv[0];
71 const ElfW(Sym) *symtab
72 = (const ElfW(Sym) *) D_PTR (match, l_info[DT_SYMTAB]);
73 const char *strtab = (const char *) D_PTR (match, l_info[DT_STRTAB]);
75 ElfW(Word) strtabsize = match->l_info[DT_STRSZ]->d_un.d_val;
77 const ElfW(Sym) *symtabend;
78 if (match->l_info[DT_HASH] != NULL)
79 symtabend = (symtab
80 + ((Elf_Symndx *) D_PTR (match, l_info[DT_HASH]))[1]);
81 else
82 /* There is no direct way to determine the number of symbols in the
83 dynamic symbol table and no hash table is present. The ELF
84 binary is ill-formed but what shall we do? Use the beginning of
85 the string table which generally follows the symbol table. */
86 symtabend = (const ElfW(Sym) *) strtab;
88 /* We assume that the string table follows the symbol table,
89 because there is no way in ELF to know the size of the
90 dynamic symbol table!! */
91 const ElfW(Sym) *matchsym;
92 for (matchsym = NULL; (void *) symtab < (void *) symtabend; ++symtab)
93 if (addr >= match->l_addr + symtab->st_value
94 #if defined USE_TLS
95 && ELFW(ST_TYPE) (symtab->st_info) != STT_TLS
96 #endif
97 && ((symtab->st_size == 0
98 && addr == match->l_addr + symtab->st_value)
99 || addr < match->l_addr + symtab->st_value + symtab->st_size)
100 && symtab->st_name < strtabsize
101 && (matchsym == NULL || matchsym->st_value < symtab->st_value)
102 && (ELFW(ST_BIND) (symtab->st_info) == STB_GLOBAL
103 || ELFW(ST_BIND) (symtab->st_info) == STB_WEAK))
104 matchsym = (ElfW(Sym) *) symtab;
106 if (mapp)
107 *mapp = match;
108 if (symbolp)
109 *symbolp = matchsym;
111 if (matchsym)
113 /* We found a symbol close by. Fill in its name and exact
114 address. */
115 lookup_t matchl = LOOKUP_VALUE (match);
117 info->dli_sname = strtab + matchsym->st_name;
118 info->dli_saddr = DL_SYMBOL_ADDRESS (matchl, matchsym);
120 else
122 /* No symbol matches. We return only the containing object. */
123 info->dli_sname = NULL;
124 info->dli_saddr = NULL;
127 result = 1;
130 __rtld_lock_unlock_recursive (GL(dl_load_lock));
132 return result;
134 libc_hidden_def (_dl_addr)