2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/elf.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>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2014-08-26 18:42:42 +02:00
|
|
|
#include <nuttx/arch.h>
|
2012-10-24 22:19:44 +02:00
|
|
|
#include <nuttx/binfmt/binfmt.h>
|
|
|
|
#include <nuttx/binfmt/elf.h>
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2014-08-26 18:42:42 +02:00
|
|
|
#include "libelf/libelf.h"
|
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
#ifdef CONFIG_ELF
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-22 10:24:15 +02:00
|
|
|
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT
|
|
|
|
* have to be defined or CONFIG_ELF_DUMPBUFFER does nothing.
|
2012-10-24 18:46:12 +02:00
|
|
|
*/
|
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
#if !defined(CONFIG_DEBUG_INFO) || !defined(CONFIG_DEBUG_BINFMT)
|
2012-10-24 18:46:12 +02:00
|
|
|
# undef CONFIG_ELF_DUMPBUFFER
|
|
|
|
#endif
|
|
|
|
|
2012-10-25 18:18:20 +02:00
|
|
|
#ifndef CONFIG_ELF_STACKSIZE
|
|
|
|
# define CONFIG_ELF_STACKSIZE 2048
|
|
|
|
#endif
|
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
#ifdef CONFIG_ELF_DUMPBUFFER
|
2023-05-20 00:32:34 +02:00
|
|
|
# define elf_dumpbuffer(m,b,n) binfodumpbuffer(m,b,n)
|
2012-10-24 18:46:12 +02:00
|
|
|
#else
|
2023-05-20 00:32:34 +02:00
|
|
|
# define elf_dumpbuffer(m,b,n)
|
2012-10-24 18:46:12 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-24 11:12:06 +02:00
|
|
|
static int elf_loadbinary(FAR struct binary_s *binp,
|
|
|
|
FAR const char *filename,
|
|
|
|
FAR const struct symtab_s *exports,
|
|
|
|
int nexports);
|
2021-11-17 16:53:28 +01:00
|
|
|
#ifdef CONFIG_ELF_COREDUMP
|
|
|
|
static int elf_dumpbinary(FAR struct memory_region_s *regions,
|
2023-05-22 05:43:08 +02:00
|
|
|
FAR struct lib_outstream_s *stream,
|
|
|
|
pid_t pid);
|
2021-11-17 16:53:28 +01:00
|
|
|
#endif
|
2016-06-11 22:14:08 +02:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
|
2012-10-24 18:46:12 +02:00
|
|
|
static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static struct binfmt_s g_elfbinfmt =
|
|
|
|
{
|
|
|
|
NULL, /* next */
|
|
|
|
elf_loadbinary, /* load */
|
2014-05-09 00:58:10 +02:00
|
|
|
NULL, /* unload */
|
2021-11-17 16:53:28 +01:00
|
|
|
#ifdef CONFIG_ELF_COREDUMP
|
|
|
|
elf_dumpbinary, /* coredump */
|
|
|
|
#endif
|
2012-10-24 18:46:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_dumploadinfo
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
|
2012-10-24 18:46:12 +02:00
|
|
|
static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo)
|
|
|
|
{
|
2012-10-25 18:18:20 +02:00
|
|
|
int i;
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2019-08-15 22:41:42 +02:00
|
|
|
binfo("LOAD_INFO:\n");
|
|
|
|
binfo(" textalloc: %08lx\n", (long)loadinfo->textalloc);
|
|
|
|
binfo(" dataalloc: %08lx\n", (long)loadinfo->dataalloc);
|
|
|
|
binfo(" textsize: %ld\n", (long)loadinfo->textsize);
|
|
|
|
binfo(" datasize: %ld\n", (long)loadinfo->datasize);
|
2022-01-28 07:13:41 +01:00
|
|
|
binfo(" textalign: %zu\n", loadinfo->textalign);
|
|
|
|
binfo(" dataalign: %zu\n", loadinfo->dataalign);
|
2019-08-15 22:41:42 +02:00
|
|
|
binfo(" filelen: %ld\n", (long)loadinfo->filelen);
|
2012-10-29 20:32:05 +01:00
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2019-08-15 22:41:42 +02:00
|
|
|
binfo(" ctoralloc: %08lx\n", (long)loadinfo->ctoralloc);
|
|
|
|
binfo(" ctors: %08lx\n", (long)loadinfo->ctors);
|
|
|
|
binfo(" nctors: %d\n", loadinfo->nctors);
|
|
|
|
binfo(" dtoralloc: %08lx\n", (long)loadinfo->dtoralloc);
|
|
|
|
binfo(" dtors: %08lx\n", (long)loadinfo->dtors);
|
|
|
|
binfo(" ndtors: %d\n", loadinfo->ndtors);
|
2012-10-25 18:18:20 +02:00
|
|
|
#endif
|
2019-08-15 22:41:42 +02:00
|
|
|
binfo(" symtabidx: %d\n", loadinfo->symtabidx);
|
|
|
|
binfo(" strtabidx: %d\n", loadinfo->strtabidx);
|
2012-10-25 18:18:20 +02:00
|
|
|
|
2019-08-15 22:41:42 +02:00
|
|
|
binfo("ELF Header:\n");
|
|
|
|
binfo(" e_ident: %02x %02x %02x %02x\n",
|
2012-10-25 18:18:20 +02:00
|
|
|
loadinfo->ehdr.e_ident[0], loadinfo->ehdr.e_ident[1],
|
|
|
|
loadinfo->ehdr.e_ident[2], loadinfo->ehdr.e_ident[3]);
|
2019-08-15 22:41:42 +02:00
|
|
|
binfo(" e_type: %04x\n", loadinfo->ehdr.e_type);
|
|
|
|
binfo(" e_machine: %04x\n", loadinfo->ehdr.e_machine);
|
|
|
|
binfo(" e_version: %08x\n", loadinfo->ehdr.e_version);
|
|
|
|
binfo(" e_entry: %08lx\n", (long)loadinfo->ehdr.e_entry);
|
2022-01-27 01:27:31 +01:00
|
|
|
binfo(" e_phoff: %ju\n", (uintmax_t)loadinfo->ehdr.e_phoff);
|
|
|
|
binfo(" e_shoff: %ju\n", (uintmax_t)loadinfo->ehdr.e_shoff);
|
2019-08-15 22:41:42 +02:00
|
|
|
binfo(" e_flags: %08x\n" , loadinfo->ehdr.e_flags);
|
|
|
|
binfo(" e_ehsize: %d\n", loadinfo->ehdr.e_ehsize);
|
|
|
|
binfo(" e_phentsize: %d\n", loadinfo->ehdr.e_phentsize);
|
|
|
|
binfo(" e_phnum: %d\n", loadinfo->ehdr.e_phnum);
|
|
|
|
binfo(" e_shentsize: %d\n", loadinfo->ehdr.e_shentsize);
|
|
|
|
binfo(" e_shnum: %d\n", loadinfo->ehdr.e_shnum);
|
|
|
|
binfo(" e_shstrndx: %d\n", loadinfo->ehdr.e_shstrndx);
|
2012-10-25 18:18:20 +02:00
|
|
|
|
2023-08-21 14:28:56 +02:00
|
|
|
if (loadinfo->phdr && loadinfo->ehdr.e_phnum > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_phnum; i++)
|
|
|
|
{
|
|
|
|
FAR Elf_Phdr *phdr = &loadinfo->phdr[i];
|
|
|
|
binfo("Programs %d:\n", i);
|
|
|
|
binfo(" p_type: %08jx\n", (uintmax_t)phdr->p_type);
|
|
|
|
binfo(" p_offset: %08jx\n", (uintmax_t)phdr->p_offset);
|
|
|
|
binfo(" p_vaddr: %08jx\n", (uintmax_t)phdr->p_vaddr);
|
|
|
|
binfo(" p_paddr: %08jx\n", (uintmax_t)phdr->p_paddr);
|
|
|
|
binfo(" p_filesz: %08jx\n", (uintmax_t)phdr->p_filesz);
|
|
|
|
binfo(" p_memsz: %08jx\n", (uintmax_t)phdr->p_memsz);
|
|
|
|
binfo(" p_flags: %08jx\n", (uintmax_t)phdr->p_flags);
|
|
|
|
binfo(" p_align: %08x\n", phdr->p_align);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 00:10:56 +02:00
|
|
|
if (loadinfo->shdr && loadinfo->ehdr.e_shnum > 0)
|
2012-10-24 18:46:12 +02:00
|
|
|
{
|
2012-10-26 00:10:56 +02:00
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
2012-10-25 18:18:20 +02:00
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
|
2019-08-15 22:41:42 +02:00
|
|
|
binfo("Sections %d:\n", i);
|
2022-01-27 01:27:31 +01:00
|
|
|
binfo(" sh_name: %08x\n", shdr->sh_name);
|
|
|
|
binfo(" sh_type: %08x\n", shdr->sh_type);
|
|
|
|
binfo(" sh_flags: %08jx\n", (uintmax_t)shdr->sh_flags);
|
|
|
|
binfo(" sh_addr: %08jx\n", (uintmax_t)shdr->sh_addr);
|
|
|
|
binfo(" sh_offset: %ju\n", (uintmax_t)shdr->sh_offset);
|
|
|
|
binfo(" sh_size: %ju\n", (uintmax_t)shdr->sh_size);
|
|
|
|
binfo(" sh_link: %d\n", shdr->sh_link);
|
|
|
|
binfo(" sh_info: %d\n", shdr->sh_info);
|
|
|
|
binfo(" sh_addralign: %ju\n", (uintmax_t)shdr->sh_addralign);
|
|
|
|
binfo(" sh_entsize: %ju\n", (uintmax_t)shdr->sh_entsize);
|
2012-10-25 18:18:20 +02:00
|
|
|
}
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2023-05-20 00:32:34 +02:00
|
|
|
# define elf_dumploadinfo(i)
|
2012-10-24 18:46:12 +02:00
|
|
|
#endif
|
|
|
|
|
2014-08-26 18:42:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_dumpentrypt
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ELF_DUMPBUFFER
|
|
|
|
static void elf_dumpentrypt(FAR struct binary_s *binp,
|
|
|
|
FAR struct elf_loadinfo_s *loadinfo)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* 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 18:42:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
elf_dumpbuffer("Entry code", (FAR const uint8_t *)binp->entrypt,
|
2014-08-26 18:42:42 +02:00
|
|
|
MIN(loadinfo->textsize - loadinfo->ehdr.e_entry, 512));
|
|
|
|
|
|
|
|
#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);
|
2014-08-26 18:42:42 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
2023-05-20 00:32:34 +02:00
|
|
|
# define elf_dumpentrypt(b,l)
|
2014-08-26 18:42:42 +02:00
|
|
|
#endif
|
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_loadbinary
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Verify that the file is an ELF binary and, if so, load the ELF
|
|
|
|
* binary into memory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-24 11:12:06 +02:00
|
|
|
static int elf_loadbinary(FAR struct binary_s *binp,
|
|
|
|
FAR const char *filename,
|
|
|
|
FAR const struct symtab_s *exports,
|
|
|
|
int nexports)
|
2012-10-24 18:46:12 +02:00
|
|
|
{
|
|
|
|
struct elf_loadinfo_s loadinfo; /* Contains globals for libelf */
|
2012-10-25 18:18:20 +02:00
|
|
|
int ret;
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2021-05-24 11:12:06 +02:00
|
|
|
binfo("Loading file: %s\n", filename);
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2013-01-16 20:08:23 +01:00
|
|
|
/* Initialize the ELF library to load the program binary. */
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2021-05-24 11:12:06 +02:00
|
|
|
ret = elf_init(filename, &loadinfo);
|
2012-10-24 18:46:12 +02:00
|
|
|
elf_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Failed to initialize for load of ELF program: %d\n", ret);
|
2019-10-22 09:08:44 +02:00
|
|
|
goto errout_with_init;
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the program binary */
|
|
|
|
|
|
|
|
ret = elf_load(&loadinfo);
|
|
|
|
elf_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Failed to load ELF program binary: %d\n", ret);
|
2012-10-24 18:46:12 +02:00
|
|
|
goto errout_with_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the program to the exported symbol table */
|
|
|
|
|
2023-05-26 06:22:36 +02:00
|
|
|
if (loadinfo.ehdr.e_type == ET_REL)
|
|
|
|
{
|
|
|
|
ret = elf_bind(&loadinfo, exports, nexports);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
berr("Failed to bind symbols program binary: %d\n", ret);
|
|
|
|
goto errout_with_load;
|
|
|
|
}
|
|
|
|
|
|
|
|
binp->entrypt = (main_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
|
|
|
|
}
|
|
|
|
else if (loadinfo.ehdr.e_type == ET_EXEC)
|
|
|
|
{
|
|
|
|
if (nexports > 0)
|
|
|
|
{
|
2023-06-10 19:24:08 +02:00
|
|
|
berr("Cannot bind exported symbols to a "
|
|
|
|
"fully linked executable\n");
|
2023-05-26 06:22:36 +02:00
|
|
|
ret = -ENOEXEC;
|
|
|
|
goto errout_with_load;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The entrypoint for a fully linked executable can be found directly */
|
|
|
|
|
|
|
|
binp->entrypt = (main_t)(loadinfo.ehdr.e_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2012-10-24 18:46:12 +02:00
|
|
|
{
|
2023-06-10 19:24:08 +02:00
|
|
|
berr("Unexpected elf type %d\n", loadinfo.ehdr.e_type);
|
|
|
|
ret = -ENOEXEC;
|
2023-07-02 14:49:26 +02:00
|
|
|
goto errout_with_load;
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the load information */
|
|
|
|
|
2012-10-25 18:18:20 +02:00
|
|
|
binp->stacksize = CONFIG_ELF_STACKSIZE;
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
/* Add the ELF allocation to the alloc[] only if there is no address
|
2014-05-07 21:47:52 +02:00
|
|
|
* environment. If there is an address environment, it will automatically
|
2012-12-19 18:54:26 +01:00
|
|
|
* be freed when the function exits
|
|
|
|
*
|
|
|
|
* REVISIT: If the module is loaded then unloaded, wouldn't this cause
|
|
|
|
* a memory leak?
|
|
|
|
*/
|
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2019-01-26 18:29:08 +01:00
|
|
|
/* Save the address environment in the binfmt structure. This will be
|
|
|
|
* needed when the module is executed.
|
|
|
|
*/
|
|
|
|
|
2023-04-13 10:48:06 +02:00
|
|
|
binp->addrenv = loadinfo.addrenv;
|
2023-01-27 12:45:03 +01:00
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
#else
|
2023-07-02 18:11:02 +02:00
|
|
|
binp->alloc[0] = (FAR void *)loadinfo.textalloc;
|
|
|
|
binp->alloc[1] = (FAR void *)loadinfo.dataalloc;
|
|
|
|
# ifdef CONFIG_BINFMT_CONSTRUCTORS
|
|
|
|
binp->alloc[2] = loadinfo.ctoralloc;
|
|
|
|
binp->alloc[3] = loadinfo.dtoralloc;
|
|
|
|
# endif
|
2012-12-19 18:54:26 +01:00
|
|
|
#endif
|
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2019-01-26 18:29:08 +01:00
|
|
|
/* Save information about constructors and destructors. */
|
2012-10-29 20:32:05 +01:00
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
binp->ctors = loadinfo.ctors;
|
|
|
|
binp->nctors = loadinfo.nctors;
|
2012-10-29 20:32:05 +01:00
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
binp->dtors = loadinfo.dtors;
|
|
|
|
binp->ndtors = loadinfo.ndtors;
|
2012-10-29 20:32:05 +01:00
|
|
|
#endif
|
|
|
|
|
2023-08-11 14:09:21 +02:00
|
|
|
#ifdef CONFIG_SCHED_USER_IDENTITY
|
|
|
|
/* Save IDs and mode from file system */
|
|
|
|
|
|
|
|
binp->uid = loadinfo.fileuid;
|
|
|
|
binp->gid = loadinfo.filegid;
|
|
|
|
binp->mode = loadinfo.filemode;
|
|
|
|
#endif
|
|
|
|
|
2014-08-26 18:42:42 +02:00
|
|
|
elf_dumpentrypt(binp, &loadinfo);
|
2012-10-24 18:46:12 +02:00
|
|
|
elf_uninit(&loadinfo);
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_load:
|
|
|
|
elf_unload(&loadinfo);
|
|
|
|
errout_with_init:
|
|
|
|
elf_uninit(&loadinfo);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:53:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_dumpbinary
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Generat the core dump stream as ELF structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ELF_COREDUMP
|
|
|
|
static int elf_dumpbinary(FAR struct memory_region_s *regions,
|
2023-05-22 05:43:08 +02:00
|
|
|
FAR struct lib_outstream_s *stream,
|
|
|
|
pid_t pid)
|
2021-11-17 16:53:28 +01:00
|
|
|
{
|
|
|
|
struct elf_dumpinfo_s dumpinfo;
|
|
|
|
|
|
|
|
dumpinfo.regions = regions;
|
|
|
|
dumpinfo.stream = stream;
|
2023-05-22 05:43:08 +02:00
|
|
|
dumpinfo.pid = pid;
|
2021-11-17 16:53:28 +01:00
|
|
|
|
|
|
|
return elf_coredump(&dumpinfo);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2018-08-24 14:10:50 +02:00
|
|
|
* In order to use the ELF binary format, this function must be called
|
|
|
|
* during system initialization to register the ELF binary format.
|
2012-10-24 18:46:12 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* This is a NuttX internal function so it follows the convention that
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int elf_initialize(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Register ourselves as a binfmt loader */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("Registering ELF\n");
|
2012-10-24 18:46:12 +02:00
|
|
|
|
|
|
|
ret = register_binfmt(&g_elfbinfmt);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Failed to register binfmt: %d\n", ret);
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_uninitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unregister the ELF binary loader
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void elf_uninitialize(void)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
unregister_binfmt(&g_elfbinfmt);
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_ELF */
|