2015-12-10 23:53:31 +08:00
|
|
|
/****************************************************************************
|
2018-05-30 03:21:26 +08:00
|
|
|
* libs/libc/modlib/modlib_load.c
|
2015-12-10 23:53:31 +08:00
|
|
|
*
|
2024-09-25 20:05:00 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-04-13 23:03:46 +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
|
2015-12-10 23:53:31 +08:00
|
|
|
*
|
2020-04-13 23:03:46 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-12-10 23:53:31 +08:00
|
|
|
*
|
2020-04-13 23:03:46 +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.
|
2015-12-10 23:53:31 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-02-01 21:41:12 +08:00
|
|
|
#include <sys/param.h>
|
2015-12-10 23:53:31 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-07-03 19:45:27 +08:00
|
|
|
#include <sys/ioctl.h>
|
2015-12-10 23:53:31 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2024-10-16 10:49:33 +08:00
|
|
|
#include <nuttx/arch.h>
|
2017-01-29 22:24:42 +08:00
|
|
|
#include <nuttx/lib/modlib.h>
|
2024-07-03 19:45:27 +08:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2015-12-10 23:53:31 +08:00
|
|
|
|
2017-01-30 01:17:29 +08:00
|
|
|
#include "libc.h"
|
|
|
|
#include "modlib/modlib.h"
|
|
|
|
|
2015-12-10 23:53:31 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-30 00:05:15 +08:00
|
|
|
#define ELF_ALIGN_MASK ((1 << CONFIG_MODLIB_ALIGN_LOG2) - 1)
|
2015-12-10 23:53:31 +08: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 16:07:39 +08:00
|
|
|
/* _ALIGN_UP: 'a' is assumed to be a power of two */
|
|
|
|
|
2023-07-03 00:11:02 +08:00
|
|
|
#define _ALIGN_UP(v, a) (((v) + ((a) - 1)) & ~((a) - 1))
|
2021-04-14 16:07:39 +08:00
|
|
|
|
2024-11-02 01:18:31 +08:00
|
|
|
#ifdef CONFIG_ARCH_USE_TEXT_HEAP
|
|
|
|
# define buffer_data_address(p) \
|
|
|
|
(FAR uint8_t *)up_textheap_data_address((FAR void *)p)
|
|
|
|
#else
|
|
|
|
# define buffer_data_address(p) ((FAR uint8_t *)p)
|
|
|
|
#endif
|
|
|
|
|
2015-12-10 23:53:31 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-11-29 22:25:47 +08:00
|
|
|
#ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
|
|
|
|
static int modlib_section_alloc(FAR struct mod_loadinfo_s *loadinfo,
|
|
|
|
FAR Elf_Shdr *shdr, uint8_t idx)
|
|
|
|
{
|
2024-07-03 19:45:27 +08:00
|
|
|
if (loadinfo->ehdr.e_type == ET_DYN)
|
2023-11-29 22:25:47 +08:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loadinfo->sectalloc == NULL)
|
|
|
|
{
|
|
|
|
/* Allocate memory info for all sections */
|
|
|
|
|
|
|
|
loadinfo->sectalloc = lib_zalloc(sizeof(uintptr_t) *
|
2024-04-29 19:59:34 +08:00
|
|
|
loadinfo->ehdr.e_shnum);
|
2023-11-29 22:25:47 +08:00
|
|
|
if (loadinfo->sectalloc == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
modlib_sectname(loadinfo, shdr);
|
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0)
|
|
|
|
{
|
|
|
|
# ifdef CONFIG_ARCH_USE_DATA_HEAP
|
|
|
|
loadinfo->sectalloc[idx] = (uintptr_t)
|
|
|
|
up_dataheap_memalign(
|
|
|
|
(FAR const char *)loadinfo->iobuffer,
|
|
|
|
shdr->sh_addralign,
|
|
|
|
shdr->sh_size);
|
|
|
|
# else
|
|
|
|
loadinfo->sectalloc[idx] = (uintptr_t)lib_memalign(shdr->sh_addralign,
|
|
|
|
shdr->sh_size);
|
|
|
|
# endif
|
|
|
|
|
|
|
|
if (loadinfo->datastart == 0)
|
|
|
|
{
|
|
|
|
loadinfo->datastart = loadinfo->sectalloc[idx];
|
|
|
|
}
|
|
|
|
}
|
2024-07-03 19:45:27 +08:00
|
|
|
else if (loadinfo->xipbase != 0)
|
|
|
|
{
|
|
|
|
loadinfo->sectalloc[idx] = loadinfo->xipbase + shdr->sh_offset;
|
|
|
|
if (loadinfo->textalloc == 0)
|
|
|
|
{
|
|
|
|
loadinfo->textalloc = loadinfo->sectalloc[idx];
|
|
|
|
}
|
|
|
|
}
|
2023-11-29 22:25:47 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
# ifdef CONFIG_ARCH_USE_TEXT_HEAP
|
|
|
|
loadinfo->sectalloc[idx] = (uintptr_t)
|
|
|
|
up_textheap_memalign(
|
|
|
|
(FAR const char *)loadinfo->iobuffer,
|
|
|
|
shdr->sh_addralign,
|
|
|
|
shdr->sh_size);
|
|
|
|
# else
|
2024-07-03 19:45:27 +08:00
|
|
|
loadinfo->sectalloc[idx] = (uintptr_t)
|
|
|
|
lib_memalign(shdr->sh_addralign,
|
|
|
|
shdr->sh_size);
|
2023-11-29 22:25:47 +08:00
|
|
|
# endif
|
|
|
|
|
|
|
|
if (loadinfo->textalloc == 0)
|
|
|
|
{
|
|
|
|
loadinfo->textalloc = loadinfo->sectalloc[idx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 20:02:14 +08:00
|
|
|
return OK;
|
2023-11-29 22:25:47 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-12-10 23:53:31 +08:00
|
|
|
/****************************************************************************
|
2017-01-30 01:17:29 +08:00
|
|
|
* Name: modlib_elfsize
|
2015-12-10 23:53:31 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate total memory allocation for the ELF file.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-06-30 16:49:13 +08:00
|
|
|
static void modlib_elfsize(FAR struct mod_loadinfo_s *loadinfo, bool alloc)
|
2015-12-10 23:53:31 +08:00
|
|
|
{
|
2023-07-03 00:11:02 +08:00
|
|
|
size_t textsize = 0;
|
|
|
|
size_t datasize = 0;
|
2015-12-10 23:53:31 +08:00
|
|
|
int i;
|
|
|
|
|
2023-11-29 22:25:47 +08:00
|
|
|
/* Accumulate the size each section into memory that is marked SHF_ALLOC
|
|
|
|
* if CONFIG_ARCH_USE_SEPARATED_SECTION is enabled, allocate
|
|
|
|
* (and zero) memory for the each section.
|
|
|
|
*/
|
2015-12-10 23:53:31 +08:00
|
|
|
|
2024-06-26 16:55:42 +08:00
|
|
|
if (loadinfo->ehdr.e_type == ET_DYN)
|
2015-12-10 23:53:31 +08:00
|
|
|
{
|
2022-09-26 14:22:03 +08:00
|
|
|
for (i = 0; i < loadinfo->ehdr.e_phnum; i++)
|
2015-12-10 23:53:31 +08:00
|
|
|
{
|
2022-09-26 14:22:03 +08:00
|
|
|
FAR Elf_Phdr *phdr = &loadinfo->phdr[i];
|
|
|
|
FAR void *textaddr = NULL;
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
if (phdr->p_type == PT_LOAD)
|
2015-12-10 23:53:31 +08:00
|
|
|
{
|
2022-09-26 14:22:03 +08:00
|
|
|
if (phdr->p_flags & PF_X)
|
|
|
|
{
|
|
|
|
textsize += phdr->p_memsz;
|
2023-10-20 15:11:29 +08:00
|
|
|
textaddr = (FAR void *)(uintptr_t)phdr->p_vaddr;
|
2022-09-26 14:22:03 +08:00
|
|
|
}
|
|
|
|
else
|
2021-04-14 16:07:39 +08:00
|
|
|
{
|
2022-09-26 14:22:03 +08:00
|
|
|
datasize += phdr->p_memsz;
|
|
|
|
loadinfo->datasec = phdr->p_vaddr;
|
|
|
|
loadinfo->segpad = phdr->p_vaddr -
|
2023-07-03 00:11:02 +08:00
|
|
|
((uintptr_t)textaddr + textsize);
|
2021-04-14 16:07:39 +08:00
|
|
|
}
|
2015-12-10 23:53:31 +08:00
|
|
|
}
|
2022-09-26 14:22:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
|
|
|
{
|
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
/* SHF_ALLOC indicates that the section requires memory during
|
|
|
|
* execution.
|
|
|
|
*/
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) != 0)
|
2015-12-10 23:53:31 +08:00
|
|
|
{
|
2022-09-26 14:22:03 +08:00
|
|
|
/* SHF_WRITE indicates that the section address space is write-
|
|
|
|
* able
|
|
|
|
*/
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2024-07-06 03:15:53 +08:00
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0
|
|
|
|
#ifdef CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ
|
|
|
|
|| (shdr->sh_flags & SHF_EXECINSTR) == 0
|
|
|
|
#endif
|
|
|
|
)
|
2022-09-26 14:22:03 +08:00
|
|
|
{
|
2023-11-29 22:25:47 +08:00
|
|
|
#ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
|
2024-06-30 16:49:13 +08:00
|
|
|
if (alloc && modlib_section_alloc(loadinfo, shdr, i) >= 0)
|
2023-11-29 22:25:47 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
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 16:07:39 +08:00
|
|
|
{
|
2023-11-29 22:25:47 +08:00
|
|
|
#ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
|
2024-06-30 16:49:13 +08:00
|
|
|
if (alloc && modlib_section_alloc(loadinfo, shdr, i) >= 0)
|
2023-11-29 22:25:47 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-09-26 14:22:03 +08: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 16:07:39 +08:00
|
|
|
}
|
2015-12-10 23:53:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the allocation size */
|
|
|
|
|
|
|
|
loadinfo->textsize = textsize;
|
|
|
|
loadinfo->datasize = datasize;
|
|
|
|
}
|
|
|
|
|
2024-06-30 17:11:23 +08:00
|
|
|
#ifdef CONFIG_MODLIB_LOADTO_LMA
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: modlib_vma2lma
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert section`s VMA to LMA according to PhysAddr(p_paddr) of
|
|
|
|
* Program Header.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int modlib_vma2lma(FAR struct mod_loadinfo_s *loadinfo,
|
|
|
|
FAR Elf_Shdr *shdr, FAR Elf_Addr *lma)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_phnum; i++)
|
|
|
|
{
|
|
|
|
FAR Elf_Phdr *phdr = &loadinfo->phdr[i];
|
|
|
|
|
|
|
|
if (shdr->sh_addr >= phdr->p_vaddr &&
|
|
|
|
shdr->sh_addr + shdr->sh_size <= phdr->p_vaddr + phdr->p_memsz &&
|
|
|
|
shdr->sh_offset >= phdr->p_offset &&
|
|
|
|
shdr->sh_offset <= phdr->p_offset + phdr->p_filesz)
|
|
|
|
{
|
|
|
|
*lma = phdr->p_paddr + shdr->sh_addr - phdr->p_vaddr;
|
2024-10-16 20:02:14 +08:00
|
|
|
return OK;
|
2024-06-30 17:11:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-10-16 20:02:14 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: modlib_set_emptysect_vma
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set VMA for empty and unallocated sections, some relocations might
|
|
|
|
* depend on this.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void modlib_set_emptysect_vma(FAR struct mod_loadinfo_s *loadinfo,
|
|
|
|
int section)
|
|
|
|
{
|
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[section];
|
|
|
|
|
|
|
|
/* Set the section as data or text, depending on SHF_WRITE */
|
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0
|
|
|
|
#ifdef CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ
|
|
|
|
|| (shdr->sh_flags & SHF_EXECINSTR) == 0
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
shdr->sh_addr = loadinfo->datastart;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shdr->sh_addr = loadinfo->textalloc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:53:31 +08:00
|
|
|
/****************************************************************************
|
2017-01-30 01:17:29 +08:00
|
|
|
* Name: modlib_loadfile
|
2015-12-10 23:53:31 +08: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-30 01:17:29 +08:00
|
|
|
static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo)
|
2015-12-10 23:53:31 +08:00
|
|
|
{
|
2023-07-03 00:11:02 +08:00
|
|
|
FAR uint8_t *text = (FAR uint8_t *)loadinfo->textalloc;
|
|
|
|
FAR uint8_t *data = (FAR uint8_t *)loadinfo->datastart;
|
2015-12-10 23:53:31 +08:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
/* Read each PT_LOAD area into memory */
|
2015-12-10 23:53:31 +08:00
|
|
|
|
2023-07-03 00:11:02 +08:00
|
|
|
binfo("Loading sections - text: %p.%zx data: %p.%zx\n",
|
|
|
|
text, loadinfo->textsize, data, loadinfo->datasize);
|
2015-12-10 23:53:31 +08:00
|
|
|
|
2024-06-26 16:55:42 +08:00
|
|
|
if (loadinfo->ehdr.e_type == ET_DYN)
|
2015-12-10 23:53:31 +08:00
|
|
|
{
|
2022-09-26 14:22:03 +08:00
|
|
|
for (i = 0; i < loadinfo->ehdr.e_phnum; i++)
|
2015-12-10 23:53:31 +08:00
|
|
|
{
|
2022-09-26 14:22:03 +08:00
|
|
|
FAR Elf_Phdr *phdr = &loadinfo->phdr[i];
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
if (phdr->p_type == PT_LOAD)
|
2015-12-10 23:53:31 +08:00
|
|
|
{
|
2022-09-26 14:22:03 +08:00
|
|
|
if (phdr->p_flags & PF_X)
|
|
|
|
{
|
2024-11-02 01:18:31 +08:00
|
|
|
ret = modlib_read(loadinfo, buffer_data_address(text),
|
|
|
|
phdr->p_filesz,
|
2022-09-26 14:22:03 +08:00
|
|
|
phdr->p_offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-03 00:11:02 +08:00
|
|
|
size_t bsssize = phdr->p_memsz - phdr->p_filesz;
|
2022-09-26 14:22:03 +08:00
|
|
|
ret = modlib_read(loadinfo, data, phdr->p_filesz,
|
|
|
|
phdr->p_offset);
|
2023-07-03 00:11:02 +08:00
|
|
|
memset(data + phdr->p_filesz, 0, bsssize);
|
2022-09-26 14:22:03 +08:00
|
|
|
}
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to read section %d: %d\n", i, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2015-12-10 23:53:31 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-26 14:22:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
2015-12-10 23:53:31 +08:00
|
|
|
{
|
2022-09-26 14:22:03 +08:00
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
|
2023-11-29 22:25:47 +08:00
|
|
|
FAR uint8_t **pptr = NULL;
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
/* SHF_ALLOC indicates that the section requires memory during
|
|
|
|
* execution
|
|
|
|
*/
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2024-10-16 20:02:14 +08:00
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) == 0 || shdr->sh_size == 0)
|
2024-07-16 11:31:07 +08:00
|
|
|
{
|
2024-10-16 20:02:14 +08:00
|
|
|
/* Set the VMA regardless */
|
2024-07-16 11:31:07 +08:00
|
|
|
|
2024-10-16 20:02:14 +08:00
|
|
|
modlib_set_emptysect_vma(loadinfo, i);
|
2022-09-26 14:22:03 +08:00
|
|
|
continue;
|
|
|
|
}
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2023-11-29 22:25:47 +08:00
|
|
|
#ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
|
2024-07-03 15:33:48 +08:00
|
|
|
if (loadinfo->ehdr.e_type == ET_REL ||
|
|
|
|
loadinfo->ehdr.e_type == ET_EXEC)
|
2022-09-26 14:22:03 +08:00
|
|
|
{
|
2023-11-29 22:25:47 +08:00
|
|
|
pptr = (FAR uint8_t **)&loadinfo->sectalloc[i];
|
2022-09-26 14:22:03 +08:00
|
|
|
}
|
2023-11-29 22:25:47 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (pptr == NULL)
|
2022-09-26 14:22:03 +08:00
|
|
|
{
|
2023-11-29 22:25:47 +08:00
|
|
|
/* SHF_WRITE indicates that the section address space is
|
|
|
|
* writeable
|
|
|
|
*/
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2023-11-29 22:25:47 +08:00
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0
|
|
|
|
#ifdef CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ
|
|
|
|
|| (shdr->sh_flags & SHF_EXECINSTR) == 0
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
pptr = &data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pptr = &text;
|
|
|
|
}
|
|
|
|
|
2024-07-03 19:45:27 +08:00
|
|
|
if (loadinfo->xipbase == 0)
|
|
|
|
{
|
|
|
|
/* If xipbase is not set, align the address
|
|
|
|
* xipbase is set, the address can't be aligned
|
|
|
|
*/
|
|
|
|
|
|
|
|
*pptr = (FAR uint8_t *)_ALIGN_UP((uintptr_t)*pptr,
|
|
|
|
shdr->sh_addralign);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_WRITE) == 0 && loadinfo->xipbase != 0)
|
|
|
|
{
|
|
|
|
goto skipload;
|
2023-11-29 22:25:47 +08:00
|
|
|
}
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
/* SHT_NOBITS indicates that there is no data in the file for the
|
|
|
|
* section.
|
|
|
|
*/
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
if (shdr->sh_type != SHT_NOBITS)
|
|
|
|
{
|
2024-06-30 17:11:23 +08:00
|
|
|
#ifdef CONFIG_MODLIB_LOADTO_LMA
|
2024-07-16 11:31:07 +08:00
|
|
|
ret = modlib_vma2lma(loadinfo, shdr, (FAR Elf_Addr *)pptr);
|
2024-06-30 17:11:23 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to convert addr %d: %d\n", i, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
/* Read the section data from sh_offset to the memory region */
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2024-11-02 01:18:31 +08:00
|
|
|
ret = modlib_read(loadinfo, buffer_data_address(*pptr),
|
|
|
|
shdr->sh_size, shdr->sh_offset);
|
2022-09-26 14:22:03 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to read section %d: %d\n", i, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
/* If there is no data in an allocated section, then the allocated
|
|
|
|
* section must be cleared.
|
|
|
|
*/
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2024-07-29 12:28:48 +08:00
|
|
|
#ifndef CONFIG_MODLIB_LOADTO_LMA
|
2024-07-16 11:31:07 +08:00
|
|
|
else if (*pptr != NULL)
|
2022-09-26 14:22:03 +08:00
|
|
|
{
|
|
|
|
memset(*pptr, 0, shdr->sh_size);
|
|
|
|
}
|
2024-07-29 12:28:48 +08:00
|
|
|
#endif
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2024-07-03 19:45:27 +08:00
|
|
|
skipload:
|
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
/* Update sh_addr to point to copy in memory */
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
binfo("%d. %08lx->%08lx\n", i,
|
|
|
|
(unsigned long)shdr->sh_addr, (unsigned long)*pptr);
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2024-07-03 15:33:48 +08:00
|
|
|
/* Use offset to remember the original file address */
|
|
|
|
|
|
|
|
shdr->sh_offset = (uintptr_t)shdr->sh_addr;
|
2022-09-26 14:22:03 +08:00
|
|
|
shdr->sh_addr = (uintptr_t)*pptr;
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2023-11-29 22:25:47 +08:00
|
|
|
#ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
|
|
|
|
if (loadinfo->ehdr.e_type != ET_REL)
|
|
|
|
{
|
|
|
|
*pptr += ELF_ALIGNUP(shdr->sh_size);
|
|
|
|
}
|
|
|
|
#else
|
2022-09-26 14:22:03 +08:00
|
|
|
/* Setup the memory pointer for the next time through the loop */
|
2023-07-24 19:24:26 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
*pptr += ELF_ALIGNUP(shdr->sh_size);
|
2023-11-29 22:25:47 +08:00
|
|
|
#endif
|
2015-12-10 23:53:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 15:33:48 +08:00
|
|
|
/* Update GOT table */
|
|
|
|
|
|
|
|
if (loadinfo->gotindex >= 0)
|
|
|
|
{
|
|
|
|
FAR Elf_Shdr *gotshdr = &loadinfo->shdr[loadinfo->gotindex];
|
|
|
|
FAR uintptr_t *got = (FAR uintptr_t *)gotshdr->sh_addr;
|
|
|
|
FAR uintptr_t *end = got + gotshdr->sh_size / sizeof(uintptr_t);
|
|
|
|
|
|
|
|
for (; got < end; got++)
|
|
|
|
{
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
|
|
|
{
|
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
|
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*got >= shdr->sh_offset &&
|
|
|
|
*got < shdr->sh_offset + shdr->sh_size)
|
|
|
|
{
|
|
|
|
*got += shdr->sh_addr - shdr->sh_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:53:31 +08:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-30 01:17:29 +08:00
|
|
|
* Name: modlib_load
|
2015-12-10 23:53:31 +08: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-30 01:17:29 +08:00
|
|
|
int modlib_load(FAR struct mod_loadinfo_s *loadinfo)
|
2015-12-10 23:53:31 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-06-02 00:10:17 +08:00
|
|
|
binfo("loadinfo: %p\n", loadinfo);
|
2015-12-10 23:53:31 +08:00
|
|
|
DEBUGASSERT(loadinfo && loadinfo->filfd >= 0);
|
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
/* Load section and program headers into memory */
|
2015-12-10 23:53:31 +08:00
|
|
|
|
2022-09-26 14:22:03 +08:00
|
|
|
ret = modlib_loadhdrs(loadinfo);
|
2015-12-10 23:53:31 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-26 14:22:03 +08:00
|
|
|
berr("ERROR: modlib_loadhdrs failed: %d\n", ret);
|
2015-12-10 23:53:31 +08:00
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
|
2024-07-03 15:33:48 +08:00
|
|
|
loadinfo->gotindex = modlib_findsection(loadinfo, ".got");
|
|
|
|
if (loadinfo->gotindex >= 0)
|
|
|
|
{
|
|
|
|
binfo("GOT section found! index %d\n", loadinfo->gotindex);
|
2024-07-03 19:45:27 +08:00
|
|
|
if (ioctl(loadinfo->filfd, FIOC_XIPBASE,
|
|
|
|
(unsigned long)&loadinfo->xipbase) >= 0)
|
|
|
|
{
|
|
|
|
binfo("can use xipbase %zu\n", loadinfo->xipbase);
|
|
|
|
}
|
2024-07-03 15:33:48 +08:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:53:31 +08:00
|
|
|
/* Determine total size to allocate */
|
|
|
|
|
2024-06-30 16:49:13 +08:00
|
|
|
modlib_elfsize(loadinfo, true);
|
2015-12-10 23:53:31 +08:00
|
|
|
|
|
|
|
/* Allocate (and zero) memory for the ELF file. */
|
|
|
|
|
|
|
|
/* Allocate memory to hold the ELF image */
|
|
|
|
|
2023-09-04 08:37:48 +08: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.
|
|
|
|
*/
|
|
|
|
|
2024-07-03 15:33:48 +08:00
|
|
|
#ifndef CONFIG_MODLIB_LOADTO_LMA
|
|
|
|
|
|
|
|
if (loadinfo->ehdr.e_type == ET_REL || loadinfo->ehdr.e_type == ET_EXEC)
|
2020-03-06 16:05:25 +08:00
|
|
|
{
|
2024-07-03 15:33:48 +08:00
|
|
|
# ifndef CONFIG_ARCH_USE_SEPARATED_SECTION
|
2024-07-03 19:45:27 +08:00
|
|
|
if (loadinfo->xipbase != 0)
|
|
|
|
{
|
|
|
|
loadinfo->textalloc = loadinfo->xipbase +
|
|
|
|
loadinfo->shdr[1].sh_offset;
|
|
|
|
}
|
|
|
|
else if (loadinfo->textsize > 0)
|
2023-09-04 08:37:48 +08:00
|
|
|
{
|
2024-07-03 15:33:48 +08:00
|
|
|
# ifdef CONFIG_ARCH_USE_TEXT_HEAP
|
2023-09-04 08:37:48 +08:00
|
|
|
loadinfo->textalloc = (uintptr_t)
|
|
|
|
up_textheap_memalign(loadinfo->textalign,
|
|
|
|
loadinfo->textsize +
|
|
|
|
loadinfo->segpad);
|
2024-07-03 15:33:48 +08:00
|
|
|
# else
|
2023-09-04 08:37:48 +08:00
|
|
|
loadinfo->textalloc = (uintptr_t)lib_memalign(loadinfo->textalign,
|
|
|
|
loadinfo->textsize +
|
|
|
|
loadinfo->segpad);
|
2024-07-03 15:33:48 +08:00
|
|
|
# endif
|
2023-09-04 08:37:48 +08: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 16:05:25 +08:00
|
|
|
{
|
2024-07-03 15:33:48 +08:00
|
|
|
# ifdef CONFIG_ARCH_USE_DATA_HEAP
|
2023-10-05 09:27:10 +08:00
|
|
|
loadinfo->datastart = (uintptr_t)
|
|
|
|
up_dataheap_memalign(loadinfo->dataalign,
|
|
|
|
loadinfo->datasize);
|
2024-07-03 15:33:48 +08:00
|
|
|
# else
|
2023-09-04 08:37:48 +08:00
|
|
|
loadinfo->datastart = (uintptr_t)lib_memalign(loadinfo->dataalign,
|
|
|
|
loadinfo->datasize);
|
2024-07-03 15:33:48 +08:00
|
|
|
# endif
|
2023-09-04 08:37:48 +08:00
|
|
|
if (!loadinfo->datastart)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to allocate memory for the module data\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
2020-03-06 16:05:25 +08:00
|
|
|
}
|
2024-07-03 15:33:48 +08:00
|
|
|
# endif
|
2020-03-06 16:05:25 +08:00
|
|
|
}
|
2024-07-16 11:31:07 +08:00
|
|
|
else if (loadinfo->ehdr.e_type == ET_DYN)
|
2020-03-06 16:05:25 +08:00
|
|
|
{
|
2023-09-04 08:37:48 +08:00
|
|
|
loadinfo->textalloc = (uintptr_t)lib_memalign(loadinfo->textalign,
|
|
|
|
loadinfo->textsize +
|
|
|
|
loadinfo->datasize +
|
|
|
|
loadinfo->segpad);
|
|
|
|
|
|
|
|
if (!loadinfo->textalloc)
|
2023-07-24 06:45:16 +08:00
|
|
|
{
|
2023-09-04 08:37:48 +08:00
|
|
|
berr("ERROR: Failed to allocate memory for the module\n");
|
2023-07-24 06:45:16 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
2023-09-04 08:37:48 +08:00
|
|
|
|
|
|
|
loadinfo->datastart = loadinfo->textalloc +
|
|
|
|
loadinfo->textsize +
|
|
|
|
loadinfo->segpad;
|
2020-03-06 16:05:25 +08:00
|
|
|
}
|
2015-12-10 23:53:31 +08:00
|
|
|
|
2024-07-03 15:33:48 +08:00
|
|
|
#endif /* CONFIG_MODLIB_LOADTO_LMA */
|
|
|
|
|
2015-12-10 23:53:31 +08:00
|
|
|
/* Load ELF section data into memory */
|
|
|
|
|
2017-01-30 01:17:29 +08:00
|
|
|
ret = modlib_loadfile(loadinfo);
|
2015-12-10 23:53:31 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-06-02 00:10:17 +08:00
|
|
|
berr("ERROR: modlib_loadfile failed: %d\n", ret);
|
2015-12-10 23:53:31 +08:00
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
|
2024-07-09 15:17:11 +08:00
|
|
|
#ifdef CONFIG_MODLIB_EXIDX_SECTNAME
|
|
|
|
ret = modlib_findsection(loadinfo, CONFIG_MODLIB_EXIDX_SECTNAME);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
binfo("modlib_findsection: Exception Index section not found: %d\n",
|
|
|
|
ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
up_init_exidx(loadinfo->shdr[ret].sh_addr,
|
|
|
|
loadinfo->shdr[ret].sh_size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-12-10 23:53:31 +08:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* Error exits */
|
|
|
|
|
|
|
|
errout_with_buffers:
|
2017-01-30 01:54:54 +08:00
|
|
|
modlib_unload(loadinfo);
|
2015-12-10 23:53:31 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2024-06-30 16:49:13 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: modlib_load_with_addrenv
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Loads the binary into memory, use the address environment to load the
|
|
|
|
* binary.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
|
|
int modlib_load_with_addrenv(FAR struct mod_loadinfo_s *loadinfo)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
binfo("loadinfo: %p\n", loadinfo);
|
|
|
|
DEBUGASSERT(loadinfo && loadinfo->filfd >= 0);
|
|
|
|
|
|
|
|
/* Load section and program headers into memory */
|
|
|
|
|
|
|
|
ret = modlib_loadhdrs(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: modlib_loadhdrs failed: %d\n", ret);
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
|
2024-07-03 15:33:48 +08:00
|
|
|
loadinfo->gotindex = modlib_findsection(loadinfo, ".got");
|
|
|
|
if (loadinfo->gotindex >= 0)
|
|
|
|
{
|
|
|
|
binfo("GOT section found! index %d\n", loadinfo->gotindex);
|
2024-07-03 19:45:27 +08:00
|
|
|
if (ioctl(loadinfo->filfd, FIOC_XIPBASE,
|
|
|
|
(unsigned long)&loadinfo->xipbase) >= 0)
|
|
|
|
{
|
|
|
|
binfo("can use xipbase %zu\n", loadinfo->xipbase);
|
|
|
|
}
|
2024-07-03 15:33:48 +08:00
|
|
|
}
|
|
|
|
|
2024-06-30 16:49:13 +08:00
|
|
|
/* Determine total size to allocate */
|
|
|
|
|
|
|
|
modlib_elfsize(loadinfo, false);
|
|
|
|
|
|
|
|
ret = modlib_addrenv_alloc(loadinfo, loadinfo->textsize,
|
|
|
|
loadinfo->datasize);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to create address environment: %d\n", ret);
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If CONFIG_ARCH_ADDRENV=y, then the loaded ELF lies in a virtual address
|
|
|
|
* space that may not be in place now. elf_addrenv_select() will
|
|
|
|
* temporarily instantiate that address space.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = modlib_addrenv_select(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: elf_addrenv_select() failed: %d\n", ret);
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = modlib_loadfile(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: modlib_loadfile failed: %d\n", ret);
|
|
|
|
goto errout_with_addrenv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore the original address environment */
|
|
|
|
|
|
|
|
ret = modlib_addrenv_restore(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: modlib_addrenv_restore() failed: %d\n", ret);
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_addrenv:
|
|
|
|
modlib_addrenv_restore(loadinfo);
|
|
|
|
|
|
|
|
errout_with_buffers:
|
|
|
|
modlib_unload(loadinfo);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|