From 1bd66e95f8b9817da320c85fc77ca6466bbdaed7 Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 24 Oct 2012 16:46:12 +0000 Subject: Add framework to support loadable ELF modules (not much logic in place yet) git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5250 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/binfmt/Kconfig | 34 ++++ nuttx/binfmt/Makefile | 55 ++++--- nuttx/binfmt/elf.c | 259 ++++++++++++++++++++++++++++++ nuttx/binfmt/libelf/Kconfig | 9 ++ nuttx/binfmt/libelf/Make.defs | 53 ++++++ nuttx/binfmt/libelf/gnu-elf.ld | 187 +++++++++++++++++++++ nuttx/binfmt/libelf/libelf_bind.c | 106 ++++++++++++ nuttx/binfmt/libelf/libelf_init.c | 189 ++++++++++++++++++++++ nuttx/binfmt/libelf/libelf_load.c | 91 +++++++++++ nuttx/binfmt/libelf/libelf_read.c | 165 +++++++++++++++++++ nuttx/binfmt/libelf/libelf_uninit.c | 85 ++++++++++ nuttx/binfmt/libelf/libelf_unload.c | 97 +++++++++++ nuttx/binfmt/libelf/libelf_verify.c | 88 ++++++++++ nuttx/binfmt/libnxflat/Kconfig | 5 + nuttx/binfmt/libnxflat/Make.defs | 24 ++- nuttx/binfmt/libnxflat/libnxflat_bind.c | 46 +++--- nuttx/binfmt/libnxflat/libnxflat_uninit.c | 4 - nuttx/binfmt/libnxflat/libnxflat_verify.c | 6 +- 18 files changed, 1441 insertions(+), 62 deletions(-) create mode 100644 nuttx/binfmt/elf.c create mode 100644 nuttx/binfmt/libelf/Kconfig create mode 100644 nuttx/binfmt/libelf/Make.defs create mode 100644 nuttx/binfmt/libelf/gnu-elf.ld create mode 100644 nuttx/binfmt/libelf/libelf_bind.c create mode 100644 nuttx/binfmt/libelf/libelf_init.c create mode 100644 nuttx/binfmt/libelf/libelf_load.c create mode 100644 nuttx/binfmt/libelf/libelf_read.c create mode 100644 nuttx/binfmt/libelf/libelf_uninit.c create mode 100644 nuttx/binfmt/libelf/libelf_unload.c create mode 100644 nuttx/binfmt/libelf/libelf_verify.c (limited to 'nuttx/binfmt') diff --git a/nuttx/binfmt/Kconfig b/nuttx/binfmt/Kconfig index ae2bf3130..3519536fb 100644 --- a/nuttx/binfmt/Kconfig +++ b/nuttx/binfmt/Kconfig @@ -2,3 +2,37 @@ # For a description of the syntax of this configuration file, # see misc/tools/kconfig-language.txt. # + +config BINFMT_DISABLE + bool "Disble BINFMT support" + default n + ---help--- + By default, support for loadable binary formats is built. This logic + may be suppressed be defining this setting. + +if !BINFMT_DISABLE + +config NXFLAT + bool "Enable the NXFLAT Binary Format" + default n + ---help--- + Enable support for the NXFLAT binary format. Default: n + +if NXFLAT +include binfmt/libnxflat/Kconfig +endif + +config ELF + bool "Enable the ELF Binary Format" + default n + ---help--- + Enable support for the ELF binary format. Default: n + +if ELF +include binfmt/libelf/Kconfig +endif +endif + +config SYMTAB_ORDEREDBYNAME + bool "Symbol Tables Ordered by Name" + default n diff --git a/nuttx/binfmt/Makefile b/nuttx/binfmt/Makefile index b3a9269b3..1b98e3ede 100644 --- a/nuttx/binfmt/Makefile +++ b/nuttx/binfmt/Makefile @@ -1,7 +1,7 @@ ############################################################################ # nxflat/Makefile # -# Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. +# Copyright (C) 2007-2009, 2012 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -36,55 +36,54 @@ -include $(TOPDIR)/Make.defs ifeq ($(WINTOOL),y) -INCDIROPT = -w +INCDIROPT = -w endif -CFLAGS += ${shell $(TOPDIR)/tools/incdir.sh $(INCDIROPT) "$(CC)" $(TOPDIR)/sched} +CFLAGS += ${shell $(TOPDIR)/tools/incdir.sh $(INCDIROPT) "$(CC)" $(TOPDIR)/sched} -ifeq ($(CONFIG_NXFLAT),y) -include libnxflat/Make.defs -LIBNXFLAT_CSRCS += nxflat.c -endif +# Basic BINFMT source files BINFMT_ASRCS = BINFMT_CSRCS = binfmt_globals.c binfmt_register.c binfmt_unregister.c \ - binfmt_loadmodule.c binfmt_unloadmodule.c binfmt_execmodule.c \ - binfmt_exec.c binfmt_dumpmodule.c + binfmt_loadmodule.c binfmt_unloadmodule.c binfmt_execmodule.c \ + binfmt_exec.c binfmt_dumpmodule.c -SYMTAB_ASRCS = -SYMTAB_CSRCS = symtab_findbyname.c symtab_findbyvalue.c \ - symtab_findorderedbyname.c symtab_findorderedbyvalue.c +# Symbol table source files -SUBDIRS = libnxflat +BINFMT_CSRCS += symtab_findbyname.c symtab_findbyvalue.c \ + symtab_findorderedbyname.c symtab_findorderedbyvalue.c -ASRCS = $(BINFMT_ASRCS) $(SYMTAB_ASRCS) $(LIBNXFLAT_ASRCS) -AOBJS = $(ASRCS:.S=$(OBJEXT)) +# Add configured binary modules -CSRCS = $(BINFMT_CSRCS) $(SYMTAB_CSRCS) $(LIBNXFLAT_CSRCS) -COBJS = $(CSRCS:.c=$(OBJEXT)) +VPATH = +SUBDIRS = + +include libnxflat/Make.defs +include libelf/Make.defs -SRCS = $(ASRCS) $(CSRCS) -OBJS = $(AOBJS) $(COBJS) +BINFMT_AOBJS = $(BINFMT_ASRCS:.S=$(OBJEXT)) +BINFMT_COBJS = $(BINFMT_CSRCS:.c=$(OBJEXT)) -BIN = libbinfmt$(LIBEXT) +BINFMT_SRCS = $(BINFMT_ASRCS) $(BINFMT_CSRCS) +BINFMT_OBJS = $(BINFMT_AOBJS) $(BINFMT_COBJS) -VPATH = libnxflat +BIN = libbinfmt$(LIBEXT) -all: $(BIN) +all: $(BIN) -$(AOBJS): %$(OBJEXT): %.S +$(BINFMT_AOBJS): %$(OBJEXT): %.S $(call ASSEMBLE, $<, $@) -$(COBJS): %$(OBJEXT): %.c +$(BINFMT_COBJS): %$(OBJEXT): %.c $(call COMPILE, $<, $@) -$(BIN): $(OBJS) - @( for obj in $(OBJS) ; do \ +$(BIN): $(BINFMT_OBJS) + @( for obj in $(BINFMT_OBJS) ; do \ $(call ARCHIVE, $@, $${obj}); \ done ; ) -.depend: Makefile $(SRCS) +.depend: Makefile $(BINFMT_SRCS) @$(MKDEP) --dep-path . --dep-path libnxflat \ - $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep + $(CC) -- $(CFLAGS) -- $(BINFMT_SRCS) >Make.dep @touch $@ depend: .depend diff --git a/nuttx/binfmt/elf.c b/nuttx/binfmt/elf.c new file mode 100644 index 000000000..74a35174f --- /dev/null +++ b/nuttx/binfmt/elf.c @@ -0,0 +1,259 @@ +/**************************************************************************** + * binfmt/elf.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#ifdef CONFIG_ELF + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* CONFIG_DEBUG, CONFIG_DEBUG_VERBOSE, and CONFIG_DEBUG_BINFMT have to be + * defined or CONFIG_ELF_DUMPBUFFER does nothing. + */ + +#if !defined(CONFIG_DEBUG_VERBOSE) || !defined (CONFIG_DEBUG_BINFMT) +# undef CONFIG_ELF_DUMPBUFFER +#endif + +#ifdef CONFIG_ELF_DUMPBUFFER +# define elf_dumpbuffer(m,b,n) bvdbgdumpbuffer(m,b,n) +#else +# define elf_dumpbuffer(m,b,n) +#endif + +#ifndef MIN +# define MIN(a,b) (a < b ? a : b) +#endif + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int elf_loadbinary(FAR struct binary_s *binp); +#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_BINFMT) +static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct binfmt_s g_elfbinfmt = +{ + NULL, /* next */ + elf_loadbinary, /* load */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_dumploadinfo + ****************************************************************************/ + +#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_BINFMT) +static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo) +{ + unsigned long dsize = loadinfo->datasize + loadinfo->bsssize; + + bdbg("LOAD_INFO:\n"); + bdbg(" ISPACE:\n"); + bdbg(" ispace: %08lx\n", loadinfo->ispace); + bdbg(" entryoffs: %08lx\n", loadinfo->entryoffs); + bdbg(" isize: %08lx\n", loadinfo->isize); + + bdbg(" DSPACE:\n"); + bdbg(" dspace: %08lx\n", loadinfo->dspace); + if (loadinfo->dspace != NULL) + { + bdbg(" crefs: %d\n", loadinfo->dspace->crefs); + bdbg(" region: %08lx\n", loadinfo->dspace->region); + } + bdbg(" datasize: %08lx\n", loadinfo->datasize); + bdbg(" bsssize: %08lx\n", loadinfo->bsssize); + bdbg(" (pad): %08lx\n", loadinfo->dsize - dsize); + bdbg(" stacksize: %08lx\n", loadinfo->stacksize); + bdbg(" dsize: %08lx\n", loadinfo->dsize); + + bdbg(" RELOCS:\n"); + bdbg(" relocstart: %08lx\n", loadinfo->relocstart); + bdbg(" reloccount: %d\n", loadinfo->reloccount); + + bdbg(" HANDLES:\n"); + bdbg(" filfd: %d\n", loadinfo->filfd); +} +#else +# define elf_dumploadinfo(i) +#endif + +/**************************************************************************** + * Name: elf_loadbinary + * + * Description: + * Verify that the file is an ELF binary and, if so, load the ELF + * binary into memory + * + ****************************************************************************/ + +static int elf_loadbinary(struct binary_s *binp) +{ + struct elf_loadinfo_s loadinfo; /* Contains globals for libelf */ + int ret; + + bvdbg("Loading file: %s\n", binp->filename); + + /* Initialize the xflat library to load the program binary. */ + + ret = elf_init(binp->filename, &loadinfo); + elf_dumploadinfo(&loadinfo); + if (ret != 0) + { + bdbg("Failed to initialize for load of NXFLT program: %d\n", ret); + goto errout; + } + + /* Load the program binary */ + + ret = elf_load(&loadinfo); + elf_dumploadinfo(&loadinfo); + if (ret != 0) + { + bdbg("Failed to load NXFLT program binary: %d\n", ret); + goto errout_with_init; + } + + /* Bind the program to the exported symbol table */ + + ret = elf_bind(&loadinfo, binp->exports, binp->nexports); + if (ret != 0) + { + bdbg("Failed to bind symbols program binary: %d\n", ret); + goto errout_with_load; + } + + /* Return the load information */ + + binp->entrypt = (main_t)(loadinfo.ispace + loadinfo.entryoffs); + binp->ispace = (void*)loadinfo.ispace; + binp->dspace = (void*)loadinfo.dspace; + binp->isize = loadinfo.isize; + binp->stacksize = loadinfo.stacksize; + + elf_dumpbuffer("Entry code", (FAR const uint8_t*)binp->entrypt, + MIN(binp->isize - loadinfo.entryoffs,512)); + + elf_uninit(&loadinfo); + return OK; + +errout_with_load: + elf_unload(&loadinfo); +errout_with_init: + elf_uninit(&loadinfo); +errout: + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_initialize + * + * Description: + * ELF support is built unconditionally. However, it order to + * use this binary format, this function must be called during system + * format in order to register the ELF binary format. + * + * Returned Value: + * This is a NuttX internal function so it follows the convention that + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_initialize(void) +{ + int ret; + + /* Register ourselves as a binfmt loader */ + + bvdbg("Registering ELF\n"); + + ret = register_binfmt(&g_elfbinfmt); + if (ret != 0) + { + bdbg("Failed to register binfmt: %d\n", ret); + } + + return ret; +} + +/**************************************************************************** + * Name: elf_uninitialize + * + * Description: + * Unregister the ELF binary loader + * + * Returned Value: + * None + * + ****************************************************************************/ + +void elf_uninitialize(void) +{ + unregister_binfmt(&g_elfbinfmt); +} + +#endif /* CONFIG_ELF */ + diff --git a/nuttx/binfmt/libelf/Kconfig b/nuttx/binfmt/libelf/Kconfig new file mode 100644 index 000000000..c47bc9f9e --- /dev/null +++ b/nuttx/binfmt/libelf/Kconfig @@ -0,0 +1,9 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +config ELF_DUMPBUFFER + bool "Dump ELF buffers" + default n + depends on DEBUG && DEBUG_VERBOSE diff --git a/nuttx/binfmt/libelf/Make.defs b/nuttx/binfmt/libelf/Make.defs new file mode 100644 index 000000000..629961863 --- /dev/null +++ b/nuttx/binfmt/libelf/Make.defs @@ -0,0 +1,53 @@ +############################################################################ +# binfmt/libelf/Make.defs +# +# Copyright (C) 2012 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# 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. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 +# COPYRIGHT OWNER 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. +# +############################################################################ + +ifeq ($(CONFIG_ELF),y) + +# ELF application interfaces + +BINFMT_CSRCS += elf.c + +# ELF library + +BINFMT_CSRCS = libelf_init.c libelf_uninit.c libelf_load.c \ + libelf_unload.c libelf_verify.c libelf_read.c \ + libelf_bind.c + +# Hook the libelf subdirectory into the build + +VPATH += libelf +SUBDIRS += libelf + +endif diff --git a/nuttx/binfmt/libelf/gnu-elf.ld b/nuttx/binfmt/libelf/gnu-elf.ld new file mode 100644 index 000000000..703f36981 --- /dev/null +++ b/nuttx/binfmt/libelf/gnu-elf.ld @@ -0,0 +1,187 @@ +/**************************************************************************** + * examples/elf/gnu-elf-gotoff.ld + * + * Copyright (C) 2009, 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +MEMORY +{ + ISPACE : ORIGIN = 0x0, LENGTH = 2097152 + DSPACE : ORIGIN = 0x0, LENGTH = 2097152 +} + +/**************************************************************************** + * The XFLAT program image is divided into two segments: + * + * (1) ISpace (Instruction Space). This is the segment that contains + * code (.text). Everything in the segment should be fetch-able + * machine PC instructions (jump, branch, call, etc.). + * (2) DSpace (Data Space). This is the segment that contains both + * read-write data (.data, .bss) as well as read-only data (.rodata). + * Everything in this segment should be access-able with machine + * PIC load and store instructions. + * + * Older versions of GCC (at least up to GCC 4.3.3), use GOT-relative + * addressing to access RO data. In that case, read-only data (.rodata) must + * reside in D-Space and this linker script should be used. + * + * Newer versions of GCC (at least as of GCC 4.6.3), use PC-relative + * addressing to access RO data. In that case, read-only data (.rodata) must + * reside in I-Space and this linker script should NOT be used with those + * newer tools. + * + ****************************************************************************/ + +SECTIONS +{ + .text 0x00000000 : + { + /* ISpace is located at address 0. Every (unrelocated) ISpace + * address is an offset from the begining of this segment. + */ + + text_start = . ; + + *(.text) + *(.text.*) + *(.gnu.warning) + *(.stub) + *(.glue_7) + *(.glue_7t) + *(.jcr) + + /* C++ support: The .init and .fini sections contain XFLAT- + * specific logic to manage static constructors and destructors. + */ + + *(.gnu.linkonce.t.*) + *(.init) + *(.fini) + + /* This is special code area at the end of the normal + text section. It contains a small lookup table at + the start followed by the code pointed to by entries + in the lookup table. */ + + . = ALIGN (4) ; + PROVIDE(__ctbp = .); + *(.call_table_data) + *(.call_table_text) + + _etext = . ; + + } > ISPACE + + /* DSpace is also located at address 0. Every (unrelocated) DSpace + * address is an offset from the begining of this segment. + */ + + .data 0x00000000 : + { + /* In this model, .rodata is access using PC-relative addressing + * and, hence, must also reside in the .text section. + */ + + __data_start = . ; + *(.rodata) + *(.rodata1) + *(.rodata.*) + *(.gnu.linkonce.r*) + + *(.data) + *(.data1) + *(.data.*) + *(.gnu.linkonce.d*) + *(.data1) + *(.eh_frame) + *(.gcc_except_table) + + *(.gnu.linkonce.s.*) + *(__libc_atexit) + *(__libc_subinit) + *(__libc_subfreeres) + *(.note.ABI-tag) + + /* C++ support. For each global and static local C++ object, + * GCC creates a small subroutine to construct the object. Pointers + * to these routines (not the routines themselves) are stored as + * simple, linear arrays in the .ctors section of the object file. + * Similarly, pointers to global/static destructor routines are + * stored in .dtors. + */ + + *(.gnu.linkonce.d.*) + + _ctors_start = . ; + *(.ctors) + _ctors_end = . ; + _dtors_start = . ; + *(.dtors) + _dtors_end = . ; + + _edata = . ; + edata = ALIGN( 0x10 ) ; + } > DSPACE + + .bss : + { + __bss_start = _edata ; + *(.dynsbss) + *(.sbss) + *(.sbss.*) + *(.scommon) + *(.dynbss) + *(.bss) + *(.bss.*) + *(.bss*) + *(.gnu.linkonce.b*) + *(COMMON) + end = ALIGN( 0x10 ) ; + _end = ALIGN( 0x10 ) ; + } > DSPACE + + .got 0 : { *(.got.plt) *(.got) } + .junk 0 : { *(.rel*) *(.rela*) } + /* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_info 0 : { *(.debug_info) } + .debug_line 0 : { *(.debug_line) } + .debug_pubnames 0 : { *(.debug_pubnames) } + .debug_aranges 0 : { *(.debug_aranges) } +} diff --git a/nuttx/binfmt/libelf/libelf_bind.c b/nuttx/binfmt/libelf/libelf_bind.c new file mode 100644 index 000000000..44532ab88 --- /dev/null +++ b/nuttx/binfmt/libelf/libelf_bind.c @@ -0,0 +1,106 @@ +/**************************************************************************** + * binfmt/libelf/libelf_bind.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* CONFIG_DEBUG, CONFIG_DEBUG_VERBOSE, and CONFIG_DEBUG_BINFMT have to be + * defined or CONFIG_ELF_DUMPBUFFER does nothing. + */ + +#if !defined(CONFIG_DEBUG_VERBOSE) || !defined (CONFIG_DEBUG_BINFMT) +# undef CONFIG_ELF_DUMPBUFFER +#endif + +#ifdef CONFIG_ELF_DUMPBUFFER +# define elf_dumpbuffer(m,b,n) bvdbgdumpbuffer(m,b,n) +#else +# define elf_dumpbuffer(m,b,n) +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_bind + * + * Description: + * Bind the imported symbol names in the loaded module described by + * 'loadinfo' using the exported symbol values provided by 'symtab'. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_bind(FAR struct elf_loadinfo_s *loadinfo, + FAR const struct symtab_s *exports, int nexports) +{ +#warning "Missing logic" + return -ENOSYS; +} + diff --git a/nuttx/binfmt/libelf/libelf_init.c b/nuttx/binfmt/libelf/libelf_init.c new file mode 100644 index 000000000..d72e96b62 --- /dev/null +++ b/nuttx/binfmt/libelf/libelf_init.c @@ -0,0 +1,189 @@ +/**************************************************************************** + * binfmt/libelf/libelf_init.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ + +/* CONFIG_DEBUG, CONFIG_DEBUG_VERBOSE, and CONFIG_DEBUG_BINFMT have to be + * defined or CONFIG_ELF_DUMPBUFFER does nothing. + */ + +#if !defined(CONFIG_DEBUG_VERBOSE) || !defined (CONFIG_DEBUG_BINFMT) +# undef CONFIG_ELF_DUMPBUFFER +#endif + +#ifdef CONFIG_ELF_DUMPBUFFER +# define elf_dumpbuffer(m,b,n) bvdbgdumpbuffer(m,b,n) +#else +# define elf_dumpbuffer(m,b,n) +#endif + +/**************************************************************************** + * Private Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_init + * + * Description: + * This function is called to configure the library to process an ELF + * program binary. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_init(FAR const char *filename, FAR struct elf_loadinfo_s *loadinfo) +{ + uint32_t datastart; + uint32_t dataend; + uint32_t bssstart; + uint32_t bssend; + int ret; + + bvdbg("filename: %s loadinfo: %p\n", filename, loadinfo); + + /* Clear the load info structure */ + + memset(loadinfo, 0, sizeof(struct elf_loadinfo_s)); + + /* Open the binary file */ + + loadinfo->filfd = open(filename, O_RDONLY); + if (loadinfo->filfd < 0) + { + bdbg("Failed to open ELF binary %s: %d\n", filename, ret); + return -errno; + } + + /* Read the ELF header from offset 0 */ + + ret = elf_read(loadinfo, (char*)&loadinfo->header, + sizeof(struct elf_hdr_s), 0); + if (ret < 0) + { + bdbg("Failed to read ELF header: %d\n", ret); + return ret; + } + + elf_dumpbuffer("ELF header", (FAR const uint8_t*)&loadinfo->header, + sizeof(struct elf_hdr_s)); + + /* Verify the ELF header */ + + if (elf_verifyheader(&loadinfo->header) != 0) + { + /* This is not an error because we will be called to attempt loading + * EVERY binary. Returning -ENOEXEC simply informs the system that + * the file is not an ELF file. Besides, if there is something worth + * complaining about, nelf_verifyheader() has already + * done so. + */ + + bdbg("Bad ELF header\n"); + return -ENOEXEC; + } + + /* Save all of the input values in the loadinfo structure + * and extract some additional information from the xflat + * header. Note that the information in the xflat header is in + * network order. + */ + + datastart = ntohl(loadinfo->header.h_datastart); + dataend = ntohl(loadinfo->header.h_dataend); + bssstart = dataend; + bssend = ntohl(loadinfo->header.h_bssend); + + /* And put this information into the loadinfo structure as well. + * + * Note that: + * + * isize = the address range from 0 up to datastart. + * datasize = the address range from datastart up to dataend + * bsssize = the address range from dataend up to bssend. + */ + + loadinfo->entryoffs = ntohl(loadinfo->header.h_entry); + loadinfo->isize = datastart; + + loadinfo->datasize = dataend - datastart; + loadinfo->bsssize = bssend - dataend; + loadinfo->stacksize = ntohl(loadinfo->header.h_stacksize); + + /* This is the initial dspace size. We'll re-calculate this later + * after the memory has been allocated. + */ + + loadinfo->dsize = bssend - datastart; + + /* Get the offset to the start of the relocations (we'll relocate + * this later). + */ + + loadinfo->relocstart = ntohl(loadinfo->header.h_relocstart); + loadinfo->reloccount = ntohs(loadinfo->header.h_reloccount); + + return 0; +} + diff --git a/nuttx/binfmt/libelf/libelf_load.c b/nuttx/binfmt/libelf/libelf_load.c new file mode 100644 index 000000000..94d200b56 --- /dev/null +++ b/nuttx/binfmt/libelf/libelf_load.c @@ -0,0 +1,91 @@ +/**************************************************************************** + * binfmt/libelf/libelf_load.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ + +#ifndef MAX +#define MAX(x,y) ((x) > (y) ? (x) : (y)) +#endif + +/**************************************************************************** + * Private Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_load + * + * Description: + * Loads the binary into memory, allocating memory, performing relocations + * and inializing the data and bss segments. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_load(FAR struct elf_loadinfo_s *loadinfo) +{ +# warning "Missing logic" + return -ENOSYS; +} + diff --git a/nuttx/binfmt/libelf/libelf_read.c b/nuttx/binfmt/libelf/libelf_read.c new file mode 100644 index 000000000..5f00a531e --- /dev/null +++ b/nuttx/binfmt/libelf/libelf_read.c @@ -0,0 +1,165 @@ +/**************************************************************************** + * binfmt/libelf/libelf_read.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ + +#undef ELF_DUMP_READDATA /* Define to dump all file data read */ +#define DUMPER lib_rawprintf /* If ELF_DUMP_READDATA is defined, this + * is the API used to dump data */ + +/**************************************************************************** + * Private Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_dumpreaddata + ****************************************************************************/ + +#if defined(ELF_DUMP_READDATA) +static inline void elf_dumpreaddata(char *buffer, int buflen) +{ + uint32_t *buf32 = (uint32_t*)buffer; + int i; + int j; + + for (i = 0; i < buflen; i += 32) + { + DUMPER("%04x:", i); + for (j = 0; j < 32; j += sizeof(uint32_t)) + { + DUMPER(" %08x", *buf32++); + } + DUMPER("\n"); + } +} +#else +# define elf_dumpreaddata(b,n) +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_read + * + * Description: + * Read 'readsize' bytes from the object file at 'offset' + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_read(struct elf_loadinfo_s *loadinfo, char *buffer, int readsize, int offset) +{ + ssize_t nbytes; /* Number of bytes read */ + off_t rpos; /* Position returned by lseek */ + char *bufptr; /* Next buffer location to read into */ + int bytesleft; /* Number of bytes of .data left to read */ + int bytesread; /* Total number of bytes read */ + + bvdbg("Read %d bytes from offset %d\n", readsize, offset); + + /* Seek to the position in the object file where the initialized + * data is saved. + */ + + bytesread = 0; + bufptr = buffer; + bytesleft = readsize; + do + { + rpos = lseek(loadinfo->filfd, offset, SEEK_SET); + if (rpos != offset) + { + bdbg("Failed to seek to position %d: %d\n", offset, errno); + return -errno; + } + + /* Read the file data at offset into the user buffer */ + + nbytes = read(loadinfo->filfd, bufptr, bytesleft); + if (nbytes < 0) + { + if (errno != EINTR) + { + bdbg("Read of .data failed: %d\n", errno); + return -errno; + } + } + else if (nbytes == 0) + { + bdbg("Unexpected end of file\n"); + return -ENODATA; + } + else + { + bytesread += nbytes; + bytesleft -= nbytes; + bufptr += nbytes; + offset += nbytes; + } + } + while (bytesread < readsize); + + elf_dumpreaddata(buffer, readsize); + return OK; +} + diff --git a/nuttx/binfmt/libelf/libelf_uninit.c b/nuttx/binfmt/libelf/libelf_uninit.c new file mode 100644 index 000000000..cd7ae7282 --- /dev/null +++ b/nuttx/binfmt/libelf/libelf_uninit.c @@ -0,0 +1,85 @@ +/**************************************************************************** + * binfmt/libelf/libelf_uninit.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_uninit + * + * Description: + * Releases any resources committed by elf_init(). This essentially + * undoes the actions of elf_init. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_uninit(struct elf_loadinfo_s *loadinfo) +{ + if (loadinfo->filfd >= 0) + { + close(loadinfo->filfd); + } + + return OK; +} + diff --git a/nuttx/binfmt/libelf/libelf_unload.c b/nuttx/binfmt/libelf/libelf_unload.c new file mode 100644 index 000000000..9e930e295 --- /dev/null +++ b/nuttx/binfmt/libelf/libelf_unload.c @@ -0,0 +1,97 @@ +/**************************************************************************** + * binfmt/libelf/libelf_unload.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_unload + * + * Description: + * This function unloads the object from memory. This essentially + * undoes the actions of elf_load. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_unload(struct elf_loadinfo_s *loadinfo) +{ + /* Reset the contents of the info structure. */ + + /* Release the memory segments */ + + if (loadinfo->ispace) + { + kfree((void*)loadinfo->ispace, loadinfo->isize); + loadinfo->ispace = 0; + } + + if (loadinfo->dspace) + { + kfree((void*)loadinfo->dspace); + loadinfo->dspace = 0; + } + + return OK; +} + diff --git a/nuttx/binfmt/libelf/libelf_verify.c b/nuttx/binfmt/libelf/libelf_verify.c new file mode 100644 index 000000000..9e265573a --- /dev/null +++ b/nuttx/binfmt/libelf/libelf_verify.c @@ -0,0 +1,88 @@ +/**************************************************************************** + * binfmt/libelf/elf_verify.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_verifyheader + * + * Description: + * Given the header from a possible ELF executable, verify that it + * is an ELF executable. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_verifyheader(const struct elf_hdr_s *header) +{ + if (!header) + { + bdbg("NULL ELF header!"); + return -ENOEXEC; + } + +#warning "Missing Logic" + return -ENOSYS; +} + diff --git a/nuttx/binfmt/libnxflat/Kconfig b/nuttx/binfmt/libnxflat/Kconfig index ae2bf3130..fdb270cfb 100644 --- a/nuttx/binfmt/libnxflat/Kconfig +++ b/nuttx/binfmt/libnxflat/Kconfig @@ -2,3 +2,8 @@ # For a description of the syntax of this configuration file, # see misc/tools/kconfig-language.txt. # + +config NXFLAT_DUMPBUFFER + bool "Dump NXFLAT buffers" + default n + depends on DEBUG && DEBUG_VERBOSE diff --git a/nuttx/binfmt/libnxflat/Make.defs b/nuttx/binfmt/libnxflat/Make.defs index f979741e5..59c3ce334 100644 --- a/nuttx/binfmt/libnxflat/Make.defs +++ b/nuttx/binfmt/libnxflat/Make.defs @@ -1,5 +1,5 @@ ############################################################################ -# nxflat/lib/Make.defs +# binfmt/libnxflat/Make.defs # # Copyright (C) 2009 Gregory Nutt. All rights reserved. # Author: Gregory Nutt @@ -33,7 +33,21 @@ # ############################################################################ -LIBNXFLAT_ASRCS = -LIBNXFLAT_CSRCS = libnxflat_init.c libnxflat_uninit.c libnxflat_load.c \ - libnxflat_unload.c libnxflat_verify.c libnxflat_read.c \ - libnxflat_bind.c +ifeq ($(CONFIG_NXFLAT),y) + +# NXFLAT application interfaces + +BINFMT_CSRCS += nxflat.c + +# NXFLAT library + +BINFMT_CSRCS = libnxflat_init.c libnxflat_uninit.c libnxflat_load.c \ + libnxflat_unload.c libnxflat_verify.c libnxflat_read.c \ + libnxflat_bind.c + +# Hook the libnxflat subdirectory into the build + +VPATH += libnxflat +SUBDIRS += libnxflat + +endif diff --git a/nuttx/binfmt/libnxflat/libnxflat_bind.c b/nuttx/binfmt/libnxflat/libnxflat_bind.c index ca348178d..c18a16cd8 100644 --- a/nuttx/binfmt/libnxflat/libnxflat_bind.c +++ b/nuttx/binfmt/libnxflat/libnxflat_bind.c @@ -263,7 +263,6 @@ static inline int nxflat_gotrelocs(FAR struct nxflat_loadinfo_s *loadinfo) result = OK; switch (NXFLAT_RELOC_TYPE(reloc.r_info)) { - /* NXFLAT_RELOC_TYPE_REL32I Meaning: Object file contains a 32-bit offset * into I-Space at the offset. * Fixup: Add mapped I-Space address to the offset. @@ -329,6 +328,7 @@ static inline int nxflat_gotrelocs(FAR struct nxflat_loadinfo_s *loadinfo) nxflat_dumpbuffer("GOT", (FAR const uint8_t*)relocs, nrelocs * sizeof(struct nxflat_reloc_s)); } #endif + return ret; } @@ -388,7 +388,7 @@ static inline int nxflat_bindimports(FAR struct nxflat_loadinfo_s *loadinfo, offset < loadinfo->isize + loadinfo->dsize); imports = (struct nxflat_import_s*) - (offset - loadinfo->isize + loadinfo->dspace->region); + (offset - loadinfo->isize + loadinfo->dspace->region); /* Now, traverse the list of imported symbols and attempt to bind * each symbol to the value exported by from the exported symbol @@ -396,41 +396,41 @@ static inline int nxflat_bindimports(FAR struct nxflat_loadinfo_s *loadinfo, */ for (i = 0; i < nimports; i++) - { - bvdbg("Import[%d] (%08p) offset: %08x func: %08x\n", - i, &imports[i], imports[i].i_funcname, imports[i].i_funcaddress); + { + bvdbg("Import[%d] (%08p) offset: %08x func: %08x\n", + i, &imports[i], imports[i].i_funcname, imports[i].i_funcaddress); - /* Get a pointer to the imported symbol name. The name itself - * lies in the TEXT segment. But the reference to the name - * lies in DATA segment. Therefore, the name reference should - * have been relocated when the module was loaded. - */ + /* Get a pointer to the imported symbol name. The name itself + * lies in the TEXT segment. But the reference to the name + * lies in DATA segment. Therefore, the name reference should + * have been relocated when the module was loaded. + */ offset = imports[i].i_funcname; DEBUGASSERT(offset < loadinfo->isize); - symname = (char*)(offset + loadinfo->ispace + sizeof(struct nxflat_hdr_s)); + symname = (char*)(offset + loadinfo->ispace + sizeof(struct nxflat_hdr_s)); - /* Find the exported symbol value for this this symbol name. */ + /* Find the exported symbol value for this this symbol name. */ #ifdef CONFIG_SYMTAB_ORDEREDBYNAME symbol = symtab_findorderedbyname(exports, symname, nexports); #else symbol = symtab_findbyname(exports, symname, nexports); #endif - if (!symbol) - { - bdbg("Exported symbol \"%s\" not found\n", symname); + if (!symbol) + { + bdbg("Exported symbol \"%s\" not found\n", symname); return -ENOENT; - } + } - /* And put this into the module's import structure. */ + /* And put this into the module's import structure. */ - imports[i].i_funcaddress = (uint32_t)symbol->sym_value; + imports[i].i_funcaddress = (uint32_t)symbol->sym_value; - bvdbg("Bound import[%d] (%08p) to export '%s' (%08x)\n", - i, &imports[i], symname, imports[i].i_funcaddress); - } + bvdbg("Bound import[%d] (%08p) to export '%s' (%08x)\n", + i, &imports[i], symname, imports[i].i_funcaddress); + } } /* Dump the relocation import table */ @@ -441,6 +441,7 @@ static inline int nxflat_bindimports(FAR struct nxflat_loadinfo_s *loadinfo, nxflat_dumpbuffer("Imports", (FAR const uint8_t*)imports, nimports * sizeof(struct nxflat_import_s)); } #endif + return OK; } @@ -484,9 +485,10 @@ int nxflat_bind(FAR struct nxflat_loadinfo_s *loadinfo, */ memset((void*)(loadinfo->dspace->region + loadinfo->datasize), - 0, loadinfo->bsssize); + 0, loadinfo->bsssize); } } + return ret; } diff --git a/nuttx/binfmt/libnxflat/libnxflat_uninit.c b/nuttx/binfmt/libnxflat/libnxflat_uninit.c index 5d06296c7..c507fdc14 100644 --- a/nuttx/binfmt/libnxflat/libnxflat_uninit.c +++ b/nuttx/binfmt/libnxflat/libnxflat_uninit.c @@ -56,10 +56,6 @@ * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: nxflat_swap32 - ****************************************************************************/ - /**************************************************************************** * Public Functions ****************************************************************************/ diff --git a/nuttx/binfmt/libnxflat/libnxflat_verify.c b/nuttx/binfmt/libnxflat/libnxflat_verify.c index f799aca4f..da25d21c7 100644 --- a/nuttx/binfmt/libnxflat/libnxflat_verify.c +++ b/nuttx/binfmt/libnxflat/libnxflat_verify.c @@ -91,10 +91,10 @@ int nxflat_verifyheader(const struct nxflat_hdr_s *header) if (strncmp(header->h_magic, NXFLAT_MAGIC, 4) != 0) { bdbg("Unrecognized magic=\"%c%c%c%c\"\n", - header->h_magic[0], header->h_magic[1], - header->h_magic[2], header->h_magic[3]); + header->h_magic[0], header->h_magic[1], + header->h_magic[2], header->h_magic[3]); return -ENOEXEC; } + return OK; } - -- cgit v1.2.3