CN118092938A - Binary translation method, binary translation device, electronic equipment and computer program product - Google Patents

Binary translation method, binary translation device, electronic equipment and computer program product Download PDF

Info

Publication number
CN118092938A
CN118092938A CN202410032997.2A CN202410032997A CN118092938A CN 118092938 A CN118092938 A CN 118092938A CN 202410032997 A CN202410032997 A CN 202410032997A CN 118092938 A CN118092938 A CN 118092938A
Authority
CN
China
Prior art keywords
library function
library
function
program
call
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202410032997.2A
Other languages
Chinese (zh)
Inventor
闫昭煜
曾露
李静
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Loongson Technology Corp Ltd
Original Assignee
Loongson Technology Corp Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Loongson Technology Corp Ltd filed Critical Loongson Technology Corp Ltd
Priority to CN202410032997.2A priority Critical patent/CN118092938A/en
Publication of CN118092938A publication Critical patent/CN118092938A/en
Pending legal-status Critical Current

Links

Classifications

    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Stored Programmes (AREA)

Abstract

The embodiment of the invention provides a binary translation method, a binary translation device, electronic equipment and a computer program product, which are applied to a binary translation system, wherein the method comprises the following steps: when translating and executing a calling instruction to a first library function, calling a second library function under the condition that the first library function is identified to meet a library through condition; the first library function is a library function of a client platform, the second library function is a library function of a host platform, and the first library function is matched with the second library function; executing a rollback operation on the second library function in the process of executing the second library function under the condition that the second library function is identified to meet a rollback condition; the rollback operation refers to a return to a call address of the first library function to translate the first library function. The embodiment of the invention can ensure the correctness of program operation on the basis of reducing the operation cost of translation execution and improving the program operation efficiency.

Description

Binary translation method, binary translation device, electronic equipment and computer program product
Technical Field
The present invention relates to the field of computer technologies, and in particular, to a binary translation method, a binary translation device, an electronic device, and a computer program product.
Background
Binary translation may convert a source program running in one architecture (the guest platform) to a target program running in another architecture (the host platform) to solve the problem of binary-level application running across instruction set architectures.
In a binary translation scenario, a guest program (client platform program) is translated and executed on a host platform, when the guest program needs to call a library function, the guest program usually calls a function in a backup library file of a source architecture in a local platform, that is, calls the library function of the guest platform, and therefore the guest program, a dynamic linker of the guest program and the library function to be called by the guest program are required to be translated and executed together, translation cost is high, and program operation efficiency is affected. The execution efficiency of the library functions can be improved and the translation overhead of the library functions can be eliminated through the library through. The library direct-through function refers to that the library function (called a local library function) of the host platform can be directly called in the process of translating and executing on the host platform by the guest program, and the library direct-through function jumps to the local library function to execute without translating and executing the library function of the guest platform, so that the execution efficiency of the library function can be improved, and the translation cost of the library function can be eliminated.
However, when the library pass-through function is used, the called local library function cannot guarantee whether the execution can be correctly performed, and the next instruction returned to the called library function after the execution of the local library function is completed continues the translation execution, so that the correctness of the program execution is affected.
Disclosure of Invention
In view of the above problems, embodiments of the present invention are provided to provide a binary translation method for overcoming the above problems or at least partially solving the above problems, which can ensure the correctness of program operation on the basis of reducing the operation cost of translation execution and improving the program operation efficiency.
Correspondingly, the embodiment of the invention also provides a binary translation device, electronic equipment and a computer program product, which are used for ensuring the realization and the application of the method.
In a first aspect, an embodiment of the present invention discloses a binary translation method, applied to a binary translation system, where the binary translation system is configured to translate a source code into a target code, where the source code is a binary code of a client platform program, and the target code is a binary code of a host platform, and the method includes:
When translating and executing a calling instruction to a first library function, calling a second library function under the condition that the first library function is identified to meet a library through condition; the first library function is a library function of a client platform, the second library function is a library function of a host platform, and the first library function is matched with the second library function;
Executing a rollback operation on the second library function in the process of executing the second library function under the condition that the second library function is identified to meet a rollback condition; the rollback operation refers to a return to a call address of the first library function to translate the first library function.
In a second aspect, an embodiment of the present invention discloses a binary translation device, which is applied to a binary translation system, where the binary translation system is configured to translate a source code into an object code, where the source code is a binary code of a client platform program, and the object code is a binary code of a host platform, and the device includes:
the library through call module is used for calling a second library function under the condition that the first library function is identified to meet the library through condition when the call instruction to the first library function is translated and executed; the first library function is a library function of a client platform, the second library function is a library function of a host platform, and the first library function is matched with the second library function;
The library through rollback module is used for executing rollback operation on the second library function under the condition that the second library function is identified to meet rollback conditions in the process of executing the second library function; the rollback operation refers to a return to a call address of the first library function to translate the first library function.
In a third aspect, an embodiment of the present invention discloses an electronic device, including: the device comprises a processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface complete communication with each other through the communication bus; the memory is configured to store at least one executable instruction that causes the processor to perform the steps of the binary translation method as described in any one of the preceding claims.
In a fourth aspect, an embodiment of the present invention discloses a readable storage medium, where a program or an instruction is stored, where the program or the instruction can implement the binary translation method according to any one of the embodiments of the present invention when executed by a processor.
In a fifth aspect, embodiments of the present invention disclose a computer program product comprising a computer program which, when executed by a processor, performs the steps of a binary translation method as described in any of the preceding claims.
The embodiment of the invention has the following advantages:
The binary translation method of the present invention provides a fallback (fallback) mechanism. In the process of translating and executing the client platform program by the host platform, when translating and executing a call instruction to a first library function, if the first library function is identified to meet the library pass-through condition, a second library function is called to realize the library pass-through function. In the process of executing the second library function, if the second library function is identified to meet the rollback condition, the call address of the first library function can be obtained, so that rollback operation is executed based on the call address of the first library function, and the first library function is translated and executed at the call address of the first library function. By the embodiment of the invention, under the condition that the library pass-through is successful, the operation cost of translation execution can be reduced, and the program operation efficiency is improved. And under the condition that the library through fails, executing the rollback operation on the second library function called by the library through, and returning to the calling address of the corresponding first library function to execute the first library function in a translation mode, so that the correctness of program operation can be ensured.
Drawings
FIG. 1 is a flow chart of steps of an embodiment of a binary translation method of the present invention;
FIG. 2 is a schematic diagram of the present invention implementing a library pass-through operation on dlsym library functions;
FIG. 3 is a schematic diagram of a rollback operation of the present invention;
FIG. 4 is a block diagram of a binary translation apparatus embodiment of the present invention;
Fig. 5 is a schematic structural diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
In order that the above-recited objects, features and advantages of the present invention will become more readily apparent, a more particular description of the invention will be rendered by reference to the appended drawings and appended detailed description.
The terms first, second and the like in the description and in the claims, are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used may be interchanged, as appropriate, such that embodiments of the present invention may be implemented in sequences other than those illustrated or described herein, and that the objects identified by "first," "second," etc. are generally of a type, and are not limited to the number of objects, such as the first object may be one or more. Furthermore, the term "and/or" as used in the specification and claims to describe an association of associated objects means that there may be three relationships, e.g., a and/or B, may mean: a exists alone, A and B exist together, and B exists alone. The character "/" generally indicates that the context-dependent object is an "or" relationship. The term "plurality" in embodiments of the present invention means two or more, and other adjectives are similar.
Binary translation may be used to solve the problem of binary-level applications running across an instruction set architecture (Instruction Set Architecture, ISA). In particular, binary translation techniques may translate sequences of instructions in one instruction set architecture into sequences of instructions in another instruction set architecture. For example, binary translation may translate instruction sequences in an x86 instruction set architecture into instruction sequences in an arm instruction set architecture (e.g., arm 64), and vice versa. Binary translation includes both static translation and dynamic translation. Static translation is the translation of binary A on the source platform to translate the binary A into binary file B on the target platform prior to its execution. Based on the static translation technology, the translation result obtained by one translation can be used for a plurality of times. Dynamic translation is the translation of fragments that are executed when a binary is run. The binary translation method of the embodiment of the invention can be applied to dynamic translation, and the translation is performed by taking basic blocks as units. A basic block typically ends with control flow change instructions (e.g., jumps, function calls, etc.).
In the embodiment of the present invention, the source program refers to a program to be binary translated, that is, a client platform program, and the source program may be any type of application program. A source platform refers to a machine platform that can run a source program. In some embodiments, the source platform is also referred to as a client platform (guest platform). The processor of the source platform may be a processor based on a first instruction set architecture. The first instruction set architecture may be, for example, an x86 instruction set architecture or an arm instruction set architecture (e.g., arm 64). The source program is a program developed based on the first instruction set architecture, and thus, the source program can be normally run on the source platform. The target platform is a machine platform where migration of source programs is desired. In some embodiments, the target platform is also referred to as a host platform (host platform). The processor of the target platform may be a processor based on a second instruction set architecture. The second instruction set architecture is a different instruction set architecture than the first instruction set architecture. For example, the first instruction set architecture is the x86 instruction set architecture and the second instruction set architecture is the arm instruction set architecture. As another example, the first instruction set architecture is an arm instruction set architecture and the second instruction set architecture is an x86 instruction set architecture. For another example, the first instruction set architecture is the x86 instruction set architecture and the second instruction set architecture is LoongArch (Dragon architecture). The source program may run on the source platform using source binary code. The source binary code is code based on a first instruction set architecture. If it is desired to run the source program on the target platform, the source binary needs to be translated into target binary. The target binary is code based on the second instruction set architecture such that the target binary can be run on the target platform.
FIG. 1 shows a flow chart of steps of an embodiment of a binary translation method of the present invention, applied to a binary translation system for translating source code into object code, the source code being the binary code of a client platform program and the object code being the binary code of a host platform, the method may include the steps of:
Step 101, when translating and executing a calling instruction to a first library function, calling a second library function under the condition that the first library function is identified to meet a library through condition; the first library function is a library function of a client platform, the second library function is a library function of a host platform, and the first library function is matched with the second library function;
102, executing a rollback operation on the second library function under the condition that the second library function is identified to meet a rollback condition in the process of executing the second library function; the rollback operation refers to a return to a call address of the first library function to translate the first library function.
The binary translation method provided by the invention can be used for a dynamic binary translation scene. The flow of dynamic binary translation is as follows: the translator divides the source program into individual basic blocks TB according to jump instructions as dividing points, the end of each basic block is the jump instruction in the source program, and the binary translation system works by taking the basic blocks as basic units. And disassembling codes of the basic block to be translated, calling a corresponding translation function after the disassembly is completed, translating codes of a source instruction set into codes of a target instruction set, storing the codes in a target code cache after the translation is completed, and then executing the translated basic block. When the instruction execution of one basic block is finished, the program flow returns to the translator, and the next basic block of the source program is translated until the source program is executed to exit logic of the program.
In practical applications, in order to limit the program size and solve the problems of wasting storage space and difficulty in updating modules, dynamic links are generally used to divide the modules of the program into separate files, so that they are not linked together statically, and the linking process is deferred until the operation is performed. A dynamic library is one such program module.
Dynamic libraries are also known as shared libraries or dynamically linked libraries. And the file is a so file in a Linux environment. Dynamic libraries are loaded through dynamic links while the program is running. When one application loads one dynamic library, other applications can still load the same dynamic library. The functions in the dynamic library are called library functions.
The following code is an example of a program calling library function, and after compiling, the program will call the strcpy library function and the strcmp library function in the system libc library when running in the operating system.
#include<string.h>
int main()
{
char str1[15];
char str2[15];
int ret;
strcpy(str1,"abcdef");
strcpy(str2,"ABCDEF");
ret=strcmp(str1,str2);
return(0);
}
In a binary translation scenario, a dynamically linked source program (such as the source program in the example above) will perform translation execution. And when the calling instruction to the first library function is translated and executed, if the first library function is identified to meet the library through condition, calling a second library function. The first library function is a library function of the client platform, the second library function is a library function of the host platform, and the first library function is matched with the second library function.
The embodiment of the invention does not limit the type of the first library function. Illustratively, the first library function may include a string processing function such as strstr, strcpy, strcmp, strlen; as another example, the first library function may comprise a dlsym, dlopen, dlerror, dlclose, fopen, malloc, sin or other system library function; for another example, the first library function may include an initialization function of a library, such as an initialization function glewinit of an extended library libglew.
The second library function is a library function of the host platform and is matched to the first library function. The client platform program can directly call the second library function of the host platform in the process of translation execution on the host platform, jump to the second library function of the host platform for execution, and do not call the first library function by the dynamic linker, so that the translation cost brought by the translation of the first library function by the translator can be reduced, the times of context switching in translation execution are saved, and the program running efficiency is improved. For ease of description, embodiments of the present invention refer to the direct invocation of a second library function of a host platform by a client platform program during translation execution on the host platform as library pass-through.
The embodiment of the invention does not limit the method for identifying whether the first library function meets the library through condition. For example, a preset mapping table may be pre-established, where function symbols of library functions (first library functions) of the client platform that satisfy the library through condition are stored in the preset mapping table, and related information of second library functions that are matched with the first library functions that satisfy the library through condition may be recorded. Thus, the preset mapping table can be queried based on the function symbol of the first library function, and if related information of a second library function matched with the function symbol of the first library function exists in the preset mapping table, the first library function is determined to meet a library through condition. Wherein, the related information can include, but is not limited to, the following information: the function symbols, parameters and return values of the second library function.
For example, when the first library function (such as the strcmp library function and the strcpy library function of the client platform) is called in the code, the strcmp library function and the strcpy library function of the client platform are typically executed in a translation mode. The embodiment of the invention can directly call the second library function (the strcmp library function and the strcpy library function of the host platform) under the condition that the strcmp library function and the strcpy library function of the client platform meet the library through condition. Therefore, the source program can directly call the local library function under the binary translation scene, so that the program translation overhead can be avoided and the code execution quality can be improved.
However, the library pass-through operation may occur that the same effect of the client platform library function cannot be achieved after the execution of the local library function is completed, so that the correctness of the program cannot be ensured. For example, for library functions dlopen and dlsym, dlopen the function of the library function is to open a dynamic library specified by the load so that symbols (e.g., functions, variables, etc.) in the loaded dynamic library can be used by the program. The function of the dlsym library functions is to dynamically parse and use the symbols in the loaded dynamic library. If the library pass-through operation is performed when the source program calls the dlsym library function of the client platform, that is, directly calls the dlsym library function of the host platform, when the symbol searched by the program is not locally available or cannot be returned to the source program for use, the symbol searched by the dlsym library function of the execution client platform needs to be translated, but since the library pass-through operation is performed, the dlsym library function of the host platform is directly returned to the next instruction after the program call instruction (call instruction) is performed, the program considers that the call operation of the dlsym library function is completed and does not directly call the dlsym library function of the execution client platform for the symbol in the client platform, and therefore, the dlsym library function of the host platform is directly called by using the library pass-through function in this case, and the same effect of the dlsym library function of the translation execution client platform is not achieved, thereby affecting the correctness of the program.
It can be seen that after the library pass-through operation is performed, the library function execution of the host platform is returned to complete the function call process. At this time, the library pass-through operation crosses the operation of the library function of the original translation execution client platform, and although the operation cost of the translation execution can be reduced, if the library pass-through operation cannot guarantee the correct program execution, the library function of the translation execution client platform is not returned at this time.
To solve this problem, the binary translation method of the present invention provides a fallback (fallback) mechanism. Under the binary translation scene, under the condition of realizing the library direct-connection function, if the second library function which is identified to be called meets the rollback condition, rollback operation is executed on the second function, and the first library function is translated and executed at the calling address of the corresponding first library function so as to ensure the correctness of the program.
Performing the rollback operation requires obtaining the call address of the first library function. The embodiment of the invention does not limit the method for acquiring the call address of the first library function.
In an alternative embodiment of the present invention, the method may further include:
triggering and operating a dynamic linker when translating and executing a call instruction to the first library function;
acquiring call information of the first library function through the dynamic linker, wherein the call information comprises a call address of the client platform program for calling the first library function;
And transferring the calling information of the first library function to the second library function.
The normal running of the dynamically linked program in the operating system depends on the dynamic linker existing in the operating system, the dynamic linker is the key for dynamically linking the program when the program runs, and the acquisition of the program to the library function address can be completed by dynamically linking the running program through the dynamic linker so as to carry out correct call.
When the client platform program calls the first library function, triggering a dynamic linker running the client platform program, and when the dynamic linker performs a search operation on the called first library function, obtaining call information of the first library function, wherein the call information comprises a call address of the client platform program for calling the first library function, and of course, the call information can also comprise information such as function symbols of the first library function. Therefore, the embodiment of the invention can acquire the call information of the first library function through the dynamic linker, wherein the call information comprises the call address of the first library function.
Further, after the call information of the first library function is obtained, the call information of the first library function can be saved, so that the call information of the first library function can be transferred to the second library function when the second library function is called, and the second library function can be used in a subsequent rollback operation. The embodiment of the invention does not limit the way of transferring the call information of the first library function to the second library function. For example, it may be passed through in the form of parameters, or it may be passed through in the form of global variables, etc.
In the dynamic binary translation scenario, if a library pass-through operation is performed when a call instruction of a first library function is translated and executed, a second library function is directly called, and in the process of executing the second library function, if the second library function is identified to meet a rollback condition, a call address of the first library function can be obtained, so that the rollback operation is executed based on the call address of the first library function, and the first library function is translated and executed at the call address of the first library function.
Further, the embodiment of the invention provides two alternative implementation methods for executing the rollback operation on the second library function. Scheme one is implemented by changing the return address of the second library function. Scheme two is implemented by using a function pointer in the second library function.
In particular implementations, when the program is running, the operating system allocates a program stack for each process to store one or more of the following information: parameters of function call, address of function return, local variable and function return value when program runs. The return of the function follows the call standard of the function, is a regular indirect jump, presses the return address of the function to the stack top of the program stack when each function call is executed, and takes the return address from the stack top after the function execution is finished, and jumps to the called position for continuous execution.
For example, when the function func_a is executed in a certain place, the function func_b is called, and before the function func_a calls the function func_b, parameters required by the function func_b are stored in a program stack in advance; then executing call func_B instruction to call function func_B, pressing the return address of function func_B onto the stack top of the program stack, and then executing the instruction of function func_B; after the execution of the function func_b is completed, a return instruction (ret) is executed, and at this time, the next instruction of the call instruction calling func_b in the function func_a is skipped to continue to execute according to the return address of the function func_b in the stack top of the program stack, and the return address of the function func_b is not needed any more, so that the function stack is popped up.
In the implementation of the invention, when a call instruction to a first library function is translated and executed, a return address of the first library function is pushed to the stack top of a program stack; and when the first library function returns, the return address of the first library function is fetched from the stack top, and the next instruction is translated and executed by jumping to the return address.
Referring to FIG. 2, a schematic diagram of implementing a library pass-through operation on dlsym library functions in an embodiment of the present invention is shown. As shown in FIG. 2, when a call instruction is used to call a first library function (e.g., dlsym library functions of a client platform) in a source program, the return address of the first library function is pushed to the top of the stack, which points to the next instruction of the call instruction. In this example, since the first library function satisfies the library pass-through condition, a second library function (e.g., dlsym library functions of the host platform, i.e., the native dlsym library functions) is invoked. As can be seen from FIG. 2, when the second library function completes the return, the return address is obtained from the stack top, and then the return address is returned to the address of the next instruction of the call instruction, and the call process of the dlsym library function is completed after the return. That is, the library pass-through operation goes beyond the translation execution of dlsym library functions of the source platform. In order to enable the second library function to translate the first library function back to the call address of the first library function when the second library function is identified to meet the rollback condition, the following two alternatives are provided in the embodiment of the present invention.
Scheme one: in an alternative embodiment of the present invention, the method may further include:
pushing the calling address of the first library function to the stack top of a program stack;
the performing a rollback operation on the second library function may include:
and when the second library function returns, the calling address of the first library function is fetched from the stack top of the program stack, and the first library function is translated and executed at the calling address.
After the call address of the first library function is obtained, if the second library function is identified to meet the rollback condition, the call address of the first library function can be pushed to the stack top of the program stack. Because the return address is read from the stack top when the second library function returns, the embodiment of the invention can change the address of the next instruction returned to the call instruction into the call address of the first library function by changing the return address of the second library function.
Referring to fig. 3, a schematic diagram of a rollback operation according to an embodiment of the present invention is shown. As shown in fig. 3, the rollback operation mechanism of the first aspect of the embodiment of the present invention is implemented, so that translation execution can be performed at the first library function returned to the calling client station when the rollback condition is satisfied. As shown in fig. 3, during execution of the second library function (e.g., dlsym library functions of the host platform), if the second library function is identified as satisfying the rollback condition, the call address of the first library function (e.g., dlsym library functions of the client platform) is pushed to the stack top, so that when the second library function executes to return to the ret instruction, the return address fetched from the stack top is the call address of the first library function, but not the address of the next instruction of the call instruction. Thus, upon return of the second library function, the first library function is executed in translation at the call address returned to the first library function. When the translation execution of the first library function is completed, the first library function returns, and the return address fetched from the stack top is the address of the next instruction of the call instruction, so that the next instruction returned to the call instruction can continue the translation execution. The rollback operation of the second library function ends, and the program continues the translation execution.
Scheme one is based on a mechanism for acquiring the address of the next basic block after the jump instruction is cut in a binary translation system and a mechanism for modifying the return address in a program stack. In the process of executing the second library function, the embodiment of the invention increases the operation of pressing the calling address of the first library function into the stack top under the condition that the second library function is identified to meet the rollback condition, thereby changing the return address of the second library function and further realizing the rollback operation of the second library function.
Scheme II: in an alternative embodiment of the present invention, the performing a rollback operation on the second library function may include:
and executing the first library function in the second library function through translation of a function pointer call translator, wherein the function pointer points to a call address of the first library function.
Function pointers are pointer variables that point to a function. Function pointers have two uses: calling the function and doing the parameters of the function. When the embodiment of the invention translates the calling instruction which is executed to the first library function, if the first library function is identified to meet the library through condition, the second library function is called. In the process of executing the second library function, if the second library function is identified to meet the rollback condition, the translator is called through a function pointer to translate and execute the first library function in the second library function, and the function pointer is pointed to the call address of the first library function because the call address of the first library function is acquired, so that the first library function can be translated and executed by calling the translator in the second library function. When the execution of the second library function is completed and returns, the return address fetched from the stack top is the address of the next instruction of the call instruction, so that the next instruction returned to the call instruction can continue to translate and execute. The rollback operation of the second library function ends, and the program continues the translation execution.
In an optional embodiment of the invention, the first library function is used for operating on an object indicated by a function parameter, and the method may further comprise:
when a second library function is called, transferring the function parameters of the first library function to the second library function;
the identifying that the second library function satisfies a rollback condition may include:
the second library function searches the host platform for the object not indicated by the function parameter.
In the embodiment of the invention, the second library function meets the rollback condition, which means that the same effect of executing the first library function in translation cannot be realized by calling the second library function through library pass-through. In this case, a rollback operation may be performed on the second library function during execution of the second library function, returning to the translation of the first library function at the call address of the first library function.
In an implementation, the function call includes a parametric function call and a non-parametric function call. For function call with parameters, the embodiment of the invention can transfer the function parameters of the first library function to the second library function when the second library function is called. The first library function is used for operating the object indicated by the function parameter. Illustratively, the first library function may be dlopen library functions, dlopen library functions being used to open a load-specific dynamic library. The dlopen library functions are defined as follows:
void*dlopen(const char*filename,int flags)
The parameter filename is used to specify a path of the dynamic library file to be loaded, and may be an absolute path or a relative path. Parameters flags are used to specify the manner of loading. When the dlopen library function is called, the system will find the dynamic library file according to the specified path and load it into the memory. The return value of dlopen library functions is a handle for subsequent operations on the dynamic library file.
As another example, the first library function may be dlsym library functions, dlsym library functions are used to dynamically parse and use symbols in the loaded dynamic library. The dlsym library functions are defined as follows:
void*dlsym(void*handle,const char*symbol)
The parameter handle is a handle (a pointer named handle) returned through dlopen and points to the loaded dynamic library file. The parameter symbol is the name of the symbol (which may be a variable or a function) to be looked up in the dynamic library file being loaded.
In the executing process of the second library function, if no object indicated by the function parameter exists in the host platform, it can be determined that the second library function meets the rollback condition. The object indicated by the function parameter includes, but is not limited to, a file or symbol indicated by the function parameter, etc.
Take call dlsym library functions as an example. In the case of implementing library pass-through, a second library function (dlsym library functions local to the host platform) is called, and in example one, assuming that a function parameter handle received by the second library function points to a dynamic library file loaded by dlopen library functions local to the host platform, the second library function may use the function parameter handle to search a symbol specified by a symbol parameter in the locally loaded dynamic library file. Therefore, the same effect of executing the first library function in translation can be realized by calling the second library function through the library, the rollback condition is not satisfied, and the library pass operation is successful.
In example two, assuming that the function parameter handle received by the second library function points to the dlopen library function loaded dynamic library file of the client platform, the function parameter handle cannot be used in the host platform, and the second library function cannot find the symbol specified by the symbol parameter locally by using the function parameter handle. Therefore, the same effect of executing the first library function by translation cannot be achieved by the library through call of the second library function, and it is determined that the second library function in example two satisfies the return condition. At this point a rollback operation may be performed, with the library pass-through operation failing.
It should be noted that, the embodiment of the present invention does not limit the rollback condition, and as long as the same effect of executing the first library function in translation cannot be achieved by calling the second library function through library pass-through, the second library function can be considered to satisfy the rollback condition.
In an alternative embodiment of the present invention, the identifying that the second library function meets a rollback condition may include:
The second library function is a function for initializing an extended library for the client platform.
And initializing library functions of some libraries of the client platform, and if library pass-through operation is performed, initializing the libraries of the client platform is not realized, so that the correctness of the program is affected. For example, the glewinit library function initializing the extended library libglew.so recognizes that the rollback condition is satisfied when library pass-through is performed, so as to perform rollback operation, and translate glewinit library functions of the execution client platform to perform the initialization operation on the extended library libglew.so of the client platform.
In an alternative embodiment of the present invention, before the calling the second library function, the method may further include:
Storing the current relevant register state of the client platform program; the relevant registers comprise parameter transmission registers of the client platform;
after the second library function is called, the method may further include:
restoring the saved relevant register state.
Further, before the second library function is called, the current relevant register state of the client platform program needs to be saved, the behavior of the transfer parameters and the return values of the first library function is simulated on the host platform, and then the second library function is skipped to the local host platform for execution, so that the library pass-through function is realized. Before the second library function is called, the current relevant register state of the client platform program is saved, and after the second library function is called, the saved relevant register state is restored. The relevant register refers to a parameter transmission register of the client platform, and because the register state is the execution state of the client platform program, if the execution of the library function of the host platform is skipped, the relevant register state of the client platform may be covered, so that the current relevant register state of the client platform program needs to be saved first, and the recovery is performed after the completion of function call.
It should be noted that, the current relevant register state of the client platform program is saved and the saved relevant register state is restored, that is, the context switch operation shown in fig. 2 and 3.
Because of the different instruction architectures of the client platform and the host platform, the ABI (Application Binary Interface ) is different, and parameters and return value transfer conventions of a first library function of the client platform need to be simulated on the host platform in the process of calling a second library function of the host platform. I.e., converting the function call ABI of the client platform to the function call ABI of the host platform. When there is a return value for the called function, the ABI returned by the host platform needs to be converted to the ABI returned by the client platform. Specifically, a stack of the client platform needs to be simulated, and function parameters of a first library function of the client platform need to be pushed into the simulated stack before a second library function of the host platform is called; then, the function parameters are taken out and put into a parameter transfer register and a stack of the host platform according to the function parameter transfer convention of the second library function of the host platform; calling a second library function of the host platform; and taking out the return value according to the return value transfer convention of the second library function of the host platform, and then putting the return value into the simulation register of the client platform according to the return value transfer convention of the first library function of the client platform.
In summary, the binary translation method of the present invention provides a fallback (fallback) mechanism. In the process of translating and executing the client platform program by the host platform, when translating and executing a call instruction to a first library function, if the first library function is identified to meet the library pass-through condition, a second library function is called to realize the library pass-through function. In the process of executing the second library function, if the second library function is identified to meet the rollback condition, the call address of the first library function can be obtained, so that rollback operation is executed based on the call address of the first library function, and the first library function is translated and executed at the call address of the first library function. By the embodiment of the invention, under the condition that the library pass-through is successful, the operation cost of translation execution can be reduced, and the program operation efficiency is improved. And under the condition that the library through fails, executing the rollback operation on the second library function called by the library through, and returning to the calling address of the corresponding first library function to execute the first library function in a translation mode, so that the correctness of program operation can be ensured.
It should be noted that, for simplicity of description, the method embodiments are shown as a series of acts, but it should be understood by those skilled in the art that the embodiments are not limited by the order of acts, as some steps may occur in other orders or concurrently in accordance with the embodiments. Further, those skilled in the art will appreciate that the embodiments described in the specification are presently preferred embodiments, and that the acts are not necessarily required by the embodiments of the invention.
Referring to FIG. 4, there is shown a block diagram of an embodiment of a binary translation apparatus of the present invention, the apparatus being applied to a binary translation system for translating source code, which is the binary of a client platform program, into target code, which is the binary of a host platform, the apparatus may include:
A library pass-through calling module 401, configured to, when translating a call instruction executed to a first library function, call a second library function if the first library function is identified to satisfy a library pass-through condition; the first library function is a library function of a client platform, the second library function is a library function of a host platform, and the first library function is matched with the second library function;
A library pass-through rollback module 402, configured to perform a rollback operation on the second library function when identifying that the second library function satisfies a rollback condition during the process of executing the second library function; the rollback operation refers to a return to a call address of the first library function to translate the first library function.
Optionally, the apparatus further comprises:
The address saving module is used for pushing the calling address of the first library function to the stack top of the program stack;
The library straight-through rollback module is specifically configured to:
and when the second library function returns, the calling address of the first library function is fetched from the stack top of the program stack, and the first library function is translated and executed at the calling address.
Optionally, the library pass-through rollback module is specifically configured to:
and executing the first library function in the second library function through translation of a function pointer call translator, wherein the function pointer points to a call address of the first library function.
Optionally, the first library function is used for operating an object indicated by a function parameter, and the apparatus further includes:
The parameter transfer module is used for transferring the function parameters of the first library function to the second library function when the second library function is called;
The library pass-through rollback module includes:
and the first identification sub-module is used for determining that the second library function meets a rollback condition when the second library function searches for the object which does not exist the function parameter indication in the host platform.
Optionally, the library pass-through rollback module includes:
And the second identification sub-module is used for determining that the second library function meets a rollback condition when the second library function is a function for initializing an expansion library of the client platform.
Optionally, the device further comprises an address acquisition module, specifically configured to:
Triggering and operating a dynamic linker when translating and executing a call instruction to the first library function; acquiring call information of the first library function through the dynamic linker, wherein the call information comprises a call address of the client platform program for calling the first library function; and transferring the calling information of the first library function to the second library function.
Optionally, the apparatus further comprises:
the state saving module is used for saving the current relevant register state of the client platform program before the second library function is called; the relevant registers comprise parameter transmission registers of the client platform;
and the state recovery module is used for recovering the saved state of the relevant register after the second library function is called.
The binary translation apparatus of the present invention provides a fallback (fallback) mechanism. In the process of translating and executing the client platform program by the host platform, when translating and executing a call instruction to a first library function, if the first library function is identified to meet the library pass-through condition, a second library function is called to realize the library pass-through function. In the process of executing the second library function, if the second library function is identified to meet the rollback condition, the call address of the first library function can be obtained, so that rollback operation is executed based on the call address of the first library function, and the first library function is translated and executed at the call address of the first library function. By the embodiment of the invention, under the condition that the library pass-through is successful, the operation cost of translation execution can be reduced, and the program operation efficiency is improved. And under the condition that the library through fails, executing the rollback operation on the second library function called by the library through, and returning to the calling address of the corresponding first library function to execute the first library function in a translation mode, so that the correctness of program operation can be ensured.
For the device embodiments, since they are substantially similar to the method embodiments, the description is relatively simple, and reference is made to the description of the method embodiments for relevant points.
Referring to fig. 5, a schematic structural diagram of an electronic device according to an embodiment of the present invention is shown. As shown in fig. 5, the electronic device includes: the device comprises a processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface complete communication with each other through the communication bus; the memory is configured to store at least one executable instruction that causes the processor to perform the steps of the binary translation method of the foregoing embodiment.
An embodiment of the present invention provides a non-transitory computer readable storage medium, which when executed by a program or a processor of a terminal, enables the terminal to perform the steps of the binary translation method of any of the foregoing embodiments.
An embodiment of the invention discloses a computer program product comprising a computer program which, when executed by a processor, performs the steps of the binary translation method of any of the previous embodiments.
In this specification, each embodiment is described in a progressive manner, and each embodiment is mainly described by differences from other embodiments, and identical and similar parts between the embodiments are all enough to be referred to each other.
It will be apparent to those skilled in the art that embodiments of the present invention may be provided as a method, apparatus, or computer program product. Accordingly, embodiments of the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, embodiments of the invention may take the form of a computer program product on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) having computer-usable program code embodied therein.
Embodiments of the present invention are described with reference to flowchart illustrations and/or block diagrams of methods, terminal devices (systems), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flowchart illustrations and/or block diagrams, and combinations of flows and/or blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing terminal device to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing terminal device, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
Finally, it is further noted that relational terms such as first and second, and the like are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Moreover, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or terminal that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or terminal. Without further limitation, an element defined by the phrase "comprising one … …" does not exclude the presence of other like elements in a process, method, article, or terminal device that comprises the element.
The principles and embodiments of the present invention have been described herein with reference to specific examples, the description of which is intended only to assist in understanding the methods of the present invention and the core ideas thereof; meanwhile, as those skilled in the art will have variations in the specific embodiments and application scope in accordance with the ideas of the present invention, the present description should not be construed as limiting the present invention in view of the above.

Claims (10)

1. A binary translation method, applied to a binary translation system, the binary translation system configured to translate source code into target code, the source code being a binary code of a client platform program, the target code being a binary code of a host platform, the method comprising:
When translating and executing a calling instruction to a first library function, calling a second library function under the condition that the first library function is identified to meet a library through condition; the first library function is a library function of a client platform, the second library function is a library function of a host platform, and the first library function is matched with the second library function;
Executing a rollback operation on the second library function in the process of executing the second library function under the condition that the second library function is identified to meet a rollback condition; the rollback operation refers to a return to a call address of the first library function to translate the first library function.
2. The method according to claim 1, wherein the method further comprises:
pushing the calling address of the first library function to the stack top of a program stack;
the performing a rollback operation on the second library function includes:
and when the second library function returns, the calling address of the first library function is fetched from the stack top of the program stack, and the first library function is translated and executed at the calling address.
3. The method of claim 1, wherein performing a rollback operation on the second library function comprises:
and executing the first library function in the second library function through translation of a function pointer call translator, wherein the function pointer points to a call address of the first library function.
4. The method of claim 1, wherein the first library function is used to operate on objects indicated by function parameters, the method further comprising:
when a second library function is called, transferring the function parameters of the first library function to the second library function;
the identifying that the second library function satisfies a rollback condition includes:
the second library function searches the host platform for the object not indicated by the function parameter.
5. The method of claim 1, wherein the identifying that the second library function satisfies a rollback condition comprises:
The second library function is a function for initializing an extended library for the client platform.
6. The method according to claim 1, wherein the method further comprises:
triggering and operating a dynamic linker when translating and executing a call instruction to the first library function;
acquiring call information of the first library function through the dynamic linker, wherein the call information comprises a call address of the client platform program for calling the first library function;
And transferring the calling information of the first library function to the second library function.
7. The method of claim 1, wherein prior to the invoking the second library function, further comprising:
Storing the current relevant register state of the client platform program; the relevant registers comprise parameter transmission registers of the client platform;
After the second library function is called, the method further comprises:
restoring the saved relevant register state.
8. A binary translation apparatus for use in a binary translation system for translating source code into object code, the source code being the binary code of a client platform program and the object code being the binary code of a host platform, the apparatus comprising:
the library through call module is used for calling a second library function under the condition that the first library function is identified to meet the library through condition when the call instruction to the first library function is translated and executed; the first library function is a library function of a client platform, the second library function is a library function of a host platform, and the first library function is matched with the second library function;
The library through rollback module is used for executing rollback operation on the second library function under the condition that the second library function is identified to meet rollback conditions in the process of executing the second library function; the rollback operation refers to a return to a call address of the first library function to translate the first library function.
9. An electronic device, comprising: the device comprises a processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface complete communication with each other through the communication bus; the memory is configured to hold at least one executable instruction that causes the processor to perform the steps of the binary translation method according to any one of claims 1 to 7.
10. A computer program product comprising a computer program, characterized in that the computer program, when being executed by a processor, is adapted to carry out the steps of the binary translation method according to any one of claims 1 to 7.
CN202410032997.2A 2024-01-09 2024-01-09 Binary translation method, binary translation device, electronic equipment and computer program product Pending CN118092938A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202410032997.2A CN118092938A (en) 2024-01-09 2024-01-09 Binary translation method, binary translation device, electronic equipment and computer program product

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202410032997.2A CN118092938A (en) 2024-01-09 2024-01-09 Binary translation method, binary translation device, electronic equipment and computer program product

Publications (1)

Publication Number Publication Date
CN118092938A true CN118092938A (en) 2024-05-28

Family

ID=91158155

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202410032997.2A Pending CN118092938A (en) 2024-01-09 2024-01-09 Binary translation method, binary translation device, electronic equipment and computer program product

Country Status (1)

Country Link
CN (1) CN118092938A (en)

Similar Documents

Publication Publication Date Title
KR101761498B1 (en) Method and apparatus for guest return address stack emulation supporting speculation
EP2082318B1 (en) Register-based instruction optimization for facilitating efficient emulation of an instruction stream
CN111399990B (en) Method and device for interpreting and executing intelligent contract instruction
US6243668B1 (en) Instruction set interpreter which uses a register stack to efficiently map an application register state
CN101308471B (en) Method and device for data restoration
CN111290952A (en) Tracking method and device for dynamic link library function
JP6418696B2 (en) Instruction set simulator and method for generating the simulator
US10248409B1 (en) Limiting the effects of source code patches on corresponding native-code patches
JP2002196937A (en) Method for compiling assembly language code for instruction set architecture including new instruction using conventional assembler
CN115629971A (en) Application development system and method
EP0939370B1 (en) Computer code translator
CN116934330A (en) Method for calling intelligent contract, executing method, computer equipment and storage medium
CN107168719A (en) Application program running method and device, terminal and computer readable storage medium
CN116661808B (en) Binary translation method, binary translation device, electronic equipment and storage medium
CN116501387B (en) Instruction jump method and device, electronic equipment and readable storage medium
CN109558121A (en) Development approach, device, equipment and the storage medium of interface drive program
CN111400256B (en) Method and device for calling resource file by application program
CN118092938A (en) Binary translation method, binary translation device, electronic equipment and computer program product
CN112711427A (en) Method and device for acquiring mirror image file
CN114579141A (en) Method and device for converting require frame into VUE frame
US20030191622A1 (en) Exception handling method and apparatus for use in program code conversion
CN117093229A (en) Binary translation method, binary translation device, electronic equipment and storage medium
US20170357558A1 (en) Apparatus and method to enable a corrected program to take over data used before correction thereof
CN117950675A (en) Binary translation method, binary translation device, electronic equipment and readable storage medium
CN115421875B (en) Binary translation method and device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination