]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-addr.c
elf: Unify symbol address run-time calculation [BZ #19818]
[thirdparty/glibc.git] / elf / dl-addr.c
CommitLineData
a2b08ee5 1/* Locate the shared object symbol nearest a given address.
688903eb 2 Copyright (C) 1996-2018 Free Software Foundation, Inc.
a2b08ee5
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
a2b08ee5
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
a2b08ee5 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
a2b08ee5 18
a2b08ee5 19#include <dlfcn.h>
a853022c 20#include <stddef.h>
a42195db 21#include <ldsodefs.h>
a2b08ee5
UD
22
23
2b2596b1 24static inline void
ffecd39b
UD
25__attribute ((always_inline))
26determine_info (const ElfW(Addr) addr, struct link_map *match, Dl_info *info,
27 struct link_map **mapp, const ElfW(Sym) **symbolp)
a2b08ee5 28{
ffecd39b
UD
29 /* Now we know what object the address lies in. */
30 info->dli_fname = match->l_name;
31 info->dli_fbase = (void *) match->l_map_start;
a334319f 32
ffecd39b
UD
33 /* If this is the main program the information is incomplete. */
34 if (__builtin_expect (match->l_name[0], 'a') == '\0'
35 && match->l_type == lt_executable)
36 info->dli_fname = _dl_argv[0];
a334319f 37
ffecd39b
UD
38 const ElfW(Sym) *symtab
39 = (const ElfW(Sym) *) D_PTR (match, l_info[DT_SYMTAB]);
40 const char *strtab = (const char *) D_PTR (match, l_info[DT_STRTAB]);
11bf311e 41
ffecd39b 42 ElfW(Word) strtabsize = match->l_info[DT_STRSZ]->d_un.d_val;
11bf311e 43
ffecd39b 44 const ElfW(Sym) *matchsym = NULL;
6a5cac49 45 if (match->l_info[ADDRIDX (DT_GNU_HASH)] != NULL)
32c075e1 46 {
ffecd39b
UD
47 /* We look at all symbol table entries referenced by the hash
48 table. */
49 for (Elf_Symndx bucket = 0; bucket < match->l_nbuckets; ++bucket)
11bf311e 50 {
ffecd39b
UD
51 Elf32_Word symndx = match->l_gnu_buckets[bucket];
52 if (symndx != 0)
11bf311e 53 {
ffecd39b
UD
54 const Elf32_Word *hasharr = &match->l_gnu_chain_zero[symndx];
55
56 do
11bf311e 57 {
ffecd39b
UD
58 /* The hash table never references local symbols so
59 we can omit that test here. */
60 if ((symtab[symndx].st_shndx != SHN_UNDEF
61 || symtab[symndx].st_value != 0)
62 && ELFW(ST_TYPE) (symtab[symndx].st_info) != STT_TLS
63 && DL_ADDR_SYM_MATCH (match, &symtab[symndx],
64 matchsym, addr)
65 && symtab[symndx].st_name < strtabsize)
66 matchsym = (ElfW(Sym) *) &symtab[symndx];
67
68 ++symndx;
11bf311e 69 }
ffecd39b 70 while ((*hasharr++ & 1u) == 0);
11bf311e
UD
71 }
72 }
ffecd39b
UD
73 }
74 else
75 {
76 const ElfW(Sym) *symtabend;
77 if (match->l_info[DT_HASH] != NULL)
78 symtabend = (symtab
79 + ((Elf_Symndx *) D_PTR (match, l_info[DT_HASH]))[1]);
11bf311e 80 else
ffecd39b
UD
81 /* There is no direct way to determine the number of symbols in the
82 dynamic symbol table and no hash table is present. The ELF
83 binary is ill-formed but what shall we do? Use the beginning of
84 the string table which generally follows the symbol table. */
85 symtabend = (const ElfW(Sym) *) strtab;
86
87 for (; (void *) symtab < (void *) symtabend; ++symtab)
88 if ((ELFW(ST_BIND) (symtab->st_info) == STB_GLOBAL
89 || ELFW(ST_BIND) (symtab->st_info) == STB_WEAK)
b6084a95 90 && __glibc_likely (!dl_symbol_visibility_binds_local_p (symtab))
ffecd39b
UD
91 && ELFW(ST_TYPE) (symtab->st_info) != STT_TLS
92 && (symtab->st_shndx != SHN_UNDEF
93 || symtab->st_value != 0)
94 && DL_ADDR_SYM_MATCH (match, symtab, matchsym, addr)
95 && symtab->st_name < strtabsize)
96 matchsym = (ElfW(Sym) *) symtab;
97 }
32c075e1 98
ffecd39b
UD
99 if (mapp)
100 *mapp = match;
101 if (symbolp)
102 *symbolp = matchsym;
32c075e1 103
ffecd39b
UD
104 if (matchsym)
105 {
106 /* We found a symbol close by. Fill in its name and exact
107 address. */
108 lookup_t matchl = LOOKUP_VALUE (match);
32c075e1 109
ffecd39b
UD
110 info->dli_sname = strtab + matchsym->st_name;
111 info->dli_saddr = DL_SYMBOL_ADDRESS (matchl, matchsym);
112 }
113 else
114 {
115 /* No symbol matches. We return only the containing object. */
116 info->dli_sname = NULL;
117 info->dli_saddr = NULL;
118 }
119}
120
121
122int
ffecd39b
UD
123_dl_addr (const void *address, Dl_info *info,
124 struct link_map **mapp, const ElfW(Sym) **symbolp)
125{
126 const ElfW(Addr) addr = DL_LOOKUP_ADDRESS (address);
127 int result = 0;
128
129 /* Protect against concurrent loads and unloads. */
130 __rtld_lock_lock_recursive (GL(dl_load_lock));
131
be179c8a
SP
132 struct link_map *l = _dl_find_dso_for_object (addr);
133
134 if (l)
135 {
136 determine_info (addr, l, info, mapp, symbolp);
137 result = 1;
138 }
a461b147 139
a461b147
UD
140 __rtld_lock_unlock_recursive (GL(dl_load_lock));
141
142 return result;
a2b08ee5 143}
cc7e7a26 144libc_hidden_def (_dl_addr)