Home | History | Annotate | Line # | Download | only in kvm_mkdb
nlist.c revision 1.5
      1  1.1      cgd /*-
      2  1.1      cgd  * Copyright (c) 1990 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.2      cgd  *
     33  1.2      cgd  * PATCHES MAGIC                LEVEL   PATCH THAT GOT US HERE
     34  1.2      cgd  * --------------------         -----   ----------------------
     35  1.2      cgd  * CURRENT PATCH LEVEL:         1       00032
     36  1.2      cgd  * --------------------         -----   ----------------------
     37  1.2      cgd  *
     38  1.2      cgd  * 05 Aug 92	David Greenman		Fix kernel namelist db create/use
     39  1.1      cgd  */
     40  1.1      cgd 
     41  1.1      cgd #ifndef lint
     42  1.5  mycroft /*static char sccsid[] = "from: @(#)nlist.c	5.4 (Berkeley) 4/27/91";*/
     43  1.5  mycroft static char rcsid[] = "$Id: nlist.c,v 1.5 1993/08/01 17:59:19 mycroft Exp $";
     44  1.1      cgd #endif /* not lint */
     45  1.1      cgd 
     46  1.1      cgd #include <sys/param.h>
     47  1.1      cgd #include <fcntl.h>
     48  1.1      cgd #include <limits.h>
     49  1.1      cgd #include <a.out.h>
     50  1.1      cgd #include <db.h>
     51  1.1      cgd #include <errno.h>
     52  1.1      cgd #include <unistd.h>
     53  1.1      cgd #include <kvm.h>
     54  1.1      cgd #include <stdio.h>
     55  1.1      cgd #include <string.h>
     56  1.1      cgd #include <stdlib.h>
     57  1.1      cgd 
     58  1.1      cgd typedef struct nlist NLIST;
     59  1.1      cgd #define	_strx	n_un.n_strx
     60  1.1      cgd #define	_name	n_un.n_name
     61  1.1      cgd 
     62  1.1      cgd static char *kfile;
     63  1.1      cgd 
     64  1.1      cgd create_knlist(name, db)
     65  1.1      cgd 	char *name;
     66  1.1      cgd 	DB *db;
     67  1.1      cgd {
     68  1.1      cgd 	register int nsyms;
     69  1.1      cgd 	struct exec ebuf;
     70  1.1      cgd 	FILE *fp;
     71  1.1      cgd 	NLIST nbuf;
     72  1.1      cgd 	DBT data, key;
     73  1.1      cgd 	int fd, nr, strsize;
     74  1.1      cgd 	char *strtab, buf[1024];
     75  1.1      cgd 
     76  1.1      cgd 	kfile = name;
     77  1.1      cgd 	if ((fd = open(name, O_RDONLY, 0)) < 0)
     78  1.1      cgd 		error(name);
     79  1.1      cgd 
     80  1.1      cgd 	/* Read in exec structure. */
     81  1.1      cgd 	nr = read(fd, (char *)&ebuf, sizeof(struct exec));
     82  1.1      cgd 	if (nr != sizeof(struct exec))
     83  1.1      cgd 		badfmt(nr, "no exec header");
     84  1.1      cgd 
     85  1.1      cgd 	/* Check magic number and symbol count. */
     86  1.1      cgd 	if (N_BADMAG(ebuf))
     87  1.1      cgd 		badfmt("bad magic number");
     88  1.1      cgd 	if (!ebuf.a_syms)
     89  1.1      cgd 		badfmt("stripped");
     90  1.1      cgd 
     91  1.1      cgd 	/* Seek to string table. */
     92  1.1      cgd 	if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1)
     93  1.1      cgd 		badfmt("corrupted string table");
     94  1.1      cgd 
     95  1.1      cgd 	/* Read in the size of the symbol table. */
     96  1.1      cgd 	nr = read(fd, (char *)&strsize, sizeof(strsize));
     97  1.1      cgd 	if (nr != sizeof(strsize))
     98  1.1      cgd 		badread(nr, "no symbol table");
     99  1.1      cgd 
    100  1.1      cgd 	/* Read in the string table. */
    101  1.1      cgd 	strsize -= sizeof(strsize);
    102  1.1      cgd 	if (!(strtab = (char *)malloc(strsize)))
    103  1.1      cgd 		error(name);
    104  1.1      cgd 	if ((nr = read(fd, strtab, strsize)) != strsize)
    105  1.1      cgd 		badread(nr, "corrupted symbol table");
    106  1.1      cgd 
    107  1.1      cgd 	/* Seek to symbol table. */
    108  1.1      cgd 	if (!(fp = fdopen(fd, "r")))
    109  1.1      cgd 		error(name);
    110  1.1      cgd 	if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1)
    111  1.1      cgd 		error(name);
    112  1.1      cgd 
    113  1.1      cgd 	data.data = (u_char *)&nbuf;
    114  1.1      cgd 	data.size = sizeof(NLIST);
    115  1.1      cgd 
    116  1.1      cgd 	/* Read each symbol and enter it into the database. */
    117  1.1      cgd 	nsyms = ebuf.a_syms / sizeof(struct nlist);
    118  1.1      cgd 	while (nsyms--) {
    119  1.1      cgd 		if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) {
    120  1.1      cgd 			if (feof(fp))
    121  1.1      cgd 				badfmt("corrupted symbol table");
    122  1.1      cgd 			error(name);
    123  1.1      cgd 		}
    124  1.1      cgd 		if (!nbuf._strx || nbuf.n_type&N_STAB)
    125  1.1      cgd 			continue;
    126  1.1      cgd 
    127  1.1      cgd 		key.data = (u_char *)strtab + nbuf._strx - sizeof(long);
    128  1.1      cgd 		key.size = strlen((char *)key.data);
    129  1.1      cgd 		if ((db->put)(db, &key, &data, 0))
    130  1.1      cgd 			error("put");
    131  1.1      cgd 
    132  1.1      cgd 		if (!strncmp((char *)key.data, VRS_SYM, sizeof(VRS_SYM) - 1)) {
    133  1.1      cgd 			off_t cur_off, rel_off, vers_off;
    134  1.1      cgd 
    135  1.1      cgd 			/* Offset relative to start of text image in VM. */
    136  1.1      cgd #ifdef hp300
    137  1.1      cgd 			rel_off = nbuf.n_value;
    138  1.1      cgd #endif
    139  1.1      cgd #ifdef tahoe
    140  1.1      cgd 			/*
    141  1.1      cgd 			 * On tahoe, first 0x800 is reserved for communication
    142  1.1      cgd 			 * with the console processor.
    143  1.1      cgd 			 */
    144  1.1      cgd 			rel_off = ((nbuf.n_value & ~KERNBASE) - 0x800);
    145  1.1      cgd #endif
    146  1.1      cgd #ifdef vax
    147  1.1      cgd 			rel_off = nbuf.n_value & ~KERNBASE;
    148  1.2      cgd #endif
    149  1.2      cgd #ifdef i386
    150  1.4  mycroft 			/*
    151  1.4  mycroft 			 * XXX: This is a KLUGE to handle the kernel being
    152  1.4  mycroft 			 * loaded at a different address than KERNBASE.  Stupid
    153  1.4  mycroft 			 * a.out format has no way of recording the text
    154  1.4  mycroft 			 * address we gave ld.  It only works for multiples of
    155  1.4  mycroft 			 * 1MB.
    156  1.4  mycroft 			 */
    157  1.4  mycroft 			rel_off = ((nbuf.n_value - (ebuf.a_entry & -0x100000))
    158  1.4  mycroft 				  + CLBYTES);
    159  1.1      cgd #endif
    160  1.1      cgd 			/*
    161  1.1      cgd 			 * When loaded, data is rounded to next page cluster
    162  1.1      cgd 			 * after text, but not in file.
    163  1.1      cgd 			 */
    164  1.1      cgd 			rel_off -= CLBYTES - (ebuf.a_text % CLBYTES);
    165  1.1      cgd 			vers_off = N_TXTOFF(ebuf) + rel_off;
    166  1.1      cgd 
    167  1.1      cgd 			cur_off = ftell(fp);
    168  1.1      cgd 			if (fseek(fp, vers_off, SEEK_SET) == -1)
    169  1.1      cgd 				badfmt("corrupted string table");
    170  1.1      cgd 
    171  1.1      cgd 			/*
    172  1.1      cgd 			 * Read version string up to, and including newline.
    173  1.1      cgd 			 * This code assumes that a newline terminates the
    174  1.1      cgd 			 * version line.
    175  1.1      cgd 			 */
    176  1.1      cgd 			if (fgets(buf, sizeof(buf), fp) == NULL)
    177  1.1      cgd 				badfmt("corrupted string table");
    178  1.1      cgd 
    179  1.1      cgd 			key.data = (u_char *)VRS_KEY;
    180  1.1      cgd 			key.size = sizeof(VRS_KEY) - 1;
    181  1.1      cgd 			data.data = (u_char *)buf;
    182  1.1      cgd 			data.size = strlen(buf);
    183  1.1      cgd 			if ((db->put)(db, &key, &data, 0))
    184  1.1      cgd 				error("put");
    185  1.1      cgd 
    186  1.1      cgd 			/* Restore to original values. */
    187  1.1      cgd 			data.data = (u_char *)&nbuf;
    188  1.1      cgd 			data.size = sizeof(NLIST);
    189  1.1      cgd 			if (fseek(fp, cur_off, SEEK_SET) == -1)
    190  1.1      cgd 				badfmt("corrupted string table");
    191  1.1      cgd 		}
    192  1.1      cgd 	}
    193  1.1      cgd 	(void)fclose(fp);
    194  1.1      cgd }
    195  1.1      cgd 
    196  1.1      cgd badread(nr, p)
    197  1.1      cgd 	int nr;
    198  1.1      cgd 	char *p;
    199  1.1      cgd {
    200  1.1      cgd 	if (nr < 0)
    201  1.1      cgd 		error(kfile);
    202  1.1      cgd 	badfmt(p);
    203  1.1      cgd }
    204  1.1      cgd 
    205  1.1      cgd badfmt(p)
    206  1.1      cgd 	char *p;
    207  1.1      cgd {
    208  1.1      cgd 	(void)fprintf(stderr,
    209  1.3  mycroft 	    "kvm_mkdb: %s: %s: %s\n", kfile, p, strerror(EFTYPE));
    210  1.1      cgd 	exit(1);
    211  1.1      cgd }
    212