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>
|
|
|
|
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2015-12-10 16:53:31 +01:00
|
|
|
#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)
|
|
|
|
|
2021-04-14 10:07:39 +02:00
|
|
|
/* _ALIGN_UP: 'a' is assumed to be a power of two */
|
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
#define _ALIGN_UP(v, a) (((v) + ((a) - 1)) & ~((a) - 1))
|
2021-04-14 10:07:39 +02:00
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
static void modlib_elfsize(FAR struct mod_loadinfo_s *loadinfo)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
size_t textsize = 0;
|
|
|
|
size_t datasize = 0;
|
2015-12-10 16:53:31 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Accumulate the size each section into memory that is marked SHF_ALLOC */
|
|
|
|
|
2023-07-24 13:24:26 +02:00
|
|
|
if (loadinfo->ehdr.e_phnum > 0)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2022-09-26 08:22:03 +02:00
|
|
|
for (i = 0; i < loadinfo->ehdr.e_phnum; i++)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2022-09-26 08:22:03 +02:00
|
|
|
FAR Elf_Phdr *phdr = &loadinfo->phdr[i];
|
|
|
|
FAR void *textaddr = NULL;
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
if (phdr->p_type == PT_LOAD)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2022-09-26 08:22:03 +02:00
|
|
|
if (phdr->p_flags & PF_X)
|
|
|
|
{
|
|
|
|
textsize += phdr->p_memsz;
|
2023-07-02 18:11:02 +02:00
|
|
|
textaddr = (FAR void *)phdr->p_vaddr;
|
2022-09-26 08:22:03 +02:00
|
|
|
}
|
|
|
|
else
|
2021-04-14 10:07:39 +02:00
|
|
|
{
|
2022-09-26 08:22:03 +02:00
|
|
|
datasize += phdr->p_memsz;
|
|
|
|
loadinfo->datasec = phdr->p_vaddr;
|
|
|
|
loadinfo->segpad = phdr->p_vaddr -
|
2023-07-02 18:11:02 +02:00
|
|
|
((uintptr_t)textaddr + textsize);
|
2021-04-14 10:07:39 +02:00
|
|
|
}
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
2022-09-26 08:22:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
|
|
|
{
|
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
/* SHF_ALLOC indicates that the section requires memory during
|
|
|
|
* execution.
|
|
|
|
*/
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) != 0)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2022-09-26 08:22:03 +02:00
|
|
|
/* SHF_WRITE indicates that the section address space is write-
|
|
|
|
* able
|
|
|
|
*/
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0)
|
|
|
|
{
|
|
|
|
datasize = _ALIGN_UP(datasize, shdr->sh_addralign);
|
|
|
|
datasize += ELF_ALIGNUP(shdr->sh_size);
|
|
|
|
if (loadinfo->dataalign < shdr->sh_addralign)
|
|
|
|
{
|
|
|
|
loadinfo->dataalign = shdr->sh_addralign;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2021-04-14 10:07:39 +02:00
|
|
|
{
|
2022-09-26 08:22:03 +02:00
|
|
|
textsize = _ALIGN_UP(textsize, shdr->sh_addralign);
|
|
|
|
textsize += ELF_ALIGNUP(shdr->sh_size);
|
|
|
|
if (loadinfo->textalign < shdr->sh_addralign)
|
|
|
|
{
|
|
|
|
loadinfo->textalign = shdr->sh_addralign;
|
|
|
|
}
|
2021-04-14 10:07:39 +02:00
|
|
|
}
|
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
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
FAR uint8_t *text = (FAR uint8_t *)loadinfo->textalloc;
|
|
|
|
FAR uint8_t *data = (FAR uint8_t *)loadinfo->datastart;
|
2015-12-10 16:53:31 +01:00
|
|
|
FAR uint8_t **pptr;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
/* Read each PT_LOAD area into memory */
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
binfo("Loading sections - text: %p.%zx data: %p.%zx\n",
|
|
|
|
text, loadinfo->textsize, data, loadinfo->datasize);
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
if (loadinfo->ehdr.e_phnum > 0)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2022-09-26 08:22:03 +02:00
|
|
|
for (i = 0; i < loadinfo->ehdr.e_phnum; i++)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2022-09-26 08:22:03 +02:00
|
|
|
FAR Elf_Phdr *phdr = &loadinfo->phdr[i];
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
if (phdr->p_type == PT_LOAD)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2022-09-26 08:22:03 +02:00
|
|
|
if (phdr->p_flags & PF_X)
|
|
|
|
{
|
|
|
|
ret = modlib_read(loadinfo, text, phdr->p_filesz,
|
|
|
|
phdr->p_offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
size_t bsssize = phdr->p_memsz - phdr->p_filesz;
|
2022-09-26 08:22:03 +02:00
|
|
|
ret = modlib_read(loadinfo, data, phdr->p_filesz,
|
|
|
|
phdr->p_offset);
|
2023-07-02 18:11:02 +02:00
|
|
|
memset(data + phdr->p_filesz, 0, bsssize);
|
2022-09-26 08:22:03 +02:00
|
|
|
}
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to read section %d: %d\n", i, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
2022-09-26 08:22:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2022-09-26 08:22:03 +02:00
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
/* SHF_ALLOC indicates that the section requires memory during
|
|
|
|
* execution
|
|
|
|
*/
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
/* SHF_WRITE indicates that the section address space is write-
|
|
|
|
* able
|
|
|
|
*/
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0)
|
|
|
|
{
|
|
|
|
pptr = &data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pptr = &text;
|
|
|
|
}
|
2023-07-24 13:24:26 +02:00
|
|
|
|
|
|
|
*pptr = (FAR uint8_t *)_ALIGN_UP((uintptr_t)*pptr,
|
2023-07-02 18:11:02 +02:00
|
|
|
shdr->sh_addralign);
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
/* SHT_NOBITS indicates that there is no data in the file for the
|
|
|
|
* section.
|
|
|
|
*/
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
if (shdr->sh_type != SHT_NOBITS)
|
|
|
|
{
|
|
|
|
/* Read the section data from sh_offset to the memory region */
|
2023-07-24 13:24:26 +02:00
|
|
|
|
|
|
|
ret = modlib_read(loadinfo, *pptr, shdr->sh_size,
|
|
|
|
shdr->sh_offset);
|
2022-09-26 08:22:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to read section %d: %d\n", i, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
/* If there is no data in an allocated section, then the allocated
|
|
|
|
* section must be cleared.
|
|
|
|
*/
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(*pptr, 0, shdr->sh_size);
|
|
|
|
}
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
/* Update sh_addr to point to copy in memory */
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
binfo("%d. %08lx->%08lx\n", i,
|
|
|
|
(unsigned long)shdr->sh_addr, (unsigned long)*pptr);
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
shdr->sh_addr = (uintptr_t)*pptr;
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
/* Setup the memory pointer for the next time through the loop */
|
2023-07-24 13:24:26 +02:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
*pptr += ELF_ALIGNUP(shdr->sh_size);
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
/* Load section and program headers into memory */
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
ret = modlib_loadhdrs(loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-26 08:22:03 +02:00
|
|
|
berr("ERROR: modlib_loadhdrs 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 */
|
|
|
|
|
2023-09-04 02:37:48 +02:00
|
|
|
/* For Dynamic shared objects the relative positions between
|
|
|
|
* text and data must be maintained due to references to the
|
|
|
|
* GOT. Therefore we cannot do two different allocations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (loadinfo->ehdr.e_type == ET_REL)
|
2020-03-06 09:05:25 +01:00
|
|
|
{
|
2023-09-04 02:37:48 +02:00
|
|
|
if (loadinfo->textsize > 0)
|
|
|
|
{
|
2021-06-18 01:47:45 +02:00
|
|
|
#if defined(CONFIG_ARCH_USE_TEXT_HEAP)
|
2023-09-04 02:37:48 +02:00
|
|
|
loadinfo->textalloc = (uintptr_t)
|
|
|
|
up_textheap_memalign(loadinfo->textalign,
|
|
|
|
loadinfo->textsize +
|
|
|
|
loadinfo->segpad);
|
2021-04-15 04:33:48 +02:00
|
|
|
#else
|
2023-09-04 02:37:48 +02:00
|
|
|
loadinfo->textalloc = (uintptr_t)lib_memalign(loadinfo->textalign,
|
|
|
|
loadinfo->textsize +
|
|
|
|
loadinfo->segpad);
|
2021-04-15 04:33:48 +02:00
|
|
|
#endif
|
2023-09-04 02:37:48 +02: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)
|
2020-03-06 09:05:25 +01:00
|
|
|
{
|
2023-09-04 02:37:48 +02:00
|
|
|
loadinfo->datastart = (uintptr_t)lib_memalign(loadinfo->dataalign,
|
|
|
|
loadinfo->datasize);
|
|
|
|
if (!loadinfo->datastart)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to allocate memory for the module data\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
2020-03-06 09:05:25 +01:00
|
|
|
}
|
|
|
|
}
|
2023-09-04 02:37:48 +02:00
|
|
|
else
|
2020-03-06 09:05:25 +01:00
|
|
|
{
|
2023-09-04 02:37:48 +02:00
|
|
|
loadinfo->textalloc = (uintptr_t)lib_memalign(loadinfo->textalign,
|
|
|
|
loadinfo->textsize +
|
|
|
|
loadinfo->datasize +
|
|
|
|
loadinfo->segpad);
|
|
|
|
|
|
|
|
if (!loadinfo->textalloc)
|
2023-07-24 00:45:16 +02:00
|
|
|
{
|
2023-09-04 02:37:48 +02:00
|
|
|
berr("ERROR: Failed to allocate memory for the module\n");
|
2023-07-24 00:45:16 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
2023-09-04 02:37:48 +02:00
|
|
|
|
|
|
|
loadinfo->datastart = loadinfo->textalloc +
|
|
|
|
loadinfo->textsize +
|
|
|
|
loadinfo->segpad;
|
2020-03-06 09:05:25 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|