A method and device for starting a dynamic link program
By constructing identification correspondence relationships to quickly calculate symbol address information, the problem of long-term startup of dynamic linker programs is solved, and the rapid startup under the randomized configuration of support address is achieved, avoiding the security and ease of use of the existing technology.
Patent Information
- Application Number
- CN201910905160.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-09-24
- Publication Date
- 2025-04-04
- Estimated Expiration
- 2039-09-24
AI Technical Summary
Dynamic linking programs take a long time to calculate symbolic addresses in shared libraries when starting. The existing technologies such as lazy binding and prelink have security and ease of use defects, and cannot effectively improve the startup speed when supporting randomized address configuration.
By obtaining the construction identifiers of executable programs and shared libraries, the corresponding relationship is cached to achieve rapid calculation of symbolic address information, avoiding finding address information, thereby improving the startup speed of dynamic link programs while supporting address randomization configuration.
It realizes the rapid start of dynamic link programs when supporting address randomization configuration, avoids the security and ease of use of the existing technology, and improves the startup speed.
Smart Images

Figure CN112559064B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to computer technology, in particular to a method and device for starting a dynamic link program. Background Art
[0002] In current general-purpose computer systems, shared libraries are widely used to save disk space. Executable programs that use shared libraries are called dynamic linkers. When a dynamic linker is started, it needs to go through a dynamic linking process before it can actually start executing. In the entire process of the dynamic linking process, the step of calculating the address of the symbol in the shared library is very time-consuming. For large programs that reference many shared libraries, this step is often the bottleneck of the startup process.
[0003] There are currently two main methods in the industry to solve this problem.
[0004] The first method (often referred to as lazy binding) is to reduce the amount of calculation at startup, and only calculate the address of the symbol when it is really needed. This method can effectively disperse the calculation of the address of the function symbol to the running process after the executable program is started, thereby accelerating the startup process. However, since the access to data symbols cannot be well intercepted at runtime, the calculation of the address of all data symbols cannot be calculated by this method. Due to the existence of the above defects, although the industry has widely used lazy binding technology, the acceleration optimization of the startup of dynamic linkers still needs to be further studied and improved.
[0005] The second method (often called prelink) is to pre-allocate the address space of each shared library, so that the addresses of all symbols can be pre-calculated before startup and written into the dynamic linker. This technology solves all speed-related problems more perfectly, but its security and ease of use have major flaws. First, prelink requires all addresses to be fixed, which runs counter to the industry's efforts to randomize addresses. Address randomization is an important basic security measure and there is no alternative in the foreseeable future. From the development trend, the application scope of prelink technology, which conflicts with address randomization in principle, will become narrower and narrower. Secondly, prelink technology requires an external mechanism to mark and arbitrate the addresses of shared libraries. This mechanism not only consumes a lot of CPU for address allocation and arbitration, thereby slowing down the execution speed of hardware; but also modifies shared libraries and executable programs in order to add relevant address tags. Since the executable program and shared library are modified, the checksum changes, but the actual content does not change, but the prelink-related information changes, thereby interfering with the normal operation of the program checksum mechanism. Due to the above defects, prelink technology has not been widely used. Summary of the invention
[0006] The embodiment of the present invention provides a method and device for starting a dynamic link program, which can improve the starting speed of the dynamic link program while supporting address randomization configuration.
[0007] An embodiment of the present invention provides a method for starting a dynamic link program, comprising:
[0008] Obtaining a first build identifier of an executable program;
[0009] For each shared library that the executable program depends on, after loading the shared library into the memory, putting the base address of the shared library into a buffer, and obtaining a second build identifier of the shared library;
[0010] After all the shared libraries that the executable program depends on are loaded into the memory, when there is a build identification sequence in the cached correspondence that is the same as the specific build identification sequence, the symbol corresponding to the specific build identification sequence, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library are searched from the cached correspondence; wherein the correspondence is a correspondence between the build identification sequence, the symbol, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library; the specific build identification sequence is composed of the obtained first build identification and the second build identifications of all shared libraries;
[0011] The address information of the symbol is calculated according to the base address of the shared library in the buffer, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library.
[0012] An embodiment of the present invention provides a device for starting a dynamic link program, including a processor and a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, and when the instructions are executed by the processor, any of the above methods for starting a dynamic link program is implemented.
[0013] An embodiment of the present invention provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of any one of the above methods for starting a dynamic link program are implemented.
[0014] An embodiment of the present invention provides a device for starting a dynamic link program, comprising:
[0015] A first acquisition module, used to acquire a first build identifier of an executable program;
[0016] A second acquisition module is used for, for each shared library that the executable program depends on, after loading the shared library into the memory, placing the base address of the shared library into a buffer, and acquiring a second build identifier of the shared library;
[0017] An address information acquisition module is used to, after all the shared libraries that the executable program depends on are loaded into the memory, when there is a build identification sequence in the build identification sequence in the cached correspondence relationship that is the same as the specific build identification sequence, search for the symbol corresponding to the specific build identification sequence, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library from the cached correspondence relationship; wherein the correspondence relationship is the correspondence between the build identification sequence, the symbol, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library; the specific build identification sequence is composed of the obtained first build identification and the second build identification of all shared libraries; and the address information of the symbol is calculated according to the base address of the shared library in the buffer, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library.
[0018] The embodiment of the present invention comprises: obtaining a first construction identification of an executable program; for each shared library that the executable program depends on, after loading the shared library into the memory, putting the base address of the shared library into the buffer, and obtaining the second construction identification of the shared library; after loading all the shared libraries that the executable program depends on into the memory, when there is a construction identification sequence that is the same as the specific construction identification sequence in the construction identification sequence in the cached correspondence, searching for the symbol corresponding to the specific construction identification sequence, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library from the cached correspondence; wherein the correspondence is the correspondence between the construction identification sequence, the symbol, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library; the specific construction identification sequence is composed of the obtained first construction identification and the second construction identification of all shared libraries; and calculating the address information of the symbol according to the base address of the shared library in the buffer, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library. The embodiment of the present invention realizes the rapid calculation of address information through the cached correspondence without the need to search for address information, and improves the startup speed of the dynamic linker in the case of supporting address randomization configuration.
[0019] Other features and advantages of the present invention will be described in the following description, and partly become apparent from the description, or understood by practicing the present invention. The purpose and other advantages of the present invention can be realized and obtained by the structures particularly pointed out in the description, claims and drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] The accompanying drawings are used to provide a further understanding of the technical solution of the present invention and constitute a part of the specification. Together with the embodiments of the present application, they are used to explain the technical solution of the present invention and do not constitute a limitation on the technical solution of the present invention.
[0021] Figure 1A flowchart of a conventional method for starting a dynamic linker;
[0022] Figure 2 A flowchart of a method for starting a dynamic link program proposed by an embodiment of the present invention;
[0023] Figure 3 A flowchart of a method for starting a dynamic link program according to Example 1 of the present invention;
[0024] Figure 4 A flowchart of a method for starting a dynamic link program according to Example 2 of the present invention;
[0025] Figure 5 A schematic diagram of the structure of a device for starting a dynamic link program according to another embodiment of the present invention. DETAILED DESCRIPTION
[0026] The embodiments of the present invention will be described in detail below in conjunction with the accompanying drawings. It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other arbitrarily.
[0027] The steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions. Also, although a logical sequence is shown in the flowchart, in some cases, the steps shown or described can be performed in a sequence different from that shown here.
[0028] See also Figure 1 , the traditional methods of starting a dynamic linker include:
[0029] Step 100: The dynamic loader obtains control and completes its own initialization.
[0030] Step 101: The dynamic loader parses input parameters to obtain the name of the executable program.
[0031] Step 102: The dynamic loader parses the executable program to obtain the shared library that the executable program depends on, loads the shared library that the executable program depends on into the memory, and caches the base address of the shared library into the memory.
[0032] Step 103, the dynamic loader searches for the shared library information to which each symbol of the executable program belongs and the offset of the symbol in the shared library; calculates the address information of each symbol according to the base address of the shared library to which each symbol belongs and the offset of each symbol in the shared library, and performs dynamic relocation according to the address information of the symbol.
[0033] Step 104: The dynamic loader calls all initialization functions of the executable program and transfers control to the executable program.
[0034] In the traditional method of starting a dynamic link program, the step of searching for the address of a symbol may take up as much as 30% to 50% of the time.
[0035] See also Figure 2 An embodiment of the present invention provides a method for starting a dynamic link program, comprising:
[0036] Step 200: Obtain a first build identifier of the executable program.
[0037] In the embodiment of the present invention, any of the following methods may be used to obtain the first build identifier of the executable program.
[0038] Method 1: when the first build identifier is added to the executable program when the executable program is built, the first build identifier exists in the executable program, and the first build identifier of the executable program can be directly obtained from the executable program; when the first build identifier is not added to the executable program when the executable program is built, the first build identifier does not exist in the executable program, and the warning information may be displayed or not; or method 2 is used to obtain the first build identifier.
[0039] In this method, when adding a first build identifier to the executable program when building the executable program, for example, a one-way hash algorithm (such as sha, md5, etc.) can be used to calculate the hash value of the executable program, and the calculated hash value can be used as the first build identifier; for example, a sufficiently long random string can be directly generated as the first build identifier; the obtained first build identifier can be placed in the executable program.
[0040] Method 2: When the first build identifier is not added to the executable program when the executable program is built, the first build identifier does not exist in the executable program. Then any of the following can be used as the first build identifier of the executable program:
[0041] The executable file name and the modification time of the executable;
[0042] The file name of the executable program and the file size of the executable program;
[0043] The executable file name, the time the executable was modified, and the executable file size.
[0044] Among them, the file name of the executable program is used to distinguish different executable programs, and the modification time and file size of the executable program are used to distinguish different versions of the same executable program. The reason for using the modification time and file size to distinguish different versions of the same executable program is that the modification time of the executable program is not so reliable. This is because there is a possibility that the content of the executable program is modified, but the modification time is not changed.
[0045] Step 201 : For each shared library that the executable program depends on, after loading the shared library into the memory, put the base address of the shared library into the buffer, and obtain the second build identifier of the shared library.
[0046] In the embodiment of the present invention, the buffer may be an array, a list, a queue, etc., and the specific form is not used to limit the protection scope of the embodiment of the present invention.
[0047] In the embodiment of the present invention, any of the following methods may be used to obtain the second build identifier of the shared library.
[0048] Method 1: when the second build identifier is added to the shared library when building the shared library, the second build identifier exists in the shared library, and the second build identifier of the shared library can be directly obtained from the shared library; when the second build identifier is not added to the shared library when building the shared library, the second build identifier does not exist in the shared library, and the warning information can be displayed or not; or the second build identifier can be obtained using method 2.
[0049] In this method, when adding a second build identifier to a shared library when building the shared library, for example, a one-way hash algorithm (such as sha, md5, etc.) can be used to calculate the hash value of the shared library, and the calculated hash value can be used as the second build identifier; for example, a sufficiently long random string can be directly generated as the second build identifier; and the obtained second build identifier can be placed in the shared library.
[0050] Method 2: When the second build identifier is not added to the shared library when building the shared library, the build identifier does not exist in the shared library. Then you can use any of the following as the second build identifier of the shared library:
[0051] The file name of the shared library and the modification time of the shared library;
[0052] The file name of the shared library and the file size of the shared library;
[0053] The file name of the shared library, the modification time of the shared library, and the file size of the shared library.
[0054] Among them, the file name of the shared library is used to distinguish different shared libraries, and the modification time and file size of the shared library are used to distinguish different versions of the same shared library. The reason for using the modification time and file size to distinguish different versions of the same shared library is that the modification time of the shared library is not so reliable. This is because there is a possibility that the content of the shared library is modified, but the modification time is not changed.
[0055] Step 202: After all the shared libraries that the executable program depends on are loaded into the memory, when there is a build identification sequence in the cached correspondence that is identical to the specific build identification sequence, search for the symbol corresponding to the specific build identification sequence, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library from the cached correspondence; wherein the correspondence is a correspondence between the build identification sequence, the symbol, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library; and the specific build identification sequence is composed of the obtained first build identification and the second build identification of all shared libraries.
[0056] In the embodiment of the present invention, when comparing whether two construction identification sequences are the same, it is necessary to compare whether the number of construction identifications contained in the two construction identification sequences is the same, and whether the construction identifications at the same position are the same. Only when the number of construction identifications contained is the same, and all the two construction identifications at the same position are the same, can the two construction identification sequences be determined to be the same. For example, construction identification sequence 1 includes: first construction identification 1, second construction identification 2, second construction identification 3 in sequence; construction identification sequence 2 includes: first construction identification 4, second construction identification 5, second construction identification 6 in sequence; then only when the first construction identification 1 is the same as the first construction identification 4, the second construction identification 2 is the same as the second construction identification 5, and the second construction identification 3 is the same as the second construction identification 6, can the construction identification sequence 1 and the construction identification sequence 2 be considered to be the same.
[0057] Step 203: Calculate the address information of the symbol according to the base address of the shared library in the buffer, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library.
[0058] In the embodiment of the present invention, the address information of the symbol is the base address of the shared library to which the symbol belongs + the offset of the symbol in the shared library.
[0059] In another embodiment of the present invention, when there is no build identification sequence identical to the specific build identification sequence in the build identification sequence in the cached corresponding relationship, the method further includes:
[0060] Find the shared library information to which each symbol of the executable program belongs and the offset of the symbol in the shared library; calculate the address information of each symbol according to the base address of the shared library to which each symbol belongs and the offset of each symbol in the shared library;
[0061] The correspondence between a specific build identification sequence, a symbol, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library is cached.
[0062] In the embodiment of the present invention, when the corresponding relationship is cached, the corresponding relationship can be cached in a memory, or the corresponding relationship can be cached in a disk file. When the corresponding relationship is cached in a disk file, the corresponding relationship can be permanently stored.
[0063] In another embodiment of the present invention, when the first build identifier of the executable program cannot be obtained, or the second build identifier of at least one shared library cannot be obtained, the method further includes:
[0064] Find the shared library information to which each symbol of the executable program belongs and the offset of the symbol in the shared library; calculate the address information of each symbol according to the base address of the shared library to which each symbol belongs and the offset of each symbol in the shared library.
[0065] In an embodiment of the present invention, step 200 can be executed before step 201 or after step 201. When step 200 is executed before step 201, the first build identifier in the build identifier sequence is located before the second build identifier; when step 200 is executed after step 201, the first build identifier in the build identifier sequence is located after the second build identifier; of course, the first build identifier can also be located at other positions in the build identifier sequence, and the specific position is not used to limit the protection scope of the embodiment of the present invention. The embodiment of the present invention emphasizes that when the order and content of each build identifier in the build identifier sequence are consistent, it means that the version of the executable program and the version of the shared library are the same.
[0066] The embodiment of the present invention realizes fast calculation of address information through the corresponding relationship of the cache without searching the address information, and improves the startup speed of the dynamic link program while supporting the address randomization configuration.
[0067] Example 1
[0068] This example describes a method for implementing a fast startup of a dynamic link program by adding a first build identifier to the executable program when building the executable program and adding a second build identifier to the shared library when building the shared library. Figure 3 , the method comprising:
[0069] Step 300: The dynamic loader obtains control and completes its own initialization.
[0070] Step 301: The dynamic loader parses input parameters to obtain the name of the executable program.
[0071] Step 302: The dynamic loader parses the executable program to obtain the shared library that the executable program depends on.
[0072] Step 303, the dynamic loader sets the "quick start" flag to true, sets the shared library loading number idx to 0, constructs a first array so_addr for storing the shared library loading base address, and constructs a second array buff for storing the construction identification sequence.
[0073] Step 304, the dynamic loader determines whether the executable program has a first build identifier. When the executable program does not have the first build identifier, an alarm is displayed (optional action), and the "quick start" identifier is set to false; when the executable program has a first build identifier, the first build identifier of the executable program is added to the end of the second array buff.
[0074] Step 305: For each shared library that the executable program depends on, after loading the shared library into the memory, the dynamic loader puts the base address of the shared library into so_addr[idx] and increases the idx value by 1.
[0075] Step 306, the dynamic loader determines whether the shared library has a second build identifier. When the shared library does not have the second build identifier, an alarm is displayed (optional action), and the "quick start" identifier is set to false; when the shared library has the second build identifier, the second build identifier of the shared library is added to the end of the second array buff.
[0076] Step 307: After the dynamic loader has loaded all shared libraries, check whether the "quick start" flag is true. If the "quick start" flag is false, execute steps 308 and 312; if the "quick start" flag is true, execute steps 309 to 312.
[0077] Step 308: The dynamic loader searches for address information of all symbols of all shared libraries that the executable program depends on, and performs dynamic relocation according to the address information of the found symbols.
[0078] Step 309, the dynamic loader determines whether there is a build identification sequence in the cached correspondence that is identical to the content in buff; when there is no build identification sequence in the cached correspondence that is identical to the content in buff, execute steps 310 and 312; when there is a build identification sequence in the cached correspondence that is identical to the content in buff, execute steps 311 and 312.
[0079] Step 310, the dynamic loader searches for the address information of all symbols of all shared libraries, and performs dynamic relocation based on the address information of the found symbols (including the base address of the shared library + the offset of the symbol in the shared library); the correspondence between the content in buff (i.e., building an identification sequence), the symbol, the shared library information to which the symbol belongs (i.e., idx) and the offset of the symbol in the shared library is cached in a disk file.
[0080] Step 311, the dynamic loader reads the symbol corresponding to the content in buff, the shared library information to which the symbol belongs (i.e., idx), and the offset of the symbol in the shared library from the cached disk file, and calculates the address information of the symbol through the so_addr[idx]+offset method, and performs dynamic relocation based on the address information of the symbol.
[0081] Step 312: The dynamic loader calls all initialization functions of the executable program and transfers control to the executable program.
[0082] In the embodiment of the present invention, steps 303 to 311 can be implemented by embedding them in a dynamic loader. This is because it is most efficient, less complex, and less labor-intensive. However, for the sake of functional isolation or copyright restrictions, steps 303 to 311 can be independently used as a module or program.
[0083] Example 2
[0084] In Example 1, in order to ensure the correctness after startup acceleration, it is necessary to add a first build identifier to the executable program when building the executable program, and to add a second build identifier to the shared library when building the shared library. However, for some legacy systems, or systems that use third-party suppliers (and therefore cannot be recompiled and linked), this constraint may not be met.
[0085] This example uses the file name plus modification time, or the file name plus file size, or the file name, modification time and file size as the build identifier to remove the dependency on the build process. Figure 4 , the method comprising:
[0086] Step 400: The dynamic loader obtains control and completes its own initialization.
[0087] Step 401: The dynamic loader parses input parameters to obtain the name of the executable program.
[0088] Step 402: The dynamic loader parses the executable program to obtain the shared library that the executable program depends on.
[0089] Step 403: The dynamic loader sets the shared library loading sequence number idx to 0, constructs a first array so_addr for storing the shared library loading base address, and constructs a second array buff for storing the construction identification sequence.
[0090] Step 404, the dynamic loader determines whether the executable program has a first build identifier. When the executable program does not have the first build identifier, the dynamic loader reads the modification time and file size of the executable program, uses the file name, modification time and file size as the first build identifier, and adds the first build identifier to the end of the second array buff; when the executable program has the first build identifier, the first build identifier of the executable program is added to the end of the second array buff.
[0091] Step 405: For each shared library that the executable program depends on, after loading the shared library into the memory, the dynamic loader puts the base address of the shared library into so_addr[idx] and increases the idx value by 1.
[0092] Step 406, the dynamic loader determines whether the shared library has a second build identifier. When the shared library does not have the second build identifier, the dynamic loader reads the modification time and file size of the shared library, uses the file name, modification time and file size as the second build identifier, and adds the second build identifier to the end of the second array buff; when the shared library has the second build identifier, the second build identifier of the shared library is added to the end of the second array buff.
[0093] Step 407: After the dynamic loader has loaded all shared libraries, it is determined whether there is a build identification sequence in the cached correspondence that is identical to the content in buff; when there is no build identification sequence in the cached correspondence that is identical to the content in buff, execute steps 408 and 410; when there is a build identification sequence in the cached correspondence that is identical to the content in buff, execute steps 409 and 410.
[0094] Step 408, the dynamic loader searches for the address information of all symbols of all shared libraries, and performs dynamic relocation based on the address information of the found symbols (including the base address of the shared library + the offset of the symbol in the shared library); the correspondence between the content in buff (i.e., the construction identification sequence), the symbol, the shared library information to which the symbol belongs (i.e., idx) and the offset of the symbol in the shared library is cached in the disk file.
[0095] Step 409, the dynamic loader reads the symbol corresponding to the content in buff, the shared library information to which the symbol belongs (i.e., idx), and the offset of the symbol in the shared library from the cached disk file, and calculates the address information of the symbol through the so_addr[idx]+offset method, and performs dynamic relocation based on the address information of the symbol.
[0096] Step 410: The dynamic loader calls all initialization functions of the executable program and transfers control to the executable program to be loaded.
[0097] Since the identifier can be dynamically constructed according to the file name, modification time and file size, this example Figure 4 When the implementation is shown, the fast start can always be successfully performed. The disadvantage of this implementation is that the modification time is relatively unreliable, because there is a possibility that the content of the shared library is modified, but the modification time is not changed. Considering that the build identifier also includes the file size, for binary files such as shared libraries, the possibility that the build identifier remains the same after the shared library is changed is very low.
[0098] Another embodiment of the present invention provides a device for starting a dynamic link program, including a processor and a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, and when the instructions are executed by the processor, any of the above methods for starting a dynamic link program is implemented.
[0099] Another embodiment of the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program implements the steps of any of the above-mentioned methods for starting a dynamic link program when executed by a processor.
[0100] See also Figure 5 Another embodiment of the present invention provides a device for starting a dynamic link program, comprising:
[0101] A first acquisition module 501 is used to acquire a first build identifier of an executable program;
[0102] A second acquisition module 502 is used for, for each shared library that the executable program depends on, after loading the shared library into the memory, putting the base address of the shared library into a buffer, and acquiring a second build identifier of the shared library;
[0103] The address information acquisition module 503 is used for, after all the shared libraries that the executable program depends on are loaded into the memory, when there is a build identification sequence in the cached correspondence that is the same as the specific build identification sequence, searching for the symbol corresponding to the specific build identification sequence, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library from the cached correspondence; wherein the correspondence is the correspondence between the build identification sequence, the symbol, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library; the specific build identification sequence is composed of the obtained first build identification and the second build identification of all shared libraries; and the address information of the symbol is calculated according to the base address of the shared library in the buffer, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library.
[0104] In the embodiment of the present invention, the first acquisition module 501 may adopt any of the following methods to acquire the first build identifier of the executable program.
[0105] Method 1: when the first build identifier is added to the executable program when the executable program is built, the first build identifier exists in the executable program, and the first build identifier of the executable program can be directly obtained from the executable program; when the first build identifier is not added to the executable program when the executable program is built, the first build identifier does not exist in the executable program, and the warning information may be displayed or not; or method 2 is used to obtain the first build identifier.
[0106] In this method, when adding a first build identifier to the executable program when building the executable program, for example, a one-way hash algorithm (such as sha, md5, etc.) can be used to calculate the hash value of the executable program, and the calculated hash value can be used as the first build identifier; for example, a sufficiently long random string can be directly generated as the first build identifier; the obtained first build identifier can be placed in the executable program.
[0107] Method 2: When the first build identifier is not added to the executable program when the executable program is built, the first build identifier does not exist in the executable program. Then any of the following can be used as the first build identifier of the executable program:
[0108] The executable file name and the modification time of the executable;
[0109] The file name of the executable program and the file size of the executable program;
[0110] The executable file name, the time the executable was modified, and the executable file size.
[0111] Among them, the file name of the executable program is used to distinguish different executable programs, and the modification time and file size of the executable program are used to distinguish different versions of the same executable program. The reason for using the modification time and file size to distinguish different versions of the same executable program is that the modification time of the executable program is not so reliable. This is because there is a possibility that the content of the executable program is modified, but the modification time is not changed.
[0112] In the embodiment of the present invention, the buffer may be an array, a list, a queue, etc., and the specific form is not used to limit the protection scope of the embodiment of the present invention.
[0113] In the embodiment of the present invention, the second acquisition module 502 may adopt any of the following methods to acquire the second build identifier of the shared library.
[0114] Method 1: when the second build identifier is added to the shared library when building the shared library, the second build identifier exists in the shared library, and the second build identifier of the shared library can be directly obtained from the shared library; when the second build identifier is not added to the shared library when building the shared library, the second build identifier does not exist in the shared library, and the warning information can be displayed or not; or the second build identifier can be obtained using method 2.
[0115] In this method, when adding a second build identifier to a shared library when building the shared library, for example, a one-way hash algorithm (such as sha, md5, etc.) can be used to calculate the hash value of the shared library, and the calculated hash value can be used as the second build identifier; for example, a sufficiently long random string can be directly generated as the second build identifier; and the obtained second build identifier can be placed in the shared library.
[0116] Method 2: When the second build identifier is not added to the shared library when building the shared library, the build identifier does not exist in the shared library. Then you can use any of the following as the second build identifier of the shared library:
[0117] The file name of the shared library and the modification time of the shared library;
[0118] The file name of the shared library and the file size of the shared library;
[0119] The file name of the shared library, the modification time of the shared library, and the file size of the shared library.
[0120] Among them, the file name of the shared library is used to distinguish different shared libraries, and the modification time and file size of the shared library are used to distinguish different versions of the same shared library. The reason for using the modification time and file size to distinguish different versions of the same shared library is that the modification time of the shared library is not so reliable. This is because there is a possibility that the content of the shared library is modified, but the modification time is not changed.
[0121] In the embodiment of the present invention, the address information of the symbol is the base address of the shared library to which the symbol belongs + the offset of the symbol in the shared library.
[0122] In the embodiment of the present invention, when comparing whether two construction identification sequences are the same, it is necessary to compare whether the number of construction identifications contained in the two construction identification sequences is the same, and whether the construction identifications at the same position are the same. Only when the number of construction identifications contained is the same, and all the two construction identifications at the same position are the same, can the two construction identification sequences be determined to be the same. For example, construction identification sequence 1 includes: first construction identification 1, second construction identification 2, second construction identification 3 in sequence; construction identification sequence 2 includes: first construction identification 4, second construction identification 5, second construction identification 6 in sequence; then only when the first construction identification 1 is the same as the first construction identification 4, the second construction identification 2 is the same as the second construction identification 5, and the second construction identification 3 is the same as the second construction identification 6, can the construction identification sequence 1 and the construction identification sequence 2 be considered to be the same.
[0123] In another embodiment of the present invention, the address information acquisition module 503 is further used to:
[0124] When there is no build identification sequence identical to the specific build identification sequence in the build identification sequence in the cached correspondence, searching for the shared library information to which each symbol of the executable program belongs and the offset of the symbol in the shared library; calculating the address information of each symbol according to the base address of the shared library to which each symbol belongs and the offset of each symbol in the shared library;
[0125] The correspondence between a specific build identification sequence, a symbol, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library is cached.
[0126] In the embodiment of the present invention, when caching the corresponding relationship, the address information acquisition module 503 may cache the corresponding relationship in a memory or in a disk file. When the corresponding relationship is cached in a disk file, the corresponding relationship can be permanently stored.
[0127] In another embodiment of the present invention, the address information acquisition module 503 is further used to:
[0128] When the first build identifier of the executable program cannot be obtained, or the second build identifier of at least one shared library cannot be obtained, the shared library information to which each symbol of the executable program belongs and the offset of the symbol in the shared library are searched; the address information of each symbol is calculated according to the base address of the shared library to which each symbol belongs and the offset of each symbol in the shared library.
[0129] In an embodiment of the present invention, step 200 can be executed before step 201 or after step 201. When step 200 is executed before step 201, the first build identifier in the build identifier sequence is located before the second build identifier; when step 200 is executed after step 201, the first build identifier in the build identifier sequence is located after the second build identifier; of course, the first build identifier can also be located at other positions in the build identifier sequence, and the specific position is not used to limit the protection scope of the embodiment of the present invention. The embodiment of the present invention emphasizes that when the order and content of each build identifier in the build identifier sequence are consistent, it means that the version of the executable program and the version of the shared library are the same.
[0130] The embodiment of the present invention realizes fast calculation of address information through the corresponding relationship of the cache without searching the address information, and improves the startup speed of the dynamic link program while supporting the address randomization configuration.
[0131] Although the embodiments disclosed in the present invention are as above, the contents described are only embodiments adopted to facilitate understanding of the present invention and are not intended to limit the present invention. Any technician in the field to which the present invention belongs can make any modifications and changes in the form and details of implementation without departing from the spirit and scope disclosed in the present invention, but the patent protection scope of the present invention shall still be subject to the scope defined in the attached claims.
Claims
1. A method for starting a dynamic linker, comprising: Obtaining a first build identifier of an executable program; For each shared library that the executable program depends on, after loading the shared library into the memory, putting the base address of the shared library into a buffer, and obtaining a second build identifier of the shared library; After all the shared libraries that the executable program depends on are loaded into the memory, when there is a build identification sequence in the cached correspondence that is the same as the specific build identification sequence, the symbol corresponding to the specific build identification sequence, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library are searched from the cached correspondence; wherein the correspondence is a correspondence between the build identification sequence, the symbol, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library; the specific build identification sequence is composed of the obtained first build identification and the second build identifications of all shared libraries; Calculating the address information of the symbol according to the base address of the shared library in the buffer, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library; When there is no construction identification sequence identical to the specific construction identification sequence in the construction identification sequence in the cached corresponding relationship, the method further includes: Find the shared library information to which each symbol of the executable program belongs and the offset of the symbol in the shared library; calculate the address information of each symbol according to the base address of the shared library to which each symbol belongs and the offset of each symbol in the shared library; The correspondence between the specific build identification sequence, the symbol, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library is cached.
2. The method according to claim 1, characterized in that in, The caching of the corresponding relationship includes: The corresponding relationship is cached in a memory, or the corresponding relationship is cached in a disk file.
3. The method according to claim 1, characterized in that When the first build identifier of the executable program cannot be obtained, or the second build identifier of at least one of the shared libraries cannot be obtained, the method further includes: Find the shared library information to which each symbol of the executable program belongs and the offset of the symbol in the shared library; calculate the address information of each symbol according to the base address of the shared library to which each symbol belongs and the offset of each symbol in the shared library.
4. The method according to any one of claims 1 to 3, characterized in that: in, The method of obtaining the first build identifier of the executable program includes any one or more of the following: When the first build identifier exists in the executable program, obtaining the first build identifier from the executable program; When the first build identifier does not exist in the executable program, any one of the following is used as the first build identifier: The file name of the executable program and the modification time of the executable program; The file name of the executable program and the file size of the executable program; The file name of the executable program, the modification time of the executable program, and the file size of the executable program.
5. The method according to any one of claims 1 to 3, characterized in that: in, The second building identifier of the shared library may be obtained by any one or more of the following: When the second build identifier exists in the shared library, obtaining the second build identifier from the shared library; When the second build identifier does not exist in the shared library, any one of the following is used as the second build identifier: The file name of the shared library and the modification time of the shared library; The file name of the shared library and the file size of the shared library; The file name of the shared library, the modification time of the shared library, and the file size of the shared library.
6. A device for starting a dynamic link program, comprising a processor and a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, characterized in that: When the instruction is executed by the processor, the method for starting a dynamic link program according to any one of claims 1 to 5 is implemented.
7. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method for starting a dynamic link program as claimed in any one of claims 1 to 5 are implemented.
8. A device for starting a dynamic link program, comprising: A first acquisition module, used to acquire a first build identifier of an executable program; A second acquisition module is used for, for each shared library that the executable program depends on, after loading the shared library into the memory, placing the base address of the shared library into a buffer, and acquiring a second build identifier of the shared library; An address information acquisition module is used for, after all the shared libraries that the executable program depends on are loaded into the memory, when there is a build identification sequence in the cached correspondence that is the same as the specific build identification sequence, searching for the symbol corresponding to the specific build identification sequence, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library from the cached correspondence; wherein the correspondence is the correspondence between the build identification sequence, the symbol, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library; the specific build identification sequence is composed of the obtained first build identification and the second build identification of all shared libraries; the address information of the symbol is calculated according to the base address of the shared library in the buffer, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library; The address information acquisition module is further used to search for the shared library information to which each symbol of the executable program belongs and the offset of the symbol in the shared library when there is no build identification sequence identical to the specific build identification sequence in the build identification sequence in the corresponding relationship of the cache; and calculate the address information of each symbol according to the base address of the shared library to which each symbol belongs and the offset of each symbol in the shared library; The correspondence between the specific build identification sequence, the symbol, the shared library information to which the symbol belongs, and the offset of the symbol in the shared library is cached.
Citation Information
Patent Citations
Dynamic loading method of program module
CN107391191A