Debian Codegraph @

ext/GOTCHA/src/gotcha_auxv.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 "gotcha_auxv.h"
17
18 #include <elf.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <link.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include "gotcha_utils.h"
30 #include "libc_wrappers.h"
31
32 static ElfW(Ehdr) *vdso_ehdr = NULL;
33 static unsigned int auxv_pagesz = 0;
34 #define BUFFER_LEN 4096
35
36 int parse_auxv_contents() {
37 char name[] = "/proc/self/auxv";
38 int fd, done = 0;
39 char buffer[BUFFER_LEN];
40 const ssize_t buffer_size = BUFFER_LEN;
41 ssize_t offset = 0, result;
42 ElfW(auxv_t) * auxv, *a;
43 static int parsed_auxv = 0;
44
45 if (parsed_auxv) return parsed_auxv == -1 ? parsed_auxv : 0;
46 parsed_auxv = 1;
47
48 fd = gotcha_open(name, O_RDONLY);
49 if (fd == -1) {
50 parsed_auxv = -1; // GCOVR_EXCL_LINE
51 return -1; // GCOVR_EXCL_LINE
52 }
53
54 do {
55 for (;;) {
56 result = gotcha_read(fd, buffer + offset, buffer_size - offset);
57 if (result == -1) {
58 if (errno == EINTR) // GCOVR_EXCL_START
59 continue;
60 gotcha_close(fd);
61 parsed_auxv = -1;
62 return -1;
63 } // GCOVR_EXCL_STOP
64 if (result == 0) {
65 gotcha_close(fd);
66 done = 1;
67 break;
68 }
69 if (offset == buffer_size) {
70 break; // GCOVR_EXCL_LINE
71 }
72 offset += result;
73 }
74
75 auxv = (ElfW(auxv_t) *)buffer;
76 for (a = auxv; a->a_type != AT_NULL; a++) {
77 if (a->a_type == AT_SYSINFO_EHDR) {
78 vdso_ehdr = (ElfW(Ehdr) *)a->a_un.a_val;
79 } else if (a->a_type == AT_PAGESZ) {
80 auxv_pagesz = (int)a->a_un.a_val;
81 }
82 }
83 } while (!done);
84
85 return 0;
86 }
87
88 struct link_map *get_vdso_from_auxv() {
89 struct link_map *m;
90
91 ElfW(Phdr) *vdso_phdrs = NULL;
92 ElfW(Half) vdso_phdr_num, p;
93 ElfW(Addr) vdso_dynamic = 0;
94
95 parse_auxv_contents();
96 if (vdso_ehdr) {
97 vdso_phdrs =
98 (ElfW(Phdr) *)(vdso_ehdr->e_phoff + ((unsigned char *)vdso_ehdr));
99 vdso_phdr_num = vdso_ehdr->e_phnum;
100
101 for (p = 0; p < vdso_phdr_num; p++) {
102 if (vdso_phdrs[p].p_type == PT_DYNAMIC) {
103 vdso_dynamic = (ElfW(Addr))vdso_phdrs[p].p_vaddr;
104 }
105 }
106
107 for (m = _r_debug.r_map; m; m = m->l_next) {
108 if (m->l_addr + vdso_dynamic == (ElfW(Addr))m->l_ld) {
109 return m;
110 }
111 }
112 }
113 return NULL; // GCOVR_EXCL_LINE
114 }
115
116 unsigned int get_auxv_pagesize() {
117 int result;
118 result = parse_auxv_contents();
119 return result == -1 ? 0 : auxv_pagesz;
120 }
121
122 static char *vdso_aliases[] = {"linux-vdso.so", "linux-gate.so", NULL};
123
124 struct link_map *get_vdso_from_aliases() {
125 struct link_map *m;
126 char **aliases;
127
128 for (m = _r_debug.r_map; m; m = m->l_next) {
129 for (aliases = vdso_aliases; *aliases; aliases++) {
130 if (m->l_name && gotcha_strcmp(m->l_name, *aliases) == 0) {
131 return m; // GCOVR_EXCL_LINE
132 }
133 }
134 }
135 return NULL;
136 }
137
138 static int read_line(char *line, int size, int fd) {
139 int i;
140 for (i = 0; i < size - 1; i++) {
141 int result = gotcha_read(fd, line + i, 1);
142 if (result == -1 && errno == EINTR) continue; // GCOVR_EXCL_LINE
143 if (result == -1 || result == 0) {
144 line[i] = '\0'; // GCOVR_EXCL_LINE
145 return -1; // GCOVR_EXCL_LINE
146 }
147 if (line[i] == '\n') {
148 line[i + 1] = '\0';
149 return 0;
150 }
151 }
152 line[size - 1] = '\0'; // GCOVR_EXCL_LINE
153 return 0; // GCOVR_EXCL_LINE
154 }
155
156 static int read_hex(char *str, unsigned long *val) {
157 unsigned long local_val = 0, len = 0;
158 for (;;) {
159 if (*str >= '0' && *str <= '9') {
160 local_val = (local_val * 16) + (*str - '0');
161 len++;
162 } else if (*str >= 'a' && *str <= 'f') {
163 local_val = (local_val * 16) + (*str - 'a' + 10);
164 len++;
165 } else if (*str >= 'A' && *str <= 'F') {
166 local_val = (local_val * 16) + (*str - 'A' + 10);
167 len++;
168 } else {
169 *val = local_val;
170 return len;
171 }
172 str++;
173 }
174 }
175
176 static int read_word(char *str, char *word, int word_size) {
177 int word_cur = 0;
178 int len = 0;
179 while (*str == ' ' || *str == '\t' || *str == '\n') {
180 str++;
181 len++;
182 }
183 if (*str == '\0') {
184 *word = '\0';
185 return len;
186 }
187 while (*str != ' ' && *str != '\t' && *str != '\n' && *str != '\0') {
188 if (word && word_cur >= word_size) {
189 if (word_size > 0 && word) // GCOVR_EXCL_START
190 word[word_size - 1] = '\0';
191 return word_cur;
192 } // GCOVR_EXCL_STOP
193 if (word) word[word_cur] = *str;
194 word_cur++;
195 str++;
196 len++;
197 }
198 if (word_cur >= word_size) word_cur--;
199 if (word) word[word_cur] = '\0';
200 return len;
201 }
202
203 struct link_map *get_vdso_from_maps() {
204 int maps, hit_eof;
205 ElfW(Addr) addr_begin, addr_end, dynamic;
206 char name[BUFFER_LEN], line[BUFFER_LEN], *line_pos;
207 struct link_map *m;
208 maps = gotcha_open("/proc/self/maps", O_RDONLY);
209 for (;;) {
210 hit_eof = read_line(line, BUFFER_LEN, maps);
211 if (hit_eof) {
212 gotcha_close(maps); // GCOVR_EXCL_LINE
213 return NULL; // GCOVR_EXCL_LINE
214 }
215 line_pos = line;
216 line_pos += read_hex(line_pos, &addr_begin);
217 if (*line_pos != '-') continue; // GCOVR_EXCL_LINE
218 line_pos++;
219 line_pos += read_hex(line_pos, &addr_end);
220 line_pos += read_word(line_pos, NULL, 0);
221 line_pos += read_word(line_pos, NULL, 0);
222 line_pos += read_word(line_pos, NULL, 0);
223 line_pos += read_word(line_pos, NULL, 0);
224 line_pos += read_word(line_pos, name, sizeof(name));
225 if (gotcha_strcmp(name, "[vdso]") == 0) {
226 gotcha_close(maps);
227 break;
228 }
229 }
230
231 for (m = _r_debug.r_map; m; m = m->l_next) {
232 dynamic = (ElfW(Addr))m->l_ld;
233 if (dynamic >= addr_begin && dynamic < addr_end) return m;
234 }
235
236 return NULL; // GCOVR_EXCL_LINE
237 }
238
239 int is_vdso(const struct link_map *map) {
240 static int vdso_checked = 0;
241 static struct link_map *vdso = NULL;
242 struct link_map *result;
243
244 if (!map) return 0;
245 if (vdso_checked) return (map == vdso);
246
247 vdso_checked = 1;
248
249 result = get_vdso_from_aliases();
250 if (result) {
251 vdso = result; // GCOVR_EXCL_LINE
252 return (map == vdso); // GCOVR_EXCL_LINE
253 }
254
255 result = get_vdso_from_auxv();
256 if (result) {
257 vdso = result;
258 return (map == vdso);
259 }
260
261 result = get_vdso_from_maps(); // GCOVR_EXCL_START
262 if (result) {
263 vdso = result;
264 return (map == vdso);
265 }
266
267 return 0;
268 } // GCOVR_EXCL_STOP