Home | History | Annotate | Line # | Download | only in kvm_mkdb
nlist.c revision 1.11
      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[] = "from: @(#)nlist.c	8.1 (Berkeley) 6/6/93";*/
     36 static char *rcsid = "$Id: nlist.c,v 1.11 1996/05/16 21:17:21 gwr Exp $";
     37 #endif /* not lint */
     38 
     39 #include <sys/param.h>
     40 
     41 #include <a.out.h>
     42 #include <db.h>
     43 #include <err.h>
     44 #include <errno.h>
     45 #include <fcntl.h>
     46 #include <kvm.h>
     47 #include <limits.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <unistd.h>
     52 
     53 #include "extern.h"
     54 
     55 typedef struct nlist NLIST;
     56 #define	_strx	n_un.n_strx
     57 #define	_name	n_un.n_name
     58 
     59 #define	badfmt(str)	errx(1, "%s: %s: %s", kfile, str, strerror(EFTYPE))
     60 
     61 static void badread __P((int, char *));
     62 static u_long get_kerntext __P((char *kfn));
     63 
     64 static char *kfile;
     65 
     66 void
     67 create_knlist(name, db)
     68 	char *name;
     69 	DB *db;
     70 {
     71 	register int nsyms;
     72 	struct exec ebuf;
     73 	FILE *fp;
     74 	NLIST nbuf;
     75 	DBT data, key;
     76 	int fd, nr, strsize;
     77 	u_long kerntextoff;
     78 	char *strtab, buf[1024];
     79 
     80 	kfile = name;
     81 	if ((fd = open(name, O_RDONLY, 0)) < 0)
     82 		err(1, "%s", name);
     83 
     84 	/* Read in exec structure. */
     85 	nr = read(fd, &ebuf, sizeof(struct exec));
     86 	if (nr != sizeof(struct exec))
     87 		badfmt("no exec header");
     88 
     89 	/* Check magic number and symbol count. */
     90 	if (N_BADMAG(ebuf))
     91 		badfmt("bad magic number");
     92 	if (!ebuf.a_syms)
     93 		badfmt("stripped");
     94 
     95 	/* Seek to string table. */
     96 	if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1)
     97 		badfmt("corrupted string table");
     98 
     99 	/* Read in the size of the symbol table. */
    100 	nr = read(fd, (char *)&strsize, sizeof(strsize));
    101 	if (nr != sizeof(strsize))
    102 		badread(nr, "no symbol table");
    103 
    104 	/* Read in the string table. */
    105 	strsize -= sizeof(strsize);
    106 	if (!(strtab = malloc(strsize)))
    107 		err(1, NULL);
    108 	if ((nr = read(fd, strtab, strsize)) != strsize)
    109 		badread(nr, "corrupted symbol table");
    110 
    111 	/* Seek to symbol table. */
    112 	if (!(fp = fdopen(fd, "r")))
    113 		err(1, "%s", name);
    114 	if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1)
    115 		err(1, "%s", name);
    116 
    117 	data.data = (u_char *)&nbuf;
    118 	data.size = sizeof(NLIST);
    119 
    120 	kerntextoff = get_kerntext(name);
    121 
    122 	/* Read each symbol and enter it into the database. */
    123 	nsyms = ebuf.a_syms / sizeof(struct nlist);
    124 	while (nsyms--) {
    125 		if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) {
    126 			if (feof(fp))
    127 				badfmt("corrupted symbol table");
    128 			err(1, "%s", name);
    129 		}
    130 		if (!nbuf._strx || nbuf.n_type&N_STAB)
    131 			continue;
    132 
    133 		key.data = (u_char *)strtab + nbuf._strx - sizeof(long);
    134 		key.size = strlen((char *)key.data);
    135 		if (db->put(db, &key, &data, 0))
    136 			err(1, "record enter");
    137 
    138 		if (strcmp((char *)key.data, VRS_SYM) == 0) {
    139 			long cur_off, voff;
    140 			/*
    141 			 * Calculate offset relative to a normal (non-kernel)
    142 			 * a.out.  KERNTEXTOFF is where the kernel is really
    143 			 * loaded; N_TXTADDR is where a normal file is loaded.
    144 			 * From there, locate file offset in text or data.
    145 			 */
    146 			voff = nbuf.n_value - kerntextoff + N_TXTADDR(ebuf);
    147 			if ((nbuf.n_type & N_TYPE) == N_TEXT)
    148 				voff += N_TXTOFF(ebuf) - N_TXTADDR(ebuf);
    149 			else
    150 				voff += N_DATOFF(ebuf) - N_DATADDR(ebuf);
    151 			cur_off = ftell(fp);
    152 			if (fseek(fp, voff, SEEK_SET) == -1)
    153 				badfmt("corrupted string table");
    154 
    155 			/*
    156 			 * Read version string up to, and including newline.
    157 			 * This code assumes that a newline terminates the
    158 			 * version line.
    159 			 */
    160 			if (fgets(buf, sizeof(buf), fp) == NULL)
    161 				badfmt("corrupted string table");
    162 
    163 			key.data = (u_char *)VRS_KEY;
    164 			key.size = sizeof(VRS_KEY) - 1;
    165 			data.data = (u_char *)buf;
    166 			data.size = strlen(buf);
    167 			if (db->put(db, &key, &data, 0))
    168 				err(1, "record enter");
    169 
    170 			/* Restore to original values. */
    171 			data.data = (u_char *)&nbuf;
    172 			data.size = sizeof(NLIST);
    173 			if (fseek(fp, cur_off, SEEK_SET) == -1)
    174 				badfmt("corrupted string table");
    175 		}
    176 	}
    177 	(void)fclose(fp);
    178 }
    179 
    180 static void
    181 badread(nr, p)
    182 	int nr;
    183 	char *p;
    184 {
    185 	if (nr < 0)
    186 		err(1, "%s", kfile);
    187 	badfmt(p);
    188 }
    189 
    190 /*
    191  * XXX: Using this value from machine/param.h introduces a
    192  * XXX: machine dependency on this program, so /usr can not
    193  * XXX: be shared between (i.e.) several m68k machines.
    194  * Instead of compiling in KERNTEXTOFF or KERNBASE, try to
    195  * determine the text start address from a standard symbol.
    196  * For backward compatibility, use the old compiled-in way
    197  * when the standard symbol name is not found.
    198  */
    199 #ifndef KERNTEXTOFF
    200 #define KERNTEXTOFF KERNBASE
    201 #endif
    202 
    203 static u_long
    204 get_kerntext(name)
    205 	char *name;
    206 {
    207 	NLIST nl[2];
    208 
    209 	bzero((caddr_t)nl, sizeof(nl));
    210 	nl[0]._name = "_kernel_text";
    211 
    212 	if (nlist(name, nl) != 0)
    213 		return (KERNTEXTOFF);
    214 
    215 	return (nl[0].n_value);
    216 }
    217