xref: /original-bsd/usr.sbin/kvm_mkdb/nlist.c (revision 2ca53284)
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.2 (Berkeley) 02/12/91";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <fcntl.h>
14 #include <limits.h>
15 #include <ndbm.h>
16 #include <a.out.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 	DBM *db;
33 {
34 	register int nsyms;
35 	struct exec ebuf;
36 	FILE *fp;
37 	NLIST nbuf;
38 	datum key, data;
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.dptr = (char *)&nbuf;
80 	data.dsize = 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.dptr = strtab + nbuf._strx - sizeof(long);
94 		key.dsize = strlen(key.dptr);
95 		if (dbm_store(db, key, data, DBM_INSERT) < 0)
96 			error("dbm_store");
97 
98 		if (!strncmp(key.dptr, 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 #ifdef hp300
103 			rel_off = nbuf.n_value;
104 #endif
105 #ifdef tahoe
106 			/*
107 			 * On tahoe, first 0x800 is reserved for communication
108 			 * with the console processor.
109 			 */
110 			rel_off = ((nbuf.n_value & ~KERNBASE) - 0x800);
111 #endif
112 #ifdef vax
113 			rel_off = nbuf.n_value & ~KERNBASE;
114 #endif
115 			/*
116 			 * When loaded, data is rounded to next page cluster
117 			 * after text, but not in file.
118 			 */
119 			rel_off -= CLBYTES - (ebuf.a_text % CLBYTES);
120 			vers_off = N_TXTOFF(ebuf) + rel_off;
121 
122 			cur_off = ftell(fp);
123 			if (fseek(fp, vers_off, SEEK_SET) == -1)
124 				badfmt("corrupted string table");
125 
126 			/*
127 			 * Read version string up to, and including newline.
128 			 * This code assumes that a newline terminates the
129 			 * version line.
130 			 */
131 			if (fgets(buf, sizeof(buf), fp) == NULL)
132 				badfmt("corrupted string table");
133 
134 			key.dptr = VRS_KEY;
135 			key.dsize = sizeof(VRS_KEY) - 1;
136 			data.dptr = buf;
137 			data.dsize = strlen(buf);
138 			if (dbm_store(db, key, data, DBM_INSERT) < 0)
139 				error("dbm_store");
140 
141 			/* Restore to original values. */
142 			data.dptr = (char *)&nbuf;
143 			data.dsize = sizeof(NLIST);
144 			if (fseek(fp, cur_off, SEEK_SET) == -1)
145 				badfmt("corrupted string table");
146 		}
147 	}
148 	(void)fclose(fp);
149 }
150 
151 badread(nr, p)
152 	int nr;
153 	char *p;
154 {
155 	if (nr < 0)
156 		error(kfile);
157 	badfmt(p);
158 }
159 
160 badfmt(p)
161 	char *p;
162 {
163 	(void)fprintf(stderr,
164 	    "symorder: %s: %s: %s\n", kfile, p, strerror(EFTYPE));
165 	exit(1);
166 }
167