US-20260127010-A1 - DYNAMIC LOADING METHOD AND ELECTRONIC DEVICE
Abstract
A dynamic loading method and an electronic device are disclosed. The method includes: parsing one or more target files, and storing one or more first parsing results, where the one or more first parsing results are in a one-to-one correspondence with the one or more target files, the one or more target files include an executable file or a dynamic library file; searching the one or more first parsing results for a first parsing result of a dynamic library file on which a first executable file depends; and mapping, based on the first parsing result of the dynamic library file, the dynamic library file into a process address space in which the first executable file is located.
Inventors
- Jingqiang Zhou
- Liming Zheng
- Dongxu Liu
- Xin Chen
- Jubin ZHONG
- Xu Wang
- Yuxin Ren
Assignees
- HUAWEI TECHNOLOGIES CO., LTD.
Dates
- Publication Date
- 20260507
- Application Date
- 20251229
- Priority Date
- 20230630
Claims (20)
- 1 . A dynamic loading method, comprising: parsing one or more target files, and storing one or more first parsing results, wherein the one or more first parsing results are in a one-to-one correspondence with the one or more target files, the one or more target files comprise an executable file or a dynamic library file; searching the one or more first parsing results for a first parsing result of a dynamic library file on which a first executable file depends; and mapping, based on the first parsing result of the dynamic library file, the dynamic library file into a process address space in which the first executable file is located.
- 2 . The method according to claim 1 , wherein the one or more target files comprise a first dynamic library file, and the first parsing result comprises a path, a mapping range, and dependency information of the first dynamic library file.
- 3 . The method according to claim 2 , wherein the dynamic library file comprises the first dynamic library file; and mapping, based on the first parsing result of the dynamic library file, the dynamic library file into the process address space in which the first executable file is located comprises: obtaining the first dynamic library file based on the path of the first dynamic library file; and mapping the first dynamic library file into the process address space based on the mapping range of the first dynamic library file.
- 4 . The method according to claim 1 , wherein the first executable file or the dynamic library file is a file in a to-be-parsed file list that needs to be parsed before the first executable file is loaded into the process address space.
- 5 . The method according to claim 1 , further comprising: searching the one or more first parsing results for first parsing results of a second dynamic library file and a dynamic library file on which the second dynamic library file depends, wherein the second dynamic library file is a specified dynamic library file that a process in which the first executable file is located requests to load; and mapping, based on the first parsing results of the second dynamic library file and the dynamic library file on which the second dynamic library file depends, the second dynamic library file and the dynamic library file on which the second dynamic library file depends into the process address space.
- 6 . The method according to claim 5 , further comprising: when the first parsing result of the dynamic library file on which the first executable file depends is not found, parsing the first executable file, and storing a first parsing result of the first executable file and a first parsing result of the dynamic library file on which the first executable file depends; and/or when the first parsing result of the second dynamic library file is not found, parsing the second dynamic library file, and storing a first parsing result of the second dynamic library file and a first parsing result of the dynamic library file on which the second dynamic library file depends.
- 7 . The method according to claim 1 , wherein parsing the one or more target files, and storing the one or more first parsing results comprise: parsing, by a plurality of different threads, the one or more target files in parallel, and storing the one or more first parsing results.
- 8 . The method according to claim 1 , wherein the dynamic library file comprises a third dynamic library file and a fourth dynamic library file; and mapping, based on the first parsing result of the dynamic library file, the dynamic library file into the process address space in which the first executable file is located comprises: mapping, by a first thread, based on a first parsing result of the third dynamic library file, the third dynamic library file into the process address space; and mapping, by a second thread, the fourth dynamic library file into the process address space based on a first parsing result of the fourth dynamic library file, wherein the first thread is different from the second thread.
- 9 . The method according to claim 1 , further comprising: parsing the one or more target files, and storing information about one or more symbols, wherein the one or more symbols are symbols defined in the one or more target files, the one or more symbols comprise a first symbol, and information about the first symbol comprises a name of a target file to which the first symbol belongs and location information of the first symbol in the target file to which the first symbol belongs.
- 10 . The method according to claim 9 , wherein the dynamic library file comprises a fifth dynamic library file, a relocation table of the fifth dynamic library file comprises a second symbol, and the method further comprises: after the dynamic library file is mapped into the process address space, searching the information about the one or more symbols for information about the second symbol for relocating the second symbol.
- 11 . The method according to claim 10 , further comprising: obtaining an address of the second symbol in the process address space based on the information about the second symbol; and relocating the second symbol based on the address of the second symbol in the process address space.
- 12 . The method according to claim 9 , further comprising: when the first executable file and the dynamic library file are not in a parsed file list, parsing the first executable file and the dynamic library file, and storing information about symbols defined in the first executable file and the dynamic library file, wherein the parsed file list is generated for storing a name of a parsed executable file or dynamic library file.
- 13 . The method according to claim 9 , wherein parsing the one or more target files, and storing the information about the one or more symbols comprise: parsing, by a plurality of different threads, the one or more target files in parallel, and storing the information about the one or more symbols.
- 14 . A dynamic loading method, comprising: parsing one or more target files, and storing information about one or more symbols, wherein the one or more symbols are defined in the one or more target files, the one or more symbols comprise a first symbol, information about the first symbol comprises a name of a target file to which the first symbol belongs and location information of the first symbol in the target file to which the first symbol belongs, the one or more target files comprise an executable file or a dynamic library file; and searching the information about the one or more symbols for information about a second symbol for relocating the second symbol, wherein the second symbol is a symbol in a relocation table of a dynamic library file on which a first executable file depends.
- 15 . The method according to claim 14 , further comprising: obtaining, based on the information about the second symbol, an address of the second symbol in a process address space in which the first executable file is located; and relocating the second symbol based on the address of the second symbol in the process address space.
- 16 . The method according to claim 15 , wherein the first executable file or the dynamic library file is a file in a to-be-parsed file list that needs to be parsed before the first executable file is loaded into the process address space.
- 17 . The method according to claim 14 , further comprising: when the first executable file or the dynamic library file is not in a parsed file list, parsing the first executable file and the dynamic library file, and storing information about symbols defined in the first executable file and the dynamic library file, wherein the parsed file list is used to store a name of a parsed executable file or dynamic library file.
- 18 . The method according to claim 14 , wherein parsing the one or more target files, and storing the information about the one or more symbols comprise: parsing, by a plurality of different threads, the one or more target files in parallel, and storing the information about the one or more symbols.
- 19 . An electronic device, comprising one or more processors and one or more memories storing one or more computer programs including instructions that, when executed by the one or more processors, cause the electronic device to: parse one or more target files, and store one or more first parsing results, wherein the one or more first parsing results are in a one-to-one correspondence with the one or more target files, the one or more target files comprise an executable file or a dynamic library file; search the one or more first parsing results for a first parsing result of a dynamic library file on which a first executable file depends; and map, based on the first parsing result of the dynamic library file, the dynamic library file into a process address space in which the first executable file is located.
- 20 . The electronic device according to claim 19 , wherein the one or more target files comprise a first dynamic library file, and the first parsing result comprises a path, a mapping range, and dependency information of the first dynamic library file.
Description
CROSS-REFERENCE TO RELATED APPLICATIONS This application is a continuation of International Application No. PCT/CN2024/079257, filed on Feb. 29, 2024, which claims priority to Chinese Patent Application No. 202310808428.8, filed on Jun. 30, 2023. The disclosures of the aforementioned applications are hereby incorporated by reference in their entireties. TECHNICAL FIELD This application relates to the computer field, and more specifically, to a dynamic loading method and an electronic device. BACKGROUND The dynamic loading technology means that one or more library files are dynamically loaded to an internal memory during running of a program, and functions and variables in the library files are mapped into a process address space of the program, for the program to invoke. The dynamic loading technology can cause the program to load library files as required during running, instead of linking all library files to the program during compilation, thereby saving internal memory space and improving flexibility of an application. However, dynamic library loading of different applications is independent of each other. Each time a new application is started, complete dynamic loading may need to be performed, a startup speed is low, and a lot of redundant calculation is caused. SUMMARY This application provides a dynamic loading method and an electronic device, to implement sharing of one or more parsing results. This helps optimize a startup speed of an application, save computing resources, save internal memory space, and improve performance of an electronic device, thereby improving user experience. According to a first aspect, a dynamic loading method is provided, and the method includes: parsing one or more target files, and storing one or more first parsing results, where the one or more first parsing results are in a one-to-one correspondence with the one or more target files, the one or more target files include a first target file, and the first target file is an executable file or a dynamic library file; searching the one or more first parsing results for a first parsing result of a dynamic library file on which a first executable file depends; and mapping, based on the first parsing result of the dynamic library file on which the first executable file depends, the dynamic library file on which the first executable file depends into a process address space in which the first executable file is located. It should be understood that the one or more target files may include the first target file, and the first target file may be the executable file or the dynamic library file. It should be understood that there may be a dependency relationship or no dependency relationship between the one or more target files. This is not limited in embodiments of this application. It should be noted that the first executable file may be, for example, an executable file that is of an application and that is loaded in a user process, and the process address space is internal memory space allocated by a system to the user process. It should be noted that the first parsing result may be, for example, a format parsing result in an embodiment shown in FIG. 4, and the one or more first parsing results may be, for example, stored in a form of global target file list in the embodiment shown in FIG. 4. In this embodiment, when the target files are parsed, the first parsing results of the target files may be stored. These first parsing results are used in loading processes of dynamic libraries, to implement decoupling between parsing processes and loading processes of the target files, thereby helping optimize a startup speed of an application and improving user experience. In another aspect, a first parsing result of one target file may be used in loading processes of a plurality of different executable files, to implement sharing of the one or more first parsing results. This helps save computing resources and internal memory space. In addition, the user may perform an operation such as adding, deleting, or modifying on the one or more stored first parsing results, thereby improving flexibility of dynamic loading. With reference to the first aspect, in some embodiments of the first aspect, the one or more target files include a first dynamic library file, and the first parsing result includes a path, a mapping range, and dependency information of the first dynamic library file. It should be understood that the first parsing result of the first dynamic library file may include the name, the path, the mapping range, and the dependency information of the first dynamic library file. The path of the first dynamic library file may indicate a location at which the first dynamic library file is stored in a disk. The dependency information of the first dynamic library file may indicate a dynamic library file on which the first dynamic library file depends, including a dynamic library file on which the first dynamic library file directly depend