nuttx/libs/libc/elf/elf_insert.c

468 lines
14 KiB
C
Raw Normal View History

/****************************************************************************
* libs/libc/elf/elf_insert.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 <assert.h>
#include <nuttx/debug.h>
#include <errno.h>
#include <sys/param.h>
#include <nuttx/lib/lib.h>
#include <nuttx/lib/elf.h>
#include "elf.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: libelf_dumploadinfo
*
* Description:
* Dump the load information to debug output.
*
****************************************************************************/
#ifdef CONFIG_DEBUG_BINFMT_INFO
void libelf_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo)
{
int i;
binfo("LOAD_INFO:\n");
binfo(" textalloc: %08lx\n", (long)loadinfo->textalloc);
binfo(" datastart: %08lx\n", (long)loadinfo->datastart);
binfo(" textsize: %ld\n", (long)loadinfo->textsize);
binfo(" datasize: %ld\n", (long)loadinfo->datasize);
binfo(" textalign: %zu\n", loadinfo->textalign);
binfo(" dataalign: %zu\n", loadinfo->dataalign);
binfo(" filelen: %ld\n", (long)loadinfo->filelen);
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: %ju\n", (uintmax_t)loadinfo->ehdr.e_phoff);
binfo(" e_shoff: %ju\n", (uintmax_t)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);
# ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
if (loadinfo->ehdr.e_type == ET_REL)
{
binfo(" sh_alloc: %08jx\n",
(uintmax_t)loadinfo->sectalloc[i]);
}
# endif
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);
}
}
}
/****************************************************************************
* Name: libelf_dumpmodule
****************************************************************************/
void libelf_dumpmodule(FAR struct module_s *modp)
{
binfo("Module:\n");
#ifdef HAVE_LIBC_ELF_NAMES
binfo(" modname: %s\n", modp->modname);
#endif
binfo(" textalloc: %08lx\n", (long)modp->textalloc);
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
binfo(" dataalloc: %08lx\n", (long)modp->dataalloc);
binfo(" textsize: %ld\n", (long)modp->textsize);
#endif
#ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
binfo(" sectalloc: %p\n", modp->sectalloc);
binfo(" nsect: %ld\n", (long)modp->nsect);
for (int i = 0; i < modp->nsect; i++)
{
binfo(" sectalloc[%d]: %p\n", i, modp->sectalloc[i]);
}
#endif
#if CONFIG_LIBC_ELF_MAXDEPEND > 0
binfo(" dependents: %d\n", modp->dependents);
for (int i = 0; i < modp->dependents; i++)
{
# ifdef HAVE_LIBC_ELF_NAMES
binfo("%d %s\n", i, modp->dependencies[i]->modname);
# else
binfo("%d\n", i);
# endif
libelf_dumpmodule(modp->dependencies[i]);
}
#endif
binfo(" finiarr: %08lx\n", (long)modp->finiarr);
binfo(" nfini: %d\n", modp->nfini);
}
#endif
/****************************************************************************
* Name: elf_dumpentrypt
****************************************************************************/
#ifdef CONFIG_LIBC_ELF_DUMPBUFFER
void libelf_dumpentrypt(FAR struct mod_loadinfo_s *loadinfo)
{
FAR const uint8_t *entry;
#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. libelf_addrenv_select() will
* temporarily instantiate that address space.
*/
if (loadinfo->addrenv != NULL)
{
ret = libelf_addrenv_select(loadinfo);
if (ret < 0)
{
berr("ERROR: libelf_addrenv_select() failed: %d\n", ret);
return;
}
}
#endif
if (loadinfo->ehdr.e_type == ET_REL)
{
entry = (FAR const uint8_t *)
((uintptr_t)loadinfo->textalloc + loadinfo->ehdr.e_entry);
}
else if (loadinfo->ehdr.e_type == ET_EXEC)
{
entry = (FAR const uint8_t *)loadinfo->ehdr.e_entry;
}
else
{
entry = (FAR const uint8_t *)loadinfo->textalloc;
}
libelf_dumpbuffer("Entry code", entry,
MIN(loadinfo->textsize - loadinfo->ehdr.e_entry, 512));
#ifdef CONFIG_ARCH_ADDRENV
/* Restore the original address environment */
if (loadinfo->addrenv != NULL)
{
ret = libelf_addrenv_restore(loadinfo);
if (ret < 0)
{
berr("ERROR: libelf_addrenv_restore() failed: %d\n", ret);
}
}
#endif
}
#endif
/****************************************************************************
* Name: libelf_loadsymtab
*
* Description:
* Load the symbol table into memory.
*
****************************************************************************/
static int libelf_loadsymtab(FAR struct module_s *modp,
FAR struct mod_loadinfo_s *loadinfo)
{
FAR Elf_Shdr *symhdr = &loadinfo->shdr[loadinfo->symtabidx];
FAR Elf_Sym *sym = lib_malloc(symhdr->sh_size);
int ret;
int i;
if (sym == NULL)
{
return -ENOMEM;
}
ret = libelf_read(loadinfo, (FAR uint8_t *)sym, symhdr->sh_size,
symhdr->sh_offset);
if (ret < 0)
{
berr("Failed to read symbol table\n");
lib_free(sym);
return ret;
}
for (i = 0; i < symhdr->sh_size / sizeof(Elf_Sym); i++)
{
if (sym[i].st_shndx != SHN_UNDEF &&
sym[i].st_shndx < loadinfo->ehdr.e_shnum)
{
libs/libc/elf: Publish FDPIC functions as descriptors for dlsym. A module that dlopen()s a library gets back function addresses from dlsym() and calls them. Under FDPIC a bare code address is not enough: the callee needs its own data base as well, so what dlsym() returns has to be a function descriptor. The exported symbol table carries no type information -- symtab_s is a name and a value, and its own comment says typing would have to be added to support anything but function pointers -- so by the time dlsym() is asked there is no way to tell a function from an object. libelf_insertsymtab() is the last point that can: st_info is still in hand there. So an FDPIC object's exported functions are published as the address of a descriptor carved from the module's pool, and dlopen(), dlsym() and the module registry need no knowledge of FDPIC at all. The pool is sized for the dynamic symbol table as well as the relocations, since both can draw from it. That leaves the symbol values themselves, which were wrong for any ET_DYN object. libelf_loadsymtab() adds the symbol's section address to its value, which is right for ET_REL, where the section address is where the section was actually placed and the value is relative to it. In a shared object both are already full link-time addresses, so adding them counts the section twice. It needs translating onto wherever the object was placed instead. Library data is shared between everything that dlopen()s it, because the registry holds one instance per name. Giving each user its own copy would mean teaching the registry about instances, which is a much larger change to shared code; an executable loaded through exec() already gets its own data, since that path loads a fresh copy each time. Built and run on lm3s6965-ek with the examples/elf ROMFS; the FDPIC module continues to load, relocate and call through its own descriptors. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-08-03 11:34:35 +02:00
if (loadinfo->ehdr.e_type == ET_DYN)
{
/* A shared object's symbol value is already the full
* link-time address. It only needs translating onto where
* the object was placed.
*/
libs/libc/elf: Publish FDPIC functions as descriptors for dlsym. A module that dlopen()s a library gets back function addresses from dlsym() and calls them. Under FDPIC a bare code address is not enough: the callee needs its own data base as well, so what dlsym() returns has to be a function descriptor. The exported symbol table carries no type information -- symtab_s is a name and a value, and its own comment says typing would have to be added to support anything but function pointers -- so by the time dlsym() is asked there is no way to tell a function from an object. libelf_insertsymtab() is the last point that can: st_info is still in hand there. So an FDPIC object's exported functions are published as the address of a descriptor carved from the module's pool, and dlopen(), dlsym() and the module registry need no knowledge of FDPIC at all. The pool is sized for the dynamic symbol table as well as the relocations, since both can draw from it. That leaves the symbol values themselves, which were wrong for any ET_DYN object. libelf_loadsymtab() adds the symbol's section address to its value, which is right for ET_REL, where the section address is where the section was actually placed and the value is relative to it. In a shared object both are already full link-time addresses, so adding them counts the section twice. It needs translating onto wherever the object was placed instead. Library data is shared between everything that dlopen()s it, because the registry holds one instance per name. Giving each user its own copy would mean teaching the registry about instances, which is a much larger change to shared code; an executable loaded through exec() already gets its own data, since that path loads a fresh copy each time. Built and run on lm3s6965-ek with the examples/elf ROMFS; the FDPIC module continues to load, relocate and call through its own descriptors. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-08-03 11:34:35 +02:00
sym[i].st_value = libelf_addr(loadinfo, sym[i].st_value);
}
else
{
FAR Elf_Shdr *s = &loadinfo->shdr[sym[i].st_shndx];
sym[i].st_value = sym[i].st_value + s->sh_addr;
}
}
}
ret = libelf_insertsymtab(modp, loadinfo, symhdr, sym);
lib_free(sym);
if (ret != 0)
{
binfo("Failed to export symbols program binary: %d\n", ret);
return ret;
}
return ret;
}
/****************************************************************************
* Name: libelf_insert
*
* Description:
* Verify that the file is an ELF module binary and, if so, load the
* module into kernel memory and initialize it for use.
*
* NOTE: libelf_setsymtab() had to have been called in board-specific OS
* logic prior to calling this function from application logic (perhaps via
* boardctl(BOARDIOC_OS_SYMTAB). Otherwise, insmod will be unable to
* resolve symbols in the OS module.
*
* Input Parameters:
*
* filename - Full path to the module binary to be loaded
* modname - The name that can be used to refer to the module after
* it has been loaded.
*
* Returned Value:
* A non-NULL module handle that can be used on subsequent calls to other
* module interfaces is returned on success. If libelf_insert() was
* unable to load the module libelf_insert() will return a NULL handle
* and the errno variable will be set appropriately.
*
****************************************************************************/
FAR void *libelf_insert(FAR const char *filename, FAR const char *modname)
{
FAR const struct symtab_s *exports;
struct mod_loadinfo_s loadinfo;
FAR struct module_s *modp;
FAR void (**array)(void);
int nexports;
int ret;
int i;
DEBUGASSERT(filename != NULL && modname != NULL);
binfo("Loading file: %s\n", filename);
/* Get exclusive access to the module registry */
libelf_registry_lock();
libc/dlfcn: Count opens so a library can be shared. dlopen() of a library that is already loaded fails. libelf_insert() rejects a name that is already in the module registry with EEXIST, and dlinsert() passes that straight out, so the second caller gets NULL. POSIX says dlopen() shall return a handle to the object, and there is no way today for two modules to hold the same library at once -- which is what a shared library is for. So dlopen() now takes another reference on a library that is already there, and dlclose() only tears it down when the last handle goes. The count lives in the dlfcn layer rather than in libelf_insert() so that insmod keeps its own behaviour: a second insmod of the same name still fails with EEXIST, which is right for a kernel module. The module name is what makes any of this possible, and a PROTECTED build did not have one. Names were defined for CONFIG_BUILD_FLAT or the kernel side of a split build, on the reasoning that only the kernel needed them, which predates dlopen() being usable from user space. Without a name the user-space copy of libelf cannot recognise a second open of a library, cannot count opens, and cannot make dlclose() mean anything -- two dlopen()s there produce two independent copies of the library and lose track of the first. Names are therefore defined wherever CONFIG_LIBC_DLFCN is, which costs NAME_MAX per loaded module in that configuration. The path no longer has to be copied either. The module name is the basename of the file and libelf_insert() takes it as a const string, so dlinsert() finds it with strrchr() instead of handing a writable duplicate of the whole path to basename(). BUILD_KERNEL is deliberately untouched. dlopen() returns NULL there unconditionally: dlinsert() is a stub, because sharing a library between processes with separate address spaces needs the text in a shared region and the data per process at a matching virtual address, which is a different problem from this one. Built for mps3-an547:picostest with and without CONFIG_LIBC_DLFCN, and for stm32f4discovery:kostest, a PROTECTED configuration, with it enabled. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-08-03 12:05:40 +02:00
/* Already installed? Take another reference rather than load a second
* copy: there is one instance of a module per name, and every caller
* shares it. The count is kept here so that insmod()/rmmod() and
* dlopen()/dlclose() get the same behaviour from the same code.
*/
#ifdef HAVE_LIBC_ELF_NAMES
libc/dlfcn: Count opens so a library can be shared. dlopen() of a library that is already loaded fails. libelf_insert() rejects a name that is already in the module registry with EEXIST, and dlinsert() passes that straight out, so the second caller gets NULL. POSIX says dlopen() shall return a handle to the object, and there is no way today for two modules to hold the same library at once -- which is what a shared library is for. So dlopen() now takes another reference on a library that is already there, and dlclose() only tears it down when the last handle goes. The count lives in the dlfcn layer rather than in libelf_insert() so that insmod keeps its own behaviour: a second insmod of the same name still fails with EEXIST, which is right for a kernel module. The module name is what makes any of this possible, and a PROTECTED build did not have one. Names were defined for CONFIG_BUILD_FLAT or the kernel side of a split build, on the reasoning that only the kernel needed them, which predates dlopen() being usable from user space. Without a name the user-space copy of libelf cannot recognise a second open of a library, cannot count opens, and cannot make dlclose() mean anything -- two dlopen()s there produce two independent copies of the library and lose track of the first. Names are therefore defined wherever CONFIG_LIBC_DLFCN is, which costs NAME_MAX per loaded module in that configuration. The path no longer has to be copied either. The module name is the basename of the file and libelf_insert() takes it as a const string, so dlinsert() finds it with strrchr() instead of handing a writable duplicate of the whole path to basename(). BUILD_KERNEL is deliberately untouched. dlopen() returns NULL there unconditionally: dlinsert() is a stub, because sharing a library between processes with separate address spaces needs the text in a shared region and the data per process at a matching virtual address, which is a different problem from this one. Built for mps3-an547:picostest with and without CONFIG_LIBC_DLFCN, and for stm32f4discovery:kostest, a PROTECTED configuration, with it enabled. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-08-03 12:05:40 +02:00
modp = libelf_registry_find(modname);
if (modp != NULL)
{
libc/dlfcn: Count opens so a library can be shared. dlopen() of a library that is already loaded fails. libelf_insert() rejects a name that is already in the module registry with EEXIST, and dlinsert() passes that straight out, so the second caller gets NULL. POSIX says dlopen() shall return a handle to the object, and there is no way today for two modules to hold the same library at once -- which is what a shared library is for. So dlopen() now takes another reference on a library that is already there, and dlclose() only tears it down when the last handle goes. The count lives in the dlfcn layer rather than in libelf_insert() so that insmod keeps its own behaviour: a second insmod of the same name still fails with EEXIST, which is right for a kernel module. The module name is what makes any of this possible, and a PROTECTED build did not have one. Names were defined for CONFIG_BUILD_FLAT or the kernel side of a split build, on the reasoning that only the kernel needed them, which predates dlopen() being usable from user space. Without a name the user-space copy of libelf cannot recognise a second open of a library, cannot count opens, and cannot make dlclose() mean anything -- two dlopen()s there produce two independent copies of the library and lose track of the first. Names are therefore defined wherever CONFIG_LIBC_DLFCN is, which costs NAME_MAX per loaded module in that configuration. The path no longer has to be copied either. The module name is the basename of the file and libelf_insert() takes it as a const string, so dlinsert() finds it with strrchr() instead of handing a writable duplicate of the whole path to basename(). BUILD_KERNEL is deliberately untouched. dlopen() returns NULL there unconditionally: dlinsert() is a stub, because sharing a library between processes with separate address spaces needs the text in a shared region and the data per process at a matching virtual address, which is a different problem from this one. Built for mps3-an547:picostest with and without CONFIG_LIBC_DLFCN, and for stm32f4discovery:kostest, a PROTECTED configuration, with it enabled. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-08-03 12:05:40 +02:00
if (modp->nopen == UINT8_MAX)
{
libelf_registry_unlock();
set_errno(EMFILE);
return NULL;
}
modp->nopen++;
libelf_registry_unlock();
libc/dlfcn: Count opens so a library can be shared. dlopen() of a library that is already loaded fails. libelf_insert() rejects a name that is already in the module registry with EEXIST, and dlinsert() passes that straight out, so the second caller gets NULL. POSIX says dlopen() shall return a handle to the object, and there is no way today for two modules to hold the same library at once -- which is what a shared library is for. So dlopen() now takes another reference on a library that is already there, and dlclose() only tears it down when the last handle goes. The count lives in the dlfcn layer rather than in libelf_insert() so that insmod keeps its own behaviour: a second insmod of the same name still fails with EEXIST, which is right for a kernel module. The module name is what makes any of this possible, and a PROTECTED build did not have one. Names were defined for CONFIG_BUILD_FLAT or the kernel side of a split build, on the reasoning that only the kernel needed them, which predates dlopen() being usable from user space. Without a name the user-space copy of libelf cannot recognise a second open of a library, cannot count opens, and cannot make dlclose() mean anything -- two dlopen()s there produce two independent copies of the library and lose track of the first. Names are therefore defined wherever CONFIG_LIBC_DLFCN is, which costs NAME_MAX per loaded module in that configuration. The path no longer has to be copied either. The module name is the basename of the file and libelf_insert() takes it as a const string, so dlinsert() finds it with strrchr() instead of handing a writable duplicate of the whole path to basename(). BUILD_KERNEL is deliberately untouched. dlopen() returns NULL there unconditionally: dlinsert() is a stub, because sharing a library between processes with separate address spaces needs the text in a shared region and the data per process at a matching virtual address, which is a different problem from this one. Built for mps3-an547:picostest with and without CONFIG_LIBC_DLFCN, and for stm32f4discovery:kostest, a PROTECTED configuration, with it enabled. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-08-03 12:05:40 +02:00
return modp;
}
#endif
/* Initialize the ELF library to load the program binary. */
ret = libelf_initialize(filename, &loadinfo);
libelf_dumploadinfo(&loadinfo);
if (ret != 0)
{
berr("ERROR: Failed to initialize to load module: %d\n", ret);
goto errout_with_loadinfo;
}
/* Allocate a module registry entry to hold the module data */
modp = lib_zalloc(sizeof(struct module_s));
if (modp == NULL)
{
berr("Failed to allocate struct module_s\n");
ret = -ENOMEM;
goto errout_with_loadinfo;
}
#ifdef HAVE_LIBC_ELF_NAMES
/* Save the module name in the registry entry */
strlcpy(modp->modname, modname, sizeof(modp->modname));
libc/dlfcn: Count opens so a library can be shared. dlopen() of a library that is already loaded fails. libelf_insert() rejects a name that is already in the module registry with EEXIST, and dlinsert() passes that straight out, so the second caller gets NULL. POSIX says dlopen() shall return a handle to the object, and there is no way today for two modules to hold the same library at once -- which is what a shared library is for. So dlopen() now takes another reference on a library that is already there, and dlclose() only tears it down when the last handle goes. The count lives in the dlfcn layer rather than in libelf_insert() so that insmod keeps its own behaviour: a second insmod of the same name still fails with EEXIST, which is right for a kernel module. The module name is what makes any of this possible, and a PROTECTED build did not have one. Names were defined for CONFIG_BUILD_FLAT or the kernel side of a split build, on the reasoning that only the kernel needed them, which predates dlopen() being usable from user space. Without a name the user-space copy of libelf cannot recognise a second open of a library, cannot count opens, and cannot make dlclose() mean anything -- two dlopen()s there produce two independent copies of the library and lose track of the first. Names are therefore defined wherever CONFIG_LIBC_DLFCN is, which costs NAME_MAX per loaded module in that configuration. The path no longer has to be copied either. The module name is the basename of the file and libelf_insert() takes it as a const string, so dlinsert() finds it with strrchr() instead of handing a writable duplicate of the whole path to basename(). BUILD_KERNEL is deliberately untouched. dlopen() returns NULL there unconditionally: dlinsert() is a stub, because sharing a library between processes with separate address spaces needs the text in a shared region and the data per process at a matching virtual address, which is a different problem from this one. Built for mps3-an547:picostest with and without CONFIG_LIBC_DLFCN, and for stm32f4discovery:kostest, a PROTECTED configuration, with it enabled. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-08-03 12:05:40 +02:00
modp->nopen = 1;
#endif
/* Load the program binary */
ret = libelf_load(&loadinfo);
libelf_dumploadinfo(&loadinfo);
if (ret != 0)
{
binfo("Failed to load ELF program binary: %d\n", ret);
goto errout_with_registry_entry;
}
/* Get the symbol table */
libelf_getsymtab(&exports, &nexports);
/* Bind the program to the kernel symbol table */
ret = libelf_bind(modp, &loadinfo, exports, nexports);
if (ret != 0)
{
binfo("Failed to bind symbols program binary: %d\n", ret);
goto errout_with_load;
}
ret = libelf_loadsymtab(modp, &loadinfo);
if (ret != 0)
{
binfo("Failed to load symbol table: %d\n", ret);
goto errout_with_load;
}
/* Save the load information */
modp->textalloc = (FAR void *)loadinfo.textalloc;
modp->dataalloc = (FAR void *)loadinfo.datastart;
#ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
modp->sectalloc = (FAR void **)loadinfo.sectalloc;
modp->nsect = loadinfo.ehdr.e_shnum;
#endif
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
modp->textsize = loadinfo.textsize;
modp->datasize = loadinfo.datasize;
#endif
/* Call the module initializer */
switch (loadinfo.ehdr.e_type)
{
case ET_REL :
case ET_DYN :
/* Process any preinit_array entries */
array = (FAR void (**)(void))loadinfo.preiarr;
for (i = 0; i < loadinfo.nprei; i++)
{
array[i]();
}
/* Process any init_array entries */
array = (FAR void (**)(void))loadinfo.initarr;
for (i = 0; i < loadinfo.ninit; i++)
{
array[i]();
}
modp->initarr = loadinfo.initarr;
modp->ninit = loadinfo.ninit;
modp->finiarr = loadinfo.finiarr;
modp->nfini = loadinfo.nfini;
break;
}
/* Add the new module entry to the registry */
libelf_registry_add(modp);
libelf_uninitialize(&loadinfo);
libelf_registry_unlock();
return modp;
errout_with_load:
libelf_unload(&loadinfo);
#if CONFIG_LIBC_ELF_MAXDEPEND > 0
libelf_undepend(modp);
#endif
errout_with_registry_entry:
lib_free(modp);
errout_with_loadinfo:
libelf_uninitialize(&loadinfo);
libelf_registry_unlock();
set_errno(-ret);
return NULL;
}