Commit | Line | Data |
---|---|---|
436c24c2 | 1 | /*- |
6bf0e47e KB |
2 | * Copyright (c) 1990, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
436c24c2 KB |
4 | * |
5 | * %sccs.include.redist.c% | |
6 | */ | |
7 | ||
8 | #ifndef lint | |
6bf0e47e | 9 | static char sccsid[] = "@(#)nlist.c 8.1 (Berkeley) %G%"; |
436c24c2 KB |
10 | #endif /* not lint */ |
11 | ||
12 | #include <sys/param.h> | |
6e74e80d | 13 | |
436c24c2 | 14 | #include <a.out.h> |
573b8d71 | 15 | #include <db.h> |
6e74e80d | 16 | #include <err.h> |
3b06e4c9 | 17 | #include <errno.h> |
6e74e80d | 18 | #include <fcntl.h> |
3b06e4c9 | 19 | #include <kvm.h> |
6e74e80d | 20 | #include <limits.h> |
436c24c2 | 21 | #include <stdio.h> |
3b06e4c9 | 22 | #include <stdlib.h> |
6e74e80d KB |
23 | #include <string.h> |
24 | #include <unistd.h> | |
436c24c2 | 25 | |
13e7db0b KB |
26 | #include "extern.h" |
27 | ||
436c24c2 KB |
28 | typedef struct nlist NLIST; |
29 | #define _strx n_un.n_strx | |
30 | #define _name n_un.n_name | |
31 | ||
6e74e80d KB |
32 | #define badfmt(str) errx(1, "%s: %s: %s", kfile, str, strerror(EFTYPE)) |
33 | ||
13e7db0b | 34 | static void badread __P((int, char *)); |
13e7db0b | 35 | |
436c24c2 KB |
36 | static char *kfile; |
37 | ||
13e7db0b | 38 | void |
436c24c2 KB |
39 | create_knlist(name, db) |
40 | char *name; | |
573b8d71 | 41 | DB *db; |
436c24c2 | 42 | { |
3b06e4c9 | 43 | register int nsyms; |
436c24c2 | 44 | struct exec ebuf; |
3b06e4c9 | 45 | FILE *fp; |
436c24c2 | 46 | NLIST nbuf; |
573b8d71 | 47 | DBT data, key; |
3b06e4c9 KB |
48 | int fd, nr, strsize; |
49 | char *strtab, buf[1024]; | |
436c24c2 | 50 | |
436c24c2 | 51 | kfile = name; |
3b06e4c9 | 52 | if ((fd = open(name, O_RDONLY, 0)) < 0) |
6e74e80d | 53 | err(1, "%s", name); |
436c24c2 | 54 | |
3b06e4c9 | 55 | /* Read in exec structure. */ |
6e74e80d | 56 | nr = read(fd, &ebuf, sizeof(struct exec)); |
3b06e4c9 | 57 | if (nr != sizeof(struct exec)) |
13e7db0b | 58 | badfmt("no exec header"); |
3b06e4c9 KB |
59 | |
60 | /* Check magic number and symbol count. */ | |
436c24c2 KB |
61 | if (N_BADMAG(ebuf)) |
62 | badfmt("bad magic number"); | |
3b06e4c9 | 63 | if (!ebuf.a_syms) |
436c24c2 KB |
64 | badfmt("stripped"); |
65 | ||
3b06e4c9 KB |
66 | /* Seek to string table. */ |
67 | if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1) | |
68 | badfmt("corrupted string table"); | |
69 | ||
70 | /* Read in the size of the symbol table. */ | |
71 | nr = read(fd, (char *)&strsize, sizeof(strsize)); | |
72 | if (nr != sizeof(strsize)) | |
73 | badread(nr, "no symbol table"); | |
436c24c2 | 74 | |
3b06e4c9 KB |
75 | /* Read in the string table. */ |
76 | strsize -= sizeof(strsize); | |
6e74e80d KB |
77 | if (!(strtab = malloc(strsize))) |
78 | err(1, NULL); | |
3b06e4c9 KB |
79 | if ((nr = read(fd, strtab, strsize)) != strsize) |
80 | badread(nr, "corrupted symbol table"); | |
436c24c2 | 81 | |
3b06e4c9 KB |
82 | /* Seek to symbol table. */ |
83 | if (!(fp = fdopen(fd, "r"))) | |
6e74e80d | 84 | err(1, "%s", name); |
3b06e4c9 | 85 | if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1) |
6e74e80d | 86 | err(1, "%s", name); |
3b06e4c9 | 87 | |
573b8d71 KB |
88 | data.data = (u_char *)&nbuf; |
89 | data.size = sizeof(NLIST); | |
436c24c2 | 90 | |
3b06e4c9 KB |
91 | /* Read each symbol and enter it into the database. */ |
92 | nsyms = ebuf.a_syms / sizeof(struct nlist); | |
93 | while (nsyms--) { | |
94 | if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) { | |
95 | if (feof(fp)) | |
96 | badfmt("corrupted symbol table"); | |
6e74e80d | 97 | err(1, "%s", name); |
436c24c2 | 98 | } |
3b06e4c9 | 99 | if (!nbuf._strx || nbuf.n_type&N_STAB) |
436c24c2 KB |
100 | continue; |
101 | ||
573b8d71 KB |
102 | key.data = (u_char *)strtab + nbuf._strx - sizeof(long); |
103 | key.size = strlen((char *)key.data); | |
6e74e80d KB |
104 | if (db->put(db, &key, &data, 0)) |
105 | err(1, "record enter"); | |
436c24c2 | 106 | |
13e7db0b | 107 | if (strcmp((char *)key.data, VRS_SYM) == 0) { |
40492f4a | 108 | long cur_off, voff; |
13e7db0b KB |
109 | #ifndef KERNTEXTOFF |
110 | #define KERNTEXTOFF KERNBASE | |
436c24c2 KB |
111 | #endif |
112 | /* | |
13e7db0b KB |
113 | * Calculate offset relative to a normal (non-kernel) |
114 | * a.out. KERNTEXTOFF is where the kernel is really | |
115 | * loaded; N_TXTADDR is where a normal file is loaded. | |
116 | * From there, locate file offset in text or data. | |
436c24c2 | 117 | */ |
13e7db0b KB |
118 | voff = nbuf.n_value - KERNTEXTOFF + N_TXTADDR(ebuf); |
119 | if ((nbuf.n_type & N_TYPE) == N_TEXT) | |
120 | voff += N_TXTOFF(ebuf) - N_TXTADDR(ebuf); | |
121 | else | |
122 | voff += N_DATOFF(ebuf) - N_DATADDR(ebuf); | |
3b06e4c9 | 123 | cur_off = ftell(fp); |
13e7db0b | 124 | if (fseek(fp, voff, 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); | |
6e74e80d KB |
139 | if (db->put(db, &key, &data, 0)) |
140 | err(1, "record enter"); | |
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 | ||
13e7db0b | 152 | static void |
3b06e4c9 KB |
153 | badread(nr, p) |
154 | int nr; | |
155 | char *p; | |
156 | { | |
157 | if (nr < 0) | |
6e74e80d | 158 | err(1, "%s", kfile); |
3b06e4c9 | 159 | badfmt(p); |
436c24c2 | 160 | } |