2015-12-10 16:53:31 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/modlib/modlib_load.c
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
2020-04-13 17:03:46 +02: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
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
2020-04-13 17:03:46 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
2020-04-13 17:03:46 +02: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.
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2017-01-29 15:24:42 +01:00
|
|
|
#include <nuttx/lib/modlib.h>
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
#include "libc.h"
|
|
|
|
#include "modlib/modlib.h"
|
|
|
|
|
2015-12-10 16:53:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
#define ELF_ALIGN_MASK ((1 << CONFIG_MODLIB_ALIGN_LOG2) - 1)
|
2015-12-10 16:53:31 +01:00
|
|
|
#define ELF_ALIGNUP(a) (((unsigned long)(a) + ELF_ALIGN_MASK) & ~ELF_ALIGN_MASK)
|
|
|
|
#define ELF_ALIGNDOWN(a) ((unsigned long)(a) & ~ELF_ALIGN_MASK)
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
# define MAX(x,y) ((x) > (y) ? (x) : (y))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
# define MIN(x,y) ((x) < (y) ? (x) : (y))
|
|
|
|
#endif
|
|
|
|
|
2021-04-14 10:07:39 +02:00
|
|
|
/* _ALIGN_UP: 'a' is assumed to be a power of two */
|
|
|
|
|
|
|
|
#define _ALIGN_UP(v, a) (((v) + ((a) - 1)) & ~((a) - 1))
|
|
|
|
|
2015-12-10 16:53:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-29 18:17:29 +01:00
|
|
|
* Name: modlib_elfsize
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate total memory allocation for the ELF file.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
static void modlib_elfsize(struct mod_loadinfo_s *loadinfo)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
|
|
|
size_t textsize;
|
|
|
|
size_t datasize;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Accumulate the size each section into memory that is marked SHF_ALLOC */
|
|
|
|
|
|
|
|
textsize = 0;
|
|
|
|
datasize = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
/* SHF_ALLOC indicates that the section requires memory during
|
|
|
|
* execution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) != 0)
|
|
|
|
{
|
|
|
|
/* SHF_WRITE indicates that the section address space is write-
|
|
|
|
* able
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0)
|
|
|
|
{
|
2021-04-14 10:07:39 +02:00
|
|
|
datasize = _ALIGN_UP(datasize, shdr->sh_addralign);
|
2015-12-10 16:53:31 +01:00
|
|
|
datasize += ELF_ALIGNUP(shdr->sh_size);
|
2021-04-14 10:07:39 +02:00
|
|
|
if (loadinfo->dataalign < shdr->sh_addralign)
|
|
|
|
{
|
|
|
|
loadinfo->dataalign = shdr->sh_addralign;
|
|
|
|
}
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-14 10:07:39 +02:00
|
|
|
textsize = _ALIGN_UP(textsize, shdr->sh_addralign);
|
2015-12-10 16:53:31 +01:00
|
|
|
textsize += ELF_ALIGNUP(shdr->sh_size);
|
2021-04-14 10:07:39 +02:00
|
|
|
if (loadinfo->textalign < shdr->sh_addralign)
|
|
|
|
{
|
|
|
|
loadinfo->textalign = shdr->sh_addralign;
|
|
|
|
}
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the allocation size */
|
|
|
|
|
|
|
|
loadinfo->textsize = textsize;
|
|
|
|
loadinfo->datasize = datasize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-29 18:17:29 +01:00
|
|
|
* Name: modlib_loadfile
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the section data into memory. Section addresses in the shdr[] are
|
|
|
|
* updated to point to the corresponding position in the memory.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
|
|
|
FAR uint8_t *text;
|
|
|
|
FAR uint8_t *data;
|
|
|
|
FAR uint8_t **pptr;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Read each section into memory that is marked SHF_ALLOC + SHT_NOBITS */
|
|
|
|
|
2018-06-01 18:10:17 +02:00
|
|
|
binfo("Loaded sections:\n");
|
2015-12-10 16:53:31 +01:00
|
|
|
text = (FAR uint8_t *)loadinfo->textalloc;
|
2015-12-11 17:55:21 +01:00
|
|
|
data = (FAR uint8_t *)loadinfo->datastart;
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
/* SHF_ALLOC indicates that the section requires memory during
|
2020-03-10 06:52:58 +01:00
|
|
|
* execution
|
|
|
|
*/
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SHF_WRITE indicates that the section address space is write-
|
|
|
|
* able
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0)
|
|
|
|
{
|
|
|
|
pptr = &data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pptr = &text;
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:07:39 +02:00
|
|
|
*pptr = (FAR uint8_t *)_ALIGN_UP((uintptr_t)*pptr, shdr->sh_addralign);
|
|
|
|
|
2015-12-10 16:53:31 +01:00
|
|
|
/* SHT_NOBITS indicates that there is no data in the file for the
|
|
|
|
* section.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (shdr->sh_type != SHT_NOBITS)
|
|
|
|
{
|
|
|
|
/* Read the section data from sh_offset to the memory region */
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
ret = modlib_read(loadinfo, *pptr, shdr->sh_size, shdr->sh_offset);
|
2015-12-10 16:53:31 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-06-01 18:10:17 +02:00
|
|
|
berr("ERROR: Failed to read section %d: %d\n", i, ret);
|
2015-12-10 16:53:31 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there is no data in an allocated section, then the allocated
|
|
|
|
* section must be cleared.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(*pptr, 0, shdr->sh_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update sh_addr to point to copy in memory */
|
|
|
|
|
2018-06-01 18:10:17 +02:00
|
|
|
binfo("%d. %08lx->%08lx\n", i,
|
2015-12-10 16:53:31 +01:00
|
|
|
(unsigned long)shdr->sh_addr, (unsigned long)*pptr);
|
|
|
|
|
|
|
|
shdr->sh_addr = (uintptr_t)*pptr;
|
|
|
|
|
|
|
|
/* Setup the memory pointer for the next time through the loop */
|
|
|
|
|
|
|
|
*pptr += ELF_ALIGNUP(shdr->sh_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-29 18:17:29 +01:00
|
|
|
* Name: modlib_load
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Loads the binary into memory, allocating memory, performing relocations
|
|
|
|
* and initializing the data and bss segments.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
int modlib_load(FAR struct mod_loadinfo_s *loadinfo)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-06-01 18:10:17 +02:00
|
|
|
binfo("loadinfo: %p\n", loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
DEBUGASSERT(loadinfo && loadinfo->filfd >= 0);
|
|
|
|
|
|
|
|
/* Load section headers into memory */
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
ret = modlib_loadshdrs(loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-06-01 18:10:17 +02:00
|
|
|
berr("ERROR: modlib_loadshdrs failed: %d\n", ret);
|
2015-12-10 16:53:31 +01:00
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine total size to allocate */
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
modlib_elfsize(loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
/* Allocate (and zero) memory for the ELF file. */
|
|
|
|
|
|
|
|
/* Allocate memory to hold the ELF image */
|
|
|
|
|
2020-03-06 09:05:25 +01:00
|
|
|
if (loadinfo->textsize > 0)
|
|
|
|
{
|
2021-06-18 01:47:45 +02:00
|
|
|
#if defined(CONFIG_ARCH_USE_TEXT_HEAP)
|
2020-03-06 09:05:25 +01:00
|
|
|
loadinfo->textalloc = (uintptr_t)
|
2021-06-18 01:47:45 +02:00
|
|
|
up_textheap_memalign(loadinfo->textalign,
|
|
|
|
loadinfo->textsize);
|
2021-04-15 04:33:48 +02:00
|
|
|
#else
|
|
|
|
loadinfo->textalloc = (uintptr_t)lib_memalign(loadinfo->textalign,
|
|
|
|
loadinfo->textsize);
|
|
|
|
#endif
|
2020-03-06 09:05:25 +01:00
|
|
|
if (!loadinfo->textalloc)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to allocate memory for the module text\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loadinfo->datasize > 0)
|
|
|
|
{
|
2021-04-14 10:07:39 +02:00
|
|
|
loadinfo->datastart = (uintptr_t)lib_memalign(loadinfo->dataalign,
|
|
|
|
loadinfo->datasize);
|
2020-03-06 09:05:25 +01:00
|
|
|
if (!loadinfo->datastart)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to allocate memory for the module data\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
/* Load ELF section data into memory */
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
ret = modlib_loadfile(loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-06-01 18:10:17 +02:00
|
|
|
berr("ERROR: modlib_loadfile failed: %d\n", ret);
|
2015-12-10 16:53:31 +01:00
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* Error exits */
|
|
|
|
|
|
|
|
errout_with_buffers:
|
2017-01-29 18:54:54 +01:00
|
|
|
modlib_unload(loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
return ret;
|
|
|
|
}
|