xref: /freebsd/lib/libproc/proc_sym.c (revision 96ec3cdd8ea16c2d2ce6b9832505b266ec1b2062)
12c633af4SJohn Birrell /*-
28eb20f36SRui Paulo  * Copyright (c) 2010 The FreeBSD Foundation
32c633af4SJohn Birrell  * Copyright (c) 2008 John Birrell (jb@freebsd.org)
42c633af4SJohn Birrell  * All rights reserved.
52c633af4SJohn Birrell  *
68eb20f36SRui Paulo  * Portions of this software were developed by Rui Paulo under sponsorship
78eb20f36SRui Paulo  * from the FreeBSD Foundation.
88eb20f36SRui Paulo  *
92c633af4SJohn Birrell  * Redistribution and use in source and binary forms, with or without
102c633af4SJohn Birrell  * modification, are permitted provided that the following conditions
112c633af4SJohn Birrell  * are met:
122c633af4SJohn Birrell  * 1. Redistributions of source code must retain the above copyright
132c633af4SJohn Birrell  *    notice, this list of conditions and the following disclaimer.
142c633af4SJohn Birrell  * 2. Redistributions in binary form must reproduce the above copyright
152c633af4SJohn Birrell  *    notice, this list of conditions and the following disclaimer in the
162c633af4SJohn Birrell  *    documentation and/or other materials provided with the distribution.
172c633af4SJohn Birrell  *
182c633af4SJohn Birrell  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
192c633af4SJohn Birrell  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202c633af4SJohn Birrell  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212c633af4SJohn Birrell  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
222c633af4SJohn Birrell  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232c633af4SJohn Birrell  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242c633af4SJohn Birrell  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252c633af4SJohn Birrell  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262c633af4SJohn Birrell  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272c633af4SJohn Birrell  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282c633af4SJohn Birrell  * SUCH DAMAGE.
292c633af4SJohn Birrell  *
302c633af4SJohn Birrell  * $FreeBSD$
312c633af4SJohn Birrell  */
322c633af4SJohn Birrell 
338eb20f36SRui Paulo #include <sys/types.h>
348eb20f36SRui Paulo #include <sys/user.h>
358eb20f36SRui Paulo 
368eb20f36SRui Paulo #include <assert.h>
378eb20f36SRui Paulo #include <err.h>
382c633af4SJohn Birrell #include <stdio.h>
398eb20f36SRui Paulo #include <libgen.h>
408eb20f36SRui Paulo #include <string.h>
418eb20f36SRui Paulo #include <stdlib.h>
428eb20f36SRui Paulo #include <fcntl.h>
438eb20f36SRui Paulo #include <string.h>
448eb20f36SRui Paulo #include <unistd.h>
458eb20f36SRui Paulo #include <libutil.h>
468eb20f36SRui Paulo 
478eb20f36SRui Paulo #include "_libproc.h"
488eb20f36SRui Paulo 
493d12a343SDimitry Andric #ifndef NO_CXA_DEMANGLE
50cd906041SRui Paulo extern char *__cxa_demangle(const char *, char *, size_t *, int *);
513d12a343SDimitry Andric #endif /* NO_CXA_DEMANGLE */
52cd906041SRui Paulo 
538eb20f36SRui Paulo static void	proc_rdl2prmap(rd_loadobj_t *, prmap_t *);
548eb20f36SRui Paulo 
558eb20f36SRui Paulo static void
56404087ccSRui Paulo demangle(const char *symbol, char *buf, size_t len)
57404087ccSRui Paulo {
583d12a343SDimitry Andric #ifndef NO_CXA_DEMANGLE
59404087ccSRui Paulo 	char *dembuf;
60404087ccSRui Paulo 
613d12a343SDimitry Andric 	if (symbol[0] == '_' && symbol[1] == 'Z' && symbol[2]) {
62*96ec3cddSDimitry Andric 		dembuf = __cxa_demangle(symbol, NULL, NULL, NULL);
63404087ccSRui Paulo 		if (!dembuf)
64404087ccSRui Paulo 			goto fail;
65404087ccSRui Paulo 		strlcpy(buf, dembuf, len);
66404087ccSRui Paulo 		free(dembuf);
67404087ccSRui Paulo 		return;
68*96ec3cddSDimitry Andric 	}
69404087ccSRui Paulo fail:
703d12a343SDimitry Andric #endif /* NO_CXA_DEMANGLE */
71404087ccSRui Paulo 	strlcpy(buf, symbol, len);
72404087ccSRui Paulo }
73404087ccSRui Paulo 
74404087ccSRui Paulo static void
758eb20f36SRui Paulo proc_rdl2prmap(rd_loadobj_t *rdl, prmap_t *map)
768eb20f36SRui Paulo {
778eb20f36SRui Paulo 	map->pr_vaddr = rdl->rdl_saddr;
788eb20f36SRui Paulo 	map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr;
798eb20f36SRui Paulo 	map->pr_offset = rdl->rdl_offset;
808eb20f36SRui Paulo 	map->pr_mflags = 0;
818eb20f36SRui Paulo 	if (rdl->rdl_prot & RD_RDL_R)
828eb20f36SRui Paulo 		map->pr_mflags |= MA_READ;
838eb20f36SRui Paulo 	if (rdl->rdl_prot & RD_RDL_W)
848eb20f36SRui Paulo 		map->pr_mflags |= MA_WRITE;
858eb20f36SRui Paulo 	if (rdl->rdl_prot & RD_RDL_X)
868eb20f36SRui Paulo 		map->pr_mflags |= MA_EXEC;
878eb20f36SRui Paulo 	strlcpy(map->pr_mapname, rdl->rdl_path,
888eb20f36SRui Paulo 	    sizeof(map->pr_mapname));
898eb20f36SRui Paulo }
902c633af4SJohn Birrell 
912c633af4SJohn Birrell char *
922c633af4SJohn Birrell proc_objname(struct proc_handle *p, uintptr_t addr, char *objname,
932c633af4SJohn Birrell     size_t objnamesz)
942c633af4SJohn Birrell {
958eb20f36SRui Paulo 	size_t i;
968eb20f36SRui Paulo 	rd_loadobj_t *rdl;
978eb20f36SRui Paulo 
988eb20f36SRui Paulo 	for (i = 0; i < p->nobjs; i++) {
998eb20f36SRui Paulo 		rdl = &p->rdobjs[i];
1001e6b3858SMark Johnston 		if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
1018eb20f36SRui Paulo 			strlcpy(objname, rdl->rdl_path, objnamesz);
1028eb20f36SRui Paulo 			return (objname);
1038eb20f36SRui Paulo 		}
1048eb20f36SRui Paulo 	}
1052c633af4SJohn Birrell 	return (NULL);
1062c633af4SJohn Birrell }
1072c633af4SJohn Birrell 
1088eb20f36SRui Paulo prmap_t *
1098eb20f36SRui Paulo proc_obj2map(struct proc_handle *p, const char *objname)
1108eb20f36SRui Paulo {
1118eb20f36SRui Paulo 	size_t i;
1128eb20f36SRui Paulo 	prmap_t *map;
1138eb20f36SRui Paulo 	rd_loadobj_t *rdl;
1148eb20f36SRui Paulo 	char path[MAXPATHLEN];
1158eb20f36SRui Paulo 
116acc0eea6SMark Johnston 	rdl = NULL;
1178eb20f36SRui Paulo 	for (i = 0; i < p->nobjs; i++) {
118acc0eea6SMark Johnston 		basename_r(p->rdobjs[i].rdl_path, path);
1198eb20f36SRui Paulo 		if (strcmp(path, objname) == 0) {
120acc0eea6SMark Johnston 			rdl = &p->rdobjs[i];
121acc0eea6SMark Johnston 			break;
122acc0eea6SMark Johnston 		}
123acc0eea6SMark Johnston 	}
124acc0eea6SMark Johnston 	if (rdl == NULL && strcmp(objname, "a.out") == 0 && p->rdexec != NULL)
125acc0eea6SMark Johnston 		rdl = p->rdexec;
126acc0eea6SMark Johnston 	else
127acc0eea6SMark Johnston 		return (NULL);
128acc0eea6SMark Johnston 
1298eb20f36SRui Paulo 	if ((map = malloc(sizeof(*map))) == NULL)
1308eb20f36SRui Paulo 		return (NULL);
1318eb20f36SRui Paulo 	proc_rdl2prmap(rdl, map);
1328eb20f36SRui Paulo 	return (map);
1338eb20f36SRui Paulo }
1348eb20f36SRui Paulo 
1358eb20f36SRui Paulo int
1368eb20f36SRui Paulo proc_iter_objs(struct proc_handle *p, proc_map_f *func, void *cd)
1378eb20f36SRui Paulo {
1388eb20f36SRui Paulo 	size_t i;
1398eb20f36SRui Paulo 	rd_loadobj_t *rdl;
1408eb20f36SRui Paulo 	prmap_t map;
1418eb20f36SRui Paulo 	char path[MAXPATHLEN];
1424c74b245SRui Paulo 	char last[MAXPATHLEN];
1438eb20f36SRui Paulo 
1448eb20f36SRui Paulo 	if (p->nobjs == 0)
1458eb20f36SRui Paulo 		return (-1);
1464c74b245SRui Paulo 	memset(last, 0, sizeof(last));
1478eb20f36SRui Paulo 	for (i = 0; i < p->nobjs; i++) {
1488eb20f36SRui Paulo 		rdl = &p->rdobjs[i];
1498eb20f36SRui Paulo 		proc_rdl2prmap(rdl, &map);
1508eb20f36SRui Paulo 		basename_r(rdl->rdl_path, path);
1514c74b245SRui Paulo 		/*
1524c74b245SRui Paulo 		 * We shouldn't call the callback twice with the same object.
1534c74b245SRui Paulo 		 * To do that we are assuming the fact that if there are
1544c74b245SRui Paulo 		 * repeated object names (i.e. different mappings for the
1554c74b245SRui Paulo 		 * same object) they occur next to each other.
1564c74b245SRui Paulo 		 */
1574c74b245SRui Paulo 		if (strcmp(path, last) == 0)
1584c74b245SRui Paulo 			continue;
1598eb20f36SRui Paulo 		(*func)(cd, &map, path);
1604c74b245SRui Paulo 		strlcpy(last, path, sizeof(last));
1618eb20f36SRui Paulo 	}
1628eb20f36SRui Paulo 
1638eb20f36SRui Paulo 	return (0);
1648eb20f36SRui Paulo }
1658eb20f36SRui Paulo 
1668eb20f36SRui Paulo prmap_t *
1672c633af4SJohn Birrell proc_addr2map(struct proc_handle *p, uintptr_t addr)
1682c633af4SJohn Birrell {
1698eb20f36SRui Paulo 	size_t i;
1708eb20f36SRui Paulo 	int cnt, lastvn = 0;
1718eb20f36SRui Paulo 	prmap_t *map;
1728eb20f36SRui Paulo 	rd_loadobj_t *rdl;
1738eb20f36SRui Paulo 	struct kinfo_vmentry *kves, *kve;
1748eb20f36SRui Paulo 
1758eb20f36SRui Paulo 	/*
1768eb20f36SRui Paulo 	 * If we don't have a cache of listed objects, we need to query
1778eb20f36SRui Paulo 	 * it ourselves.
1788eb20f36SRui Paulo 	 */
1798eb20f36SRui Paulo 	if (p->nobjs == 0) {
1808eb20f36SRui Paulo 		if ((kves = kinfo_getvmmap(p->pid, &cnt)) == NULL)
1818eb20f36SRui Paulo 			return (NULL);
1828eb20f36SRui Paulo 		for (i = 0; i < (size_t)cnt; i++) {
1838eb20f36SRui Paulo 			kve = kves + i;
1848eb20f36SRui Paulo 			if (kve->kve_type == KVME_TYPE_VNODE)
1858eb20f36SRui Paulo 				lastvn = i;
1861e6b3858SMark Johnston 			if (addr >= kve->kve_start && addr < kve->kve_end) {
1878eb20f36SRui Paulo 				if ((map = malloc(sizeof(*map))) == NULL) {
1888eb20f36SRui Paulo 					free(kves);
1898eb20f36SRui Paulo 					return (NULL);
1908eb20f36SRui Paulo 				}
1918eb20f36SRui Paulo 				map->pr_vaddr = kve->kve_start;
1928eb20f36SRui Paulo 				map->pr_size = kve->kve_end - kve->kve_start;
1938eb20f36SRui Paulo 				map->pr_offset = kve->kve_offset;
1948eb20f36SRui Paulo 				map->pr_mflags = 0;
1958eb20f36SRui Paulo 				if (kve->kve_protection & KVME_PROT_READ)
1968eb20f36SRui Paulo 					map->pr_mflags |= MA_READ;
1978eb20f36SRui Paulo 				if (kve->kve_protection & KVME_PROT_WRITE)
1988eb20f36SRui Paulo 					map->pr_mflags |= MA_WRITE;
1998eb20f36SRui Paulo 				if (kve->kve_protection & KVME_PROT_EXEC)
2008eb20f36SRui Paulo 					map->pr_mflags |= MA_EXEC;
2018eb20f36SRui Paulo 				if (kve->kve_flags & KVME_FLAG_COW)
2028eb20f36SRui Paulo 					map->pr_mflags |= MA_COW;
2038eb20f36SRui Paulo 				if (kve->kve_flags & KVME_FLAG_NEEDS_COPY)
2048eb20f36SRui Paulo 					map->pr_mflags |= MA_NEEDS_COPY;
2058eb20f36SRui Paulo 				if (kve->kve_flags & KVME_FLAG_NOCOREDUMP)
2068eb20f36SRui Paulo 					map->pr_mflags |= MA_NOCOREDUMP;
2078eb20f36SRui Paulo 				strlcpy(map->pr_mapname, kves[lastvn].kve_path,
2088eb20f36SRui Paulo 				    sizeof(map->pr_mapname));
2098eb20f36SRui Paulo 				free(kves);
2108eb20f36SRui Paulo 				return (map);
2118eb20f36SRui Paulo 			}
2128eb20f36SRui Paulo 		}
2138eb20f36SRui Paulo 		free(kves);
2148eb20f36SRui Paulo 		return (NULL);
2158eb20f36SRui Paulo 	}
2168eb20f36SRui Paulo 
2178eb20f36SRui Paulo 	for (i = 0; i < p->nobjs; i++) {
2188eb20f36SRui Paulo 		rdl = &p->rdobjs[i];
2191e6b3858SMark Johnston 		if (addr >= rdl->rdl_saddr && addr < rdl->rdl_eaddr) {
2208eb20f36SRui Paulo 			if ((map = malloc(sizeof(*map))) == NULL)
2218eb20f36SRui Paulo 				return (NULL);
2228eb20f36SRui Paulo 			proc_rdl2prmap(rdl, map);
2238eb20f36SRui Paulo 			return (map);
2248eb20f36SRui Paulo 		}
2258eb20f36SRui Paulo 	}
2262c633af4SJohn Birrell 	return (NULL);
2272c633af4SJohn Birrell }
2282c633af4SJohn Birrell 
2292c633af4SJohn Birrell int
2302c633af4SJohn Birrell proc_addr2sym(struct proc_handle *p, uintptr_t addr, char *name,
2318eb20f36SRui Paulo     size_t namesz, GElf_Sym *symcopy)
2322c633af4SJohn Birrell {
2338eb20f36SRui Paulo 	Elf *e;
2348eb20f36SRui Paulo 	Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
2358eb20f36SRui Paulo 	Elf_Data *data;
2368eb20f36SRui Paulo 	GElf_Shdr shdr;
2378eb20f36SRui Paulo 	GElf_Sym sym;
2388eb20f36SRui Paulo 	GElf_Ehdr ehdr;
2398eb20f36SRui Paulo 	int fd, error = -1;
2408eb20f36SRui Paulo 	size_t i;
2418eb20f36SRui Paulo 	uint64_t rsym;
2428eb20f36SRui Paulo 	prmap_t *map;
2438eb20f36SRui Paulo 	char *s;
2448eb20f36SRui Paulo 	unsigned long symtabstridx = 0, dynsymstridx = 0;
2458eb20f36SRui Paulo 
2468eb20f36SRui Paulo 	if ((map = proc_addr2map(p, addr)) == NULL)
2478eb20f36SRui Paulo 		return (-1);
24830e81f7eSMark Johnston 	if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
24930e81f7eSMark Johnston 		DPRINTF("ERROR: open %s failed", map->pr_mapname);
2508eb20f36SRui Paulo 		goto err0;
2518eb20f36SRui Paulo 	}
2528eb20f36SRui Paulo 	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
25330e81f7eSMark Johnston 		DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
2548eb20f36SRui Paulo 		goto err1;
2558eb20f36SRui Paulo 	}
2568eb20f36SRui Paulo 	if (gelf_getehdr(e, &ehdr) == NULL) {
25730e81f7eSMark Johnston 		DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
2588eb20f36SRui Paulo 		goto err2;
2598eb20f36SRui Paulo 	}
2608eb20f36SRui Paulo 	/*
2618eb20f36SRui Paulo 	 * Find the index of the STRTAB and SYMTAB sections to locate
2628eb20f36SRui Paulo 	 * symbol names.
2638eb20f36SRui Paulo 	 */
2648eb20f36SRui Paulo 	scn = NULL;
2658eb20f36SRui Paulo 	while ((scn = elf_nextscn(e, scn)) != NULL) {
2668eb20f36SRui Paulo 		gelf_getshdr(scn, &shdr);
2678eb20f36SRui Paulo 		switch (shdr.sh_type) {
2688eb20f36SRui Paulo 		case SHT_SYMTAB:
2698eb20f36SRui Paulo 			symtabscn = scn;
2708eb20f36SRui Paulo 			symtabstridx = shdr.sh_link;
2718eb20f36SRui Paulo 			break;
2728eb20f36SRui Paulo 		case SHT_DYNSYM:
2738eb20f36SRui Paulo 			dynsymscn = scn;
2748eb20f36SRui Paulo 			dynsymstridx = shdr.sh_link;
2758eb20f36SRui Paulo 			break;
2768eb20f36SRui Paulo 		default:
2778eb20f36SRui Paulo 			break;
2788eb20f36SRui Paulo 		}
2798eb20f36SRui Paulo 	}
2808eb20f36SRui Paulo 	/*
2818eb20f36SRui Paulo 	 * Iterate over the Dynamic Symbols table to find the symbol.
2828eb20f36SRui Paulo 	 * Then look up the string name in STRTAB (.dynstr)
2838eb20f36SRui Paulo 	 */
2848eb20f36SRui Paulo 	if ((data = elf_getdata(dynsymscn, NULL)) == NULL) {
28530e81f7eSMark Johnston 		DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
28619a75affSRui Paulo 		goto symtab;
2878eb20f36SRui Paulo 	}
2888eb20f36SRui Paulo 	i = 0;
2898eb20f36SRui Paulo 	while (gelf_getsym(data, i++, &sym) != NULL) {
2908eb20f36SRui Paulo 		/*
2918eb20f36SRui Paulo 		 * Calculate the address mapped to the virtual memory
2928eb20f36SRui Paulo 		 * by rtld.
2938eb20f36SRui Paulo 		 */
294fbce8e16SMark Johnston 		if (ehdr.e_type != ET_EXEC)
2958eb20f36SRui Paulo 			rsym = map->pr_vaddr + sym.st_value;
296fbce8e16SMark Johnston 		else
297fbce8e16SMark Johnston 			rsym = sym.st_value;
2984ec6017eSMark Johnston 		if (addr >= rsym && addr < rsym + sym.st_size) {
2998eb20f36SRui Paulo 			s = elf_strptr(e, dynsymstridx, sym.st_name);
3008eb20f36SRui Paulo 			if (s) {
301404087ccSRui Paulo 				demangle(s, name, namesz);
3028eb20f36SRui Paulo 				memcpy(symcopy, &sym, sizeof(sym));
3038eb20f36SRui Paulo 				/*
3048eb20f36SRui Paulo 				 * DTrace expects the st_value to contain
3058eb20f36SRui Paulo 				 * only the address relative to the start of
3068eb20f36SRui Paulo 				 * the function.
3078eb20f36SRui Paulo 				 */
3088eb20f36SRui Paulo 				symcopy->st_value = rsym;
3097bc9877dSRui Paulo 				error = 0;
3108eb20f36SRui Paulo 				goto out;
3118eb20f36SRui Paulo 			}
3128eb20f36SRui Paulo 		}
3138eb20f36SRui Paulo 	}
31419a75affSRui Paulo symtab:
3158eb20f36SRui Paulo 	/*
3168eb20f36SRui Paulo 	 * Iterate over the Symbols Table to find the symbol.
3178eb20f36SRui Paulo 	 * Then look up the string name in STRTAB (.dynstr)
3188eb20f36SRui Paulo 	 */
3198eb20f36SRui Paulo 	if ((data = elf_getdata(symtabscn, NULL)) == NULL) {
32030e81f7eSMark Johnston 		DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
3218eb20f36SRui Paulo 		goto err2;
3228eb20f36SRui Paulo 	}
3238eb20f36SRui Paulo 	i = 0;
3248eb20f36SRui Paulo 	while (gelf_getsym(data, i++, &sym) != NULL) {
3258eb20f36SRui Paulo 		/*
3268eb20f36SRui Paulo 		 * Calculate the address mapped to the virtual memory
3278eb20f36SRui Paulo 		 * by rtld.
3288eb20f36SRui Paulo 		 */
3298eb20f36SRui Paulo 		if (ehdr.e_type != ET_EXEC)
3308eb20f36SRui Paulo 			rsym = map->pr_vaddr + sym.st_value;
3318eb20f36SRui Paulo 		else
3328eb20f36SRui Paulo 			rsym = sym.st_value;
3334ec6017eSMark Johnston 		if (addr >= rsym && addr < rsym + sym.st_size) {
3348eb20f36SRui Paulo 			s = elf_strptr(e, symtabstridx, sym.st_name);
3358eb20f36SRui Paulo 			if (s) {
336404087ccSRui Paulo 				demangle(s, name, namesz);
3378eb20f36SRui Paulo 				memcpy(symcopy, &sym, sizeof(sym));
3388eb20f36SRui Paulo 				/*
3398eb20f36SRui Paulo 				 * DTrace expects the st_value to contain
3408eb20f36SRui Paulo 				 * only the address relative to the start of
3418eb20f36SRui Paulo 				 * the function.
3428eb20f36SRui Paulo 				 */
3438eb20f36SRui Paulo 				symcopy->st_value = rsym;
3448eb20f36SRui Paulo 				error = 0;
3458eb20f36SRui Paulo 				goto out;
3468eb20f36SRui Paulo 			}
3478eb20f36SRui Paulo 		}
3488eb20f36SRui Paulo 	}
3498eb20f36SRui Paulo out:
3508eb20f36SRui Paulo err2:
3518eb20f36SRui Paulo 	elf_end(e);
3528eb20f36SRui Paulo err1:
3538eb20f36SRui Paulo 	close(fd);
3548eb20f36SRui Paulo err0:
3558eb20f36SRui Paulo 	free(map);
3568eb20f36SRui Paulo 	return (error);
3572c633af4SJohn Birrell }
3582c633af4SJohn Birrell 
3598eb20f36SRui Paulo prmap_t *
3602c633af4SJohn Birrell proc_name2map(struct proc_handle *p, const char *name)
3612c633af4SJohn Birrell {
3628eb20f36SRui Paulo 	size_t i;
3638eb20f36SRui Paulo 	int cnt;
3648eb20f36SRui Paulo 	prmap_t *map;
3658eb20f36SRui Paulo 	char tmppath[MAXPATHLEN];
3668eb20f36SRui Paulo 	struct kinfo_vmentry *kves, *kve;
3678eb20f36SRui Paulo 	rd_loadobj_t *rdl;
3688eb20f36SRui Paulo 
3698eb20f36SRui Paulo 	/*
3708eb20f36SRui Paulo 	 * If we haven't iterated over the list of loaded objects,
3718eb20f36SRui Paulo 	 * librtld_db isn't yet initialized and it's very likely
3728eb20f36SRui Paulo 	 * that librtld_db called us. We need to do the heavy
3738eb20f36SRui Paulo 	 * lifting here to find the symbol librtld_db is looking for.
3748eb20f36SRui Paulo 	 */
3758eb20f36SRui Paulo 	if (p->nobjs == 0) {
3768eb20f36SRui Paulo 		if ((kves = kinfo_getvmmap(proc_getpid(p), &cnt)) == NULL)
3778eb20f36SRui Paulo 			return (NULL);
3788eb20f36SRui Paulo 		for (i = 0; i < (size_t)cnt; i++) {
3798eb20f36SRui Paulo 			kve = kves + i;
3808eb20f36SRui Paulo 			basename_r(kve->kve_path, tmppath);
3818eb20f36SRui Paulo 			if (strcmp(tmppath, name) == 0) {
3828eb20f36SRui Paulo 				map = proc_addr2map(p, kve->kve_start);
3838eb20f36SRui Paulo 				free(kves);
3848eb20f36SRui Paulo 				return (map);
3858eb20f36SRui Paulo 			}
3868eb20f36SRui Paulo 		}
3878eb20f36SRui Paulo 		free(kves);
3888eb20f36SRui Paulo 		return (NULL);
3898eb20f36SRui Paulo 	}
390acc0eea6SMark Johnston 	if ((name == NULL || strcmp(name, "a.out") == 0) &&
391acc0eea6SMark Johnston 	    p->rdexec != NULL) {
392acc0eea6SMark Johnston 		map = proc_addr2map(p, p->rdexec->rdl_saddr);
3938eb20f36SRui Paulo 		return (map);
3948eb20f36SRui Paulo 	}
3958eb20f36SRui Paulo 	for (i = 0; i < p->nobjs; i++) {
3968eb20f36SRui Paulo 		rdl = &p->rdobjs[i];
3978eb20f36SRui Paulo 		basename_r(rdl->rdl_path, tmppath);
3988eb20f36SRui Paulo 		if (strcmp(tmppath, name) == 0) {
3998eb20f36SRui Paulo 			if ((map = malloc(sizeof(*map))) == NULL)
4008eb20f36SRui Paulo 				return (NULL);
4018eb20f36SRui Paulo 			proc_rdl2prmap(rdl, map);
4028eb20f36SRui Paulo 			return (map);
4038eb20f36SRui Paulo 		}
4048eb20f36SRui Paulo 	}
4058eb20f36SRui Paulo 
4062c633af4SJohn Birrell 	return (NULL);
4072c633af4SJohn Birrell }
4082c633af4SJohn Birrell 
4092c633af4SJohn Birrell int
4102c633af4SJohn Birrell proc_name2sym(struct proc_handle *p, const char *object, const char *symbol,
4118eb20f36SRui Paulo     GElf_Sym *symcopy)
4122c633af4SJohn Birrell {
4138eb20f36SRui Paulo 	Elf *e;
4148eb20f36SRui Paulo 	Elf_Scn *scn, *dynsymscn = NULL, *symtabscn = NULL;
4158eb20f36SRui Paulo 	Elf_Data *data;
4168eb20f36SRui Paulo 	GElf_Shdr shdr;
4178eb20f36SRui Paulo 	GElf_Sym sym;
4188eb20f36SRui Paulo 	GElf_Ehdr ehdr;
4198eb20f36SRui Paulo 	int fd, error = -1;
4208eb20f36SRui Paulo 	size_t i;
4218eb20f36SRui Paulo 	prmap_t *map;
4228eb20f36SRui Paulo 	char *s;
4238eb20f36SRui Paulo 	unsigned long symtabstridx = 0, dynsymstridx = 0;
4248eb20f36SRui Paulo 
4258eb20f36SRui Paulo 	if ((map = proc_name2map(p, object)) == NULL) {
42630e81f7eSMark Johnston 		DPRINTFX("ERROR: couldn't find object %s", object);
4278eb20f36SRui Paulo 		goto err0;
4288eb20f36SRui Paulo 	}
4298eb20f36SRui Paulo 	if ((fd = open(map->pr_mapname, O_RDONLY, 0)) < 0) {
4308eb20f36SRui Paulo 		DPRINTF("ERROR: open %s failed", map->pr_mapname);
4318eb20f36SRui Paulo 		goto err0;
4328eb20f36SRui Paulo 	}
4338eb20f36SRui Paulo 	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
43430e81f7eSMark Johnston 		DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
4358eb20f36SRui Paulo 		goto err1;
4368eb20f36SRui Paulo 	}
4378eb20f36SRui Paulo 	if (gelf_getehdr(e, &ehdr) == NULL) {
43830e81f7eSMark Johnston 		DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
4398eb20f36SRui Paulo 		goto err2;
4408eb20f36SRui Paulo 	}
4418eb20f36SRui Paulo 	/*
4428eb20f36SRui Paulo 	 * Find the index of the STRTAB and SYMTAB sections to locate
4438eb20f36SRui Paulo 	 * symbol names.
4448eb20f36SRui Paulo 	 */
4458eb20f36SRui Paulo 	scn = NULL;
4468eb20f36SRui Paulo 	while ((scn = elf_nextscn(e, scn)) != NULL) {
4478eb20f36SRui Paulo 		gelf_getshdr(scn, &shdr);
4488eb20f36SRui Paulo 		switch (shdr.sh_type) {
4498eb20f36SRui Paulo 		case SHT_SYMTAB:
4508eb20f36SRui Paulo 			symtabscn = scn;
4518eb20f36SRui Paulo 			symtabstridx = shdr.sh_link;
4528eb20f36SRui Paulo 			break;
4538eb20f36SRui Paulo 		case SHT_DYNSYM:
4548eb20f36SRui Paulo 			dynsymscn = scn;
4558eb20f36SRui Paulo 			dynsymstridx = shdr.sh_link;
4568eb20f36SRui Paulo 			break;
4578eb20f36SRui Paulo 		default:
4588eb20f36SRui Paulo 			break;
4598eb20f36SRui Paulo 		}
4608eb20f36SRui Paulo 	}
4618eb20f36SRui Paulo 	/*
4628eb20f36SRui Paulo 	 * Iterate over the Dynamic Symbols table to find the symbol.
4638eb20f36SRui Paulo 	 * Then look up the string name in STRTAB (.dynstr)
4648eb20f36SRui Paulo 	 */
46519a75affSRui Paulo 	if ((data = elf_getdata(dynsymscn, NULL))) {
4668eb20f36SRui Paulo 		i = 0;
4678eb20f36SRui Paulo 		while (gelf_getsym(data, i++, &sym) != NULL) {
4688eb20f36SRui Paulo 			s = elf_strptr(e, dynsymstridx, sym.st_name);
4698eb20f36SRui Paulo 			if (s && strcmp(s, symbol) == 0) {
4708eb20f36SRui Paulo 				memcpy(symcopy, &sym, sizeof(sym));
471fbce8e16SMark Johnston 				if (ehdr.e_type != ET_EXEC)
472fbce8e16SMark Johnston 					symcopy->st_value += map->pr_vaddr;
4738eb20f36SRui Paulo 				error = 0;
4748eb20f36SRui Paulo 				goto out;
4758eb20f36SRui Paulo 			}
4768eb20f36SRui Paulo 		}
47719a75affSRui Paulo 	}
4788eb20f36SRui Paulo 	/*
4798eb20f36SRui Paulo 	 * Iterate over the Symbols Table to find the symbol.
4808eb20f36SRui Paulo 	 * Then look up the string name in STRTAB (.dynstr)
4818eb20f36SRui Paulo 	 */
48219a75affSRui Paulo 	if ((data = elf_getdata(symtabscn, NULL))) {
4838eb20f36SRui Paulo 		i = 0;
4848eb20f36SRui Paulo 		while (gelf_getsym(data, i++, &sym) != NULL) {
4858eb20f36SRui Paulo 			s = elf_strptr(e, symtabstridx, sym.st_name);
4868eb20f36SRui Paulo 			if (s && strcmp(s, symbol) == 0) {
4878eb20f36SRui Paulo 				memcpy(symcopy, &sym, sizeof(sym));
488fbce8e16SMark Johnston 				if (ehdr.e_type != ET_EXEC)
489fbce8e16SMark Johnston 					symcopy->st_value += map->pr_vaddr;
4908eb20f36SRui Paulo 				error = 0;
4918eb20f36SRui Paulo 				goto out;
4928eb20f36SRui Paulo 			}
4938eb20f36SRui Paulo 		}
49419a75affSRui Paulo 	}
4958eb20f36SRui Paulo out:
496fbce8e16SMark Johnston 	DPRINTFX("found addr 0x%lx for %s", symcopy->st_value, symbol);
4978eb20f36SRui Paulo err2:
4988eb20f36SRui Paulo 	elf_end(e);
4998eb20f36SRui Paulo err1:
5008eb20f36SRui Paulo 	close(fd);
5018eb20f36SRui Paulo err0:
5028eb20f36SRui Paulo 	free(map);
5038eb20f36SRui Paulo 
5048eb20f36SRui Paulo 	return (error);
5058eb20f36SRui Paulo }
5068eb20f36SRui Paulo 
5078eb20f36SRui Paulo 
5088eb20f36SRui Paulo int
5098eb20f36SRui Paulo proc_iter_symbyaddr(struct proc_handle *p, const char *object, int which,
5108eb20f36SRui Paulo     int mask, proc_sym_f *func, void *cd)
5118eb20f36SRui Paulo {
5128eb20f36SRui Paulo 	Elf *e;
5138eb20f36SRui Paulo 	int i, fd;
5148eb20f36SRui Paulo 	prmap_t *map;
5158eb20f36SRui Paulo 	Elf_Scn *scn, *foundscn = NULL;
5168eb20f36SRui Paulo 	Elf_Data *data;
517fbce8e16SMark Johnston 	GElf_Ehdr ehdr;
5188eb20f36SRui Paulo 	GElf_Shdr shdr;
5198eb20f36SRui Paulo 	GElf_Sym sym;
5208eb20f36SRui Paulo 	unsigned long stridx = -1;
5218eb20f36SRui Paulo 	char *s;
5228eb20f36SRui Paulo 	int error = -1;
5238eb20f36SRui Paulo 
5248eb20f36SRui Paulo 	if ((map = proc_name2map(p, object)) == NULL)
5258eb20f36SRui Paulo 		return (-1);
5268eb20f36SRui Paulo 	if ((fd = open(map->pr_mapname, O_RDONLY)) < 0) {
52730e81f7eSMark Johnston 		DPRINTF("ERROR: open %s failed", map->pr_mapname);
5288eb20f36SRui Paulo 		goto err0;
5298eb20f36SRui Paulo 	}
5308eb20f36SRui Paulo 	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
53130e81f7eSMark Johnston 		DPRINTFX("ERROR: elf_begin() failed: %s", elf_errmsg(-1));
5328eb20f36SRui Paulo 		goto err1;
5338eb20f36SRui Paulo 	}
534fbce8e16SMark Johnston 	if (gelf_getehdr(e, &ehdr) == NULL) {
535fbce8e16SMark Johnston 		DPRINTFX("ERROR: gelf_getehdr() failed: %s", elf_errmsg(-1));
536fbce8e16SMark Johnston 		goto err2;
537fbce8e16SMark Johnston 	}
5388eb20f36SRui Paulo 	/*
5398eb20f36SRui Paulo 	 * Find the section we are looking for.
5408eb20f36SRui Paulo 	 */
5418eb20f36SRui Paulo 	scn = NULL;
5428eb20f36SRui Paulo 	while ((scn = elf_nextscn(e, scn)) != NULL) {
5438eb20f36SRui Paulo 		gelf_getshdr(scn, &shdr);
5448eb20f36SRui Paulo 		if (which == PR_SYMTAB &&
5458eb20f36SRui Paulo 		    shdr.sh_type == SHT_SYMTAB) {
5468eb20f36SRui Paulo 			foundscn = scn;
5478eb20f36SRui Paulo 			break;
5488eb20f36SRui Paulo 		} else if (which == PR_DYNSYM &&
5498eb20f36SRui Paulo 		    shdr.sh_type == SHT_DYNSYM) {
5508eb20f36SRui Paulo 			foundscn = scn;
5518eb20f36SRui Paulo 			break;
5528eb20f36SRui Paulo 		}
5538eb20f36SRui Paulo 	}
5548eb20f36SRui Paulo 	if (!foundscn)
5558eb20f36SRui Paulo 		return (-1);
5568eb20f36SRui Paulo 	stridx = shdr.sh_link;
5578eb20f36SRui Paulo 	if ((data = elf_getdata(foundscn, NULL)) == NULL) {
55830e81f7eSMark Johnston 		DPRINTFX("ERROR: elf_getdata() failed: %s", elf_errmsg(-1));
5598eb20f36SRui Paulo 		goto err2;
5608eb20f36SRui Paulo 	}
5618eb20f36SRui Paulo 	i = 0;
5628eb20f36SRui Paulo 	while (gelf_getsym(data, i++, &sym) != NULL) {
5638eb20f36SRui Paulo 		if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
5648eb20f36SRui Paulo 		    (mask & BIND_LOCAL) == 0)
5658eb20f36SRui Paulo 			continue;
5668eb20f36SRui Paulo 		if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL &&
5678eb20f36SRui Paulo 		    (mask & BIND_GLOBAL) == 0)
5688eb20f36SRui Paulo 			continue;
5698eb20f36SRui Paulo 		if (GELF_ST_BIND(sym.st_info) == STB_WEAK &&
5708eb20f36SRui Paulo 		    (mask & BIND_WEAK) == 0)
5718eb20f36SRui Paulo 			continue;
5728eb20f36SRui Paulo 		if (GELF_ST_TYPE(sym.st_info) == STT_NOTYPE &&
5738eb20f36SRui Paulo 		    (mask & TYPE_NOTYPE) == 0)
5748eb20f36SRui Paulo 			continue;
5758eb20f36SRui Paulo 		if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT &&
5768eb20f36SRui Paulo 		    (mask & TYPE_OBJECT) == 0)
5778eb20f36SRui Paulo 			continue;
5788eb20f36SRui Paulo 		if (GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
5798eb20f36SRui Paulo 		    (mask & TYPE_FUNC) == 0)
5808eb20f36SRui Paulo 			continue;
5818eb20f36SRui Paulo 		if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
5828eb20f36SRui Paulo 		    (mask & TYPE_SECTION) == 0)
5838eb20f36SRui Paulo 			continue;
5848eb20f36SRui Paulo 		if (GELF_ST_TYPE(sym.st_info) == STT_FILE &&
5858eb20f36SRui Paulo 		    (mask & TYPE_FILE) == 0)
5868eb20f36SRui Paulo 			continue;
5878eb20f36SRui Paulo 		s = elf_strptr(e, stridx, sym.st_name);
588fbce8e16SMark Johnston 		if (ehdr.e_type != ET_EXEC)
5898eb20f36SRui Paulo 			sym.st_value += map->pr_vaddr;
5908eb20f36SRui Paulo 		(*func)(cd, &sym, s);
5918eb20f36SRui Paulo 	}
5928eb20f36SRui Paulo 	error = 0;
5938eb20f36SRui Paulo err2:
5948eb20f36SRui Paulo 	elf_end(e);
5958eb20f36SRui Paulo err1:
5968eb20f36SRui Paulo 	close(fd);
5978eb20f36SRui Paulo err0:
5988eb20f36SRui Paulo 	free(map);
5998eb20f36SRui Paulo 	return (error);
6002c633af4SJohn Birrell }
601