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