You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: components/libc/posix/libdl/dlelf.c
+43-2Lines changed: 43 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/*
2
-
* Copyright (c) 2006-2021, RT-Thread Development Team
2
+
* Copyright (c) 2006-2024 RT-Thread Development Team
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
@@ -14,8 +14,32 @@
14
14
15
15
#defineDBG_TAG "DLMD"
16
16
#defineDBG_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*/
18
18
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.
* @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.
0 commit comments