Home | History | Annotate | Line # | Download | only in lockstat
elf32.c revision 1.1
      1  1.1  ad /*	$NetBSD: elf32.c,v 1.1 2006/09/07 00:50:45 ad Exp $	*/
      2  1.1  ad 
      3  1.1  ad /*-
      4  1.1  ad  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  1.1  ad  * All rights reserved.
      6  1.1  ad  *
      7  1.1  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  ad  * by Andrew Doran.
      9  1.1  ad  *
     10  1.1  ad  * Redistribution and use in source and binary forms, with or without
     11  1.1  ad  * modification, are permitted provided that the following conditions
     12  1.1  ad  * are met:
     13  1.1  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  ad  *    documentation and/or other materials provided with the distribution.
     18  1.1  ad  * 3. All advertising materials mentioning features or use of this software
     19  1.1  ad  *    must display the following acknowledgement:
     20  1.1  ad  *	This product includes software developed by the NetBSD
     21  1.1  ad  *	Foundation, Inc. and its contributors.
     22  1.1  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  ad  *    contributors may be used to endorse or promote products derived
     24  1.1  ad  *    from this software without specific prior written permission.
     25  1.1  ad  *
     26  1.1  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  ad  */
     38  1.1  ad 
     39  1.1  ad /*
     40  1.1  ad  * Copyright (c) 1996 Christopher G. Demetriou
     41  1.1  ad  * All rights reserved.
     42  1.1  ad  *
     43  1.1  ad  * Redistribution and use in source and binary forms, with or without
     44  1.1  ad  * modification, are permitted provided that the following conditions
     45  1.1  ad  * are met:
     46  1.1  ad  * 1. Redistributions of source code must retain the above copyright
     47  1.1  ad  *    notice, this list of conditions and the following disclaimer.
     48  1.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.1  ad  *    notice, this list of conditions and the following disclaimer in the
     50  1.1  ad  *    documentation and/or other materials provided with the distribution.
     51  1.1  ad  * 3. All advertising materials mentioning features or use of this software
     52  1.1  ad  *    must display the following acknowledgement:
     53  1.1  ad  *          This product includes software developed for the
     54  1.1  ad  *          NetBSD Project.  See http://www.NetBSD.org/ for
     55  1.1  ad  *          information about NetBSD.
     56  1.1  ad  * 4. The name of the author may not be used to endorse or promote products
     57  1.1  ad  *    derived from this software without specific prior written permission.
     58  1.1  ad  *
     59  1.1  ad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     60  1.1  ad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     61  1.1  ad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62  1.1  ad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     63  1.1  ad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     64  1.1  ad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     65  1.1  ad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     66  1.1  ad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     67  1.1  ad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     68  1.1  ad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     69  1.1  ad  *
     70  1.1  ad  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
     71  1.1  ad  */
     72  1.1  ad 
     73  1.1  ad #include <sys/cdefs.h>
     74  1.1  ad #if !defined(lint)
     75  1.1  ad __RCSID("$NetBSD: elf32.c,v 1.1 2006/09/07 00:50:45 ad Exp $");
     76  1.1  ad #endif
     77  1.1  ad 
     78  1.1  ad #ifndef ELFSIZE
     79  1.1  ad #define	ELFSIZE		32
     80  1.1  ad #endif
     81  1.1  ad 
     82  1.1  ad #include <sys/param.h>
     83  1.1  ad #include <sys/exec_elf.h>
     84  1.1  ad #include <sys/queue.h>
     85  1.1  ad #include <sys/lockstat.h>
     86  1.1  ad 
     87  1.1  ad #include <stdio.h>
     88  1.1  ad #include <stdlib.h>
     89  1.1  ad #include <string.h>
     90  1.1  ad #include <unistd.h>
     91  1.1  ad #include <err.h>
     92  1.1  ad 
     93  1.1  ad #include "extern.h"
     94  1.1  ad 
     95  1.1  ad #if (ELFSIZE == 32)
     96  1.1  ad #define	NAME(x)	x##32
     97  1.1  ad #elif (ELFSIZE == 64)
     98  1.1  ad #define	NAME(x)	x##64
     99  1.1  ad #endif
    100  1.1  ad 
    101  1.1  ad static int		nsyms;
    102  1.1  ad static Elf_Sym		*symp;
    103  1.1  ad static char		*strp;
    104  1.1  ad 
    105  1.1  ad int
    106  1.1  ad NAME(loadsym)(int fd)
    107  1.1  ad {
    108  1.1  ad 	Elf_Shdr symhdr, strhdr;
    109  1.1  ad 	Elf_Ehdr ehdr;
    110  1.1  ad 	size_t sz;
    111  1.1  ad 	off_t off;
    112  1.1  ad 	int i;
    113  1.1  ad 
    114  1.1  ad 	/*
    115  1.1  ad 	 * Read the ELF header and make sure it's OK.
    116  1.1  ad 	 */
    117  1.1  ad 	if (pread(fd, &ehdr, sizeof(ehdr), 0) != sizeof(ehdr))
    118  1.1  ad 		return -1;
    119  1.1  ad 
    120  1.1  ad 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
    121  1.1  ad 	    ehdr.e_ident[EI_CLASS] != ELFCLASS)
    122  1.1  ad 		return -1;
    123  1.1  ad 
    124  1.1  ad 	switch (ehdr.e_machine) {
    125  1.1  ad 	ELFDEFNNAME(MACHDEP_ID_CASES)
    126  1.1  ad 	default:
    127  1.1  ad 		return -1;
    128  1.1  ad 	}
    129  1.1  ad 
    130  1.1  ad 	/*
    131  1.1  ad 	 * Find the symbol table header, and make sure the binary isn't
    132  1.1  ad 	 * stripped.
    133  1.1  ad 	 */
    134  1.1  ad 	off = ehdr.e_shoff;
    135  1.1  ad 	for (i = 0; i < ehdr.e_shnum; i++, off += sizeof(symhdr)) {
    136  1.1  ad 		sz = pread(fd, &symhdr, sizeof(symhdr), off);
    137  1.1  ad 		if (sz != sizeof(symhdr))
    138  1.1  ad 			err(EXIT_FAILURE, "pread (section headers)");
    139  1.1  ad 		if (symhdr.sh_type == SHT_SYMTAB)
    140  1.1  ad 			break;
    141  1.1  ad 	}
    142  1.1  ad 	if (i == ehdr.e_shnum || symhdr.sh_offset == 0)
    143  1.1  ad 		err(EXIT_FAILURE, "namelist is stripped");
    144  1.1  ad 
    145  1.1  ad 	/*
    146  1.1  ad 	 * Pull in the string table header, and then read in both the symbol
    147  1.1  ad 	 * table and string table proper.
    148  1.1  ad 	 *
    149  1.1  ad 	 * XXX We can't use mmap(), as /dev/ksyms doesn't support mmap yet.
    150  1.1  ad 	 */
    151  1.1  ad 	off = ehdr.e_shoff + symhdr.sh_link * sizeof(symhdr);
    152  1.1  ad 	if (pread(fd, &strhdr, sizeof(strhdr), off) != sizeof(strhdr))
    153  1.1  ad 		err(EXIT_FAILURE, "pread");
    154  1.1  ad 
    155  1.1  ad 	if ((symp = malloc(symhdr.sh_size)) == NULL)
    156  1.1  ad 		err(EXIT_FAILURE, "malloc (symbol table)");
    157  1.1  ad 	sz = pread(fd, symp, symhdr.sh_size, symhdr.sh_offset	);
    158  1.1  ad 	if (sz != symhdr.sh_size)
    159  1.1  ad 		err(EXIT_FAILURE, "pread (symbol table)");
    160  1.1  ad 
    161  1.1  ad 	if ((strp = malloc(strhdr.sh_size)) == NULL)
    162  1.1  ad 		err(EXIT_FAILURE, "malloc (string table)");
    163  1.1  ad 	sz = pread(fd, strp, strhdr.sh_size, strhdr.sh_offset);
    164  1.1  ad 	if (sz != strhdr.sh_size)
    165  1.1  ad 		err(EXIT_FAILURE, "pread (string table)");
    166  1.1  ad 
    167  1.1  ad 	nsyms = (int)(symhdr.sh_size / sizeof(Elf_Sym));
    168  1.1  ad 
    169  1.1  ad 	return 0;
    170  1.1  ad }
    171  1.1  ad 
    172  1.1  ad int
    173  1.1  ad NAME(findsym)(findsym_t find, char *name, uintptr_t *start, uintptr_t *end)
    174  1.1  ad {
    175  1.1  ad 	static int lastptr[FIND_MAX];
    176  1.1  ad 	uintptr_t sa, ea;
    177  1.1  ad 	int i, rv;
    178  1.1  ad 
    179  1.1  ad 	rv = -1;
    180  1.1  ad 
    181  1.1  ad #ifdef dump_core
    182  1.1  ad 	for (i = lastptr[find];;) {
    183  1.1  ad #else
    184  1.1  ad 	for (i = 0; i < nsyms; i++) {
    185  1.1  ad #endif
    186  1.1  ad 		switch (find) {
    187  1.1  ad 		case LOCK_BYNAME:
    188  1.1  ad 			if (ELF_ST_TYPE(symp[i].st_info) != STT_OBJECT)
    189  1.1  ad 				break;
    190  1.1  ad 			if (strcmp(&strp[symp[i].st_name], name) != 0)
    191  1.1  ad 				break;
    192  1.1  ad 			*start = (uintptr_t)symp[i].st_value;
    193  1.1  ad 			*end = *start + (uintptr_t)symp[i].st_size;
    194  1.1  ad 			goto found;
    195  1.1  ad 
    196  1.1  ad 		case LOCK_BYADDR:
    197  1.1  ad 			if (ELF_ST_TYPE(symp[i].st_info) != STT_OBJECT)
    198  1.1  ad 				break;
    199  1.1  ad 			if (*start != (uintptr_t)symp[i].st_value)
    200  1.1  ad 				break;
    201  1.1  ad 			strcpy(name, &strp[symp[i].st_name]);
    202  1.1  ad 			goto found;
    203  1.1  ad 
    204  1.1  ad 		case FUNC_BYNAME:
    205  1.1  ad 			if (ELF_ST_TYPE(symp[i].st_info) != STT_FUNC)
    206  1.1  ad 				break;
    207  1.1  ad 			if (strcmp(&strp[symp[i].st_name], name) != 0)
    208  1.1  ad 				break;
    209  1.1  ad 			*start = (uintptr_t)symp[i].st_value;
    210  1.1  ad 			*end = *start + (uintptr_t)symp[i].st_size;
    211  1.1  ad 			goto found;
    212  1.1  ad 
    213  1.1  ad 		case FUNC_BYADDR:
    214  1.1  ad 			if (ELF_ST_TYPE(symp[i].st_info) != STT_FUNC)
    215  1.1  ad 				break;
    216  1.1  ad 			sa = (uintptr_t)symp[i].st_value;
    217  1.1  ad 			ea = sa + (uintptr_t)symp[i].st_size - 1;
    218  1.1  ad 			if (*start < sa || *start > ea)
    219  1.1  ad 				break;
    220  1.1  ad 			sprintf(name, "%s+0x%x",
    221  1.1  ad 			    &strp[symp[i].st_name], (int)(*start - sa));
    222  1.1  ad 			goto found;
    223  1.1  ad 
    224  1.1  ad 		default:
    225  1.1  ad 			break;
    226  1.1  ad 		}
    227  1.1  ad 
    228  1.1  ad #ifdef dump_core
    229  1.1  ad 		if (++i >= nsyms)
    230  1.1  ad 			i = 0;
    231  1.1  ad 		if (i == lastptr[find])
    232  1.1  ad 			return -1;
    233  1.1  ad #endif
    234  1.1  ad 	}
    235  1.1  ad 
    236  1.1  ad 	return -1;
    237  1.1  ad 
    238  1.1  ad  found:
    239  1.1  ad  	lastptr[find] = i;
    240  1.1  ad  	return 0;
    241  1.1  ad }
    242