A code processing method, device and equipment

By relocating the variable addresses associated with functions in the compiled first code and performing cross-platform decompilation, cross-platform code conversion is realized, solving the problem of inefficient application development and porting in the existing technology, and improving the accuracy and reliability of decompilation.

CN114327497BActive Publication Date: 2025-03-11HUAWEI TECH CO LTD

Patent Information

Application Number
CN202011063134.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-09-30
Publication Date
2025-03-11
Estimated Expiration
2040-09-30

Smart Images

  • Figure CN114327497B_ABST
    Figure CN114327497B_ABST
Patent Text Reader

Abstract

In order to solve the problem of low efficiency in application development and transplantation of the present application, the present application provides a code processing method, including: obtaining a first code, which is a compiled code suitable for the source platform, and then relocating the address of the variable associated with the function in the first code to obtain the logical address of the variable, and then decompiling according to the logical address of the variable and the first code to obtain a second code suitable for the target platform. On the one hand, this method realizes cross-platform decompilation and improves the efficiency of application development or transplantation; on the other hand, the variable associated with the function in the first code is relocated to solve the problem of pseudo code logic errors caused by uncertain definition and access address of the variable, thereby improving the accuracy of decompilation and the reliability of decompilation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of application development technology, and in particular to a code processing method and corresponding devices and equipment. Background Art

[0002] When developing applications, developers can learn from existing applications to improve development efficiency. Specifically, developers can use decompilation to restore compiled program code to source code, and then analyze the source code to help with application development. Among them, the compiled program code is usually the code of a low-level program language, such as the code of a machine language (also called machine code), and the source code is usually the code of a high-level program language, such as the code of C language, the code of Java language, etc. In some cases, the source code can also be the code of an assembly language.

[0003] Existing decompilation technology can support code conversion between different programming languages ​​on the same platform. For example, existing decompilers can convert machine language code of the x86 platform into assembly language code of the x86 platform. However, in many scenarios, it is also necessary to port the code of some platforms to other platforms, so as to realize the porting of applications to other platforms.

[0004] Based on this, the industry urgently needs to provide a method for cross-platform code conversion to improve the efficiency of application development and transplantation. Summary of the invention

[0005] The present application provides a code processing method, which realizes automatic cross-platform code conversion by relocating variables associated with functions in a first code obtained through compilation, and then performing cross-platform decompilation, thereby improving the efficiency of application development and transplantation. The present application also provides a device, an apparatus, a computer-readable storage medium, and a computer program product corresponding to the above method.

[0006] In a first aspect, the present application provides a code processing method. The method may be performed by a decompiler. The decompiler may be a software module that provides code decompilation services by running on a hardware device such as a computer. In some possible implementations, the decompiler may also be a hardware module with a code decompilation function.

[0007] Specifically, the decompiler obtains a first code, which is a compiled code suitable for a source platform, such as a code in an obj file under an x86 platform. The decompiler then relocates the address of a variable associated with a function in the first code to obtain a logical address of the variable. The logical address of the variable can be used to generate a definition of the variable, thereby determining the definition and access address of the variable in the first code, thereby obtaining a complete executable file. The decompiler decompiles according to the logical address of the variable and the first code, which is equivalent to decompiling the complete executable file, thereby obtaining a second code suitable for the target platform.

[0008] On the one hand, the method realizes cross-platform decompilation and improves the efficiency of application development or transplantation; on the other hand, the method relocates the address of the variable associated with the function in the first code, and the relocated address can be used to generate the definition of the variable, thereby solving the problem of logical errors in the generated pseudocode due to uncertain definition and access address of the variable, improving the accuracy of decompilation, and improving the reliability of decompilation.

[0009] In some possible implementations, the decompiler may be provided to the user in the form of a software package. Specifically, the owner of the decompiler may release the software package of the decompiler, and the user obtains the software package through a computing device and then runs the software package to implement cross-platform decompilation of the first code applicable to the source platform. In this way, cross-platform decompilation of the code may be implemented locally.

[0010] In some possible implementations, the decompiler can be provided to users in the form of cloud services. Users can upload the first code applicable to the source platform to the cloud, and the decompilation service in the cloud can decompile the code and then return the second code applicable to the target platform to the user. The decompilation process is mainly carried out in the cloud environment, and the computing device on the terminal side mainly assists in the decompilation. Therefore, the performance requirements of the computing device are relatively low, and the solution has high availability.

[0011] In some possible implementations, the decompiler can perform decompilation based on the logical address of the variable and the first code to obtain a compiler intermediate representation related to the target platform. The intermediate representation specifically refers to a code represented by an intermediate language. During the compilation or decompilation process, the compiler or decompiler can translate the first code into a code represented by an intermediate language, i.e., an intermediate representation, and then translate the intermediate representation to obtain the second code.

[0012] In the method, the compiler intermediate representation generated according to the logical address of the variable and the first code includes the definition of the variable. The definition of the variable associates the symbol and address of the variable. Based on the compiler intermediate representation, a logically correct pseudo code can be generated, and then a second code with higher accuracy and suitable for the target platform can be generated.

[0013] On the one hand, the method realizes cross-platform code decompilation through the compiler intermediate representation. On the other hand, the method relocates the variables associated with the function in the first code so that the generated pseudocode logic is correct, thereby making the generated second code suitable for the target platform have higher accuracy.

[0014] In some possible implementations, the compiler intermediate representation includes a first variable and a second variable. The first variable has a first logical address, the second variable has a second logical address, and the first logical address and the second logical address are different. The logical address may be an absolute address, based on which data of different variables can be accurately obtained, thereby generating an accurate second code.

[0015] In some possible implementations, the first code includes a code area, a symbol area, a data area and a relocation table. The decompiler can obtain the above-mentioned relocation table from the first code. The relocation table stores the logical addresses of variables. The decompiler can relocate the addresses of variables associated with functions in the first code according to the relocation table.

[0016] Based on this, the decompiler can generate the definition of the variable according to the logical address of the variable obtained by relocation, thereby solving the problem of logical errors in the generated pseudocode caused by uncertain variable definition and access address, which in turn leads to low usability of the generated code suitable for the target platform. This method improves the accuracy and reliability of decompilation.

[0017] In some possible implementations, the variables associated with a function may include multiple types. For example, the variables associated with a function may include local variables and global variables. In some embodiments, the variables associated with a function may also be divided into static variables and dynamic variables according to whether the original value is retained. Accordingly, when the decompiler is performing variable relocation, it may determine the relocation type according to the type of the variable associated with a function in the first code, and then relocate the address of the variable associated with a function in the first code according to the relocation type according to the relocation table.

[0018] The decompiler can determine the relocation type based on the type of the variable and the associated compilation options in combination with the relocation table. The compilation options are used to indicate the compilation mode, such as fpIC indicating that the code is position independent code (PIC), and mcmodel indicating the code model. mcmodel can take the value of small, medium or large, which are used to indicate the small code model, medium code model and large code model respectively.

[0019] For example, for common variables such as global variables and static variables, when mcmodel=large, the decompiler determines the relocation type as R_X86_64_64 based on the immediate instruction in the relocation instruction segment, that is, the addressing mode is changed to immediate addressing. After determining the addressing mode, the decompiler can obtain the logical address of the variable according to the immediate addressing mode combined with the relocation table.

[0020] The method relocates different types of variables using corresponding relocation methods, so that the entire code file can be accurately located, thereby generating a logically correct pseudo code, and based on the logically correct pseudo code, a second code with higher accuracy can be generated, which can meet business needs.

[0021] In some possible implementations, before decompiling according to the logical address of the variable and the first code, the decompiler may also adjust parameters of the function in the first code according to differences in function calling rules between the source platform and the target platform.

[0022] The method performs cross-platform difference processing on the first code, thereby realizing cross-platform conversion of the first code, and the converted code can be executed on the target platform.

[0023] In some possible implementations, when the decompiler adjusts the parameters of the function in the first code, the decompiler specifically adjusts the register information or stack information of the function in the first code according to the difference in function calling rules between the source platform and the target platform.

[0024] Specifically, the parameters of the function are stored in registers or in a stack stored in memory. The storage method of parameters is different in different platforms. The decompiler can adjust the register information and / or stack information of the function in the first code according to the difference in function calling rules between the source platform and the target platform. For example, the decompiler can adjust the parameters stored in the registers and store them in the stack; or adjust the parameters stored in the stack and store them in the registers, so that the runtime before the cross-platform operation is consistent with the runtime after the cross-platform operation. This allows the code after the cross-platform operation to be executed normally.

[0025] In some possible implementations, the decompiler may provide a user interface, such as a graphical user interface. A user may input the first code through a user interface such as a graphical user interface, and then the decompiler receives the first code and further decompiles the first code across platforms, thereby improving the efficiency of code or application transplantation.

[0026] In some possible implementations, the second code applicable to the target platform may be a code in a low-level programming language, such as a code in a target file format. Of course, the second code applicable to the target platform may also be a code in a high-level programming language, such as a code in a C language, etc. Among them, the efficiency of code transplantation is improved by obtaining the code in the high-level programming language through cross-platform decompilation.

[0027] In a second aspect, the present application provides a code processing device. The device includes:

[0028] The communication module is used to obtain a first code, where the first code is a compiled code suitable for a source platform;

[0029] A relocation module, used for relocating the address of the variable associated with the function in the first code to obtain the logical address of the variable;

[0030] The decompiling module is used to perform decompiling according to the logical address of the variable and the first code to obtain a second code suitable for the target platform.

[0031] In some possible implementations, the decompilation module is specifically used to:

[0032] Decompiling according to the logical address and the first code to obtain a compiler intermediate representation related to the target platform, the compiler intermediate representation including a definition of a variable, the definition of the variable associating a symbol and an address of the variable;

[0033] A second code suitable for the target platform is generated according to the compiler intermediate representation.

[0034] In some possible implementations, the compiler intermediate representation includes a first variable and a second variable, the first variable has a first logical address, the second variable has a second logical address, and the first logical address and the second logical address are different.

[0035] In some possible implementations, the relocation module is specifically used to:

[0036] Obtaining a relocation table from the first code, wherein the relocation table stores a logical address of the variable;

[0037] The address of the variable associated with the function in the first code is relocated according to the relocation table.

[0038] In some possible implementations, the relocation module is specifically used to:

[0039] Determine a relocation type according to the type of the variable associated with the function in the first code;

[0040] According to the relocation table, the address of the variable associated with the function in the first code is relocated according to the relocation type.

[0041] In some possible implementations, the device further includes:

[0042] The adjustment module is used to adjust the parameters of the function in the first code according to the difference between the function calling rules of the source platform and the target platform before decompiling according to the logical address of the variable and the first code.

[0043] In some possible implementations, the adjustment module is specifically used to:

[0044] According to the difference between the function calling rules of the source platform and the target platform, the register information or stack information of the function in the first code is adjusted.

[0045] In some possible implementations, the communication module is specifically used to:

[0046] A first code input by a user through a graphical user interface is received.

[0047] In a third aspect, the present application provides a computing device, the computing device comprising a processor and a memory. The processor and the memory communicate with each other. The processor is used to execute instructions stored in the memory, so that the computing device executes the code processing method in the first aspect or any implementation of the first aspect.

[0048] In a fourth aspect, the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, wherein the instructions instruct a computing device to execute the code processing method described in the above-mentioned first aspect or any one of the implementations of the first aspect.

[0049] In a fifth aspect, the present application provides a computer program product comprising instructions, which, when executed on a computing device, enables the computing device to execute the code processing method described in the first aspect or any one of the implementations of the first aspect.

[0050] Based on the implementations provided in the above aspects, the present application can also be further combined to provide more implementations. BRIEF DESCRIPTION OF THE DRAWINGS

[0051] In order to more clearly illustrate the technical method of the embodiments of the present application, the drawings required for use in the embodiments are briefly introduced below.

[0052] Figure 1A system architecture diagram of a code processing method provided in an embodiment of the present application;

[0053] Figure 2 A system architecture diagram of a code processing method provided in an embodiment of the present application;

[0054] Figure 3 A flowchart of a code processing method provided in an embodiment of the present application;

[0055] Figure 4 A schematic diagram of receiving a first code through a GUI provided in an embodiment of the present application;

[0056] Figure 5 A schematic diagram of adjusting function parameters provided in an embodiment of the present application;

[0057] Figure 6 A flowchart of a code processing method provided in an embodiment of the present application;

[0058] Figure 7 A schematic diagram of the structure of a code processing device provided in an embodiment of the present application;

[0059] Figure 8 A schematic diagram of the structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0060] The terms "first" and "second" in the embodiments of the present application are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features.

[0061] First, some technical terms involved in the embodiments of the present application are introduced.

[0062] Programming language is a formal language used to define computer programs, so programming language is also called computer language. When developers develop applications, they write code files in the above programming language.

[0063] Programming languages ​​can be divided into low-level programming languages ​​and high-level programming languages ​​according to whether they are machine-oriented. Among them, programming languages ​​oriented to machines (such as computers) are low-level programming languages. Low-level programming languages ​​include machine languages, assembly languages, etc.

[0064] Machine language is a language represented by binary code. Machine language is the only language that computers can recognize and execute. Assembly language is a language that uses easy-to-understand and memorable names and symbols to represent the opcodes in machine instructions in order to solve the shortcomings of machine language, which is difficult to understand and remember. Assembly language uses symbols to replace the binary code of machine language, so assembly language is essentially a symbolic language.

[0065] It should be noted that code files written in assembly language are usually not directly recognized by machines (such as computers). For this reason, developers can usually also use assemblers to translate assembly language into machine language so that the machine can recognize and execute it. Therefore, compared with machine language, assembly language is a relatively high-level programming language.

[0066] Programming languages ​​other than machine-oriented programming languages ​​are high-level programming languages. A high-level programming language is a language that is independent of a machine. That is, a high-level programming language can usually be applied to different types of machines, such as machines with an x86 instruction set architecture or machines with an advanced RISC machines (ARM) architecture. High-level programming languages ​​are less dependent on machines.

[0067] High-level programming languages ​​are usually close to natural languages ​​and can use mathematical expressions, so they have stronger expressive power, can easily represent data operations and program control structures, and can better describe various algorithms. High-level programming languages ​​specifically include different languages ​​such as java, c, c++, C#, pascal, python, etc. Similar to assembly language, high-level programming languages ​​cannot be directly recognized and executed by machines. Developers can compile code files written based on high-level programming languages ​​through compilation programs such as compilers so that they can be recognized and executed by machines.

[0068] When developing applications, developers can learn from existing applications to improve development efficiency. Specifically, developers can use decompilation to restore compiled program codes and then analyze the restored codes to help application development.

[0069] Among them, the compiled program code is usually the code of a low-level programming language, such as the code of a machine language (also called machine code), and the restored code is usually the code of a high-level programming language, such as the code of C language, Java language, etc. In some cases, the restored code may also be the code of assembly language.

[0070] Existing decompilation technology can support code conversion between different programming languages ​​on the same platform. For example, existing decompilers can convert machine language code of the x86 platform into assembly language code of the x86 platform. However, in many scenarios, it is also necessary to port the code of some platforms to other platforms, so as to realize the porting of applications to other platforms.

[0071] Among them, the platform corresponding to the ported code is called the source platform, and the platform corresponding to the ported code is called the target platform. The source platform and the target platform are specifically different platforms. Different platforms refer to platforms with different instruction sets. Among them, platforms with different instruction sets can be platforms with different instruction sets under the same type, such as Pentium II and Pentium III under the x86 platform (introducing the new SSE instruction set), or ARM V8 and ARM V9 under the ARM platform. Platforms with different instruction sets can also be platforms of different types, such as any platform under the x86 platform and any platform under the ARM platform. The code of the source platform can specifically be the code obtained after compilation, which can be, for example, the code in the target (object, obj) file.

[0072] However, although the code in the obj file is binary code, the obj file is not a complete executable file. The definition and access address of the variables in the obj file are uncertain, which leads to logical errors in the generated pseudocode, and in turn causes the decompiler to directly decompile the code in the obj file with low accuracy and reliability.

[0073] In view of this, an embodiment of the present application provides a code processing method. The method can be performed by a decompiler. Among them, the decompiler can be a software module, which provides code decompilation services by running on a hardware device such as a computer. In some possible implementations, the decompiler can also be a hardware module with a code decompilation function.

[0074] Specifically, the decompiler obtains the first code, which is the compiled code suitable for the source platform, such as the code in the obj file under the x86 platform, and then the decompiler relocates the address of the variable associated with the function in the first code to obtain the logical address of the variable. The logical address is also called the program address, which is usually the address assigned by the developer when writing the code, and can be translated or mapped to a physical address for addressing.

[0075] The logical address based on the variable can be used to generate the definition of the variable, so that the definition and access address of the variable in the first code can be determined, thereby obtaining a complete executable file. The decompiler decompiles according to the logical address of the variable and the first code, which is equivalent to decompiling the complete executable file, thereby obtaining a second code suitable for the target platform.

[0076] On the one hand, the method realizes cross-platform decompilation and improves the efficiency of application development or transplantation; on the other hand, the method relocates the address of the variable associated with the function in the first code, and the relocated address can be used to generate the definition of the variable, thereby solving the problem of logical errors in the generated pseudocode due to uncertain definition and access address of the variable, improving the accuracy of decompilation, and improving the reliability of decompilation.

[0077] It should be noted that the decompiler provided by the embodiment of the present application can be provided to the user in the form of a software package. Specifically, the owner of the decompiler can publish the software package of the decompiler, and the user obtains the software package, and then runs the software package, thereby realizing that the first code applicable to the source platform is carried out cross-platform decompilation. In some possible implementations, the decompiler provided by the embodiment of the present application can be provided to the user in the form of a cloud service. The user can upload the first code applicable to the source platform to the cloud, and the decompilation service in the cloud can decompile the code, and then return to the user the second code applicable to the target platform.

[0078] In order to make the technical solution of the present application clearer and easier to understand, the deployment method of the decompiler is described in detail below with reference to the accompanying drawings.

[0079] See also Figure 1 In the system architecture diagram of the code processing method shown in FIG. 1 , the decompiler 100 can be deployed on a computing device 200. The computing device 200 includes but is not limited to a desktop computer, a laptop computer, and the like. The decompiler 100 is used to decompile the code. The decompiler 100 can be independent or integrated with other development tools, such as an editor, a compiler, a debugger, etc., to form an integrated development environment (IDE).

[0080] The decompiler 100 running on the computing device 200 can obtain the first code, which is a compiled code suitable for the source platform, for example, it can be a binary code in an obj file suitable for the x86 platform, and then the decompiler 100 relocates the address of the variable associated with the function in the first code, and then the decompiler 100 decompiles according to the address of the relocated variable and the first code to obtain the second code suitable for the target platform. In this way, the first code is decompiled locally (for example, the local computing device 200) across platforms.

[0081] Next, see Figure 2 As shown in the system architecture diagram of the code processing method, the decompiler 100 is deployed in the cloud environment 300, and the user accesses the decompiler 100 in the cloud environment 300 through the computing device 200, thereby realizing cross-platform decompilation of the first code.

[0082] The cloud environment 300 indicates a cloud computing cluster owned by a cloud service provider for providing computing, storage, and communication resources. The cloud computing cluster can be divided into a central cloud and an edge cloud according to its location in the network topology. The cloud computing cluster includes at least one cloud computing device, such as at least one central server or at least one edge server.

[0083] The computing device 200 can submit the first code to the decompiler 100 running in the cloud environment 300. The decompiler 100 obtains the first code and can relocate the address of the variable associated with the function in the first code. Then the decompiler 100 decompiles according to the address after the variable relocation and the first code, thereby obtaining the second code suitable for the target platform. Further, the decompiler 100 in the cloud environment 300 can return the second code to the computing device 200. In this way, cross-platform decompilation of the code is achieved in the cloud. Since the decompilation process is mainly carried out in the cloud, the computing device 200 mainly assists in decompilation, so the performance requirements of the computing device 200 are relatively low, and it has high availability.

[0084] Figure 1 and Figure 2 The above description is merely an example of some deployment methods of the decompiler 100. In other possible implementation methods of the embodiment of the present application, the decompiler 100 may also be deployed in other ways, which are not limited in the embodiment of the present application.

[0085] Next, in conjunction with the accompanying drawings, the code processing method provided in the embodiment of the present application is introduced in detail from the perspective of the decompiler 100.

[0086] See also Figure 3 The flowchart of the code processing method shown in FIG. 1 comprises:

[0087] S302: The decompiler 100 obtains the first code.

[0088] The first code is a compiled code suitable for a source platform. The source platform may be any one of an x86 platform, an ARM platform, or the like. The compiled code is a code obtained by a compiler compiling a source code. The code is specifically a binary code. In some embodiments, the first code may be a code in an obj file. In other embodiments, the first code may also be a mixed code, which is specifically mixed with a code in an obj file.

[0089] In some possible implementations, the decompiler 100 may receive the first code input by the user through a user interface, such as a graphical user interface (GUI) or a command user interface (CUI).

[0090] For ease of understanding, a specific example is used below to illustrate. Figure 4 The decompiler 100 shown in FIG. 1 is a schematic diagram of obtaining the first code through the GUI, such as Figure 4 As shown, the GUI provided by the decompiler 100 carries a code input control 402 and a code decompilation control 404, and the user can input the first code through the code input control 402. Specifically, the user can input the storage path of the first code through the code input control 402, so that the decompiler 100 can obtain the first code according to the storage path. The user can also trigger the decompilation operation of the above-mentioned first code through the code decompilation control 404, and the decompiler 100 can start the cross-platform decompilation of the first code in response to the operation.

[0091] It should be noted that the first code may be part of the code in the code file, or may be all of the code in the code file, which is not limited in the present embodiment of the application.

[0092] S304: The decompiler 100 relocates the address of the variable associated with the function in the first code to obtain the logical address of the variable.

[0093] The first code includes at least one function call. The called function includes independent variables and dependent variables, wherein the independent variable is a condition or factor that can be manipulated to cause the dependent variable to change, and the dependent variable is a quantity that changes as the independent variable changes. When the independent variable takes a unique value, the dependent variable has and only has a unique value corresponding to it. Based on this, the variables associated with the function may include at least one of the above-mentioned independent variables and dependent variables. For example, in the function y=a+b*c, the variables associated with the function include the above-mentioned independent variables a, b, c and the dependent variable y.

[0094] In this embodiment, the address of the variable associated with the function in the first code (such as the code in the obj file) is a relative address, that is, the address of each variable is uncertain. For this reason, the decompiler 100 can relocate the variables associated with the function in the first code to determine the logical address of each variable, which specifically refers to the absolute address of the variable.

[0095] Specifically, the first code includes a code area, a symbol area, a data area and a relocation table. Among them, the code area mainly includes code, the symbol area mainly includes symbols of variables, the data area mainly includes data in the code, and the relocation table mainly includes the logical address of each variable. Based on this, the decompiler 100 can obtain the relocation table from the first code, and then obtain the logical address of the variable associated with the function in the first code by accessing the relocation table, thereby realizing the relocation of the address of the variable associated with the function in the first code.

[0096] Furthermore, when the decompiler 100 relocates the address of the variable associated with the function in the first code according to the relocation table, the decompiler 100 may also perform the relocation in combination with the type of the variable. Specifically, the decompiler 100 may determine the relocation type according to the type of the variable associated with the function in the first code, and then relocate the address of the variable associated with the function in the first code according to the relocation type according to the relocation table.

[0097] Wherein, the type of variable may include local variables and global variables. Of course, the type of variable may also be divided into static variables and dynamic variables according to whether the original value is maintained. Decompiler 100 may determine the relocation type according to the type of variable and the associated compilation options in combination with the relocation table. Wherein, the compilation options are used to indicate the compilation mode, such as fpIC indicating that the code is position-independent code (PIC), and mcmodel indicating the code model. mcmodel may take the values ​​of small, medium or large, which are used to indicate a small code model, a medium code model and a large code model, respectively.

[0098] For example, for common variables such as global variables and static variables, when mcmodel=large, the decompiler 100 determines the relocation type as R_X86_64_64 according to the immediate instruction of the relocation instruction segment, that is, the addressing mode is changed to immediate addressing. After determining the addressing mode, the decompiler 100 can obtain the logical address of the variable according to the immediate addressing mode combined with the relocation table.

[0099] Furthermore, the decompiler 100 may associate the absolute address with the symbol of the variable, thereby relocating the address of the variable associated with the function.

[0100] S306: The decompiler 100 performs decompilation according to the logical address of the variable and the first code to obtain a second code suitable for the target platform.

[0101] The target platform is a platform different from the source platform. The target platform can be any one of the x86 platform, ARM platform, etc. When the source platform is the x86 platform, the target platform can be the ARM platform, and when the source platform is the ARM platform, the target platform can be the x86 platform.

[0102] The second code applicable to the target platform may be a low-level program language code applicable to the target platform. For example, when the first code is a code of a machine language under an x86 platform, the second code may be a code of a machine language under an ARM platform. Further, the second code may also be a code of an assembly language under an ARM platform.

[0103] The second code applicable to the target platform can also be a high-level program language code. For example, when the first code is a machine language code under the x86 platform, the second code can be a C language code, a Java language code, or a Python language code, etc.

[0104] In this embodiment, the logical address of the variable and the first code may constitute a complete executable file, and the decompiler 100 may decompile the complete executable file to obtain a second code suitable for the target platform.

[0105] Specifically, the decompiler 100 may perform decompilation according to the logical address of the variable and the first code to obtain a compiler intermediate representation (IR) related to the target platform.

[0106] The intermediate representation specifically refers to code represented by an intermediate language. During the compilation or decompilation process, the compiler or decompiler can translate the first code into code represented by an intermediate language, namely, the intermediate representation, and then translate the intermediate representation to obtain the second code.

[0107] Since the decompiler 100 performs decompilation according to the logical address after the variable relocation and the first code, the intermediate representation obtained by the decompiler 100 includes the definition of the variable associated with the function. Among them, the definition of the variable associates the symbol of the variable with the logical address of the variable. The decompiler 100 can generate a logically correct pseudo code according to the intermediate representation, and then generate the corresponding second code. In this way, the problem of logical errors in the generated pseudo code due to the uncertainty of the definition and access address of the variable is overcome, the accuracy of the decompilation is improved, and the reliability of the decompilation is improved.

[0108] In some possible implementations, the decompiler 100 may compile the compiler intermediate representation to obtain a second code suitable for the target platform. The second code may be a machine language code (such as a code in obj format) suitable for the target platform.

[0109] In some other possible implementations, after obtaining the low-level program language code suitable for the target platform, such as after obtaining the machine language code, the decompiler 100 can further process the low-level program language code, such as disassembling it, to obtain the assembly language code. The second code can also be the assembly language code suitable for the target platform. Of course, the decompiler 100 can also process the compiler intermediate representation to obtain the high-level program language code. The second code can also be the high-level program language code.

[0110] After the decompiler 100 decompiles the first code and obtains the second code, it can also output the second code. In some embodiments, the decompiler 100 can output the second code in the form of a file. In other embodiments, the decompiler 100 can also present the second code to the user through a user interface, such as a GUI or a CUI.

[0111] Based on the above description, an embodiment of the present application provides a code processing method. The method converts the compiled first code into an intermediate representation related to the target platform through decompilation, and then compiles based on the intermediate representation to obtain a second code suitable for the target platform, thereby realizing cross-platform decompilation of the first code, reducing the technical threshold of cross-platform code migration, and improving the efficiency of code migration. In addition, the method solves the problem of missing address redirection information of low-level program language modules during the decompilation process, improves the accuracy of decompilation, and improves the reliability of decompilation.

[0112] exist Figure 3 In the illustrated embodiment, taking into account the differences in function calling rules on different platforms, before the decompiler 100 decompiles the code across platforms, specifically before decompiling based on the logical address after variable relocation and the first code, the decompiler 100 can adjust the parameters of the function in the first code according to the differences in function calling rules between the source platform and the target platform.

[0113] Specifically, the parameters of the function are stored in registers or in a stack in memory. The storage methods of parameters are different in different platforms. The decompiler 100 can adjust the register information and / or stack information of the function in the first code according to the difference in function call rules between the source platform and the target platform. For example, the decompiler 100 can adjust the parameters stored in the registers and store them in the stack; or adjust the parameters stored in the stack and store them in the registers.

[0114] Before adjusting the register information and / or stack information, the decompiler 100 may first use a decoding tool, such as Intel XED, to decode the first code to obtain the instruction control flow. The decompiler 100 may then execute a data flow analysis algorithm for the instruction control flow to analyze the active registers and stacks, thereby obtaining the parameter type and quantity of the function in the first code. The parameter type is mainly used to indicate whether the parameter is stored in a register or in a stack.

[0115] For ease of understanding, the processes of register analysis and stack analysis are described in detail below.

[0116] First, this embodiment defines the following data sets:

[0117] Use[n]:set of variables used by n;

[0118] Def[n]:set of variables defined by n;

[0119] In[n]:variables live on entry to n;

[0120] Out[n]:variables live on exit to n;

[0121] Among them, variables represents the registers corresponding to the variables. In[n] and Out[n] represent the sets of registers corresponding to input and output respectively, and Def[n] and Use[n] represent the sets of registers corresponding to definition and use respectively.

[0122] The decompiler 100 can traverse the blocks in the first code and construct the use set and def set of each block. The specific construction process is as follows:

[0123] a) Traverse the instructions in the block according to the execution order of the instructions in the block;

[0124] b) If the operand type of the instruction is Register and the action is kActionRead, it will be added to the use set;

[0125] c) If the operand type of the instruction is Register and the action is kActionWrite, it will be added to the def set;

[0126] d) If the operand type of the instruction is Address, add its base_reg and index_reg to the use set;

[0127] The decompiler 100 can establish a data flow analysis equation according to the above set, as shown below:

[0128]

[0129] Among them, n represents a block, symbol Indicates that the set to the right of the symbol is a subset of the set to the left of the symbol. succ[n] indicates the registers that are still valid in the block.

[0130] The decompiler 100 can solve the above equation by a fixed point algorithm as follows:

[0131]

[0132] After the fixed point algorithm, the intersection of the in set at the function entry and the input parameter Reg specified by the Calling Convention is the input parameter register; the intersection of the out set at the function exit and the output parameter Reg specified by the Calling Convention is the possible return value register.

[0133] When performing stack analysis, the decompiler 100 may use an algorithm based on a rex-extended stack pointer (RSP) register or an algorithm based on a rex-extended base pointer (RBP) register to analyze and implement the instruction control flow.

[0134] The process of the decompiler 100 performing analysis using the RSP-based algorithm may specifically include the following steps:

[0135] a. Check whether RSP has an offset based on the function prelogue part (entry basic block) and record the offset value off;

[0136] The decompiler 100 may determine the offset through a sub instruction or a push instruction. The decompiler 100 also records the register associated with the RSP.

[0137] b. Traverse all instructions in all blocks and find the usage scenario where the operand Operand Type is kTypeAddress, action is kActionRead, base_reg = RSP (associated register), and the memory offset (dispalcement, dis) is a positive number. This parameter is the (dis-off) / 8th stack parameter, and then count the total number of parameters S.

[0138] c. For cases that meet rule b, further distinguish parameter types:

[0139] If the other register operands of the same instruction are integer registers (RXX), and the instruction is a non-floating point->integer type conversion related instruction, the stack parameter is determined to be an integer. If the other register operands of the same instruction are floating point registers (XMM), and the instruction is a non-integer->floating point type conversion related instruction, the stack parameter is determined to be a floating point.

[0140] The process of the decompiler 100 performing analysis using the RBP-based algorithm may specifically include the following steps:

[0141] a. Traverse all instructions in all blocks and find the usage scenario where the operand Operand Type is kTypeAddress, action is kActionRead, base_reg = RBP, and dis is a positive number. This parameter is the (dis-8) / 8th stack parameter, and the total number of parameters is X.

[0142] b. For cases that meet the a rule, further distinguish the parameter types:

[0143] If the other register operand of the same instruction is an integer register (RXX), and the instruction is a non-floating point->integer type conversion related instruction, the stack parameter is judged to be an integer. If the other register operand of the same instruction is a floating point register (XMM), and the instruction is a non-integer->floating point type conversion related instruction, the stack parameter is judged to be a floating point.

[0144] In some possible implementations, the decompiler 100 may execute the above two algorithms simultaneously, and then take the maximum value of the total number of parameters S determined by the two algorithms.

[0145] After obtaining the total number of parameters and parameter types, the decompiler 100 can adjust the storage location of the parameters according to the difference in function call rules. Specifically, the decompiler 100 performs cross-platform processing on the input parameter registers and stack according to the difference in function call rules, such as pushing several parameters in the registers into the stack, switching the stack pointer, etc., so that the input parameter registers and stack space perspectives are consistent at runtime on different platforms.

[0146] To facilitate understanding, a specific example is provided below for explanation.

[0147] See also Figure 5 Schematic diagram of adjusting the parameters of a function shown in the example, the function test includes 10 parameters from i0 to i9. In the runtime under the x86 platform, parameters i0 to i5 are stored in registers, and i6 to i9 are stored in the stack. In the runtime under the ARM platform, parameters i0 to i7 are stored in registers, and i8 to i9 are stored in the stack. The decompiler 100 can push parameters i6 and i7 onto the stack and switch the stack pointer, so that the input parameter registers and stack space perspectives of the runtime under different platforms are consistent.

[0148] This method uses compiler active register analysis and stack analysis to obtain accurate input parameter registers and stack input parameters, thereby reducing unnecessary register conversions of function call conventions.

[0149] In addition, the present application also provides a specific example of relocating the address of a variable associated with a function, and provides a detailed description of a second code obtained by decompiling the first code based on the relocated logical address.

[0150] See also Figure 6 The flowchart of the code processing method shown in FIG. Figure 6 As shown, the decompiler 100 first obtains the first code to be decompiled, such as Figure 6 As shown in (A), the first code includes at least one function call, such as test1 and main, and each called function is associated with a variable. The address of the variable is specifically shown in the marked box 602 in (A). Based on 602, it can be known that the address of the variable in the first code is a relative address.

[0151] Then see Figure 6 In (B), the decompiler 100 relocates the variables associated with the function in the first code, for example, the address of test1 is redirected from 0000000000000000 to 0000000000400536, the address of main is redirected from 00000000000000c to 0000000000400542, and the address of a variable associated with the function body of test1 is redirected from 00 00 00 00 to dc 0a 20 00, which are not listed one by one here. The addresses of the variables after relocation are shown in the marked box 604 in (B).

[0152] See next Figure 6 In (C), the decompiler 100 performs decompilation according to the address after the variable relocation and the first code to obtain a compiler intermediate representation. The compiler intermediate representation includes the definition of the variable, as shown in 606 in (C). As shown in 606, @test1 is the definition of test1.

[0153] Last seen Figure 6 In (D), the decompiler 100 generates a second code suitable for a target platform such as an ARM platform according to the compiler intermediate representation, specifically an assembly language code suitable for the ARM platform.

[0154] It should be noted that the first code is a machine language code, specifically a binary code, in order to enhance readability. Figure 6 In (A) and (B), examples are given by using codes obtained by disassembling the first code.

[0155] Combination of the above Figures 1 to 6 The code processing method provided in the embodiment of the present application is introduced in detail. The device and equipment provided in the embodiment of the present application will be introduced in conjunction with the accompanying drawings.

[0156] See also Figure 7 The schematic diagram of the structure of the code processing device shown in FIG. 700 is a device that can realize the function of the decompiler 100. The device 700 includes:

[0157] The communication module 702 is used to obtain a first code, where the first code is a compiled code suitable for a source platform;

[0158] A relocation module 704, used for relocating the address of the variable associated with the function in the first code to obtain the logical address of the variable;

[0159] The decompiling module 706 is used to perform decompiling according to the logical address of the variable and the first code to obtain a second code suitable for the target platform.

[0160] In some possible implementations, the decompile module 706 is specifically used to:

[0161] Decompiling according to the logical address of the variable and the first code to obtain a compiler intermediate representation related to the target platform, the compiler intermediate representation including a definition of the variable, the definition of the variable associating a symbol and an address of the variable;

[0162] A second code suitable for the target platform is generated according to the compiler intermediate representation.

[0163] In some possible implementations, the compiler intermediate representation includes a first variable and a second variable, the first variable has a first logical address, the second variable has a second logical address, and the first logical address and the second logical address are different.

[0164] In some possible implementations, the relocation module 704 is specifically configured to:

[0165] Obtaining a relocation table from the first code, wherein the relocation table stores a logical address of the variable;

[0166] The address of the variable associated with the function in the first code is relocated according to the relocation table.

[0167] In some possible implementations, the relocation module 704 is specifically configured to:

[0168] Determine a relocation type according to the type of the variable associated with the function in the first code;

[0169] According to the relocation table, the address of the variable associated with the function in the first code is relocated according to the relocation type.

[0170] In some possible implementations, the apparatus 700 further includes:

[0171] The adjustment module is used to adjust the parameters of the function in the first code according to the difference between the function calling rules of the source platform and the target platform before decompiling according to the logical address of the variable and the first code.

[0172] In some possible implementations, the adjustment module is specifically used to:

[0173] According to the difference between the function calling rules of the source platform and the target platform, the register information or stack information of the function in the first code is adjusted.

[0174] In some possible implementations, the communication module 702 is specifically configured to:

[0175] A first code input by a user through a graphical user interface is received.

[0176] The code processing device 700 according to the embodiment of the present application may correspond to executing the method described in the embodiment of the present application, and the above and other operations and / or functions of each module / unit of the code processing device 700 are respectively to implement Figure 3 For the sake of brevity, the corresponding processes of each method in the illustrated embodiment are not described in detail here.

[0177] The present application also provides a computing device 200. The computing device 200 may be a terminal device such as a laptop computer or a desktop computer. The computing device 200 is specifically used to implement the following Figure 7 The functions of the code processing device 700 in the illustrated embodiment.

[0178] Figure 8 A schematic diagram of the structure of a computing device 200 is provided. Figure 8As shown, the computing device 200 includes a bus 201, a processor 202, a communication interface 203, and a memory 204. The processor 202, the memory 204, and the communication interface 203 communicate with each other via the bus 201.

[0179] The bus 201 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 8 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0180] The processor 202 may be any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).

[0181] The communication interface 203 is used for communicating with the outside, for example, obtaining a first code applicable to a source platform, the first code being a compiled code, such as a code in an obj file, or outputting a second code applicable to a target platform, and so on.

[0182] The memory 204 may include a volatile memory, such as a random access memory (RAM). The memory 204 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD), or a solid state drive (SSD).

[0183] The memory 204 stores executable codes, and the processor 202 executes the executable codes to perform the aforementioned code processing method.

[0184] Specifically, in implementing Figure 7 In the case of the embodiment shown, and Figure 7 When each module of the code processing device 700 described in the embodiment is implemented by software, Figure 7The software or program codes required for the functions of the relocation module 704, the decompilation module 706 and other modules are stored in the memory 204. The functions of the communication module 702 are implemented through the communication interface 203.

[0185] Specifically, the communication interface 203 obtains a first code, which is a compiled code suitable for the source platform. The communication interface 203 transmits the first code to the processor 202 through the bus 201. The processor 202 executes the program code corresponding to each module stored in the memory 204, such as the program code corresponding to the relocation module 704 and the decompilation module 706, to relocate the address of the variable associated with the function in the first code to obtain the logical address of the variable, and then decompile according to the logical address of the relocated variable and the first code, so as to obtain the step of obtaining the second code suitable for the target platform.

[0186] Optionally, the processor 202 may also be configured to execute Figure 3 Other possible implementations in the illustrated embodiment correspond to method steps.

[0187] The computing device 200 described above is illustrated by taking a device located on the end side as an example, and the embodiment of the present application also provides a cloud computing device in a cloud environment 300, such as a central server, etc. The cloud computing device has a similar structure to the computing device 200 on the end side, and has the same function as the computing device 200, that is, the function of cross-platform decompiling of code.

[0188] The coupling in the embodiment of the present application is an indirect coupling or communication connection between devices, modules or modules, which can be electrical, mechanical or other forms, and is used for information exchange between devices, modules or modules. The specific connection medium between the above-mentioned communication interface 203, processor 202 and memory 204 is not limited in the embodiment of the present application. For example, the memory, processor and communication interface can be connected through a bus. The bus can be divided into an address bus, a data bus, a control bus, etc.

[0189] Based on the above embodiments, the embodiments of the present application further provide a computer storage medium, in which a software program is stored, and when the software program is read and executed by one or more processors, the method executed by the terminal device and the cloud computing device provided in any one or more of the above embodiments can be implemented. The computer storage medium may include: a U disk, a mobile hard disk, a read-only memory, a random access memory, a magnetic disk or an optical disk, and other media that can store program codes.

[0190] Based on the above embodiments, the present application also provides a chip, which includes a processor, for implementing the functions of the terminal device or cloud computing device involved in the above embodiments, for example, for implementing Figure 1-2The method is performed by the computing device 200 and the cloud computing equipment in the cloud environment 300.

[0191] Optionally, the chip further includes a memory, and the memory is used for storing necessary program instructions and data executed by the processor. The chip can be composed of a chip, or can include a chip and other discrete devices.

[0192] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0193] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0194] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0195] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0196] Obviously, those skilled in the art can make various changes and modifications to the embodiments of the present application without departing from the scope of the embodiments of the present application. Thus, if these modifications and variations of the embodiments of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.

Claims

1. A code processing method, characterized in that: The method comprises: Obtaining a first code, where the first code is a compiled code suitable for a source platform; Relocating the address of the variable associated with the function in the first code to obtain the logical address of the variable; Obtaining a parameter type of a function in the first code; wherein the parameter type is used to indicate whether the parameter is stored in a register or in a stack; According to the difference between the function calling rules of the source platform and the target platform, the register information and / or stack information of the function in the first code is adjusted to obtain an adjusted first code; Decompiling is performed according to the logical address of the variable and the adjusted first code to obtain a second code suitable for the target platform.

2. The method according to claim 1, characterized in that Decompiling according to the logical address of the variable and the adjusted first code to obtain a second code suitable for the target platform includes: Decompiling according to the logical address of the variable and the adjusted first code to obtain a compiler intermediate representation related to the target platform, the compiler intermediate representation including a definition of the variable, the definition of the variable associating a symbol of the variable with the logical address; A second code suitable for the target platform is generated according to the compiler intermediate representation.

3. The method according to claim 2, characterized in that The compiler intermediate representation includes a first variable and a second variable, the first variable has a first logical address, the second variable has a second logical address, and the first logical address and the second logical address are different.

4. The method according to claim 1, characterized in that: The relocating the address of the variable associated with the function in the first code includes: Obtaining a relocation table from the first code, wherein the relocation table stores a logical address of the variable; The addresses of the variables associated with the functions in the first code are relocated according to the relocation table.

5. The method according to claim 4, characterized in that Relocating the address of the variable associated with the function in the first code according to the relocation table includes: Determine a relocation type according to the type of the variable associated with the function in the first code; According to the relocation table, the address of the variable associated with the function in the first code is relocated according to the relocation type.

6. The method according to any one of claims 1 to 5, characterized in that: Before decompiling according to the logical address of the variable and the adjusted first code, the method further includes: According to the difference between the function calling rules of the source platform and the target platform, the parameters of the function in the first code are adjusted.

7. The method according to any one of claims 1 to 5, characterized in that: The obtaining of the first code comprises: A first code input by a user through a graphical user interface is received.

8. A code processing device, characterized in that: The device comprises: A communication module, used for acquiring a first code, where the first code is a compiled code suitable for a source platform; A relocation module, used for relocating the address of the variable associated with the function in the first code to obtain the logical address of the variable; An adjustment module, used for obtaining a parameter type of a function in the first code; wherein the parameter type is used to indicate whether the parameter is stored in a register or in a stack; The adjustment module is further used to adjust the register information or stack information of the function in the first code according to the difference between the function calling rules of the source platform and the target platform, so as to obtain the adjusted first code; The decompiling module is used to perform decompiling according to the logical address of the variable and the adjusted first code to obtain a second code suitable for the target platform.

9. The device according to claim 8, characterized in that The decompilation module is specifically used for: Decompiling according to the logical address of the variable and the first code to obtain a compiler intermediate representation related to the target platform, the compiler intermediate representation including a definition of the variable, the definition of the variable associating a symbol of the variable with the logical address; A second code suitable for the target platform is generated according to the compiler intermediate representation.

10. The device according to claim 9, characterized in that The compiler intermediate representation includes a first variable and a second variable, the first variable has a first logical address, the second variable has a second logical address, and the first logical address and the second logical address are different.

11. The device according to claim 8, characterized in that The relocation module is specifically used for: Obtaining a relocation table from the first code, wherein the relocation table stores a logical address of the variable; The addresses of the variables associated with the functions in the first code are relocated according to the relocation table.

12. The device according to claim 11, characterized in that The relocation module is specifically used for: Determine a relocation type according to the type of the variable associated with the function in the first code; According to the relocation table, the address of the variable associated with the function in the first code is relocated according to the relocation type.

13. The device according to any one of claims 8 to 12, characterized in that The adjustment module is used to adjust the parameters of the function in the first code according to the difference between the function calling rules of the source platform and the target platform before decompiling according to the logical address of the variable and the adjusted first code.

14. The device according to any one of claims 8 to 12, characterized in that The communication module is specifically used for: A first code input by a user through a graphical user interface is received.

15. A computing device, characterized in that: The computing device includes a processor and a memory; The processor is configured to execute instructions stored in the memory so as to enable the computing device to perform the method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Load module compiler

    US20190227779A1

Cited By

  • Code processing method and apparatus, and device

    WO2022068559A1