A code relocation method and apparatus
By obtaining the list of loading commands and relocation information of the target file, the address calculation method and parameters of the target code are determined, solving the problem that the address parameters of the target code are difficult to determine before static linking, and realizing the reverse analysis of the target code.
Patent Information
- Application Number
- CN202111679140.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-31
- Publication Date
- 2026-02-10
- Estimated Expiration
- 2042-02-10
AI Technical Summary
Existing technologies make it difficult to determine the address parameters of the target code before static linking, which cannot meet the needs of reverse analysis.
By obtaining the list of loading commands for the target file, searching the list of relocation information, determining the target address calculation method and address parameters of the target code, and using the target address calculation method to calculate, the relocated address information is obtained.
Without static linking, the address information of the target code can be determined, supporting reverse analysis of the target file.
Smart Images

Figure CN114296794B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the technical field of computer, and in particular, to a code relocation method and device. BACKGROUND
[0002] In the development process of an application, codes are usually placed in multiple source files, and the source files are not independent and have various dependency relationships, such as one source file may call a function defined in another source file, but each source file is independently compiled. Therefore, the object files generated by compiling the source files need to be linked to form an executable program, and this linking process is static linking.
[0003] Static linking usually needs to link the same areas in each compiled module together to form an executable file. Since the object files in the static library before static linking are not an executable unit, the address parameters of the target code in the object files are not calculated before static linking is completed, so it is difficult to determine the address parameters of the target code before static linking, and the demand for reverse analysis of the object files before static linking cannot be met. SUMMARY
[0004] The present disclosure provides at least a code relocation method and device.
[0005] In a first aspect, the present disclosure provides a code relocation method, comprising:
[0006] obtaining an object file to be relocated, and reading a loading command list corresponding to target code from the object file; wherein the object file is used to generate an executable file through static linking;
[0007] based on the loading command list, finding a relocation information list corresponding to the target code from the object file;
[0008] based on the relocation information list, determining a target address calculation method and address parameters of the target code;
[0009] calculating the address parameters by using the target address calculation method to obtain address information of the target code after relocation.
[0010] In an optional implementation, the finding of the relocation information list corresponding to the target code from the object file based on the loading command list comprises:
[0011] finding data structure information and address offset information of the relocation information list from the loading command list;
[0012] Based on the data structure information and address offset information of the relocation information list, the target file is searched to find the relocation information list.
[0013] In an optional implementation, based on the relocation information list, the target address calculation manner and the address parameter of the target code are determined, including:
[0014] Based on the pointer type in the relocation information list, the target address calculation manner of the target code is filtered from a plurality of address calculation manners.
[0015] Based on the target data indicated by the target address calculation manner, the address parameter is extracted from the relocation information list.
[0016] In an optional implementation, the method further includes:
[0017] Using the address information of the target code after relocation, the initial address information in the target file is updated to obtain a target file after relocation.
[0018] In an optional implementation, the method further includes:
[0019] Using the target file after relocation, attribute information of a class and a method contained in the target file after relocation is determined.
[0020] Using the attribute information, an analysis report of the target file is generated.
[0021] In an optional implementation, the target file is an MH_OBJECT file.
[0022] In a second aspect, the embodiments of the present disclosure further provide a code relocation device, including:
[0023] An acquisition module is configured to acquire a target file to be relocated, and read a loading command list corresponding to target code from the target file, wherein the target file is used to generate an executable file through static linking.
[0024] A search module is configured to search the target file to find a relocation information list corresponding to the target code based on the loading command list.
[0025] A determination module is configured to determine a target address calculation manner and an address parameter of the target code based on the relocation information list.
[0026] A positioning module is configured to calculate the address parameter by using the target address calculation manner to obtain address information of the target code after relocation.
[0027] In an alternative implementation, the searching module is specifically configured to:
[0028] search, from the loading command list, data structure information and address offset information of the relocation information list;
[0029] search, from the target file, the relocation information list based on the data structure information and the address offset information of the relocation information list.
[0030] In an alternative implementation, the determining module is specifically configured to:
[0031] filter, based on the pointer type in the relocation information list, a target address calculation manner of the target code from a plurality of address calculation manners;
[0032] extract, based on target data indicated by the target address calculation manner, the address parameter from the relocation information list.
[0033] In an alternative implementation, the apparatus further includes an updating module configured to:
[0034] update, based on address information of the target code after relocation, initial address information in the target file to obtain a target file after relocation.
[0035] In an alternative implementation, the apparatus further includes an analyzing module configured to:
[0036] determine, based on the target file after relocation, attribute information of classes and methods contained in the target file after relocation;
[0037] generate, based on the attribute information, an analysis report of the target file.
[0038] In an alternative implementation, the target file is an MH_OBJECT file.
[0039] In a third aspect, the embodiments of the present disclosure further provide an electronic device, including a processor, a memory and a bus, the memory stores machine readable instructions executable by the processor, when the electronic device is running, the processor and the memory communicate through the bus, and the machine readable instructions are executed by the processor to perform the steps of the first aspect or any possible implementation manner of the first aspect.
[0040] In a fourth aspect, the embodiments of the present disclosure further provide a computer readable storage medium, the computer readable storage medium stores a computer program, and the computer program is executed by a processor to perform the steps of the first aspect or any possible implementation manner of the first aspect.
[0041] The code relocation method and device provided by the embodiments of the present disclosure first acquire a target file to be relocated, and read a list of loading commands corresponding to target code from the target file; wherein the target file is used to generate an executable file through static linking; then, based on the list of loading commands, a list of relocation information corresponding to the target code is found from the target file; thereafter, based on the list of relocation information, a target address calculation method and an address parameter of the target code are determined; finally, the address parameter is calculated by using the target address calculation method, and address information of the target code after relocation is obtained. The embodiments of the present disclosure determine the target address calculation method and the address parameter of the target code through the list of relocation information, and then calculate the address parameter by using the target address calculation method to obtain the address information of the target code after relocation, without static linking, so as to determine the address information of the target code.
[0042] In order to make the above objectives, features and advantages of the present disclosure more apparent, clear and easy to understand, the following will specifically describe a preferred embodiment in conjunction with the accompanying drawings, and make a detailed description as follows. BRIEF DESCRIPTION OF DRAWINGS
[0043] In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the following will briefly introduce the drawings needed to be used in the embodiments, the drawings herein are incorporated into the description and form a part of the description, which show the embodiments consistent with the present disclosure, and are used to illustrate the technical solutions of the present disclosure together with the description. It should be understood that the following drawings only show some embodiments of the present disclosure, and therefore should not be regarded as a limitation to the scope, and for those skilled in the art, other related drawings can also be obtained without creative labor.
[0044] Figure 1 A flow chart of a code relocation method provided by the embodiments of the present disclosure is shown;
[0045] Figure 2 A flow chart of another code relocation method provided by the embodiments of the present disclosure is shown;
[0046] Figure 3 A schematic diagram of a code relocation device provided by the embodiments of the present disclosure is shown;
[0047] Figure 4 A schematic diagram of an electronic device provided by the embodiments of the present disclosure is shown. DETAILED DESCRIPTION
[0048] To make the objects, technical solutions and advantages of the embodiments of the present disclosure clearer, the following will be combined with the accompanying drawings of the embodiments of the present disclosure to make a clear and complete description of the technical solutions in the embodiments of the present disclosure. Obviously, the described embodiments are only a part of the embodiments of the present disclosure, rather than all the embodiments. The components of the embodiments of the present disclosure generally described and shown in the accompanying drawings can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present disclosure provided in the accompanying drawings is not intended to limit the scope of the claimed present disclosure, but only represents selected embodiments of the present disclosure. Based on the embodiments of the present disclosure, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present disclosure.
[0049] It should be noted that similar reference numerals and letters refer to similar items throughout the accompanying drawings, and therefore, once an item is defined in one drawing, it need not be further defined and explained in subsequent drawings.
[0050] The term "and / or" herein only describes an associated relationship, which means that there can be three relationships, for example, A and / or B can mean that A exists alone, A and B exist together, and B exists alone. In addition, the term "at least one" herein means any one of a plurality or any combination of at least two of a plurality, for example, including at least one of A, B, and C can mean including any one or more elements selected from the set consisting of A, B, and C.
[0051] It is found through research that the existing code address relocation needs to be performed after the static linking operation is completed. If the static linking operation is not performed, it is difficult to determine the address information of the target file assembled from each source file, which is not conducive to the reverse analysis of the target file.
[0052] Based on the above research, the present disclosure provides a code relocation method, which determines the target address calculation method and address parameters of the target code through a relocation information list, and then calculates the address parameters by using the target address calculation method to obtain the address information of the target code after relocation. The address information of the target code can be determined without static linking.
[0053] In order to facilitate the understanding of the present embodiment, first, a code relocation method disclosed by the present embodiment will be described in detail. The execution subject of the code relocation method provided by the present embodiment is generally a computer device with certain computing capability. In some possible implementation manners, the code relocation method can be realized by calling the computer readable instructions stored in the memory through the processor.
[0054] Referring to Figure 1As shown, a flowchart of a code relocation method provided by an embodiment of the present disclosure is shown, and the method comprises steps S101-S104, wherein:
[0055] S101, obtaining a target file to be relocated, and reading a load command list corresponding to target code from the target file; wherein the target file is used to generate an executable file through static linking.
[0056] In this step, the application program can include multiple source files, and assembling one source file can obtain a target file corresponding to the source file. The target file is usually binary machine language. Static linking of each target file of the application program can generate a corresponding executable file. Specifically, the target file can be an MH_OBJECT file, and the executable file can be an MH_EXECUTE file.
[0057] Generally, the generated executable file is relocated after static linking is completed. However, in some special scenarios, such as reverse analysis of the target file before static linking, it is necessary to directly obtain the address information of the target file, and therefore, it is necessary to relocate the target file before static linking.
[0058] In the target file, a load command list is stored, usually at the back of the file header. The load command list can include type information and length information of each command of the target file, data of the command itself, and information of a relocation information list Relocation of each section. Through the relocation information list, the target file can be relocated. The relocation information list can be found by traversing the load command list.
[0059] S102, based on the load command list, finding a relocation information list corresponding to the target code from the target file.
[0060] In this step, the relocation information list is usually used to correct the memory address when the application program is loaded into the memory, so as to prevent different files from being loaded into the memory and being allocated with the same memory address, which can cause the program to be unable to run. The load command list can include the relocation information list corresponding to each command
[0061] Specifically, the relocation information list can be found through the following steps:
[0062] finding data structure information and address offset information of the relocation information list from the load command list;
[0063] Based on the data structure information and address offset information of the relocation information list, the relocation information list is searched from the target file.
[0064] In this step, the information related to the relocation information list in each command is searched from the loading command, so as to obtain the data structure information and address offset information of the relocation information list. Then, the code segment matching the data structure information is searched from the target file, and the relocation information list matching the address offset information is screened from the determined code segment according to the address offset information.
[0065] For example, the description information of the data structure information is number of relocations, which means the serial number of the corresponding relocation information list. The description information of the address offset information can be relocations offset, which means the address offset length of the corresponding relocation information list.
[0066] S103、Based on the relocation information list, the target address calculation method and address parameter of the target code are determined.
[0067] In this step, the relocation information list is traversed, the required information is extracted from the relocation information list, and the target address calculation method and address parameter are determined according to the extracted information.
[0068] Specifically, the pointer type in the relocation information list and the initial address parameter can be read out. The initial address parameter is usually not the real address parameter of each instruction, and the real address information after relocation can be obtained only by calculating the initial address parameter.
[0069] For example, based on the pointer type in the relocation information list, the target address calculation method of the target code is screened from a plurality of address calculation methods. Then, based on the target data indicated by the target address calculation method, the address parameter is extracted from the relocation information list.
[0070] For different pointer types, different calculation methods need to be adopted to obtain correct address information. The address parameter can include symbol information symbolnum, regular expression library information pcrel, address length information length, and external variable information extern in the relocation information list. Through the obtained data structure information and address offset information of the relocation information list, the above information can be searched.
[0071] The pointer types mentioned above can include ARM64_RELOC_UNSIGNED, ARM64_RELOC_BRANCH26, ARM64_RELOC_PAGE21, ARM64_RELOC_PAGEOFF12, etc. The address calculation method for each pointer type can be determined based on the relationship between its corresponding address parameters and the corresponding real address information.
[0072] S104. Calculate the address parameters using the target address calculation method to obtain the address information after the target code relocation.
[0073] In this step, after determining the target address calculation method and address parameters, the target address calculation method can be used to calculate the address parameters to obtain the relocated address information.
[0074] For example, for target code of type ARM64_RELOC_UNSIGNED, the symbol information in the address parameter can be directly used as the relocated address information.
[0075] For target code of type ARM64_RELOC_BRANCH26, the code data corresponding to the symbol information indicator address can be determined. This code data is then converted to binary, and the corresponding jump address is determined according to the assembly jump instruction BL. The last 26 bits of the jump address are taken for calculation, and the immediate value of the last 26 bits is obtained using the imm26 instruction. For example, if the original code data is 0x0000094, it will be 0b1001 after conversion to binary. The immediate value obtained from 0100…… is imm26=0. Then, imm26=0 can be disassembled to obtain the original assembly instruction bl#0. Then, the address can be relocated using the calculation formula indicated in the target address calculation method. 0x110c(Value)-0x2c(PC) is calculated to obtain 0x10e0, where Value is the symbol information in the above address parameters, and PC is the parameter determined by the regular expression library information pcrel in the above address parameters. The intermediate information after relocation can be obtained. Dividing the intermediate information after relocation by 4 can obtain the relocated imm26. Restoring the relocated imm26 can obtain the relocated address information.
[0076] For target code of type ARM64_RELOC_PAGE21, the ADRP instruction in assembly is required to first disassemble the code, calculate the offset from the current PC to the exper value, clear the lower 12 bits of the PC to zero, add the offset, and then assemble the code to obtain the required address information. For target code of type ARM64_RELOC_PAGEOFF12, the ADD instruction in assembly is required, and the steps are similar to those of the ADRP instruction.
[0077] After obtaining the required address information, the relocated address information can be used to update the initial address information in the target file, resulting in the relocated target file. The relocated target file can then be analyzed to obtain the attribute information of the classes and methods contained in the target file. The attribute information can include the relocated address information. Using this information, an analysis report of the target file can be generated.
[0078] The code relocation method and apparatus provided in this disclosure first obtain the target file to be relocated and read the loading command list corresponding to the target code from the target file; wherein, the target file is used to generate an executable file through static linking; then, based on the loading command list, a relocation information list corresponding to the target code is searched from the target file; subsequently, based on the relocation information list, the target address calculation method and address parameters of the target code are determined; finally, the address parameters are calculated using the target address calculation method to obtain the relocated address information of the target code. This disclosure determines the target address calculation method and address parameters of the target code through the relocation information list, and then calculates the address parameters using the target address calculation method to obtain the relocated address information of the target code, thus determining the address information of the target code without static linking.
[0079] See Figure 2 The diagram shows a flowchart of another code relocation method provided in this embodiment. The method first collects the load commands in the target file, traverses the load commands, reads the relocations, obtains the address parameters and pointer types in the relocations, selects different target address calculation methods according to the pointer types, determines the relocated address information of each target code in the target file through the target address calculation method, and writes it into the target file to overwrite the original address data.
[0080] Those skilled in the art will understand that, in the above-described method of the specific implementation, the order in which each step is written does not imply a strict execution order and does not constitute any limitation on the implementation process. The specific execution order of each step should be determined by its function and possible internal logic.
[0081] Based on the same inventive concept, this disclosure also provides a code relocation device corresponding to the code relocation method. Since the principle of the device in this disclosure for solving the problem is similar to the code relocation method described above in this disclosure, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be described again.
[0082] Reference Figure 3 The diagram shown is a schematic of a code relocation device provided in an embodiment of this disclosure. The device includes:
[0083] The acquisition module 310 is used to acquire the target file to be relocated and read the list of loading commands corresponding to the target code from the target file; wherein, the target file is used to generate an executable file through static linking;
[0084] The lookup module 320 is used to look up the relocation information list corresponding to the target code in the target file based on the loading command list;
[0085] The determination module 330 is used to determine the target address calculation method and address parameters of the target code based on the relocation information list;
[0086] The positioning module 340 is used to calculate the address parameters using the target address calculation method to obtain the address information after the target code is relocated.
[0087] In one optional implementation, the lookup module 320 is specifically used for:
[0088] Search the data structure information and address offset information of the relocation information list from the list of loading commands;
[0089] Based on the data structure information and address offset information of the relocation information list, the relocation information list is searched from the target file.
[0090] In one optional implementation, the determining module 330 is specifically used for:
[0091] Based on the pointer types in the relocation information list, the target address calculation method of the target code is selected from multiple address calculation methods;
[0092] Based on the target data indicated by the target address calculation method, the address parameters are extracted from the relocation information list.
[0093] In one optional implementation, the apparatus further includes an update module for:
[0094] Using the relocated address information of the target code, the initial address information in the target file is updated to obtain the relocated target file.
[0095] In one optional implementation, the apparatus further includes an analysis module for:
[0096] Using the relocated target file, determine the attribute information of the classes and methods contained in the relocated target file;
[0097] An analysis report for the target file is generated using the attribute information.
[0098] In one optional implementation, the target file is an MH_OBJECT file.
[0099] The processing flow of each module in the device and the interaction flow between each module can be referred to the relevant descriptions in the above method embodiments, and will not be detailed here.
[0100] Corresponding to Figure 1 In addition to the code relocation method in the present disclosure, this embodiment also provides an electronic device 400, such as... Figure 4 The diagram shown is a structural schematic of an electronic device 400 provided in an embodiment of this disclosure, including:
[0101] The system includes a processor 41, a memory 42, and a bus 43. The memory 42 stores execution instructions and includes main memory 421 and external memory 422. The main memory 421, also called internal memory, temporarily stores the computational data in the processor 41, as well as data exchanged with external memory such as a hard disk. The processor 41 exchanges data with the external memory 422 through the main memory 421. When the electronic device 400 is running, the processor 41 communicates with the memory 42 through the bus 43, causing the processor 41 to execute the following instructions:
[0102] Obtain the target file to be relocated, and read the list of loading commands corresponding to the target code from the target file; wherein, the target file is used to generate an executable file through static linking;
[0103] Based on the list of loading commands, the relocation information list corresponding to the target code is retrieved from the target file;
[0104] Based on the relocation information list, the target address calculation method and address parameters of the target code are determined;
[0105] The address parameters are calculated using the target address calculation method to obtain the address information after the target code is relocated.
[0106] In one optional implementation, the step of searching for the relocation information list corresponding to the target code in the target file based on the load command list in the instructions executed by the processor 41 includes:
[0107] Search the data structure information and address offset information of the relocation information list from the list of loading commands;
[0108] Based on the data structure information and address offset information of the relocation information list, the relocation information list is searched from the target file.
[0109] In one optional implementation, the instruction executed by the processor 41, wherein determining the target address calculation method and address parameters of the target code based on the relocation information list, includes:
[0110] Based on the pointer types in the relocation information list, the target address calculation method of the target code is selected from multiple address calculation methods;
[0111] Based on the target data indicated by the target address calculation method, the address parameters are extracted from the relocation information list.
[0112] In an optional implementation, the processor 41 is further configured to perform:
[0113] Using the relocated address information of the target code, the initial address information in the target file is updated to obtain the relocated target file.
[0114] In an optional implementation, the processor 41 is further configured to perform:
[0115] Using the relocated target file, determine the attribute information of the classes and methods contained in the relocated target file;
[0116] An analysis report for the target file is generated using the attribute information.
[0117] In one optional implementation, the target file is an MH_OBJECT file.
[0118] This disclosure also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, performs the steps of the code relocation method described in the above method embodiments. The storage medium can be a volatile or non-volatile computer-readable storage medium.
[0119] This disclosure also provides a computer program product carrying program code. The program code includes instructions that can be used to execute the steps of the code relocation method described in the above method embodiments. For details, please refer to the above method embodiments, which will not be repeated here.
[0120] The aforementioned computer program product can be implemented through hardware, software, or a combination thereof. In one optional embodiment, the computer program product is specifically embodied in a computer storage medium; in another optional embodiment, the computer program product is specifically embodied in a software product, such as a software development kit (SDK), etc.
[0121] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems and devices described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here. In the several embodiments provided in this disclosure, it should be understood that the disclosed systems, devices, and methods can be implemented in other ways. The device embodiments described above are merely illustrative. For example, the division of units is only a logical functional division; in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Another point is that the displayed or discussed mutual coupling or direct coupling or communication connection may be through some communication interfaces; the indirect coupling or communication connection of devices or units may be electrical, mechanical, or other forms.
[0122] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0123] In addition, the functional units in the various embodiments of this disclosure can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0124] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a processor-executable, non-volatile, computer-readable storage medium. Based on this understanding, the technical solution of this disclosure, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this disclosure. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0125] Finally, it should be noted that the above-described embodiments are merely specific implementations of this disclosure, used to illustrate the technical solutions of this disclosure, and not to limit it. The protection scope of this disclosure is not limited thereto. Although this disclosure has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments, or make equivalent substitutions for some of the technical features, within the scope of the technology disclosed in this disclosure. Such modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this disclosure, and should all be covered within the protection scope of this disclosure. Therefore, the protection scope of this disclosure should be determined by the protection scope of the claims.
Claims
1. A code relocation method, characterized in that, Before generating an executable file via static linking, the following is included: Obtain the target file to be relocated, and read the list of loading commands corresponding to the target code from the target file; wherein, the target file is used to generate the executable file through the static linking; Based on the list of loading commands, the relocation information list corresponding to the target code is retrieved from the target file; Based on the relocation information list, the target address calculation method and address parameters of the target code are determined; The address parameters are calculated using the target address calculation method to obtain the address information after the target code is relocated. This address information is not obtained through the static link.
2. The method according to claim 1, characterized in that, The step of searching for the relocation information list corresponding to the target code in the target file based on the loading command list includes: Search the data structure information and address offset information of the relocation information list from the list of loading commands; Based on the data structure information and address offset information of the relocation information list, the relocation information list is searched from the target file.
3. The method according to claim 1, characterized in that, The step of determining the target address calculation method and address parameters of the target code based on the relocation information list includes: Based on the pointer types in the relocation information list, the target address calculation method of the target code is selected from multiple address calculation methods; Based on the target data indicated by the target address calculation method, the address parameters are extracted from the relocation information list.
4. The method according to claim 1, characterized in that, The method further includes: Using the relocated address information of the target code, the initial address information in the target file is updated to obtain the relocated target file.
5. The method according to claim 4, characterized in that, The method further includes: Using the relocated target file, determine the attribute information of the classes and methods contained in the relocated target file; An analysis report for the target file is generated using the attribute information.
6. The method according to claim 1, characterized in that, The target file is the MH_OBJECT file.
7. A code relocation device, characterized in that, include: The acquisition module is used to acquire the target file to be relocated before generating the executable file through static linking, and to read the list of loading commands corresponding to the target code from the target file; wherein, the target file is used to generate the executable file through static linking; The lookup module is used to search for a list of relocation information corresponding to the target code in the target file based on the list of loading commands; The determination module is used to determine the target address calculation method and address parameters of the target code based on the relocation information list; The positioning module is used to calculate the address parameters using the target address calculation method to obtain the address information after the target code is relocated. The address information is not obtained through the static link.
8. The apparatus according to claim 7, characterized in that, The search module is specifically used for: Search the data structure information and address offset information of the relocation information list from the list of loading commands; Based on the data structure information and address offset information of the relocation information list, the relocation information list is searched from the target file.
9. An electronic device, characterized in that, include: The device includes a processor, a memory, and a bus. The memory stores machine-readable instructions executable by the processor. When the electronic device is running, the processor communicates with the memory via the bus. When the machine-readable instructions are executed by the processor, the steps of the code relocation method as described in any one of claims 1 to 6 are performed.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, performs the steps of the code relocation method as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Relocation method and device, linker and compiling system
CN113760236A