Subject: Adding a start-of-data marker to loadelf
To: None <tech-kern@NetBSD.org>
From: Martin Husemann <martin@duskware.de>
List: tech-kern
Date: 06/04/2007 18:46:49
--5vNYLRcllDrimb99
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi folks,

I found myself in the need to figure out the final VA of the start of the
data segment after loading a kernel with loadfile. There seemed to be no
easy way ready to use, so I added a new marker, MARK_DATA, and filled it
with the first data address loaded.

It works for me, but I'm not sure if I didn't miss some other easy way
to get this info.

Martin

--5vNYLRcllDrimb99
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="loadelf.patch"

Index: loadfile.h
===================================================================
RCS file: /cvsroot/src/sys/lib/libsa/loadfile.h,v
retrieving revision 1.5
diff -u -p -b -B -r1.5 loadfile.h
--- loadfile.h	29 Apr 2003 13:03:55 -0000	1.5
+++ loadfile.h	4 Jun 2007 16:42:29 -0000
@@ -44,7 +44,8 @@
 #define	MARK_NSYM	2
 #define MARK_SYM	3
 #define	MARK_END	4
-#define	MARK_MAX	5
+#define	MARK_DATA	5
+#define	MARK_MAX	6
 
 /*
  * Bit flags for sections to load
Index: loadfile_aout.c
===================================================================
RCS file: /cvsroot/src/sys/lib/libsa/loadfile_aout.c,v
retrieving revision 1.8
diff -u -p -b -B -r1.8 loadfile_aout.c
--- loadfile_aout.c	11 Dec 2005 12:24:46 -0000	1.8
+++ loadfile_aout.c	4 Jun 2007 16:42:29 -0000
@@ -179,6 +179,7 @@ loadfile_aout(fd, x, marks, flags)
 	if (flags & LOAD_DATA) {
 		PROGRESS(("+%ld", x->a_data));
 
+		marks[MARK_DATA] = LOADADDR(maxp);
 		if (READ(fd, maxp, x->a_data) != (ssize_t)x->a_data) {
 			WARN(("read data"));
 			return 1;
Index: loadfile_ecoff.c
===================================================================
RCS file: /cvsroot/src/sys/lib/libsa/loadfile_ecoff.c,v
retrieving revision 1.6
diff -u -p -b -B -r1.6 loadfile_ecoff.c
--- loadfile_ecoff.c	11 Dec 2005 12:24:46 -0000	1.6
+++ loadfile_ecoff.c	4 Jun 2007 16:42:29 -0000
@@ -141,6 +141,7 @@ loadfile_coff(fd, coff, marks, flags)
 	marks[MARK_NSYM] = 1;	/* XXX: Kernel needs >= 0 */
 	marks[MARK_SYM] = LOADADDR(maxp);
 	marks[MARK_END] = LOADADDR(maxp);
+	marks[MARK_DATA] = LOADADDR(coff->a.data);
 	return 0;
 }
 
Index: loadfile_elf32.c
===================================================================
RCS file: /cvsroot/src/sys/lib/libsa/loadfile_elf32.c,v
retrieving revision 1.16
diff -u -p -b -B -r1.16 loadfile_elf32.c
--- loadfile_elf32.c	6 Apr 2006 09:25:58 -0000	1.16
+++ loadfile_elf32.c	4 Jun 2007 16:42:29 -0000
@@ -318,6 +318,9 @@ ELFNAMEEND(loadfile)(fd, elf, marks, fla
 		    (IS_DATA(phdr[i]) && (flags & LOAD_DATA))) {
 
 		loadseg:
+			if (marks[MARK_DATA] == 0 && IS_DATA(phdr[i]))
+				marks[MARK_DATA] = LOADADDR(phdr[i].p_vaddr);
+
 			/* Read in segment. */
 			PROGRESS(("%s%lu", first ? "" : "+",
 			    (u_long)phdr[i].p_filesz));

--5vNYLRcllDrimb99--