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