386BSD 0.0 development
authorWilliam F. Jolitz <wjolitz@soda.berkeley.edu>
Sat, 27 Apr 1991 23:23:58 +0000 (15:23 -0800)
committerWilliam F. Jolitz <wjolitz@soda.berkeley.edu>
Sat, 27 Apr 1991 23:23:58 +0000 (15:23 -0800)
Work on file usr/src/usr.sbin/kvm_mkdb/nlist.c
Work on file usr/src/usr.sbin/kvm_mkdb/kvm_mkdb.c

Co-Authored-By: Lynne Greer Jolitz <ljolitz@cardio.ucsf.edu>
Synthesized-from: 386BSD-0.0/src

usr/src/usr.sbin/kvm_mkdb/kvm_mkdb.c [new file with mode: 0644]
usr/src/usr.sbin/kvm_mkdb/nlist.c [new file with mode: 0644]

diff --git a/usr/src/usr.sbin/kvm_mkdb/kvm_mkdb.c b/usr/src/usr.sbin/kvm_mkdb/kvm_mkdb.c
new file mode 100644 (file)
index 0000000..ff6fa1c
--- /dev/null
@@ -0,0 +1,114 @@
+/*-
+ * Copyright (c) 1990 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)kvm_mkdb.c 5.11 (Berkeley) 4/27/91";
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <db.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <paths.h>
+
+char *tmp;
+#define basename(cp)   ((tmp=rindex((cp), '/')) ? tmp+1 : (cp))
+
+main(argc, argv)
+       int argc;
+       char **argv;
+{
+       extern int optind;
+       DB *db;
+       int ch;
+       char *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
+
+       while ((ch = getopt(argc, argv, "")) != EOF)
+               switch((char)ch) {
+               case '?':
+               default:
+                       usage();
+               }
+       argc -= optind;
+       argv += optind;
+
+       nlistpath = argc > 1 ? argv[0] : _PATH_UNIX;
+       nlistname = basename(nlistpath);
+
+       (void)sprintf(dbtemp, "%s/kvm_%s.tmp", _PATH_VARRUN, nlistname);
+       (void)sprintf(dbname, "%s/kvm_%s.db", _PATH_VARRUN, nlistname);
+       (void)umask(0);
+       db = hash_open(dbtemp, O_CREAT|O_WRONLY|O_EXCL,
+           S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, NULL);
+       if (!db) {
+               (void)fprintf(stderr,
+                   "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
+               exit(1);
+       }
+       create_knlist(nlistpath, db);
+       (void)(db->close)(db);
+       if (rename(dbtemp, dbname)) {
+               (void)fprintf(stderr, "kvm_mkdb: %s to %s: %s.\n",
+                   dbtemp, dbname, strerror(errno));
+               exit(1);
+       }
+       exit(0);
+}
+
+error(n)
+       char *n;
+{
+       int sverr;
+
+       sverr = errno;
+       (void)fprintf(stderr, "kvm_mkdb: ");
+       if (n)
+               (void)fprintf(stderr, "%s: ", n);
+       (void)fprintf(stderr, "%s\n", strerror(sverr));
+       exit(1);
+}
+
+usage()
+{
+       (void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
+       exit(1);
+}
diff --git a/usr/src/usr.sbin/kvm_mkdb/nlist.c b/usr/src/usr.sbin/kvm_mkdb/nlist.c
new file mode 100644 (file)
index 0000000..36eac49
--- /dev/null
@@ -0,0 +1,192 @@
+/*-
+ * Copyright (c) 1990 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)nlist.c    5.4 (Berkeley) 4/27/91";
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <a.out.h>
+#include <db.h>
+#include <errno.h>
+#include <unistd.h>
+#include <kvm.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+typedef struct nlist NLIST;
+#define        _strx   n_un.n_strx
+#define        _name   n_un.n_name
+
+static char *kfile;
+
+create_knlist(name, db)
+       char *name;
+       DB *db;
+{
+       register int nsyms;
+       struct exec ebuf;
+       FILE *fp;
+       NLIST nbuf;
+       DBT data, key;
+       int fd, nr, strsize;
+       char *strtab, buf[1024];
+
+       kfile = name;
+       if ((fd = open(name, O_RDONLY, 0)) < 0)
+               error(name);
+
+       /* Read in exec structure. */
+       nr = read(fd, (char *)&ebuf, sizeof(struct exec));
+       if (nr != sizeof(struct exec))
+               badfmt(nr, "no exec header");
+
+       /* Check magic number and symbol count. */
+       if (N_BADMAG(ebuf))
+               badfmt("bad magic number");
+       if (!ebuf.a_syms)
+               badfmt("stripped");
+
+       /* Seek to string table. */
+       if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1)
+               badfmt("corrupted string table");
+
+       /* Read in the size of the symbol table. */
+       nr = read(fd, (char *)&strsize, sizeof(strsize));
+       if (nr != sizeof(strsize))
+               badread(nr, "no symbol table");
+
+       /* Read in the string table. */
+       strsize -= sizeof(strsize);
+       if (!(strtab = (char *)malloc(strsize)))
+               error(name);
+       if ((nr = read(fd, strtab, strsize)) != strsize)
+               badread(nr, "corrupted symbol table");
+
+       /* Seek to symbol table. */
+       if (!(fp = fdopen(fd, "r")))
+               error(name);
+       if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1)
+               error(name);
+       
+       data.data = (u_char *)&nbuf;
+       data.size = sizeof(NLIST);
+
+       /* Read each symbol and enter it into the database. */
+       nsyms = ebuf.a_syms / sizeof(struct nlist);
+       while (nsyms--) {
+               if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) {
+                       if (feof(fp))
+                               badfmt("corrupted symbol table");
+                       error(name);
+               }
+               if (!nbuf._strx || nbuf.n_type&N_STAB)
+                       continue;
+
+               key.data = (u_char *)strtab + nbuf._strx - sizeof(long);
+               key.size = strlen((char *)key.data);
+               if ((db->put)(db, &key, &data, 0))
+                       error("put");
+
+               if (!strncmp((char *)key.data, VRS_SYM, sizeof(VRS_SYM) - 1)) {
+                       off_t cur_off, rel_off, vers_off;
+
+                       /* Offset relative to start of text image in VM. */
+#ifdef hp300
+                       rel_off = nbuf.n_value;
+#endif
+#ifdef tahoe
+                       /*
+                        * On tahoe, first 0x800 is reserved for communication
+                        * with the console processor.
+                        */
+                       rel_off = ((nbuf.n_value & ~KERNBASE) - 0x800);
+#endif
+#ifdef vax
+                       rel_off = nbuf.n_value & ~KERNBASE;
+#endif
+                       /*
+                        * When loaded, data is rounded to next page cluster
+                        * after text, but not in file.
+                        */
+                       rel_off -= CLBYTES - (ebuf.a_text % CLBYTES);
+                       vers_off = N_TXTOFF(ebuf) + rel_off;
+
+                       cur_off = ftell(fp);
+                       if (fseek(fp, vers_off, SEEK_SET) == -1)
+                               badfmt("corrupted string table");
+
+                       /*
+                        * Read version string up to, and including newline.
+                        * This code assumes that a newline terminates the
+                        * version line.
+                        */
+                       if (fgets(buf, sizeof(buf), fp) == NULL)
+                               badfmt("corrupted string table");
+
+                       key.data = (u_char *)VRS_KEY;
+                       key.size = sizeof(VRS_KEY) - 1;
+                       data.data = (u_char *)buf;
+                       data.size = strlen(buf);
+                       if ((db->put)(db, &key, &data, 0))
+                               error("put");
+
+                       /* Restore to original values. */
+                       data.data = (u_char *)&nbuf;
+                       data.size = sizeof(NLIST);
+                       if (fseek(fp, cur_off, SEEK_SET) == -1)
+                               badfmt("corrupted string table");
+               }
+       }
+       (void)fclose(fp);
+}
+
+badread(nr, p)
+       int nr;
+       char *p;
+{
+       if (nr < 0)
+               error(kfile);
+       badfmt(p);
+}
+
+badfmt(p)
+       char *p;
+{
+       (void)fprintf(stderr,
+           "symorder: %s: %s: %s\n", kfile, p, strerror(EFTYPE));
+       exit(1);
+}