mirror of
https://github.com/apache/nuttx.git
synced 2026-10-03 12:18:06 +00:00
exec() of an FDPIC module now works. The loader already places such an object and binds it; what was missing is everything binfmt has to carry across from the load to the running task. The task needs the module's data base in its PIC base register. binfmt builds a D-Space for any object with a GOT, taking the base from the .got section address; an FDPIC object names it in DT_PLTGOT instead, which the loader has already translated, so the two are the same idea reached by different routes and both are what up_initial_state() installs. Constructors are not binfmt's business. A module carries its own crt0, which walks .init_array on the task that runs the module and then calls main, so they run in the module's own context and with its own data base. For a module that arrives through dlopen(), libelf_insert() walks the array instead, and it enters each entry through fdpic_invoke() because a descriptor resolved on the calling task carries the wrong base. The read-only segment of a module that executes in place is held by a filesystem pin. The load takes it, and the module owns it from the point where nothing can fail any more; it is given back when the task that runs the module exits. The pin is held through a reference to the file rather than a descriptor, because the descriptor belongs to the task that called the loader and the release happens on another one. libelf_remove() and libelf_uninit() give back what an FDPIC module holds: the pin, and the writable segment, while the read-only one is media rather than an allocation and must not be freed. Built for mps3-an547:picostest with CONFIG_FDPIC both ways. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
376 lines
9.8 KiB
C
376 lines
9.8 KiB
C
/****************************************************************************
|
|
* binfmt/elf.c
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* 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/param.h>
|
|
#include <sys/types.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <nuttx/debug.h>
|
|
#include <errno.h>
|
|
|
|
#include <nuttx/arch.h>
|
|
#include <nuttx/binfmt/binfmt.h>
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include "binfmt.h"
|
|
|
|
#ifdef CONFIG_ELF
|
|
|
|
/****************************************************************************
|
|
* Pre-processor Definitions
|
|
****************************************************************************/
|
|
|
|
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT
|
|
* have to be defined or CONFIG_LIBC_ELF_DUMPBUFFER does nothing.
|
|
*/
|
|
|
|
#if !defined(CONFIG_DEBUG_INFO) || !defined(CONFIG_DEBUG_BINFMT)
|
|
# undef CONFIG_LIBC_ELF_DUMPBUFFER
|
|
#endif
|
|
|
|
#ifndef CONFIG_ELF_STACKSIZE
|
|
# define CONFIG_ELF_STACKSIZE 2048
|
|
#endif
|
|
|
|
/****************************************************************************
|
|
* Private Function Prototypes
|
|
****************************************************************************/
|
|
|
|
static int elf_loadbinary(FAR struct binary_s *binp,
|
|
FAR const char *filename,
|
|
FAR const struct symtab_s *exports,
|
|
int nexports);
|
|
|
|
static int elf_unloadbinary(FAR struct binary_s *binp);
|
|
|
|
/****************************************************************************
|
|
* Private Data
|
|
****************************************************************************/
|
|
|
|
static struct binfmt_s g_elfbinfmt =
|
|
{
|
|
NULL, /* next */
|
|
elf_loadbinary, /* load */
|
|
elf_unloadbinary, /* unload */
|
|
};
|
|
|
|
/****************************************************************************
|
|
* Private Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* 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,
|
|
FAR const char *filename,
|
|
FAR const struct symtab_s *exports,
|
|
int nexports)
|
|
{
|
|
struct mod_loadinfo_s loadinfo;
|
|
Elf_Sym sym;
|
|
int ret;
|
|
|
|
binfo("Loading file: %s\n", filename);
|
|
|
|
/* Initialize the ELF library to load the program binary. */
|
|
|
|
ret = libelf_initialize(filename, &loadinfo);
|
|
libelf_dumploadinfo(&loadinfo);
|
|
if (ret != 0)
|
|
{
|
|
berr("Failed to initialize to load ELF program binary: %d\n", ret);
|
|
goto errout_with_init;
|
|
}
|
|
|
|
#ifdef CONFIG_SCHED_USER_IDENTITY
|
|
/* Save IDs and mode from file system before loading segments */
|
|
|
|
binp->uid = loadinfo.fileuid;
|
|
binp->gid = loadinfo.filegid;
|
|
binp->mode = loadinfo.filemode;
|
|
|
|
ret = binfmt_checkexecperm(binp);
|
|
if (ret < 0)
|
|
{
|
|
goto errout_with_init;
|
|
}
|
|
#endif
|
|
|
|
/* Load the program binary */
|
|
|
|
ret = libelf_load_with_addrenv(&loadinfo);
|
|
libelf_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 */
|
|
|
|
if (loadinfo.ehdr.e_type == ET_REL || loadinfo.gotsize != 0)
|
|
{
|
|
ret = libelf_bind(&binp->mod, &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)
|
|
{
|
|
berr("Cannot bind exported symbols to a "
|
|
"fully linked executable\n");
|
|
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
|
|
{
|
|
berr("Unexpected elf type %d\n", loadinfo.ehdr.e_type);
|
|
ret = -ENOEXEC;
|
|
goto errout_with_load;
|
|
}
|
|
|
|
/* Return the load information */
|
|
|
|
ret = libelf_findsymbol(&loadinfo, "nx_stacksize", &sym);
|
|
if (ret == 0)
|
|
{
|
|
binp->stacksize = sym.st_value;
|
|
}
|
|
else
|
|
{
|
|
binp->stacksize = CONFIG_ELF_STACKSIZE;
|
|
}
|
|
|
|
ret = libelf_findsymbol(&loadinfo, "nx_priority", &sym);
|
|
if (ret == 0)
|
|
{
|
|
binp->priority = sym.st_value;
|
|
}
|
|
else
|
|
{
|
|
binp->priority = SCHED_PRIORITY_DEFAULT;
|
|
}
|
|
|
|
#ifdef CONFIG_SCHED_USER_IDENTITY
|
|
ret = libelf_findsymbol(&loadinfo, "nx_uid", &sym);
|
|
if (ret == 0)
|
|
{
|
|
binp->uid = sym.st_value;
|
|
}
|
|
else
|
|
{
|
|
binp->uid = 0;
|
|
}
|
|
|
|
ret = libelf_findsymbol(&loadinfo, "nx_gid", &sym);
|
|
if (ret == 0)
|
|
{
|
|
binp->gid = sym.st_value;
|
|
}
|
|
else
|
|
{
|
|
binp->gid = 0;
|
|
}
|
|
|
|
ret = libelf_findsymbol(&loadinfo, "nx_mode", &sym);
|
|
if (ret == 0)
|
|
{
|
|
binp->mode = sym.st_value;
|
|
}
|
|
else
|
|
{
|
|
binp->mode = 0;
|
|
}
|
|
#endif
|
|
|
|
/* 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.
|
|
*/
|
|
|
|
binp->addrenv = loadinfo.addrenv;
|
|
|
|
#else
|
|
# ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
|
|
if (loadinfo.ehdr.e_type == ET_REL)
|
|
{
|
|
binp->mod.sectalloc = (FAR void *)loadinfo.sectalloc;
|
|
binp->mod.nsect = loadinfo.ehdr.e_shnum;
|
|
}
|
|
# endif
|
|
|
|
binp->mod.textalloc = (FAR void *)loadinfo.textalloc;
|
|
binp->mod.dataalloc = (FAR void *)loadinfo.datastart;
|
|
binp->mod.gotbase = loadinfo.fdpic ? loadinfo.gotbase : 0;
|
|
# ifdef CONFIG_BINFMT_CONSTRUCTORS
|
|
binp->mod.initarr = loadinfo.initarr;
|
|
binp->mod.finiarr = loadinfo.finiarr;
|
|
# endif
|
|
#endif
|
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
|
/* Save information about constructors and destructors. */
|
|
|
|
binp->mod.initarr = loadinfo.initarr;
|
|
binp->mod.ninit = loadinfo.ninit;
|
|
|
|
binp->mod.finiarr = loadinfo.finiarr;
|
|
binp->mod.nfini = loadinfo.nfini;
|
|
#endif
|
|
|
|
libelf_dumpentrypt(&loadinfo);
|
|
#ifdef CONFIG_PIC
|
|
if (loadinfo.gotsize != 0)
|
|
{
|
|
FAR struct dspace_s *dspaces = kmm_zalloc(sizeof(struct dspace_s));
|
|
|
|
if (dspaces == NULL)
|
|
{
|
|
ret = -ENOMEM;
|
|
goto errout_with_load;
|
|
}
|
|
|
|
dspaces->region = (FAR void *)loadinfo.gotbase;
|
|
dspaces->crefs = 1;
|
|
binp->picbase = (FAR void *)dspaces;
|
|
}
|
|
#endif
|
|
|
|
#ifdef HAVE_LIBC_ELF_PIN
|
|
/* Past the last thing that can fail, so the module owns the pin: it is
|
|
* given back when the task that runs the module exits.
|
|
*/
|
|
|
|
binp->mod.pinfile = loadinfo.pinfile;
|
|
loadinfo.pinfile = NULL;
|
|
#endif
|
|
|
|
libelf_uninitialize(&loadinfo);
|
|
return OK;
|
|
|
|
errout_with_load:
|
|
libelf_unload(&loadinfo);
|
|
errout_with_init:
|
|
libelf_uninitialize(&loadinfo);
|
|
return ret;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: elf_unloadbinary
|
|
*
|
|
* Description:
|
|
* Unload the ELF binary that was loaded into memory by elf_loadbinary.
|
|
*
|
|
****************************************************************************/
|
|
|
|
static int elf_unloadbinary(FAR struct binary_s *binp)
|
|
{
|
|
binfo("Unloading %p\n", binp);
|
|
libelf_uninit(&binp->mod);
|
|
|
|
return OK;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* 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 */
|