|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
- * Copyright (c) 2006-2021, RT-Thread Development Team
|
|
|
+ * Copyright (c) 2006-2024 RT-Thread Development Team
|
|
|
*
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
*
|
|
@@ -24,7 +24,7 @@
|
|
|
|
|
|
#define DBG_TAG "DLMD"
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
-#include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
|
|
|
+#include <rtdbg.h> /* must after of DEBUG_ENABLE or some other options*/
|
|
|
|
|
|
static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
|
|
|
static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
|
|
@@ -176,6 +176,11 @@ __exit:
|
|
|
return ;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief create a dynamic module object and initialize it.
|
|
|
+ *
|
|
|
+ * @return struct rt_dlmodule* If module create successfully, return the pointer to its rt_dlmodule structure.
|
|
|
+ */
|
|
|
struct rt_dlmodule *dlmodule_create(void)
|
|
|
{
|
|
|
struct rt_dlmodule *module = RT_NULL;
|
|
@@ -233,6 +238,12 @@ void dlmodule_destroy_subthread(struct rt_dlmodule *module, rt_thread_t thread)
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief destroy dynamic module and cleanup all kernel objects inside it.
|
|
|
+ *
|
|
|
+ * @param module Pointer to the module to be destroyed.
|
|
|
+ * @return rt_err_t On success, it returns RT_EOK. Otherwise, it returns the error code.
|
|
|
+ */
|
|
|
rt_err_t dlmodule_destroy(struct rt_dlmodule* module)
|
|
|
{
|
|
|
int i;
|
|
@@ -255,7 +266,7 @@ rt_err_t dlmodule_destroy(struct rt_dlmodule* module)
|
|
|
rt_exit_critical();
|
|
|
}
|
|
|
|
|
|
- // list_object(&(module->object_list));
|
|
|
+ /* list_object(&(module->object_list));*/
|
|
|
|
|
|
/* cleanup for all kernel objects inside module*/
|
|
|
{
|
|
@@ -393,6 +404,11 @@ rt_err_t dlmodule_destroy(struct rt_dlmodule* module)
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief retrieve the dynamically loaded module that the current thread belongs to.
|
|
|
+ *
|
|
|
+ * @return struct rt_dlmodule* On success, it returns a pointer to the module. otherwise, it returns RT_NULL.
|
|
|
+ */
|
|
|
struct rt_dlmodule *dlmodule_self(void)
|
|
|
{
|
|
|
rt_thread_t tid;
|
|
@@ -415,6 +431,20 @@ struct rt_dlmodule *rt_module_self(void)
|
|
|
return dlmodule_self();
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief load an ELF module to memory.
|
|
|
+ *
|
|
|
+ * @param filename the path to the module to load.
|
|
|
+ * @return struct rt_dlmodule* On success, it returns a pointer to the module object. otherwise, RT_NULL is returned.
|
|
|
+ *
|
|
|
+ * @note the function is used to load an ELF (Executable and Linkable Format) module from a file, validate it,
|
|
|
+ * and initialize it as a dynamically loaded module. what it implements are as follows:
|
|
|
+ * 1. Load and Validate ELF: It loads an ELF file, checks its validity, and identifies it as either a relocatable or shared object.
|
|
|
+ * 2. Memory Allocation and Cleanup: Uses rt_malloc and rt_free to allocate and free memory for module data.
|
|
|
+ * Error handling ensures all resources are released if an error occurs.
|
|
|
+ * 3. Symbol Resolution and Initialization: Sets up init function and cleanup function, and calls the module_init function if it is present.
|
|
|
+ * 4. Cache Management: Optionally (when RT_USING_CACHE defined) flushes data and invalidates instruction caches to ensure the module is correctly loaded into memory.
|
|
|
+ */
|
|
|
struct rt_dlmodule* dlmodule_load(const char* filename)
|
|
|
{
|
|
|
#ifdef RT_USING_POSIX_FS
|
|
@@ -525,6 +555,14 @@ __exit:
|
|
|
return RT_NULL;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief load a dynamic module, and create a thread to excute the module main function.
|
|
|
+ *
|
|
|
+ * @param pgname path of the module to be loaded.
|
|
|
+ * @param cmd the command string (with commandline options) for startup module.
|
|
|
+ * @param cmd_size the command's length.
|
|
|
+ * @return struct rt_dlmodule* On success, it returns a pointer to the module object. otherwise, RT_NULL is returned.
|
|
|
+ */
|
|
|
struct rt_dlmodule* dlmodule_exec(const char* pgname, const char* cmd, int cmd_size)
|
|
|
{
|
|
|
struct rt_dlmodule *module = RT_NULL;
|
|
@@ -742,6 +780,17 @@ struct rt_dlmodule* dlmodule_exec_custom(const char* pgname, const char* cmd, in
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
+/**
|
|
|
+ * @brief exit a dynamically loaded module.
|
|
|
+ *
|
|
|
+ * @param ret_code the return code for module exit.
|
|
|
+ *
|
|
|
+ * @note this function is responsible for gracefully exiting a dynamically loaded module, releasing resources associated with the module,
|
|
|
+ * and handling cleanup operations. what it implements are as follows:
|
|
|
+ * 1. Thread and Resource Cleanup: The function safely exits a module by deleting its main thread and freeing resources associated with it.
|
|
|
+ * 2. Status Management: Checks and updates the module's state, setting a return code and calling _dlmodule_exit() to transition to a closing state.
|
|
|
+ * 3. Critical Sections: Critical sections ensure that the exit process is atomic and free from race conditions.
|
|
|
+ */
|
|
|
void dlmodule_exit(int ret_code)
|
|
|
{
|
|
|
rt_thread_t thread;
|
|
@@ -784,6 +833,13 @@ void dlmodule_exit(int ret_code)
|
|
|
rt_exit_critical();
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief search for a symbol by its name in the kernel symbol table.
|
|
|
+ *
|
|
|
+ * @param sym_str the symbol name string.
|
|
|
+ * @return rt_uint32_t On success, it returns the address of the symbol.
|
|
|
+ * Otherwise, it returns 0 (indicating the symbol was not found).
|
|
|
+ */
|
|
|
rt_uint32_t dlmodule_symbol_find(const char *sym_str)
|
|
|
{
|
|
|
/* find in kernel symbol table */
|