Fix typo.
[glibc/history.git] / elf / dladdr.c
blob59e97837603d314b0d9ec9bfd1de8fd20a545599
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 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 <link.h>
22 #include <dlfcn.h>
25 int
26 dladdr (void *address, Dl_info *info)
28 const ElfW(Addr) addr = (ElfW(Addr)) address;
29 struct link_map *l, *match;
30 const ElfW(Sym) *symtab, *matchsym;
31 const char *strtab;
33 /* Find the highest-addressed object that ADDRESS is not below. */
34 match = NULL;
35 for (l = _dl_loaded; l; l = l->l_next)
36 if (addr >= l->l_addr && !match || match->l_addr < l->l_addr)
37 match = l;
39 if (match)
41 /* We know ADDRESS lies within MATCH if in any shared object.
42 Make sure it isn't past the end of MATCH's segments. */
43 size_t n = match->l_phnum;
45 --n;
46 while (match->l_phdr[n].p_type != PT_LOAD);
47 if (addr >= (match->l_addr +
48 match->l_phdr[n].p_vaddr + match->l_phdr[n].p_memsz))
49 /* Off the end of the highest-addressed shared object. */
50 return 0;
52 else
53 return 0;
55 /* Now we know what object the address lies in. */
56 info->dli_fname = match->l_name;
57 info->dli_fbase = (void *) match->l_addr;
59 symtab = ((void *) match->l_addr + match->l_info[DT_SYMTAB]->d_un.d_ptr);
60 strtab = ((void *) match->l_addr + match->l_info[DT_STRTAB]->d_un.d_ptr);
62 /* We assume that the string table follows the symbol table, because
63 there is no way in ELF to know the size of the dynamic symbol table!! */
64 for (matchsym = NULL; (void *) symtab < (void *) strtab; ++symtab)
65 if (addr >= match->l_addr + symtab->st_value && !matchsym ||
66 matchsym->st_value < symtab->st_value &&
67 ELFW(ST_BIND) (symtab->st_info) == STB_GLOBAL ||
68 ELFW(ST_BIND) (symtab->st_info) == STB_WEAK)
69 matchsym = symtab;
71 if (matchsym)
73 /* We found a symbol close by. Fill in its name and exact address. */
74 info->dli_sname = strtab + matchsym->st_name;
75 info->dli_saddr = (void *) (match->l_addr + matchsym->st_value);
77 else
79 /* No symbol matches. We return only the containing object. */
80 info->dli_sname = NULL;
81 info->dli_saddr = NULL;
84 return 1;