xref: /freebsd/lib/libproc/proc_sym.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
12c633af4SJohn Birrell /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
41bdc41d2SMark Johnston  * Copyright (c) 2016-2017 Mark Johnston <markj@FreeBSD.org>
58eb20f36SRui Paulo  * Copyright (c) 2010 The FreeBSD Foundation
62c633af4SJohn Birrell  * Copyright (c) 2008 John Birrell (jb@freebsd.org)
72c633af4SJohn Birrell  * All rights reserved.
82c633af4SJohn Birrell  *
98eb20f36SRui Paulo  * Portions of this software were developed by Rui Paulo under sponsorship
108eb20f36SRui Paulo  * from the FreeBSD Foundation.
118eb20f36SRui Paulo  *
122c633af4SJohn Birrell  * Redistribution and use in source and binary forms, with or without
132c633af4SJohn Birrell  * modification, are permitted provided that the following conditions
142c633af4SJohn Birrell  * are met:
152c633af4SJohn Birrell  * 1. Redistributions of source code must retain the above copyright
162c633af4SJohn Birrell  *    notice, this list of conditions and the following disclaimer.
172c633af4SJohn Birrell  * 2. Redistributions in binary form must reproduce the above copyright
182c633af4SJohn Birrell  *    notice, this list of conditions and the following disclaimer in the
192c633af4SJohn Birrell  *    documentation and/or other materials provided with the distribution.
202c633af4SJohn Birrell  *
212c633af4SJohn Birrell  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
222c633af4SJohn Birrell  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
232c633af4SJohn Birrell  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
242c633af4SJohn Birrell  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
252c633af4SJohn Birrell  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
262c633af4SJohn Birrell  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
272c633af4SJohn Birrell  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
282c633af4SJohn Birrell  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
292c633af4SJohn Birrell  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
302c633af4SJohn Birrell  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
312c633af4SJohn Birrell  * SUCH DAMAGE.
322c633af4SJohn Birrell  */
332c633af4SJohn Birrell 
348eb20f36SRui Paulo #include <sys/types.h>
3541da933cSMark Johnston #ifndef NO_CTF
3641da933cSMark Johnston #include <sys/ctf.h>
3741da933cSMark Johnston #include <sys/ctf_api.h>
3841da933cSMark Johnston #endif
398eb20f36SRui Paulo #include <sys/user.h>
408eb20f36SRui Paulo 
418eb20f36SRui Paulo #include <assert.h>
428eb20f36SRui Paulo #include <err.h>
438eb20f36SRui Paulo #include <fcntl.h>
44540cc663SMark Johnston #include <libgen.h>
45540cc663SMark Johnston #include <stdio.h>
46540cc663SMark Johnston #include <stdlib.h>
478eb20f36SRui Paulo #include <string.h>
488eb20f36SRui Paulo #include <unistd.h>
4941da933cSMark Johnston #ifndef NO_CTF
5041da933cSMark Johnston #include <libctf.h>
5141da933cSMark Johnston #endif
528eb20f36SRui Paulo #include <libutil.h>
538eb20f36SRui Paulo 
540bbf2702SXin LI #include <zlib.h>
558eb20f36SRui Paulo #include "_libproc.h"
568eb20f36SRui Paulo 
5776c6531cSMark Johnston #define	PATH_DEBUG_DIR	"/usr/lib/debug"
5876c6531cSMark Johnston 
5941da933cSMark Johnston #ifdef NO_CTF
6041da933cSMark Johnston typedef struct ctf_file ctf_file_t;
6141da933cSMark Johnston #endif
6241da933cSMark Johnston 
633d12a343SDimitry Andric #ifndef NO_CXA_DEMANGLE
64cd906041SRui Paulo extern char *__cxa_demangle(const char *, char *, size_t *, int *);
653d12a343SDimitry Andric #endif /* NO_CXA_DEMANGLE */
66cd906041SRui Paulo 
6776c6531cSMark Johnston static int
crc32_file(int fd,uint32_t * crc)6876c6531cSMark Johnston crc32_file(int fd, uint32_t *crc)
6976c6531cSMark Johnston {
700bbf2702SXin LI 	char buf[MAXPHYS];
710bbf2702SXin LI 	ssize_t nr;
7276c6531cSMark Johnston 
730bbf2702SXin LI 	*crc = crc32(0L, Z_NULL, 0);
740bbf2702SXin LI 	while ((nr = read(fd, buf, sizeof(buf))) > 0) {
750bbf2702SXin LI 		*crc = crc32(*crc, (char *)buf, nr);
7676c6531cSMark Johnston 	}
770bbf2702SXin LI 	return (!!nr);
7876c6531cSMark Johnston }
7976c6531cSMark Johnston 
808eb20f36SRui Paulo static void
demangle(const char * symbol,char * buf,size_t len)81404087ccSRui Paulo demangle(const char *symbol, char *buf, size_t len)
82404087ccSRui Paulo {
833d12a343SDimitry Andric #ifndef NO_CXA_DEMANGLE
84404087ccSRui Paulo 	char *dembuf;
85404087ccSRui Paulo 
863d12a343SDimitry Andric 	if (symbol[0] == '_' && symbol[1] == 'Z' && symbol[2]) {
8796ec3cddSDimitry Andric 		dembuf = __cxa_demangle(symbol, NULL, NULL, NULL);
88404087ccSRui Paulo 		if (!dembuf)
89404087ccSRui Paulo 			goto fail;
90404087ccSRui Paulo 		strlcpy(buf, dembuf, len);
91404087ccSRui Paulo 		free(dembuf);
92404087ccSRui Paulo 		return;
9396ec3cddSDimitry Andric 	}
94404087ccSRui Paulo fail:
953d12a343SDimitry Andric #endif /* NO_CXA_DEMANGLE */
96404087ccSRui Paulo 	strlcpy(buf, symbol, len);
97404087ccSRui Paulo }
98404087ccSRui Paulo 
991bdc41d2SMark Johnston struct symsort_thunk {
1001bdc41d2SMark Johnston 	Elf *e;
101c156354fSMark Johnston 	struct symtab *symtab;
1021bdc41d2SMark Johnston };
1031bdc41d2SMark Johnston 
1041bdc41d2SMark Johnston static int
symvalcmp(const void * a1,const void * a2,void * _thunk)105af3c7888SEd Schouten symvalcmp(const void *a1, const void *a2, void *_thunk)
1061bdc41d2SMark Johnston {
107c156354fSMark Johnston 	GElf_Sym sym1, sym2;
1081bdc41d2SMark Johnston 	struct symsort_thunk *thunk;
1091bdc41d2SMark Johnston 	const char *s1, *s2;
110c156354fSMark Johnston 	u_int i1, i2;
1111bdc41d2SMark Johnston 	int bind1, bind2;
112c156354fSMark Johnston 
113c156354fSMark Johnston 	i1 = *(const u_int *)a1;
114c156354fSMark Johnston 	i2 = *(const u_int *)a2;
1151bdc41d2SMark Johnston 	thunk = _thunk;
116c156354fSMark Johnston 
1171bdc41d2SMark Johnston 	(void)gelf_getsym(thunk->symtab->data, i1, &sym1);
1181bdc41d2SMark Johnston 	(void)gelf_getsym(thunk->symtab->data, i2, &sym2);
1191bdc41d2SMark Johnston 
1201bdc41d2SMark Johnston 	if (sym1.st_value != sym2.st_value)
1211bdc41d2SMark Johnston 		return (sym1.st_value < sym2.st_value ? -1 : 1);
1221bdc41d2SMark Johnston 
1231bdc41d2SMark Johnston 	/* Prefer non-local symbols. */
1241bdc41d2SMark Johnston 	bind1 = GELF_ST_BIND(sym1.st_info);
1251bdc41d2SMark Johnston 	bind2 = GELF_ST_BIND(sym2.st_info);
1261bdc41d2SMark Johnston 	if (bind1 != bind2) {
1271bdc41d2SMark Johnston 		if (bind1 == STB_LOCAL && bind2 != STB_LOCAL)
1281bdc41d2SMark Johnston 			return (-1);
1291bdc41d2SMark Johnston 		if (bind1 != STB_LOCAL && bind2 == STB_LOCAL)
1301bdc41d2SMark Johnston 			return (1);
1311bdc41d2SMark Johnston 	}
1321bdc41d2SMark Johnston 
1331bdc41d2SMark Johnston 	s1 = elf_strptr(thunk->e, thunk->symtab->stridx, sym1.st_name);
1341bdc41d2SMark Johnston 	s2 = elf_strptr(thunk->e, thunk->symtab->stridx, sym2.st_name);
1351bdc41d2SMark Johnston 	if (s1 != NULL && s2 != NULL) {
1361bdc41d2SMark Johnston 		/* Prefer symbols without a leading '$'. */
1371bdc41d2SMark Johnston 		if (*s1 == '$')
1381bdc41d2SMark Johnston 			return (-1);
1391bdc41d2SMark Johnston 		if (*s2 == '$')
1401bdc41d2SMark Johnston 			return (1);
1411bdc41d2SMark Johnston 
1421bdc41d2SMark Johnston 		/* Prefer symbols with fewer leading underscores. */
1431bdc41d2SMark Johnston 		for (; *s1 == '_' && *s2 == '_'; s1++, s2++)
1441bdc41d2SMark Johnston 			;
1451bdc41d2SMark Johnston 		if (*s1 == '_')
1461bdc41d2SMark Johnston 			return (-1);
1471bdc41d2SMark Johnston 		if (*s2 == '_')
1481bdc41d2SMark Johnston 			return (1);
1491bdc41d2SMark Johnston 	}
1501bdc41d2SMark Johnston 
1511bdc41d2SMark Johnston 	return (0);
152c156354fSMark Johnston }
153c156354fSMark Johnston 
154c156354fSMark Johnston static int
load_symtab(Elf * e,struct symtab * symtab,u_long sh_type)155c156354fSMark Johnston load_symtab(Elf *e, struct symtab *symtab, u_long sh_type)
156c156354fSMark Johnston {
157c156354fSMark Johnston 	GElf_Ehdr ehdr;
158c156354fSMark Johnston 	GElf_Shdr shdr;
1591bdc41d2SMark Johnston 	struct symsort_thunk thunk;
160c156354fSMark Johnston 	Elf_Scn *scn;
161c156354fSMark Johnston 	u_int nsyms;
162c156354fSMark Johnston 
163c156354fSMark Johnston 	if (gelf_getehdr(e, &ehdr) == NULL)
164c156354fSMark Johnston 		return (-1);
165c156354fSMark Johnston 
166c156354fSMark Johnston 	scn = NULL;
167c156354fSMark Johnston 	while ((scn = elf_nextscn(e, scn)) != NULL) {
168c156354fSMark Johnston 		(void)gelf_getshdr(scn, &shdr);
169c156354fSMark Johnston 		if (shdr.sh_type == sh_type)
170c156354fSMark Johnston 			break;
171c156354fSMark Johnston 	}
172c156354fSMark Johnston 	if (scn == NULL)
173c156354fSMark Johnston 		return (-1);
174c156354fSMark Johnston 
1751f7967e1SMark Johnston 	nsyms = shdr.sh_size / shdr.sh_entsize;
1761f7967e1SMark Johnston 	if (nsyms > (1 << 20))
177c156354fSMark Johnston 		return (-1);
178c156354fSMark Johnston 
1791f7967e1SMark Johnston 	if ((symtab->data = elf_getdata(scn, NULL)) == NULL)
1801f7967e1SMark Johnston 		return (-1);
181c156354fSMark Johnston 
182c156354fSMark Johnston 	symtab->index = calloc(nsyms, sizeof(u_int));
183c156354fSMark Johnston 	if (symtab->index == NULL)
184c156354fSMark Johnston 		return (-1);
185c156354fSMark Johnston 	for (u_int i = 0; i < nsyms; i++)
186c156354fSMark Johnston 		symtab->index[i] = i;
187c156354fSMark Johnston 	symtab->nsyms = nsyms;
188c156354fSMark Johnston 	symtab->stridx = shdr.sh_link;
1891bdc41d2SMark Johnston 
1901bdc41d2SMark Johnston 	thunk.e = e;
1911bdc41d2SMark Johnston 	thunk.symtab = symtab;
192af3c7888SEd Schouten 	qsort_r(symtab->index, nsyms, sizeof(u_int), symvalcmp, &thunk);
1931bdc41d2SMark Johnston 
194c156354fSMark Johnston 	return (0);
195c156354fSMark Johnston }
196c156354fSMark Johnston 
197c156354fSMark Johnston static void
load_symtabs(struct file_info * file)198c156354fSMark Johnston load_symtabs(struct file_info *file)
199c156354fSMark Johnston {
200c156354fSMark Johnston 
201c156354fSMark Johnston 	file->symtab.nsyms = file->dynsymtab.nsyms = 0;
202c156354fSMark Johnston 	(void)load_symtab(file->elf, &file->symtab, SHT_SYMTAB);
203c156354fSMark Johnston 	(void)load_symtab(file->elf, &file->dynsymtab, SHT_DYNSYM);
204c156354fSMark Johnston }
205c156354fSMark Johnston 
206c156354fSMark Johnston static int
open_debug_file(char * path,const char * debugfile,uint32_t crc)20776c6531cSMark Johnston open_debug_file(char *path, const char *debugfile, uint32_t crc)
208eda94c07SRui Paulo {
20976c6531cSMark Johnston 	size_t n;
21076c6531cSMark Johnston 	uint32_t compcrc;
211eda94c07SRui Paulo 	int fd;
212eda94c07SRui Paulo 
21376c6531cSMark Johnston 	fd = -1;
21476c6531cSMark Johnston 	if ((n = strlcat(path, "/", PATH_MAX)) >= PATH_MAX)
215eda94c07SRui Paulo 		return (fd);
21676c6531cSMark Johnston 	if (strlcat(path, debugfile, PATH_MAX) >= PATH_MAX)
21776c6531cSMark Johnston 		goto out;
21876c6531cSMark Johnston 	if ((fd = open(path, O_RDONLY | O_CLOEXEC)) < 0)
21976c6531cSMark Johnston 		goto out;
22076c6531cSMark Johnston 	if (crc32_file(fd, &compcrc) != 0 || crc != compcrc) {
22176c6531cSMark Johnston 		DPRINTFX("ERROR: CRC32 mismatch for %s", path);
22276c6531cSMark Johnston 		(void)close(fd);
22376c6531cSMark Johnston 		fd = -1;
22476c6531cSMark Johnston 	}
22576c6531cSMark Johnston out:
22676c6531cSMark Johnston 	path[n] = '\0';
22776c6531cSMark Johnston 	return (fd);
22876c6531cSMark Johnston }
22976c6531cSMark Johnston 
23076c6531cSMark Johnston /*
23176c6531cSMark Johnston  * Obtain an ELF descriptor for the specified mapped object. If a GNU debuglink
23276c6531cSMark Johnston  * section is present, a descriptor for the corresponding debug file is
23376c6531cSMark Johnston  * returned.
23476c6531cSMark Johnston  */
23576c6531cSMark Johnston static int
open_object(struct map_info * mapping)23607a9c2e6SMark Johnston open_object(struct map_info *mapping)
23776c6531cSMark Johnston {
23876c6531cSMark Johnston 	char path[PATH_MAX];
23976c6531cSMark Johnston 	GElf_Shdr shdr;
24076c6531cSMark Johnston 	Elf *e, *e2;
24176c6531cSMark Johnston 	Elf_Data *data;
24276c6531cSMark Johnston 	Elf_Scn *scn;
24307a9c2e6SMark Johnston 	struct file_info *file;
24407a9c2e6SMark Johnston 	prmap_t *map;
24576c6531cSMark Johnston 	const char *debugfile, *scnname;
24676c6531cSMark Johnston 	size_t ndx;
24776c6531cSMark Johnston 	uint32_t crc;
24876c6531cSMark Johnston 	int fd, fd2;
24976c6531cSMark Johnston 
25007a9c2e6SMark Johnston 	if (mapping->map.pr_mapname[0] == '\0')
25107a9c2e6SMark Johnston 		return (-1); /* anonymous object */
25207a9c2e6SMark Johnston 	if (mapping->file->elf != NULL)
25307a9c2e6SMark Johnston 		return (0); /* already loaded */
25407a9c2e6SMark Johnston 
25507a9c2e6SMark Johnston 	file = mapping->file;
25607a9c2e6SMark Johnston 	map = &mapping->map;
25776c6531cSMark Johnston 	if ((fd = open(map->pr_mapname, O_RDONLY | O_CLOEXEC)) < 0) {
25876c6531cSMark Johnston 		DPRINTF("ERROR: open %s failed", map->pr_mapname);
25976c6531cSMark Johnston 		return (-1);
26076c6531cSMark Johnston 	}
26176c6531cSMark Johnston 	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
26276c6531cSMark Johnston 		DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
26376c6531cSMark Johnston 		goto err;
26476c6531cSMark Johnston 	}
265c156354fSMark Johnston 	if (gelf_getehdr(e, &file->ehdr) != &file->ehdr) {
266c156354fSMark Johnston 		DPRINTFX("ERROR: elf_getehdr() failed: %s", elf_errmsg(-1));
267c156354fSMark Johnston 		goto err;
268c156354fSMark Johnston 	}
26976c6531cSMark Johnston 
27076c6531cSMark Johnston 	scn = NULL;
27176c6531cSMark Johnston 	while ((scn = elf_nextscn(e, scn)) != NULL) {
27276c6531cSMark Johnston 		if (gelf_getshdr(scn, &shdr) != &shdr) {
27376c6531cSMark Johnston 			DPRINTFX("ERROR: gelf_getshdr failed: %s",
27476c6531cSMark Johnston 			    elf_errmsg(-1));
27576c6531cSMark Johnston 			goto err;
27676c6531cSMark Johnston 		}
27776c6531cSMark Johnston 		if (shdr.sh_type != SHT_PROGBITS)
27876c6531cSMark Johnston 			continue;
27976c6531cSMark Johnston 		if (elf_getshdrstrndx(e, &ndx) != 0) {
28076c6531cSMark Johnston 			DPRINTFX("ERROR: elf_getshdrstrndx failed: %s",
28176c6531cSMark Johnston 			    elf_errmsg(-1));
28276c6531cSMark Johnston 			goto err;
28376c6531cSMark Johnston 		}
28476c6531cSMark Johnston 		if ((scnname = elf_strptr(e, ndx, shdr.sh_name)) == NULL)
28576c6531cSMark Johnston 			continue;
28676c6531cSMark Johnston 
28776c6531cSMark Johnston 		if (strcmp(scnname, ".gnu_debuglink") == 0)
28876c6531cSMark Johnston 			break;
28976c6531cSMark Johnston 	}
29076c6531cSMark Johnston 	if (scn == NULL)
29176c6531cSMark Johnston 		goto internal;
29276c6531cSMark Johnston 
29376c6531cSMark Johnston 	if ((data = elf_getdata(scn, NULL)) == NULL) {
29476c6531cSMark Johnston 		DPRINTFX("ERROR: elf_getdata failed: %s", elf_errmsg(-1));
29576c6531cSMark Johnston 		goto err;
29676c6531cSMark Johnston 	}
29776c6531cSMark Johnston 
29876c6531cSMark Johnston 	/*
29976c6531cSMark Johnston 	 * The data contains a null-terminated file name followed by a 4-byte
30076c6531cSMark Johnston 	 * CRC.
30176c6531cSMark Johnston 	 */
30276c6531cSMark Johnston 	if (data->d_size < sizeof(crc) + 1) {
30376c6531cSMark Johnston 		DPRINTFX("ERROR: debuglink section is too small (%zd bytes)",
3049e5787d2SMatt Macy 		    (ssize_t)data->d_size);
30576c6531cSMark Johnston 		goto internal;
30676c6531cSMark Johnston 	}
30776c6531cSMark Johnston 	if (strnlen(data->d_buf, data->d_size) >= data->d_size - sizeof(crc)) {
30876c6531cSMark Johnston 		DPRINTFX("ERROR: no null-terminator in gnu_debuglink section");
30976c6531cSMark Johnston 		goto internal;
31076c6531cSMark Johnston 	}
31176c6531cSMark Johnston 
31276c6531cSMark Johnston 	debugfile = data->d_buf;
31376c6531cSMark Johnston 	memcpy(&crc, (char *)data->d_buf + data->d_size - sizeof(crc),
31476c6531cSMark Johnston 	    sizeof(crc));
31576c6531cSMark Johnston 
31676c6531cSMark Johnston 	/*
31776c6531cSMark Johnston 	 * Search for the debug file using the algorithm described in the gdb
31876c6531cSMark Johnston 	 * documentation:
31976c6531cSMark Johnston 	 * - look in the directory containing the object,
32076c6531cSMark Johnston 	 * - look in the subdirectory ".debug" of the directory containing the
32176c6531cSMark Johnston 	 *   object,
32276c6531cSMark Johnston 	 * - look in the global debug directories (currently /usr/lib/debug).
32376c6531cSMark Johnston 	 */
32476c6531cSMark Johnston 	(void)strlcpy(path, map->pr_mapname, sizeof(path));
32576c6531cSMark Johnston 	(void)dirname(path);
32676c6531cSMark Johnston 
32776c6531cSMark Johnston 	if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
32876c6531cSMark Johnston 		goto external;
32976c6531cSMark Johnston 
33076c6531cSMark Johnston 	if (strlcat(path, "/.debug", sizeof(path)) < sizeof(path) &&
33176c6531cSMark Johnston 	    (fd2 = open_debug_file(path, debugfile, crc)) >= 0)
33276c6531cSMark Johnston 		goto external;
33376c6531cSMark Johnston 
33476c6531cSMark Johnston 	(void)snprintf(path, sizeof(path), PATH_DEBUG_DIR);
33576c6531cSMark Johnston 	if (strlcat(path, map->pr_mapname, sizeof(path)) < sizeof(path)) {
33676c6531cSMark Johnston 		(void)dirname(path);
33776c6531cSMark Johnston 		if ((fd2 = open_debug_file(path, debugfile, crc)) >= 0)
33876c6531cSMark Johnston 			goto external;
33976c6531cSMark Johnston 	}
34076c6531cSMark Johnston 
34176c6531cSMark Johnston internal:
34276c6531cSMark Johnston 	/* We didn't find a debug file, just return the object's descriptor. */
34307a9c2e6SMark Johnston 	file->elf = e;
34407a9c2e6SMark Johnston 	file->fd = fd;
345c156354fSMark Johnston 	load_symtabs(file);
34676c6531cSMark Johnston 	return (0);
34776c6531cSMark Johnston 
34876c6531cSMark Johnston external:
34976c6531cSMark Johnston 	if ((e2 = elf_begin(fd2, ELF_C_READ, NULL)) == NULL) {
35076c6531cSMark Johnston 		DPRINTFX("ERROR: elf_begin failed: %s", elf_errmsg(-1));
35176c6531cSMark Johnston 		(void)close(fd2);
35276c6531cSMark Johnston 		goto err;
35376c6531cSMark Johnston 	}
35476c6531cSMark Johnston 	(void)elf_end(e);
35576c6531cSMark Johnston 	(void)close(fd);
35607a9c2e6SMark Johnston 	file->elf = e2;
35707a9c2e6SMark Johnston 	file->fd = fd2;
358c156354fSMark Johnston 	load_symtabs(file);
35976c6531cSMark Johnston 	return (0);
36076c6531cSMark Johnston 
36176c6531cSMark Johnston err:
36276c6531cSMark Johnston 	if (e != NULL)
36376c6531cSMark Johnston 		(void)elf_end(e);
36476c6531cSMark Johnston 	(void)close(fd);
36576c6531cSMark Johnston 	return (-1);
366eda94c07SRui Paulo }
367eda94c07SRui Paulo 
3682c633af4SJohn Birrell char *
proc_objname(struct proc_handle * p,uintptr_t addr,char * objname,size_t objnamesz)3692c633af4SJohn Birrell proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
3702c633af4SJohn Birrell     size_t objnamesz)
3712c633af4SJohn Birrell {
37207a9c2e6SMark Johnston 	prmap_t *map;
3738eb20f36SRui Paulo 	size_t i;
3748eb20f36SRui Paulo 
3753573cf33SMark Johnston 	if (p->nmappings == 0)
3763573cf33SMark Johnston 		if (proc_rdagent(p) == NULL)
3773573cf33SMark Johnston 			return (NULL);
37807a9c2e6SMark Johnston 	for (i = 0; i < p->nmappings; i++) {
37907a9c2e6SMark Johnston 		map = &p->mappings[i].map;
38007a9c2e6SMark Johnston 		if (addr >= map->pr_vaddr &&
38107a9c2e6SMark Johnston 		    addr < map->pr_vaddr + map->pr_size) {
38207a9c2e6SMark Johnston 			strlcpy(objname, map->pr_mapname, objnamesz);
3838eb20f36SRui Paulo 			return (objname);
3848eb20f36SRui Paulo 		}
3858eb20f36SRui Paulo 	}
3862c633af4SJohn Birrell 	return (NULL);
3872c633af4SJohn Birrell }
3882c633af4SJohn Birrell 
3898eb20f36SRui Paulo int
proc_iter_objs(struct proc_handle * p,proc_map_f * func,void * cd)3908eb20f36SRui Paulo proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
3918eb20f36SRui Paulo {
39207a9c2e6SMark Johnston 	char last[MAXPATHLEN], path[MAXPATHLEN], *base;
39307a9c2e6SMark Johnston 	prmap_t *map;
3948eb20f36SRui Paulo 	size_t i;
395c7fa6f0bSMark Johnston 	int error;
3968eb20f36SRui Paulo 
39707a9c2e6SMark Johnston 	if (p->nmappings == 0)
39807a9c2e6SMark Johnston 		if (proc_rdagent(p) == NULL)
3998eb20f36SRui Paulo 			return (-1);
400c7fa6f0bSMark Johnston 
401c7fa6f0bSMark Johnston 	error = 0;
4024c74b245SRui Paulo 	memset(last, 0, sizeof(last));
40307a9c2e6SMark Johnston 	for (i = 0; i < p->nmappings; i++) {
40407a9c2e6SMark Johnston 		map = &p->mappings[i].map;
40507a9c2e6SMark Johnston 		strlcpy(path, map->pr_mapname, sizeof(path));
40607a9c2e6SMark Johnston 		base = basename(path);
4074c74b245SRui Paulo 		/*
4084c74b245SRui Paulo 		 * We shouldn't call the callback twice with the same object.
4094c74b245SRui Paulo 		 * To do that we are assuming the fact that if there are
4104c74b245SRui Paulo 		 * repeated object names (i.e. different mappings for the
4114c74b245SRui Paulo 		 * same object) they occur next to each other.
4124c74b245SRui Paulo 		 */
41307a9c2e6SMark Johnston 		if (strcmp(base, last) == 0)
4144c74b245SRui Paulo 			continue;
41507a9c2e6SMark Johnston 		if ((error = (*func)(cd, map, base)) != 0)
416c7fa6f0bSMark Johnston 			break;
4174c74b245SRui Paulo 		strlcpy(last, path, sizeof(last));
4188eb20f36SRui Paulo 	}
419c7fa6f0bSMark Johnston 	return (error);
4208eb20f36SRui Paulo }
4218eb20f36SRui Paulo 
42207a9c2e6SMark Johnston static struct map_info *
_proc_addr2map(struct proc_handle * p,uintptr_t addr)42307a9c2e6SMark Johnston _proc_addr2map(struct proc_handle *p, uintptr_t addr)
42407a9c2e6SMark Johnston {
42507a9c2e6SMark Johnston 	struct map_info *mapping;
42607a9c2e6SMark Johnston 	size_t i;
42707a9c2e6SMark Johnston 
42807a9c2e6SMark Johnston 	if (p->nmappings == 0)
42907a9c2e6SMark Johnston 		if (proc_rdagent(p) == NULL)
43007a9c2e6SMark Johnston 			return (NULL);
43107a9c2e6SMark Johnston 	for (i = 0; i < p->nmappings; i++) {
43207a9c2e6SMark Johnston 		mapping = &p->mappings[i];
43307a9c2e6SMark Johnston 		if (addr >= mapping->map.pr_vaddr &&
43407a9c2e6SMark Johnston 		    addr < mapping->map.pr_vaddr + mapping->map.pr_size)
43507a9c2e6SMark Johnston 			return (mapping);
43607a9c2e6SMark Johnston 	}
43707a9c2e6SMark Johnston 	return (NULL);
43807a9c2e6SMark Johnston }
43907a9c2e6SMark Johnston 
4408eb20f36SRui Paulo prmap_t *
proc_addr2map(struct proc_handle * p,uintptr_t addr)4412c633af4SJohn Birrell proc_addr2map(struct proc_handle *p, uintptr_t addr)
4422c633af4SJohn Birrell {
4438eb20f36SRui Paulo 
44407a9c2e6SMark Johnston 	return (&_proc_addr2map(p, addr)->map);
4452c633af4SJohn Birrell }
4462c633af4SJohn Birrell 
447540cc663SMark Johnston /*
448c156354fSMark Johnston  * Look up the symbol at addr using a binary search, returning a copy of the
449c156354fSMark Johnston  * symbol and its name.
450540cc663SMark Johnston  */
451540cc663SMark Johnston static int
lookup_symbol_by_addr(Elf * e,struct symtab * symtab,uintptr_t addr,const char ** namep,GElf_Sym * symp)4521bdc41d2SMark Johnston lookup_symbol_by_addr(Elf *e, struct symtab *symtab, uintptr_t addr,
4531bdc41d2SMark Johnston     const char **namep, GElf_Sym *symp)
454540cc663SMark Johnston {
4551bdc41d2SMark Johnston 	GElf_Sym sym;
456540cc663SMark Johnston 	Elf_Data *data;
457540cc663SMark Johnston 	const char *s;
4581bdc41d2SMark Johnston 	u_int i, min, max, mid;
4591bdc41d2SMark Johnston 
4601bdc41d2SMark Johnston 	if (symtab->nsyms == 0)
4611bdc41d2SMark Johnston 		return (ENOENT);
462540cc663SMark Johnston 
463c156354fSMark Johnston 	data = symtab->data;
464c156354fSMark Johnston 	min = 0;
465c156354fSMark Johnston 	max = symtab->nsyms - 1;
466c156354fSMark Johnston 
467c156354fSMark Johnston 	while (min <= max) {
468c156354fSMark Johnston 		mid = (max + min) / 2;
4691bdc41d2SMark Johnston 		(void)gelf_getsym(data, symtab->index[mid], &sym);
4701bdc41d2SMark Johnston 		if (addr >= sym.st_value && addr < sym.st_value + sym.st_size)
4711bdc41d2SMark Johnston 			break;
472c156354fSMark Johnston 
4731bdc41d2SMark Johnston 		if (addr < sym.st_value)
474c156354fSMark Johnston 			max = mid - 1;
475c156354fSMark Johnston 		else
476c156354fSMark Johnston 			min = mid + 1;
477540cc663SMark Johnston 	}
4781bdc41d2SMark Johnston 	if (min > max)
479c156354fSMark Johnston 		return (ENOENT);
4801bdc41d2SMark Johnston 
4811bdc41d2SMark Johnston 	/*
4821bdc41d2SMark Johnston 	 * Advance until we find the matching symbol with largest index.
4831bdc41d2SMark Johnston 	 */
4841bdc41d2SMark Johnston 	for (i = mid; i < symtab->nsyms; i++) {
4851bdc41d2SMark Johnston 		(void)gelf_getsym(data, symtab->index[i], &sym);
4861bdc41d2SMark Johnston 		if (addr < sym.st_value || addr >= sym.st_value + sym.st_size)
4871bdc41d2SMark Johnston 			break;
4881bdc41d2SMark Johnston 	}
4891bdc41d2SMark Johnston 	(void)gelf_getsym(data, symtab->index[i - 1], symp);
4901bdc41d2SMark Johnston 	s = elf_strptr(e, symtab->stridx, symp->st_name);
4911bdc41d2SMark Johnston 	if (s != NULL && namep != NULL)
4921bdc41d2SMark Johnston 		*namep = s;
4931bdc41d2SMark Johnston 	return (0);
494540cc663SMark Johnston }
495540cc663SMark Johnston 
4962c633af4SJohn Birrell int
proc_addr2sym(struct proc_handle * p,uintptr_t addr,char * name,size_t namesz,GElf_Sym * symcopy)4972c633af4SJohn Birrell proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
4988eb20f36SRui Paulo     size_t namesz, GElf_Sym *symcopy)
4992c633af4SJohn Birrell {
500c156354fSMark Johnston 	struct file_info *file;
50107a9c2e6SMark Johnston 	struct map_info *mapping;
502540cc663SMark Johnston 	const char *s;
503540cc663SMark Johnston 	uintptr_t off;
504c156354fSMark Johnston 	int error;
5058eb20f36SRui Paulo 
50607a9c2e6SMark Johnston 	if ((mapping = _proc_addr2map(p, addr)) == NULL) {
5079e5787d2SMatt Macy 		DPRINTFX("ERROR: proc_addr2map failed to resolve 0x%jx", (uintmax_t)addr);
5088eb20f36SRui Paulo 		return (-1);
5098eb20f36SRui Paulo 	}
51007a9c2e6SMark Johnston 	if (open_object(mapping) != 0) {
51107a9c2e6SMark Johnston 		DPRINTFX("ERROR: failed to open object %s",
51207a9c2e6SMark Johnston 		    mapping->map.pr_mapname);
51376c6531cSMark Johnston 		return (-1);
5148eb20f36SRui Paulo 	}
515540cc663SMark Johnston 
516c156354fSMark Johnston 	file = mapping->file;
5175aa9b11bSMark Johnston 	off = file->ehdr.e_type == ET_DYN ?
5185aa9b11bSMark Johnston 	    mapping->map.pr_vaddr - mapping->map.pr_offset : 0;
519c156354fSMark Johnston 	if (addr < off)
520c156354fSMark Johnston 		return (ENOENT);
521c156354fSMark Johnston 	addr -= off;
522540cc663SMark Johnston 
523c156354fSMark Johnston 	error = lookup_symbol_by_addr(file->elf, &file->dynsymtab, addr, &s,
524c156354fSMark Johnston 	    symcopy);
525c156354fSMark Johnston 	if (error == ENOENT)
526c156354fSMark Johnston 		error = lookup_symbol_by_addr(file->elf, &file->symtab, addr,
527c156354fSMark Johnston 		    &s, symcopy);
528c156354fSMark Johnston 	if (error == 0) {
529c156354fSMark Johnston 		symcopy->st_value += off;
530540cc663SMark Johnston 		demangle(s, name, namesz);
531c156354fSMark Johnston 	}
5328eb20f36SRui Paulo 	return (error);
5332c633af4SJohn Birrell }
5342c633af4SJohn Birrell 
53507a9c2e6SMark Johnston static struct map_info *
_proc_name2map(struct proc_handle * p,const char * name)53607a9c2e6SMark Johnston _proc_name2map(struct proc_handle *p, const char *name)
53707a9c2e6SMark Johnston {
53807a9c2e6SMark Johnston 	char path[MAXPATHLEN], *base;
53907a9c2e6SMark Johnston 	struct map_info *mapping;
540d42df2a4SMark Johnston 	size_t i, len;
54107a9c2e6SMark Johnston 
542d42df2a4SMark Johnston 	if ((len = strlen(name)) == 0)
543d42df2a4SMark Johnston 		return (NULL);
54407a9c2e6SMark Johnston 	if (p->nmappings == 0)
54507a9c2e6SMark Johnston 		if (proc_rdagent(p) == NULL)
54607a9c2e6SMark Johnston 			return (NULL);
54707a9c2e6SMark Johnston 	for (i = 0; i < p->nmappings; i++) {
54807a9c2e6SMark Johnston 		mapping = &p->mappings[i];
54907a9c2e6SMark Johnston 		(void)strlcpy(path, mapping->map.pr_mapname, sizeof(path));
55007a9c2e6SMark Johnston 		base = basename(path);
55107a9c2e6SMark Johnston 		if (strcmp(base, name) == 0)
55207a9c2e6SMark Johnston 			return (mapping);
55307a9c2e6SMark Johnston 	}
554d42df2a4SMark Johnston 	/* If we didn't find a match, try matching prefixes of the basename. */
555d42df2a4SMark Johnston 	for (i = 0; i < p->nmappings; i++) {
55641fc64c2SBrooks Davis 		mapping = &p->mappings[i];
55741fc64c2SBrooks Davis 		strlcpy(path, mapping->map.pr_mapname, sizeof(path));
558d42df2a4SMark Johnston 		base = basename(path);
559d42df2a4SMark Johnston 		if (strncmp(base, name, len) == 0)
56041fc64c2SBrooks Davis 			return (mapping);
561d42df2a4SMark Johnston 	}
562d42df2a4SMark Johnston 	if (strcmp(name, "a.out") == 0)
5632c73c414SMark Johnston 		return (_proc_addr2map(p,
5642c73c414SMark Johnston 		    p->mappings[p->exec_map].map.pr_vaddr));
565d42df2a4SMark Johnston 	return (NULL);
566d42df2a4SMark Johnston }
56707a9c2e6SMark Johnston 
5688eb20f36SRui Paulo prmap_t *
proc_name2map(struct proc_handle * p,const char * name)5692c633af4SJohn Birrell proc_name2map(struct proc_handle *p, const char *name)
5702c633af4SJohn Birrell {
5718eb20f36SRui Paulo 
57207a9c2e6SMark Johnston 	return (&_proc_name2map(p, name)->map);
573540cc663SMark Johnston }
574540cc663SMark Johnston 
575540cc663SMark Johnston /*
576540cc663SMark Johnston  * Look up the symbol with the given name and return a copy of it.
577540cc663SMark Johnston  */
578540cc663SMark Johnston static int
lookup_symbol_by_name(Elf * elf,struct symtab * symtab,const char * symbol,GElf_Sym * symcopy,prsyminfo_t * si)579c156354fSMark Johnston lookup_symbol_by_name(Elf *elf, struct symtab *symtab, const char *symbol,
58041da933cSMark Johnston     GElf_Sym *symcopy, prsyminfo_t *si)
581540cc663SMark Johnston {
582540cc663SMark Johnston 	GElf_Sym sym;
583540cc663SMark Johnston 	Elf_Data *data;
584540cc663SMark Johnston 	char *s;
585540cc663SMark Johnston 	int i;
586540cc663SMark Johnston 
587c156354fSMark Johnston 	if (symtab->nsyms == 0)
588c156354fSMark Johnston 		return (ENOENT);
589c156354fSMark Johnston 	data = symtab->data;
590540cc663SMark Johnston 	for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
591c156354fSMark Johnston 		s = elf_strptr(elf, symtab->stridx, sym.st_name);
592540cc663SMark Johnston 		if (s != NULL && strcmp(s, symbol) == 0) {
593540cc663SMark Johnston 			memcpy(symcopy, &sym, sizeof(*symcopy));
59441da933cSMark Johnston 			if (si != NULL)
59541da933cSMark Johnston 				si->prs_id = i;
596540cc663SMark Johnston 			return (0);
597540cc663SMark Johnston 		}
598540cc663SMark Johnston 	}
599c156354fSMark Johnston 	return (ENOENT);
6002c633af4SJohn Birrell }
6012c633af4SJohn Birrell 
6022c633af4SJohn Birrell int
proc_name2sym(struct proc_handle * p,const char * object,const char * symbol,GElf_Sym * symcopy,prsyminfo_t * si)6032c633af4SJohn Birrell proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
60441da933cSMark Johnston     GElf_Sym *symcopy, prsyminfo_t *si)
6052c633af4SJohn Birrell {
606c156354fSMark Johnston 	struct file_info *file;
60707a9c2e6SMark Johnston 	struct map_info *mapping;
608540cc663SMark Johnston 	uintptr_t off;
609c156354fSMark Johnston 	int error;
6108eb20f36SRui Paulo 
61107a9c2e6SMark Johnston 	if ((mapping = _proc_name2map(p, object)) == NULL) {
61276c6531cSMark Johnston 		DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
61376c6531cSMark Johnston 		return (-1);
6148eb20f36SRui Paulo 	}
61507a9c2e6SMark Johnston 	if (open_object(mapping) != 0) {
61607a9c2e6SMark Johnston 		DPRINTFX("ERROR: failed to open object %s",
61707a9c2e6SMark Johnston 		    mapping->map.pr_mapname);
61876c6531cSMark Johnston 		return (-1);
6198eb20f36SRui Paulo 	}
62076c6531cSMark Johnston 
621c156354fSMark Johnston 	file = mapping->file;
6225aa9b11bSMark Johnston 	off = file->ehdr.e_type == ET_DYN ?
6235aa9b11bSMark Johnston 	    mapping->map.pr_vaddr - mapping->map.pr_offset : 0;
624540cc663SMark Johnston 
625c156354fSMark Johnston 	error = lookup_symbol_by_name(file->elf, &file->dynsymtab, symbol,
626c156354fSMark Johnston 	    symcopy, si);
627c156354fSMark Johnston 	if (error == ENOENT)
628c156354fSMark Johnston 		error = lookup_symbol_by_name(file->elf, &file->symtab, symbol,
629c156354fSMark Johnston 		    symcopy, si);
630540cc663SMark Johnston 	if (error == 0)
631540cc663SMark Johnston 		symcopy->st_value += off;
6328eb20f36SRui Paulo 	return (error);
6338eb20f36SRui Paulo }
6348eb20f36SRui Paulo 
63541da933cSMark Johnston ctf_file_t *
proc_name2ctf(struct proc_handle * p,const char * name)63641da933cSMark Johnston proc_name2ctf(struct proc_handle *p, const char *name)
63741da933cSMark Johnston {
63841da933cSMark Johnston #ifndef NO_CTF
6393f1cbdbeSMark Johnston 	ctf_file_t *ctf;
64041da933cSMark Johnston 	prmap_t *map;
64141da933cSMark Johnston 	int error;
64241da933cSMark Johnston 
643ec045490SMark Johnston 	if ((map = proc_name2map(p, name)) == NULL)
64441da933cSMark Johnston 		return (NULL);
64541da933cSMark Johnston 
6463f1cbdbeSMark Johnston 	ctf = ctf_open(map->pr_mapname, &error);
6473f1cbdbeSMark Johnston 	return (ctf);
64841da933cSMark Johnston #else
64941da933cSMark Johnston 	(void)p;
65041da933cSMark Johnston 	(void)name;
65141da933cSMark Johnston 	return (NULL);
65241da933cSMark Johnston #endif
65341da933cSMark Johnston }
65441da933cSMark Johnston 
6558eb20f36SRui Paulo int
proc_iter_symbyaddr(struct proc_handle * p,const char * object,int which,int mask,proc_sym_f * func,void * cd)6568eb20f36SRui Paulo proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
6578eb20f36SRui Paulo     int mask, proc_sym_f *func, void *cd)
6588eb20f36SRui Paulo {
6598eb20f36SRui Paulo 	GElf_Sym sym;
660c156354fSMark Johnston 	struct file_info *file;
66107a9c2e6SMark Johnston 	struct map_info *mapping;
662c156354fSMark Johnston 	struct symtab *symtab;
663c156354fSMark Johnston 	const char *s;
664c156354fSMark Johnston 	int error, i;
6658eb20f36SRui Paulo 
66607a9c2e6SMark Johnston 	if ((mapping = _proc_name2map(p, object)) == NULL) {
66776c6531cSMark Johnston 		DPRINTFX("ERROR: proc_name2map failed to resolve %s", object);
6688eb20f36SRui Paulo 		return (-1);
6698eb20f36SRui Paulo 	}
67007a9c2e6SMark Johnston 	if (open_object(mapping) != 0) {
67107a9c2e6SMark Johnston 		DPRINTFX("ERROR: failed to open object %s",
67207a9c2e6SMark Johnston 		    mapping->map.pr_mapname);
67376c6531cSMark Johnston 		return (-1);
6748eb20f36SRui Paulo 	}
675c156354fSMark Johnston 
676c156354fSMark Johnston 	file = mapping->file;
677c156354fSMark Johnston 	symtab = which == PR_SYMTAB ? &file->symtab : &file->dynsymtab;
678c156354fSMark Johnston 	if (symtab->nsyms == 0)
6798eb20f36SRui Paulo 		return (-1);
680c156354fSMark Johnston 
681c156354fSMark Johnston 	error = 0;
682c156354fSMark Johnston 	for (i = 0; gelf_getsym(symtab->data, i, &sym) != NULL; i++) {
6838eb20f36SRui Paulo 		if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
6848eb20f36SRui Paulo 		    (mask & BIND_LOCAL) == 0)
6858eb20f36SRui Paulo 			continue;
6868eb20f36SRui Paulo 		if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
6878eb20f36SRui Paulo 		    (mask & BIND_GLOBAL) == 0)
6888eb20f36SRui Paulo 			continue;
6898eb20f36SRui Paulo 		if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
6908eb20f36SRui Paulo 		    (mask & BIND_WEAK) == 0)
6918eb20f36SRui Paulo 			continue;
6928eb20f36SRui Paulo 		if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
6938eb20f36SRui Paulo 		    (mask & TYPE_NOTYPE) == 0)
6948eb20f36SRui Paulo 			continue;
6958eb20f36SRui Paulo 		if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
6968eb20f36SRui Paulo 		    (mask & TYPE_OBJECT) == 0)
6978eb20f36SRui Paulo 			continue;
6988eb20f36SRui Paulo 		if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
6998eb20f36SRui Paulo 		    (mask & TYPE_FUNC) == 0)
7008eb20f36SRui Paulo 			continue;
7018eb20f36SRui Paulo 		if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
7028eb20f36SRui Paulo 		    (mask & TYPE_SECTION) == 0)
7038eb20f36SRui Paulo 			continue;
7048eb20f36SRui Paulo 		if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
7058eb20f36SRui Paulo 		    (mask & TYPE_FILE) == 0)
7068eb20f36SRui Paulo 			continue;
707c156354fSMark Johnston 		s = elf_strptr(file->elf, symtab->stridx, sym.st_name);
708c156354fSMark Johnston 		if (file->ehdr.e_type == ET_DYN)
70907a9c2e6SMark Johnston 			sym.st_value += mapping->map.pr_vaddr;
710c7fa6f0bSMark Johnston 		if ((error = (*func)(cd, &sym, s)) != 0)
711c156354fSMark Johnston 			break;
7128eb20f36SRui Paulo 	}
7138eb20f36SRui Paulo 	return (error);
7142c633af4SJohn Birrell }
715