Modules: Move first five of many C files from sched/module to libc/modlib

This commit is contained in:
Gregory Nutt
2017-01-29 10:05:15 -06:00
parent 00e46b5966
commit 5e94dd22bb
22 changed files with 241 additions and 250 deletions
+2 -3
View File
@@ -1,7 +1,7 @@
############################################################################
# sched/module/Make.defs
#
# Copyright (C) 2015 Gregory Nutt. All rights reserved.
# Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
@@ -41,8 +41,7 @@ CSRCS += mod_insmod.c mod_rmmod.c mod_modsym.c mod_symtab.c mod_modhandle.c
# Loadable module library
CSRCS += mod_bind.c mod_depend.c mod_init.c mod_iobuffer.c mod_load.c
CSRCS += mod_read.c mod_registry.c mod_sections.c mod_symbols.c mod_uninit.c
CSRCS += mod_iobuffer.c mod_load.c mod_read.c mod_sections.c mod_symbols.c
CSRCS += mod_unload.c mod_verify.c
# procfs support
-304
View File
@@ -1,304 +0,0 @@
/****************************************************************************
* sched/module/mod_bind.c
*
* Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <string.h>
#include <elf32.h>
#include <errno.h>
#include <assert.h>
#include <debug.h>
#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>
#include <nuttx/binfmt/symtab.h>
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: mod_readrel
*
* Description:
* Read the ELF32_Rel structure into memory.
*
****************************************************************************/
static inline int mod_readrel(FAR struct mod_loadinfo_s *loadinfo,
FAR const Elf32_Shdr *relsec,
int index, FAR Elf32_Rel *rel)
{
off_t offset;
/* Verify that the symbol table index lies within symbol table */
if (index < 0 || index > (relsec->sh_size / sizeof(Elf32_Rel)))
{
serr("ERROR: Bad relocation symbol index: %d\n", index);
return -EINVAL;
}
/* Get the file offset to the symbol table entry */
offset = relsec->sh_offset + sizeof(Elf32_Rel) * index;
/* And, finally, read the symbol table entry into memory */
return mod_read(loadinfo, (FAR uint8_t *)rel, sizeof(Elf32_Rel), offset);
}
/****************************************************************************
* Name: mod_relocate and mod_relocateadd
*
* Description:
* Perform all relocations associated with a section.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
static int mod_relocate(FAR struct module_s *modp,
FAR struct mod_loadinfo_s *loadinfo, int relidx)
{
FAR Elf32_Shdr *relsec = &loadinfo->shdr[relidx];
FAR Elf32_Shdr *dstsec = &loadinfo->shdr[relsec->sh_info];
Elf32_Rel rel;
Elf32_Sym sym;
FAR Elf32_Sym *psym;
uintptr_t addr;
int symidx;
int ret;
int i;
/* Examine each relocation in the section. 'relsec' is the section
* containing the relations. 'dstsec' is the section containing the data
* to be relocated.
*/
for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++)
{
psym = &sym;
/* Read the relocation entry into memory */
ret = mod_readrel(loadinfo, relsec, i, &rel);
if (ret < 0)
{
serr("ERROR: Section %d reloc %d: Failed to read relocation entry: %d\n",
relidx, i, ret);
return ret;
}
/* Get the symbol table index for the relocation. This is contained
* in a bit-field within the r_info element.
*/
symidx = ELF32_R_SYM(rel.r_info);
/* Read the symbol table entry into memory */
ret = mod_readsym(loadinfo, symidx, &sym);
if (ret < 0)
{
serr("ERROR: Section %d reloc %d: Failed to read symbol[%d]: %d\n",
relidx, i, symidx, ret);
return ret;
}
/* Get the value of the symbol (in sym.st_value) */
ret = mod_symvalue(modp, loadinfo, &sym);
if (ret < 0)
{
/* The special error -ESRCH is returned only in one condition: The
* symbol has no name.
*
* There are a few relocations for a few architectures that do
* no depend upon a named symbol. We don't know if that is the
* case here, but we will use a NULL symbol pointer to indicate
* that case to up_relocate(). That function can then do what
* is best.
*/
if (ret == -ESRCH)
{
serr("ERROR: Section %d reloc %d: Undefined symbol[%d] has no name: %d\n",
relidx, i, symidx, ret);
psym = NULL;
}
else
{
serr("ERROR: Section %d reloc %d: Failed to get value of symbol[%d]: %d\n",
relidx, i, symidx, ret);
return ret;
}
}
/* Calculate the relocation address. */
if (rel.r_offset < 0 || rel.r_offset > dstsec->sh_size - sizeof(uint32_t))
{
serr("ERROR: Section %d reloc %d: Relocation address out of range, offset %d size %d\n",
relidx, i, rel.r_offset, dstsec->sh_size);
return -EINVAL;
}
addr = dstsec->sh_addr + rel.r_offset;
/* Now perform the architecture-specific relocation */
ret = up_relocate(&rel, psym, addr);
if (ret < 0)
{
serr("ERROR: Section %d reloc %d: Relocation failed: %d\n", relidx, i, ret);
return ret;
}
}
return OK;
}
static int mod_relocateadd(FAR struct module_s *modp,
FAR struct mod_loadinfo_s *loadinfo, int relidx)
{
serr("ERROR: Not implemented\n");
return -ENOSYS;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: mod_bind
*
* Description:
* Bind the imported symbol names in the loaded module described by
* 'loadinfo' using the exported symbol values provided by mod_setsymtab().
*
* Input Parameters:
* modp - Module state information
* loadinfo - Load state information
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
int mod_bind(FAR struct module_s *modp, FAR struct mod_loadinfo_s *loadinfo)
{
int ret;
int i;
/* Find the symbol and string tables */
ret = mod_findsymtab(loadinfo);
if (ret < 0)
{
return ret;
}
/* Allocate an I/O buffer. This buffer is used by mod_symname() to
* accumulate the variable length symbol name.
*/
ret = mod_allocbuffer(loadinfo);
if (ret < 0)
{
serr("ERROR: mod_allocbuffer failed: %d\n", ret);
return -ENOMEM;
}
/* Process relocations in every allocated section */
for (i = 1; i < loadinfo->ehdr.e_shnum; i++)
{
/* Get the index to the relocation section */
int infosec = loadinfo->shdr[i].sh_info;
if (infosec >= loadinfo->ehdr.e_shnum)
{
continue;
}
/* Make sure that the section is allocated. We can't relocate
* sections that were not loaded into memory.
*/
if ((loadinfo->shdr[infosec].sh_flags & SHF_ALLOC) == 0)
{
continue;
}
/* Process the relocations by type */
if (loadinfo->shdr[i].sh_type == SHT_REL)
{
ret = mod_relocate(modp, loadinfo, i);
}
else if (loadinfo->shdr[i].sh_type == SHT_RELA)
{
ret = mod_relocateadd(modp, loadinfo, i);
}
if (ret < 0)
{
break;
}
}
#if defined(CONFIG_ARCH_HAVE_COHERENT_DCACHE)
/* Ensure that the I and D caches are coherent before starting the newly
* loaded module by cleaning the D cache (i.e., flushing the D cache
* contents to memory and invalidating the I cache).
*/
up_coherent_dcache(loadinfo->textalloc, loadinfo->textsize);
up_coherent_dcache(loadinfo->datastart, loadinfo->datasize);
#endif
return ret;
}
-199
View File
@@ -1,199 +0,0 @@
/****************************************************************************
* sched/module/mod_depend.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdlib.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: mod_depend
*
* Description:
* Set up module dependencies between the exporter and the importer of a
* symbol. The happens when the module is installed via insmod and a
* symbol is imported from another module.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
* Assumptions:
* Caller holds the registry lock.
*
****************************************************************************/
int mod_depend(FAR struct module_s *importer, FAR struct module_s *exporter)
{
#if CONFIG_LIBC_MODLIB_MAXDEPEND > 0
int freendx;
int i;
DEBUGASSERT(importer != NULL && exporter != NULL);
/* First checker if the exported is already in our list if dependencies.
* This would happen if we are importing multiple symbols from the same
* exporting module. In that case, the module would already be in the
* list of dependencies.
*
* The list dependency list is a a dumb, upacked array of pointers. This
* should not be too inefficient if the number of CONFIG_LIBC_MODLIB_MAXDEPEND
* is small. Otherwise, a more dynamic data structure would be in order.
*/
for (i = 0, freendx = -1; i < CONFIG_LIBC_MODLIB_MAXDEPEND; i++)
{
FAR const struct module_s *modp;
/* Check if this dependency slot is available. */
modp = importer->dependencies[i];
if (modp == NULL)
{
/* Remember this slot for use the module is NOT already in the
* list of dependencies.
*/
freendx = i;
}
else if (modp == exporter)
{
/* Yes, we are already importing symbols from this module. Nothing
* more needs to be done.
*/
return OK;
}
}
/* If we get here, then (1) this is a new exporting module that does not
* already appear in the list of dependencies, and (2) freendx is the
* index to the last free slot in the dependency list. If freendx is
* negative, then the dependency list is full.
*/
if (freendx >= 0)
{
/* Increment the count of dependencies on the exporter module */
DEBUGASSERT(exporter->dependents < UINT8_MAX);
if (exporter->dependents >= UINT8_MAX)
{
return -ENOSPC;
}
exporter->dependents++;
/* And remember the exporter so that we can decrement the count of
* dependents if the importer is removed.
*/
DEBUGASSERT(importer->dependencies[freendx] == NULL);
importer->dependencies[freendx] = exporter;
return OK;
}
/* If we get there then the list of dependencies is full. */
DEBUGPANIC();
return -ENFILE;
#else
return OK;
#endif
}
/****************************************************************************
* Name: mod_undepend
*
* Description:
* Tear down module dependencies between the exporters and the importer of
* symbols. This happens when the module is removed via rmmod (and on
* error handling cases in insmod).
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
* Assumptions:
* Caller holds the registry lock.
*
****************************************************************************/
int mod_undepend(FAR struct module_s *importer)
{
#if CONFIG_LIBC_MODLIB_MAXDEPEND > 0
FAR struct module_s *exporter;
int i;
DEBUGASSERT(importer != NULL && importer->dependents == 0);
/* Decrement the dependency count on each of exporters of symbols used by
* this importer module. This is an upacked array of pointers. This
* should not be too inefficient if the number of CONFIG_LIBC_MODLIB_MAXDEPEND
* is small. Otherwise, a more dynamic data structure would be in order.
*/
for (i = 0; i < CONFIG_LIBC_MODLIB_MAXDEPEND; i++)
{
exporter = importer->dependencies[i];
if (exporter != NULL)
{
DEBUGASSERT(exporter->dependents > 0);
if (exporter->dependents > 0)
{
exporter->dependents--;
}
importer->dependencies[i] = NULL;
}
}
#endif
return OK;
}
-203
View File
@@ -1,203 +0,0 @@
/****************************************************************************
* sched/module/mod_init.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/stat.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <elf32.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_LIBC_MODLIB_DUMPBUFFER
* have to be defined or CONFIG_LIBC_MODLIB_DUMPBUFFER does nothing.
*/
#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_LIBC_MODLIB_DUMPBUFFER)
# undef CONFIG_LIBC_MODLIB_DUMPBUFFER
#endif
#ifdef CONFIG_LIBC_MODLIB_DUMPBUFFER
# define mod_dumpbuffer(m,b,n) sinfodumpbuffer(m,b,n)
#else
# define mod_dumpbuffer(m,b,n)
#endif
/****************************************************************************
* Private Constant Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: mod_filelen
*
* Description:
* Get the size of the ELF file
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
static inline int mod_filelen(FAR struct mod_loadinfo_s *loadinfo,
FAR const char *filename)
{
struct stat buf;
int ret;
/* Get the file stats */
ret = stat(filename, &buf);
if (ret < 0)
{
int errval = errno;
serr("ERROR: Failed to stat file: %d\n", errval);
return -errval;
}
/* Verify that it is a regular file */
if (!S_ISREG(buf.st_mode))
{
serr("ERROR: Not a regular file. mode: %d\n", buf.st_mode);
return -ENOENT;
}
/* TODO: Verify that the file is readable. Not really important because
* we will detect this when we try to open the file read-only.
*/
/* Return the size of the file in the loadinfo structure */
loadinfo->filelen = buf.st_size;
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: mod_initialize
*
* Description:
* This function is called to configure the library to process an ELF
* program binary.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
int mod_initialize(FAR const char *filename,
FAR struct mod_loadinfo_s *loadinfo)
{
int ret;
sinfo("filename: %s loadinfo: %p\n", filename, loadinfo);
/* Clear the load info structure */
memset(loadinfo, 0, sizeof(struct mod_loadinfo_s));
/* Get the length of the file. */
ret = mod_filelen(loadinfo, filename);
if (ret < 0)
{
serr("ERROR: mod_filelen failed: %d\n", ret);
return ret;
}
/* Open the binary file for reading (only) */
loadinfo->filfd = open(filename, O_RDONLY);
if (loadinfo->filfd < 0)
{
int errval = errno;
serr("ERROR: Failed to open ELF binary %s: %d\n", filename, errval);
return -errval;
}
/* Read the ELF ehdr from offset 0 */
ret = mod_read(loadinfo, (FAR uint8_t *)&loadinfo->ehdr,
sizeof(Elf32_Ehdr), 0);
if (ret < 0)
{
serr("ERROR: Failed to read ELF header: %d\n", ret);
return ret;
}
mod_dumpbuffer("ELF header", (FAR const uint8_t *)&loadinfo->ehdr,
sizeof(Elf32_Ehdr));
/* Verify the ELF header */
ret = mod_verifyheader(&loadinfo->ehdr);
if (ret < 0)
{
/* This may not be an error because we will be called to attempt loading
* EVERY binary. If mod_verifyheader() does not recognize the ELF header,
* it will -ENOEXEC whcih simply informs the system that the file is not an
* ELF file. mod_verifyheader() will return other errors if the ELF header
* is not correctly formed.
*/
serr("ERROR: Bad ELF header: %d\n", ret);
return ret;
}
return OK;
}
+15 -15
View File
@@ -59,14 +59,14 @@
****************************************************************************/
/* CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT have to be defined or
* CONFIG_LIBC_MODLIB_DUMPBUFFER does nothing.
* CONFIG_MODLIB_DUMPBUFFER does nothing.
*/
#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT)
# undef CONFIG_LIBC_MODLIB_DUMPBUFFER
# undef CONFIG_MODLIB_DUMPBUFFER
#endif
#ifdef CONFIG_LIBC_MODLIB_DUMPBUFFER
#ifdef CONFIG_MODLIB_DUMPBUFFER
# define mod_dumpbuffer(m,b,n) sinfodumpbuffer(m,b,n)
#else
# define mod_dumpbuffer(m,b,n)
@@ -144,7 +144,7 @@ static void mod_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo)
* Name: mod_dumpinitializer
****************************************************************************/
#ifdef CONFIG_LIBC_MODLIB_DUMPBUFFER
#ifdef CONFIG_MODLIB_DUMPBUFFER
static void mod_dumpinitializer(mod_initializer_t initializer,
FAR struct mod_loadinfo_s *loadinfo)
{
@@ -197,20 +197,20 @@ FAR void *insmod(FAR const char *filename, FAR const char *modulename)
/* Get exclusive access to the module registry */
mod_registry_lock();
modlib_registry_lock();
/* Check if this module is already installed */
if (mod_registry_find(modulename) != NULL)
if (modlib_registry_find(modulename) != NULL)
{
mod_registry_unlock();
modlib_registry_unlock();
ret = -EEXIST;
goto errout_with_lock;
}
/* Initialize the ELF library to load the program binary. */
ret = mod_initialize(filename, &loadinfo);
ret = modlib_initialize(filename, &loadinfo);
mod_dumploadinfo(&loadinfo);
if (ret != 0)
{
@@ -243,7 +243,7 @@ FAR void *insmod(FAR const char *filename, FAR const char *modulename)
/* Bind the program to the kernel symbol table */
ret = mod_bind(modp, &loadinfo);
ret = modlib_bind(modp, &loadinfo);
if (ret != 0)
{
sinfo("Failed to bind symbols program binary: %d\n", ret);
@@ -277,21 +277,21 @@ FAR void *insmod(FAR const char *filename, FAR const char *modulename)
/* Add the new module entry to the registry */
mod_registry_add(modp);
modlib_registry_add(modp);
mod_uninitialize(&loadinfo);
mod_registry_unlock();
modlib_uninitialize(&loadinfo);
modlib_registry_unlock();
return (FAR void *)modp;
errout_with_load:
mod_unload(&loadinfo);
(void)mod_undepend(modp);
(void)modlib_undepend(modp);
errout_with_registry_entry:
kmm_free(modp);
errout_with_loadinfo:
mod_uninitialize(&loadinfo);
modlib_uninitialize(&loadinfo);
errout_with_lock:
mod_registry_unlock();
modlib_registry_unlock();
set_errno(-ret);
return NULL;
}
+2 -2
View File
@@ -71,14 +71,14 @@ int mod_allocbuffer(FAR struct mod_loadinfo_s *loadinfo)
{
/* No.. allocate one now */
loadinfo->iobuffer = (FAR uint8_t *)kmm_malloc(CONFIG_LIBC_MODLIB_BUFFERSIZE);
loadinfo->iobuffer = (FAR uint8_t *)kmm_malloc(CONFIG_MODLIB_BUFFERSIZE);
if (!loadinfo->iobuffer)
{
serr("ERROR: Failed to allocate an I/O buffer\n");
return -ENOMEM;
}
loadinfo->buflen = CONFIG_LIBC_MODLIB_BUFFERSIZE;
loadinfo->buflen = CONFIG_MODLIB_BUFFERSIZE;
}
return OK;
+1 -1
View File
@@ -58,7 +58,7 @@
* Pre-processor Definitions
****************************************************************************/
#define ELF_ALIGN_MASK ((1 << CONFIG_LIBC_MODLIB_ALIGN_LOG2) - 1)
#define ELF_ALIGN_MASK ((1 << CONFIG_MODLIB_ALIGN_LOG2) - 1)
#define ELF_ALIGNUP(a) (((unsigned long)(a) + ELF_ALIGN_MASK) & ~ELF_ALIGN_MASK)
#define ELF_ALIGNDOWN(a) ((unsigned long)(a) & ~ELF_ALIGN_MASK)
+3 -3
View File
@@ -78,18 +78,18 @@ FAR void *modhandle(FAR const char *name)
/* Get exclusive access to the module registry */
mod_registry_lock();
modlib_registry_lock();
/* Find the module entry for this name in the registry */
modp = mod_registry_find(name);
modp = modlib_registry_find(name);
if (modp == NULL)
{
serr("ERROR: Failed to find module %s: %d\n", name, ret);
set_errno(ENOENT);
}
mod_registry_unlock();
modlib_registry_unlock();
return (FAR void *)modp;
}
+5 -4
View File
@@ -44,6 +44,7 @@
#include <debug.h>
#include <nuttx/module.h>
#include <nuttx/symtab.h>
#include <nuttx/lib/modlib.h>
/****************************************************************************
@@ -88,8 +89,8 @@ FAR const void *modsym(FAR void *handle, FAR const char *name)
/* Verify that the module is in the registry */
mod_registry_lock();
ret = mod_registry_verify(modp);
modlib_registry_lock();
ret = modlib_registry_verify(modp);
if (ret < 0)
{
serr("ERROR: Failed to verify module: %d\n", ret);
@@ -119,12 +120,12 @@ FAR const void *modsym(FAR void *handle, FAR const char *name)
/* Return the address within the module assoicated with the symbol */
mod_registry_unlock();
modlib_registry_unlock();
DEBUGASSERT(symbol->sym_value != NULL);
return symbol->sym_value;
errout_with_lock:
mod_registry_unlock();
modlib_registry_unlock();
set_errno(err);
return NULL;
}
+1 -1
View File
@@ -247,7 +247,7 @@ static ssize_t modprocfs_read(FAR struct file *filep, FAR char *buffer,
priv->buflen = buflen;
priv->offset = filep->f_pos;
ret = mod_registry_foreach(modprocfs_callback, priv);
ret = modlib_registry_foreach(modprocfs_callback, priv);
if (ret >= 0)
{
filep->f_pos += priv->totalsize;
-345
View File
@@ -1,345 +0,0 @@
/****************************************************************************
* sched/module/mod_registry.c
*
* Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <semaphore.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NO_HOLDER ((pid_t)-1)
/****************************************************************************
* Private Types
****************************************************************************/
struct mod_registrylock_s
{
sem_t lock; /* The actual registry lock */
pid_t holder; /* The PID of the current holder of the lock */
int16_t count; /* The number of nested calls to mod_registry_lock */
};
/****************************************************************************
* Private Data
****************************************************************************/
static struct mod_registrylock_s g_modlock =
{
SEM_INITIALIZER(1), /* lock */
0, /* pid */
0 /* count */
};
static FAR struct module_s *g_mod_registry;
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: mod_registry_lock
*
* Description:
* Get exclusive access to the module registry.
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
void mod_registry_lock(void)
{
pid_t me;
/* Do we already hold the semaphore? */
me = getpid();
if (me == g_modlock.holder)
{
/* Yes... just increment the count */
g_modlock.count++;
DEBUGASSERT(g_modlock.count > 0);
}
/* Take the semaphore (perhaps waiting) */
else
{
while (sem_wait(&g_modlock.lock) != 0)
{
/* The only case that an error should occr here is if
* the wait was awakened by a signal.
*/
ASSERT(get_errno() == EINTR);
}
/* No we hold the semaphore */
g_modlock.holder = me;
g_modlock.count = 1;
}
}
/****************************************************************************
* Name: mod_registry_unlock
*
* Description:
* Relinquish the lock on the module registry
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
void mod_registry_unlock(void)
{
DEBUGASSERT(g_modlock.holder == getpid());
/* Is this our last count on the semaphore? */
if (g_modlock.count > 1)
{
/* No.. just decrement the count */
g_modlock.count--;
}
/* Yes.. then we can really release the semaphore */
else
{
g_modlock.holder = NO_HOLDER;
g_modlock.count = 0;
sem_post(&g_modlock.lock);
}
}
/****************************************************************************
* Name: mod_registry_add
*
* Description:
* Add a new entry to the module registry.
*
* Input Parameters:
* modp - The module data structure to be registered.
*
* Returned Value:
* None
*
* Assumptions:
* The caller holds the lock on the module registry.
*
****************************************************************************/
void mod_registry_add(FAR struct module_s *modp)
{
DEBUGASSERT(modp);
modp->flink = g_mod_registry;
g_mod_registry = modp;
}
/****************************************************************************
* Name: mod_registry_del
*
* Description:
* Remove a module entry from the registry
*
* Input Parameters:
* modp - The registry entry to be removed.
*
* Returned Value:
* Zero (OK) is returned if the registry entry was deleted. Otherwise,
* a negated errno value is returned.
*
* Assumptions:
* The caller holds the lock on the module registry.
*
****************************************************************************/
int mod_registry_del(FAR struct module_s *modp)
{
FAR struct module_s *prev;
FAR struct module_s *curr;
for (prev = NULL, curr = g_mod_registry;
curr != NULL && curr != modp;
prev = curr, curr = curr->flink);
if (curr == NULL)
{
serr("ERROR: Could not find module entry\n");
return -ENOENT;
}
if (prev == NULL)
{
g_mod_registry = modp->flink;
}
else
{
prev->flink = modp->flink;
}
modp->flink = NULL;
return OK;
}
/****************************************************************************
* Name: mod_registry_find
*
* Description:
* Find an entry in the module registry using the name of the module.
*
* Input Parameters:
* modulename - The name of the module to be found
*
* Returned Value:
* If the registry entry is found, a pointer to the module entry is
* returned. NULL is returned if the they entry is not found.
*
* Assumptions:
* The caller holds the lock on the module registry.
*
****************************************************************************/
FAR struct module_s *mod_registry_find(FAR const char *modulename)
{
FAR struct module_s *modp;
for (modp = g_mod_registry;
modp != NULL && strncmp(modp->modulename, modulename, MODULENAME_MAX) != 0;
modp = modp->flink);
return modp;
}
/****************************************************************************
* Name: mod_registry_verify
*
* Description:
* Verify that a module handle is valid by traversing the module list and
* assuring that the module still resides in the list. If it does not,
* the handle is probably a stale pointer.
*
* Input Parameters:
* modp - The registry entry to be verified.
*
* Returned Value:
* Returns OK is the module is valid; -ENOENT otherwise.
*
* Assumptions:
* The caller holds the lock on the module registry.
*
****************************************************************************/
int mod_registry_verify(FAR struct module_s *modp)
{
FAR struct module_s *node;
for (node = g_mod_registry; node != NULL; node = node->flink)
{
if (node == modp)
{
return OK;
}
}
return -ENOENT;
}
/****************************************************************************
* Name: mod_registry_foreach
*
* Description:
* Visit each module in the registry
*
* Input Parameters:
* callback - This callback function was be called for each entry in the
* registry.
* arg - This opaque argument will be passed to the callback function.
*
* Returned Value:
* This function normally returns zero (OK). If, however, any callback
* function returns a non-zero value, the traversal will be terminated and
* that non-zero value will be returned.
*
****************************************************************************/
int mod_registry_foreach(mod_callback_t callback, FAR void *arg)
{
FAR struct module_s *modp;
int ret = OK;
/* Get exclusive access to the module registry */
mod_registry_lock();
/* Visit each installed module */
for (modp = g_mod_registry; modp != NULL; modp = modp->flink)
{
/* Perform the callback */
ret = callback(modp, arg);
if (ret != 0)
{
break;
}
}
mod_registry_unlock();
return ret;
}
+8 -8
View File
@@ -77,18 +77,18 @@ int rmmod(FAR void *handle)
/* Get exclusive access to the module registry */
mod_registry_lock();
modlib_registry_lock();
/* Verify that the module is in the registry */
ret = mod_registry_verify(modp);
ret = modlib_registry_verify(modp);
if (ret < 0)
{
serr("ERROR: Failed to verify module: %d\n", ret);
goto errout_with_lock;
}
#if CONFIG_LIBC_MODLIB_MAXDEPEND > 0
#if CONFIG_MODLIB_MAXDEPEND > 0
/* Refuse to remove any module that other modules may depend upon. */
if (modp->dependents > 0)
@@ -145,19 +145,19 @@ int rmmod(FAR void *handle)
/* Remove the module from the registry */
ret = mod_registry_del(modp);
ret = modlib_registry_del(modp);
if (ret < 0)
{
serr("ERROR: Failed to remove the module from the registry: %d\n", ret);
goto errout_with_lock;
}
#if CONFIG_LIBC_MODLIB_MAXDEPEND > 0
#if CONFIG_MODLIB_MAXDEPEND > 0
/* Eliminate any dependencies that this module has on other modules */
(void)mod_undepend(modp);
(void)modlib_undepend(modp);
#endif
mod_registry_unlock();
modlib_registry_unlock();
/* And free the registry entry */
@@ -165,7 +165,7 @@ int rmmod(FAR void *handle)
return OK;
errout_with_lock:
mod_registry_unlock();
modlib_registry_unlock();
set_errno(-ret);
return ERROR;
}
+1 -1
View File
@@ -147,7 +147,7 @@ static inline int mod_sectname(FAR struct mod_loadinfo_s *loadinfo,
/* No.. then we have to read more */
ret = mod_reallocbuffer(loadinfo, CONFIG_LIBC_MODLIB_BUFFERINCR);
ret = mod_reallocbuffer(loadinfo, CONFIG_MODLIB_BUFFERINCR);
if (ret < 0)
{
serr("ERROR: mod_reallocbuffer failed: %d\n", ret);
+7 -7
View File
@@ -55,8 +55,8 @@
/* Amount to reallocate buffer when buffer is full */
#ifndef CONFIG_LIBC_MODLIB_BUFFERINCR
# define CONFIG_LIBC_MODLIB_BUFFERINCR 32
#ifndef CONFIG_MODLIB_BUFFERINCR
# define CONFIG_MODLIB_BUFFERINCR 32
#endif
/* Return values search for exported modules */
@@ -159,7 +159,7 @@ static int mod_symname(FAR struct mod_loadinfo_s *loadinfo,
/* No.. then we have to read more */
ret = mod_reallocbuffer(loadinfo, CONFIG_LIBC_MODLIB_BUFFERINCR);
ret = mod_reallocbuffer(loadinfo, CONFIG_MODLIB_BUFFERINCR);
if (ret < 0)
{
serr("ERROR: mod_reallocbuffer failed: %d\n", ret);
@@ -176,7 +176,7 @@ static int mod_symname(FAR struct mod_loadinfo_s *loadinfo,
* Name: mod_symcallback
*
* Description:
* mod_registry_foreach() callback function. Test if the provided module,
* modlib_registry_foreach() callback function. Test if the provided module,
* modp, exports the symbol of interest. If so, return that symbol value
* and setup the module dependency relationship.
*
@@ -209,10 +209,10 @@ static int mod_symcallback(FAR struct module_s *modp, FAR void *arg)
* stop the traversal.
*/
ret = mod_depend(exportinfo->modp, modp);
ret = modlib_depend(exportinfo->modp, modp);
if (ret < 0)
{
serr("ERROR: mod_depend failed: %d\n", ret);
serr("ERROR: modlib_depend failed: %d\n", ret);
return ret;
}
@@ -382,7 +382,7 @@ int mod_symvalue(FAR struct module_s *modp,
exportinfo.modp = modp;
exportinfo.symbol = NULL;
ret = mod_registry_foreach(mod_symcallback, (FAR void *)&exportinfo);
ret = modlib_registry_foreach(mod_symcallback, (FAR void *)&exportinfo);
if (ret < 0)
{
serr("ERROR: mod_symcallback failed: \n", ret);
+4 -4
View File
@@ -77,10 +77,10 @@ void mod_getsymtab(FAR const struct symtab_s **symtab, FAR int *nsymbols)
/* Borrow the registry lock to assure atomic access */
mod_registry_lock();
modlib_registry_lock();
*symtab = g_mod_symtab;
*nsymbols = g_mod_nsymbols;
mod_registry_unlock();
modlib_registry_unlock();
}
/****************************************************************************
@@ -102,8 +102,8 @@ void mod_setsymtab(FAR const struct symtab_s *symtab, int nsymbols)
{
/* Borrow the registry lock to assure atomic access */
mod_registry_lock();
modlib_registry_lock();
g_mod_symtab = symtab;
g_mod_nsymbols = nsymbols;
mod_registry_unlock();
modlib_registry_unlock();
}
-113
View File
@@ -1,113 +0,0 @@
/****************************************************************************
* sched/module/mod_uninit.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/kmalloc.h>
#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: mod_uninitialize
*
* Description:
* Releases any resources committed by mod_initialize(). This essentially
* undoes the actions of mod_initialize.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
int mod_uninitialize(struct mod_loadinfo_s *loadinfo)
{
/* Free all working buffers */
mod_freebuffers(loadinfo);
/* Close the ELF file */
if (loadinfo->filfd >= 0)
{
close(loadinfo->filfd);
}
return OK;
}
/****************************************************************************
* Name: mod_freebuffers
*
* Description:
* Release all working buffers.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
int mod_freebuffers(struct mod_loadinfo_s *loadinfo)
{
/* Release all working allocations */
if (loadinfo->shdr)
{
kmm_free((FAR void *)loadinfo->shdr);
loadinfo->shdr = NULL;
}
if (loadinfo->iobuffer)
{
kmm_free((FAR void *)loadinfo->iobuffer);
loadinfo->iobuffer = NULL;
loadinfo->buflen = 0;
}
return OK;
}
+2
View File
@@ -40,9 +40,11 @@
#include <nuttx/config.h>
#include <string.h>
#include <elf32.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/arch.h>
#include <nuttx/module.h>
/****************************************************************************