Add basic module management logic

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1894 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo
2009-06-17 18:45:48 +00:00
parent 60d59fa80b
commit 8ce89fe17d
16 changed files with 829 additions and 216 deletions

View File

@@ -35,14 +35,21 @@
-include $(TOPDIR)/Make.defs
ifeq ($(CONFIG_NXFLAT),y)
include libnxflat/Make.defs
LIBNXFLAT_CSRCS += nxflat.c
endif
BINFMT_ASRCS =
BINFMT_CSRCS = binfmt_globals.c binfmt_register.c binfmt_unregister.c \
binfmt_loadmodule.c binfmt_unloadmodule.c binfmt_dumpmodule.c
SUBDIRS = libnxflat
ASRCS = $(LIBNXFLAT_ASRCS)
ASRCS = $(BINFMT_ASRCS) $(LIBNXFLAT_ASRCS)
AOBJS = $(ASRCS:.S=$(OBJEXT))
CSRCS = nxflat.c $(LIBNXFLAT_CSRCS)
CSRCS = $(BINFMT_CSRCS) $(LIBNXFLAT_CSRCS)
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)

View File

@@ -0,0 +1,97 @@
/****************************************************************************
* binfmt/binfmt_dumpmodule.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#include <sys/types.h>
#include <sched.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/binfmt.h>
#include "binfmt_internal.h"
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_BINFMT)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/***********************************************************************
* Public Functions
***********************************************************************/
/***********************************************************************
* Name: load_module
*
* Description:
* Load a module into memory and prep it for execution.
*
***********************************************************************/
int dump_module(FAR const struct binary_s *bin)
{
if (bin)
{
bdbg("Module:\n");
bdbg(" filename: %s\n", bin->filename);
bdbg(" argv: %p\n", bin->argv);
bdbg(" entrypt: %p\n", bin->entrypt);
bdbg(" ispace: %p size=%d\n", bin->ispace, bin->isize);
bdbg(" dspace: %p\n", bin->dspace);
bdbg(" stacksize: %d\n", bin->stacksize);
}
return OK;
}
#endif

67
binfmt/binfmt_globals.c Normal file
View File

@@ -0,0 +1,67 @@
/****************************************************************************
* binfmt/binfmt_globals.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#include <sys/types.h>
#include <nuttx/binfmt.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/* This is a list of registered handlers for different binary formats. This
* list should only be accessed by normal user programs. It should be sufficient
* protection to simply disable pre-emption when accessing this list.
*/
FAR struct binfmt_s *g_binfmts;
/****************************************************************************
* Private Functions
****************************************************************************/
/***********************************************************************
* Public Functions
***********************************************************************/

89
binfmt/binfmt_internal.h Normal file
View File

@@ -0,0 +1,89 @@
/****************************************************************************
* binfmt/binfmt_internal.h
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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.
*
****************************************************************************/
#ifndef __BINFMT_BINFMT_INTERNAL_H
#define __BINFMT_BINFMT_INTERNAL_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <nuttx/binfmt.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C" {
#else
#define EXTERN extern
#endif
/* This is a list of registered handlers for different binary formats. This
* list should only be accessed by normal user programs. It should be sufficient
* protection to simply disable pre-emption when accessing this list.
*/
EXTERN FAR struct binfmt_s *g_binfmts;
/***********************************************************************
* Public Function Prototypes
***********************************************************************/
/* Dump the contents of strtuc binary_s */
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_BINFMT)
EXTERN int dump_module(FAR const struct binary_s *bin);
#else
# define dump_module(bin)
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __BINFMT_BINFMT_INTERNAL_H */

127
binfmt/binfmt_loadmodule.c Normal file
View File

@@ -0,0 +1,127 @@
/****************************************************************************
* binfmt/binfmt_loadmodule.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#include <sys/types.h>
#include <sched.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/binfmt.h>
#include "binfmt_internal.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/***********************************************************************
* Public Functions
***********************************************************************/
/***********************************************************************
* Name: load_module
*
* Description:
* Load a module into memory and prep it for execution.
*
***********************************************************************/
int load_module(const char *filename, FAR struct binary_s *bin)
{
FAR struct binfmt_s *binfmt;
int ret;
#ifdef CONFIG_DEBUG
if (!filename || !bin)
{
ret = -EINVAL;
}
else
{
#endif
bdbg("Loading %s\n", filename);
/* Disabling pre-emption should be sufficient protection while
* accessing the list of registered binary format handlers.
*/
sched_lock();
/* Traverse the list of registered binary format handlers. Stop
* when either (1) a handler recognized and loads the format, or
* (2) no handler recognizes the format.
*/
for (binfmt = g_binfmts; binfmt; binfmt = binfmt->next)
{
/* Use this handler to try to load the format */
ret = binfmt->load(bin);
if (ret == OK)
{
/* Successfully loaded -- break out with ret == 0 */
dump_module(bin);
break;
}
}
sched_unlock();
}
if (ret < 0) bdbg("Returning %d\n", ret);
return ret;
}

97
binfmt/binfmt_register.c Normal file
View File

@@ -0,0 +1,97 @@
/****************************************************************************
* binfmt/binfmt_register.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#include <sys/types.h>
#include <string.h>
#include <sched.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/binfmt.h>
#include "binfmt_internal.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/***********************************************************************
* Public Functions
***********************************************************************/
/***********************************************************************
* Name: register_binfmt
*
* Description:
* Register a loader for a binary format
*
***********************************************************************/
int register_binfmt(FAR struct binfmt_s *binfmt)
{
if (binfmt)
{
/* Add the new binary format handler to the head of the list of
* handlers
*/
sched_lock();
binfmt->next = g_binfmts;
g_binfmts = binfmt;
sched_unlock();
return OK;
}
return -EINVAL;
}

View File

@@ -0,0 +1,100 @@
/****************************************************************************
* binfmt/binfmt_loadmodule.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <sched.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/binfmt.h>
#include "binfmt_internal.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/***********************************************************************
* Public Functions
***********************************************************************/
/***********************************************************************
* Name: unload_module
*
* Description:
* Unload a (non-executing) module from memory. If the module has
* been started (via exec_module), calling this will be fatal.
*
***********************************************************************/
int unload_module(FAR const struct binary_s *bin)
{
if (bin)
{
if (bin->ispace)
{
bvdbg("Unmapping ISpace: %p\n", bin->ispace);
munmap(bin->ispace, bin->isize);
}
if (bin->dspace)
{
bvdbg("Freeing DSpace: %p\n", bin->dspace);
free(bin->dspace);
}
}
return OK;
}

130
binfmt/binfmt_unregister.c Normal file
View File

@@ -0,0 +1,130 @@
/****************************************************************************
* binfmt/binfmt_unregister.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#include <sys/types.h>
#include <string.h>
#include <sched.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/binfmt.h>
#include "binfmt_internal.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/***********************************************************************
* Public Functions
***********************************************************************/
/***********************************************************************
* Name: unregister_binfmt
*
* Description:
* Register a loader for a binary format
*
***********************************************************************/
int unregister_binfmt(FAR struct binfmt_s *binfmt)
{
FAR struct binfmt_s *curr;
FAR struct binfmt_s *prev;
int ret = -EINVAL;
if (binfmt)
{
/* Disabling pre-emption should be sufficient protection while
* accessing the list of registered binary format handlers.
*/
sched_lock();
/* Search the list of registered binary format handlers for the
* one to be unregistered.
*/
for (prev = NULL, curr = g_binfmts;
curr && curr != binfmt;
prev = curr, curr = curr->next);
/* Was it in the list? */
if (curr)
{
/* Yes.. was it at the head of the list? */
if (!prev)
{
/* Yes.. remove it from the head of the list */
g_binfmts = binfmt->next;
}
else
{
/* No.. remove it from the middle/end of the list */
prev->next = binfmt->next;
}
binfmt->next = NULL;
ret = OK;
}
sched_unlock();
}
return ret;
}

View File

@@ -136,34 +136,30 @@ int nxflat_init(const char *filename, struct nxflat_hdr_s *header,
*
* Note that:
*
* ispace_size = the address range from 0 up to datastart.
* data_size = the address range from datastart up to dataend
* bss_size = the address range from dataend up to bssend.
* 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->entry_offset = ntohl(header->h_entry);
loadinfo->ispace_size = datastart;
loadinfo->entryoffs = ntohl(header->h_entry);
loadinfo->isize = datastart;
loadinfo->data_size = dataend - datastart;
loadinfo->bss_size = bssend - dataend;
loadinfo->stack_size = ntohl(header->h_stacksize);
loadinfo->datasize = dataend - datastart;
loadinfo->bsssize = bssend - dataend;
loadinfo->stacksize = ntohl(header->h_stacksize);
/* This is the initial dspace size. We'll recaculate this later
* after the memory has been allocated. So that the caller can feel
* free to modify dspace_size values from now until then.
/* This is the initial dspace size. We'll re-calculate this later
* after the memory has been allocated.
*/
loadinfo->dspace_size = /* Total DSpace Size is: */
(NXFLAT_DATA_OFFSET + /* Memory set aside for ldso */
bssend - datastart + /* Data and bss segment sizes */
loadinfo->stack_size); /* (Current) stack size */
loadinfo->dsize = bssend - datastart;
/* Get the offset to the start of the relocations (we'll relocate
* this later).
*/
loadinfo->reloc_start = ntohl(header->h_relocstart);
loadinfo->reloc_count = ntohl(header->h_reloccount);
loadinfo->relocstart = ntohl(header->h_relocstart);
loadinfo->reloccount = ntohl(header->h_reloccount);
return 0;
}

View File

@@ -106,11 +106,11 @@ static void nxflat_reloc(struct nxflat_loadinfo_s *loadinfo, uint32 rl)
* section of the file image.
*/
if (reloc.s.r_offset > loadinfo->data_size)
if (reloc.s.r_offset > loadinfo->datasize)
{
bdbg("ERROR: Relocation at 0x%08x invalid -- "
"does not lie in the data segment, size=0x%08x\n",
reloc.s.r_offset, loadinfo->data_size);
reloc.s.r_offset, loadinfo->datasize);
bdbg(" Relocation not performed!\n");
}
else if ((reloc.s.r_offset & 0x00000003) != 0)
@@ -126,7 +126,7 @@ static void nxflat_reloc(struct nxflat_loadinfo_s *loadinfo, uint32 rl)
* DSpace to hold information needed by ld.so at run time.
*/
datastart = loadinfo->dspace + NXFLAT_DATA_OFFSET;
datastart = loadinfo->dspace;
/* Get a pointer to the value that needs relocation in
* DSpace.
@@ -149,15 +149,14 @@ static void nxflat_reloc(struct nxflat_loadinfo_s *loadinfo, uint32 rl)
break;
/* DATA and BSS are always contiguous regions. DATA
* begins at an offset of NXFLAT_DATA_OFFSET from
* the beginning of the allocated data segment.
* begins at the beginning of the allocated data segment.
* BSS is positioned after DATA, unrelocated references
* to BSS include the data offset.
*
* In other contexts, is it necessary to add the data_size
* In other contexts, is it necessary to add the datasize
* to get the BSS offset like:
*
* *ptr += datastart + loadinfo->data_size;
* *ptr += datastart + loadinfo->datasize;
*/
case NXFLAT_RELOC_TYPE_DATA:
@@ -198,11 +197,11 @@ int nxflat_load(struct nxflat_loadinfo_s *loadinfo)
uint32 ret;
int i;
/* Calculate the extra space we need to map in. This region will be the
* BSS segment and the stack. It will also be used temporarily to hold
* relocation information. So the size of this region will either be the
* size of the BSS section and the stack OR, it the size of the relocation
* entries, whichever is larger
/* Calculate the extra space we need to allocate. This extra space will be
* the size of the BSS section. This extra space will also be used
* temporarily to hold relocation information. So the allocated size of this
* region will either be the size of .data + size of.bss section OR, the
* size of .data + the relocation entries, whichever is larger
*/
{
@@ -213,35 +212,32 @@ int nxflat_load(struct nxflat_loadinfo_s *loadinfo)
* relocations.
*/
relocsize = loadinfo->reloc_count * sizeof(uint32);
relocsize = loadinfo->reloccount * sizeof(uint32);
/* In the file, the relocations should lie at the same offset as BSS.
* The additional amount that we allocate have to be either (1) the
* BSS size + the stack size, or (2) the size of the relocation records,
* whicher is larger.
* BSS size, or (2) the size of the relocation records, whicher is
* larger.
*/
extrasize = MAX(loadinfo->bss_size + loadinfo->stack_size, relocsize);
extrasize = MAX(loadinfo->bsssize, relocsize);
/* Use this addtional amount to adjust the total size of the dspace
* region.
*/
loadinfo->dspace_size =
NXFLAT_DATA_OFFSET + /* Memory used by ldso */
loadinfo->data_size + /* Initialized data */
extrasize; /* bss+stack/relocs */
loadinfo->dsize = loadinfo->datasize + extrasize;
/* The number of bytes of data that we have to read from the file is
* the data size plus the size of the relocation table.
*/
dreadsize = loadinfo->data_size + relocsize;
dreadsize = loadinfo->datasize + relocsize;
}
/* We'll need this a few times as well. */
doffset = loadinfo->ispace_size;
doffset = loadinfo->isize;
/* We will make two mmap calls create an address space for the executable.
* We will attempt to map the file to get the ISpace address space and
@@ -255,7 +251,7 @@ int nxflat_load(struct nxflat_loadinfo_s *loadinfo)
* resides as long as it is fully initialized and ready to execute.
*/
loadinfo->ispace = (uint32)mmap(NULL, loadinfo->ispace_size, PROT_READ,
loadinfo->ispace = (uint32)mmap(NULL, loadinfo->isize, PROT_READ,
MAP_SHARED|MAP_FILE, loadinfo->filfd, 0);
if (loadinfo->ispace == (uint32)MAP_FAILED)
{
@@ -263,14 +259,13 @@ int nxflat_load(struct nxflat_loadinfo_s *loadinfo)
return -errno;
}
bvdbg("Mapped ISpace (%d bytes) at 0x%08x\n",
loadinfo->ispace_size, loadinfo->ispace);
bvdbg("Mapped ISpace (%d bytes) at 0x%08x\n", loadinfo->isize, loadinfo->ispace);
/* The following call will give a pointer to the allocated but
* uninitialized ISpace memory.
*/
loadinfo->dspace = (uint32)malloc(loadinfo->dspace_size);
loadinfo->dspace = (uint32)malloc(loadinfo->dsize);
if (loadinfo->dspace == 0)
{
bdbg("Failed to allocate DSpace\n");
@@ -279,57 +274,46 @@ int nxflat_load(struct nxflat_loadinfo_s *loadinfo)
}
bvdbg("Allocated DSpace (%d bytes) at %08x\n",
loadinfo->dspace_size, loadinfo->dspace);
loadinfo->dsize, loadinfo->dspace);
/* Now, read the data into allocated DSpace at doffset into the
* allocated DSpace memory.
*/
ret = nxflat_read(loadinfo, (char*)(loadinfo->dspace + NXFLAT_DATA_OFFSET), dreadsize, doffset);
ret = nxflat_read(loadinfo, (char*)loadinfo->dspace, dreadsize, doffset);
if (ret < 0)
{
bdbg("Failed to read .data section: %d\n", ret);
goto errout;
}
/* Save information about the allocation. */
loadinfo->alloc_start = loadinfo->dspace;
loadinfo->alloc_size = loadinfo->dspace_size;
bvdbg("TEXT=0x%x Entry point offset=0x%08x, datastart is 0x%08x\n",
loadinfo->ispace, loadinfo->entry_offset, doffset);
loadinfo->ispace, loadinfo->entryoffs, doffset);
/* Resolve the address of the relocation table. In the file, the
* relocations should lie at the same offset as BSS. The current
* value of reloc_start is the offset from the beginning of the file.
* value of relocstart is the offset from the beginning of the file.
* The following adjustment will convert it to an address in DSpace.
*/
reloctab = (uint32*)
(loadinfo->reloc_start /* File offset to reloc records */
+ loadinfo->dspace /* + Allocated DSpace memory */
+ NXFLAT_DATA_OFFSET /* + Offset for ldso usage */
- loadinfo->ispace_size); /* - File offset to DSpace */
reloctab = (uint32*)(loadinfo->relocstart + loadinfo->dspace - loadinfo->isize);
bvdbg("Relocation table at 0x%p, reloc_count=%d\n",
reloctab, loadinfo->reloc_count);
bvdbg("Relocation table at 0x%p, reloccount=%d\n",
reloctab, loadinfo->reloccount);
/* Now run through the relocation entries. */
for (i=0; i < loadinfo->reloc_count; i++)
for (i=0; i < loadinfo->reloccount; i++)
{
nxflat_reloc(loadinfo, htonl(reloctab[i]));
}
/* Zero the BSS, BRK and stack areas, trashing the relocations
* that lived in the corresponding space in the file. */
memset((void*)(loadinfo->dspace + NXFLAT_DATA_OFFSET + loadinfo->data_size),
0,
(loadinfo->dspace_size - NXFLAT_DATA_OFFSET -
loadinfo->data_size));
/* Zero the BSS area, trashing the relocations that lived in space
* in the file.
*/
memset((void*)(loadinfo->dspace + loadinfo->datasize),
0, loadinfo->bsssize);
return OK;
errout:

View File

@@ -75,16 +75,11 @@ int nxflat_unload(struct nxflat_loadinfo_s *loadinfo)
{
/* Reset the contents of the info structure. */
/* Nothing is allocated */
loadinfo->alloc_start = 0;
loadinfo->alloc_size = 0;
/* Release the memory segments */
if (loadinfo->ispace)
{
munmap((void*)loadinfo->ispace, loadinfo->ispace_size);
munmap((void*)loadinfo->ispace, loadinfo->isize);
loadinfo->ispace = 0;
}

View File

@@ -67,8 +67,6 @@
int nxflat_verifyheader(const struct nxflat_hdr_s *header)
{
uint16 revision;
if (!header)
{
bdbg("NULL NXFLAT header!");
@@ -88,15 +86,6 @@ int nxflat_verifyheader(const struct nxflat_hdr_s *header)
header->h_magic[2], header->h_magic[3]);
return -ENOEXEC;
}
/* Complain a little more if the version does not match. */
revision = ntohs(header->h_rev);
if (revision != NXFLAT_VERSION_CURRENT)
{
bdbg("Unsupported NXFLAT version=%d\n", revision);
return -ENOEXEC;
}
return 0;
return OK;
}

View File

@@ -64,7 +64,7 @@ static void nxflat_dumploadinfo(struct nxflat_loadinfo_s *loadinfo);
#endif
/****************************************************************************
* Private Constant Data
* Private Data
****************************************************************************/
static struct binfmt_s g_nxflatbinfmt =
@@ -103,45 +103,31 @@ static void nxflat_dumpmemory(void *addr, int nbytes)
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_BINFMT)
static void nxflat_dumploadinfo(struct nxflat_loadinfo_s *loadinfo)
{
unsigned long dspace_size =
NXFLAT_DATA_OFFSET +
loadinfo->data_size +
loadinfo->bss_size +
loadinfo->stack_size;
unsigned long dsize = loadinfo->datasize + loadinfo->bsssize;
bdbg("LOAD_INFO:\n");
bdbg(" ISPACE:\n");
bdbg(" ispace: %08lx\n", loadinfo->ispace);
bdbg(" entry_offset: %08lx\n", loadinfo->entry_offset);
bdbg(" ispace_size: %08lx\n", loadinfo->ispace_size);
bdbg(" entryoffs: %08lx\n", loadinfo->entryoffs);
bdbg(" isize: %08lx\n", loadinfo->isize);
bdbg(" DSPACE:\n");
bdbg(" dspace: %08lx\n", loadinfo->dspace);
bdbg(" (ldso): %08x\n", NXFLAT_DATA_OFFSET);
bdbg(" data_size: %08lx\n", loadinfo->data_size);
bdbg(" bss_size: %08lx\n", loadinfo->bss_size);
bdbg(" (pad): %08lx\n", loadinfo->dspace_size - dspace_size);
bdbg(" stack_size: %08lx\n", loadinfo->stack_size);
bdbg(" dspace_size: %08lx\n", loadinfo->dspace_size);
bdbg(" ARGUMENTS:\n");
bdbg(" arg_start: %08lx\n", loadinfo->arg_start);
bdbg(" env_start: %08lx\n", loadinfo->env_start);
bdbg(" env_end: %08lx\n", loadinfo->env_end);
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(" reloc_start: %08lx\n", loadinfo->reloc_start);
bdbg(" reloc_count: %08lx\n", loadinfo->reloc_count);
bdbg(" relocstart: %08lx\n", loadinfo->relocstart);
bdbg(" reloccount: %08lx\n", loadinfo->reloccount);
bdbg(" HANDLES:\n");
bdbg(" filfd: %d\n", loadinfo->filfd);
bdbg(" NXFLT HEADER:");
bdbg(" header: %p\n", loadinfo->header);
bdbg(" ALLOCATIONS:\n");
bdbg(" alloc_start: %08lx\n", loadinfo->alloc_start);
bdbg(" alloc_size: %08lx\n", loadinfo->alloc_size);
}
#else /* CONFIG_XFLAT_DEBUG */
# define nxflat_dumploadinfo(i)
@@ -187,11 +173,14 @@ static int nxflat_loadbinary(struct binary_s *binp)
/* Return the load information */
binp->entrypt = (main_t)(loadinfo.ispace + loadinfo.entry_offset);
binp->picbase = (void*)loadinfo.dspace;
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;
bvdbg("ENTRY CODE:\n");
nxflat_dumpmemory(binp->entrypt, 16*sizeof(unsigned long));
nxflat_dumpmemory(binp->entrypt, 16*sizeof(uint32));
nxflat_uninit(&loadinfo);
return OK;
}

View File

@@ -58,21 +58,24 @@ struct binary_s
{
/* Provided to the loader */
const char *filename; /* Full path to the binary */
const char **argv; /* Argument list */
FAR const char *filename; /* Full path to the binary */
FAR const char **argv; /* Argument list */
/* Provided by the loader (if successful) */
main_t entrypt; /* Entry point into a program module */
void *picbase; /* Address of the allocate .data/.bss space */
FAR void *ispace; /* Memory-mapped, I-space (.text) address */
FAR void *dspace; /* Address of the allocated .data/.bss space */
size_t isize; /* Size of the I-space region (needed for munmap) */
size_t stacksize; /* Size of the stack in bytes (unallocated) */
};
/* This describes one binary format handler */
struct binfmt_s
{
struct binfmt_s *next; /* Supports a singly-linked list */
int (*load)(struct binary_s *bin); /* Verify and load binary into memory */
FAR struct binfmt_s *next; /* Supports a singly-linked list */
int (*load)(FAR struct binary_s *bin); /* Verify and load binary into memory */
};
/****************************************************************************
@@ -87,10 +90,25 @@ extern "C" {
#define EXTERN extern
#endif
/* Register/unregister a binary format */
/* Register a binary format handler */
EXTERN int register_binfmt(struct binfmt_s *binfmt);
EXTERN int unregister_binfmt(struct binfmt_s *binfmt);
EXTERN int register_binfmt(FAR struct binfmt_s *binfmt);
/* Unregister a binary format handler */
EXTERN int unregister_binfmt(FAR struct binfmt_s *binfmt);
/* Load a module into memory */
EXTERN int load_module(const char *filename, FAR struct binary_s *bin);
/* Unload a (non-running) module from memory */
EXTERN int unload_module(FAR const struct binary_s *bin);
/* Execute a module that has been loaded into memory */
EXTERN int exec_module(FAR const struct binary_s *bin);
#undef EXTERN
#if defined(__cplusplus)
@@ -98,3 +116,4 @@ EXTERN int unregister_binfmt(struct binfmt_s *binfmt);
#endif
#endif /* __INCLUDE_NUTTX_BINFMT_H */

View File

@@ -50,71 +50,42 @@
* Public Types
****************************************************************************/
/* When DSpace is allocated, space is reserved at the beginning to
* hold ldso-specific information. The following structure defines
* that information. This structure can be referenced at run-time
* using a negative offset from the PID base address.
*/
struct nxflat_ldso_info
{
uint32 dspace; /* The beginning of ldso DSpace */
};
#define NXFLAT_DATA_OFFSET sizeof(struct nxflat_ldso_info)
/* This struct provides a desciption of the currently loaded
* instantiation of an xflat binary.
/* This struct provides a desciption of the currently loaded instantiation
* of an nxflat binary.
*/
struct nxflat_loadinfo_s
{
/* Instruction Space (ISpace): This region contains the flat
* file header plus everything from the text section. Ideally,
* will have only one text section instance in the system.
/* Instruction Space (ISpace): This region contains the nxflat file header
* plus everything from the text section. Ideally, will have only one mmap'ed
* text section instance in the system for each module.
*/
uint32 ispace; /* Address where hdr/text is loaded */
/* 1st: struct nxflat_hdr_s */
/* 2nd: text section */
uint32 entry_offset; /* Offset from ispace to entry point */
uint32 ispace_size; /* Size of ispace. */
uint32 entryoffs; /* Offset from ispace to entry point */
uint32 isize; /* Size of ispace. */
/* Data Space (DSpace): This region contains all information that
* in referenced as data. There will be a unique instance of
* DSpace for each instance of a process.
/* Data Space (DSpace): This region contains all information that in referenced
* as data (other than the stack which is separately allocated). There will be
* a unique instance of DSpace (and stack) for each instance of a process.
*/
uint32 dspace; /* Address where data/bss/stack/etc. is loaded */
/* 1st: Memory set aside for ldso */
uint32 data_size; /* 2nd: Size of data segment in dspace */
uint32 bss_size; /* 3rd: Size of bss segment in dspace */
/* 4th: Potential padding from relocs/mm/etc. */
uint32 stack_size; /* 5th: Size of stack in dspace */
uint32 dspace_size; /* Size of dspace (may be large than parts) */
/* Program arguments (addresses in dspace) */
uint32 arg_start; /* Beginning of program arguments */
uint32 env_start; /* End of argments, beginning of env */
uint32 env_end; /* End(+4) of env */
uint32 dspace; /* Address where data/bss/etc. is loaded */
uint32 datasize; /* Size of data segment in dspace */
uint32 bsssize; /* Size of bss segment in dspace */
uint32 stacksize; /* Size of stack (not allocated) */
uint32 dsize; /* Size of dspace (may be large than parts) */
/* This is temporary memory where relocation records will be loaded. */
uint32 reloc_start; /* Start of array of struct flat_reloc */
uint32 reloc_count; /* Number of elements in reloc array */
uint32 relocstart; /* Start of array of struct flat_reloc */
uint32 reloccount; /* Number of elements in reloc array */
/* File descriptors */
int filfd; /* Descriptor for the file being loaded */
const struct nxflat_hdr_s *header; /* A reference to the flat file header */
/* At most one memory allocation will be made. These describe that
* allocation.
*/
uint32 alloc_start; /* Start of the allocation */
uint32 alloc_size; /* Size of the allocation */
};
/****************************************************************************

View File

@@ -60,16 +60,12 @@
struct nxflat_hdr_s
{
/* The "magic number identifying the file type. This field should contain
* "NxFT"
* "NxFT". NOTE that there is not other versioning information other than
* this magic number.
*/
char h_magic[4];
/* NXFLAT revision number number. */
uint16 h_rev;
uint16 h_pad;
/* The following fields provide the memory map for the nxflat binary.
*
* h_entry - Offset to the the first executable insruction from
@@ -113,7 +109,7 @@ struct nxflat_hdr_s
uint32 h_relocstart; /* Offset of relocation records */
uint32 h_reloccount; /* Number of relocation records */
/* Imported and exported symbol tables
/* Imported symbol table (NOTE no symbols are exported)
*
* h_importsymbols - Offset to the beginning of an array of imported
* symbol structures (struct nxflat_import). The
@@ -123,39 +119,12 @@ struct nxflat_hdr_s
* the beginning of the file) to the name of
* a symbol string. This string is null-terminated.
* h_importcount - The number of records in the h_exportsymbols array.
* h_exportsymbols - Offset to the beginning of an array of export
* symbol structures (struct nxflat_export). The
* h_importsymbols offset is relative to the
* beginning of the file. Each entry of the
* array contains an uint32 offset (again from
* the beginning of the file) to the name of
* a symbol string. This string is null-terminated.
* h_exportcount - The number of records in the h_exportsymbols array.
*
* NOTE: All of the arrays referenced in the header reside in the
* the .text section. This is possible because these arrays are
* read-only and are only referenced by the load. Residing in text
* also guarantees that only one copy of the array is required.
*
* An exception is the h_importsymbols array with will lie
* in .data. This array contains write-able data and must have
* a single instance per process. NOTE: The string offset contained
* within nxflat_import still refers to strings residing in the text
* section.
*/
uint32 h_importsymbols; /* Offset to list of imported symbols */
uint32 h_exportsymbols; /* Offset to list of exported symbols */
uint16 h_importcount; /* Number of imported symbols */
uint16 h_exportcount; /* Number of imported symbols */
};
/* Legal values for the version field. */
#define NXFLAT_VERSION_NONE 0 /* Invalid NXFLAT version */
#define NXFLAT_VERSION_CURRENT 1 /* Current version */
#define NXFLAT_VERSION_NUM 2
/****************************************************************************
* NXFLAT Relocation types.
*
@@ -193,17 +162,4 @@ struct nxflat_import_s
uint32 i_funcaddress; /* Resolved address of imported function */
};
/****************************************************************************
* Exported symbol type
*
* The exported symbols are an array of the following type. The fields
* in each element are stored in native machine order.
****************************************************************************/
struct nxflat_export_s
{
uint32 x_funcname; /* Offset to name of exported function */
uint32 x_funcaddress; /* Address of exported function */
};
#endif /* __INCLUDE_NXFLAT_H */