Subject: lkm diffs for symbol table loading
To: None <current-users@netbsd.org>
From: John Kohl <jtk@kolvir.blrc.ma.us>
List: current-users
Date: 11/20/1994 10:58:04
Based on some stuff John Brezak sent me (thanks, John!), I put together
changes to modload and the lkm stuff to allow loading of symbol tables
into the kernel along with modules.  This vastly improves debugging
sessions with ddb.

I had to rev the LKM version number due to structure changes.

I'd still like to find a way to only add the new symbols from the module
instead of all the symbols of the kernel and the module combined.  This
requires some "symbol table arithmetic", which I haven't (yet) taken the
time to implement/figure out.  Perhaps there's some ld option that could
be used for this?

I suppose I could also have the kernel reject symbol table loading if
DDB is not enabled?  Suggestions as to whether this is reasonable?

==John

===================================================================
RCS file: sys/sys/RCS/lkm.h,v
retrieving revision 1.1
diff -u -r1.1 sys/sys/lkm.h
--- 1.1	1994/11/19 23:34:51
+++ sys/sys/lkm.h	1994/11/20 13:55:46
@@ -53,7 +53,7 @@
 } MODTYPE;
 
 
-#define	LKM_VERSION	1		/* version of module loader */
+#define	LKM_VERSION	2		/* version of module loader */
 #define	MAXLKMNAME	32
 
 /****************************************************************************/
@@ -188,7 +188,11 @@
 	int	type;
 	u_long	size;
 	u_long	offset;
+	u_long	sym_size;		/* size of symtab+strings */
+	u_long	sym_symsize;		/* size of symbol table entry part */
+	u_long	sym_offset;
 	u_long	area;
+	u_long	syms;
 	char	used;
 
 	int	ver;		/* version (INIT) */
@@ -294,6 +298,7 @@
 #define	LMLOADBUF	_IOW('K', 1, struct lmc_loadbuf)
 #define	LMUNRESRV	_IO('K', 2)
 #define	LMREADY		_IOW('K', 3, int)
+#define	LMLOADSYMS	_IOW('K', 4, struct lmc_loadbuf)
 
 #define	LMLOAD		_IOW('K', 9, struct lmc_load)
 #define	LMUNLOAD	_IOWR('K', 10, struct lmc_unload)
@@ -311,9 +316,12 @@
  */
 struct lmc_resrv {
 	u_long	size;		/* IN: size of module to reserve */
+	u_long	sym_size;	/* IN: size of symbol table + strtable */
+	u_long	sym_symsize;	/* IN: size of symbol table itself */
 	char	*name;		/* IN: name (must be provided */
 	int	slot;		/* OUT: allocated slot (module ID) */
 	u_long	addr;		/* OUT: Link-to address */
+	u_long	sym_addr;	/* OUT: Symbol table address */
 };
 
 
===================================================================
RCS file: sys/kern/RCS/kern_lkm.c,v
retrieving revision 1.1
diff -u -r1.1 sys/kern/kern_lkm.c
--- 1.1	1994/11/19 23:50:09
+++ sys/kern/kern_lkm.c	1994/11/20 15:23:34
@@ -54,6 +54,10 @@
 #include <sys/mount.h>
 #include <sys/exec.h>
 #include <sys/lkm.h>
+#ifdef DDB
+#include <machine/db_machdep.h>
+#include <ddb/db_sym.h>
+#endif
 
 #include <vm/vm.h>
 #include <vm/vm_param.h>
@@ -67,6 +71,7 @@
 #define	LKMS_IDLE	0x00
 #define	LKMS_RESERVED	0x01
 #define	LKMS_LOADING	0x02
+#define	LKMS_LOADING_SYMS	0x03
 #define	LKMS_LOADED	0x04
 #define	LKMS_UNLOADING	0x08
 
@@ -126,14 +131,22 @@
 	if (lkm_state == LKMS_IDLE)
 		return;
 
+#ifdef DDB
+	if (curp && curp->private.lkm_any && curp->private.lkm_any->lkm_name)
+	    db_del_symbol_table(curp->private.lkm_any->lkm_name);
+#endif
 	/*
 	 * Actually unreserve the memory
 	 */
 	if (curp && curp->area) {
 		kmem_free(kmem_map, curp->area, curp->size);/**/
 		curp->area = 0;
-	}
 
+	}
+	if (curp && curp->syms) {
+	    kmem_free( kmem_map, curp->syms, curp->sym_size);
+	    curp->syms = 0;
+	}
 	lkm_state = LKMS_IDLE;
 }
 
@@ -218,6 +231,18 @@
 
 		resrvp->addr = curp->area; /* ret kernel addr */
 
+		if (resrvp->sym_size) {
+			curp->sym_size = resrvp->sym_size;
+			curp->sym_symsize = resrvp->sym_symsize;
+			curp->syms = kmem_alloc( kmem_map, curp->sym_size);
+			curp->sym_offset = 0;
+			resrvp->sym_addr = curp->syms; /* ret symbol addr */
+		} else {
+			curp->sym_size = 0;
+			curp->syms = 0;
+			curp->sym_offset = 0;
+			resrvp->sym_addr = 0;
+		}
 #ifdef DEBUG
 		printf("LKM: LMRESERV (actual   = 0x%08x)\n", curp->area);
 		printf("LKM: LMRESERV (adjusted = 0x%08x)\n",
@@ -251,7 +276,7 @@
 			curp->offset, curp->size, i);
 #endif	/* DEBUG */
 		} else {
-			lkm_state = LKMS_LOADED;
+			lkm_state = LKMS_LOADING_SYMS;
 #ifdef DEBUG
 			printf("LKM: LMLOADBUF (loaded)\n");
 #endif	/* DEBUG */
@@ -259,6 +284,40 @@
 		curp->offset += i;
 		break;
 
+	case LMLOADSYMS:	/* Copy in; stateful, follows LMRESERV*/
+		if ((flag & FWRITE) == 0) /* only allow this if writing */
+			return EPERM;
+
+		loadbufp = (struct lmc_loadbuf *)data;
+		i = loadbufp->cnt;
+		if ((lkm_state != LKMS_LOADING &&
+		     lkm_state != LKMS_LOADING_SYMS)
+		    || i < 0
+		    || i > MODIOBUF
+		    || i > curp->sym_size - curp->sym_offset) {
+			err = ENOMEM;
+			break;
+		}
+
+		/* copy in buffer full of data*/
+		if (err = copyin((caddr_t)loadbufp->data, (caddr_t)curp->syms + curp->sym_offset, i))
+			break;
+
+		if ((curp->sym_offset + i) < curp->sym_size) {
+			lkm_state = LKMS_LOADING_SYMS;
+#ifdef DEBUG
+			printf( "LKM: LMLOADSYMS (loading @ %d of %d, i = %d)\n",
+			curp->sym_offset, curp->sym_size, i);
+#endif	/* DEBUG*/
+		} else {
+			lkm_state = LKMS_LOADED;
+#ifdef DEBUG
+			printf( "LKM: LMLOADSYMS (loaded)\n");
+#endif	/* DEBUG*/
+		}
+		curp->sym_offset += i;
+		break;
+
 	case LMUNRESRV:		/* discard reserved pages for a module */
 		if ((flag & FWRITE) == 0) /* only allow this if writing */
 			return EPERM;
@@ -270,6 +329,9 @@
 		break;
 
 	case LMREADY:		/* module loaded: call entry */
+#ifdef DEBUG
+	    printf("LKM: try READY");
+#endif	/* DEBUG */
 		if ((flag & FWRITE) == 0) /* only allow this if writing */
 			return EPERM;
 
@@ -277,6 +339,8 @@
 		case LKMS_LOADED:
 			break;
 		case LKMS_LOADING:
+		case LKMS_LOADING_SYMS:
+		    if (curp->size - curp->offset > 0)
 			/* The remainder must be bss, so we clear it */
 			bzero((caddr_t)curp->area + curp->offset,
 			      curp->size - curp->offset);
@@ -291,6 +355,9 @@
 
 		curp->entry = (int (*)()) (*((int *) (data)));
 
+#ifdef DEBUG
+		printf("LKM: call entrypoint %x\n", curp->entry);
+#endif
 		/* call entry(load)... (assigns "private" portion) */
 		if (err = (*(curp->entry))(curp, LKM_E_LOAD, LKM_VERSION)) {
 			/*
@@ -307,6 +374,13 @@
 #ifdef DEBUG
 		printf("LKM: LMREADY\n");
 #endif	/* DEBUG */
+#ifdef DDB
+		if (curp->syms && curp->sym_offset >= curp->sym_size)
+		    db_add_symbol_table(curp->syms,
+					curp->syms + curp->sym_symsize,
+					curp->private.lkm_any->lkm_name,
+					curp->syms);
+#endif
 		lkm_state = LKMS_IDLE;
 		break;
 
===================================================================
RCS file: sbin/modload/RCS/modload.8,v
retrieving revision 1.1
diff -u -r1.1 sbin/modload/modload.8
--- 1.1	1994/11/20 14:18:49
+++ sbin/modload/modload.8	1994/11/20 14:20:40
@@ -23,7 +23,7 @@
 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.\"	$Id: modload.8,v 1.1 1994/11/20 14:18:49 jtk Exp $
+.\"	$Id: modload.8,v 1.2 1994/11/20 14:20:39 jtk Exp $
 .\"
 .Dd June 7, 1993
 .Dt MODLOAD 8
@@ -33,7 +33,7 @@
 .Nd load a kernel module
 .Sh SYNOPSIS
 .Nm modload
-.Op Fl dv
+.Op Fl dvs
 .Op Fl A Ar kernel
 .Op Fl e Ar entry
 .Op Fl p Ar postinstall
@@ -55,6 +55,8 @@
 itself.
 .It Fl v
 Print comments about the loading process.
+.It Fl s
+Suppress loading of the symbol table.
 .It Fl A Ar kernel
 Specify the file that is passed to the linker
 to resolve module references to external symbols. 
===================================================================
RCS file: sbin/modload/RCS/modload.c,v
retrieving revision 1.1
diff -u -r1.1 sbin/modload/modload.c
--- 1.1	1994/11/20 00:41:41
+++ sbin/modload/modload.c	1994/11/20 15:34:20
@@ -29,7 +29,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- *	$Id: modload.c,v 1.1 1994/11/20 00:41:41 jtk Exp $
+ *	$Id: modload.c,v 1.3 1994/11/20 15:34:20 jtk Exp $
  */
 
 #include <stdio.h>
@@ -42,6 +42,7 @@
 #include <sys/conf.h>
 #include <sys/mount.h>
 #include <sys/lkm.h>
+#include <sys/stat.h>
 #include <sys/file.h>
 #include <sys/errno.h>
 #include "pathnames.h"
@@ -65,6 +66,7 @@
 
 int debug = 0;
 int verbose = 0;
+int symtab = 1;
 
 int
 linkcmd(kernel, entry, outfile, address, object)
@@ -152,14 +154,18 @@
 	char *modobj;
 	char modout[80], *p;
 	struct exec info_buf;
+	struct stat stb;
 	u_int modsize;	/* XXX */
 	u_int modentry;	/* XXX */
+	struct nlist nl, *nlp;
+	int strtablen, numsyms;
 
 	struct lmc_loadbuf ldbuf;
 	int sz, bytesleft;
 	char buf[MODIOBUF];
+	char *symbuf;
 
-	while ((c = getopt(argc, argv, "dvA:e:p:o:")) != EOF) {
+	while ((c = getopt(argc, argv, "dvsA:e:p:o:")) != EOF) {
 		switch (c) {
 		case 'd':
 			debug = 1;
@@ -179,6 +185,9 @@
 		case 'o':
 			out = optarg;
 			break;	/* output file */
+		case 's':
+			symtab = 0;
+			break;
 		case '?':
 			usage();
 		default:
@@ -234,6 +243,11 @@
 	 */
 	if (read(modfd, &info_buf, sizeof(struct exec)) == -1)
 		err(3, "read `%s'", out);
+	/*
+	 * stat for filesize to figure out string table size
+	 */
+	if (fstat(modfd, &stb) == -1)
+	    err(3, "fstat `%s'", out);
 
 	/*
 	 * Close the dummy module -- we have our sizing information.
@@ -260,6 +274,16 @@
 	resrv.name = modout;	/* objname w/o ".o" */
 	resrv.slot = -1;	/* returned */
 	resrv.addr = 0;		/* returned */
+	strtablen = stb.st_size - N_STROFF(info_buf);
+	if (symtab) {
+	    /* XXX TODO:  grovel through symbol table looking
+	       for just the symbol table stuff from the new module,
+	       and skip the stuff from the kernel. */
+	    resrv.sym_size = info_buf.a_syms + strtablen;
+	    resrv.sym_symsize = info_buf.a_syms;
+	} else
+	    resrv.sym_size = resrv.sym_symsize = 0;
+
 	if (ioctl(devfd, LMRESERV, &resrv) == -1)
 		err(9, "can't reserve memory");
 	fileopen |= PART_RESRV;
@@ -311,6 +335,69 @@
 			err(11, "error transferring buffer");
 	}
 
+
+	if (symtab) {
+	    /*
+	     * Seek to the symbol table to start loading it...
+	     */
+	    if (lseek(modfd, N_SYMOFF(info_buf), SEEK_SET) == -1)
+		err(12, "lseek");
+
+	    /*
+	     * Transfer the symbol table entries.  First, read them all in,
+	     * then adjust their string table pointers, then
+	     * copy in bulk.  Then copy the string table itself.
+	     */
+
+	    symbuf = malloc(info_buf.a_syms);
+	    if (symbuf == 0)
+		err(13, "malloc");
+
+	    if (read(modfd, symbuf, info_buf.a_syms) != info_buf.a_syms)
+		err(14, "read");
+	    numsyms = info_buf.a_syms / sizeof(struct nlist);
+	    for (nlp = (struct nlist *)symbuf; 
+		 (char *)nlp < symbuf + info_buf.a_syms;
+		 nlp++) {
+		register int strx;
+		strx = nlp->n_un.n_strx;
+		if (strx != 0) {
+		    /* If a valid name, set the name ptr to point at the
+		     * loaded address for the string in the string table.
+		     */
+		    if (strx > strtablen)
+			nlp->n_un.n_name = 0;
+		    else
+			nlp->n_un.n_name =
+			    (char *)(strx + resrv.sym_addr + info_buf.a_syms);
+		}
+	    }
+	    /*
+	     * we've fixed the symbol table entries, now load them
+	     */
+	    for (bytesleft = info_buf.a_syms;
+		 bytesleft > 0;
+		 bytesleft -= sz) {
+		sz = min(bytesleft, MODIOBUF);
+		ldbuf.cnt = sz;
+		ldbuf.data = symbuf;
+		if (ioctl(devfd, LMLOADSYMS, &ldbuf) == -1)
+		    err(11, "error transferring sym buffer");
+		symbuf += sz;
+	    }
+	    free(symbuf - info_buf.a_syms);
+	    /* and now read the string table and load it. */
+	    for (bytesleft = strtablen;
+		 bytesleft > 0;
+		 bytesleft -= sz) {
+		sz = min(bytesleft, MODIOBUF);
+		read(modfd, buf, sz);
+		ldbuf.cnt = sz;
+		ldbuf.data = buf;
+		if (ioctl(devfd, LMLOADSYMS, &ldbuf) == -1)
+		    err(11, "error transferring stringtable buffer");
+	    }
+	}
 	/*
 	 * Save ourselves before disaster (potentitally) strikes...
 	 */