Update copyright notices with scripts/update-copyrights.
[jlayton/glibc.git] / elf / dl-addr.c
1 /* Locate the shared object symbol nearest a given address.
2    Copyright (C) 1996-2013 Free Software Foundation, Inc.
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
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.
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
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <dlfcn.h>
20 #include <stddef.h>
21 #include <ldsodefs.h>
22
23
24 static inline void
25 __attribute ((always_inline))
26 determine_info (const ElfW(Addr) addr, struct link_map *match, Dl_info *info,
27                 struct link_map **mapp, const ElfW(Sym) **symbolp)
28 {
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;
32
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];
37
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]);
41
42   ElfW(Word) strtabsize = match->l_info[DT_STRSZ]->d_un.d_val;
43
44   const ElfW(Sym) *matchsym = NULL;
45   if (match->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM + DT_THISPROCNUM
46                     + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM] != NULL)
47     {
48       /* We look at all symbol table entries referenced by the hash
49          table.  */
50       for (Elf_Symndx bucket = 0; bucket < match->l_nbuckets; ++bucket)
51         {
52           Elf32_Word symndx = match->l_gnu_buckets[bucket];
53           if (symndx != 0)
54             {
55               const Elf32_Word *hasharr = &match->l_gnu_chain_zero[symndx];
56
57               do
58                 {
59                   /* The hash table never references local symbols so
60                      we can omit that test here.  */
61                   if ((symtab[symndx].st_shndx != SHN_UNDEF
62                        || symtab[symndx].st_value != 0)
63                       && ELFW(ST_TYPE) (symtab[symndx].st_info) != STT_TLS
64                       && DL_ADDR_SYM_MATCH (match, &symtab[symndx],
65                                             matchsym, addr)
66                       && symtab[symndx].st_name < strtabsize)
67                     matchsym = (ElfW(Sym) *) &symtab[symndx];
68
69                   ++symndx;
70                 }
71               while ((*hasharr++ & 1u) == 0);
72             }
73         }
74     }
75   else
76     {
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;
87
88       for (; (void *) symtab < (void *) symtabend; ++symtab)
89         if ((ELFW(ST_BIND) (symtab->st_info) == STB_GLOBAL
90              || ELFW(ST_BIND) (symtab->st_info) == STB_WEAK)
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     }
98
99   if (mapp)
100     *mapp = match;
101   if (symbolp)
102     *symbolp = matchsym;
103
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);
109
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
122 int
123 internal_function
124 _dl_addr (const void *address, Dl_info *info,
125           struct link_map **mapp, const ElfW(Sym) **symbolp)
126 {
127   const ElfW(Addr) addr = DL_LOOKUP_ADDRESS (address);
128   int result = 0;
129
130   /* Protect against concurrent loads and unloads.  */
131   __rtld_lock_lock_recursive (GL(dl_load_lock));
132
133   /* Find the highest-addressed object that ADDRESS is not below.  */
134   for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns)
135     for (struct link_map *l = GL(dl_ns)[ns]._ns_loaded; l; l = l->l_next)
136       if (addr >= l->l_map_start && addr < l->l_map_end
137           && (l->l_contiguous || _dl_addr_inside_object (l, addr)))
138         {
139           determine_info (addr, l, info, mapp, symbolp);
140           result = 1;
141           goto out;
142         }
143
144  out:
145   __rtld_lock_unlock_recursive (GL(dl_load_lock));
146
147   return result;
148 }
149 libc_hidden_def (_dl_addr)
150
151 /* Return non-zero if ADDR lies within one of L's segments.  */
152 int
153 internal_function
154 _dl_addr_inside_object (struct link_map *l, const ElfW(Addr) addr)
155 {
156   int n = l->l_phnum;
157   const ElfW(Addr) reladdr = addr - l->l_addr;
158
159   while (--n >= 0)
160     if (l->l_phdr[n].p_type == PT_LOAD
161         && reladdr - l->l_phdr[n].p_vaddr >= 0
162         && reladdr - l->l_phdr[n].p_vaddr < l->l_phdr[n].p_memsz)
163       return 1;
164   return 0;
165 }