Home | History | Annotate | Line # | Download | only in kvm_mkdb
nlist.c revision 1.9
      1 /*-
      2  * Copyright (c) 1990, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char sccsid[] = "@(#)nlist.c	8.1 (Berkeley) 6/6/93";
     36 #endif /* not lint */
     37 
     38 #include <sys/param.h>
     39 
     40 #include <a.out.h>
     41 #include <db.h>
     42 #include <err.h>
     43 #include <errno.h>
     44 #include <fcntl.h>
     45 #include <kvm.h>
     46 #include <limits.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <unistd.h>
     51 
     52 #include "extern.h"
     53 
     54 typedef struct nlist NLIST;
     55 #define	_strx	n_un.n_strx
     56 #define	_name	n_un.n_name
     57 
     58 #define	badfmt(str)	errx(1, "%s: %s: %s", kfile, str, strerror(EFTYPE))
     59 
     60 static void badread __P((int, char *));
     61 
     62 static char *kfile;
     63 
     64 void
     65 create_knlist(name, db)
     66 	char *name;
     67 	DB *db;
     68 {
     69 	register int nsyms;
     70 	struct exec ebuf;
     71 	FILE *fp;
     72 	NLIST nbuf;
     73 	DBT data, key;
     74 	int fd, nr, strsize;
     75 	char *strtab, buf[1024];
     76 
     77 	kfile = name;
     78 	if ((fd = open(name, O_RDONLY, 0)) < 0)
     79 		err(1, "%s", name);
     80 
     81 	/* Read in exec structure. */
     82 	nr = read(fd, &ebuf, sizeof(struct exec));
     83 	if (nr != sizeof(struct exec))
     84 		badfmt("no exec header");
     85 
     86 	/* Check magic number and symbol count. */
     87 	if (N_BADMAG(ebuf))
     88 		badfmt("bad magic number");
     89 	if (!ebuf.a_syms)
     90 		badfmt("stripped");
     91 
     92 	/* Seek to string table. */
     93 	if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1)
     94 		badfmt("corrupted string table");
     95 
     96 	/* Read in the size of the symbol table. */
     97 	nr = read(fd, (char *)&strsize, sizeof(strsize));
     98 	if (nr != sizeof(strsize))
     99 		badread(nr, "no symbol table");
    100 
    101 	/* Read in the string table. */
    102 	strsize -= sizeof(strsize);
    103 	if (!(strtab = malloc(strsize)))
    104 		err(1, NULL);
    105 	if ((nr = read(fd, strtab, strsize)) != strsize)
    106 		badread(nr, "corrupted symbol table");
    107 
    108 	/* Seek to symbol table. */
    109 	if (!(fp = fdopen(fd, "r")))
    110 		err(1, "%s", name);
    111 	if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1)
    112 		err(1, "%s", name);
    113 
    114 	data.data = (u_char *)&nbuf;
    115 	data.size = sizeof(NLIST);
    116 
    117 	/* Read each symbol and enter it into the database. */
    118 	nsyms = ebuf.a_syms / sizeof(struct nlist);
    119 	while (nsyms--) {
    120 		if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) {
    121 			if (feof(fp))
    122 				badfmt("corrupted symbol table");
    123 			err(1, "%s", name);
    124 		}
    125 		if (!nbuf._strx || nbuf.n_type&N_STAB)
    126 			continue;
    127 
    128 		key.data = (u_char *)strtab + nbuf._strx - sizeof(long);
    129 		key.size = strlen((char *)key.data);
    130 		if (db->put(db, &key, &data, 0))
    131 			err(1, "record enter");
    132 
    133 		if (strcmp((char *)key.data, VRS_SYM) == 0) {
    134 			long cur_off, voff;
    135 #ifndef KERNTEXTOFF
    136 #define KERNTEXTOFF KERNBASE
    137 #endif
    138 			/*
    139 			 * Calculate offset relative to a normal (non-kernel)
    140 			 * a.out.  KERNTEXTOFF is where the kernel is really
    141 			 * loaded; N_TXTADDR is where a normal file is loaded.
    142 			 * From there, locate file offset in text or data.
    143 			 */
    144 			voff = nbuf.n_value - KERNTEXTOFF + N_TXTADDR(ebuf);
    145 			if ((nbuf.n_type & N_TYPE) == N_TEXT)
    146 				voff += N_TXTOFF(ebuf) - N_TXTADDR(ebuf);
    147 			else
    148 				voff += N_DATOFF(ebuf) - N_DATADDR(ebuf);
    149 			cur_off = ftell(fp);
    150 			if (fseek(fp, voff, SEEK_SET) == -1)
    151 				badfmt("corrupted string table");
    152 
    153 			/*
    154 			 * Read version string up to, and including newline.
    155 			 * This code assumes that a newline terminates the
    156 			 * version line.
    157 			 */
    158 			if (fgets(buf, sizeof(buf), fp) == NULL)
    159 				badfmt("corrupted string table");
    160 
    161 			key.data = (u_char *)VRS_KEY;
    162 			key.size = sizeof(VRS_KEY) - 1;
    163 			data.data = (u_char *)buf;
    164 			data.size = strlen(buf);
    165 			if (db->put(db, &key, &data, 0))
    166 				err(1, "record enter");
    167 
    168 			/* Restore to original values. */
    169 			data.data = (u_char *)&nbuf;
    170 			data.size = sizeof(NLIST);
    171 			if (fseek(fp, cur_off, SEEK_SET) == -1)
    172 				badfmt("corrupted string table");
    173 		}
    174 	}
    175 	(void)fclose(fp);
    176 }
    177 
    178 static void
    179 badread(nr, p)
    180 	int nr;
    181 	char *p;
    182 {
    183 	if (nr < 0)
    184 		err(1, "%s", kfile);
    185 	badfmt(p);
    186 }
    187