1 /* Cache the location of a link map hash table.
2    Copyright (C) 1995-2022 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    <https://www.gnu.org/licenses/>.  */
18 
19 #include <assert.h>
20 #include <link.h>
21 #include <ldsodefs.h>
22 
23 void
_dl_setup_hash(struct link_map * map)24 _dl_setup_hash (struct link_map *map)
25 {
26   Elf_Symndx *hash;
27 
28   if (__glibc_likely (map->l_info[ELF_MACHINE_GNU_HASH_ADDRIDX] != NULL))
29     {
30       Elf32_Word *hash32
31         = (void *) D_PTR (map, l_info[ELF_MACHINE_GNU_HASH_ADDRIDX]);
32       map->l_nbuckets = *hash32++;
33       Elf32_Word symbias = *hash32++;
34       Elf32_Word bitmask_nwords = *hash32++;
35       /* Must be a power of two.  */
36       assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
37       map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
38       map->l_gnu_shift = *hash32++;
39 
40       map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
41       hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
42 
43       map->l_gnu_buckets = hash32;
44       hash32 += map->l_nbuckets;
45       map->l_gnu_chain_zero = hash32 - symbias;
46 
47       /* Initialize MIPS xhash translation table.  */
48       ELF_MACHINE_XHASH_SETUP (hash32, symbias, map);
49 
50       return;
51     }
52 
53   if (!map->l_info[DT_HASH])
54     return;
55   hash = (void *) D_PTR (map, l_info[DT_HASH]);
56 
57   map->l_nbuckets = *hash++;
58   /* Skip nchain.  */
59   hash++;
60   map->l_buckets = hash;
61   hash += map->l_nbuckets;
62   map->l_chain = hash;
63 }
64