2009-06-18 00:28:50 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/nxflat.c
|
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2009-06-18 00:28:50 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-18 00:28:50 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2009-06-18 00:28:50 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-16 01:06:59 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
2009-06-18 00:28:50 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <nxflat.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2019-03-12 22:48:29 +08:00
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2012-10-25 04:19:44 +08:00
|
|
|
#include <nuttx/binfmt/binfmt.h>
|
|
|
|
#include <nuttx/binfmt/nxflat.h>
|
2009-06-18 00:28:50 +08:00
|
|
|
|
2009-06-18 05:15:31 +08:00
|
|
|
#ifdef CONFIG_NXFLAT
|
|
|
|
|
2009-06-18 00:28:50 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-22 16:24:15 +08:00
|
|
|
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT
|
|
|
|
* have to be defined or CONFIG_NXFLAT_DUMPBUFFER does nothing.
|
2009-06-26 01:44:35 +08:00
|
|
|
*/
|
|
|
|
|
2016-06-12 01:50:18 +08:00
|
|
|
#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT)
|
2009-06-26 05:59:30 +08:00
|
|
|
# undef CONFIG_NXFLAT_DUMPBUFFER
|
2009-06-26 01:44:35 +08:00
|
|
|
#endif
|
|
|
|
|
2009-06-26 05:59:30 +08:00
|
|
|
#ifdef CONFIG_NXFLAT_DUMPBUFFER
|
2016-06-12 01:59:51 +08:00
|
|
|
# define nxflat_dumpbuffer(m,b,n) binfodumpbuffer(m,b,n)
|
2009-06-26 01:44:35 +08:00
|
|
|
#else
|
|
|
|
# define nxflat_dumpbuffer(m,b,n)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
# define MIN(a,b) (a < b ? a : b)
|
|
|
|
#endif
|
|
|
|
|
2009-06-18 00:28:50 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-03-12 22:48:29 +08:00
|
|
|
static int nxflat_loadbinary(FAR struct binary_s *binp);
|
|
|
|
static int nxflat_unloadbinary(FAR struct binary_s *binp);
|
|
|
|
|
2016-06-12 04:14:08 +08:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
|
2019-03-12 22:48:29 +08:00
|
|
|
static void nxflat_dumploadinfo(FAR struct nxflat_loadinfo_s *loadinfo);
|
2009-06-18 00:28:50 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2009-06-18 02:45:48 +08:00
|
|
|
* Private Data
|
2009-06-18 00:28:50 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static struct binfmt_s g_nxflatbinfmt =
|
|
|
|
{
|
|
|
|
NULL, /* next */
|
|
|
|
nxflat_loadbinary, /* load */
|
2019-03-12 22:48:29 +08:00
|
|
|
nxflat_unloadbinary, /* unload */
|
2009-06-18 00:28:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_dumploadinfo
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-12 04:14:08 +08:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
|
2019-03-12 22:48:29 +08:00
|
|
|
static void nxflat_dumploadinfo(FAR struct nxflat_loadinfo_s *loadinfo)
|
2009-06-18 00:28:50 +08:00
|
|
|
{
|
2009-06-18 02:45:48 +08:00
|
|
|
unsigned long dsize = loadinfo->datasize + loadinfo->bsssize;
|
2009-06-18 00:28:50 +08:00
|
|
|
|
2019-08-16 04:41:42 +08:00
|
|
|
binfo("LOAD_INFO:\n");
|
|
|
|
binfo(" ISPACE:\n");
|
|
|
|
binfo(" ispace: %08lx\n", loadinfo->ispace);
|
|
|
|
binfo(" entryoffs: %08lx\n", loadinfo->entryoffs);
|
|
|
|
binfo(" isize: %08lx\n", loadinfo->isize);
|
|
|
|
|
|
|
|
binfo(" DSPACE:\n");
|
|
|
|
binfo(" dspace: %08lx\n", loadinfo->dspace);
|
2020-01-12 00:10:49 +08:00
|
|
|
|
2009-06-26 01:44:35 +08:00
|
|
|
if (loadinfo->dspace != NULL)
|
2009-06-25 22:38:42 +08:00
|
|
|
{
|
2019-08-16 04:41:42 +08:00
|
|
|
binfo(" crefs: %d\n", loadinfo->dspace->crefs);
|
|
|
|
binfo(" region: %08lx\n", loadinfo->dspace->region);
|
2009-06-25 22:38:42 +08:00
|
|
|
}
|
2020-01-12 00:10:49 +08:00
|
|
|
|
2019-08-16 04:41:42 +08:00
|
|
|
binfo(" datasize: %08lx\n", loadinfo->datasize);
|
|
|
|
binfo(" bsssize: %08lx\n", loadinfo->bsssize);
|
|
|
|
binfo(" (pad): %08lx\n", loadinfo->dsize - dsize);
|
|
|
|
binfo(" stacksize: %08lx\n", loadinfo->stacksize);
|
|
|
|
binfo(" dsize: %08lx\n", loadinfo->dsize);
|
|
|
|
|
|
|
|
binfo(" RELOCS:\n");
|
|
|
|
binfo(" relocstart: %08lx\n", loadinfo->relocstart);
|
|
|
|
binfo(" reloccount: %d\n", loadinfo->reloccount);
|
|
|
|
|
|
|
|
binfo(" HANDLES:\n");
|
|
|
|
binfo(" filfd: %d\n", loadinfo->filfd);
|
2009-06-18 00:28:50 +08:00
|
|
|
}
|
2009-06-22 03:36:07 +08:00
|
|
|
#else
|
2009-06-18 00:28:50 +08:00
|
|
|
# define nxflat_dumploadinfo(i)
|
2009-06-22 03:36:07 +08:00
|
|
|
#endif
|
2009-06-18 00:28:50 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_loadbinary
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Verify that the file is an NXFLAT binary and, if so, load the NXFLAT
|
|
|
|
* binary into memory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-03-12 22:48:29 +08:00
|
|
|
static int nxflat_loadbinary(FAR struct binary_s *binp)
|
2009-06-18 00:28:50 +08:00
|
|
|
{
|
|
|
|
struct nxflat_loadinfo_s loadinfo; /* Contains globals for libnxflat */
|
|
|
|
int ret;
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
binfo("Loading file: %s\n", binp->filename);
|
2009-06-18 00:28:50 +08:00
|
|
|
|
|
|
|
/* Initialize the xflat library to load the program binary. */
|
|
|
|
|
2009-06-26 05:59:30 +08:00
|
|
|
ret = nxflat_init(binp->filename, &loadinfo);
|
2009-06-18 00:28:50 +08:00
|
|
|
nxflat_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
berr("Failed to initialize for load of NXFLAT program: %d\n", ret);
|
2009-06-26 06:25:30 +08:00
|
|
|
goto errout;
|
2009-06-18 00:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the program binary */
|
|
|
|
|
|
|
|
ret = nxflat_load(&loadinfo);
|
|
|
|
nxflat_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
berr("Failed to load NXFLAT program binary: %d\n", ret);
|
2009-06-26 06:25:30 +08:00
|
|
|
goto errout_with_init;
|
2009-06-18 00:28:50 +08:00
|
|
|
}
|
|
|
|
|
2009-06-21 21:41:39 +08:00
|
|
|
/* Bind the program to the exported symbol table */
|
|
|
|
|
|
|
|
ret = nxflat_bind(&loadinfo, binp->exports, binp->nexports);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
berr("Failed to bind symbols program binary: %d\n", ret);
|
2009-06-26 06:25:30 +08:00
|
|
|
goto errout_with_load;
|
2009-06-21 21:41:39 +08:00
|
|
|
}
|
|
|
|
|
2012-10-30 03:32:05 +08:00
|
|
|
/* Return the load information. By convention, D-space address
|
|
|
|
* space is stored as the first allocated memory.
|
|
|
|
*/
|
2009-06-18 00:28:50 +08:00
|
|
|
|
2009-06-18 02:45:48 +08:00
|
|
|
binp->entrypt = (main_t)(loadinfo.ispace + loadinfo.entryoffs);
|
2015-10-09 09:20:17 +08:00
|
|
|
binp->mapped = (FAR void *)loadinfo.ispace;
|
2012-10-30 03:32:05 +08:00
|
|
|
binp->mapsize = loadinfo.isize;
|
2009-06-18 02:45:48 +08:00
|
|
|
binp->stacksize = loadinfo.stacksize;
|
2009-06-18 00:28:50 +08:00
|
|
|
|
2012-12-20 01:54:26 +08:00
|
|
|
/* Add the ELF allocation to the alloc[] only if there is no address
|
2020-02-23 16:50:23 +08:00
|
|
|
* environment. If there is an address environment, it will automatically
|
2012-12-20 01:54:26 +08:00
|
|
|
* be freed when the function exits
|
|
|
|
*
|
|
|
|
* REVISIT: If the module is loaded then unloaded, wouldn't this cause
|
|
|
|
* a memory leak?
|
|
|
|
*/
|
|
|
|
|
2014-08-24 20:42:11 +08:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-20 01:54:26 +08:00
|
|
|
# warning "REVISIT"
|
|
|
|
#else
|
2015-10-09 09:20:17 +08:00
|
|
|
binp->alloc[0] = (FAR void *)loadinfo.dspace;
|
2012-12-20 01:54:26 +08:00
|
|
|
#endif
|
|
|
|
|
2014-08-24 20:42:11 +08:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2014-08-27 02:16:05 +08:00
|
|
|
/* Save the address environment in the binfmt structure. This will be
|
|
|
|
* needed when the module is executed.
|
2012-12-20 01:54:26 +08:00
|
|
|
*/
|
|
|
|
|
2014-08-27 02:16:05 +08:00
|
|
|
up_addrenv_clone(&loadinfo.addrenv, &binp->addrenv);
|
2012-12-20 01:54:26 +08:00
|
|
|
#endif
|
|
|
|
|
2015-10-09 09:20:17 +08:00
|
|
|
nxflat_dumpbuffer("Entry code", (FAR const uint8_t *)binp->entrypt,
|
2012-10-30 03:32:05 +08:00
|
|
|
MIN(loadinfo.isize - loadinfo.entryoffs, 512));
|
2009-06-26 06:25:30 +08:00
|
|
|
|
2009-06-18 00:28:50 +08:00
|
|
|
nxflat_uninit(&loadinfo);
|
|
|
|
return OK;
|
2009-06-26 06:25:30 +08:00
|
|
|
|
|
|
|
errout_with_load:
|
|
|
|
nxflat_unload(&loadinfo);
|
|
|
|
errout_with_init:
|
|
|
|
nxflat_uninit(&loadinfo);
|
|
|
|
errout:
|
|
|
|
return ret;
|
2009-06-18 00:28:50 +08:00
|
|
|
}
|
|
|
|
|
2019-03-12 22:48:29 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_unloadbinary
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Verify that the file is an NXFLAT binary and, if so, load the NXFLAT
|
|
|
|
* binary into memory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nxflat_unloadbinary(FAR struct binary_s *binp)
|
|
|
|
{
|
|
|
|
FAR struct dspace_s *dspace = (FAR struct dspace_s *)binp->alloc[0];
|
|
|
|
|
2019-03-13 00:09:03 +08:00
|
|
|
/* Check if this is the last reference to dspace. It may still be needed
|
|
|
|
* by other threads. In that case, it must persist after this thread
|
|
|
|
* terminates.
|
|
|
|
*/
|
2019-03-12 22:48:29 +08:00
|
|
|
|
2019-03-13 00:09:03 +08:00
|
|
|
if (dspace != NULL && dspace->crefs == 1)
|
|
|
|
{
|
|
|
|
/* Free the dspace region */
|
2019-03-12 23:42:04 +08:00
|
|
|
|
2019-03-13 00:09:03 +08:00
|
|
|
kumm_free(dspace->region);
|
|
|
|
dspace->region = NULL;
|
2019-03-12 22:48:29 +08:00
|
|
|
|
2019-03-13 00:09:03 +08:00
|
|
|
/* Mark alloc[0] (dspace) as freed */
|
2019-03-12 22:48:29 +08:00
|
|
|
|
2019-03-13 04:22:52 +08:00
|
|
|
binp->alloc[0] = NULL;
|
2019-03-12 22:48:29 +08:00
|
|
|
|
2019-03-13 00:09:03 +08:00
|
|
|
/* The reference count will be decremented to zero and the dspace
|
2020-05-09 22:04:45 +08:00
|
|
|
* container will be freed in sched/nxsched_release_tcb.c
|
2019-03-13 00:09:03 +08:00
|
|
|
*/
|
2019-03-12 23:42:04 +08:00
|
|
|
}
|
2019-03-12 22:48:29 +08:00
|
|
|
|
2020-01-12 00:10:49 +08:00
|
|
|
return OK;
|
2019-03-12 22:48:29 +08:00
|
|
|
}
|
|
|
|
|
2015-10-03 07:43:18 +08:00
|
|
|
/****************************************************************************
|
2009-06-18 00:28:50 +08:00
|
|
|
* Public Functions
|
2015-10-03 07:43:18 +08:00
|
|
|
****************************************************************************/
|
2009-06-18 00:28:50 +08:00
|
|
|
|
2015-10-03 07:43:18 +08:00
|
|
|
/****************************************************************************
|
2009-06-18 00:28:50 +08:00
|
|
|
* Name: nxflat_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2018-08-24 20:10:50 +08:00
|
|
|
* In order to use the NxFLAT binary format, this function must be called
|
|
|
|
* during system initialization to register the NXFLAT binary
|
|
|
|
* format.
|
2009-06-18 00:28:50 +08:00
|
|
|
*
|
2009-06-18 04:25:27 +08:00
|
|
|
* 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.
|
|
|
|
*
|
2015-10-03 07:43:18 +08:00
|
|
|
****************************************************************************/
|
2009-06-18 00:28:50 +08:00
|
|
|
|
|
|
|
int nxflat_initialize(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Register ourselves as a binfmt loader */
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
binfo("Registering NXFLAT\n");
|
2009-06-18 00:28:50 +08:00
|
|
|
ret = register_binfmt(&g_nxflatbinfmt);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
berr("Failed to register binfmt: %d\n", ret);
|
2009-06-18 00:28:50 +08:00
|
|
|
}
|
2020-01-12 00:10:49 +08:00
|
|
|
|
2009-06-18 00:28:50 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_uninitialize
|
2009-06-18 04:25:27 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unregister the NXFLAT binary loader
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2009-06-18 00:28:50 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void nxflat_uninitialize(void)
|
|
|
|
{
|
2020-01-03 00:49:34 +08:00
|
|
|
unregister_binfmt(&g_nxflatbinfmt);
|
2009-06-18 00:28:50 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 05:15:31 +08:00
|
|
|
#endif /* CONFIG_NXFLAT */
|