xref: /netbsd/usr.sbin/lockstat/elf32.c (revision 12793652)
1*12793652Sad /*	$NetBSD: elf32.c,v 1.3 2006/11/08 23:12:57 ad 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  * 3. All advertising materials mentioning features or use of this software
19297f4619Sad  *    must display the following acknowledgement:
20297f4619Sad  *	This product includes software developed by the NetBSD
21297f4619Sad  *	Foundation, Inc. and its contributors.
22297f4619Sad  * 4. Neither the name of The NetBSD Foundation nor the names of its
23297f4619Sad  *    contributors may be used to endorse or promote products derived
24297f4619Sad  *    from this software without specific prior written permission.
25297f4619Sad  *
26297f4619Sad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27297f4619Sad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28297f4619Sad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29297f4619Sad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30297f4619Sad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31297f4619Sad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32297f4619Sad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33297f4619Sad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34297f4619Sad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35297f4619Sad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36297f4619Sad  * POSSIBILITY OF SUCH DAMAGE.
37297f4619Sad  */
38297f4619Sad 
39297f4619Sad /*
40297f4619Sad  * Copyright (c) 1996 Christopher G. Demetriou
41297f4619Sad  * All rights reserved.
42297f4619Sad  *
43297f4619Sad  * Redistribution and use in source and binary forms, with or without
44297f4619Sad  * modification, are permitted provided that the following conditions
45297f4619Sad  * are met:
46297f4619Sad  * 1. Redistributions of source code must retain the above copyright
47297f4619Sad  *    notice, this list of conditions and the following disclaimer.
48297f4619Sad  * 2. Redistributions in binary form must reproduce the above copyright
49297f4619Sad  *    notice, this list of conditions and the following disclaimer in the
50297f4619Sad  *    documentation and/or other materials provided with the distribution.
51297f4619Sad  * 3. All advertising materials mentioning features or use of this software
52297f4619Sad  *    must display the following acknowledgement:
53297f4619Sad  *          This product includes software developed for the
54297f4619Sad  *          NetBSD Project.  See http://www.NetBSD.org/ for
55297f4619Sad  *          information about NetBSD.
56297f4619Sad  * 4. The name of the author may not be used to endorse or promote products
57297f4619Sad  *    derived from this software without specific prior written permission.
58297f4619Sad  *
59297f4619Sad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60297f4619Sad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61297f4619Sad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62297f4619Sad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63297f4619Sad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64297f4619Sad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65297f4619Sad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66297f4619Sad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67297f4619Sad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68297f4619Sad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69297f4619Sad  *
70297f4619Sad  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
71297f4619Sad  */
72297f4619Sad 
73297f4619Sad #include <sys/cdefs.h>
74297f4619Sad #if !defined(lint)
75*12793652Sad __RCSID("$NetBSD: elf32.c,v 1.3 2006/11/08 23:12:57 ad Exp $");
76297f4619Sad #endif
77297f4619Sad 
78297f4619Sad #ifndef ELFSIZE
79297f4619Sad #define	ELFSIZE		32
80297f4619Sad #endif
81297f4619Sad 
82297f4619Sad #include <sys/param.h>
83297f4619Sad #include <sys/exec_elf.h>
84297f4619Sad #include <sys/queue.h>
8596d4a987Sad 
8696d4a987Sad #include <dev/lockstat.h>
87297f4619Sad 
88297f4619Sad #include <stdio.h>
89297f4619Sad #include <stdlib.h>
90297f4619Sad #include <string.h>
91297f4619Sad #include <unistd.h>
92297f4619Sad #include <err.h>
93297f4619Sad 
94297f4619Sad #include "extern.h"
95297f4619Sad 
96297f4619Sad #if (ELFSIZE == 32)
97297f4619Sad #define	NAME(x)	x##32
98297f4619Sad #elif (ELFSIZE == 64)
99297f4619Sad #define	NAME(x)	x##64
100297f4619Sad #endif
101297f4619Sad 
102297f4619Sad static int		nsyms;
103297f4619Sad static Elf_Sym		*symp;
104297f4619Sad static char		*strp;
105297f4619Sad 
106297f4619Sad int
107297f4619Sad NAME(loadsym)(int fd)
108297f4619Sad {
109297f4619Sad 	Elf_Shdr symhdr, strhdr;
110297f4619Sad 	Elf_Ehdr ehdr;
111297f4619Sad 	size_t sz;
112297f4619Sad 	off_t off;
113297f4619Sad 	int i;
114297f4619Sad 
115297f4619Sad 	/*
116297f4619Sad 	 * Read the ELF header and make sure it's OK.
117297f4619Sad 	 */
118297f4619Sad 	if (pread(fd, &ehdr, sizeof(ehdr), 0) != sizeof(ehdr))
119297f4619Sad 		return -1;
120297f4619Sad 
121297f4619Sad 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
122297f4619Sad 	    ehdr.e_ident[EI_CLASS] != ELFCLASS)
123297f4619Sad 		return -1;
124297f4619Sad 
125297f4619Sad 	switch (ehdr.e_machine) {
126297f4619Sad 	ELFDEFNNAME(MACHDEP_ID_CASES)
127297f4619Sad 	default:
128297f4619Sad 		return -1;
129297f4619Sad 	}
130297f4619Sad 
131297f4619Sad 	/*
132297f4619Sad 	 * Find the symbol table header, and make sure the binary isn't
133297f4619Sad 	 * stripped.
134297f4619Sad 	 */
135297f4619Sad 	off = ehdr.e_shoff;
136297f4619Sad 	for (i = 0; i < ehdr.e_shnum; i++, off += sizeof(symhdr)) {
137297f4619Sad 		sz = pread(fd, &symhdr, sizeof(symhdr), off);
138297f4619Sad 		if (sz != sizeof(symhdr))
139297f4619Sad 			err(EXIT_FAILURE, "pread (section headers)");
140297f4619Sad 		if (symhdr.sh_type == SHT_SYMTAB)
141297f4619Sad 			break;
142297f4619Sad 	}
143297f4619Sad 	if (i == ehdr.e_shnum || symhdr.sh_offset == 0)
144297f4619Sad 		err(EXIT_FAILURE, "namelist is stripped");
145297f4619Sad 
146297f4619Sad 	/*
147297f4619Sad 	 * Pull in the string table header, and then read in both the symbol
148297f4619Sad 	 * table and string table proper.
149297f4619Sad 	 *
150297f4619Sad 	 * XXX We can't use mmap(), as /dev/ksyms doesn't support mmap yet.
151297f4619Sad 	 */
152297f4619Sad 	off = ehdr.e_shoff + symhdr.sh_link * sizeof(symhdr);
153297f4619Sad 	if (pread(fd, &strhdr, sizeof(strhdr), off) != sizeof(strhdr))
154297f4619Sad 		err(EXIT_FAILURE, "pread");
155297f4619Sad 
156297f4619Sad 	if ((symp = malloc(symhdr.sh_size)) == NULL)
157297f4619Sad 		err(EXIT_FAILURE, "malloc (symbol table)");
158297f4619Sad 	sz = pread(fd, symp, symhdr.sh_size, symhdr.sh_offset	);
159297f4619Sad 	if (sz != symhdr.sh_size)
160297f4619Sad 		err(EXIT_FAILURE, "pread (symbol table)");
161297f4619Sad 
162297f4619Sad 	if ((strp = malloc(strhdr.sh_size)) == NULL)
163297f4619Sad 		err(EXIT_FAILURE, "malloc (string table)");
164297f4619Sad 	sz = pread(fd, strp, strhdr.sh_size, strhdr.sh_offset);
165297f4619Sad 	if (sz != strhdr.sh_size)
166297f4619Sad 		err(EXIT_FAILURE, "pread (string table)");
167297f4619Sad 
168297f4619Sad 	nsyms = (int)(symhdr.sh_size / sizeof(Elf_Sym));
169297f4619Sad 
170297f4619Sad 	return 0;
171297f4619Sad }
172297f4619Sad 
173297f4619Sad int
174297f4619Sad NAME(findsym)(findsym_t find, char *name, uintptr_t *start, uintptr_t *end)
175297f4619Sad {
176297f4619Sad 	static int lastptr[FIND_MAX];
177297f4619Sad 	uintptr_t sa, ea;
178*12793652Sad 	int i, rv, st, off;
179*12793652Sad 
180*12793652Sad 	switch (find) {
181*12793652Sad 	case LOCK_BYNAME:
182*12793652Sad 	case LOCK_BYADDR:
183*12793652Sad 		st = STT_OBJECT;
184*12793652Sad 		break;
185*12793652Sad 	case FUNC_BYNAME:
186*12793652Sad 	case FUNC_BYADDR:
187*12793652Sad 		st = STT_FUNC;
188*12793652Sad 		break;
189*12793652Sad 	default:
190*12793652Sad 		return -1;
191*12793652Sad 	}
192297f4619Sad 
193297f4619Sad 	rv = -1;
194297f4619Sad 
195297f4619Sad #ifdef dump_core
196297f4619Sad 	for (i = lastptr[find];;) {
197297f4619Sad #else
198297f4619Sad 	for (i = 0; i < nsyms; i++) {
199297f4619Sad #endif
200297f4619Sad 		switch (find) {
201297f4619Sad 		case LOCK_BYNAME:
202*12793652Sad 		case FUNC_BYNAME:
203*12793652Sad 			if (ELF_ST_TYPE(symp[i].st_info) != st)
204297f4619Sad 				break;
205297f4619Sad 			if (strcmp(&strp[symp[i].st_name], name) != 0)
206297f4619Sad 				break;
207297f4619Sad 			*start = (uintptr_t)symp[i].st_value;
208297f4619Sad 			*end = *start + (uintptr_t)symp[i].st_size;
209297f4619Sad 			goto found;
210297f4619Sad 
211297f4619Sad 		case LOCK_BYADDR:
212297f4619Sad 		case FUNC_BYADDR:
213*12793652Sad 			if (ELF_ST_TYPE(symp[i].st_info) != st)
214297f4619Sad 				break;
215297f4619Sad 			sa = (uintptr_t)symp[i].st_value;
216297f4619Sad 			ea = sa + (uintptr_t)symp[i].st_size - 1;
217297f4619Sad 			if (*start < sa || *start > ea)
218297f4619Sad 				break;
219*12793652Sad 			off = (int)(*start - sa);
220*12793652Sad 			if (off == 0)
221*12793652Sad 				strcpy(name, &strp[symp[i].st_name]);
222*12793652Sad 			else
223297f4619Sad 				sprintf(name, "%s+0x%x",
224*12793652Sad 				    &strp[symp[i].st_name], off);
225297f4619Sad 			goto found;
226297f4619Sad 
227297f4619Sad 		default:
228297f4619Sad 			break;
229297f4619Sad 		}
230297f4619Sad 
231297f4619Sad #ifdef dump_core
232297f4619Sad 		if (++i >= nsyms)
233297f4619Sad 			i = 0;
234297f4619Sad 		if (i == lastptr[find])
235297f4619Sad 			return -1;
236297f4619Sad #endif
237297f4619Sad 	}
238297f4619Sad 
239297f4619Sad 	return -1;
240297f4619Sad 
241297f4619Sad  found:
242297f4619Sad  	lastptr[find] = i;
243297f4619Sad  	return 0;
244297f4619Sad }
245