diff --git a/include/nuttx/lib/modlib.h b/include/nuttx/lib/modlib.h index 4ddd0ee1ad..2ddde316d4 100644 --- a/include/nuttx/lib/modlib.h +++ b/include/nuttx/lib/modlib.h @@ -53,16 +53,16 @@ ****************************************************************************/ /* Configuration ************************************************************/ -#ifndef CONFIG_LIBC_MODLIB_ALIGN_LOG2 -# define CONFIG_LIBC_MODLIB_ALIGN_LOG2 2 +#ifndef CONFIG_MODLIB_ALIGN_LOG2 +# define CONFIG_MODLIB_ALIGN_LOG2 2 #endif -#ifndef CONFIG_LIBC_MODLIB_BUFFERSIZE -# define CONFIG_LIBC_MODLIB_BUFFERSIZE 128 +#ifndef CONFIG_MODLIB_BUFFERSIZE +# define CONFIG_MODLIB_BUFFERSIZE 128 #endif -#ifndef CONFIG_LIBC_MODLIB_BUFFERINCR -# define CONFIG_LIBC_MODLIB_BUFFERINCR 32 +#ifndef CONFIG_MODLIB_BUFFERINCR +# define CONFIG_MODLIB_BUFFERINCR 32 #endif #define MODULENAME_MAX 16 @@ -71,6 +71,64 @@ * Public Types ****************************************************************************/ +/* This is the type of the function that is called to uninitialize the + * the loaded module. This may mean, for example, un-registering a device + * driver. If the module is successfully initialized, its memory will be + * deallocated. + * + * Input Parameters: + * arg - An opaque argument that was previously returned by the initializer + * function. + * + * Returned Value: + * Zero (OK) on success; a negated errno value on any failure to + * initialize the module. If zero is returned, then the module memory + * will be deallocated. If the module is still in use (for example with + * open driver instances), the uninitialization function should fail with + * -EBUSY + */ + +typedef CODE int (*mod_uninitializer_t)(FAR void *arg); + +/* The contect of this structure is returned by module_initialize(). + * + * uninitializer - The pointer to the uninitialization function. NULL may + * be returned if no uninitialization is needed (i.e, the + * the module memory can be deallocated at any time). + * arg - An argument that will be passed to the uninitialization + function. + * exports - A symbol table exported by the module + * nexports - The number of symbols in the exported symbol table. + */ + +struct mod_info_s +{ + mod_uninitializer_t uninitializer; /* Module uninitializer */ + FAR void *arg; /* Uninitializer argument */ + FAR const struct symtab_s *exports; /* Symbols exported by module */ + unsigned int nexports; /* Number of symobols in exports list */ +}; + +/* A NuttX module is expected to export a function called module_initialize() + * that has the following function prototype. This function should appear as + * the entry point in the ELF module file and will be called by the binfmt + * logic after the module has been loaded into kernel memory. + * + * Input Parameters: + * modinfo - Module information returned by modlib_initialize(). + * + * Returned Value: + * Zero (OK) on success; a negated errno value on any failure to + * initialize the module. + */ + +typedef CODE int (*mod_initializer_t)(FAR struct mod_info_s *modinfo); + +/* This is the type of the callback function used by modlib_registry_foreach() */ + +struct module_s; +typedef CODE int (*mod_callback_t)(FAR struct module_s *modp, FAR void *arg); + /* This describes the file to be loaded. */ struct symtab_s; @@ -88,14 +146,14 @@ struct module_s size_t datasize; /* Size of the kernel .bss/.data memory allocation */ #endif -#if CONFIG_LIBC_MODLIB_MAXDEPEND > 0 +#if CONFIG_MODLIB_MAXDEPEND > 0 uint8_t dependents; /* Number of modules that depend on this module */ /* This is an upacked array of pointers to other modules that this module * depends upon. */ - FAR struct module_s *dependencies[CONFIG_LIBC_MODLIB_MAXDEPEND]; + FAR struct module_s *dependencies[CONFIG_MODLIB_MAXDEPEND]; #endif }; @@ -139,7 +197,7 @@ FAR int g_mod_nsymbols; ****************************************************************************/ /**************************************************************************** - * Name: mod_initialize + * Name: modlib_initialize * * Description: * This function is called to configure the library to process an kernel @@ -151,15 +209,15 @@ FAR int g_mod_nsymbols; * ****************************************************************************/ -int mod_initialize(FAR const char *filename, - FAR struct mod_loadinfo_s *loadinfo); +int modlib_initialize(FAR const char *filename, + FAR struct mod_loadinfo_s *loadinfo); /**************************************************************************** - * Name: mod_uninitialize + * Name: modlib_uninitialize * * Description: - * Releases any resources committed by mod_init(). This essentially - * undoes the actions of mod_initialize. + * Releases any resources committed by modlib_initialize(). This essentially + * undoes the actions of modlib_initialize. * * Returned Value: * 0 (OK) is returned on success and a negated errno is returned on @@ -167,7 +225,7 @@ int mod_initialize(FAR const char *filename, * ****************************************************************************/ -int mod_uninitialize(FAR struct mod_loadinfo_s *loadinfo); +int modlib_uninitialize(FAR struct mod_loadinfo_s *loadinfo); /**************************************************************************** * Name: mod_load @@ -185,7 +243,7 @@ int mod_uninitialize(FAR struct mod_loadinfo_s *loadinfo); int mod_load(FAR struct mod_loadinfo_s *loadinfo); /**************************************************************************** - * Name: mod_bind + * Name: modlib_bind * * Description: * Bind the imported symbol names in the loaded module described by @@ -197,7 +255,7 @@ int mod_load(FAR struct mod_loadinfo_s *loadinfo); * ****************************************************************************/ -int mod_bind(FAR struct module_s *modp, FAR struct mod_loadinfo_s *loadinfo); +int modlib_bind(FAR struct module_s *modp, FAR struct mod_loadinfo_s *loadinfo); /**************************************************************************** * Name: mod_unload @@ -220,7 +278,7 @@ int mod_bind(FAR struct module_s *modp, FAR struct mod_loadinfo_s *loadinfo); int mod_unload(struct mod_loadinfo_s *loadinfo); /**************************************************************************** - * Name: mod_depend + * Name: modlib_depend * * Description: * Set up module dependencies between the exporter and the importer of a @@ -236,12 +294,12 @@ int mod_unload(struct mod_loadinfo_s *loadinfo); * ****************************************************************************/ -#if CONFIG_LIBC_MODLIB_MAXDEPEND > 0 -int mod_depend(FAR struct module_s *importer, FAR struct module_s *exporter); +#if CONFIG_MODLIB_MAXDEPEND > 0 +int modlib_depend(FAR struct module_s *importer, FAR struct module_s *exporter); #endif /**************************************************************************** - * Name: mod_undepend + * Name: modlib_undepend * * Description: * Tear down module dependencies between the exporters and the importer of @@ -257,8 +315,8 @@ int mod_depend(FAR struct module_s *importer, FAR struct module_s *exporter); * ****************************************************************************/ -#if CONFIG_LIBC_MODLIB_MAXDEPEND > 0 -int mod_undepend(FAR struct module_s *importer); +#if CONFIG_MODLIB_MAXDEPEND > 0 +int modlib_undepend(FAR struct module_s *importer); #endif /**************************************************************************** @@ -430,7 +488,7 @@ int mod_allocbuffer(FAR struct mod_loadinfo_s *loadinfo); int mod_reallocbuffer(FAR struct mod_loadinfo_s *loadinfo, size_t increment); /**************************************************************************** - * Name: mod_registry_lock + * Name: modlib_registry_lock * * Description: * Get exclusive access to the module registry. @@ -443,10 +501,10 @@ int mod_reallocbuffer(FAR struct mod_loadinfo_s *loadinfo, size_t increment); * ****************************************************************************/ -void mod_registry_lock(void); +void modlib_registry_lock(void); /**************************************************************************** - * Name: mod_registry_unlock + * Name: modlib_registry_unlock * * Description: * Relinquish the lock on the module registry @@ -459,10 +517,10 @@ void mod_registry_lock(void); * ****************************************************************************/ -void mod_registry_unlock(void); +void modlib_registry_unlock(void); /**************************************************************************** - * Name: mod_registry_add + * Name: modlib_registry_add * * Description: * Add a new entry to the module registry. @@ -478,10 +536,10 @@ void mod_registry_unlock(void); * ****************************************************************************/ -void mod_registry_add(FAR struct module_s *modp); +void modlib_registry_add(FAR struct module_s *modp); /**************************************************************************** - * Name: mod_registry_del + * Name: modlib_registry_del * * Description: * Remove a module entry from the registry @@ -498,10 +556,10 @@ void mod_registry_add(FAR struct module_s *modp); * ****************************************************************************/ -int mod_registry_del(FAR struct module_s *modp); +int modlib_registry_del(FAR struct module_s *modp); /**************************************************************************** - * Name: mod_registry_find + * Name: modlib_registry_find * * Description: * Find an entry in the module registry using the name of the module. @@ -518,10 +576,10 @@ int mod_registry_del(FAR struct module_s *modp); * ****************************************************************************/ -FAR struct module_s *mod_registry_find(FAR const char *modulename); +FAR struct module_s *modlib_registry_find(FAR const char *modulename); /**************************************************************************** - * Name: mod_registry_verify + * Name: modlib_registry_verify * * Description: * Verify that a module handle is valid by traversing the module list and @@ -539,6 +597,30 @@ FAR struct module_s *mod_registry_find(FAR const char *modulename); * ****************************************************************************/ -int mod_registry_verify(FAR struct module_s *modp); +int modlib_registry_verify(FAR struct module_s *modp); + +/**************************************************************************** + * Name: modlib_registry_foreach + * + * Description: + * Visit each module in the registry. This is an internal OS interface and + * not available for use by applications. + * + * 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. + * + * Assumptions: + * The caller does NOT hold the lock on the module registry. + * + ****************************************************************************/ + +int modlib_registry_foreach(mod_callback_t callback, FAR void *arg); #endif /* __INCLUDE_NUTTX_LIB_MODLIB_H */ diff --git a/include/nuttx/module.h b/include/nuttx/module.h index cf9eed5a9a..911c8bbb03 100644 --- a/include/nuttx/module.h +++ b/include/nuttx/module.h @@ -44,78 +44,6 @@ #include -#include -#include -#include - -#include -#include -#include - -/**************************************************************************** - * Public Types - ****************************************************************************/ - -/* This is the type of the function that is called to uninitialize the - * the loaded module. This may mean, for example, un-registering a device - * driver. If the module is successfully initialized, its memory will be - * deallocated. - * - * Input Parameters: - * arg - An opaque argument that was previously returned by the initializer - * function. - * - * Returned Value: - * Zero (OK) on success; a negated errno value on any failure to - * initialize the module. If zero is returned, then the module memory - * will be deallocated. If the module is still in use (for example with - * open driver instances), the uninitialization function should fail with - * -EBUSY - */ - -typedef CODE int (*mod_uninitializer_t)(FAR void *arg); - -/* The contect of this structure is returned by module_initialize(). - * - * uninitializer - The pointer to the uninitialization function. NULL may - * be returned if no uninitialization is needed (i.e, the - * the module memory can be deallocated at any time). - * arg - An argument that will be passed to the uninitialization - function. - * exports - A symbol table exported by the module - * nexports - The number of symbols in the exported symbol table. - */ - -struct mod_info_s -{ - mod_uninitializer_t uninitializer; /* Module uninitializer */ - FAR void *arg; /* Uninitializer argument */ - FAR const struct symtab_s *exports; /* Symbols exported by module */ - unsigned int nexports; /* Number of symobols in exports list */ -}; - -/* A NuttX module is expected to export a function called module_initialize() - * that has the following function prototype. This function should appear as - * the entry point in the ELF module file and will be called by the binfmt - * logic after the module has been loaded into kernel memory. - * - * Input Parameters: - * modinfo - Module information returned by mod_initialize(). - * - * Returned Value: - * Zero (OK) on success; a negated errno value on any failure to - * initialize the module. - */ - -typedef CODE int (*mod_initializer_t)(FAR struct mod_info_s *modinfo); - -#if defined(__KERNEL__) || defined(CONFIG_BUILD_FLAT) -/* This is the type of the callback function used by mod_registry_foreach() */ - -struct module_s; -typedef CODE int (*mod_callback_t)(FAR struct module_s *modp, FAR void *arg); -#endif - /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -260,32 +188,6 @@ FAR const void *modsym(FAR void *handle, FAR const char *name); FAR void *modhandle(FAR const char *name); -/**************************************************************************** - * Name: mod_registry_foreach - * - * Description: - * Visit each module in the registry. This is an internal OS interface and - * not available for use by applications. - * - * 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. - * - * Assumptions: - * The caller does NOT hold the lock on the module registry. - * - ****************************************************************************/ - -#if defined(__KERNEL__) || defined(CONFIG_BUILD_FLAT) -int mod_registry_foreach(mod_callback_t callback, FAR void *arg); -#endif - #undef EXTERN #if defined(__cplusplus) } diff --git a/libc/modlib/Kconfig b/libc/modlib/Kconfig index eeb5d05dcc..81e8d3178f 100644 --- a/libc/modlib/Kconfig +++ b/libc/modlib/Kconfig @@ -7,6 +7,10 @@ config LIBC_MODLIB bool default n +config MODLIB_NAMES + bool + default n + menu "Module library configuration" depends on LIBC_MODLIB diff --git a/libc/modlib/Make.defs b/libc/modlib/Make.defs index 4953096085..7917eee92e 100644 --- a/libc/modlib/Make.defs +++ b/libc/modlib/Make.defs @@ -37,6 +37,9 @@ ifeq ($(CONFIG_LIBC_MODLIB),y) # Add the nuttx/lib/modlib.h files to the build +CSRCS += modlib_bind.c modlib_depend.c modlib_init.c modlib_registry.c +CSRCS += modlib_uninit.c + # Add the modlib directory to the build DEPPATH += --dep-path modlib diff --git a/sched/module/mod_bind.c b/libc/modlib/modlib_bind.c similarity index 92% rename from sched/module/mod_bind.c rename to libc/modlib/modlib_bind.c index 40c5bfe618..08b745a6ff 100644 --- a/sched/module/mod_bind.c +++ b/libc/modlib/modlib_bind.c @@ -1,5 +1,5 @@ /**************************************************************************** - * sched/module/mod_bind.c + * libc/modlib/modlib_bind.c * * Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -55,16 +55,16 @@ ****************************************************************************/ /**************************************************************************** - * Name: mod_readrel + * Name: modlib_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) +static inline int modlib_readrel(FAR struct mod_loadinfo_s *loadinfo, + FAR const Elf32_Shdr *relsec, + int index, FAR Elf32_Rel *rel) { off_t offset; @@ -86,7 +86,7 @@ static inline int mod_readrel(FAR struct mod_loadinfo_s *loadinfo, } /**************************************************************************** - * Name: mod_relocate and mod_relocateadd + * Name: modlib_relocate and modlib_relocateadd * * Description: * Perform all relocations associated with a section. @@ -97,8 +97,8 @@ static inline int mod_readrel(FAR struct mod_loadinfo_s *loadinfo, * ****************************************************************************/ -static int mod_relocate(FAR struct module_s *modp, - FAR struct mod_loadinfo_s *loadinfo, int relidx) +static int modlib_relocate(FAR struct module_s *modp, + FAR struct mod_loadinfo_s *loadinfo, int relidx) { FAR Elf32_Shdr *relsec = &loadinfo->shdr[relidx]; @@ -122,7 +122,7 @@ static int mod_relocate(FAR struct module_s *modp, /* Read the relocation entry into memory */ - ret = mod_readrel(loadinfo, relsec, i, &rel); + ret = modlib_readrel(loadinfo, relsec, i, &rel); if (ret < 0) { serr("ERROR: Section %d reloc %d: Failed to read relocation entry: %d\n", @@ -199,7 +199,7 @@ static int mod_relocate(FAR struct module_s *modp, return OK; } -static int mod_relocateadd(FAR struct module_s *modp, +static int modlib_relocateadd(FAR struct module_s *modp, FAR struct mod_loadinfo_s *loadinfo, int relidx) { serr("ERROR: Not implemented\n"); @@ -211,7 +211,7 @@ static int mod_relocateadd(FAR struct module_s *modp, ****************************************************************************/ /**************************************************************************** - * Name: mod_bind + * Name: modlib_bind * * Description: * Bind the imported symbol names in the loaded module described by @@ -227,7 +227,7 @@ static int mod_relocateadd(FAR struct module_s *modp, * ****************************************************************************/ -int mod_bind(FAR struct module_s *modp, FAR struct mod_loadinfo_s *loadinfo) +int modlib_bind(FAR struct module_s *modp, FAR struct mod_loadinfo_s *loadinfo) { int ret; int i; @@ -276,11 +276,11 @@ int mod_bind(FAR struct module_s *modp, FAR struct mod_loadinfo_s *loadinfo) if (loadinfo->shdr[i].sh_type == SHT_REL) { - ret = mod_relocate(modp, loadinfo, i); + ret = modlib_relocate(modp, loadinfo, i); } else if (loadinfo->shdr[i].sh_type == SHT_RELA) { - ret = mod_relocateadd(modp, loadinfo, i); + ret = modlib_relocateadd(modp, loadinfo, i); } if (ret < 0) diff --git a/sched/module/mod_depend.c b/libc/modlib/modlib_depend.c similarity index 91% rename from sched/module/mod_depend.c rename to libc/modlib/modlib_depend.c index 3de75fb23b..31ba2e56d8 100644 --- a/sched/module/mod_depend.c +++ b/libc/modlib/modlib_depend.c @@ -1,5 +1,5 @@ /**************************************************************************** - * sched/module/mod_depend.c + * libc/modlib/modlib_depend.c * * Copyright (C) 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -43,7 +43,6 @@ #include #include -#include #include #include @@ -52,7 +51,7 @@ ****************************************************************************/ /**************************************************************************** - * Name: mod_depend + * Name: modlib_depend * * Description: * Set up module dependencies between the exporter and the importer of a @@ -68,9 +67,9 @@ * ****************************************************************************/ -int mod_depend(FAR struct module_s *importer, FAR struct module_s *exporter) +int modlib_depend(FAR struct module_s *importer, FAR struct module_s *exporter) { -#if CONFIG_LIBC_MODLIB_MAXDEPEND > 0 +#if CONFIG_MODLIB_MAXDEPEND > 0 int freendx; int i; @@ -82,11 +81,11 @@ int mod_depend(FAR struct module_s *importer, FAR struct module_s *exporter) * 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 + * should not be too inefficient if the number of CONFIG_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++) + for (i = 0, freendx = -1; i < CONFIG_MODLIB_MAXDEPEND; i++) { FAR const struct module_s *modp; @@ -149,7 +148,7 @@ int mod_depend(FAR struct module_s *importer, FAR struct module_s *exporter) } /**************************************************************************** - * Name: mod_undepend + * Name: modlib_undepend * * Description: * Tear down module dependencies between the exporters and the importer of @@ -165,9 +164,9 @@ int mod_depend(FAR struct module_s *importer, FAR struct module_s *exporter) * ****************************************************************************/ -int mod_undepend(FAR struct module_s *importer) +int modlib_undepend(FAR struct module_s *importer) { -#if CONFIG_LIBC_MODLIB_MAXDEPEND > 0 +#if CONFIG_MODLIB_MAXDEPEND > 0 FAR struct module_s *exporter; int i; @@ -175,11 +174,11 @@ int mod_undepend(FAR struct module_s *importer) /* 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 + * should not be too inefficient if the number of CONFIG_MODLIB_MAXDEPEND * is small. Otherwise, a more dynamic data structure would be in order. */ - for (i = 0; i < CONFIG_LIBC_MODLIB_MAXDEPEND; i++) + for (i = 0; i < CONFIG_MODLIB_MAXDEPEND; i++) { exporter = importer->dependencies[i]; if (exporter != NULL) diff --git a/sched/module/mod_init.c b/libc/modlib/modlib_init.c similarity index 89% rename from sched/module/mod_init.c rename to libc/modlib/modlib_init.c index cfb2364269..0753e80f24 100644 --- a/sched/module/mod_init.c +++ b/libc/modlib/modlib_init.c @@ -1,7 +1,7 @@ /**************************************************************************** - * sched/module/mod_init.c + * libc/modlib/modlib_init.c * - * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -55,24 +55,20 @@ * 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. +/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_MODLIB_DUMPBUFFER + * have to be defined or CONFIG_MODLIB_DUMPBUFFER does nothing. */ -#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_LIBC_MODLIB_DUMPBUFFER) -# undef CONFIG_LIBC_MODLIB_DUMPBUFFER +#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_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) #endif -/**************************************************************************** - * Private Constant Data - ****************************************************************************/ - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -128,7 +124,7 @@ static inline int mod_filelen(FAR struct mod_loadinfo_s *loadinfo, ****************************************************************************/ /**************************************************************************** - * Name: mod_initialize + * Name: modlib_initialize * * Description: * This function is called to configure the library to process an ELF @@ -140,8 +136,8 @@ static inline int mod_filelen(FAR struct mod_loadinfo_s *loadinfo, * ****************************************************************************/ -int mod_initialize(FAR const char *filename, - FAR struct mod_loadinfo_s *loadinfo) +int modlib_initialize(FAR const char *filename, + FAR struct mod_loadinfo_s *loadinfo) { int ret; diff --git a/sched/module/mod_registry.c b/libc/modlib/modlib_registry.c similarity index 92% rename from sched/module/mod_registry.c rename to libc/modlib/modlib_registry.c index 99fc4c9dcc..5fe8ef02c0 100644 --- a/sched/module/mod_registry.c +++ b/libc/modlib/modlib_registry.c @@ -1,5 +1,5 @@ /**************************************************************************** - * sched/module/mod_registry.c + * libc/modlib/modlib_registry.c * * Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -61,7 +61,7 @@ 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 */ + int16_t count; /* The number of nested calls to modlib_registry_lock */ }; /**************************************************************************** @@ -82,7 +82,7 @@ static FAR struct module_s *g_mod_registry; ****************************************************************************/ /**************************************************************************** - * Name: mod_registry_lock + * Name: modlib_registry_lock * * Description: * Get exclusive access to the module registry. @@ -95,7 +95,7 @@ static FAR struct module_s *g_mod_registry; * ****************************************************************************/ -void mod_registry_lock(void) +void modlib_registry_lock(void) { pid_t me; @@ -131,7 +131,7 @@ void mod_registry_lock(void) } /**************************************************************************** - * Name: mod_registry_unlock + * Name: modlib_registry_unlock * * Description: * Relinquish the lock on the module registry @@ -144,7 +144,7 @@ void mod_registry_lock(void) * ****************************************************************************/ -void mod_registry_unlock(void) +void modlib_registry_unlock(void) { DEBUGASSERT(g_modlock.holder == getpid()); @@ -168,7 +168,7 @@ void mod_registry_unlock(void) } /**************************************************************************** - * Name: mod_registry_add + * Name: modlib_registry_add * * Description: * Add a new entry to the module registry. @@ -184,7 +184,7 @@ void mod_registry_unlock(void) * ****************************************************************************/ -void mod_registry_add(FAR struct module_s *modp) +void modlib_registry_add(FAR struct module_s *modp) { DEBUGASSERT(modp); modp->flink = g_mod_registry; @@ -192,7 +192,7 @@ void mod_registry_add(FAR struct module_s *modp) } /**************************************************************************** - * Name: mod_registry_del + * Name: modlib_registry_del * * Description: * Remove a module entry from the registry @@ -209,7 +209,7 @@ void mod_registry_add(FAR struct module_s *modp) * ****************************************************************************/ -int mod_registry_del(FAR struct module_s *modp) +int modlib_registry_del(FAR struct module_s *modp) { FAR struct module_s *prev; FAR struct module_s *curr; @@ -238,7 +238,7 @@ int mod_registry_del(FAR struct module_s *modp) } /**************************************************************************** - * Name: mod_registry_find + * Name: modlib_registry_find * * Description: * Find an entry in the module registry using the name of the module. @@ -255,7 +255,7 @@ int mod_registry_del(FAR struct module_s *modp) * ****************************************************************************/ -FAR struct module_s *mod_registry_find(FAR const char *modulename) +FAR struct module_s *modlib_registry_find(FAR const char *modulename) { FAR struct module_s *modp; @@ -267,7 +267,7 @@ FAR struct module_s *mod_registry_find(FAR const char *modulename) } /**************************************************************************** - * Name: mod_registry_verify + * Name: modlib_registry_verify * * Description: * Verify that a module handle is valid by traversing the module list and @@ -285,7 +285,7 @@ FAR struct module_s *mod_registry_find(FAR const char *modulename) * ****************************************************************************/ -int mod_registry_verify(FAR struct module_s *modp) +int modlib_registry_verify(FAR struct module_s *modp) { FAR struct module_s *node; @@ -301,7 +301,7 @@ int mod_registry_verify(FAR struct module_s *modp) } /**************************************************************************** - * Name: mod_registry_foreach + * Name: modlib_registry_foreach * * Description: * Visit each module in the registry @@ -318,14 +318,14 @@ int mod_registry_verify(FAR struct module_s *modp) * ****************************************************************************/ -int mod_registry_foreach(mod_callback_t callback, FAR void *arg) +int modlib_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(); + modlib_registry_lock(); /* Visit each installed module */ @@ -340,6 +340,6 @@ int mod_registry_foreach(mod_callback_t callback, FAR void *arg) } } - mod_registry_unlock(); + modlib_registry_unlock(); return ret; } \ No newline at end of file diff --git a/sched/module/mod_uninit.c b/libc/modlib/modlib_uninit.c similarity index 87% rename from sched/module/mod_uninit.c rename to libc/modlib/modlib_uninit.c index 7987425c76..a2e939be1e 100644 --- a/sched/module/mod_uninit.c +++ b/libc/modlib/modlib_uninit.c @@ -1,7 +1,7 @@ /**************************************************************************** - * sched/module/mod_uninit.c + * libc/modlib/modlib_uninit.c * - * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Copyright (C) 2015. 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -47,16 +47,18 @@ #include #include +#include "libc.h" + /**************************************************************************** * Public Functions ****************************************************************************/ /**************************************************************************** - * Name: mod_uninitialize + * Name: modlib_uninitialize * * Description: - * Releases any resources committed by mod_initialize(). This essentially - * undoes the actions of mod_initialize. + * Releases any resources committed by modlib_initialize(). This + * essentially undoes the actions of modlib_initialize. * * Returned Value: * 0 (OK) is returned on success and a negated errno is returned on @@ -64,7 +66,7 @@ * ****************************************************************************/ -int mod_uninitialize(struct mod_loadinfo_s *loadinfo) +int modlib_uninitialize(struct mod_loadinfo_s *loadinfo) { /* Free all working buffers */ @@ -96,15 +98,15 @@ int mod_freebuffers(struct mod_loadinfo_s *loadinfo) { /* Release all working allocations */ - if (loadinfo->shdr) + if (loadinfo->shdr != NULL) { - kmm_free((FAR void *)loadinfo->shdr); + lib_free((FAR void *)loadinfo->shdr); loadinfo->shdr = NULL; } - if (loadinfo->iobuffer) + if (loadinfo->iobuffer != NULL) { - kmm_free((FAR void *)loadinfo->iobuffer); + lib_free((FAR void *)loadinfo->iobuffer); loadinfo->iobuffer = NULL; loadinfo->buflen = 0; } diff --git a/sched/Kconfig b/sched/Kconfig index db3e695019..199fe439b7 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -1123,6 +1123,7 @@ config MODULE bool "Enable loadable OS modules" default n select LIBC_MODLIB + select LIBC_MODLIB_NAMES select LIBC_ARCH_ELF ---help--- Enable support for loadable OS modules. Default: n diff --git a/sched/module/Make.defs b/sched/module/Make.defs index 03b363c685..7bf6394a76 100644 --- a/sched/module/Make.defs +++ b/sched/module/Make.defs @@ -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 # # 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 diff --git a/sched/module/mod_insmod.c b/sched/module/mod_insmod.c index c0a17f3379..3f9a073b96 100644 --- a/sched/module/mod_insmod.c +++ b/sched/module/mod_insmod.c @@ -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; } diff --git a/sched/module/mod_iobuffer.c b/sched/module/mod_iobuffer.c index b2e77efeec..b2ba2f2815 100644 --- a/sched/module/mod_iobuffer.c +++ b/sched/module/mod_iobuffer.c @@ -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; diff --git a/sched/module/mod_load.c b/sched/module/mod_load.c index 2a8738a84a..3287c1486e 100644 --- a/sched/module/mod_load.c +++ b/sched/module/mod_load.c @@ -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) diff --git a/sched/module/mod_modhandle.c b/sched/module/mod_modhandle.c index 7331a69f09..2ebf3a4b5e 100644 --- a/sched/module/mod_modhandle.c +++ b/sched/module/mod_modhandle.c @@ -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; } diff --git a/sched/module/mod_modsym.c b/sched/module/mod_modsym.c index a7eae25357..8aba9b1753 100644 --- a/sched/module/mod_modsym.c +++ b/sched/module/mod_modsym.c @@ -44,6 +44,7 @@ #include #include +#include #include /**************************************************************************** @@ -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; } diff --git a/sched/module/mod_procfs.c b/sched/module/mod_procfs.c index a91c17e23c..89a65b161a 100644 --- a/sched/module/mod_procfs.c +++ b/sched/module/mod_procfs.c @@ -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; diff --git a/sched/module/mod_rmmod.c b/sched/module/mod_rmmod.c index 88b221fd76..cc47e79220 100644 --- a/sched/module/mod_rmmod.c +++ b/sched/module/mod_rmmod.c @@ -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; } diff --git a/sched/module/mod_sections.c b/sched/module/mod_sections.c index 6e4dc2f5dd..574eb8b9ac 100644 --- a/sched/module/mod_sections.c +++ b/sched/module/mod_sections.c @@ -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); diff --git a/sched/module/mod_symbols.c b/sched/module/mod_symbols.c index 80a52e7557..4ec42c581a 100644 --- a/sched/module/mod_symbols.c +++ b/sched/module/mod_symbols.c @@ -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); diff --git a/sched/module/mod_symtab.c b/sched/module/mod_symtab.c index 51860b7fd5..864f7e9200 100644 --- a/sched/module/mod_symtab.c +++ b/sched/module/mod_symtab.c @@ -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(); } diff --git a/sched/module/mod_verify.c b/sched/module/mod_verify.c index 5ad4d74596..252ea32296 100644 --- a/sched/module/mod_verify.c +++ b/sched/module/mod_verify.c @@ -40,9 +40,11 @@ #include #include +#include #include #include +#include #include /****************************************************************************