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