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