2015-12-10 16:53:31 +01:00
|
|
|
/****************************************************************************
|
2015-12-12 17:51:54 +01:00
|
|
|
* sched/module/mod_insmod.c
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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.
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2015-12-10 16:53:31 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2017-01-22 21:26:22 +01:00
|
|
|
#include <assert.h>
|
2015-12-10 16:53:31 +01:00
|
|
|
#include <errno.h>
|
2017-01-22 21:26:22 +01:00
|
|
|
#include <debug.h>
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2015-12-13 00:42:25 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2015-12-12 14:09:17 +01:00
|
|
|
#include <nuttx/module.h>
|
2017-01-29 15:24:42 +01:00
|
|
|
#include <nuttx/lib/modlib.h>
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MODULE
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mod_dumploadinfo
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-08-07 18:50:25 +02:00
|
|
|
#ifdef CONFIG_DEBUG_BINFMT_INFO
|
2015-12-12 14:09:17 +01:00
|
|
|
static void mod_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-01-29 20:03:53 +01:00
|
|
|
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);
|
2021-04-14 10:07:39 +02:00
|
|
|
binfo(" textalign: %zu\n", loadinfo->textalign);
|
|
|
|
binfo(" dataalign: %zu\n", loadinfo->dataalign);
|
2017-01-29 20:03:53 +01:00
|
|
|
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",
|
2017-08-15 01:19:27 +02:00
|
|
|
loadinfo->ehdr.e_ident[0], loadinfo->ehdr.e_ident[1],
|
|
|
|
loadinfo->ehdr.e_ident[2], loadinfo->ehdr.e_ident[3]);
|
2017-01-29 20:03:53 +01: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);
|
2021-03-23 07:25:49 +01:00
|
|
|
binfo(" e_phoff: %ju\n", (uintmax_t)loadinfo->ehdr.e_phoff);
|
|
|
|
binfo(" e_shoff: %ju\n", (uintmax_t)loadinfo->ehdr.e_shoff);
|
2017-08-15 01:19:27 +02:00
|
|
|
binfo(" e_flags: %08x\n", loadinfo->ehdr.e_flags);
|
2017-01-29 20:03:53 +01:00
|
|
|
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);
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
if (loadinfo->shdr && loadinfo->ehdr.e_shnum > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
|
2017-01-29 20:03:53 +01:00
|
|
|
binfo("Sections %d:\n", i);
|
2021-03-23 07:25:49 +01: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);
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2023-05-20 00:32:34 +02:00
|
|
|
# define mod_dumploadinfo(i)
|
2015-12-10 16:53:31 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-12-11 14:27:45 +01:00
|
|
|
* Name: mod_dumpinitializer
|
2015-12-10 16:53:31 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
#ifdef CONFIG_MODLIB_DUMPBUFFER
|
2015-12-11 14:27:45 +01:00
|
|
|
static void mod_dumpinitializer(mod_initializer_t initializer,
|
|
|
|
FAR struct mod_loadinfo_s *loadinfo)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2017-01-29 20:03:53 +01:00
|
|
|
modlib_dumpbuffer("Initializer code", (FAR const uint8_t *)initializer,
|
|
|
|
MIN(loadinfo->textsize - loadinfo->ehdr.e_entry, 512));
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
#else
|
2023-05-20 00:32:34 +02:00
|
|
|
# define mod_dumpinitializer(b,l)
|
2015-12-10 16:53:31 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-12-11 17:55:21 +01:00
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: insmod
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2015-12-11 17:55:21 +01:00
|
|
|
* Verify that the file is an ELF module binary and, if so, load the
|
|
|
|
* module into kernel memory and initialize it for use.
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
2017-01-29 18:54:54 +01:00
|
|
|
* NOTE: modlib_setsymtab() had to have been called in board-specific OS
|
2017-01-29 20:03:53 +01:00
|
|
|
* logic prior to calling this function from application logic (perhaps via
|
2015-12-13 15:10:01 +01:00
|
|
|
* boardctl(BOARDIOC_OS_SYMTAB). Otherwise, insmod will be unable to
|
|
|
|
* resolve symbols in the OS module.
|
|
|
|
*
|
2015-12-13 00:42:25 +01:00
|
|
|
* Input Parameters:
|
|
|
|
*
|
2017-01-29 19:23:24 +01:00
|
|
|
* filename - Full path to the module binary to be loaded
|
|
|
|
* modname - The name that can be used to refer to the module after
|
2015-12-13 00:42:25 +01:00
|
|
|
* it has been loaded.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-01-22 21:26:22 +01:00
|
|
|
* A non-NULL module handle that can be used on subsequent calls to other
|
|
|
|
* module interfaces is returned on success. If insmod() was unable to
|
|
|
|
* load the module insmod() will return a NULL handle and the errno
|
|
|
|
* variable will be set appropriately.
|
2015-12-13 00:42:25 +01:00
|
|
|
*
|
2015-12-10 16:53:31 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 19:23:24 +01:00
|
|
|
FAR void *insmod(FAR const char *filename, FAR const char *modname)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2015-12-12 14:09:17 +01:00
|
|
|
struct mod_loadinfo_s loadinfo;
|
2015-12-13 00:42:25 +01:00
|
|
|
FAR struct module_s *modp;
|
2015-12-12 17:51:54 +01:00
|
|
|
mod_initializer_t initializer;
|
2023-09-04 02:37:48 +02:00
|
|
|
FAR void (**array)(void);
|
2015-12-11 17:55:21 +01:00
|
|
|
int ret;
|
2023-09-04 02:37:48 +02:00
|
|
|
int i;
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2017-01-29 19:23:24 +01:00
|
|
|
DEBUGASSERT(filename != NULL && modname != NULL);
|
2017-01-29 20:03:53 +01:00
|
|
|
binfo("Loading file: %s\n", filename);
|
2015-12-13 00:42:25 +01:00
|
|
|
|
|
|
|
/* Get exclusive access to the module registry */
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
modlib_registry_lock();
|
2015-12-13 00:42:25 +01:00
|
|
|
|
|
|
|
/* Check if this module is already installed */
|
|
|
|
|
2017-01-29 19:23:24 +01:00
|
|
|
if (modlib_registry_find(modname) != NULL)
|
2015-12-13 00:42:25 +01:00
|
|
|
{
|
|
|
|
ret = -EEXIST;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
/* Initialize the ELF library to load the program binary. */
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
ret = modlib_initialize(filename, &loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
mod_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2018-06-01 18:10:17 +02:00
|
|
|
berr("ERROR: Failed to initialize to load module: %d\n", ret);
|
2021-02-25 05:53:20 +01:00
|
|
|
goto errout_with_loadinfo;
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 00:42:25 +01:00
|
|
|
/* Allocate a module registry entry to hold the module data */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
modp = kmm_zalloc(sizeof(struct module_s));
|
2022-03-18 17:42:30 +01:00
|
|
|
if (modp == NULL)
|
2015-12-13 00:42:25 +01:00
|
|
|
{
|
2022-03-18 17:42:30 +01:00
|
|
|
berr("Failed to allocate struct module_s\n");
|
2023-07-02 18:11:02 +02:00
|
|
|
ret = -ENOMEM;
|
2015-12-13 00:42:25 +01:00
|
|
|
goto errout_with_loadinfo;
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:30:17 +02:00
|
|
|
#ifdef HAVE_MODLIB_NAMES
|
2015-12-13 00:42:25 +01:00
|
|
|
/* Save the module name in the registry entry */
|
|
|
|
|
2022-03-31 12:30:17 +02:00
|
|
|
strlcpy(modp->modname, modname, sizeof(modp->modname));
|
|
|
|
#endif
|
2015-12-13 00:42:25 +01:00
|
|
|
|
2015-12-10 16:53:31 +01:00
|
|
|
/* Load the program binary */
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
ret = modlib_load(&loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
mod_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2017-01-29 20:03:53 +01:00
|
|
|
binfo("Failed to load ELF program binary: %d\n", ret);
|
2015-12-13 00:42:25 +01:00
|
|
|
goto errout_with_registry_entry;
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 15:10:01 +01:00
|
|
|
/* Bind the program to the kernel symbol table */
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
ret = modlib_bind(modp, &loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2017-01-29 20:03:53 +01:00
|
|
|
binfo("Failed to bind symbols program binary: %d\n", ret);
|
2015-12-10 16:53:31 +01:00
|
|
|
goto errout_with_load;
|
|
|
|
}
|
|
|
|
|
2017-01-22 21:26:22 +01:00
|
|
|
/* Save the load information */
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
modp->textalloc = (FAR void *)loadinfo.textalloc;
|
|
|
|
modp->dataalloc = (FAR void *)loadinfo.datastart;
|
2015-12-13 01:56:32 +01:00
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
|
2023-07-02 18:11:02 +02:00
|
|
|
modp->textsize = loadinfo.textsize;
|
|
|
|
modp->datasize = loadinfo.datasize;
|
2015-12-13 01:56:32 +01:00
|
|
|
#endif
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2015-12-11 14:27:45 +01:00
|
|
|
/* Get the module initializer entry point */
|
|
|
|
|
2020-03-10 06:38:38 +01:00
|
|
|
initializer = (mod_initializer_t)(loadinfo.textalloc +
|
|
|
|
loadinfo.ehdr.e_entry);
|
2015-12-13 01:56:32 +01:00
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
|
|
|
|
modp->initializer = initializer;
|
|
|
|
#endif
|
2015-12-12 17:51:54 +01:00
|
|
|
mod_dumpinitializer(initializer, &loadinfo);
|
2015-12-11 14:27:45 +01:00
|
|
|
|
2015-12-12 17:51:54 +01:00
|
|
|
/* Call the module initializer */
|
2015-12-11 14:27:45 +01:00
|
|
|
|
2023-09-04 02:37:48 +02:00
|
|
|
switch (loadinfo.ehdr.e_type)
|
2015-12-12 17:51:54 +01:00
|
|
|
{
|
2023-09-04 02:37:48 +02:00
|
|
|
case ET_REL :
|
|
|
|
ret = initializer(&modp->modinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
binfo("Failed to initialize the module: %d\n", ret);
|
|
|
|
goto errout_with_load;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
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->finiarr = loadinfo.finiarr;
|
|
|
|
modp->nfini = loadinfo.nfini;
|
|
|
|
break;
|
2015-12-11 14:27:45 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 00:42:25 +01:00
|
|
|
/* Add the new module entry to the registry */
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
modlib_registry_add(modp);
|
2015-12-13 00:42:25 +01:00
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
modlib_uninitialize(&loadinfo);
|
|
|
|
modlib_registry_unlock();
|
2023-07-02 18:11:02 +02:00
|
|
|
return modp;
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
errout_with_load:
|
2017-01-29 18:54:54 +01:00
|
|
|
modlib_unload(&loadinfo);
|
2020-01-02 17:49:34 +01:00
|
|
|
modlib_undepend(modp);
|
2015-12-13 00:42:25 +01:00
|
|
|
errout_with_registry_entry:
|
|
|
|
kmm_free(modp);
|
|
|
|
errout_with_loadinfo:
|
2017-01-29 17:05:15 +01:00
|
|
|
modlib_uninitialize(&loadinfo);
|
2015-12-13 00:42:25 +01:00
|
|
|
errout_with_lock:
|
2017-01-29 17:05:15 +01:00
|
|
|
modlib_registry_unlock();
|
2015-12-13 00:42:25 +01:00
|
|
|
set_errno(-ret);
|
2017-01-22 21:26:22 +01:00
|
|
|
return NULL;
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_MODULE */
|