2024-04-29 19:59:34 +08:00
|
|
|
/****************************************************************************
|
2025-04-10 09:51:25 +08:00
|
|
|
* libs/libc/elf/elf_insert.c
|
2024-04-29 19:59:34 +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
|
|
|
|
|
*
|
|
|
|
|
* 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>
|
2026-04-01 05:11:15 +05:30
|
|
|
#include <nuttx/debug.h>
|
2024-04-29 19:59:34 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
|
|
#include <nuttx/lib/lib.h>
|
2025-04-10 09:51:25 +08:00
|
|
|
#include <nuttx/lib/elf.h>
|
2024-04-29 19:59:34 +08:00
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
#include "elf.h"
|
2024-10-13 17:25:10 +08:00
|
|
|
|
2024-04-29 19:59:34 +08:00
|
|
|
/****************************************************************************
|
2024-07-09 23:38:38 +08:00
|
|
|
* Public Functions
|
2024-04-29 19:59:34 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
2025-04-10 09:51:25 +08:00
|
|
|
* Name: libelf_dumploadinfo
|
2024-07-09 23:38:38 +08:00
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Dump the load information to debug output.
|
|
|
|
|
*
|
2024-04-29 19:59:34 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_BINFMT_INFO
|
2025-04-10 09:51:25 +08:00
|
|
|
void libelf_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo)
|
2024-04-29 19:59:34 +08:00
|
|
|
{
|
|
|
|
|
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];
|
2026-08-26 18:16:13 +02:00
|
|
|
|
2024-04-29 19:59:34 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-09 23:38:38 +08:00
|
|
|
|
|
|
|
|
/****************************************************************************
|
2025-04-10 09:51:25 +08:00
|
|
|
* Name: libelf_dumpmodule
|
2024-07-09 23:38:38 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
void libelf_dumpmodule(FAR struct module_s *modp)
|
2024-07-09 23:38:38 +08:00
|
|
|
{
|
|
|
|
|
binfo("Module:\n");
|
2025-04-10 09:51:25 +08:00
|
|
|
#ifdef HAVE_LIBC_ELF_NAMES
|
2024-07-09 23:38:38 +08:00
|
|
|
binfo(" modname: %s\n", modp->modname);
|
2025-01-22 14:07:34 +08:00
|
|
|
#endif
|
2024-07-09 23:38:38 +08:00
|
|
|
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
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
#if CONFIG_LIBC_ELF_MAXDEPEND > 0
|
2024-07-09 23:38:38 +08:00
|
|
|
binfo(" dependents: %d\n", modp->dependents);
|
|
|
|
|
for (int i = 0; i < modp->dependents; i++)
|
|
|
|
|
{
|
2025-04-10 09:51:25 +08:00
|
|
|
# ifdef HAVE_LIBC_ELF_NAMES
|
2024-07-09 23:38:38 +08:00
|
|
|
binfo("%d %s\n", i, modp->dependencies[i]->modname);
|
2025-01-22 14:07:34 +08:00
|
|
|
# else
|
|
|
|
|
binfo("%d\n", i);
|
|
|
|
|
# endif
|
2025-04-10 09:51:25 +08:00
|
|
|
libelf_dumpmodule(modp->dependencies[i]);
|
2024-07-09 23:38:38 +08:00
|
|
|
}
|
2024-04-29 19:59:34 +08:00
|
|
|
#endif
|
|
|
|
|
|
2024-07-09 23:38:38 +08:00
|
|
|
binfo(" finiarr: %08lx\n", (long)modp->finiarr);
|
|
|
|
|
binfo(" nfini: %d\n", modp->nfini);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
2024-04-29 19:59:34 +08:00
|
|
|
/****************************************************************************
|
2024-07-09 23:38:38 +08:00
|
|
|
* Name: elf_dumpentrypt
|
2024-04-29 19:59:34 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
#ifdef CONFIG_LIBC_ELF_DUMPBUFFER
|
|
|
|
|
void libelf_dumpentrypt(FAR struct mod_loadinfo_s *loadinfo)
|
2024-07-09 23:38:38 +08:00
|
|
|
{
|
|
|
|
|
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
|
2025-04-10 09:51:25 +08:00
|
|
|
* space that may not be in place now. libelf_addrenv_select() will
|
2024-07-09 23:38:38 +08:00
|
|
|
* temporarily instantiate that address space.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (loadinfo->addrenv != NULL)
|
|
|
|
|
{
|
2025-04-10 09:51:25 +08:00
|
|
|
ret = libelf_addrenv_select(loadinfo);
|
2024-07-09 23:38:38 +08:00
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
2025-04-10 09:51:25 +08:00
|
|
|
berr("ERROR: libelf_addrenv_select() failed: %d\n", ret);
|
2024-07-09 23:38:38 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
libelf_dumpbuffer("Entry code", entry,
|
2024-07-09 23:38:38 +08:00
|
|
|
MIN(loadinfo->textsize - loadinfo->ehdr.e_entry, 512));
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
|
|
|
/* Restore the original address environment */
|
|
|
|
|
|
|
|
|
|
if (loadinfo->addrenv != NULL)
|
|
|
|
|
{
|
2025-04-10 09:51:25 +08:00
|
|
|
ret = libelf_addrenv_restore(loadinfo);
|
2024-07-09 23:38:38 +08:00
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
2025-04-10 09:51:25 +08:00
|
|
|
berr("ERROR: libelf_addrenv_restore() failed: %d\n", ret);
|
2024-07-09 23:38:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-10-13 17:25:10 +08:00
|
|
|
/****************************************************************************
|
2025-04-10 09:51:25 +08:00
|
|
|
* Name: libelf_loadsymtab
|
2024-10-13 17:25:10 +08:00
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Load the symbol table into memory.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
static int libelf_loadsymtab(FAR struct module_s *modp,
|
2024-10-13 17:25:10 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
ret = libelf_read(loadinfo, (FAR uint8_t *)sym, symhdr->sh_size,
|
2024-10-13 17:25:10 +08:00
|
|
|
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.
|
|
|
|
|
*/
|
2024-10-13 17:25:10 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2024-10-13 17:25:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
ret = libelf_insertsymtab(modp, loadinfo, symhdr, sym);
|
2024-10-13 17:25:10 +08:00
|
|
|
lib_free(sym);
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
{
|
|
|
|
|
binfo("Failed to export symbols program binary: %d\n", ret);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 19:59:34 +08:00
|
|
|
/****************************************************************************
|
2025-04-10 09:51:25 +08:00
|
|
|
* Name: libelf_insert
|
2024-04-29 19:59:34 +08:00
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Verify that the file is an ELF module binary and, if so, load the
|
|
|
|
|
* module into kernel memory and initialize it for use.
|
|
|
|
|
*
|
2025-04-10 09:51:25 +08:00
|
|
|
* NOTE: libelf_setsymtab() had to have been called in board-specific OS
|
2024-04-29 19:59:34 +08:00
|
|
|
* 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
|
2025-04-10 09:51:25 +08:00
|
|
|
* module interfaces is returned on success. If libelf_insert() was
|
|
|
|
|
* unable to load the module libelf_insert() will return a NULL handle
|
2024-04-29 19:59:34 +08:00
|
|
|
* and the errno variable will be set appropriately.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
FAR void *libelf_insert(FAR const char *filename, FAR const char *modname)
|
2024-04-29 19:59:34 +08:00
|
|
|
{
|
2024-07-09 23:27:59 +08:00
|
|
|
FAR const struct symtab_s *exports;
|
2024-04-29 19:59:34 +08:00
|
|
|
struct mod_loadinfo_s loadinfo;
|
|
|
|
|
FAR struct module_s *modp;
|
|
|
|
|
FAR void (**array)(void);
|
2024-07-09 23:27:59 +08:00
|
|
|
int nexports;
|
2024-04-29 19:59:34 +08:00
|
|
|
int ret;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
DEBUGASSERT(filename != NULL && modname != NULL);
|
|
|
|
|
binfo("Loading file: %s\n", filename);
|
|
|
|
|
|
|
|
|
|
/* Get exclusive access to the module registry */
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
libelf_registry_lock();
|
2024-04-29 19:59:34 +08:00
|
|
|
|
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.
|
|
|
|
|
*/
|
2024-04-29 19:59:34 +08:00
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
#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)
|
2024-04-29 19:59:34 +08:00
|
|
|
{
|
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++;
|
2025-04-10 09:51:25 +08:00
|
|
|
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;
|
2024-04-29 19:59:34 +08:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Initialize the ELF library to load the program binary. */
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
ret = libelf_initialize(filename, &loadinfo);
|
|
|
|
|
libelf_dumploadinfo(&loadinfo);
|
2024-04-29 19:59:34 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
#ifdef HAVE_LIBC_ELF_NAMES
|
2024-04-29 19:59:34 +08:00
|
|
|
/* 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;
|
2024-04-29 19:59:34 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Load the program binary */
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
ret = libelf_load(&loadinfo);
|
|
|
|
|
libelf_dumploadinfo(&loadinfo);
|
2024-04-29 19:59:34 +08:00
|
|
|
if (ret != 0)
|
|
|
|
|
{
|
|
|
|
|
binfo("Failed to load ELF program binary: %d\n", ret);
|
|
|
|
|
goto errout_with_registry_entry;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 23:27:59 +08:00
|
|
|
/* Get the symbol table */
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
libelf_getsymtab(&exports, &nexports);
|
2024-07-09 23:27:59 +08:00
|
|
|
|
2024-04-29 19:59:34 +08:00
|
|
|
/* Bind the program to the kernel symbol table */
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
ret = libelf_bind(modp, &loadinfo, exports, nexports);
|
2024-04-29 19:59:34 +08:00
|
|
|
if (ret != 0)
|
|
|
|
|
{
|
|
|
|
|
binfo("Failed to bind symbols program binary: %d\n", ret);
|
|
|
|
|
goto errout_with_load;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
ret = libelf_loadsymtab(modp, &loadinfo);
|
2024-10-13 17:25:10 +08:00
|
|
|
if (ret != 0)
|
|
|
|
|
{
|
|
|
|
|
binfo("Failed to load symbol table: %d\n", ret);
|
|
|
|
|
goto errout_with_load;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 19:59:34 +08:00
|
|
|
/* Save the load information */
|
|
|
|
|
|
|
|
|
|
modp->textalloc = (FAR void *)loadinfo.textalloc;
|
|
|
|
|
modp->dataalloc = (FAR void *)loadinfo.datastart;
|
2024-07-09 23:30:04 +08:00
|
|
|
#ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
|
|
|
|
|
modp->sectalloc = (FAR void **)loadinfo.sectalloc;
|
|
|
|
|
modp->nsect = loadinfo.ehdr.e_shnum;
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-04-29 19:59:34 +08:00
|
|
|
#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 :
|
|
|
|
|
|
2026-08-26 18:16:13 +02:00
|
|
|
/* Process any preinit_array entries */
|
2024-04-29 19:59:34 +08:00
|
|
|
|
2026-08-26 18:16:13 +02:00
|
|
|
array = (FAR void (**)(void))loadinfo.preiarr;
|
|
|
|
|
for (i = 0; i < loadinfo.nprei; i++)
|
|
|
|
|
{
|
|
|
|
|
array[i]();
|
|
|
|
|
}
|
2024-04-29 19:59:34 +08:00
|
|
|
|
2026-08-26 18:16:13 +02:00
|
|
|
/* Process any init_array entries */
|
2024-04-29 19:59:34 +08:00
|
|
|
|
2026-08-26 18:16:13 +02:00
|
|
|
array = (FAR void (**)(void))loadinfo.initarr;
|
|
|
|
|
for (i = 0; i < loadinfo.ninit; i++)
|
|
|
|
|
{
|
|
|
|
|
array[i]();
|
|
|
|
|
}
|
2024-04-29 19:59:34 +08:00
|
|
|
|
2026-08-26 18:16:13 +02:00
|
|
|
modp->initarr = loadinfo.initarr;
|
|
|
|
|
modp->ninit = loadinfo.ninit;
|
|
|
|
|
modp->finiarr = loadinfo.finiarr;
|
|
|
|
|
modp->nfini = loadinfo.nfini;
|
|
|
|
|
break;
|
2024-04-29 19:59:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add the new module entry to the registry */
|
|
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
libelf_registry_add(modp);
|
2024-04-29 19:59:34 +08:00
|
|
|
|
2025-04-10 09:51:25 +08:00
|
|
|
libelf_uninitialize(&loadinfo);
|
|
|
|
|
libelf_registry_unlock();
|
2024-04-29 19:59:34 +08:00
|
|
|
return modp;
|
|
|
|
|
|
|
|
|
|
errout_with_load:
|
2025-04-10 09:51:25 +08:00
|
|
|
libelf_unload(&loadinfo);
|
|
|
|
|
#if CONFIG_LIBC_ELF_MAXDEPEND > 0
|
|
|
|
|
libelf_undepend(modp);
|
2024-04-29 19:59:34 +08:00
|
|
|
#endif
|
|
|
|
|
errout_with_registry_entry:
|
|
|
|
|
lib_free(modp);
|
|
|
|
|
errout_with_loadinfo:
|
2025-04-10 09:51:25 +08:00
|
|
|
libelf_uninitialize(&loadinfo);
|
|
|
|
|
libelf_registry_unlock();
|
2024-04-29 19:59:34 +08:00
|
|
|
set_errno(-ret);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|