xref: /original-bsd/usr.sbin/kvm_mkdb/nlist.c (revision e59fb703)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)nlist.c	5.5 (Berkeley) 09/12/91";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <fcntl.h>
14 #include <limits.h>
15 #include <a.out.h>
16 #include <db.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include <kvm.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 
24 typedef struct nlist NLIST;
25 #define	_strx	n_un.n_strx
26 #define	_name	n_un.n_name
27 
28 static char *kfile;
29 
30 create_knlist(name, db)
31 	char *name;
32 	DB *db;
33 {
34 	register int nsyms;
35 	struct exec ebuf;
36 	FILE *fp;
37 	NLIST nbuf;
38 	DBT data, key;
39 	int fd, nr, strsize;
40 	char *strtab, buf[1024];
41 
42 	kfile = name;
43 	if ((fd = open(name, O_RDONLY, 0)) < 0)
44 		error(name);
45 
46 	/* Read in exec structure. */
47 	nr = read(fd, (char *)&ebuf, sizeof(struct exec));
48 	if (nr != sizeof(struct exec))
49 		badfmt(nr, "no exec header");
50 
51 	/* Check magic number and symbol count. */
52 	if (N_BADMAG(ebuf))
53 		badfmt("bad magic number");
54 	if (!ebuf.a_syms)
55 		badfmt("stripped");
56 
57 	/* Seek to string table. */
58 	if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1)
59 		badfmt("corrupted string table");
60 
61 	/* Read in the size of the symbol table. */
62 	nr = read(fd, (char *)&strsize, sizeof(strsize));
63 	if (nr != sizeof(strsize))
64 		badread(nr, "no symbol table");
65 
66 	/* Read in the string table. */
67 	strsize -= sizeof(strsize);
68 	if (!(strtab = (char *)malloc(strsize)))
69 		error(name);
70 	if ((nr = read(fd, strtab, strsize)) != strsize)
71 		badread(nr, "corrupted symbol table");
72 
73 	/* Seek to symbol table. */
74 	if (!(fp = fdopen(fd, "r")))
75 		error(name);
76 	if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1)
77 		error(name);
78 
79 	data.data = (u_char *)&nbuf;
80 	data.size = sizeof(NLIST);
81 
82 	/* Read each symbol and enter it into the database. */
83 	nsyms = ebuf.a_syms / sizeof(struct nlist);
84 	while (nsyms--) {
85 		if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) {
86 			if (feof(fp))
87 				badfmt("corrupted symbol table");
88 			error(name);
89 		}
90 		if (!nbuf._strx || nbuf.n_type&N_STAB)
91 			continue;
92 
93 		key.data = (u_char *)strtab + nbuf._strx - sizeof(long);
94 		key.size = strlen((char *)key.data);
95 		if ((db->put)(db, &key, &data, 0))
96 			error("put");
97 
98 		if (!strncmp((char *)key.data, VRS_SYM, sizeof(VRS_SYM) - 1)) {
99 			off_t cur_off, rel_off, vers_off;
100 
101 			/* Offset relative to start of text image in VM. */
102 			rel_off = nbuf.n_value & ~KERNBASE;
103 #ifdef tahoe
104 			/*
105 			 * On tahoe, first 0x800 is reserved for communication
106 			 * with the console processor.
107 			 */
108 			rel_off -= 0x800;
109 #endif
110 			/*
111 			 * When loaded, data is rounded to next page cluster
112 			 * after text, but not in file.
113 			 */
114 			rel_off -= CLBYTES - (ebuf.a_text % CLBYTES);
115 			vers_off = N_TXTOFF(ebuf) + rel_off;
116 
117 			cur_off = ftell(fp);
118 			if (fseek(fp, vers_off, SEEK_SET) == -1)
119 				badfmt("corrupted string table");
120 
121 			/*
122 			 * Read version string up to, and including newline.
123 			 * This code assumes that a newline terminates the
124 			 * version line.
125 			 */
126 			if (fgets(buf, sizeof(buf), fp) == NULL)
127 				badfmt("corrupted string table");
128 
129 			key.data = (u_char *)VRS_KEY;
130 			key.size = sizeof(VRS_KEY) - 1;
131 			data.data = (u_char *)buf;
132 			data.size = strlen(buf);
133 			if ((db->put)(db, &key, &data, 0))
134 				error("put");
135 
136 			/* Restore to original values. */
137 			data.data = (u_char *)&nbuf;
138 			data.size = sizeof(NLIST);
139 			if (fseek(fp, cur_off, SEEK_SET) == -1)
140 				badfmt("corrupted string table");
141 		}
142 	}
143 	(void)fclose(fp);
144 }
145 
146 badread(nr, p)
147 	int nr;
148 	char *p;
149 {
150 	if (nr < 0)
151 		error(kfile);
152 	badfmt(p);
153 }
154 
155 badfmt(p)
156 	char *p;
157 {
158 	(void)fprintf(stderr,
159 	    "kvm_mkdb: %s: %s: %s\n", kfile, p, strerror(EFTYPE));
160 	exit(1);
161 }
162