2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libelf/libelf_load.c
|
|
|
|
*
|
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 */
|
|
|
|
|
|
|
|
#define _ALIGN_UP(v, a) (((v) + ((a) - 1)) & ~((a) - 1))
|
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-29 21:43:35 +01:00
|
|
|
static void elf_elfsize(struct elf_loadinfo_s *loadinfo)
|
2012-10-25 03:23:03 +02:00
|
|
|
{
|
2014-08-24 19:54:14 +02:00
|
|
|
size_t textsize;
|
|
|
|
size_t datasize;
|
2012-10-25 03:23:03 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Accumulate the size each section into memory that is marked SHF_ALLOC */
|
|
|
|
|
2014-08-24 19:54:14 +02:00
|
|
|
textsize = 0;
|
|
|
|
datasize = 0;
|
|
|
|
|
2012-10-25 03:23:03 +02:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0)
|
|
|
|
{
|
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
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
{
|
2014-08-24 19:54:14 +02:00
|
|
|
FAR uint8_t *text;
|
|
|
|
FAR uint8_t *data;
|
|
|
|
FAR uint8_t **pptr;
|
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");
|
2015-10-09 03:20:17 +02:00
|
|
|
text = (FAR uint8_t *)loadinfo->textalloc;
|
|
|
|
data = (FAR uint8_t *)loadinfo->dataalloc;
|
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
|
|
|
|
|
|
|
/* SHF_ALLOC indicates that the section requires memory during
|
2020-01-11 17:10:49 +01:00
|
|
|
* execution.
|
|
|
|
*/
|
2012-10-25 03:23:03 +02:00
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) == 0)
|
2012-10-25 01:40:31 +02:00
|
|
|
{
|
2012-10-25 03:23:03 +02:00
|
|
|
continue;
|
2012-10-25 01:40:31 +02:00
|
|
|
}
|
2012-10-25 03:23:03 +02:00
|
|
|
|
2014-08-24 20:57:02 +02:00
|
|
|
/* SHF_WRITE indicates that the section address space is write-
|
|
|
|
* able
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_WRITE) != 0)
|
|
|
|
{
|
|
|
|
pptr = &data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pptr = &text;
|
|
|
|
}
|
|
|
|
|
2022-01-28 07:13:41 +01:00
|
|
|
*pptr = (FAR uint8_t *)_ALIGN_UP((uintptr_t)*pptr, shdr->sh_addralign);
|
|
|
|
|
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
|
|
|
|
|
|
|
/* 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);
|
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)
|
|
|
|
{
|
2014-09-10 23:55:36 +02:00
|
|
|
size_t heapsize;
|
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
|
|
|
|
|
|
|
/* 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-10-13 20:07:40 +02:00
|
|
|
/* Determine the heapsize to allocate. heapsize is ignored if there is
|
2014-10-14 18:21:18 +02:00
|
|
|
* 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.
|
2014-10-13 20:07:40 +02:00
|
|
|
*/
|
2014-09-10 23:55:36 +02:00
|
|
|
|
2014-10-13 20:07:40 +02:00
|
|
|
#if !defined(CONFIG_ARCH_ADDRENV)
|
|
|
|
heapsize = 0;
|
|
|
|
#elif defined(CONFIG_ARCH_STACK_DYNAMIC)
|
2014-09-10 23:55:36 +02:00
|
|
|
heapsize = ARCH_HEAP_SIZE;
|
|
|
|
#else
|
2019-01-26 18:36:38 +01:00
|
|
|
heapsize = MAX(ARCH_HEAP_SIZE, CONFIG_ELF_STACKSIZE);
|
2014-09-10 23:55:36 +02:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
}
|