2009-06-17 00:08:59 +02:00
|
|
|
/****************************************************************************
|
2009-06-17 18:28:50 +02:00
|
|
|
* binfmt/libnxflat/libnxflat_load.c
|
2009-06-17 00:08:59 +02:00
|
|
|
*
|
2024-09-09 09:12:59 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-03 14:47:23 +01: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-17 00:08:59 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-17 00:08:59 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01: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-17 00:08:59 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-15 18:06:59 +01:00
|
|
|
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2009-06-17 00:08:59 +02:00
|
|
|
#include <sys/types.h>
|
2009-06-17 18:28:50 +02:00
|
|
|
#include <sys/mman.h>
|
2012-12-19 18:54:26 +01:00
|
|
|
|
2020-11-25 12:17:19 +01:00
|
|
|
#include <inttypes.h>
|
2009-12-15 18:06:59 +01:00
|
|
|
#include <stdint.h>
|
2009-06-17 18:28:50 +02:00
|
|
|
#include <stdlib.h>
|
2009-06-17 00:08:59 +02:00
|
|
|
#include <nxflat.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
2009-06-17 18:28:50 +02:00
|
|
|
|
2009-06-17 00:08:59 +02:00
|
|
|
#include <arpa/inet.h>
|
2012-12-19 18:54:26 +01:00
|
|
|
|
2012-10-24 22:19:44 +02:00
|
|
|
#include <nuttx/binfmt/nxflat.h>
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
#include "libnxflat.h"
|
|
|
|
|
2009-06-17 00:08:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_load
|
2009-06-23 00:12:19 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Loads the binary specified by nxflat_init into memory, mapping
|
|
|
|
* the I-space executable regions, allocating the D-Space region,
|
2014-05-07 21:47:52 +02:00
|
|
|
* and initializing the data segment (relocation information is
|
2009-06-23 00:12:19 +02:00
|
|
|
* temporarily loaded into the BSS region. BSS will be cleared
|
|
|
|
* by nxflat_bind() after the relocation data has been processed).
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
2009-06-17 00:08:59 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
int nxflat_load(struct nxflat_loadinfo_s *loadinfo)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2009-12-15 18:06:59 +01:00
|
|
|
off_t doffset; /* Offset to .data in the NXFLAT file */
|
|
|
|
uint32_t dreadsize; /* Total number of bytes of .data to be read */
|
|
|
|
uint32_t relocsize; /* Memory needed to hold relocations */
|
|
|
|
uint32_t extrasize; /* MAX(BSS size, relocsize) */
|
|
|
|
int ret = OK;
|
2009-06-17 18:28:50 +02:00
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
/* 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
|
2020-11-25 12:14:21 +01:00
|
|
|
* 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
|
2009-06-25 02:05:11 +02:00
|
|
|
*
|
|
|
|
* This is the amount of memory that we have to have to hold the
|
|
|
|
* relocations.
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
2009-06-25 02:05:11 +02:00
|
|
|
relocsize = loadinfo->reloccount * sizeof(struct nxflat_reloc_s);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-25 02:05:11 +02:00
|
|
|
/* 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, or (2) the size of the relocation records, whicher is
|
|
|
|
* larger.
|
|
|
|
*/
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-25 02:05:11 +02:00
|
|
|
extrasize = MAX(loadinfo->bsssize, relocsize);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-25 02:05:11 +02:00
|
|
|
/* Use this additional amount to adjust the total size of the dspace
|
|
|
|
* region.
|
|
|
|
*/
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-25 02:05:11 +02:00
|
|
|
loadinfo->dsize = loadinfo->datasize + extrasize;
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-25 02:05:11 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-25 02:05:11 +02:00
|
|
|
dreadsize = loadinfo->datasize + relocsize;
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-25 02:05:11 +02:00
|
|
|
/* We'll need this a few times. */
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
doffset = loadinfo->isize;
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* 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
|
|
|
|
* to allocate RAM to get the DSpace address space. If the filesystem does
|
|
|
|
* not support file mapping, the map() implementation should do the
|
2009-06-17 00:08:59 +02:00
|
|
|
* right thing.
|
|
|
|
*/
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* The following call will give as a pointer to the mapped file ISpace.
|
2020-11-25 12:14:21 +01:00
|
|
|
* This may be in ROM, RAM, Flash, ... We don't really care where the
|
|
|
|
* memory resides as long as it is fully initialized and ready to execute.
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
2021-02-15 18:15:08 +01:00
|
|
|
ret = file_mmap(&loadinfo->file, NULL, loadinfo->isize, PROT_READ,
|
|
|
|
MAP_SHARED | MAP_FILE, 0, (FAR void **)&loadinfo->ispace);
|
|
|
|
if (ret < 0)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2021-02-15 18:15:08 +01:00
|
|
|
berr("Failed to map NXFLAT ISpace: %d\n", ret);
|
|
|
|
return ret;
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
2020-11-25 12:17:19 +01:00
|
|
|
binfo("Mapped ISpace (%" PRId32 " bytes) at %08x\n",
|
2020-11-25 12:14:21 +01:00
|
|
|
loadinfo->isize, loadinfo->ispace);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
/* The following call allocate D-Space memory and will provide a pointer
|
|
|
|
* to the allocated (but still uninitialized) D-Space memory.
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
ret = nxflat_addrenv_alloc(loadinfo, loadinfo->dsize);
|
|
|
|
if (ret < 0)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: nxflat_addrenv_alloc() failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
return ret;
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
2020-11-25 12:17:19 +01:00
|
|
|
binfo("Allocated DSpace (%" PRId32 " bytes) at %p\n",
|
2012-12-19 18:54:26 +01:00
|
|
|
loadinfo->dsize, loadinfo->dspace->region);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
/* If CONFIG_ARCH_ADDRENV=y, then the D-Space allocation lies in an address
|
2012-12-19 18:54:26 +01:00
|
|
|
* environment that may not be in place. So, in that case, we must call
|
|
|
|
* nxflat_addrenv_select to temporarily instantiate that address space
|
|
|
|
* it can be initialized.
|
|
|
|
*/
|
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01:00
|
|
|
ret = nxflat_addrenv_select(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: nxflat_addrenv_select() failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Now, read the data into allocated DSpace at doffset into the allocated
|
|
|
|
* DSpace memory.
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
ret = nxflat_read(loadinfo, (FAR char *)loadinfo->dspace->region,
|
|
|
|
dreadsize, doffset);
|
2009-06-17 18:28:50 +02:00
|
|
|
if (ret < 0)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Failed to read .data section: %d\n", ret);
|
2009-06-17 18:28:50 +02:00
|
|
|
goto errout;
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2020-11-25 12:17:19 +01:00
|
|
|
binfo("TEXT: %08x Entry point offset: %08" PRIx32 " Data offset: %08jx\n",
|
|
|
|
loadinfo->ispace, loadinfo->entryoffs, (intmax_t)doffset);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
/* Restore the original address environment */
|
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01:00
|
|
|
ret = nxflat_addrenv_restore(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: nxflat_addrenv_restore() failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
return OK;
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
errout:
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2020-01-02 17:49:34 +01:00
|
|
|
nxflat_addrenv_restore(loadinfo);
|
2012-12-19 18:54:26 +01:00
|
|
|
#endif
|
2020-01-02 17:49:34 +01:00
|
|
|
nxflat_unload(loadinfo);
|
2009-06-17 18:28:50 +02:00
|
|
|
return ret;
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|