Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/lib/libsa Utility function to lookup a symbol value in a...
details: https://anonhg.NetBSD.org/src/rev/d9fdc55c4ea4
branches: trunk
changeset: 751998:d9fdc55c4ea4
user: martin <martin%NetBSD.org@localhost>
date: Wed Feb 10 21:54:47 2010 +0000
description:
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).
diffstat:
sys/lib/libsa/Makefile | 6 +-
sys/lib/libsa/lookup_elf32.c | 94 ++++++++++++++++++++++++++++++++++++++++++++
sys/lib/libsa/lookup_elf64.c | 33 +++++++++++++++
3 files changed, 130 insertions(+), 3 deletions(-)
diffs (155 lines):
diff -r 2250bbb035c8 -r d9fdc55c4ea4 sys/lib/libsa/Makefile
--- a/sys/lib/libsa/Makefile Wed Feb 10 20:54:46 2010 +0000
+++ b/sys/lib/libsa/Makefile Wed Feb 10 21:54:47 2010 +0000
@@ -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
diff -r 2250bbb035c8 -r d9fdc55c4ea4 sys/lib/libsa/lookup_elf32.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/lib/libsa/lookup_elf32.c Wed Feb 10 21:54:47 2010 +0000
@@ -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) */
diff -r 2250bbb035c8 -r d9fdc55c4ea4 sys/lib/libsa/lookup_elf64.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/lib/libsa/lookup_elf64.c Wed Feb 10 21:54:47 2010 +0000
@@ -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"
Home |
Main Index |
Thread Index |
Old Index