Subject: kern/791: modload doesn't load a.out symbols
To: None <gnats-admin@NetBSD.ORG>
From: John Kohl <jtk@kolvir.blrc.ma.us>
List: netbsd-bugs
Date: 02/08/1995 19:35:03
>Number:         791
>Category:       kern
>Synopsis:       modload doesn't load a.out symbols
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    kern-bug-people (Kernel Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Wed Feb  8 19:35:01 1995
>Originator:     John Kohl
>Organization:
NetBSD Kernel Hackers `R` Us
>Release:        1.0-current (7 Feb 1995)
>Environment:
	
System: NetBSD kolvir 1.0A NetBSD 1.0A (KOLVIR) #29: Wed Feb 8 20:12:07 EST 1995 jtk@kolvir:/u1/NetBSD-current/src/sys/arch/i386/compile/KOLVIR i386


>Description:
modload doesn't install kernel symbols where ddb can read them.

>How-To-Repeat:
modload something, try to use its symbols with ddb, and fail.

>Fix:
These diffs have the feature that they're both forward and backward
compatible--v2 modules can be loaded on v1 systems, and v1 modules can
be loaded on a v2 system.  [Neither cross-revision case supports loading
symbols, though]

===================================================================
RCS file: RCS/modload.8,v
retrieving revision 1.1
diff -ubw -r1.1 src/sbin/modload/modload.8
--- 1.1	1994/11/20 14:54:57
+++ modload.8	1994/11/20 14:55:20
@@ -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:54:57 jtkohl Exp $
+.\"	$Id: modload.8,v 1.2 1994/11/20 14:55:17 jtkohl 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: RCS/modload.c,v
retrieving revision 1.1
diff -ubw -r1.1 src/sbin/modload/modload.c
--- 1.1	1994/11/20 02:03:35
+++ modload.c	1994/11/24 03:57:04
@@ -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 02:03:35 jtkohl Exp $
+ *	$Id: modload.c,v 1.4 1994/11/24 03:57:02 jtkohl Exp $
  */
 
 #include <stdio.h>
@@ -42,10 +42,14 @@
 #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"
 
+#define TRUE 1
+#define FALSE 0
+
 #ifndef DFLT_ENTRY
 #define	DFLT_ENTRY	"xxxinit"
 #endif	/* !DFLT_ENTRY */
@@ -65,6 +69,7 @@
 
 int debug = 0;
 int verbose = 0;
+int symtab = 1;
 
 int
 linkcmd(kernel, entry, outfile, address, object)
@@ -152,14 +157,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;
+	int sz, bytesleft, old = FALSE;
 	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 +188,9 @@
 		case 'o':
 			out = optarg;
 			break;	/* output file */
+		case 's':
+			symtab = 0;
+			break;
 		case '?':
 			usage();
 		default:
@@ -234,6 +246,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,8 +277,25 @@
 	resrv.name = modout;	/* objname w/o ".o" */
 	resrv.slot = -1;	/* returned */
 	resrv.addr = 0;		/* returned */
-	if (ioctl(devfd, LMRESERV, &resrv) == -1)
+	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) {
+	    if (symtab)
+		warn("not loading symbols: kernel does not support symbol table loading");
+	doold:
+	    symtab = 0;
+	    if (ioctl(devfd, LMRESERV_O, &resrv) == -1)
 		err(9, "can't reserve memory");
+	    old = TRUE;
+	}
 	fileopen |= PART_RESRV;
 
 	/*
@@ -311,6 +345,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...
 	 */
@@ -322,9 +419,19 @@
 	 * is maintained on success, or blow everything back to ground
 	 * zero on failure.
 	 */
-	if (ioctl(devfd, LMREADY, &modentry) == -1)
+	if (ioctl(devfd, LMREADY, &modentry) == -1) {
+	  if (errno == EINVAL && !old) {
+	    if (fileopen & MOD_OPEN)
+	      close(modfd);
+	    /* PART_RESRV is not true since the kernel cleans up
+	       after a failed LMREADY */
+	    fileopen &= ~(MOD_OPEN|PART_RESRV);
+	    /* try using oldstyle */
+	    warn("module failed to load using new version; trying old version");
+	    goto doold;
+	  } else
 		err(14, "error initializing module");
-
+	}
 	/*
 	 * Success!
 	 */

===================================================================
RCS file: kern/RCS/kern_lkm.c,v
retrieving revision 1.1
diff -ubw -r1.1 src/sys/kern/kern_lkm.c
--- 1.1	1994/11/20 02:02:30
+++ kern/kern_lkm.c	1994/12/06 05:02:52
@@ -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;
 }
 
@@ -187,6 +200,7 @@
 
 	switch(cmd) {
 	case LMRESERV:		/* reserve pages for a module */
+	case LMRESERV_O:	/* reserve pages for a module */
 		if ((flag & FWRITE) == 0) /* only allow this if writing */
 			return EPERM;
 
@@ -204,6 +218,7 @@
 		}
 		curp = &lkmods[i];
 		curp->id = i;		/* self reference slot offset */
+		curp->ver = (cmd == LMRESERV) ? LKM_VERSION : LKM_OLDVERSION;
 
 		resrvp->slot = i;		/* return slot */
 
@@ -218,8 +233,22 @@
 
 		resrvp->addr = curp->area; /* ret kernel addr */
 
+		if (cmd == LMRESERV && 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;
+			if (cmd == LMRESERV)
+			    resrvp->sym_addr = 0;
+		}
 #ifdef DEBUG
 		printf("LKM: LMRESERV (actual   = 0x%08x)\n", curp->area);
+		printf("LKM: LMRESERV (syms     = 0x%08x)\n", curp->syms);
 		printf("LKM: LMRESERV (adjusted = 0x%08x)\n",
 			trunc_page(curp->area));
 #endif	/* DEBUG */
@@ -251,7 +280,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 +288,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 +333,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 +343,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,8 +359,11 @@
 
 		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)) {
+		if (err = (*(curp->entry))(curp, LKM_E_LOAD, curp->ver)) {
 			/*
 			 * Module may refuse loading or may have a
 			 * version mismatch...
@@ -307,6 +378,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;
 
@@ -352,7 +430,7 @@
 		}
 
 		/* call entry(unload) */
-		if ((*(curp->entry))(curp, LKM_E_UNLOAD, LKM_VERSION)) {
+		if ((*(curp->entry))(curp, LKM_E_UNLOAD, curp->ver)) {
 			err = EBUSY;
 			break;
 		}
===================================================================
RCS file: sys/RCS/lkm.h,v
retrieving revision 1.1
diff -ubw -r1.1 src/sys/sys/lkm.h
--- 1.1	1994/11/20 02:02:57
+++ sys/lkm.h	1994/11/30 01:49:34
@@ -53,7 +53,8 @@
 } MODTYPE;
 
 
-#define	LKM_VERSION	1		/* version of module loader */
+#define	LKM_VERSION	2		/* version of module loader */
+#define	LKM_OLDVERSION	1
 #define	MAXLKMNAME	32
 
 /****************************************************************************/
@@ -198,6 +199,11 @@
 
 	int	(*entry)();	/* entry function */
 	union lkm_generic	private;	/* module private data */
+	/* LKM v2 stuff: */
+	u_long	sym_size;		/* size of symtab+strings */
+	u_long	sym_symsize;		/* size of symbol table entry part */
+	u_long	sym_offset;
+	u_long	syms;
 };
 
 
@@ -262,8 +268,15 @@
  * corresponding entry instance.  "cmd" is passed to each function so
  * that a single function can be used if desired.
  */
+/*
+ * Note that something compiled with this header file (v2) can also be loaded
+ * by a v1 loader...the lkm_table structs have a common prefix, and
+ * the module itself never looks at the stuff added by v2.  The
+ * lkm_syscall/vfs/... structs are the same size, and the lkm code
+ * doesn't care about the embedded version # there either.
+ */
 #define	DISPATCH(lkmtp,cmd,ver,load,unload,stat)			\
-	if (ver != LKM_VERSION)						\
+	if (ver != LKM_VERSION && ver != LKM_OLDVERSION)		\
 		return EINVAL;	/* version mismatch */			\
 	switch (cmd) {							\
 	int	error;							\
@@ -294,6 +307,9 @@
 #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	LMRESERV_O	_IOWR('K', 0, struct _old_lmc_resrv)
 
 #define	LMLOAD		_IOW('K', 9, struct lmc_load)
 #define	LMUNLOAD	_IOWR('K', 10, struct lmc_unload)
@@ -310,6 +326,16 @@
  * Reserve a page-aligned block of kernel memory for the module
  */
 struct lmc_resrv {
+	u_long	size;		/* IN: size of module to reserve */
+	char	*name;		/* IN: name (must be provided */
+	int	slot;		/* OUT: allocated slot (module ID) */
+	u_long	addr;		/* OUT: Link-to address */
+	u_long	sym_size;	/* IN: size of symbol table + strtable */
+	u_long	sym_symsize;	/* IN: size of symbol table itself */
+	u_long	sym_addr;	/* OUT: Symbol table address */
+};
+
+struct _old_lmc_resrv {
 	u_long	size;		/* IN: size of module to reserve */
 	char	*name;		/* IN: name (must be provided */
 	int	slot;		/* OUT: allocated slot (module ID) */



>Audit-Trail:
>Unformatted: