Debian Codegraph @

ext/GOTCHA/src/elf_ops.c

raw symbols SCIP
1 /*
2 This file is part of GOTCHA. For copyright information see the COPYRIGHT
3 file in the top level directory, or at
4 https://github.com/LLNL/gotcha/blob/master/COPYRIGHT
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU Lesser General Public License (as published by the Free
7 Software Foundation) version 2.1 dated February 1999. This program is
8 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 PURPOSE. See the terms and conditions of the GNU Lesser General Public License
11 for more details. You should have received a copy of the GNU Lesser General
12 Public License along with this program; if not, write to the Free Software
13 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14 */
15
16 #include "elf_ops.h"
17
18 #include <elf.h>
19
20 #include "libc_wrappers.h"
21 struct gnu_hash_header {
22 uint32_t nbuckets; //!< The number of buckets to hash symbols into
23 uint32_t symndx; //!< Index of the first symbol accessible via hashtable in
24 //!< the symbol table
25 uint32_t maskwords; //!< Number of words in the hash table's bloom filter
26 uint32_t shift2; //!< The bloom filter's shift count
27 };
28
29 static uint32_t gnu_hash_func(const char *str) {
30 uint32_t hash = 5381;
31 for (; *str != '\0'; str++) {
32 hash = hash * 33 + *str;
33 }
34 return hash;
35 }
36
37 /* Symbol versioning
38 *
39 * https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA.junk/symversion.html
40 *
41 * versym[symidx] does only provides an index into the ElfW(Verdef) array
42 * (DT_VERDEF/SHT_GNU_verdef) and is not the version itself, but SHT_GNU_verdef
43 * is sorted in ascending order and the entries have a parent relation, thus a
44 * higher index should always be a higher version. As we only search for the
45 * latest symbol/highest version it is sufficient to compare the index.
46 */
47
48 signed long lookup_gnu_hash_symbol(const char *name, ElfW(Sym) * syms,
49 ElfW(Half) * versym, char *symnames,
50 void *sheader) {
51 uint32_t *buckets, *vals;
52 uint32_t hash_val;
53 uint32_t cur_sym, cur_sym_hashval;
54 signed long latest_sym = -1;
55 ElfW(Half) latest_sym_ver = 0;
56 struct gnu_hash_header *header = (struct gnu_hash_header *)(sheader);
57
58 buckets = (uint32_t *)(((unsigned char *)(header + 1)) +
59 (header->maskwords * sizeof(ElfW(Addr))));
60 vals = buckets + header->nbuckets;
61
62 hash_val = gnu_hash_func(name);
63 cur_sym = buckets[hash_val % header->nbuckets];
64 if (cur_sym == 0) {
65 return -1;
66 }
67
68 hash_val &= ~1;
69 for (;;) {
70 cur_sym_hashval = vals[cur_sym - header->symndx];
71 if (((cur_sym_hashval & ~1) == hash_val) &&
72 (gotcha_strcmp(name, symnames + syms[cur_sym].st_name) == 0)) {
73 if (!versym) {
74 return (signed long)cur_sym;
75 }
76
77 if ((versym[cur_sym] & 0x7fff) > latest_sym_ver) {
78 latest_sym = (signed long)cur_sym;
79 latest_sym_ver = versym[cur_sym] & 0x7fff;
80 }
81 }
82 if (cur_sym_hashval & 1) {
83 break;
84 }
85 cur_sym++;
86 }
87
88 return latest_sym;
89 }
90
91 static unsigned long elf_hash(const unsigned char *name) {
92 unsigned int h = 0, g;
93 while (*name != '\0') {
94 h = (h << 4) + *name++;
95 if ((g = h & 0xf0000000)) {
96 h ^= g >> 24;
97 }
98 h &= ~g;
99 }
100 return h;
101 }
102
103 signed long lookup_elf_hash_symbol(const char *name, ElfW(Sym) * syms,
104 ElfW(Half) * versym, char *symnames,
105 ElfW(Word) * header) {
106 ElfW(Word) *nbucket = header + 0;
107 ElfW(Word) *buckets = header + 2;
108 ElfW(Word) *chains = buckets + *nbucket;
109 signed long latest_sym = -1;
110 ElfW(Half) latest_sym_ver = 0;
111
112 unsigned int x = elf_hash((const unsigned char *)name);
113 signed long y = (signed long)buckets[x % *nbucket];
114 while (y != STN_UNDEF) {
115 if (gotcha_strcmp(name, symnames + syms[y].st_name) == 0) {
116 if (!versym) {
117 // in general all libs would have version but it is a guard condition.
118 return y; // GCOVR_EXCL_LINE
119 }
120
121 if ((versym[y] & 0x7fff) > latest_sym_ver) {
122 latest_sym = y;
123 latest_sym_ver = versym[y] & 0x7fff;
124 }
125 }
126 y = chains[y];
127 }
128
129 return latest_sym;
130 }