Home | History | Annotate | Line # | Download | only in kvm_mkdb
nlist_elf32.c revision 1.10
      1 /*	$NetBSD: nlist_elf32.c,v 1.10 1999/11/04 02:00:18 erh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Christopher G. Demetriou
     17  *	for the NetBSD Project.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __RCSID("$NetBSD: nlist_elf32.c,v 1.10 1999/11/04 02:00:18 erh Exp $");
     36 #endif /* not lint */
     37 
     38 /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
     39 #ifndef ELFSIZE
     40 #define	ELFSIZE		32
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/mman.h>
     45 #include <sys/stat.h>
     46 
     47 #include <a.out.h>
     48 #include <db.h>
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <fcntl.h>
     52 #include <kvm.h>
     53 #include <limits.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <unistd.h>
     58 
     59 #include "extern.h"
     60 
     61 #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
     62 #include <sys/exec_elf.h>
     63 #endif
     64 
     65 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
     66     (defined(NLIST_ELF64) && (ELFSIZE == 64))
     67 
     68 typedef struct nlist NLIST;
     69 #define	_strx	n_un.n_strx
     70 #define	_name	n_un.n_name
     71 
     72 #define	badfmt(str)							\
     73 	do {								\
     74 		warnx("%s: %s: %s", kfile, str, strerror(EFTYPE));	\
     75 		punt();							\
     76 	} while (0)
     77 
     78 #define	check(off, size)	((off < 0) || (off + size > mappedsize))
     79 #define	BAD			do { rv = -1; goto out; } while (0)
     80 #define	BADUNMAP		do { rv = -1; goto unmap; } while (0)
     81 
     82 static const char *kfile;
     83 
     84 int
     85 ELFNAMEEND(create_knlist)(name, db)
     86 	const char *name;
     87 	DB *db;
     88 {
     89 	struct stat st;
     90 	struct nlist nbuf;
     91 	DBT key, data;
     92 	char *mappedfile, *symname, *fsymname, *tmpcp, *strtab;
     93 	size_t mappedsize, symnamesize, fsymnamesize;
     94 	Elf_Ehdr *ehdrp;
     95 	Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
     96 	Elf_Sym *symp;
     97 	Elf_Off shdr_off;
     98 	Elf_Word shdr_size;
     99 #if (ELFSIZE == 32)
    100 	Elf32_Half nshdr;
    101 #elif (ELFSIZE == 64)
    102 	Elf64_Half nshdr;
    103 #endif
    104 	unsigned long i, nsyms;
    105 	int fd, rv;
    106 
    107 	rv = -1;
    108 #ifdef __GNUC__
    109 	/* fix compiler warnings */
    110 	symshdrp = NULL;
    111 	symstrshdrp = NULL;
    112 #endif
    113 
    114 	/*
    115 	 * Open and map the whole file.  If we can't open/stat it,
    116 	 * something bad is going on so we punt.
    117 	 */
    118 	kfile = name;
    119 	if ((fd = open(name, O_RDONLY, 0)) < 0) {
    120 		warn("%s", kfile);
    121 		punt();
    122 	}
    123 	if (fstat(fd, &st) < 0) {
    124 		warn("%s", kfile);
    125 		punt();
    126 	}
    127 	if (st.st_size > SIZE_T_MAX)
    128 		BAD;
    129 
    130 	/*
    131 	 * Map the file in its entirety.
    132 	 */
    133 	mappedsize = st.st_size;
    134 	mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
    135 	    fd, 0);
    136 	if (mappedfile == (char *)-1)
    137 		BAD;
    138 
    139 	/*
    140 	 * Make sure we can access the executable's header
    141 	 * directly, and make sure the recognize the executable
    142 	 * as an ELF binary.
    143 	 */
    144 	if (check(0, sizeof *ehdrp))
    145 		BADUNMAP;
    146 	ehdrp = (Elf_Ehdr *)&mappedfile[0];
    147 
    148 	if (memcmp(ehdrp->e_ident, ELFMAG, SELFMAG) != 0 ||
    149 	    ehdrp->e_ident[EI_CLASS] != ELFCLASS)
    150 		BADUNMAP;
    151 
    152 	switch (ehdrp->e_machine) {
    153 	ELFDEFNNAME(MACHDEP_ID_CASES)
    154 
    155 	default:
    156 		BADUNMAP;
    157 	}
    158 
    159 	/*
    160 	 * We've recognized it as an ELF binary.  From here
    161 	 * on out, all errors are fatal.
    162 	 */
    163 
    164 	/*
    165 	 * Find the symbol list and string table.
    166 	 */
    167 	nshdr = ehdrp->e_shnum;
    168 	shdr_off = ehdrp->e_shoff;
    169 	shdr_size = ehdrp->e_shentsize * nshdr;
    170 
    171 	if (check(shdr_off, shdr_size) ||
    172 	    (sizeof *shdrp != ehdrp->e_shentsize))
    173 		badfmt("bogus section header table");
    174 	shdrp = (Elf_Shdr *)&mappedfile[shdr_off];
    175 
    176 	for (i = 0; i < nshdr; i++) {
    177 		if (shdrp[i].sh_type == SHT_SYMTAB) {
    178 			symshdrp = &shdrp[i];
    179 			symstrshdrp = &shdrp[shdrp[i].sh_link];
    180 		}
    181 	}
    182 
    183 	if (symshdrp->sh_offset == 0)
    184 		badfmt("stripped");
    185 	if (check(symshdrp->sh_offset, symshdrp->sh_size))
    186 		badfmt("bogus symbol section header");
    187 	if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
    188 		badfmt("bogus symbol string section header");
    189 
    190 	symp = (Elf_Sym *)&mappedfile[symshdrp->sh_offset];
    191 	nsyms = symshdrp->sh_size / sizeof(*symp);
    192 	strtab = &mappedfile[symstrshdrp->sh_offset];
    193 
    194 	/*
    195 	 * Set up the data item, pointing to a nlist structure.
    196 	 * which we fill in for each symbol.
    197 	 */
    198 	data.data = (u_char *)&nbuf;
    199 	data.size = sizeof(nbuf);
    200 
    201 	/*
    202 	 * Create a buffer (to be expanded later, if necessary)
    203 	 * to hold symbol names after we've added underscores
    204 	 * to them.
    205 	 */
    206 	symnamesize = 1024;
    207 	if ((symname = malloc(symnamesize)) == NULL) {
    208 		warn("malloc");
    209 		punt();
    210 	}
    211 
    212 	/*
    213 	 * Read each symbol and enter it into the database.
    214 	 */
    215 	for (i = 0; i < nsyms; i++) {
    216 
    217 		/*
    218 		 * No symbol name; ignore this symbol.
    219 		 */
    220 		if (symp[i].st_name == 0)
    221 			continue;
    222 
    223 		/*
    224 		 * Find symbol name, copy it (with added underscore) to
    225 		 * temporary buffer, and prepare the database key for
    226 		 * insertion.
    227 		 */
    228 		fsymname = &strtab[symp[i].st_name];
    229 		fsymnamesize = strlen(fsymname) + 1;
    230 		while (symnamesize < fsymnamesize + 1) {
    231 			symnamesize *= 2;
    232 			if ((symname = realloc(symname, symnamesize)) == NULL) {
    233 				warn("malloc");
    234 				punt();
    235 			}
    236 		}
    237 		strcpy(symname, "_");
    238 		strcat(symname, fsymname);
    239 
    240 		key.data = symname;
    241 		key.size = strlen((char *)key.data);
    242 
    243 		/*
    244 		 * Convert the symbol information into an nlist structure,
    245 		 * as best we can.
    246 		 */
    247 		nbuf.n_value = symp[i].st_value;
    248 		switch (ELFDEFNNAME(ST_TYPE)(symp[i].st_info)) {
    249 		default:
    250 		case STT_NOTYPE:
    251 			nbuf.n_type = N_UNDF;
    252 			break;
    253 		case STT_OBJECT:
    254 			nbuf.n_type = N_DATA;
    255 			break;
    256 		case STT_FUNC:
    257 			nbuf.n_type = N_TEXT;
    258 			break;
    259 		case STT_FILE:
    260 			nbuf.n_type = N_FN;
    261 			break;
    262 		}
    263 		if (ELFDEFNNAME(ST_BIND)(symp[i].st_info) != STB_LOCAL)
    264 			nbuf.n_type |= N_EXT;
    265 		nbuf.n_desc = 0;				/* XXX */
    266 		nbuf.n_other = 0;				/* XXX */
    267 
    268 		/*
    269 		 * Enter the symbol into the database.
    270 		 */
    271 		if (db->put(db, &key, &data, 0)) {
    272 			warn("record enter");
    273 			punt();
    274 		}
    275 
    276 		/*
    277 		 * If it's the kernel version string, we've gotta keep
    278 		 * some extra data around.  Under a seperate key,
    279 		 * we enter the first line (i.e. up to the first newline,
    280 		 * with the newline replaced by a NUL to terminate the
    281 		 * entered string) of the version string.
    282 		 */
    283 		if (strcmp((char *)key.data, VRS_SYM) == 0) {
    284 			key.data = (u_char *)VRS_KEY;
    285 			key.size = sizeof(VRS_KEY) - 1;
    286 			/* Find the version string, relative to its section */
    287 			data.data = strdup(&mappedfile[nbuf.n_value -
    288 			    shdrp[symp[i].st_shndx].sh_addr +
    289 			    shdrp[symp[i].st_shndx].sh_offset]);
    290 			/* assumes newline terminates version. */
    291 			if ((tmpcp = strchr(data.data, '\n')) != NULL)
    292 				*tmpcp = '\0';
    293 			data.size = strlen((char *)data.data);
    294 
    295 			if (db->put(db, &key, &data, 0)) {
    296 				warn("record enter");
    297 				punt();
    298 			}
    299 
    300 			/* free pointer created by strdup(). */
    301 			free(data.data);
    302 
    303 			/* Restore to original values */
    304 			data.data = (u_char *)&nbuf;
    305 			data.size = sizeof(nbuf);
    306 		}
    307 	}
    308 
    309 	rv = 0;
    310 
    311 unmap:
    312 	munmap(mappedfile, mappedsize);
    313 out:
    314 	return (rv);
    315 }
    316 
    317 #endif
    318