Debian Codegraph @

ext/GOTCHA/src/elf_ops.h

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 #if !defined(ELF_OPS_H_)
17 #define ELF_OPS_H_
18
19 #include <elf.h>
20 #include <link.h>
21
22 #include "library_filters.h"
23
24 /*!
25 ******************************************************************************
26 * \def GOTCHA_CHECK_VISIBILITY(sym)
27 * \brief Checks whether a given symbol is associated with a real function
28 * \param sym The symbol you wish to check
29 ******************************************************************************
30 */
31 #define GOTCHA_CHECK_VISIBILITY(sym) ((sym.st_size > 0))
32
33 /*!
34 ******************************************************************************
35 *
36 * \fn signed long lookup_gnu_hash_symbol(const char *name,
37 ElfW(Sym) *syms,
38 ElfW(Half) *versym,
39 char *symnames,
40 void *header);
41 *
42 * \brief Looks up the index of a symbol in a symbol table given a symbol name
43 *
44 * \param name The name of the function to be looked up
45 * \param syms The pointer to the symbol table
46 * \param versym The pointer to the symbol versioning table
47 * \param symnames A pointer into the string table
48 * \param header The parameters the underlying GNU Hash function will use
49 *
50 ******************************************************************************
51 */
52 signed long lookup_gnu_hash_symbol(const char *name, ElfW(Sym) * syms,
53 ElfW(Half) * versym, char *symnames,
54 void *sheader);
55
56 /*!
57 ******************************************************************************
58 *
59 * \fn signed long lookup_elf_hash_symbol(const char *name,
60 ElfW(Sym) *syms,
61 ElfW(Half) *versym,
62 char *symnames,
63 ElfW(Word) *header);
64 *
65 * \brief Looks up the index of a symbol in a symbol table given a symbol name
66 *
67 * \param name The name of the function to be looked up
68 * \param syms The pointer to the symbol table
69 * \param versym The pointer to the symbol versioning table
70 * \param symnames A pointer into the string table
71 * \param header The parameters the underlying ELF Hash function will use
72 *
73 ******************************************************************************
74 */
75 signed long lookup_elf_hash_symbol(const char *name, ElfW(Sym) * syms,
76 ElfW(Half) * versym, char *symnames,
77 ElfW(Word) * header);
78
79 /*!
80 ******************************************************************************
81 *
82 * \def INIT_DYNAMIC(lmap)
83 *
84 * \brief This macro initializes a set of variables from an link.h
85 * link_map entry
86 *
87 * \param lmap The link map entry associated with this symbol
88 * \param[out] dynsec The dynamic section associated with the link_map
89 * \param[out] rela
90 * \param[out] rel
91 * \param[out] jmprel
92 * \param[out] symtab
93 * \param[out] gnu_hash
94 * \param[out] got
95 * \param[out] strtab
96 * \param[out] versym
97 *
98 ******************************************************************************
99 */
100 #define INIT_DYNAMIC(lmap) \
101 ElfW(Dyn) *dynsec = NULL, *dentry = NULL; \
102 ElfW(Rela) *rela = NULL; \
103 ElfW(Rel) *rel = NULL; \
104 ElfW(Addr) jmprel = 0; \
105 ElfW(Sym) *symtab = NULL; \
106 ElfW(Addr) gnu_hash = 0x0, elf_hash = 0x0; \
107 ElfW(Addr) got = 0x0; \
108 ElfW(Half) *versym = NULL; \
109 char *strtab = NULL; \
110 unsigned int rel_size = 0, rel_count = 0, is_rela = 0, i; \
111 unsigned int rela_size = 0, rela_count = 0, rela_esz = 1; \
112 unsigned int jmprel_size = 0, jmprel_count = 0, rel_esz = 1; \
113 dynsec = lmap->l_ld; \
114 if (!dynsec) return -1; \
115 for (dentry = dynsec; dentry->d_tag != DT_NULL; dentry++) { \
116 switch (dentry->d_tag) { \
117 case DT_REL: { \
118 rel = (ElfW(Rel) *)dentry->d_un.d_ptr; \
119 break; \
120 } \
121 case DT_RELSZ: { \
122 rel_size = (unsigned int)dentry->d_un.d_val; \
123 break; \
124 } \
125 case DT_RELENT: { \
126 rel_esz = (unsigned int)dentry->d_un.d_val; \
127 break; \
128 } \
129 case DT_RELA: { \
130 rela = (ElfW(Rela) *)dentry->d_un.d_ptr; \
131 break; \
132 } \
133 case DT_RELASZ: { \
134 rela_size = (unsigned int)dentry->d_un.d_val; \
135 break; \
136 } \
137 case DT_RELAENT: { \
138 rela_esz = (unsigned int)dentry->d_un.d_val; \
139 break; \
140 } \
141 case DT_PLTRELSZ: { \
142 jmprel_size = (unsigned int)dentry->d_un.d_val; \
143 break; \
144 } \
145 case DT_PLTGOT: { \
146 got = dentry->d_un.d_ptr; \
147 break; \
148 } \
149 case DT_HASH: { \
150 elf_hash = dentry->d_un.d_val; \
151 break; \
152 } \
153 case DT_STRTAB: { \
154 strtab = (char *)dentry->d_un.d_ptr; \
155 break; \
156 } \
157 case DT_SYMTAB: { \
158 symtab = (ElfW(Sym) *)dentry->d_un.d_ptr; \
159 break; \
160 } \
161 case DT_PLTREL: { \
162 is_rela = (dentry->d_un.d_val == DT_RELA); \
163 break; \
164 } \
165 case DT_JMPREL: { \
166 jmprel = dentry->d_un.d_ptr; \
167 break; \
168 } \
169 case DT_GNU_HASH: { \
170 gnu_hash = dentry->d_un.d_val; \
171 break; \
172 } \
173 case DT_VERSYM: { \
174 versym = (ElfW(Half) *)dentry->d_un.d_ptr; \
175 break; \
176 } \
177 } \
178 } \
179 jmprel_count = \
180 jmprel_size / (is_rela ? sizeof(ElfW(Rela)) : sizeof(ElfW(Rel))); \
181 if (rel) rel_count = rel_size / rel_esz; \
182 if (rela) rela_count = rela_size / rela_esz; \
183 (void)rela; \
184 (void)jmprel_count; \
185 (void)rela_count; \
186 (void)rel; \
187 (void)jmprel; \
188 (void)symtab; \
189 (void)gnu_hash; \
190 (void)elf_hash; \
191 (void)got; \
192 (void)strtab; \
193 (void)rel_size; \
194 (void)rel_count; \
195 (void)is_rela; \
196 (void)versym; \
197 (void)i;
198
199 /*!
200 ******************************************************************************
201 *
202 * \def FOR_EACH_PLTREL_INT(relptr, op, ...)
203 *
204 * \brief This macro calls an operation on each relocation in a dynamic section.
205 * It should be called only by FOR_EACH_PLTREL, users should not call it
206 *
207 * \param relptr The pointer to the first relocation in the section
208 * \param op The operation to be performed on each relocation
209 * \param ... Any additional arguments you wish to pass to op
210 *
211 ******************************************************************************
212 */
213 #define FOR_EACH_PLTREL_INT(relptr, rel_count, op, ...) \
214 for (i = 0; i < rel_count; i++) { \
215 ElfW(Addr) offset = relptr[i].r_offset; \
216 unsigned long symidx = R_SYM(relptr[i].r_info); \
217 ElfW(Sym) *sym = symtab + symidx; \
218 char *symname = strtab + sym->st_name; \
219 op(sym, symname, offset, ##__VA_ARGS__); \
220 }
221
222 /*!
223 ******************************************************************************
224 *
225 * \def FOR_EACH_PLTREL(lmap, op, ...)
226 *
227 * \brief This macro calls an operation on each relocation in the dynamic
228 *section associated with a link map entry
229 *
230 * \param lmap The link map whose relocations you want processed
231 * \param op The operation to be performed on each relocation
232 * \param ... Any additional arguments you wish to pass to op
233 *
234 ******************************************************************************
235 */
236 #define FOR_EACH_PLTREL(lmap, op, ...) \
237 { \
238 INIT_DYNAMIC(lmap) \
239 ElfW(Addr) offset = lmap->l_addr; \
240 (void)offset; \
241 if (is_rela) { \
242 ElfW(Rela) *jmp_rela = (ElfW(Rela) *)jmprel; \
243 FOR_EACH_PLTREL_INT(jmp_rela, jmprel_count, op, ##__VA_ARGS__); \
244 if (rela) { \
245 FOR_EACH_PLTREL_INT(rela, rela_count, op, ##__VA_ARGS__); \
246 } \
247 } else { \
248 ElfW(Rel) *jmp_rel = (ElfW(Rel) *)jmprel; \
249 FOR_EACH_PLTREL_INT(jmp_rel, jmprel_count, op, ##__VA_ARGS__); \
250 if (rel) { \
251 FOR_EACH_PLTREL_INT(rel, rel_count, op, ##__VA_ARGS__); \
252 } \
253 } \
254 }
255
256 #endif