From 9b40c0f8c33d62df2885520755bf3e9d947438a5 Mon Sep 17 00:00:00 2001 From: martin Date: Wed, 10 Feb 2010 21:54:47 +0000 Subject: [PATCH] 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). --- 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(-) create mode 100644 sys/lib/libsa/lookup_elf32.c create mode 100644 sys/lib/libsa/lookup_elf64.c diff --git a/sys/lib/libsa/Makefile b/sys/lib/libsa/Makefile index 82338d844b6d..0a618fdf1c42 100644 --- a/sys/lib/libsa/Makefile +++ b/sys/lib/libsa/Makefile @@ -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 @@ SRCS+= cread.c .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 --git a/sys/lib/libsa/lookup_elf32.c b/sys/lib/libsa/lookup_elf32.c new file mode 100644 index 000000000000..5bee2ff6490a --- /dev/null +++ b/sys/lib/libsa/lookup_elf32.c @@ -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 +#include +#include + +#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 --git a/sys/lib/libsa/lookup_elf64.c b/sys/lib/libsa/lookup_elf64.c new file mode 100644 index 000000000000..24b5399172f4 --- /dev/null +++ b/sys/lib/libsa/lookup_elf64.c @@ -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"