Commit | Line | Data |
---|---|---|
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 | |
4fec1dea | 9 | static char sccsid[] = "@(#)nlist.c 5.4 (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 | |
24 | typedef struct nlist NLIST; | |
25 | #define _strx n_un.n_strx | |
26 | #define _name n_un.n_name | |
27 | ||
436c24c2 KB |
28 | static char *kfile; |
29 | ||
30 | create_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 KB |
101 | /* Offset relative to start of text image in VM. */ |
102 | #ifdef hp300 | |
103 | rel_off = nbuf.n_value; | |
104 | #endif | |
105 | #ifdef tahoe | |
106 | /* | |
107 | * On tahoe, first 0x800 is reserved for communication | |
108 | * with the console processor. | |
109 | */ | |
110 | rel_off = ((nbuf.n_value & ~KERNBASE) - 0x800); | |
111 | #endif | |
112 | #ifdef vax | |
113 | rel_off = nbuf.n_value & ~KERNBASE; | |
114 | #endif | |
115 | /* | |
116 | * When loaded, data is rounded to next page cluster | |
117 | * after text, but not in file. | |
118 | */ | |
119 | rel_off -= CLBYTES - (ebuf.a_text % CLBYTES); | |
120 | vers_off = N_TXTOFF(ebuf) + rel_off; | |
3b06e4c9 KB |
121 | |
122 | cur_off = ftell(fp); | |
123 | if (fseek(fp, vers_off, SEEK_SET) == -1) | |
436c24c2 KB |
124 | badfmt("corrupted string table"); |
125 | ||
126 | /* | |
127 | * Read version string up to, and including newline. | |
128 | * This code assumes that a newline terminates the | |
129 | * version line. | |
130 | */ | |
3b06e4c9 | 131 | if (fgets(buf, sizeof(buf), fp) == NULL) |
436c24c2 KB |
132 | badfmt("corrupted string table"); |
133 | ||
573b8d71 KB |
134 | key.data = (u_char *)VRS_KEY; |
135 | key.size = sizeof(VRS_KEY) - 1; | |
136 | data.data = (u_char *)buf; | |
137 | data.size = strlen(buf); | |
138 | if ((db->put)(db, &key, &data, 0)) | |
139 | error("put"); | |
436c24c2 KB |
140 | |
141 | /* Restore to original values. */ | |
573b8d71 KB |
142 | data.data = (u_char *)&nbuf; |
143 | data.size = sizeof(NLIST); | |
3b06e4c9 KB |
144 | if (fseek(fp, cur_off, SEEK_SET) == -1) |
145 | badfmt("corrupted string table"); | |
436c24c2 KB |
146 | } |
147 | } | |
3b06e4c9 KB |
148 | (void)fclose(fp); |
149 | } | |
150 | ||
151 | badread(nr, p) | |
152 | int nr; | |
153 | char *p; | |
154 | { | |
155 | if (nr < 0) | |
156 | error(kfile); | |
157 | badfmt(p); | |
436c24c2 KB |
158 | } |
159 | ||
160 | badfmt(p) | |
3b06e4c9 | 161 | char *p; |
436c24c2 KB |
162 | { |
163 | (void)fprintf(stderr, | |
164 | "symorder: %s: %s: %s\n", kfile, p, strerror(EFTYPE)); | |
165 | exit(1); | |
166 | } |