doesn't need user.h anymore
[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
573b8d71 9static char sccsid[] = "@(#)nlist.c 5.3 (Berkeley) %G%";
436c24c2
KB
10#endif /* not lint */
11
12#include <sys/param.h>
573b8d71 13#include <sys/user.h>
3b06e4c9 14#include <fcntl.h>
436c24c2 15#include <limits.h>
436c24c2 16#include <a.out.h>
573b8d71 17#include <db.h>
3b06e4c9 18#include <errno.h>
436c24c2 19#include <unistd.h>
3b06e4c9 20#include <kvm.h>
436c24c2 21#include <stdio.h>
3b06e4c9
KB
22#include <string.h>
23#include <stdlib.h>
436c24c2
KB
24
25typedef struct nlist NLIST;
26#define _strx n_un.n_strx
27#define _name n_un.n_name
28
436c24c2
KB
29static char *kfile;
30
31create_knlist(name, db)
32 char *name;
573b8d71 33 DB *db;
436c24c2 34{
3b06e4c9 35 register int nsyms;
436c24c2 36 struct exec ebuf;
3b06e4c9 37 FILE *fp;
436c24c2 38 NLIST nbuf;
573b8d71 39 DBT data, key;
3b06e4c9
KB
40 int fd, nr, strsize;
41 char *strtab, buf[1024];
436c24c2 42
436c24c2 43 kfile = name;
3b06e4c9 44 if ((fd = open(name, O_RDONLY, 0)) < 0)
436c24c2
KB
45 error(name);
46
3b06e4c9
KB
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. */
436c24c2
KB
53 if (N_BADMAG(ebuf))
54 badfmt("bad magic number");
3b06e4c9 55 if (!ebuf.a_syms)
436c24c2
KB
56 badfmt("stripped");
57
3b06e4c9
KB
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");
436c24c2 66
3b06e4c9
KB
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");
436c24c2 73
3b06e4c9
KB
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
573b8d71
KB
80 data.data = (u_char *)&nbuf;
81 data.size = sizeof(NLIST);
436c24c2 82
3b06e4c9
KB
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);
436c24c2 90 }
3b06e4c9 91 if (!nbuf._strx || nbuf.n_type&N_STAB)
436c24c2
KB
92 continue;
93
573b8d71
KB
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");
436c24c2 98
573b8d71 99 if (!strncmp((char *)key.data, VRS_SYM, sizeof(VRS_SYM) - 1)) {
3b06e4c9
KB
100 off_t cur_off, rel_off, vers_off;
101
436c24c2
KB
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;
3b06e4c9
KB
122
123 cur_off = ftell(fp);
124 if (fseek(fp, vers_off, SEEK_SET) == -1)
436c24c2
KB
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 */
3b06e4c9 132 if (fgets(buf, sizeof(buf), fp) == NULL)
436c24c2
KB
133 badfmt("corrupted string table");
134
573b8d71
KB
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");
436c24c2
KB
141
142 /* Restore to original values. */
573b8d71
KB
143 data.data = (u_char *)&nbuf;
144 data.size = sizeof(NLIST);
3b06e4c9
KB
145 if (fseek(fp, cur_off, SEEK_SET) == -1)
146 badfmt("corrupted string table");
436c24c2
KB
147 }
148 }
3b06e4c9
KB
149 (void)fclose(fp);
150}
151
152badread(nr, p)
153 int nr;
154 char *p;
155{
156 if (nr < 0)
157 error(kfile);
158 badfmt(p);
436c24c2
KB
159}
160
161badfmt(p)
3b06e4c9 162 char *p;
436c24c2
KB
163{
164 (void)fprintf(stderr,
165 "symorder: %s: %s: %s\n", kfile, p, strerror(EFTYPE));
166 exit(1);
167}