2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libelf/libelf_load.c
|
|
|
|
*
|
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
|
2012-10-24 18:46:12 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-10-24 18:46:12 +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.
|
2012-10-24 18:46:12 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2012-10-24 18:46:12 +02:00
|
|
|
#include <sys/types.h>
|
2012-10-25 01:40:31 +02:00
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2014-09-12 18:32:23 +02:00
|
|
|
#include <string.h>
|
2012-10-25 01:40:31 +02:00
|
|
|
#include <unistd.h>
|
2012-10-25 18:18:20 +02:00
|
|
|
#include <assert.h>
|
2012-10-24 18:46:12 +02:00
|
|
|
#include <errno.h>
|
2012-10-25 18:18:20 +02:00
|
|
|
#include <debug.h>
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2019-01-28 00:23:01 +01:00
|
|
|
#include <nuttx/arch.h>
|
2014-09-10 23:55:36 +02:00
|
|
|
#include <nuttx/addrenv.h>
|
2020-01-13 23:41:09 +01:00
|
|
|
#include <nuttx/elf.h>
|
2012-10-24 22:19:44 +02:00
|
|
|
#include <nuttx/binfmt/elf.h>
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2012-10-25 18:18:20 +02:00
|
|
|
#include "libelf.h"
|
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
2015-04-08 16:29:03 +02:00
|
|
|
* Pre-processor Definitions
|
2012-10-24 18:46:12 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-25 03:23:03 +02:00
|
|
|
#define ELF_ALIGN_MASK ((1 << CONFIG_ELF_ALIGN_LOG2) - 1)
|
|
|
|
#define ELF_ALIGNUP(a) (((unsigned long)(a) + ELF_ALIGN_MASK) & ~ELF_ALIGN_MASK)
|
|
|
|
#define ELF_ALIGNDOWN(a) ((unsigned long)(a) & ~ELF_ALIGN_MASK)
|
|
|
|
|
2022-01-28 07:13:41 +01: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))
|
2022-01-28 07:13:41 +01:00
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-11-29 15:25:47 +01:00
|
|
|
#if defined(CONFIG_ARCH_USE_SEPARATED_SECTION) && !defined(CONFIG_ARCH_ADDRENV)
|
|
|
|
static int elf_section_alloc(FAR struct elf_loadinfo_s *loadinfo,
|
|
|
|
FAR Elf_Shdr *shdr, uint8_t idx)
|
|
|
|
{
|
|
|
|
if (loadinfo->ehdr.e_type != ET_REL)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loadinfo->sectalloc == NULL)
|
|
|
|
{
|
|
|
|
/* Allocate memory info for all sections */
|
|
|
|
|
|
|
|
loadinfo->sectalloc = kmm_zalloc(sizeof(uintptr_t) *
|
|
|
|
loadinfo->ehdr.e_shnum);
|
|
|
|
if (loadinfo->sectalloc == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
elf_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)kumm_memalign(shdr->sh_addralign,
|
|
|
|
shdr->sh_size);
|
|
|
|
# endif
|
|
|
|
|
|
|
|
if (loadinfo->dataalloc == 0)
|
|
|
|
{
|
|
|
|
loadinfo->dataalloc = loadinfo->sectalloc[idx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
loadinfo->sectalloc[idx] = (uintptr_t)kumm_memalign(shdr->sh_addralign,
|
|
|
|
shdr->sh_size);
|
|
|
|
# endif
|
|
|
|
|
|
|
|
if (loadinfo->textalloc == 0)
|
|
|
|
{
|
|
|
|
loadinfo->textalloc = loadinfo->sectalloc[idx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-25 03:23:03 +02:00
|
|
|
/****************************************************************************
|
2012-10-29 21:43:35 +01:00
|
|
|
* Name: elf_elfsize
|
2012-10-25 03:23:03 +02: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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
static void elf_elfsize(FAR struct elf_loadinfo_s *loadinfo)
|
2012-10-25 03:23:03 +02:00
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
size_t textsize = 0;
|
|
|
|
size_t datasize = 0;
|
2012-10-25 03:23:03 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Accumulate the size each section into memory that is marked SHF_ALLOC */
|
|
|
|
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
2012-10-25 01:40:31 +02:00
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
|
2012-10-25 01:40:31 +02:00
|
|
|
|
2012-10-25 03:23:03 +02:00
|
|
|
/* SHF_ALLOC indicates that the section requires memory during
|
|
|
|
* execution.
|
|
|
|
*/
|
2012-10-25 01:40:31 +02:00
|
|
|
|
2012-10-25 03:23:03 +02:00
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) != 0)
|
|
|
|
{
|
2014-08-24 19:54:14 +02:00
|
|
|
/* SHF_WRITE indicates that the section address space is write-
|
|
|
|
* able
|
|
|
|
*/
|
|
|
|
|
2024-07-05 21:15:53 +02:00
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0
|
|
|
|
#ifdef CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ
|
|
|
|
|| (shdr->sh_flags & SHF_EXECINSTR) == 0
|
|
|
|
#endif
|
|
|
|
)
|
2014-08-24 19:54:14 +02:00
|
|
|
{
|
2023-11-29 15:25:47 +01:00
|
|
|
#if defined(CONFIG_ARCH_USE_SEPARATED_SECTION) && !defined(CONFIG_ARCH_ADDRENV)
|
|
|
|
if (elf_section_alloc(loadinfo, shdr, i) >= 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-28 07:13:41 +01:00
|
|
|
datasize = _ALIGN_UP(datasize, shdr->sh_addralign);
|
2014-08-24 19:54:14 +02:00
|
|
|
datasize += ELF_ALIGNUP(shdr->sh_size);
|
2022-01-28 07:13:41 +01:00
|
|
|
if (loadinfo->dataalign < shdr->sh_addralign)
|
|
|
|
{
|
|
|
|
loadinfo->dataalign = shdr->sh_addralign;
|
|
|
|
}
|
2014-08-24 19:54:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-29 15:25:47 +01:00
|
|
|
#if defined(CONFIG_ARCH_USE_SEPARATED_SECTION) && !defined(CONFIG_ARCH_ADDRENV)
|
|
|
|
if (elf_section_alloc(loadinfo, shdr, i) >= 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-28 07:13:41 +01:00
|
|
|
textsize = _ALIGN_UP(textsize, shdr->sh_addralign);
|
2014-08-24 19:54:14 +02:00
|
|
|
textsize += ELF_ALIGNUP(shdr->sh_size);
|
2022-01-28 07:13:41 +01:00
|
|
|
if (loadinfo->textalign < shdr->sh_addralign)
|
|
|
|
{
|
|
|
|
loadinfo->textalign = shdr->sh_addralign;
|
|
|
|
}
|
2014-08-24 19:54:14 +02:00
|
|
|
}
|
2012-10-25 01:40:31 +02:00
|
|
|
}
|
2012-10-25 03:23:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the allocation size */
|
|
|
|
|
2014-08-24 19:54:14 +02:00
|
|
|
loadinfo->textsize = textsize;
|
|
|
|
loadinfo->datasize = datasize;
|
2012-10-25 03:23:03 +02:00
|
|
|
}
|
|
|
|
|
2023-08-21 14:28:56 +02:00
|
|
|
#ifdef CONFIG_ELF_LOADTO_LMA
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_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 elf_vma2lma(FAR struct elf_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 < phdr->p_vaddr + phdr->p_memsz)
|
|
|
|
{
|
|
|
|
*lma = phdr->p_paddr + shdr->sh_addr - phdr->p_vaddr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-25 03:23:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_loadfile
|
|
|
|
*
|
|
|
|
* Description:
|
2014-08-26 15:57:30 +02:00
|
|
|
* Read the section data into memory. Section addresses in the shdr[] are
|
|
|
|
* updated to point to the corresponding position in the memory.
|
2012-10-25 03:23:03 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-25 18:18:20 +02:00
|
|
|
static inline int elf_loadfile(FAR struct elf_loadinfo_s *loadinfo)
|
2012-10-25 03:23:03 +02: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->dataalloc;
|
2023-11-29 15:25:47 +01:00
|
|
|
FAR uint8_t **pptr = NULL;
|
2012-10-25 18:18:20 +02:00
|
|
|
int ret;
|
2012-10-25 03:23:03 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Read each section into memory that is marked SHF_ALLOC + SHT_NOBITS */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("Loaded sections:\n");
|
2012-10-25 03:23:03 +02: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];
|
2012-10-25 03:23:03 +02:00
|
|
|
|
2023-11-29 15:25:47 +01:00
|
|
|
#ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
|
|
|
|
if (loadinfo->ehdr.e_type == ET_REL)
|
|
|
|
{
|
|
|
|
pptr = (FAR uint8_t **)&loadinfo->sectalloc[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
|
2014-08-24 20:57:02 +02:00
|
|
|
/* SHF_WRITE indicates that the section address space is write-
|
|
|
|
* able
|
|
|
|
*/
|
|
|
|
|
2024-07-05 21:15:53 +02:00
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0
|
|
|
|
#ifdef CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ
|
|
|
|
|| (shdr->sh_flags & SHF_EXECINSTR) == 0
|
|
|
|
#endif
|
|
|
|
)
|
2014-08-24 20:57:02 +02:00
|
|
|
{
|
|
|
|
pptr = &data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pptr = &text;
|
|
|
|
}
|
|
|
|
|
2024-08-06 11:28:08 +02:00
|
|
|
/* SHF_ALLOC indicates that the section requires memory during
|
|
|
|
* execution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) == 0)
|
|
|
|
{
|
|
|
|
/* Set the VMA regardless, some relocations might depend on this */
|
|
|
|
|
|
|
|
shdr->sh_addr = (uintptr_t)*pptr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:40:33 +02:00
|
|
|
if (*pptr == NULL)
|
|
|
|
{
|
|
|
|
if (shdr->sh_type != SHT_NOBITS)
|
|
|
|
{
|
2023-08-21 14:28:56 +02:00
|
|
|
Elf_Addr addr = shdr->sh_addr;
|
|
|
|
|
|
|
|
#ifdef CONFIG_ELF_LOADTO_LMA
|
|
|
|
ret = elf_vma2lma(loadinfo, shdr, &addr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to convert addr %d: %d\n", i, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-06-27 12:40:33 +02:00
|
|
|
/* Read the section data from sh_offset to specified region */
|
|
|
|
|
2023-08-21 14:28:56 +02:00
|
|
|
ret = elf_read(loadinfo, (FAR uint8_t *)addr,
|
2023-06-27 12:40:33 +02:00
|
|
|
shdr->sh_size, shdr->sh_offset);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to read section %d: %d\n", i, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 14:28:56 +02:00
|
|
|
#ifndef CONFIG_ELF_LOADTO_LMA
|
2023-07-13 05:32:01 +02:00
|
|
|
/* If there is no data in an allocated section, then the
|
|
|
|
* allocated section must be cleared.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset((FAR uint8_t *)shdr->sh_addr, 0, shdr->sh_size);
|
|
|
|
}
|
2023-08-21 14:28:56 +02:00
|
|
|
#endif
|
2023-07-13 05:32:01 +02:00
|
|
|
|
2023-06-27 12:40:33 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-11-29 15:25:47 +01:00
|
|
|
#ifndef CONFIG_ARCH_USE_SEPARATED_SECTION
|
2022-01-28 07:13:41 +01:00
|
|
|
*pptr = (FAR uint8_t *)_ALIGN_UP((uintptr_t)*pptr, shdr->sh_addralign);
|
2023-11-29 15:25:47 +01:00
|
|
|
#endif
|
2022-01-28 07:13:41 +01:00
|
|
|
|
2012-10-25 03:23:03 +02:00
|
|
|
/* SHT_NOBITS indicates that there is no data in the file for the
|
|
|
|
* section.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (shdr->sh_type != SHT_NOBITS)
|
2012-10-25 01:40:31 +02:00
|
|
|
{
|
2014-08-24 19:54:14 +02:00
|
|
|
/* Read the section data from sh_offset to the memory region */
|
2012-10-25 03:23:03 +02:00
|
|
|
|
2014-08-24 19:54:14 +02:00
|
|
|
ret = elf_read(loadinfo, *pptr, shdr->sh_size, shdr->sh_offset);
|
2012-10-25 03:23:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: Failed to read section %d: %d\n", i, ret);
|
2012-10-29 01:52:23 +01:00
|
|
|
return ret;
|
2012-10-25 03:23:03 +02:00
|
|
|
}
|
2012-10-25 01:40:31 +02:00
|
|
|
}
|
2012-10-25 03:23:03 +02:00
|
|
|
|
2014-09-12 18:32:23 +02:00
|
|
|
/* If there is no data in an allocated section, then the allocated
|
|
|
|
* section must be cleared.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(*pptr, 0, shdr->sh_size);
|
|
|
|
}
|
|
|
|
|
2012-10-25 18:18:20 +02:00
|
|
|
/* Update sh_addr to point to copy in memory */
|
2012-10-25 03:23:03 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("%d. %08lx->%08lx\n", i,
|
2014-08-24 19:54:14 +02:00
|
|
|
(unsigned long)shdr->sh_addr, (unsigned long)*pptr);
|
|
|
|
|
|
|
|
shdr->sh_addr = (uintptr_t)*pptr;
|
2012-10-25 03:23:03 +02:00
|
|
|
|
2023-11-29 15:25:47 +01:00
|
|
|
#ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
|
|
|
|
if (loadinfo->ehdr.e_type != ET_REL)
|
|
|
|
{
|
|
|
|
*pptr += ELF_ALIGNUP(shdr->sh_size);
|
|
|
|
}
|
|
|
|
#else
|
2012-10-25 03:23:03 +02:00
|
|
|
/* Setup the memory pointer for the next time through the loop */
|
|
|
|
|
2014-08-24 19:54:14 +02:00
|
|
|
*pptr += ELF_ALIGNUP(shdr->sh_size);
|
2023-11-29 15:25:47 +01:00
|
|
|
#endif
|
2012-10-25 01:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_load
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Loads the binary into memory, allocating memory, performing relocations
|
2014-05-07 21:47:52 +02:00
|
|
|
* and initializing the data and bss segments.
|
2012-10-24 18:46:12 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int elf_load(FAR struct elf_loadinfo_s *loadinfo)
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
/* Determine the heapsize to allocate. heapsize is ignored if there is
|
|
|
|
* no address environment because the heap is a shared resource in that
|
|
|
|
* case. If there is no dynamic stack then heapsize must at least as big
|
|
|
|
* as the fixed stack size since the stack will be allocated from the heap
|
|
|
|
* in that case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(CONFIG_ARCH_ADDRENV)
|
|
|
|
size_t heapsize = 0;
|
|
|
|
#elif defined(CONFIG_ARCH_STACK_DYNAMIC)
|
|
|
|
size_t heapsize = ARCH_HEAP_SIZE;
|
|
|
|
#else
|
|
|
|
size_t heapsize = MAX(ARCH_HEAP_SIZE, CONFIG_ELF_STACKSIZE);
|
|
|
|
#endif
|
2020-07-20 16:36:03 +02:00
|
|
|
#ifdef CONFIG_ELF_EXIDX_SECTNAME
|
2015-06-01 22:16:18 +02:00
|
|
|
int exidx;
|
|
|
|
#endif
|
2012-10-25 05:13:11 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("loadinfo: %p\n", loadinfo);
|
2021-02-15 18:15:08 +01:00
|
|
|
DEBUGASSERT(loadinfo && loadinfo->file.f_inode);
|
2012-10-25 05:13:11 +02:00
|
|
|
|
2023-08-21 14:28:56 +02:00
|
|
|
/* Load program headers into memory */
|
|
|
|
|
|
|
|
ret = elf_loadphdrs(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: elf_loadphdrs failed: %d\n", ret);
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
|
2012-10-25 05:13:11 +02:00
|
|
|
/* Load section headers into memory */
|
|
|
|
|
|
|
|
ret = elf_loadshdrs(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: elf_loadshdrs failed: %d\n", ret);
|
2012-10-29 01:52:23 +01:00
|
|
|
goto errout_with_buffers;
|
2012-10-25 05:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine total size to allocate */
|
|
|
|
|
2012-10-29 21:43:35 +01:00
|
|
|
elf_elfsize(loadinfo);
|
2012-10-25 05:13:11 +02:00
|
|
|
|
2014-08-26 15:57:30 +02:00
|
|
|
/* Allocate (and zero) memory for the ELF file. */
|
2012-10-25 05:13:11 +02:00
|
|
|
|
2020-02-08 00:10:23 +01:00
|
|
|
ret = elf_addrenv_alloc(loadinfo, loadinfo->textsize, loadinfo->datasize,
|
|
|
|
heapsize);
|
2012-10-25 05:13:11 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: elf_addrenv_alloc() failed: %d\n", ret);
|
2012-10-29 01:52:23 +01:00
|
|
|
goto errout_with_buffers;
|
2012-10-25 05:13:11 +02:00
|
|
|
}
|
|
|
|
|
2014-08-26 15:57:30 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
|
|
/* 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 = elf_addrenv_select(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: elf_addrenv_select() failed: %d\n", ret);
|
2014-08-26 15:57:30 +02:00
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Load ELF section data into memory */
|
|
|
|
|
|
|
|
ret = elf_loadfile(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: elf_loadfile failed: %d\n", ret);
|
2014-08-26 15:57:30 +02:00
|
|
|
goto errout_with_addrenv;
|
|
|
|
}
|
|
|
|
|
2012-10-29 21:43:35 +01:00
|
|
|
/* Load static constructors and destructors. */
|
2012-10-25 21:21:47 +02:00
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2012-10-29 01:52:23 +01:00
|
|
|
ret = elf_loadctors(loadinfo);
|
|
|
|
if (ret < 0)
|
2012-10-25 21:21:47 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: elf_loadctors failed: %d\n", ret);
|
2014-08-26 15:57:30 +02:00
|
|
|
goto errout_with_addrenv;
|
2012-10-25 21:21:47 +02:00
|
|
|
}
|
2012-10-29 21:43:35 +01:00
|
|
|
|
|
|
|
ret = elf_loaddtors(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: elf_loaddtors failed: %d\n", ret);
|
2014-08-26 15:57:30 +02:00
|
|
|
goto errout_with_addrenv;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-28 12:18:53 +02:00
|
|
|
#ifdef CONFIG_ELF_EXIDX_SECTNAME
|
2015-06-01 22:16:18 +02:00
|
|
|
exidx = elf_findsection(loadinfo, CONFIG_ELF_EXIDX_SECTNAME);
|
|
|
|
if (exidx < 0)
|
|
|
|
{
|
2020-03-26 07:54:27 +01:00
|
|
|
binfo("elf_findsection: Exception Index section not found: %d\n",
|
|
|
|
exidx);
|
2015-06-01 22:16:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-26 07:54:27 +01:00
|
|
|
up_init_exidx(loadinfo->shdr[exidx].sh_addr,
|
|
|
|
loadinfo->shdr[exidx].sh_size);
|
2015-06-01 22:16:18 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-26 15:57:30 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
|
|
/* Restore the original address environment */
|
|
|
|
|
|
|
|
ret = elf_addrenv_restore(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: elf_addrenv_restore() failed: %d\n", ret);
|
2012-10-29 21:43:35 +01:00
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
2012-10-25 21:21:47 +02:00
|
|
|
#endif
|
2012-10-25 18:18:20 +02:00
|
|
|
|
2012-10-25 05:13:11 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* Error exits */
|
|
|
|
|
2014-08-26 15:57:30 +02:00
|
|
|
errout_with_addrenv:
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2020-01-02 17:49:34 +01:00
|
|
|
elf_addrenv_restore(loadinfo);
|
2014-08-26 15:57:30 +02:00
|
|
|
#endif
|
|
|
|
|
2012-10-29 01:52:23 +01:00
|
|
|
errout_with_buffers:
|
2012-10-29 21:43:35 +01:00
|
|
|
elf_unload(loadinfo);
|
2012-10-25 05:13:11 +02:00
|
|
|
return ret;
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|