BSD 4_4 release
[unix-history] / usr / src / usr.sbin / kvm_mkdb / nlist.c
CommitLineData
436c24c2 1/*-
ad787160
C
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
436c24c2 4 *
ad787160
C
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
436c24c2
KB
32 */
33
34#ifndef lint
ad787160 35static char sccsid[] = "@(#)nlist.c 8.1 (Berkeley) 6/6/93";
436c24c2
KB
36#endif /* not lint */
37
38#include <sys/param.h>
6e74e80d 39
436c24c2 40#include <a.out.h>
573b8d71 41#include <db.h>
6e74e80d 42#include <err.h>
3b06e4c9 43#include <errno.h>
6e74e80d 44#include <fcntl.h>
3b06e4c9 45#include <kvm.h>
6e74e80d 46#include <limits.h>
436c24c2 47#include <stdio.h>
3b06e4c9 48#include <stdlib.h>
6e74e80d
KB
49#include <string.h>
50#include <unistd.h>
436c24c2 51
13e7db0b
KB
52#include "extern.h"
53
436c24c2
KB
54typedef struct nlist NLIST;
55#define _strx n_un.n_strx
56#define _name n_un.n_name
57
6e74e80d
KB
58#define badfmt(str) errx(1, "%s: %s: %s", kfile, str, strerror(EFTYPE))
59
13e7db0b 60static void badread __P((int, char *));
13e7db0b 61
436c24c2
KB
62static char *kfile;
63
13e7db0b 64void
436c24c2
KB
65create_knlist(name, db)
66 char *name;
573b8d71 67 DB *db;
436c24c2 68{
3b06e4c9 69 register int nsyms;
436c24c2 70 struct exec ebuf;
3b06e4c9 71 FILE *fp;
436c24c2 72 NLIST nbuf;
573b8d71 73 DBT data, key;
3b06e4c9
KB
74 int fd, nr, strsize;
75 char *strtab, buf[1024];
436c24c2 76
436c24c2 77 kfile = name;
3b06e4c9 78 if ((fd = open(name, O_RDONLY, 0)) < 0)
6e74e80d 79 err(1, "%s", name);
436c24c2 80
3b06e4c9 81 /* Read in exec structure. */
6e74e80d 82 nr = read(fd, &ebuf, sizeof(struct exec));
3b06e4c9 83 if (nr != sizeof(struct exec))
13e7db0b 84 badfmt("no exec header");
3b06e4c9
KB
85
86 /* Check magic number and symbol count. */
436c24c2
KB
87 if (N_BADMAG(ebuf))
88 badfmt("bad magic number");
3b06e4c9 89 if (!ebuf.a_syms)
436c24c2
KB
90 badfmt("stripped");
91
3b06e4c9
KB
92 /* Seek to string table. */
93 if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1)
94 badfmt("corrupted string table");
95
96 /* Read in the size of the symbol table. */
97 nr = read(fd, (char *)&strsize, sizeof(strsize));
98 if (nr != sizeof(strsize))
99 badread(nr, "no symbol table");
436c24c2 100
3b06e4c9
KB
101 /* Read in the string table. */
102 strsize -= sizeof(strsize);
6e74e80d
KB
103 if (!(strtab = malloc(strsize)))
104 err(1, NULL);
3b06e4c9
KB
105 if ((nr = read(fd, strtab, strsize)) != strsize)
106 badread(nr, "corrupted symbol table");
436c24c2 107
3b06e4c9
KB
108 /* Seek to symbol table. */
109 if (!(fp = fdopen(fd, "r")))
6e74e80d 110 err(1, "%s", name);
3b06e4c9 111 if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1)
6e74e80d 112 err(1, "%s", name);
3b06e4c9 113
573b8d71
KB
114 data.data = (u_char *)&nbuf;
115 data.size = sizeof(NLIST);
436c24c2 116
3b06e4c9
KB
117 /* Read each symbol and enter it into the database. */
118 nsyms = ebuf.a_syms / sizeof(struct nlist);
119 while (nsyms--) {
120 if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) {
121 if (feof(fp))
122 badfmt("corrupted symbol table");
6e74e80d 123 err(1, "%s", name);
436c24c2 124 }
3b06e4c9 125 if (!nbuf._strx || nbuf.n_type&N_STAB)
436c24c2
KB
126 continue;
127
573b8d71
KB
128 key.data = (u_char *)strtab + nbuf._strx - sizeof(long);
129 key.size = strlen((char *)key.data);
6e74e80d
KB
130 if (db->put(db, &key, &data, 0))
131 err(1, "record enter");
436c24c2 132
13e7db0b 133 if (strcmp((char *)key.data, VRS_SYM) == 0) {
40492f4a 134 long cur_off, voff;
13e7db0b
KB
135#ifndef KERNTEXTOFF
136#define KERNTEXTOFF KERNBASE
436c24c2
KB
137#endif
138 /*
13e7db0b
KB
139 * Calculate offset relative to a normal (non-kernel)
140 * a.out. KERNTEXTOFF is where the kernel is really
141 * loaded; N_TXTADDR is where a normal file is loaded.
142 * From there, locate file offset in text or data.
436c24c2 143 */
13e7db0b
KB
144 voff = nbuf.n_value - KERNTEXTOFF + N_TXTADDR(ebuf);
145 if ((nbuf.n_type & N_TYPE) == N_TEXT)
146 voff += N_TXTOFF(ebuf) - N_TXTADDR(ebuf);
147 else
148 voff += N_DATOFF(ebuf) - N_DATADDR(ebuf);
3b06e4c9 149 cur_off = ftell(fp);
13e7db0b 150 if (fseek(fp, voff, SEEK_SET) == -1)
436c24c2
KB
151 badfmt("corrupted string table");
152
153 /*
154 * Read version string up to, and including newline.
155 * This code assumes that a newline terminates the
156 * version line.
157 */
3b06e4c9 158 if (fgets(buf, sizeof(buf), fp) == NULL)
436c24c2
KB
159 badfmt("corrupted string table");
160
573b8d71
KB
161 key.data = (u_char *)VRS_KEY;
162 key.size = sizeof(VRS_KEY) - 1;
163 data.data = (u_char *)buf;
164 data.size = strlen(buf);
6e74e80d
KB
165 if (db->put(db, &key, &data, 0))
166 err(1, "record enter");
436c24c2
KB
167
168 /* Restore to original values. */
573b8d71
KB
169 data.data = (u_char *)&nbuf;
170 data.size = sizeof(NLIST);
3b06e4c9
KB
171 if (fseek(fp, cur_off, SEEK_SET) == -1)
172 badfmt("corrupted string table");
436c24c2
KB
173 }
174 }
3b06e4c9
KB
175 (void)fclose(fp);
176}
177
13e7db0b 178static void
3b06e4c9
KB
179badread(nr, p)
180 int nr;
181 char *p;
182{
183 if (nr < 0)
6e74e80d 184 err(1, "%s", kfile);
3b06e4c9 185 badfmt(p);
436c24c2 186}