Call initializer entry point on start-up; do not create a task

This commit is contained in:
Gregory Nutt 2015-12-11 07:27:45 -06:00
parent 251e8395c7
commit 05cb7a9043
6 changed files with 51 additions and 43 deletions

View file

@ -49,11 +49,7 @@
#include <nuttx/binfmt/elf.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Types
* Public Function Prototypes
****************************************************************************/
/****************************************************************************

View file

@ -9,13 +9,6 @@ config MODULE_ALIGN_LOG2
---help---
Align all sections to this Log2 value: 0->1, 1->2, 2->4, etc.
config MODULE_STACKSIZE
int "Module Stack Size"
default 2048
---help---
This is the default stack size that will be used when starting
module initialization tasks.
config MODULE_BUFFERSIZE
int "Module I/O Buffer Size"
default 128

View file

@ -1,7 +1,7 @@
/****************************************************************************
* binfmt/libmodule/gnu-elf.ld
*
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without

View file

@ -56,6 +56,10 @@
* Public Types
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: libmod_verifyheader
*

View file

@ -68,10 +68,6 @@
# undef CONFIG_MODULE_DUMPBUFFER
#endif
#ifndef CONFIG_MODULE_STACKSIZE
# define CONFIG_MODULE_STACKSIZE 2048
#endif
#ifdef CONFIG_MODULE_DUMPBUFFER
# define mod_dumpbuffer(m,b,n) bvdbgdumpbuffer(m,b,n)
#else
@ -175,18 +171,18 @@ static void mod_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo)
#endif
/****************************************************************************
* Name: mod_dumpentrypt
* Name: mod_dumpinitializer
****************************************************************************/
#ifdef CONFIG_MODULE_DUMPBUFFER
static void mod_dumpentrypt(FAR struct binary_s *binp,
FAR struct mod_loadinfo_s *loadinfo)
static void mod_dumpinitializer(mod_initializer_t initializer,
FAR struct mod_loadinfo_s *loadinfo)
{
mod_dumpbuffer("Entry code", (FAR const uint8_t *)binp->entrypt,
mod_dumpbuffer("Initializer code", (FAR const uint8_t *)initializer,
MIN(loadinfo->textsize - loadinfo->ehdr.e_entry, 512));
}
#else
# define mod_dumpentrypt(b,l)
# define mod_dumpinitializer(b,l)
#endif
/****************************************************************************
@ -201,6 +197,7 @@ static void mod_dumpentrypt(FAR struct binary_s *binp,
static int mod_loadbinary(FAR struct binary_s *binp)
{
struct mod_loadinfo_s loadinfo; /* Contains globals for libmodule */
mod_initializer_t initializer;
int ret;
bvdbg("Loading file: %s\n", binp->filename);
@ -236,8 +233,8 @@ static int mod_loadbinary(FAR struct binary_s *binp)
/* Return the load information */
binp->entrypt = (main_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
binp->stacksize = CONFIG_MODULE_STACKSIZE;
binp->entrypt = NULL;
binp->stacksize = 0;
/* Add the ELF allocation to the alloc[] only if there is no address
* environment. If there is an address environment, it will automatically
@ -263,7 +260,23 @@ static int mod_loadbinary(FAR struct binary_s *binp)
binp->ndtors = loadinfo.ndtors;
#endif
mod_dumpentrypt(binp, &loadinfo);
/* Get the module initializer entry point */
initializer = (mod_initializer_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
if (initialize)
{
mod_dumpinitializer(initializer, &loadinfo);
/* Call the module initializer */
ret = initializer();
if (ret < 0)
{
bdbg("Failed to initialize the module: %d\n", ret);
goto errout_with_load;
}
}
libmod_uninitialize(&loadinfo);
return OK;

View file

@ -95,11 +95,6 @@ struct mod_loadinfo_s
/* elfalloc is the base address of the memory that is allocated to hold the
* module image.
*
* If CONFIG_ARCH_ADDRENV=n, elfalloc will be allocated using kmm_malloc() (or
* kmm_zalloc()). If CONFIG_ARCH_ADDRENV-y, then elfalloc will be allocated using
* up_addrenv_create(). In either case, there will be a unique instance
* of elfalloc (and stack) for each instance of a process.
*
* The alloc[] array in struct binary_s will hold memory that persists after
* the module has been loaded.
*/
@ -124,25 +119,32 @@ struct mod_loadinfo_s
uint16_t ndtors; /* Number of destructors */
#endif
/* Address environment.
*
* addrenv - This is the handle created by up_addrenv_create() that can be
* used to manage the tasks address space.
* oldenv - This is a value returned by up_addrenv_select() that must be
* used to restore the current address environment.
*/
#ifdef CONFIG_ARCH_ADDRENV
group_addrenv_t addrenv; /* Task group address environment */
save_addrenv_t oldenv; /* Saved address environment */
#endif
uint16_t symtabidx; /* Symbol table section index */
uint16_t strtabidx; /* String table section index */
uint16_t buflen; /* size of iobuffer[] */
int filfd; /* Descriptor for the file being loaded */
};
/* A NuttX module is expected to export a function called module_initialize()
* that has the following function prototype. This function should appear as
* the entry point in the ELF module file and will be called bythe binfmt
* logic after the module has been loaded into kernel memory.
*
* As an alternative using GCC, the module may mark a function with the
* "constructor" attribute and the module initializer will be called along
* with any other C++ constructors. The "destructor" attribute may also
* be used to mark an module uninitialization function.
*
* Input Parameters:
* None
*
* Returned Value:
* Zero (OK) on success; a negated errno value on any failure to
* initialize the module.
*/
typedef CODE int (*mod_initializer_t)(void);
/****************************************************************************
* Public Functions
****************************************************************************/