123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- /****************************************************************************
- * binfmt/elf.c
- *
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * 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.
- *
- ****************************************************************************/
- /****************************************************************************
- * Included Files
- ****************************************************************************/
- #include <nuttx/config.h>
- #include <sys/types.h>
- #include <stdint.h>
- #include <string.h>
- #include <debug.h>
- #include <errno.h>
- #include <nuttx/arch.h>
- #include <nuttx/binfmt/binfmt.h>
- #include <nuttx/binfmt/elf.h>
- #include "libelf/libelf.h"
- #ifdef CONFIG_ELF
- /****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
- /* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT
- * have to be defined or CONFIG_ELF_DUMPBUFFER does nothing.
- */
- #if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT)
- # undef CONFIG_ELF_DUMPBUFFER
- #endif
- #ifndef CONFIG_ELF_STACKSIZE
- # define CONFIG_ELF_STACKSIZE 2048
- #endif
- #ifdef CONFIG_ELF_DUMPBUFFER
- # define elf_dumpbuffer(m,b,n) binfodumpbuffer(m,b,n)
- #else
- # define elf_dumpbuffer(m,b,n)
- #endif
- #ifndef MIN
- # define MIN(a,b) (a < b ? a : b)
- #endif
- /****************************************************************************
- * Private Function Prototypes
- ****************************************************************************/
- static int elf_loadbinary(FAR struct binary_s *binp);
- #if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
- static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo);
- #endif
- /****************************************************************************
- * Private Data
- ****************************************************************************/
- static struct binfmt_s g_elfbinfmt =
- {
- NULL, /* next */
- elf_loadbinary, /* load */
- NULL, /* unload */
- };
- /****************************************************************************
- * Private Functions
- ****************************************************************************/
- /****************************************************************************
- * Name: elf_dumploadinfo
- ****************************************************************************/
- #if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
- static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo)
- {
- int i;
- 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);
- binfo(" filelen: %ld\n", (long)loadinfo->filelen);
- #ifdef CONFIG_BINFMT_CONSTRUCTORS
- 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);
- #endif
- binfo(" filfd: %d\n", loadinfo->filfd);
- binfo(" symtabidx: %d\n", loadinfo->symtabidx);
- binfo(" strtabidx: %d\n", loadinfo->strtabidx);
- binfo("ELF Header:\n");
- binfo(" e_ident: %02x %02x %02x %02x\n",
- loadinfo->ehdr.e_ident[0], loadinfo->ehdr.e_ident[1],
- loadinfo->ehdr.e_ident[2], loadinfo->ehdr.e_ident[3]);
- 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);
- binfo(" e_phoff: %d\n", loadinfo->ehdr.e_phoff);
- binfo(" e_shoff: %d\n", loadinfo->ehdr.e_shoff);
- 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);
- if (loadinfo->shdr && loadinfo->ehdr.e_shnum > 0)
- {
- for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
- {
- FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
- binfo("Sections %d:\n", i);
- binfo(" sh_name: %08x\n", shdr->sh_name);
- binfo(" sh_type: %08x\n", shdr->sh_type);
- binfo(" sh_flags: %08x\n", shdr->sh_flags);
- binfo(" sh_addr: %08x\n", shdr->sh_addr);
- binfo(" sh_offset: %d\n", shdr->sh_offset);
- binfo(" sh_size: %d\n", shdr->sh_size);
- binfo(" sh_link: %d\n", shdr->sh_link);
- binfo(" sh_info: %d\n", shdr->sh_info);
- binfo(" sh_addralign: %d\n", shdr->sh_addralign);
- binfo(" sh_entsize: %d\n", shdr->sh_entsize);
- }
- }
- }
- #else
- # define elf_dumploadinfo(i)
- #endif
- /****************************************************************************
- * 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)
- {
- berr("ERROR: elf_addrenv_select() failed: %d\n", ret);
- return;
- }
- #endif
- elf_dumpbuffer("Entry code", (FAR const uint8_t *)binp->entrypt,
- 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)
- {
- berr("ERROR: elf_addrenv_restore() failed: %d\n", ret);
- }
- #endif
- }
- #else
- # define elf_dumpentrypt(b,l)
- #endif
- /****************************************************************************
- * Name: elf_loadbinary
- *
- * Description:
- * Verify that the file is an ELF binary and, if so, load the ELF
- * binary into memory
- *
- ****************************************************************************/
- static int elf_loadbinary(FAR struct binary_s *binp)
- {
- struct elf_loadinfo_s loadinfo; /* Contains globals for libelf */
- int ret;
- binfo("Loading file: %s\n", binp->filename);
- /* Initialize the ELF library to load the program binary. */
- ret = elf_init(binp->filename, &loadinfo);
- elf_dumploadinfo(&loadinfo);
- if (ret != 0)
- {
- berr("Failed to initialize for load of ELF program: %d\n", ret);
- goto errout;
- }
- /* Load the program binary */
- ret = elf_load(&loadinfo);
- elf_dumploadinfo(&loadinfo);
- if (ret != 0)
- {
- berr("Failed to load ELF program binary: %d\n", ret);
- goto errout_with_init;
- }
- /* Bind the program to the exported symbol table */
- ret = elf_bind(&loadinfo, binp->exports, binp->nexports);
- if (ret != 0)
- {
- berr("Failed to bind symbols program binary: %d\n", ret);
- goto errout_with_load;
- }
- /* Return the load information */
- binp->entrypt = (main_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
- binp->stacksize = CONFIG_ELF_STACKSIZE;
- /* Add the ELF allocation to the alloc[] only if there is no address
- * environment. If there is an address environment, it will automatically
- * be freed when the function exits
- *
- * REVISIT: If the module is loaded then unloaded, wouldn't this cause
- * a memory leak?
- */
- #ifdef CONFIG_ARCH_ADDRENV
- /* Save the address environment in the binfmt structure. This will be
- * needed when the module is executed.
- */
- up_addrenv_clone(&loadinfo.addrenv, &binp->addrenv);
- #else
- binp->alloc[0] = (FAR void *)loadinfo.textalloc;
- #ifdef CONFIG_BINFMT_CONSTRUCTORS
- binp->alloc[1] = loadinfo.ctoralloc;
- binp->alloc[2] = loadinfo.dtoralloc;
- #endif
- #endif
- #ifdef CONFIG_BINFMT_CONSTRUCTORS
- /* Save information about constructors and destructors. */
- binp->ctors = loadinfo.ctors;
- binp->nctors = loadinfo.nctors;
- binp->dtors = loadinfo.dtors;
- binp->ndtors = loadinfo.ndtors;
- #endif
- elf_dumpentrypt(binp, &loadinfo);
- elf_uninit(&loadinfo);
- return OK;
- errout_with_load:
- elf_unload(&loadinfo);
- errout_with_init:
- elf_uninit(&loadinfo);
- errout:
- return ret;
- }
- /****************************************************************************
- * Public Functions
- ****************************************************************************/
- /****************************************************************************
- * Name: elf_initialize
- *
- * Description:
- * In order to use the ELF binary format, this function must be called
- * during system initialization to register the ELF binary format.
- *
- * 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 */
- binfo("Registering ELF\n");
- ret = register_binfmt(&g_elfbinfmt);
- if (ret != 0)
- {
- berr("Failed to register binfmt: %d\n", ret);
- }
- return ret;
- }
- /****************************************************************************
- * Name: elf_uninitialize
- *
- * Description:
- * Unregister the ELF binary loader
- *
- * Returned Value:
- * None
- *
- ****************************************************************************/
- void elf_uninitialize(void)
- {
- unregister_binfmt(&g_elfbinfmt);
- }
- #endif /* CONFIG_ELF */
|