Module Name:    src
Committed By:   martin
Date:           Wed Feb 10 21:54:47 UTC 2010

Modified Files:
        src/sys/lib/libsa: Makefile
Added Files:
        src/sys/lib/libsa: lookup_elf32.c lookup_elf64.c

Log Message:
Utility function to lookup a symbol value in an elf symbol table - allows,
for example, a bootloader to access symbols in the just loaded kernel
(or module).


To generate a diff of this commit:
cvs rdiff -u -r1.70 -r1.71 src/sys/lib/libsa/Makefile
cvs rdiff -u -r0 -r1.1 src/sys/lib/libsa/lookup_elf32.c \
    src/sys/lib/libsa/lookup_elf64.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/lib/libsa/Makefile
diff -u src/sys/lib/libsa/Makefile:1.70 src/sys/lib/libsa/Makefile:1.71
--- src/sys/lib/libsa/Makefile:1.70	Tue Aug 25 14:10:54 2009
+++ src/sys/lib/libsa/Makefile	Wed Feb 10 21:54:47 2010
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.70 2009/08/25 14:10:54 he Exp $
+#	$NetBSD: Makefile,v 1.71 2010/02/10 21:54:47 martin Exp $
 
 LIB=	sa
 NOPIC=	# defined
@@ -45,8 +45,8 @@
 .endif
 
 .if (${SA_USE_LOADFILE} == "yes")
-SRCS+=	loadfile.c loadfile_ecoff.c loadfile_elf32.c \
-	loadfile_elf64.c
+SRCS+=	loadfile.c loadfile_ecoff.c loadfile_elf32.c lookup_elf32.c \
+	loadfile_elf64.c lookup_elf64.c
 .if (${MACHINE_CPU} != "mips")
 SRCS+=	loadfile_aout.c
 .endif

Added files:

Index: src/sys/lib/libsa/lookup_elf32.c
diff -u /dev/null src/sys/lib/libsa/lookup_elf32.c:1.1
--- /dev/null	Wed Feb 10 21:54:47 2010
+++ src/sys/lib/libsa/lookup_elf32.c	Wed Feb 10 21:54:47 2010
@@ -0,0 +1,94 @@
+/* $NetBSD: lookup_elf32.c,v 1.1 2010/02/10 21:54:47 martin Exp $ */
+
+/*-
+ * Copyright (c) 2010 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Martin Husemann.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+/* If not included by lookup_elf64.c, ELFSIZE won't be defined. */
+#ifndef ELFSIZE
+#define	ELFSIZE	32
+#endif
+
+#include <string.h>
+#include <sys/param.h>
+#include <sys/exec_elf.h>
+
+#if ((ELFSIZE == 32) && defined(BOOT_ELF32)) || \
+    ((ELFSIZE == 64) && defined(BOOT_ELF64))
+
+void *
+ELFNAMEEND(lookup_symbol)(const char *symname, void *sstab, void *estab)
+{
+	Elf_Ehdr *elf;
+	Elf_Shdr *shp;
+	Elf_Sym *symtab_start, *symtab_end, *sp;
+	char *strtab_start, *strtab_end;
+	int i, j;
+
+	elf = sstab;
+	if (elf->e_shoff == 0)
+		return NULL;
+
+	switch (elf->e_machine) {
+	ELFDEFNNAME(MACHDEP_ID_CASES)
+	default:
+		return NULL;
+	}
+
+	symtab_start = symtab_end = NULL;
+	strtab_start = strtab_end = NULL;
+
+	shp = (Elf_Shdr *)((char *)sstab + elf->e_shoff);
+	for (i = 0; i < elf->e_shnum; i++) {
+		if (shp[i].sh_type != SHT_SYMTAB)
+			continue;
+		if (shp[i].sh_offset == 0)
+			continue;
+		symtab_start = (Elf_Sym*)((char*)sstab + shp[i].sh_offset);
+		symtab_end = (Elf_Sym*)((char*)sstab + shp[i].sh_offset
+			+ shp[i].sh_size);
+		j = shp[i].sh_link;
+		if (shp[j].sh_offset == 0)
+			continue;
+		strtab_start = (char*)sstab + shp[j].sh_offset;
+		strtab_end = (char*)sstab + shp[j].sh_offset + shp[j].sh_size;
+		break;
+	}
+
+	if (!symtab_start || !strtab_start)
+		return NULL;
+
+	for (sp = symtab_start; sp < symtab_end; sp++)
+		if (sp->st_name != 0 &&
+		    strcmp(strtab_start + sp->st_name, symname) == 0)
+			return (void*)(uintptr_t)sp->st_value;
+
+	return NULL;
+}
+
+#endif /* (ELFSIZE == 32 && BOOT_ELF32) || (ELFSIZE == 64 && BOOT_ELF64) */
Index: src/sys/lib/libsa/lookup_elf64.c
diff -u /dev/null src/sys/lib/libsa/lookup_elf64.c:1.1
--- /dev/null	Wed Feb 10 21:54:47 2010
+++ src/sys/lib/libsa/lookup_elf64.c	Wed Feb 10 21:54:47 2010
@@ -0,0 +1,33 @@
+/* $NetBSD: lookup_elf64.c,v 1.1 2010/02/10 21:54:47 martin Exp $ */
+/*-
+ * Copyright (c) 2010 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Martin Husemann.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+#define	ELFSIZE	64
+
+#include "lookup_elf32.c"

Reply via email to