xref: /freebsd/lib/libproc/proc_sym.c (revision 23f6875a43f7ce365f2d52cf857da010c47fb03b)
1 /*-
2  * Copyright (c) 2016 Mark Johnston <markj@FreeBSD.org>
3  * Copyright (c) 2010 The FreeBSD Foundation
4  * Copyright (c) 2008 John Birrell (jb@freebsd.org)
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Rui Paulo under sponsorship
8  * from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/types.h>
36 #ifndef NO_CTF
37 #include <sys/ctf.h>
38 #include <sys/ctf_api.h>
39 #endif
40 #include <sys/user.h>
41 
42 #include <assert.h>
43 #include <err.h>
44 #include <fcntl.h>
45 #include <libgen.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #ifndef NO_CTF
51 #include <libctf.h>
52 #endif
53 #include <libutil.h>
54 
55 #include "crc32.h"
56 #include "_libproc.h"
57 
58 #define	PATH_DEBUG_DIR	"/usr/lib/debug"
59 
60 #ifdef NO_CTF
61 typedef struct ctf_file ctf_file_t;
62 #endif
63 
64 #ifndef NO_CXA_DEMANGLE
65 extern char *__cxa_demangle(const char *, char *, size_t *, int *);
66 #endif /* NO_CXA_DEMANGLE */
67 
68 static int
69 crc32_file(int fd, uint32_t *crc)
70 {
71 	uint8_t buf[PAGE_SIZE], *p;
72 	size_t n;
73 
74 	*crc = ~0;
75 	while ((n = read(fd, buf, sizeof(buf))) > 0) {
76 		p = &buf[0];
77 		while (n-- > 0)
78 			*crc = crc32_tab[(*crc ^ *p++) & 0xff] ^ (*crc >> 8);
79 	}
80 	*crc = ~*crc;
81 	return (n);
82 }
83 
84 static void
85 demangle(const char *symbol, char *buf, size_t len)
86 {
87 #ifndef NO_CXA_DEMANGLE
88 	char *dembuf;
89 
90 	if (symbol[0] == '_' && symbol[1] == 'Z' && symbol[2]) {
91 		dembuf = __cxa_demangle(symbol, NULL, NULL, NULL);
92 		if (!dembuf)
93 			goto fail;
94 		strlcpy(buf, dembuf, len);
95 		free(dembuf);
96 		return;
97 	}
98 fail:
99 #endif /* NO_CXA_DEMANGLE */
100 	strlcpy(buf, symbol, len);
101 }
102 
103 static int
104 symvalcomp(void *thunk, const void *a1, const void *a2)
105 {
106 	struct symtab *symtab;
107 	GElf_Sym sym1, sym2;
108 	u_int i1, i2;
109 	int ret;
110 
111 	i1 = *(const u_int *)a1;
112 	i2 = *(const u_int *)a2;
113 	symtab = thunk;
114 
115 	(void)gelf_getsym(symtab->data, i1, &sym1);
116 	(void)gelf_getsym(symtab->data, i2, &sym2);
117 	if (sym1.st_value < sym2.st_value)
118 		ret = -1;
119 	else if (sym1.st_value == sym2.st_value)
120 		ret = 0;
121 	else
122 		ret = 1;
123 	return (ret);
124 }
125 
126 static int
127 load_symtab(Elf *e, struct symtab *symtab, u_long sh_type)
128 {
129 	GElf_Ehdr ehdr;
130 	GElf_Shdr shdr;
131 	Elf_Scn *scn;
132 	u_int nsyms;
133 
134 	if (gelf_getehdr(e, &ehdr) == NULL)
135 		return (-1);
136 
137 	scn = NULL;
138 	while ((scn = elf_nextscn(e, scn)) != NULL) {
139 		(void)gelf_getshdr(scn, &shdr);
140 		if (shdr.sh_type == sh_type)
141 			break;
142 	}
143 	if (scn == NULL)
144 		return (-1);
145 
146 	nsyms = shdr.sh_size / shdr.sh_entsize;
147 	if (nsyms > (1 << 20))
148 		return (-1);
149 
150 	if ((symtab->data = elf_getdata(scn, NULL)) == NULL)
151 		return (-1);
152 
153 	symtab->index = calloc(nsyms, sizeof(u_int));
154 	if (symtab->index == NULL)
155 		return (-1);
156 	for (u_int i = 0; i < nsyms; i++)
157 		symtab->index[i] = i;
158 	qsort_r(symtab->index, nsyms, sizeof(u_int), symtab, symvalcomp);
159 	symtab->nsyms = nsyms;
160 	symtab->stridx = shdr.sh_link;
161 	return (0);
162 }
163 
164 static void
165 load_symtabs(struct file_info *file)
166 {
167 
168 	file->symtab.nsyms = file->dynsymtab.nsyms = 0;
169 	(void)load_symtab(file->elf, &file->symtab, SHT_SYMTAB);
170 	(void)load_symtab(file->elf, &file->dynsymtab, SHT_DYNSYM);
171 }
172 
173 static int
174 open_debug_file(char *path, const char *debugfile, uint32_t crc)
175 {
176 	size_t n;
177 	uint32_t compcrc;
178 	int fd;
179 
180 	fd = -1;
181 	if ((n = strlcat(path, "/", PATH_MAX)) >= PATH_MAX)
182 		return (fd);
183 	if (strlcat(path, debugfile, PATH_MAX) >= PATH_MAX)
184 		goto out;
185 	if ((fd = open(path, O_RDONLY | O_CLOEXEC)) < 0)
186 		goto out;
187 	if (crc32_file(fd, &compcrc) != 0 || crc != compcrc) {
188 		DPRINTFX("ERROR: CRC32 mismatch for %s", path);
189 		(void)close(fd);
190 		fd = -1;
191 	}
192 out:
193 	path[n] = '\0';
194 	return (fd);
195 }
196 
197 /*
198  * Obtain an ELF descriptor for the specified mapped object. If a GNU debuglink
199  * section is present, a descriptor for the corresponding debug file is
200  * returned.
201  */
202 static int
203 open_object(struct map_info *mapping)
204 {
205 	char path[PATH_MAX];
206 	GElf_Shdr shdr;
207 	Elf *e, *e2;
208 	Elf_Data *data;
209 	Elf_Scn *scn;
210 	struct file_info *file;
211 	prmap_t *map;
212 	const char *debugfile, *scnname;
213 	size_t ndx;
214 	uint32_t crc;
215 	int fd, fd2;
216 
217 	if (mapping->map.pr_mapname[0] == '\0')
218 		return (-1); /* anonymous object */
219 	if (mapping->file->elf != NULL)
220 		return (0); /* already loaded */
221 
222 	file = mapping->file;
223 	map = &mapping->map;
224 	if ((fd = open(map->pr_mapname, O_RDONLY | O_CLOEXEC)) < 0) {
225 		DPRINTF("ERROR: open %s failed", map->pr_mapname);
226 		return (-1);
227 	}
228 	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
229 		DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
230 		goto err;
231 	}
232 	if (gelf_getehdr(e, &file->ehdr) != &file->ehdr) {
233 		DPRINTFX("ERROR: elf_getehdr() failed: %s", elf_errmsg(-1));
234 		goto err;
235 	}
236 
237 	scn = NULL;
238 	while ((scn = elf_nextscn(e, scn)) != NULL) {
239 		if (gelf_getshdr(scn, &shdr) != &shdr) {
240 			DPRINTFX("ERROR: gelf_getshdr failed: %s",
241 			    elf_errmsg(-1));
242 			goto err;
243 		}
244 		if (shdr.sh_type != SHT_PROGBITS)
245 			continue;
246 		if (elf_getshdrstrndx(e, &ndx) != 0) {
247 			DPRINTFX("ERROR: elf_getshdrstrndx failed: %s",
248 			    elf_errmsg(-1));
249 			goto err;
250 		}
251 		if ((scnname = elf_strptr(e, ndx, shdr.sh_name)) == NULL)
252 			continue;
253 
254 		if (strcmp(scnname, ".gnu_debuglink") == 0)
255 			break;
256 	}
257 	if (scn == NULL)
258 		goto internal;
259 
260 	if ((data = elf_getdata(scn, NULL)) == NULL) {
261 		DPRINTFX("ERROR: elf_getdata failed: %s", elf_errmsg(-1));
262 		goto err;
263 	}
264 
265 	/*
266 	 * The data contains a null-terminated file name followed by a 4-byte
267 	 * CRC.
268 	 */
269 	if (data->d_size < sizeof(crc) + 1) {
270 		DPRINTFX("ERROR: debuglink section is too small (%zd bytes)",
271 		    data->d_size);
272 		goto internal;
273 	}
274 	if (strnlen(data->d_buf, data->d_size) >= data->d_size - sizeof(crc)) {
275 		DPRINTFX("ERROR: no null-terminator in gnu_debuglink section");
276 		goto internal;
277 	}
278 
279 	debugfile = data->d_buf;
280 	memcpy(&crc, (char *)data->d_buf + data->d_size - sizeof(crc),
281 	    sizeof(crc));
282 
283 	/*
284 	 * Search for the debug file using the algorithm described in the gdb
285 	 * documentation:
286 	 * - look in the directory containing the object,
287 	 * - look in the subdirectory ".debug" of the directory containing the
288 	 *   object,
289 	 * - look in the global debug directories (currently /usr/lib/debug).
290 	 */
291 	(void)strlcpy(path, map->pr_mapname, sizeof(path));
292 	(void)dirname(path);
293 
294 	if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
295 		goto external;
296 
297 	if (strlcat(path, "/.debug", sizeof(path)) < sizeof(path) &&
298 	    (fd2 = open_debug_file(path, debugfile, crc)) >= 0)
299 		goto external;
300 
301 	(void)snprintf(path, sizeof(path), PATH_DEBUG_DIR);
302 	if (strlcat(path, map->pr_mapname, sizeof(path)) < sizeof(path)) {
303 		(void)dirname(path);
304 		if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
305 			goto external;
306 	}
307 
308 internal:
309 	/* We didn't find a debug file, just return the object's descriptor. */
310 	file->elf = e;
311 	file->fd = fd;
312 	load_symtabs(file);
313 	return (0);
314 
315 external:
316 	if ((e2 = elf_begin(fd2, ELF_C_READ, NULL)) == NULL) {
317 		DPRINTFX("ERROR: elf_begin failed: %s", elf_errmsg(-1));
318 		(void)close(fd2);
319 		goto err;
320 	}
321 	(void)elf_end(e);
322 	(void)close(fd);
323 	file->elf = e2;
324 	file->fd = fd2;
325 	load_symtabs(file);
326 	return (0);
327 
328 err:
329 	if (e != NULL)
330 		(void)elf_end(e);
331 	(void)close(fd);
332 	return (-1);
333 }
334 
335 char *
336 proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
337     size_t objnamesz)
338 {
339 	prmap_t *map;
340 	size_t i;
341 
342 	for (i = 0; i < p->nmappings; i++) {
343 		map = &p->mappings[i].map;
344 		if (addr >= map->pr_vaddr &&
345 		    addr < map->pr_vaddr + map->pr_size) {
346 			strlcpy(objname, map->pr_mapname, objnamesz);
347 			return (objname);
348 		}
349 	}
350 	return (NULL);
351 }
352 
353 int
354 proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
355 {
356 	char last[MAXPATHLEN], path[MAXPATHLEN], *base;
357 	prmap_t *map;
358 	size_t i;
359 	int error;
360 
361 	if (p->nmappings == 0)
362 		if (proc_rdagent(p) == NULL)
363 			return (-1);
364 
365 	error = 0;
366 	memset(last, 0, sizeof(last));
367 	for (i = 0; i < p->nmappings; i++) {
368 		map = &p->mappings[i].map;
369 		strlcpy(path, map->pr_mapname, sizeof(path));
370 		base = basename(path);
371 		/*
372 		 * We shouldn't call the callback twice with the same object.
373 		 * To do that we are assuming the fact that if there are
374 		 * repeated object names (i.e. different mappings for the
375 		 * same object) they occur next to each other.
376 		 */
377 		if (strcmp(base, last) == 0)
378 			continue;
379 		if ((error = (*func)(cd, map, base)) != 0)
380 			break;
381 		strlcpy(last, path, sizeof(last));
382 	}
383 	return (error);
384 }
385 
386 static struct map_info *
387 _proc_addr2map(struct proc_handle *p, uintptr_t addr)
388 {
389 	struct map_info *mapping;
390 	size_t i;
391 
392 	if (p->nmappings == 0)
393 		if (proc_rdagent(p) == NULL)
394 			return (NULL);
395 	for (i = 0; i < p->nmappings; i++) {
396 		mapping = &p->mappings[i];
397 		if (addr >= mapping->map.pr_vaddr &&
398 		    addr < mapping->map.pr_vaddr + mapping->map.pr_size)
399 			return (mapping);
400 	}
401 	return (NULL);
402 }
403 
404 prmap_t *
405 proc_addr2map(struct proc_handle *p, uintptr_t addr)
406 {
407 
408 	return (&_proc_addr2map(p, addr)->map);
409 }
410 
411 /*
412  * Look up the symbol at addr using a binary search, returning a copy of the
413  * symbol and its name.
414  */
415 static int
416 lookup_symbol_by_addr(Elf *elf, struct symtab *symtab, uintptr_t addr,
417     const char **namep, GElf_Sym *sym)
418 {
419 	Elf_Data *data;
420 	const char *s;
421 	int min, max, mid;
422 
423 	data = symtab->data;
424 	min = 0;
425 	max = symtab->nsyms - 1;
426 
427 	while (min <= max) {
428 		mid = (max + min) / 2;
429 		(void)gelf_getsym(data, symtab->index[mid], sym);
430 		if (addr >= sym->st_value &&
431 		    addr < sym->st_value + sym->st_size) {
432 			s = elf_strptr(elf, symtab->stridx, sym->st_name);
433 			if (s != NULL && namep != NULL)
434 				*namep = s;
435 			return (0);
436 		}
437 
438 		if (addr < sym->st_value)
439 			max = mid - 1;
440 		else
441 			min = mid + 1;
442 	}
443 	return (ENOENT);
444 }
445 
446 int
447 proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
448     size_t namesz, GElf_Sym *symcopy)
449 {
450 	struct file_info *file;
451 	struct map_info *mapping;
452 	const char *s;
453 	uintptr_t off;
454 	int error;
455 
456 	if ((mapping = _proc_addr2map(p, addr)) == NULL) {
457 		DPRINTFX("ERROR: proc_addr2map failed to resolve 0x%jx", addr);
458 		return (-1);
459 	}
460 	if (open_object(mapping) != 0) {
461 		DPRINTFX("ERROR: failed to open object %s",
462 		    mapping->map.pr_mapname);
463 		return (-1);
464 	}
465 
466 	file = mapping->file;
467 	off = file->ehdr.e_type == ET_DYN ? mapping->map.pr_vaddr : 0;
468 	if (addr < off)
469 		return (ENOENT);
470 	addr -= off;
471 
472 	error = lookup_symbol_by_addr(file->elf, &file->dynsymtab, addr, &s,
473 	    symcopy);
474 	if (error == ENOENT)
475 		error = lookup_symbol_by_addr(file->elf, &file->symtab, addr,
476 		    &s, symcopy);
477 	if (error == 0) {
478 		symcopy->st_value += off;
479 		demangle(s, name, namesz);
480 	}
481 	return (error);
482 }
483 
484 static struct map_info *
485 _proc_name2map(struct proc_handle *p, const char *name)
486 {
487 	char path[MAXPATHLEN], *base;
488 	struct map_info *mapping;
489 	size_t i, len;
490 
491 	if ((len = strlen(name)) == 0)
492 		return (NULL);
493 	if (p->nmappings == 0)
494 		if (proc_rdagent(p) == NULL)
495 			return (NULL);
496 	for (i = 0; i < p->nmappings; i++) {
497 		mapping = &p->mappings[i];
498 		(void)strlcpy(path, mapping->map.pr_mapname, sizeof(path));
499 		base = basename(path);
500 		if (strcmp(base, name) == 0)
501 			return (mapping);
502 	}
503 	/* If we didn't find a match, try matching prefixes of the basename. */
504 	for (i = 0; i < p->nmappings; i++) {
505 		strlcpy(path, p->mappings[i].map.pr_mapname, sizeof(path));
506 		base = basename(path);
507 		if (strncmp(base, name, len) == 0)
508 			return (&p->mappings[i]);
509 	}
510 	if (strcmp(name, "a.out") == 0)
511 		return (_proc_addr2map(p, p->exec_map->pr_vaddr));
512 	return (NULL);
513 }
514 
515 prmap_t *
516 proc_name2map(struct proc_handle *p, const char *name)
517 {
518 
519 	return (&_proc_name2map(p, name)->map);
520 }
521 
522 /*
523  * Look up the symbol with the given name and return a copy of it.
524  */
525 static int
526 lookup_symbol_by_name(Elf *elf, struct symtab *symtab, const char *symbol,
527     GElf_Sym *symcopy, prsyminfo_t *si)
528 {
529 	GElf_Sym sym;
530 	Elf_Data *data;
531 	char *s;
532 	int i;
533 
534 	if (symtab->nsyms == 0)
535 		return (ENOENT);
536 	data = symtab->data;
537 	for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
538 		s = elf_strptr(elf, symtab->stridx, sym.st_name);
539 		if (s != NULL && strcmp(s, symbol) == 0) {
540 			memcpy(symcopy, &sym, sizeof(*symcopy));
541 			if (si != NULL)
542 				si->prs_id = i;
543 			return (0);
544 		}
545 	}
546 	return (ENOENT);
547 }
548 
549 int
550 proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
551     GElf_Sym *symcopy, prsyminfo_t *si)
552 {
553 	struct file_info *file;
554 	struct map_info *mapping;
555 	uintptr_t off;
556 	int error;
557 
558 	if ((mapping = _proc_name2map(p, object)) == NULL) {
559 		DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
560 		return (-1);
561 	}
562 	if (open_object(mapping) != 0) {
563 		DPRINTFX("ERROR: failed to open object %s",
564 		    mapping->map.pr_mapname);
565 		return (-1);
566 	}
567 
568 	file = mapping->file;
569 	off = file->ehdr.e_type == ET_DYN ? mapping->map.pr_vaddr : 0;
570 
571 	error = lookup_symbol_by_name(file->elf, &file->dynsymtab, symbol,
572 	    symcopy, si);
573 	if (error == ENOENT)
574 		error = lookup_symbol_by_name(file->elf, &file->symtab, symbol,
575 		    symcopy, si);
576 	if (error == 0)
577 		symcopy->st_value += off;
578 	return (error);
579 }
580 
581 ctf_file_t *
582 proc_name2ctf(struct proc_handle *p, const char *name)
583 {
584 #ifndef NO_CTF
585 	ctf_file_t *ctf;
586 	prmap_t *map;
587 	int error;
588 
589 	if ((map = proc_name2map(p, name)) == NULL)
590 		return (NULL);
591 
592 	ctf = ctf_open(map->pr_mapname, &error);
593 	return (ctf);
594 #else
595 	(void)p;
596 	(void)name;
597 	return (NULL);
598 #endif
599 }
600 
601 int
602 proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
603     int mask, proc_sym_f *func, void *cd)
604 {
605 	GElf_Sym sym;
606 	struct file_info *file;
607 	struct map_info *mapping;
608 	struct symtab *symtab;
609 	const char *s;
610 	int error, i;
611 
612 	if ((mapping = _proc_name2map(p, object)) == NULL) {
613 		DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
614 		return (-1);
615 	}
616 	if (open_object(mapping) != 0) {
617 		DPRINTFX("ERROR: failed to open object %s",
618 		    mapping->map.pr_mapname);
619 		return (-1);
620 	}
621 
622 	file = mapping->file;
623 	symtab = which == PR_SYMTAB ? &file->symtab : &file->dynsymtab;
624 	if (symtab->nsyms == 0)
625 		return (-1);
626 
627 	error = 0;
628 	for (i = 0; gelf_getsym(symtab->data, i, &sym) != NULL; i++) {
629 		if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
630 		    (mask & BIND_LOCAL) == 0)
631 			continue;
632 		if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
633 		    (mask & BIND_GLOBAL) == 0)
634 			continue;
635 		if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
636 		    (mask & BIND_WEAK) == 0)
637 			continue;
638 		if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
639 		    (mask & TYPE_NOTYPE) == 0)
640 			continue;
641 		if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
642 		    (mask & TYPE_OBJECT) == 0)
643 			continue;
644 		if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
645 		    (mask & TYPE_FUNC) == 0)
646 			continue;
647 		if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
648 		    (mask & TYPE_SECTION) == 0)
649 			continue;
650 		if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
651 		    (mask & TYPE_FILE) == 0)
652 			continue;
653 		s = elf_strptr(file->elf, symtab->stridx, sym.st_name);
654 		if (file->ehdr.e_type == ET_DYN)
655 			sym.st_value += mapping->map.pr_vaddr;
656 		if ((error = (*func)(cd, &sym, s)) != 0)
657 			break;
658 	}
659 	return (error);
660 }
661