2012-10-25 00:46:12 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/elf.c
|
|
|
|
*
|
2021-02-03 21:47:23 +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
|
2012-10-25 00:46:12 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-10-25 00:46:12 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +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.
|
2012-10-25 00:46:12 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-02-01 21:41:12 +08:00
|
|
|
#include <sys/param.h>
|
2012-10-25 00:46:12 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2014-08-27 00:42:42 +08:00
|
|
|
#include <nuttx/arch.h>
|
2012-10-25 04:19:44 +08:00
|
|
|
#include <nuttx/binfmt/binfmt.h>
|
|
|
|
#include <nuttx/binfmt/elf.h>
|
2012-10-25 00:46:12 +08:00
|
|
|
|
2014-08-27 00:42:42 +08:00
|
|
|
#include "libelf/libelf.h"
|
|
|
|
|
2012-10-25 00:46:12 +08:00
|
|
|
#ifdef CONFIG_ELF
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-22 16:24:15 +08:00
|
|
|
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT
|
|
|
|
* have to be defined or CONFIG_ELF_DUMPBUFFER does nothing.
|
2012-10-25 00:46:12 +08:00
|
|
|
*/
|
|
|
|
|
2023-07-03 00:11:02 +08:00
|
|
|
#if !defined(CONFIG_DEBUG_INFO) || !defined(CONFIG_DEBUG_BINFMT)
|
2012-10-25 00:46:12 +08:00
|
|
|
# undef CONFIG_ELF_DUMPBUFFER
|
|
|
|
#endif
|
|
|
|
|
2012-10-26 00:18:20 +08:00
|
|
|
#ifndef CONFIG_ELF_STACKSIZE
|
|
|
|
# define CONFIG_ELF_STACKSIZE 2048
|
|
|
|
#endif
|
|
|
|
|
2012-10-25 00:46:12 +08:00
|
|
|
#ifdef CONFIG_ELF_DUMPBUFFER
|
2023-05-20 06:32:34 +08:00
|
|
|
# define elf_dumpbuffer(m,b,n) binfodumpbuffer(m,b,n)
|
2012-10-25 00:46:12 +08:00
|
|
|
#else
|
2023-05-20 06:32:34 +08:00
|
|
|
# define elf_dumpbuffer(m,b,n)
|
2012-10-25 00:46:12 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-24 17:12:06 +08: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 23:53:28 +08:00
|
|
|
#ifdef CONFIG_ELF_COREDUMP
|
|
|
|
static int elf_dumpbinary(FAR struct memory_region_s *regions,
|
2023-05-22 11:43:08 +08:00
|
|
|
FAR struct lib_outstream_s *stream,
|
|
|
|
pid_t pid);
|
2021-11-17 23:53:28 +08:00
|
|
|
#endif
|
2016-06-12 04:14:08 +08:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
|
2012-10-25 00:46:12 +08: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 06:58:10 +08:00
|
|
|
NULL, /* unload */
|
2021-11-17 23:53:28 +08:00
|
|
|
#ifdef CONFIG_ELF_COREDUMP
|
|
|
|
elf_dumpbinary, /* coredump */
|
|
|
|
#endif
|
2012-10-25 00:46:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_dumploadinfo
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-12 04:14:08 +08:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
|
2012-10-25 00:46:12 +08:00
|
|
|
static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo)
|
|
|
|
{
|
2012-10-26 00:18:20 +08:00
|
|
|
int i;
|
2012-10-25 00:46:12 +08:00
|
|
|
|
2019-08-16 04:41:42 +08: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 14:13:41 +08:00
|
|
|
binfo(" textalign: %zu\n", loadinfo->textalign);
|
|
|
|
binfo(" dataalign: %zu\n", loadinfo->dataalign);
|
2019-08-16 04:41:42 +08:00
|
|
|
binfo(" filelen: %ld\n", (long)loadinfo->filelen);
|
2012-10-30 03:32:05 +08:00
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2019-08-16 04:41:42 +08: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-26 00:18:20 +08:00
|
|
|
#endif
|
2019-08-16 04:41:42 +08:00
|
|
|
binfo(" symtabidx: %d\n", loadinfo->symtabidx);
|
|
|
|
binfo(" strtabidx: %d\n", loadinfo->strtabidx);
|
2012-10-26 00:18:20 +08:00
|
|
|
|
2019-08-16 04:41:42 +08:00
|
|
|
binfo("ELF Header:\n");
|
|
|
|
binfo(" e_ident: %02x %02x %02x %02x\n",
|
2012-10-26 00:18:20 +08:00
|
|
|
loadinfo->ehdr.e_ident[0], loadinfo->ehdr.e_ident[1],
|
|
|
|
loadinfo->ehdr.e_ident[2], loadinfo->ehdr.e_ident[3]);
|
2019-08-16 04:41:42 +08: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 08:27:31 +08: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-16 04:41:42 +08: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-26 00:18:20 +08:00
|
|
|
|
2023-08-21 20:28:56 +08: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 06:10:56 +08:00
|
|
|
if (loadinfo->shdr && loadinfo->ehdr.e_shnum > 0)
|
2012-10-25 00:46:12 +08:00
|
|
|
{
|
2012-10-26 06:10:56 +08:00
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
2012-10-26 00:18:20 +08:00
|
|
|
{
|
2020-02-08 07:10:23 +08:00
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
|
2019-08-16 04:41:42 +08:00
|
|
|
binfo("Sections %d:\n", i);
|
2022-01-27 08:27:31 +08: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-26 00:18:20 +08:00
|
|
|
}
|
2012-10-25 00:46:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2023-05-20 06:32:34 +08:00
|
|
|
# define elf_dumploadinfo(i)
|
2012-10-25 00:46:12 +08:00
|
|
|
#endif
|
|
|
|
|
2014-08-27 00:42:42 +08: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-12 05:50:49 +08:00
|
|
|
berr("ERROR: elf_addrenv_select() failed: %d\n", ret);
|
2014-08-27 00:42:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-09 09:20:17 +08:00
|
|
|
elf_dumpbuffer("Entry code", (FAR const uint8_t *)binp->entrypt,
|
2014-08-27 00:42:42 +08: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-12 05:50:49 +08:00
|
|
|
berr("ERROR: elf_addrenv_restore() failed: %d\n", ret);
|
2014-08-27 00:42:42 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
2023-05-20 06:32:34 +08:00
|
|
|
# define elf_dumpentrypt(b,l)
|
2014-08-27 00:42:42 +08:00
|
|
|
#endif
|
|
|
|
|
2012-10-25 00:46:12 +08: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 17:12:06 +08:00
|
|
|
static int elf_loadbinary(FAR struct binary_s *binp,
|
|
|
|
FAR const char *filename,
|
|
|
|
FAR const struct symtab_s *exports,
|
|
|
|
int nexports)
|
2012-10-25 00:46:12 +08:00
|
|
|
{
|
|
|
|
struct elf_loadinfo_s loadinfo; /* Contains globals for libelf */
|
2012-10-26 00:18:20 +08:00
|
|
|
int ret;
|
2012-10-25 00:46:12 +08:00
|
|
|
|
2021-05-24 17:12:06 +08:00
|
|
|
binfo("Loading file: %s\n", filename);
|
2012-10-25 00:46:12 +08:00
|
|
|
|
2013-01-17 03:08:23 +08:00
|
|
|
/* Initialize the ELF library to load the program binary. */
|
2012-10-25 00:46:12 +08:00
|
|
|
|
2021-05-24 17:12:06 +08:00
|
|
|
ret = elf_init(filename, &loadinfo);
|
2012-10-25 00:46:12 +08:00
|
|
|
elf_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
berr("Failed to initialize for load of ELF program: %d\n", ret);
|
2019-10-22 15:08:44 +08:00
|
|
|
goto errout_with_init;
|
2012-10-25 00:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the program binary */
|
|
|
|
|
|
|
|
ret = elf_load(&loadinfo);
|
|
|
|
elf_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
berr("Failed to load ELF program binary: %d\n", ret);
|
2012-10-25 00:46:12 +08:00
|
|
|
goto errout_with_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the program to the exported symbol table */
|
|
|
|
|
2023-05-26 12:22:36 +08: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-11 01:24:08 +08:00
|
|
|
berr("Cannot bind exported symbols to a "
|
|
|
|
"fully linked executable\n");
|
2023-05-26 12:22:36 +08: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-25 00:46:12 +08:00
|
|
|
{
|
2023-06-11 01:24:08 +08:00
|
|
|
berr("Unexpected elf type %d\n", loadinfo.ehdr.e_type);
|
|
|
|
ret = -ENOEXEC;
|
2023-07-02 20:49:26 +08:00
|
|
|
goto errout_with_load;
|
2012-10-25 00:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the load information */
|
|
|
|
|
2012-10-26 00:18:20 +08:00
|
|
|
binp->stacksize = CONFIG_ELF_STACKSIZE;
|
2012-10-25 00:46:12 +08:00
|
|
|
|
2012-12-20 01:54:26 +08:00
|
|
|
/* Add the ELF allocation to the alloc[] only if there is no address
|
2014-05-08 03:47:52 +08:00
|
|
|
* environment. If there is an address environment, it will automatically
|
2012-12-20 01:54:26 +08: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 20:42:11 +08:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2019-01-27 01:29:08 +08:00
|
|
|
/* Save the address environment in the binfmt structure. This will be
|
|
|
|
* needed when the module is executed.
|
|
|
|
*/
|
|
|
|
|
2023-04-13 16:48:06 +08:00
|
|
|
binp->addrenv = loadinfo.addrenv;
|
2023-01-27 19:45:03 +08:00
|
|
|
|
2012-12-20 01:54:26 +08:00
|
|
|
#else
|
2023-07-03 00:11:02 +08: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-20 01:54:26 +08:00
|
|
|
#endif
|
|
|
|
|
2012-10-30 03:32:05 +08:00
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2019-01-27 01:29:08 +08:00
|
|
|
/* Save information about constructors and destructors. */
|
2012-10-30 03:32:05 +08:00
|
|
|
|
2023-07-03 00:11:02 +08:00
|
|
|
binp->ctors = loadinfo.ctors;
|
|
|
|
binp->nctors = loadinfo.nctors;
|
2012-10-30 03:32:05 +08:00
|
|
|
|
2023-07-03 00:11:02 +08:00
|
|
|
binp->dtors = loadinfo.dtors;
|
|
|
|
binp->ndtors = loadinfo.ndtors;
|
2012-10-30 03:32:05 +08:00
|
|
|
#endif
|
|
|
|
|
2023-08-11 20:09:21 +08: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-27 00:42:42 +08:00
|
|
|
elf_dumpentrypt(binp, &loadinfo);
|
2012-10-25 00:46:12 +08:00
|
|
|
elf_uninit(&loadinfo);
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_load:
|
|
|
|
elf_unload(&loadinfo);
|
|
|
|
errout_with_init:
|
|
|
|
elf_uninit(&loadinfo);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-11-17 23:53:28 +08: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 11:43:08 +08:00
|
|
|
FAR struct lib_outstream_s *stream,
|
|
|
|
pid_t pid)
|
2021-11-17 23:53:28 +08:00
|
|
|
{
|
|
|
|
struct elf_dumpinfo_s dumpinfo;
|
|
|
|
|
|
|
|
dumpinfo.regions = regions;
|
|
|
|
dumpinfo.stream = stream;
|
2023-05-22 11:43:08 +08:00
|
|
|
dumpinfo.pid = pid;
|
2021-11-17 23:53:28 +08:00
|
|
|
|
|
|
|
return elf_coredump(&dumpinfo);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-25 00:46:12 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2018-08-24 20:10:50 +08: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-25 00:46:12 +08: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-12 01:59:51 +08:00
|
|
|
binfo("Registering ELF\n");
|
2012-10-25 00:46:12 +08:00
|
|
|
|
|
|
|
ret = register_binfmt(&g_elfbinfmt);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
berr("Failed to register binfmt: %d\n", ret);
|
2012-10-25 00:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_uninitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unregister the ELF binary loader
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void elf_uninitialize(void)
|
|
|
|
{
|
2020-01-03 00:49:34 +08:00
|
|
|
unregister_binfmt(&g_elfbinfmt);
|
2012-10-25 00:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_ELF */
|