Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 14 Jun 1998 14:08:37 +0200 (MDT)
From:      Martin Kammerhofer <mkamm@icg.tu-graz.ac.at>
To:        freebsd-small@FreeBSD.ORG
Cc:        Martin Kammerhofer <dada@sbox.tu-graz.ac.at>
Subject:   kernel symbols (was: netstat is not working)
Message-ID:  <Pine.SGI.3.91.980614140132.27223A-100000@fcggsg17.icg.tu-graz.ac.at>

next in thread | raw e-mail | index | archive | help
On Sat, 13 Jun 1998, Ekaterina N. Ivannikova wrote:

> Ok, it seems the way to go would be to make dumpnlist dump addresses 
> netstat needs, then change it to read the file "symbols".
> 

Before PicoBSD started I did something very similar, i.e.
created a few customized FreeBSD bootfloppies and a build system.
Since PicoBSD wasn't out there then, I implemented some things in 
a different way.
  One thing I didn't copy from FreeBSD's ``release'' source tree is that
ugly ``symbols'' hack. I devised a way to avoid this duplication of
functionality and will present it here in the hope it might be useful for
PicoBSD developers. It must be said though, that my approach would need 
reworking for a ELF kernel.
  By looking at symorder(1)'s manpage you'll notice that there's an
option -t to exclude all unwanted symbols from the kernel. Voila!
All that's needed is a (small) list of kernel symbols needed by
some programs (ps, {net,io,sys,f,p}stat to name the most popular ones),
strip everything but those symbols from the kernel image and install
the resulting symlist (1325 bytes on my machine) as /kernel in your MFS.
  Now programs that require kernel symbols are working without any
fiddling with their sources, they are using standard nlist(3).

  Andrzej or Dinesh, if you want to take a closer look at it, a updated
sys/i386/i386/symbols.raw file can be found in PR kern/3287, a patch
to make symorder's -t option work is in PR bin/3284 and finally a patch
(against the -STABLE sources) that enables strip(1) to strip of
everything else but the symbol table follows:

Index: strip.c
===================================================================
RCS file: /home/dada/cvsroot/src/strip.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -K -w -r1.7 -r1.8
--- strip.c	1998/06/14 08:09:08	1.7
+++ strip.c	1998/06/14 08:55:52	1.8
@@ -66,9 +66,18 @@
 
 void s_stab __P((const char *, int, EXEC *));
 void s_sym __P((const char *, int, EXEC *));
+#ifdef WITH_KRUNCH
+void s_krunch __P((const char *, int, EXEC *));
+#include <assert.h>
+#endif /* WITH_KRUNCH */
 static void usage __P((void));
 
 int xflag = 0;
+int kflag = 0;
+int oflag = 0;
+int nflag = 0;
+int verbose = 0;
+int dont = 0;
 int err_val = 0;
 
 int
@@ -83,7 +92,7 @@
 	char *fn;
 
 	sfcn = s_sym;
-	while ((ch = getopt(argc, argv, "dx")) != -1)
+	while ((ch = getopt(argc, argv, "dxKvnON")) != -1)
 		switch(ch) {
                 case 'x':
                         xflag = 1;
@@ -91,6 +100,30 @@
 		case 'd':
 			sfcn = s_stab;
 			break;
+		case 'K':
+#ifdef WITH_KRUNCH
+			sfcn = s_krunch;
+			kflag = 1;
+#else
+			errx(1, "%s: option -K not compiled in\n", *argv);
+#endif /* WITH_KRUNCH */
+			break;
+#ifdef WITH_KRUNCH
+		case 'n':
+			dont = 1;
+			/*FALLTHROUGH*/
+		case 'v':
+			verbose++;
+			break;
+		case 'O':
+			oflag=1;
+			nflag=0;
+			break;
+		case 'N':
+			nflag=1;
+			oflag=0;
+			break;
+#endif /* WITH_KRUNCH */
 		case '?':
 		default:
 			usage();
@@ -258,9 +291,108 @@
 	munmap((caddr_t)ep, (size_t)sb.st_size);
 }
 
+#ifdef WITH_KRUNCH
+/*
+ *	remove everything but the header and symbol table
+ */
+void
+s_krunch(fn, fd, ep)
+	const char *fn;
+	int fd;
+	EXEC *ep;
+{
+	char *symbase, *stringbase;
+	char *nsymbase, *nstringbase;
+	unsigned long symlen, stringlen;
+	struct stat sb;
+
+	/* Quit if no symbols. */
+	if (ep->a_syms == 0)
+		return;
+
+	/* Stat the file. */
+	if (fstat(fd, &sb) < 0) {
+		warn("%s", fn);
+		err_val = 1;
+		return;
+	}
+
+	/* Check size. */
+	if (sb.st_size > SIZE_T_MAX) {
+		warnx("%s: %s", fn, strerror(EFBIG));
+		err_val = 1;
+		return;
+	}
+
+	/* Map the file. */
+	if ((ep = (EXEC *)mmap(NULL, (size_t)sb.st_size,
+	    PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t)0)) == (EXEC *)-1) {
+		warn("%s", fn);
+		err_val = 1;
+		return;
+	}
+
+	symbase = ((char *)ep + N_SYMOFF(*ep));
+	stringbase = ((char *)ep + N_STROFF(*ep));
+	symlen = ep->a_syms;
+	stringlen = *((unsigned long *)stringbase);
+
+	if (verbose) {
+		printf("symbase = 0x%x, \tstringbase = 0x%x\n",
+			symbase-(char *)ep, stringbase-(char *)ep);
+		printf("symlen = 0x%x, \tstringlen = 0x%x\n", symlen, stringlen);
+		printf("symbase = %lu, \tstringbase = %lu\n",
+			symbase-(char *)ep, stringbase-(char *)ep);
+		printf("symlen = %lu, \tstringlen = %lu\n\n", symlen, stringlen);
+	}
+	if (dont)
+		goto done;
+
+	/* check if we change to OMAGIC or NMAGIC formats */
+	if (oflag)
+		N_SETMAGIC(*ep,OMAGIC,N_GETMID(*ep),N_GETFLAG(*ep));
+	else if (nflag)
+		N_SETMAGIC(*ep,NMAGIC,N_GETMID(*ep),N_GETFLAG(*ep));
+	/* remove text, data, bss, and relocations */
+	ep->a_text = ep->a_data = ep->a_bss = ep->a_trsize = ep->a_drsize = 0;
+	/* set entry point to a invalid address */
+	ep->a_entry = -1;
+	/* Now check if we got the symboltable at zero */
+	/* This can happen with page aligned [QZ]MAGIC formats */
+	nsymbase = ((char *)ep + N_SYMOFF(*ep));
+	if (N_SYMOFF(*ep) < sizeof(EXEC)) {
+		ep->a_text = sizeof(EXEC);
+		nsymbase = ((char *)ep + N_SYMOFF(*ep));
+	}
+	nstringbase = ((char *)ep + N_STROFF(*ep));
+
+	if (verbose) {
+		printf("nsymbase = 0x%x, \tnstringbase = 0x%x\n",
+			nsymbase-(char *)ep, nstringbase-(char *)ep);
+		printf("nsymbase = %lu, \tnstringbase = %lu\n",
+			nsymbase-(char *)ep, nstringbase-(char *)ep);
+	}
+	assert(nsymbase >= (char *)ep + sizeof(EXEC));
+	bcopy(symbase, nsymbase, symlen);
+	bcopy(stringbase, nstringbase, stringlen);
+
+	/* Truncate to the current length. */
+	if (ftruncate(fd, nstringbase + stringlen - (char *)ep)) {
+		warn("%s", fn);
+		err_val = 1;
+	}
+done:
+	munmap((caddr_t)ep, (size_t)sb.st_size);
+}
+#endif /* WITH_KRUNCH */
+
 static void
 usage()
 {
+#ifdef WITH_KRUNCH
+	(void)fprintf(stderr, "usage: strip ( [-dx] | [-Kvn[O|N]] ) file ...\n");
+#else
 	(void)fprintf(stderr, "usage: strip [-dx] file ...\n");
+#endif
 	exit(1);
 }


--- /usr/src/usr.bin/strip/strip.1	Thu Dec 25 00:58:07 1997
+++ ./strip.1	Tue Mar  4 15:47:43 1997
@@ -39,13 +39,18 @@
 .Nm strip
 .Nd remove unnecessary information from executable files
 .Sh SYNOPSIS
-.Nm
+.Nm strip
 .Op Fl d
 .Op Fl x
-.Ar
+.Op Fl K
+.Op Fl v
+.Op Fl n
+.Op Fl O
+.Op Fl N
+.Ar file ...
 .Sh DESCRIPTION
 The
-.Nm
+.Nm strip
 utility
 deletes the relocation information and symbol table used by
 assemblers, loaders and debuggers.
@@ -57,7 +62,17 @@
 .It Fl d
 Delete only debugging and empty symbols.
 .It Fl x
-Delete only debugging, compiler identification, and local symbols.
+Delete only debugging, compiler identification, and local symbols. (Implies -d).
+.It Fl K
+Delete everything but the exec header, symbol- and stringtables.  This destroys the Text, Data, Bss and Relocation sections.  This option is incompatible with options -d and -x. The last one specified takes effect.
+.It Fl v
+Report file offsets and sizes of the symbol- and stringtable.
+.It Fl n
+Don't strip, report only. This option implies -v.
+.It Fl N
+Convert to NMAGIC.
+.It Fl O
+Convert to OMAGIC. Options -vnNO are ignored without -K.
 .El
 .Pp
 .Nm Strip


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-small" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.SGI.3.91.980614140132.27223A-100000>