1 /* dladdr -- Locate the shared object symbol nearest a given address.
2 Copyright (C) 1996 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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
27 dladdr (void *address, Dl_info *info)
29 const Elf32_Addr addr = (Elf32_Addr) address;
30 struct link_map *l, *match;
31 const Elf32_Sym *symtab, *matchsym;
35 /* Find the highest-addressed object that ADDRESS is not below. */
37 for (l = _dl_loaded; l; l = l->l_next)
38 if (addr >= l->l_addr && !match || match->l_addr < l->l_addr)
43 /* We know ADDRESS lies within MATCH if in any shared object.
44 Make sure it isn't past the end of MATCH's segments. */
45 size_t n = match->l_phnum;
48 while (match->l_phdr[n].p_type != PT_LOAD);
49 if (addr >= (match->l_addr +
50 match->l_phdr[n].p_vaddr + match->l_phdr[n].p_memsz))
51 /* Off the end of the highest-addressed shared object. */
57 /* Now we know what object the address lies in. */
58 info->dli_fname = match->l_name;
59 info->dli_fbase = (void *) match->l_addr;
61 symtab = ((void *) match->l_addr + match->l_info[DT_SYMTAB]->d_un.d_ptr);
62 strtab = ((void *) match->l_addr + match->l_info[DT_STRTAB]->d_un.d_ptr);
64 /* We assume that the string table follows the symbol table, because
65 there is no way in ELF to know the size of the dynamic symbol table!! */
66 for (matchsym = NULL; (void *) symtab < (void *) strtab; ++symtab)
67 if (addr >= match->l_addr + symtab->st_value && !matchsym ||
68 matchsym->st_value < symtab->st_value &&
69 ELF32_ST_BIND (symtab->st_info) == STB_GLOBAL ||
70 ELF32_ST_BIND (symtab->st_info) == STB_WEAK)
75 /* We found a symbol close by. Fill in its name and exact address. */
76 info->dli_sname = strtab + matchsym->st_name;
77 info->dli_saddr = (void *) (match->l_addr + matchsym->st_value);
81 /* No symbol matches. We return only the containing object. */
82 info->dli_sname = NULL;
83 info->dli_saddr = NULL;