Skip to content

Commit

Permalink
modlib/dlfcn:unify same code
Browse files Browse the repository at this point in the history
Signed-off-by: anjiahao <[email protected]>
  • Loading branch information
anjiahao1 committed Sep 24, 2024
1 parent cf3d495 commit 7546f59
Show file tree
Hide file tree
Showing 14 changed files with 818 additions and 753 deletions.
98 changes: 98 additions & 0 deletions include/nuttx/lib/modlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,102 @@ int modlib_registry_foreach(mod_callback_t callback, FAR void *arg);

void modlib_freesymtab(FAR struct module_s *modp);

/****************************************************************************
* Name: modlib_insert
*
* Description:
* Verify that the file is an ELF module binary and, if so, load the
* module into kernel memory and initialize it for use.
*
* NOTE: modlib_setsymtab() had to have been called in board-specific OS
* logic prior to calling this function from application logic (perhaps via
* boardctl(BOARDIOC_OS_SYMTAB). Otherwise, insmod will be unable to
* resolve symbols in the OS module.
*
* Input Parameters:
*
* filename - Full path to the module binary to be loaded
* modname - The name that can be used to refer to the module after
* it has been loaded.
*
* Returned Value:
* A non-NULL module handle that can be used on subsequent calls to other
* module interfaces is returned on success. If modlib_insert() was
* unable to load the module modlib_insert() will return a NULL handle
* and the errno variable will be set appropriately.
*
****************************************************************************/

FAR void *modlib_insert(FAR const char *filename, FAR const char *modname);

/****************************************************************************
* Name: modlib_getsymbol
*
* Description:
* modlib_getsymbol() returns the address of a symbol defined within the
* object that was previously made accessible through a modlib_getsymbol()
* call. handle is the value returned from a call to modlib_insert() (and
* which has not since been released via a call to modlib_remove()),
* name is the symbol's name as a character string.
*
* The returned symbol address will remain valid until modlib_remove() is
* called.
*
* Input Parameters:
* handle - The opaque, non-NULL value returned by a previous successful
* call to modlib_insert().
* name - A pointer to the symbol name string.
*
* Returned Value:
* The address associated with the symbol is returned on success.
* If handle does not refer to a valid module opened by modlib_insert(),
* or if the named modlib_symbol cannot be found within any of the objects
* associated with handle, modlib_getsymbol() will return NULL and the
* errno variable will be set appropriately.
*
* NOTE: This means that the address zero can never be a valid return
* value.
*
****************************************************************************/

FAR const void *modlib_getsymbol(FAR void *handle, FAR const char *name);

/****************************************************************************
* Name: modlib_remove
*
* Description:
* Remove a previously installed module from memory.
*
* Input Parameters:
* handle - The module handler previously returned by modlib_insert().
*
* Returned Value:
* Zero (OK) on success. On any failure, -1 (ERROR) is returned the
* errno value is set appropriately.
*
****************************************************************************/

int modlib_remove(FAR void *handle);

/****************************************************************************
* Name: modlib_modhandle
*
* Description:
* modlib_modhandle() returns the module handle for the installed
* module with the provided name. A secondary use of this function is to
* determine if a module has been loaded or not.
*
* Input Parameters:
* name - A pointer to the module name string.
*
* Returned Value:
* The non-NULL module handle previously returned by modlib_insert() is
* returned on success. If no module with that name is installed,
* modlib_modhandle() will return a NULL handle and the errno variable
* will be set appropriately.
*
****************************************************************************/

FAR void *modlib_gethandle(FAR const char *name);

#endif /* __INCLUDE_NUTTX_LIB_MODLIB_H */
18 changes: 4 additions & 14 deletions libs/libc/dlfcn/lib_dlclose.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,9 @@
#include <nuttx/config.h>

#include <dlfcn.h>
#include <assert.h>
#include <debug.h>
#include <errno.h>

#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>

#include "libc.h"

/****************************************************************************
* Private Functions
****************************************************************************/
Expand Down Expand Up @@ -277,23 +271,19 @@ static inline int dlremove(FAR void *handle)

int dlclose(FAR void *handle)
{
#if defined(CONFIG_BUILD_FLAT)
#if defined(CONFIG_BUILD_FLAT) || defined(CONFIG_BUILD_PROTECTED)
/* In the FLAT build, a shared library is essentially the same as a kernel
* module.
*/

return rmmod(handle);

#elif defined(CONFIG_BUILD_PROTECTED)
/* The PROTECTED build is equivalent to the FLAT build EXCEPT that there
*
* The PROTECTED build is equivalent to the FLAT build EXCEPT that there
* must be two copies of the module logic: One residing in kernel
* space and using the kernel symbol table and one residing in user space
* using the user space symbol table.
*
* dlremove() is essentially a clone of rmmod().
*/

return dlremove(handle);
return modlib_remove(handle);

#else /* if defined(CONFIG_BUILD_KERNEL) */
/* The KERNEL build is considerably more complex: In order to be shared,
Expand Down
Loading

0 comments on commit 7546f59

Please sign in to comment.