xref: /netbsd/usr.sbin/lockstat/elf32.c (revision 44ba63e8)
1*44ba63e8Schristos /*	$NetBSD: elf32.c,v 1.10 2013/10/19 17:10:17 christos Exp $	*/
2297f4619Sad 
3297f4619Sad /*-
4297f4619Sad  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5297f4619Sad  * All rights reserved.
6297f4619Sad  *
7297f4619Sad  * This code is derived from software contributed to The NetBSD Foundation
8297f4619Sad  * by Andrew Doran.
9297f4619Sad  *
10297f4619Sad  * Redistribution and use in source and binary forms, with or without
11297f4619Sad  * modification, are permitted provided that the following conditions
12297f4619Sad  * are met:
13297f4619Sad  * 1. Redistributions of source code must retain the above copyright
14297f4619Sad  *    notice, this list of conditions and the following disclaimer.
15297f4619Sad  * 2. Redistributions in binary form must reproduce the above copyright
16297f4619Sad  *    notice, this list of conditions and the following disclaimer in the
17297f4619Sad  *    documentation and/or other materials provided with the distribution.
18297f4619Sad  *
19297f4619Sad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20297f4619Sad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21297f4619Sad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22297f4619Sad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23297f4619Sad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24297f4619Sad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25297f4619Sad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26297f4619Sad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27297f4619Sad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28297f4619Sad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29297f4619Sad  * POSSIBILITY OF SUCH DAMAGE.
30297f4619Sad  */
31297f4619Sad 
32297f4619Sad /*
33297f4619Sad  * Copyright (c) 1996 Christopher G. Demetriou
34297f4619Sad  * All rights reserved.
35297f4619Sad  *
36297f4619Sad  * Redistribution and use in source and binary forms, with or without
37297f4619Sad  * modification, are permitted provided that the following conditions
38297f4619Sad  * are met:
39297f4619Sad  * 1. Redistributions of source code must retain the above copyright
40297f4619Sad  *    notice, this list of conditions and the following disclaimer.
41297f4619Sad  * 2. Redistributions in binary form must reproduce the above copyright
42297f4619Sad  *    notice, this list of conditions and the following disclaimer in the
43297f4619Sad  *    documentation and/or other materials provided with the distribution.
44297f4619Sad  * 3. All advertising materials mentioning features or use of this software
45297f4619Sad  *    must display the following acknowledgement:
46297f4619Sad  *          This product includes software developed for the
47297f4619Sad  *          NetBSD Project.  See http://www.NetBSD.org/ for
48297f4619Sad  *          information about NetBSD.
49297f4619Sad  * 4. The name of the author may not be used to endorse or promote products
50297f4619Sad  *    derived from this software without specific prior written permission.
51297f4619Sad  *
52297f4619Sad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53297f4619Sad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54297f4619Sad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55297f4619Sad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56297f4619Sad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57297f4619Sad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58297f4619Sad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59297f4619Sad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60297f4619Sad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61297f4619Sad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62297f4619Sad  *
63297f4619Sad  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
64297f4619Sad  */
65297f4619Sad 
66297f4619Sad #include <sys/cdefs.h>
67297f4619Sad #if !defined(lint)
68*44ba63e8Schristos __RCSID("$NetBSD: elf32.c,v 1.10 2013/10/19 17:10:17 christos Exp $");
69297f4619Sad #endif
70297f4619Sad 
71297f4619Sad #ifndef ELFSIZE
72297f4619Sad #define	ELFSIZE		32
73297f4619Sad #endif
74297f4619Sad 
75297f4619Sad #include <sys/param.h>
76297f4619Sad #include <sys/exec_elf.h>
77297f4619Sad #include <sys/queue.h>
7896d4a987Sad 
7996d4a987Sad #include <dev/lockstat.h>
80297f4619Sad 
81297f4619Sad #include <stdio.h>
82297f4619Sad #include <stdlib.h>
83297f4619Sad #include <string.h>
84297f4619Sad #include <unistd.h>
85297f4619Sad #include <err.h>
86297f4619Sad 
87297f4619Sad #include "extern.h"
88297f4619Sad 
89297f4619Sad #if (ELFSIZE == 32)
90297f4619Sad #define	NAME(x)	x##32
91297f4619Sad #elif (ELFSIZE == 64)
92297f4619Sad #define	NAME(x)	x##64
93297f4619Sad #endif
94297f4619Sad 
95297f4619Sad static int		nsyms;
96297f4619Sad static Elf_Sym		*symp;
97297f4619Sad static char		*strp;
98297f4619Sad 
99297f4619Sad int
NAME(loadsym)100297f4619Sad NAME(loadsym)(int fd)
101297f4619Sad {
102297f4619Sad 	Elf_Shdr symhdr, strhdr;
103297f4619Sad 	Elf_Ehdr ehdr;
104297f4619Sad 	size_t sz;
105297f4619Sad 	off_t off;
106297f4619Sad 	int i;
107297f4619Sad 
108297f4619Sad 	/*
109297f4619Sad 	 * Read the ELF header and make sure it's OK.
110297f4619Sad 	 */
111297f4619Sad 	if (pread(fd, &ehdr, sizeof(ehdr), 0) != sizeof(ehdr))
112297f4619Sad 		return -1;
113297f4619Sad 
114297f4619Sad 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
115297f4619Sad 	    ehdr.e_ident[EI_CLASS] != ELFCLASS)
116297f4619Sad 		return -1;
117297f4619Sad 
118297f4619Sad 	switch (ehdr.e_machine) {
119297f4619Sad 	ELFDEFNNAME(MACHDEP_ID_CASES)
120297f4619Sad 	default:
121297f4619Sad 		return -1;
122297f4619Sad 	}
123297f4619Sad 
124297f4619Sad 	/*
125297f4619Sad 	 * Find the symbol table header, and make sure the binary isn't
126297f4619Sad 	 * stripped.
127297f4619Sad 	 */
128297f4619Sad 	off = ehdr.e_shoff;
129297f4619Sad 	for (i = 0; i < ehdr.e_shnum; i++, off += sizeof(symhdr)) {
130297f4619Sad 		sz = pread(fd, &symhdr, sizeof(symhdr), off);
131297f4619Sad 		if (sz != sizeof(symhdr))
132297f4619Sad 			err(EXIT_FAILURE, "pread (section headers)");
133297f4619Sad 		if (symhdr.sh_type == SHT_SYMTAB)
134297f4619Sad 			break;
135297f4619Sad 	}
136297f4619Sad 	if (i == ehdr.e_shnum || symhdr.sh_offset == 0)
137297f4619Sad 		err(EXIT_FAILURE, "namelist is stripped");
138297f4619Sad 
139297f4619Sad 	/*
140297f4619Sad 	 * Pull in the string table header, and then read in both the symbol
141297f4619Sad 	 * table and string table proper.
142297f4619Sad 	 *
143297f4619Sad 	 * XXX We can't use mmap(), as /dev/ksyms doesn't support mmap yet.
144297f4619Sad 	 */
145297f4619Sad 	off = ehdr.e_shoff + symhdr.sh_link * sizeof(symhdr);
146297f4619Sad 	if (pread(fd, &strhdr, sizeof(strhdr), off) != sizeof(strhdr))
147297f4619Sad 		err(EXIT_FAILURE, "pread");
148297f4619Sad 
149297f4619Sad 	if ((symp = malloc(symhdr.sh_size)) == NULL)
150297f4619Sad 		err(EXIT_FAILURE, "malloc (symbol table)");
151297f4619Sad 	sz = pread(fd, symp, symhdr.sh_size, symhdr.sh_offset);
152297f4619Sad 	if (sz != symhdr.sh_size)
153297f4619Sad 		err(EXIT_FAILURE, "pread (symbol table)");
154297f4619Sad 
155297f4619Sad 	if ((strp = malloc(strhdr.sh_size)) == NULL)
156297f4619Sad 		err(EXIT_FAILURE, "malloc (string table)");
157297f4619Sad 	sz = pread(fd, strp, strhdr.sh_size, strhdr.sh_offset);
158297f4619Sad 	if (sz != strhdr.sh_size)
159297f4619Sad 		err(EXIT_FAILURE, "pread (string table)");
160297f4619Sad 
161297f4619Sad 	nsyms = (int)(symhdr.sh_size / sizeof(Elf_Sym));
162297f4619Sad 
163297f4619Sad 	return 0;
164297f4619Sad }
165297f4619Sad 
166297f4619Sad int
NAME(findsym)167297f4619Sad NAME(findsym)(findsym_t find, char *name, uintptr_t *start, uintptr_t *end)
168297f4619Sad {
169297f4619Sad 	uintptr_t sa, ea;
170*44ba63e8Schristos 	int i, off;
1713365bf71Slukem 	Elf_Byte st;
17212793652Sad 
17312793652Sad 	switch (find) {
17412793652Sad 	case LOCK_BYNAME:
17512793652Sad 	case LOCK_BYADDR:
17612793652Sad 		st = STT_OBJECT;
17712793652Sad 		break;
17812793652Sad 	case FUNC_BYNAME:
17912793652Sad 	case FUNC_BYADDR:
18012793652Sad 		st = STT_FUNC;
18112793652Sad 		break;
18212793652Sad 	default:
18312793652Sad 		return -1;
18412793652Sad 	}
185297f4619Sad 
186297f4619Sad 
187297f4619Sad #ifdef dump_core
188*44ba63e8Schristos 	static int lastptr[FIND_MAX];
189297f4619Sad 	for (i = lastptr[find];;) {
190297f4619Sad #else
191297f4619Sad 	for (i = 0; i < nsyms; i++) {
192297f4619Sad #endif
193297f4619Sad 		switch (find) {
194297f4619Sad 		case LOCK_BYNAME:
19512793652Sad 		case FUNC_BYNAME:
19612793652Sad 			if (ELF_ST_TYPE(symp[i].st_info) != st)
197297f4619Sad 				break;
198297f4619Sad 			if (strcmp(&strp[symp[i].st_name], name) != 0)
199297f4619Sad 				break;
200297f4619Sad 			*start = (uintptr_t)symp[i].st_value;
201297f4619Sad 			*end = *start + (uintptr_t)symp[i].st_size;
202297f4619Sad 			goto found;
203297f4619Sad 
204297f4619Sad 		case LOCK_BYADDR:
205297f4619Sad 		case FUNC_BYADDR:
20612793652Sad 			if (ELF_ST_TYPE(symp[i].st_info) != st)
207297f4619Sad 				break;
208297f4619Sad 			sa = (uintptr_t)symp[i].st_value;
209297f4619Sad 			ea = sa + (uintptr_t)symp[i].st_size - 1;
210297f4619Sad 			if (*start < sa || *start > ea)
211297f4619Sad 				break;
21212793652Sad 			off = (int)(*start - sa);
2135c2240bbSad 			*start = sa;
2145c2240bbSad 			if (end != NULL)
2155c2240bbSad 				*end = ea;
2165c2240bbSad 			if (name == NULL)
2175c2240bbSad 				goto found;
21812793652Sad 			if (off == 0)
2199f07c24eSad 				strlcpy(name, &strp[symp[i].st_name],
2209f07c24eSad 				    NAME_SIZE);
22112793652Sad 			else
2229f07c24eSad 				snprintf(name, NAME_SIZE, "%s+%x",
22312793652Sad 				    &strp[symp[i].st_name], off);
224297f4619Sad 			goto found;
225297f4619Sad 
226297f4619Sad 		default:
227297f4619Sad 			break;
228297f4619Sad 		}
229297f4619Sad 
230297f4619Sad #ifdef dump_core
231297f4619Sad 		if (++i >= nsyms)
232297f4619Sad 			i = 0;
233297f4619Sad 		if (i == lastptr[find])
234297f4619Sad 			return -1;
235297f4619Sad #endif
236297f4619Sad 	}
237297f4619Sad 
238297f4619Sad 	return -1;
239297f4619Sad 
240297f4619Sad  found:
241*44ba63e8Schristos #ifdef dump_core
242297f4619Sad  	lastptr[find] = i;
243*44ba63e8Schristos #endif
244297f4619Sad  	return 0;
245297f4619Sad }
246