Skip to content

Commit f093463

Browse files
GorrayLiRbb666
authored andcommitted
[libc] add comments for libdl APIs.
1 parent 20263be commit f093463

File tree

13 files changed

+153
-16
lines changed

13 files changed

+153
-16
lines changed

components/libc/posix/libdl/arch/arm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*

components/libc/posix/libdl/arch/riscv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*

components/libc/posix/libdl/arch/x86.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*

components/libc/posix/libdl/dlclose.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -13,6 +13,15 @@
1313

1414
#include "dlmodule.h"
1515

16+
/**
17+
* @brief close a dynamically loaded shared library.
18+
*
19+
* @param handle the handle which identifies the shared library to be closed.
20+
* @return int it returns RT_TRUE on success.
21+
*
22+
* @note This function is an API of POSIX standard, which is designed to decrease the reference count (nref) for a dynamically loaded module
23+
* and destroy it if no references remain.
24+
*/
1625
int dlclose(void *handle)
1726
{
1827
struct rt_dlmodule *module;

components/libc/posix/libdl/dlelf.c

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -14,8 +14,32 @@
1414

1515
#define DBG_TAG "DLMD"
1616
#define DBG_LVL DBG_INFO
17-
#include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
17+
#include <rtdbg.h> /* must after of DEBUG_ENABLE or some other options*/
1818

19+
/**
20+
* @brief Load a shared object file into memory.
21+
*
22+
* @param module A pointer to a rt_dlmodule object for holding the module's information.
23+
* @param module_ptr A pointer to the raw memory of the ELF file (shared object) that is being loaded.
24+
* @return rt_err_t On success, it returns RT_EOK. Otherwise, it returns the error code.
25+
*
26+
* @note This function loads a shared object (ELF file) into memory, broken down into steps:
27+
* 1. Initialization and Validation: it begins by validating the module pointer
28+
* and checking if the ELF file has been linked by comparing its magic number (RTMMAG).
29+
* If matched, the module is considered linked.
30+
* 2. Calculating the ELF Image Size: it iterates over the ELF program headers to compute the total size of the ELF image
31+
* by adding the sizes of loadable segments. It also ensures there are no overlaps or invalid addresses in the segments.
32+
* 3. Allocating Memory for the Module: After determining the module size, the function allocates memory (module->mem_space) for the ELF image
33+
* and initializes it to zero. Then, it copies the relevant program segments from the ELF file into this allocated memory.
34+
* 4. Setting the Module Entry Point: it sets the entry point address (module->entry_addr) based on the ELF entry point adjusted by the calculated base address.
35+
* 5. Handling Relocation Sections: It processes each relocation section in the ELF file.
36+
* For each relocation entry, it either resolves the symbol from the module's own symbol table
37+
* or looks up the symbol in the kernel symbol table if it was not found locally.
38+
* 6. Building the Module's Symbol Table: it looks for the .dynsym section to extract global function symbols.
39+
* It creates a symbol table (module->symtab) and populates it with the function names and addresses for all global symbols of type STT_FUNC.
40+
* 7. Extracting Additional Parameters: It extracts additional parameters, such as thread priority (dlmodule_thread_priority) and stack size (dlmodule_thread_stacksize),
41+
* from the symbol table and assigns them to the module if valid.
42+
*/
1943
rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_ptr)
2044
{
2145
rt_bool_t linked = RT_FALSE;
@@ -273,6 +297,23 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
273297
return RT_EOK;
274298
}
275299

300+
/**
301+
* @brief Load a relocatable file into memory.
302+
*
303+
* @param module A pointer to a rt_dlmodule object for holding the module's information.
304+
* @param module_ptr A pointer to the raw memory of the ELF file (relocatable file) that is being loaded.
305+
* @return rt_err_t On success, it returns RT_EOK. Otherwise, it returns the error code.
306+
*
307+
* @note This function loads a relocatable file (ELF file) into memory, broken down step by step:
308+
* 1. Calculate Module Size: iterates over the ELF sections (text, data, rodata, and bss) to calculate the total size of the module
309+
* and identifies the start address for each section.
310+
* 2. Allocate Memory: It allocates memory for the module based on the calculated size. If allocation fails, an error is returned.
311+
* 3. Load Sections into Memory: The function loads the text, rodata, data, and BSS sections into the allocated memory.
312+
* The BSS section is zeroed out, while the others are copied from the ELF image.
313+
* 4. Set Entry Point: The entry point of the module is set by calculating the address relative to the start of the allocated memory.
314+
* 5. Handle Relocation: It processes the relocation entries, resolving symbol addresses and relocating them as needed.
315+
* This includes functions, sections (rodata, bss, data), and external symbols from the kernel symbol table.
316+
*/
276317
rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module_ptr)
277318
{
278319
rt_ubase_t index, rodata_addr = 0, bss_addr = 0, data_addr = 0;

components/libc/posix/libdl/dlelf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*

components/libc/posix/libdl/dlerror.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -11,6 +11,13 @@
1111
#include <rtthread.h>
1212
#include <rtm.h>
1313

14+
/**
15+
* @brief retrieve a string describing the last error that occurred from a dynamic linking operation.
16+
*
17+
* @return const char* a string containing an error message describing the last error.
18+
*
19+
* @note This function is an API of POSIX standard, which is still remaining TBD.
20+
*/
1421
const char *dlerror(void)
1522
{
1623
return "TODO";

components/libc/posix/libdl/dlfcn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*

components/libc/posix/libdl/dlmodule.c

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -24,7 +24,7 @@
2424

2525
#define DBG_TAG "DLMD"
2626
#define DBG_LVL DBG_INFO
27-
#include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
27+
#include <rtdbg.h> /* must after of DEBUG_ENABLE or some other options*/
2828

2929
static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
3030
static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
@@ -176,6 +176,11 @@ static void _dlmodule_thread_entry(void* parameter)
176176
return ;
177177
}
178178

179+
/**
180+
* @brief create a dynamic module object and initialize it.
181+
*
182+
* @return struct rt_dlmodule* If module create successfully, return the pointer to its rt_dlmodule structure.
183+
*/
179184
struct rt_dlmodule *dlmodule_create(void)
180185
{
181186
struct rt_dlmodule *module = RT_NULL;
@@ -233,6 +238,12 @@ void dlmodule_destroy_subthread(struct rt_dlmodule *module, rt_thread_t thread)
233238
#endif
234239
}
235240

241+
/**
242+
* @brief destroy dynamic module and cleanup all kernel objects inside it.
243+
*
244+
* @param module Pointer to the module to be destroyed.
245+
* @return rt_err_t On success, it returns RT_EOK. Otherwise, it returns the error code.
246+
*/
236247
rt_err_t dlmodule_destroy(struct rt_dlmodule* module)
237248
{
238249
int i;
@@ -255,7 +266,7 @@ rt_err_t dlmodule_destroy(struct rt_dlmodule* module)
255266
rt_exit_critical();
256267
}
257268

258-
// list_object(&(module->object_list));
269+
/* list_object(&(module->object_list));*/
259270

260271
/* cleanup for all kernel objects inside module*/
261272
{
@@ -393,6 +404,11 @@ rt_err_t dlmodule_destroy(struct rt_dlmodule* module)
393404
return RT_EOK;
394405
}
395406

407+
/**
408+
* @brief retrieve the dynamically loaded module that the current thread belongs to.
409+
*
410+
* @return struct rt_dlmodule* On success, it returns a pointer to the module. otherwise, it returns RT_NULL.
411+
*/
396412
struct rt_dlmodule *dlmodule_self(void)
397413
{
398414
rt_thread_t tid;
@@ -415,6 +431,20 @@ struct rt_dlmodule *rt_module_self(void)
415431
return dlmodule_self();
416432
}
417433

434+
/**
435+
* @brief load an ELF module to memory.
436+
*
437+
* @param filename the path to the module to load.
438+
* @return struct rt_dlmodule* On success, it returns a pointer to the module object. otherwise, RT_NULL is returned.
439+
*
440+
* @note the function is used to load an ELF (Executable and Linkable Format) module from a file, validate it,
441+
* and initialize it as a dynamically loaded module. what it implements are as follows:
442+
* 1. Load and Validate ELF: It loads an ELF file, checks its validity, and identifies it as either a relocatable or shared object.
443+
* 2. Memory Allocation and Cleanup: Uses rt_malloc and rt_free to allocate and free memory for module data.
444+
* Error handling ensures all resources are released if an error occurs.
445+
* 3. Symbol Resolution and Initialization: Sets up init function and cleanup function, and calls the module_init function if it is present.
446+
* 4. Cache Management: Optionally (when RT_USING_CACHE defined) flushes data and invalidates instruction caches to ensure the module is correctly loaded into memory.
447+
*/
418448
struct rt_dlmodule* dlmodule_load(const char* filename)
419449
{
420450
#ifdef RT_USING_POSIX_FS
@@ -525,6 +555,14 @@ struct rt_dlmodule* dlmodule_load(const char* filename)
525555
return RT_NULL;
526556
}
527557

558+
/**
559+
* @brief load a dynamic module, and create a thread to excute the module main function.
560+
*
561+
* @param pgname path of the module to be loaded.
562+
* @param cmd the command string (with commandline options) for startup module.
563+
* @param cmd_size the command's length.
564+
* @return struct rt_dlmodule* On success, it returns a pointer to the module object. otherwise, RT_NULL is returned.
565+
*/
528566
struct rt_dlmodule* dlmodule_exec(const char* pgname, const char* cmd, int cmd_size)
529567
{
530568
struct rt_dlmodule *module = RT_NULL;
@@ -742,6 +780,17 @@ struct rt_dlmodule* dlmodule_exec_custom(const char* pgname, const char* cmd, in
742780
}
743781
#endif
744782

783+
/**
784+
* @brief exit a dynamically loaded module.
785+
*
786+
* @param ret_code the return code for module exit.
787+
*
788+
* @note this function is responsible for gracefully exiting a dynamically loaded module, releasing resources associated with the module,
789+
* and handling cleanup operations. what it implements are as follows:
790+
* 1. Thread and Resource Cleanup: The function safely exits a module by deleting its main thread and freeing resources associated with it.
791+
* 2. Status Management: Checks and updates the module's state, setting a return code and calling _dlmodule_exit() to transition to a closing state.
792+
* 3. Critical Sections: Critical sections ensure that the exit process is atomic and free from race conditions.
793+
*/
745794
void dlmodule_exit(int ret_code)
746795
{
747796
rt_thread_t thread;
@@ -784,6 +833,13 @@ void dlmodule_exit(int ret_code)
784833
rt_exit_critical();
785834
}
786835

836+
/**
837+
* @brief search for a symbol by its name in the kernel symbol table.
838+
*
839+
* @param sym_str the symbol name string.
840+
* @return rt_uint32_t On success, it returns the address of the symbol.
841+
* Otherwise, it returns 0 (indicating the symbol was not found).
842+
*/
787843
rt_uint32_t dlmodule_symbol_find(const char *sym_str)
788844
{
789845
/* find in kernel symbol table */

components/libc/posix/libdl/dlmodule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*

0 commit comments

Comments
 (0)