mirror of
https://github.com/apache/nuttx.git
synced 2026-05-29 20:56:47 +08:00
binfmt: Remove filename/exports/nexports from binary_s
to simplify the life cycle management Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
committed by
Masayuki Ishikawa
parent
cf78a5b6cf
commit
bebdbc5c87
@@ -57,7 +57,6 @@ int binfmt_dumpmodule(FAR const struct binary_s *bin)
|
|||||||
if (bin)
|
if (bin)
|
||||||
{
|
{
|
||||||
binfo("Module:\n");
|
binfo("Module:\n");
|
||||||
binfo(" filename: %s\n", bin->filename);
|
|
||||||
binfo(" entrypt: %p\n", bin->entrypt);
|
binfo(" entrypt: %p\n", bin->entrypt);
|
||||||
binfo(" mapped: %p size=%zd\n", bin->mapped, bin->mapsize);
|
binfo(" mapped: %p size=%zd\n", bin->mapped, bin->mapsize);
|
||||||
binfo(" alloc: %p %p %p\n", bin->alloc[0],
|
binfo(" alloc: %p %p %p\n", bin->alloc[0],
|
||||||
|
|||||||
@@ -86,15 +86,9 @@ int exec_spawn(FAR const char *filename, FAR char * const *argv,
|
|||||||
goto errout;
|
goto errout;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize the binary structure */
|
|
||||||
|
|
||||||
bin->filename = filename;
|
|
||||||
bin->exports = exports;
|
|
||||||
bin->nexports = nexports;
|
|
||||||
|
|
||||||
/* Load the module into memory */
|
/* Load the module into memory */
|
||||||
|
|
||||||
ret = load_module(bin);
|
ret = load_module(bin, filename, exports, nexports);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
berr("ERROR: Failed to load program '%s': %d\n", filename, ret);
|
berr("ERROR: Failed to load program '%s': %d\n", filename, ret);
|
||||||
@@ -127,7 +121,7 @@ int exec_spawn(FAR const char *filename, FAR char * const *argv,
|
|||||||
|
|
||||||
/* Then start the module */
|
/* Then start the module */
|
||||||
|
|
||||||
pid = exec_module(bin, argv);
|
pid = exec_module(bin, filename, argv);
|
||||||
if (pid < 0)
|
if (pid < 0)
|
||||||
{
|
{
|
||||||
ret = pid;
|
ret = pid;
|
||||||
|
|||||||
@@ -111,7 +111,8 @@ static void exec_ctors(FAR void *arg)
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int exec_module(FAR const struct binary_s *binp, FAR char * const *argv)
|
int exec_module(FAR const struct binary_s *binp,
|
||||||
|
FAR const char *filename, FAR char * const *argv)
|
||||||
{
|
{
|
||||||
FAR struct task_tcb_s *tcb;
|
FAR struct task_tcb_s *tcb;
|
||||||
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
||||||
@@ -129,7 +130,7 @@ int exec_module(FAR const struct binary_s *binp, FAR char * const *argv)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
binfo("Executing %s\n", binp->filename);
|
binfo("Executing %s\n", filename);
|
||||||
|
|
||||||
/* Allocate a TCB for the new task. */
|
/* Allocate a TCB for the new task. */
|
||||||
|
|
||||||
@@ -167,7 +168,7 @@ int exec_module(FAR const struct binary_s *binp, FAR char * const *argv)
|
|||||||
|
|
||||||
/* Initialize the task */
|
/* Initialize the task */
|
||||||
|
|
||||||
ret = nxtask_init(tcb, binp->filename, binp->priority,
|
ret = nxtask_init(tcb, filename, binp->priority,
|
||||||
NULL, binp->stacksize, binp->entrypt, argv);
|
NULL, binp->stacksize, binp->entrypt, argv);
|
||||||
binfmt_freeargv(argv);
|
binfmt_freeargv(argv);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
|
|||||||
+14
-23
@@ -86,7 +86,7 @@ static int load_default_priority(FAR struct binary_s *bin)
|
|||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Load a module into memory, bind it to an exported symbol take, and
|
* Load a module into memory, bind it to an exported symbol take, and
|
||||||
* prep the module for execution. bin->filename is known to be an absolute
|
* prep the module for execution. filename is known to be an absolute
|
||||||
* path to the file to be loaded.
|
* path to the file to be loaded.
|
||||||
*
|
*
|
||||||
* Returned Value:
|
* Returned Value:
|
||||||
@@ -95,12 +95,13 @@ static int load_default_priority(FAR struct binary_s *bin)
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static int load_absmodule(FAR struct binary_s *bin)
|
static int load_absmodule(FAR struct binary_s *bin, FAR const char *filename,
|
||||||
|
FAR const struct symtab_s *exports, int nexports)
|
||||||
{
|
{
|
||||||
FAR struct binfmt_s *binfmt;
|
FAR struct binfmt_s *binfmt;
|
||||||
int ret = -ENOENT;
|
int ret = -ENOENT;
|
||||||
|
|
||||||
binfo("Loading %s\n", bin->filename);
|
binfo("Loading %s\n", filename);
|
||||||
|
|
||||||
/* Disabling pre-emption should be sufficient protection while accessing
|
/* Disabling pre-emption should be sufficient protection while accessing
|
||||||
* the list of registered binary format handlers.
|
* the list of registered binary format handlers.
|
||||||
@@ -117,12 +118,12 @@ static int load_absmodule(FAR struct binary_s *bin)
|
|||||||
{
|
{
|
||||||
/* Use this handler to try to load the format */
|
/* Use this handler to try to load the format */
|
||||||
|
|
||||||
ret = binfmt->load(bin);
|
ret = binfmt->load(bin, filename, exports, nexports);
|
||||||
if (ret == OK)
|
if (ret == OK)
|
||||||
{
|
{
|
||||||
/* Successfully loaded -- break out with ret == 0 */
|
/* Successfully loaded -- break out with ret == 0 */
|
||||||
|
|
||||||
binfo("Successfully loaded module %s\n", bin->filename);
|
binfo("Successfully loaded module %s\n", filename);
|
||||||
|
|
||||||
/* Save the unload method for use by unload_module */
|
/* Save the unload method for use by unload_module */
|
||||||
|
|
||||||
@@ -154,14 +155,15 @@ static int load_absmodule(FAR struct binary_s *bin)
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int load_module(FAR struct binary_s *bin)
|
int load_module(FAR struct binary_s *bin, FAR const char *filename,
|
||||||
|
FAR const struct symtab_s *exports, int nexports)
|
||||||
{
|
{
|
||||||
int ret = -EINVAL;
|
int ret = -EINVAL;
|
||||||
|
|
||||||
/* Verify that we were provided something to work with */
|
/* Verify that we were provided something to work with */
|
||||||
|
|
||||||
#ifdef CONFIG_DEBUG_FEATURES
|
#ifdef CONFIG_DEBUG_FEATURES
|
||||||
if (bin && bin->filename)
|
if (bin && filename)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
/* Set the default priority of the new program. */
|
/* Set the default priority of the new program. */
|
||||||
@@ -177,16 +179,12 @@ int load_module(FAR struct binary_s *bin)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef CONFIG_LIB_ENVPATH
|
#ifdef CONFIG_LIB_ENVPATH
|
||||||
if (bin->filename[0] != '/')
|
if (filename[0] != '/')
|
||||||
{
|
{
|
||||||
FAR const char *relpath;
|
|
||||||
FAR char *fullpath;
|
FAR char *fullpath;
|
||||||
ENVPATH_HANDLE handle;
|
ENVPATH_HANDLE handle;
|
||||||
|
|
||||||
/* Set aside the relative path */
|
ret = -ENOENT;
|
||||||
|
|
||||||
relpath = bin->filename;
|
|
||||||
ret = -ENOENT;
|
|
||||||
|
|
||||||
/* Initialize to traverse the PATH variable */
|
/* Initialize to traverse the PATH variable */
|
||||||
|
|
||||||
@@ -195,12 +193,11 @@ int load_module(FAR struct binary_s *bin)
|
|||||||
{
|
{
|
||||||
/* Get the next absolute file path */
|
/* Get the next absolute file path */
|
||||||
|
|
||||||
while ((fullpath = envpath_next(handle, relpath)) != NULL)
|
while ((fullpath = envpath_next(handle, filename)) != NULL)
|
||||||
{
|
{
|
||||||
/* Try to load the file at this path */
|
/* Try to load the file at this path */
|
||||||
|
|
||||||
bin->filename = fullpath;
|
ret = load_absmodule(bin, fullpath, exports, nexports);
|
||||||
ret = load_absmodule(bin);
|
|
||||||
|
|
||||||
/* Free the allocated fullpath */
|
/* Free the allocated fullpath */
|
||||||
|
|
||||||
@@ -218,12 +215,6 @@ int load_module(FAR struct binary_s *bin)
|
|||||||
|
|
||||||
envpath_release(handle);
|
envpath_release(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Restore the relative path. This is not needed for anything
|
|
||||||
* but debug output after the file has been loaded.
|
|
||||||
*/
|
|
||||||
|
|
||||||
bin->filename = relpath;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
@@ -232,7 +223,7 @@ int load_module(FAR struct binary_s *bin)
|
|||||||
* be loaded.
|
* be loaded.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
ret = load_absmodule(bin);
|
ret = load_absmodule(bin, filename, exports, nexports);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-6
@@ -44,7 +44,10 @@
|
|||||||
* Private Function Prototypes
|
* Private Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static int builtin_loadbinary(FAR struct binary_s *binp);
|
static int builtin_loadbinary(FAR struct binary_s *binp,
|
||||||
|
FAR const char *filename,
|
||||||
|
FAR const struct symtab_s *exports,
|
||||||
|
int nexports);
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Data
|
* Private Data
|
||||||
@@ -69,22 +72,24 @@ static struct binfmt_s g_builtin_binfmt =
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static int builtin_loadbinary(struct binary_s *binp)
|
static int builtin_loadbinary(FAR struct binary_s *binp,
|
||||||
|
FAR const char *filename,
|
||||||
|
FAR const struct symtab_s *exports,
|
||||||
|
int nexports)
|
||||||
{
|
{
|
||||||
FAR const char *filename;
|
|
||||||
FAR const struct builtin_s *builtin;
|
FAR const struct builtin_s *builtin;
|
||||||
int fd;
|
int fd;
|
||||||
int index;
|
int index;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
binfo("Loading file: %s\n", binp->filename);
|
binfo("Loading file: %s\n", filename);
|
||||||
|
|
||||||
/* Open the binary file for reading (only) */
|
/* Open the binary file for reading (only) */
|
||||||
|
|
||||||
fd = nx_open(binp->filename, O_RDONLY);
|
fd = nx_open(filename, O_RDONLY);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
{
|
{
|
||||||
berr("ERROR: Failed to open binary %s: %d\n", binp->filename, fd);
|
berr("ERROR: Failed to open binary %s: %d\n", filename, fd);
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-5
@@ -68,7 +68,10 @@
|
|||||||
* Private Function Prototypes
|
* Private Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static int elf_loadbinary(FAR struct binary_s *binp);
|
static int elf_loadbinary(FAR struct binary_s *binp,
|
||||||
|
FAR const char *filename,
|
||||||
|
FAR const struct symtab_s *exports,
|
||||||
|
int nexports);
|
||||||
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
|
||||||
static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo);
|
static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo);
|
||||||
#endif
|
#endif
|
||||||
@@ -206,16 +209,19 @@ static void elf_dumpentrypt(FAR struct binary_s *binp,
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static int elf_loadbinary(FAR struct binary_s *binp)
|
static int elf_loadbinary(FAR struct binary_s *binp,
|
||||||
|
FAR const char *filename,
|
||||||
|
FAR const struct symtab_s *exports,
|
||||||
|
int nexports)
|
||||||
{
|
{
|
||||||
struct elf_loadinfo_s loadinfo; /* Contains globals for libelf */
|
struct elf_loadinfo_s loadinfo; /* Contains globals for libelf */
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
binfo("Loading file: %s\n", binp->filename);
|
binfo("Loading file: %s\n", filename);
|
||||||
|
|
||||||
/* Initialize the ELF library to load the program binary. */
|
/* Initialize the ELF library to load the program binary. */
|
||||||
|
|
||||||
ret = elf_init(binp->filename, &loadinfo);
|
ret = elf_init(filename, &loadinfo);
|
||||||
elf_dumploadinfo(&loadinfo);
|
elf_dumploadinfo(&loadinfo);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
{
|
{
|
||||||
@@ -235,7 +241,7 @@ static int elf_loadbinary(FAR struct binary_s *binp)
|
|||||||
|
|
||||||
/* Bind the program to the exported symbol table */
|
/* Bind the program to the exported symbol table */
|
||||||
|
|
||||||
ret = elf_bind(&loadinfo, binp->exports, binp->nexports);
|
ret = elf_bind(&loadinfo, exports, nexports);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
{
|
{
|
||||||
berr("Failed to bind symbols program binary: %d\n", ret);
|
berr("Failed to bind symbols program binary: %d\n", ret);
|
||||||
|
|||||||
+11
-5
@@ -65,7 +65,10 @@
|
|||||||
* Private Function Prototypes
|
* Private Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static int nxflat_loadbinary(FAR struct binary_s *binp);
|
static int nxflat_loadbinary(FAR struct binary_s *binp,
|
||||||
|
FAR const char *filename,
|
||||||
|
FAR const struct symtab_s *exports,
|
||||||
|
int nexports);
|
||||||
static int nxflat_unloadbinary(FAR struct binary_s *binp);
|
static int nxflat_unloadbinary(FAR struct binary_s *binp);
|
||||||
|
|
||||||
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
|
||||||
@@ -137,16 +140,19 @@ static void nxflat_dumploadinfo(FAR struct nxflat_loadinfo_s *loadinfo)
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static int nxflat_loadbinary(FAR struct binary_s *binp)
|
static int nxflat_loadbinary(FAR struct binary_s *binp,
|
||||||
|
FAR const char *filename,
|
||||||
|
FAR const struct symtab_s *exports,
|
||||||
|
int nexports)
|
||||||
{
|
{
|
||||||
struct nxflat_loadinfo_s loadinfo; /* Contains globals for libnxflat */
|
struct nxflat_loadinfo_s loadinfo; /* Contains globals for libnxflat */
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
binfo("Loading file: %s\n", binp->filename);
|
binfo("Loading file: %s\n", filename);
|
||||||
|
|
||||||
/* Initialize the xflat library to load the program binary. */
|
/* Initialize the xflat library to load the program binary. */
|
||||||
|
|
||||||
ret = nxflat_init(binp->filename, &loadinfo);
|
ret = nxflat_init(filename, &loadinfo);
|
||||||
nxflat_dumploadinfo(&loadinfo);
|
nxflat_dumploadinfo(&loadinfo);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
{
|
{
|
||||||
@@ -166,7 +172,7 @@ static int nxflat_loadbinary(FAR struct binary_s *binp)
|
|||||||
|
|
||||||
/* Bind the program to the exported symbol table */
|
/* Bind the program to the exported symbol table */
|
||||||
|
|
||||||
ret = nxflat_bind(&loadinfo, binp->exports, binp->nexports);
|
ret = nxflat_bind(&loadinfo, exports, nexports);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
{
|
{
|
||||||
berr("Failed to bind symbols program binary: %d\n", ret);
|
berr("Failed to bind symbols program binary: %d\n", ret);
|
||||||
|
|||||||
@@ -61,12 +61,6 @@ typedef FAR void (*binfmt_dtor_t)(void);
|
|||||||
struct symtab_s;
|
struct symtab_s;
|
||||||
struct binary_s
|
struct binary_s
|
||||||
{
|
{
|
||||||
/* Information provided to the loader to load and bind a module */
|
|
||||||
|
|
||||||
FAR const char *filename; /* Full path to the binary to be loaded (See NOTE 1 above) */
|
|
||||||
FAR const struct symtab_s *exports; /* Table of exported symbols */
|
|
||||||
int nexports; /* The number of symbols in exports[] */
|
|
||||||
|
|
||||||
/* Information provided from the loader (if successful) describing the
|
/* Information provided from the loader (if successful) describing the
|
||||||
* resources used by the loaded module.
|
* resources used by the loaded module.
|
||||||
*/
|
*/
|
||||||
@@ -118,7 +112,10 @@ struct binfmt_s
|
|||||||
|
|
||||||
/* Verify and load binary into memory */
|
/* Verify and load binary into memory */
|
||||||
|
|
||||||
CODE int (*load)(FAR struct binary_s *bin);
|
CODE int (*load)(FAR struct binary_s *bin,
|
||||||
|
FAR const char *filename,
|
||||||
|
FAR const struct symtab_s *exports,
|
||||||
|
int nexports);
|
||||||
|
|
||||||
/* Unload module callback */
|
/* Unload module callback */
|
||||||
|
|
||||||
@@ -192,7 +189,8 @@ int unregister_binfmt(FAR struct binfmt_s *binfmt);
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int load_module(FAR struct binary_s *bin);
|
int load_module(FAR struct binary_s *bin, FAR const char *filename,
|
||||||
|
FAR const struct symtab_s *exports, int nexports);
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: unload_module
|
* Name: unload_module
|
||||||
@@ -228,7 +226,8 @@ int unload_module(FAR struct binary_s *bin);
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int exec_module(FAR const struct binary_s *binp, FAR char * const *argv);
|
int exec_module(FAR const struct binary_s *binp,
|
||||||
|
FAR const char *filename, FAR char * const *argv);
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: exec
|
* Name: exec
|
||||||
|
|||||||
Reference in New Issue
Block a user