Home | History | Annotate | Line # | Download | only in kvm_mkdb
nlist.c revision 1.5
      1 /*-
      2  * Copyright (c) 1990 The Regents of the University of California.
      3  * 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  * PATCHES MAGIC                LEVEL   PATCH THAT GOT US HERE
     34  * --------------------         -----   ----------------------
     35  * CURRENT PATCH LEVEL:         1       00032
     36  * --------------------         -----   ----------------------
     37  *
     38  * 05 Aug 92	David Greenman		Fix kernel namelist db create/use
     39  */
     40 
     41 #ifndef lint
     42 /*static char sccsid[] = "from: @(#)nlist.c	5.4 (Berkeley) 4/27/91";*/
     43 static char rcsid[] = "$Id: nlist.c,v 1.5 1993/08/01 17:59:19 mycroft Exp $";
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <fcntl.h>
     48 #include <limits.h>
     49 #include <a.out.h>
     50 #include <db.h>
     51 #include <errno.h>
     52 #include <unistd.h>
     53 #include <kvm.h>
     54 #include <stdio.h>
     55 #include <string.h>
     56 #include <stdlib.h>
     57 
     58 typedef struct nlist NLIST;
     59 #define	_strx	n_un.n_strx
     60 #define	_name	n_un.n_name
     61 
     62 static char *kfile;
     63 
     64 create_knlist(name, db)
     65 	char *name;
     66 	DB *db;
     67 {
     68 	register int nsyms;
     69 	struct exec ebuf;
     70 	FILE *fp;
     71 	NLIST nbuf;
     72 	DBT data, key;
     73 	int fd, nr, strsize;
     74 	char *strtab, buf[1024];
     75 
     76 	kfile = name;
     77 	if ((fd = open(name, O_RDONLY, 0)) < 0)
     78 		error(name);
     79 
     80 	/* Read in exec structure. */
     81 	nr = read(fd, (char *)&ebuf, sizeof(struct exec));
     82 	if (nr != sizeof(struct exec))
     83 		badfmt(nr, "no exec header");
     84 
     85 	/* Check magic number and symbol count. */
     86 	if (N_BADMAG(ebuf))
     87 		badfmt("bad magic number");
     88 	if (!ebuf.a_syms)
     89 		badfmt("stripped");
     90 
     91 	/* Seek to string table. */
     92 	if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1)
     93 		badfmt("corrupted string table");
     94 
     95 	/* Read in the size of the symbol table. */
     96 	nr = read(fd, (char *)&strsize, sizeof(strsize));
     97 	if (nr != sizeof(strsize))
     98 		badread(nr, "no symbol table");
     99 
    100 	/* Read in the string table. */
    101 	strsize -= sizeof(strsize);
    102 	if (!(strtab = (char *)malloc(strsize)))
    103 		error(name);
    104 	if ((nr = read(fd, strtab, strsize)) != strsize)
    105 		badread(nr, "corrupted symbol table");
    106 
    107 	/* Seek to symbol table. */
    108 	if (!(fp = fdopen(fd, "r")))
    109 		error(name);
    110 	if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1)
    111 		error(name);
    112 
    113 	data.data = (u_char *)&nbuf;
    114 	data.size = sizeof(NLIST);
    115 
    116 	/* Read each symbol and enter it into the database. */
    117 	nsyms = ebuf.a_syms / sizeof(struct nlist);
    118 	while (nsyms--) {
    119 		if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) {
    120 			if (feof(fp))
    121 				badfmt("corrupted symbol table");
    122 			error(name);
    123 		}
    124 		if (!nbuf._strx || nbuf.n_type&N_STAB)
    125 			continue;
    126 
    127 		key.data = (u_char *)strtab + nbuf._strx - sizeof(long);
    128 		key.size = strlen((char *)key.data);
    129 		if ((db->put)(db, &key, &data, 0))
    130 			error("put");
    131 
    132 		if (!strncmp((char *)key.data, VRS_SYM, sizeof(VRS_SYM) - 1)) {
    133 			off_t cur_off, rel_off, vers_off;
    134 
    135 			/* Offset relative to start of text image in VM. */
    136 #ifdef hp300
    137 			rel_off = nbuf.n_value;
    138 #endif
    139 #ifdef tahoe
    140 			/*
    141 			 * On tahoe, first 0x800 is reserved for communication
    142 			 * with the console processor.
    143 			 */
    144 			rel_off = ((nbuf.n_value & ~KERNBASE) - 0x800);
    145 #endif
    146 #ifdef vax
    147 			rel_off = nbuf.n_value & ~KERNBASE;
    148 #endif
    149 #ifdef i386
    150 			/*
    151 			 * XXX: This is a KLUGE to handle the kernel being
    152 			 * loaded at a different address than KERNBASE.  Stupid
    153 			 * a.out format has no way of recording the text
    154 			 * address we gave ld.  It only works for multiples of
    155 			 * 1MB.
    156 			 */
    157 			rel_off = ((nbuf.n_value - (ebuf.a_entry & -0x100000))
    158 				  + CLBYTES);
    159 #endif
    160 			/*
    161 			 * When loaded, data is rounded to next page cluster
    162 			 * after text, but not in file.
    163 			 */
    164 			rel_off -= CLBYTES - (ebuf.a_text % CLBYTES);
    165 			vers_off = N_TXTOFF(ebuf) + rel_off;
    166 
    167 			cur_off = ftell(fp);
    168 			if (fseek(fp, vers_off, SEEK_SET) == -1)
    169 				badfmt("corrupted string table");
    170 
    171 			/*
    172 			 * Read version string up to, and including newline.
    173 			 * This code assumes that a newline terminates the
    174 			 * version line.
    175 			 */
    176 			if (fgets(buf, sizeof(buf), fp) == NULL)
    177 				badfmt("corrupted string table");
    178 
    179 			key.data = (u_char *)VRS_KEY;
    180 			key.size = sizeof(VRS_KEY) - 1;
    181 			data.data = (u_char *)buf;
    182 			data.size = strlen(buf);
    183 			if ((db->put)(db, &key, &data, 0))
    184 				error("put");
    185 
    186 			/* Restore to original values. */
    187 			data.data = (u_char *)&nbuf;
    188 			data.size = sizeof(NLIST);
    189 			if (fseek(fp, cur_off, SEEK_SET) == -1)
    190 				badfmt("corrupted string table");
    191 		}
    192 	}
    193 	(void)fclose(fp);
    194 }
    195 
    196 badread(nr, p)
    197 	int nr;
    198 	char *p;
    199 {
    200 	if (nr < 0)
    201 		error(kfile);
    202 	badfmt(p);
    203 }
    204 
    205 badfmt(p)
    206 	char *p;
    207 {
    208 	(void)fprintf(stderr,
    209 	    "kvm_mkdb: %s: %s: %s\n", kfile, p, strerror(EFTYPE));
    210 	exit(1);
    211 }
    212