too many args is a usage error
[unix-history] / usr / src / usr.sbin / kvm_mkdb / nlist.c
CommitLineData
436c24c2
KB
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
217aa1fa 9static char sccsid[] = "@(#)nlist.c 5.5 (Berkeley) %G%";
436c24c2
KB
10#endif /* not lint */
11
12#include <sys/param.h>
3b06e4c9 13#include <fcntl.h>
436c24c2 14#include <limits.h>
436c24c2 15#include <a.out.h>
573b8d71 16#include <db.h>
3b06e4c9 17#include <errno.h>
436c24c2 18#include <unistd.h>
3b06e4c9 19#include <kvm.h>
436c24c2 20#include <stdio.h>
3b06e4c9
KB
21#include <string.h>
22#include <stdlib.h>
436c24c2
KB
23
24typedef struct nlist NLIST;
25#define _strx n_un.n_strx
26#define _name n_un.n_name
27
436c24c2
KB
28static char *kfile;
29
30create_knlist(name, db)
31 char *name;
573b8d71 32 DB *db;
436c24c2 33{
3b06e4c9 34 register int nsyms;
436c24c2 35 struct exec ebuf;
3b06e4c9 36 FILE *fp;
436c24c2 37 NLIST nbuf;
573b8d71 38 DBT data, key;
3b06e4c9
KB
39 int fd, nr, strsize;
40 char *strtab, buf[1024];
436c24c2 41
436c24c2 42 kfile = name;
3b06e4c9 43 if ((fd = open(name, O_RDONLY, 0)) < 0)
436c24c2
KB
44 error(name);
45
3b06e4c9
KB
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. */
436c24c2
KB
52 if (N_BADMAG(ebuf))
53 badfmt("bad magic number");
3b06e4c9 54 if (!ebuf.a_syms)
436c24c2
KB
55 badfmt("stripped");
56
3b06e4c9
KB
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");
436c24c2 65
3b06e4c9
KB
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");
436c24c2 72
3b06e4c9
KB
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
573b8d71
KB
79 data.data = (u_char *)&nbuf;
80 data.size = sizeof(NLIST);
436c24c2 81
3b06e4c9
KB
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);
436c24c2 89 }
3b06e4c9 90 if (!nbuf._strx || nbuf.n_type&N_STAB)
436c24c2
KB
91 continue;
92
573b8d71
KB
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");
436c24c2 97
573b8d71 98 if (!strncmp((char *)key.data, VRS_SYM, sizeof(VRS_SYM) - 1)) {
3b06e4c9
KB
99 off_t cur_off, rel_off, vers_off;
100
436c24c2 101 /* Offset relative to start of text image in VM. */
217aa1fa 102 rel_off = nbuf.n_value & ~KERNBASE;
436c24c2
KB
103#ifdef tahoe
104 /*
105 * On tahoe, first 0x800 is reserved for communication
106 * with the console processor.
107 */
217aa1fa 108 rel_off -= 0x800;
436c24c2
KB
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;
3b06e4c9
KB
116
117 cur_off = ftell(fp);
118 if (fseek(fp, vers_off, SEEK_SET) == -1)
436c24c2
KB
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 */
3b06e4c9 126 if (fgets(buf, sizeof(buf), fp) == NULL)
436c24c2
KB
127 badfmt("corrupted string table");
128
573b8d71
KB
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");
436c24c2
KB
135
136 /* Restore to original values. */
573b8d71
KB
137 data.data = (u_char *)&nbuf;
138 data.size = sizeof(NLIST);
3b06e4c9
KB
139 if (fseek(fp, cur_off, SEEK_SET) == -1)
140 badfmt("corrupted string table");
436c24c2
KB
141 }
142 }
3b06e4c9
KB
143 (void)fclose(fp);
144}
145
146badread(nr, p)
147 int nr;
148 char *p;
149{
150 if (nr < 0)
151 error(kfile);
152 badfmt(p);
436c24c2
KB
153}
154
155badfmt(p)
3b06e4c9 156 char *p;
436c24c2
KB
157{
158 (void)fprintf(stderr,
217aa1fa 159 "kvm_mkdb: %s: %s: %s\n", kfile, p, strerror(EFTYPE));
436c24c2
KB
160 exit(1);
161}