Executable File Processing Method and Device
By obtaining and utilizing relocation tables, the rapid and accurate problem of instance member offset correction in electronic devices is solved, and efficient code correction and memory optimization are achieved.
Patent Information
- Application Number
- CN202010366846.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-04-30
- Publication Date
- 2025-06-17
- Estimated Expiration
- 2040-04-30
AI Technical Summary
In electronic devices, how to quickly and accurately correct the offset of instance members, especially during the installation and update of the system or application.
By obtaining the relocation table corresponding to the executable file, the position of the instruction to be corrected is determined in the executable file, and the corresponding instance member offset is corrected.
This enables quick and accurate correction of offsets to instance members, reducing code overhead and memory space usage, while avoiding additional overhead when the application is running.
Smart Images

Figure CN113590138B_ABST
Abstract
Description
Technical Field
[0001] This application relates to terminal technologies, and in particular, to an executable file processing method and device. Background Art
[0002] A system and various application programs can be arranged in an electronic device. The code of the application programs arranged in the electronic device often includes multiple classes; and, the code of the system usually includes multiple classes.
[0003] A device providing a system or an application program adjusts the classes in the code of the application program or adjusts the classes in the code of the system; when adjusting classes, it will affect the layout of instance members related to the classes. Therefore, when installing the system or the application program, it is necessary to correct the offsets of the instance members in the classes.
[0004] However, how to quickly and accurately correct the offsets of instance members is a problem that needs to be solved. Summary of the Invention
[0005] This application provides an executable file processing method and device to solve the problem of quickly and accurately correcting the offsets of instance members.
[0006] In a first aspect, this application provides an executable file processing method, which is applied to an electronic device. The method includes:
[0007] Obtain a relocation table corresponding to the executable file, where the relocation table is used to indicate at least one instruction with an offset to be corrected;
[0008] According to the relocation table, determine the position of the instruction with the offset to be corrected in the executable file, and determine the instance member corresponding to the instruction with the offset to be corrected;
[0009] Correct the offset of the instance member.
[0010] In an optional manner, the obtaining a relocation table corresponding to the executable file includes:
[0011] When it is determined to update the system, send the information of the executable file to a network device; receive the relocation table corresponding to the information of the executable file sent by the network device;
[0012] Or, send software version information to the network device; receive the relocation table corresponding to the software version information and the executable file sent by the network device.
[0013] In an optional manner, the relocation table includes an instruction offset, an instruction type, and an instance member of the instruction;
[0014] Wherein, the instruction offset is the offset of the instruction with the offset to be corrected in the executable file, and the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected.
[0015] In an optional manner, before correcting the offset of the instance member, it further includes:
[0016] Calculating the current actual offset of the instance member, wherein the actual offset is the offset after correcting the original member offset of the instance member;
[0017] Correcting the offset of the instance member includes: correcting the original member offset of the instance member to the actual offset.
[0018] In an optional manner, when the instance member is an instance member variable, calculating the actual offset of the instance member includes:
[0019] Starting from the highest-level class in the class inherited by the current instance member variable, traversing each class inherited by the current instance member variable, aligning and arranging the instance member variables in each class to obtain the offsets of the instance member variables in each class;
[0020] Determining that the sum of the offset of the previous instance member variable before the current instance member variable, the size of the previous instance member variable, and the size of the space padding between the current instance member variable and the previous instance member variable is the actual offset of the current instance member variable;
[0021] When the instance member is a virtual function, calculating the actual offset of the instance member includes:
[0022] Starting from the highest-level class in the class inherited by the current virtual function, traversing each class inherited by the current virtual function, calculating the offsets of the pointers of the virtual functions in each class to obtain a virtual function table;
[0023] Determining that the sum of the offset of the pointer of the previous virtual function before the current virtual function in the virtual function table and the length of the pointer of the current virtual function is the actual offset of the current virtual function.
[0024] In an optional manner, the relocation table includes an instruction type; the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected, and the encoding method includes an encoding position;
[0025] Correcting the original member offset of the instance member to the actual offset includes:
[0026] Write the actual offset to specific bit positions in the instruction of the offset to be corrected according to the encoding method indicated by the instruction type, where the specific bit positions are the bit positions indicated by the encoding positions in the encoding method.
[0027] In an alternative manner, the instruction includes an opcode and an operand. The opcode is used to indicate the instruction type, and the operand is used to indicate the data to be operated on and the unit address where the data is located.
[0028] In an alternative manner, after correcting the offset of the instance member, it further includes:
[0029] Load and run the corrected executable file, where the corrected executable file runs according to the corrected instructions.
[0030] In an alternative manner, the loading and running the corrected executable file includes:
[0031] Access the instance member according to the corrected instruction.
[0032] In a first aspect, the present application provides an executable file processing method applied to a network device. The method includes:
[0033] Generate a relocation table corresponding to the executable file according to instructions with at least one offset to be corrected. The relocation table is used to indicate the positions of the instructions with the offset to be corrected in the executable file;
[0034] Send the relocation table to an electronic device, where the relocation table is used to correct the offset of the instance member corresponding to the instruction with the offset to be corrected.
[0035] In an alternative manner, sending the relocation table to the electronic device includes:
[0036] Receive the information of the executable file sent by the electronic device when updating the system; send the relocation table corresponding to the information of the executable file to the electronic device;
[0037] Or, receive the software version information sent by the electronic device; send the relocation table corresponding to the software version information and the executable file to the electronic device.
[0038] In an alternative manner, the relocation table includes an instruction offset, an instruction type, and an instance member of the instruction;
[0039] Wherein, the instruction offset is the offset of the instruction with the offset to be corrected in the executable file, and the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected.
[0040] In an optional manner, the relocation table is specifically used to correct the offset of the instance member corresponding to the instruction with the offset to be corrected according to the current actual offset of the instance member;
[0041] Wherein, the actual offset is the offset after correcting the original member offset of the instance member.
[0042] In an optional manner, the instruction includes an opcode and an operand, the opcode is used to indicate the instruction type, and the operand is used to indicate the data to be operated on and the unit address where the data is located.
[0043] On the other hand, the present application provides a processor, which is applied to a terminal device, and the processor is used for:
[0044] Obtain a relocation table corresponding to the executable file, wherein the relocation table is used to indicate at least one instruction with an offset to be corrected;
[0045] Determine the position of the instruction with the offset to be corrected in the executable file according to the relocation table, and determine the instance member corresponding to the instruction with the offset to be corrected;
[0046] Correct the offset of the instance member.
[0047] In an optional manner, when the processor obtains the relocation table corresponding to the executable file, it is specifically used for:
[0048] When determining to update the system, send the information of the executable file to the network device; receive the relocation table corresponding to the information of the executable file sent by the network device;
[0049] Or, send the software version information to the network device; receive the relocation table and the executable file corresponding to the software version information sent by the network device.
[0050] In an optional manner, the relocation table includes an instruction offset, an instruction type, and an instance member of the instruction;
[0051] Wherein, the instruction offset is the offset of the instruction with the offset to be corrected in the executable file, and the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected.
[0052] In an alternative approach, before correcting the offset of the instance member, the processor is further configured to:
[0053] Calculate the current actual offset of the instance member, where the actual offset is the offset after correcting the original member offset of the instance member;
[0054] When the processor corrects the offset of the instance member, it is specifically configured to: correct the original member offset of the instance member to the actual offset.
[0055] In an alternative approach, when the instance member is an instance member variable, when the processor calculates the actual offset of the instance member, it is specifically configured to:
[0056] Starting from the highest-level class in the class inherited by the current instance member variable, traverse each class inherited by the current instance member variable, align and arrange the instance member variables in each class to obtain the offsets of the instance member variables in each class;
[0057] Determine that the sum of the offset of the previous instance member variable before the current instance member variable, the size of the previous instance member variable, and the size of the space padding between the current instance member variable and the previous instance member variable is the actual offset of the current instance member variable;
[0058] When the instance member is a virtual function, when the processor calculates the actual offset of the instance member, it is specifically configured to:
[0059] Starting from the highest-level class in the class inherited by the current virtual function, traverse each class inherited by the current virtual function, calculate the offsets of the pointers of the virtual functions in each class to obtain a virtual function table;
[0060] Determine that the sum of the offset of the pointer of the previous virtual function before the current virtual function in the virtual function table and the length of the pointer of the current virtual function is the actual offset of the current virtual function.
[0061] In an alternative approach, the relocation table includes an instruction type; the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected, and the encoding method includes an encoding position;
[0062] When the processor corrects the original member offset of the instance member to the actual offset, it is specifically configured to:
[0063] According to the encoding method indicated by the instruction type, write the actual offset to the specific bit position in the instruction with the offset to be corrected, and the specific bit position is the bit position indicated by the encoding position in the encoding method.
[0064] In an alternative approach, the instruction includes an opcode and an operand. The opcode is used to indicate the instruction type, and the operand is used to indicate the data to be operated on and the unit address where the data is located.
[0065] In an alternative approach, after the processor corrects the offset of the instance member, it is further configured to:
[0066] Load and run the corrected executable file, where the corrected executable file runs according to the corrected instructions.
[0067] In an alternative approach, when the processor loads and runs the corrected executable file, it is specifically configured to:
[0068] Access the instance member according to the corrected instruction.
[0069] In a fourth aspect, the present application provides a processor, which is applied to a network device. The processor is configured to:
[0070] Generate a relocation table corresponding to the executable file according to at least one instruction with an offset to be corrected. The relocation table is used to indicate the position of the instruction with the offset to be corrected in the executable file;
[0071] Send the relocation table to an electronic device, where the relocation table is used to correct the offset of the instance member corresponding to the instruction with the offset to be corrected.
[0072] In an alternative approach, when the processor sends the relocation table to the electronic device, it is specifically configured to:
[0073] Receive the information of the executable file sent by the electronic device during system update; send the relocation table corresponding to the information of the executable file to the electronic device;
[0074] Alternatively, receive the software version information sent by the electronic device; send the relocation table and the executable file corresponding to the software version information to the electronic device.
[0075] In an alternative approach, the relocation table includes an instruction offset, an instruction type, and an instance member of the instruction;
[0076] Wherein, the instruction offset is the offset of the instruction with the offset to be corrected in the executable file, and the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected.
[0077] In an optional manner, the relocation table is specifically configured to correct the offset of the instance member corresponding to the instruction to be corrected according to the current actual offset of the instance member;
[0078] wherein, the actual offset is the offset obtained by correcting the original member offset of the instance member.
[0079] In an optional manner, the instruction includes an operation code and an operand. The operation code is used to indicate the instruction type, and the operand is used to indicate the data to be operated on and the unit address where the data is located.
[0080] In a fifth aspect, the present application provides an electronic device, which includes: a processor;
[0081] The processor is configured to execute instructions in computer-executable program code; when the processor executes the instructions, the instructions cause the electronic device to execute the method provided in the first aspect.
[0082] In a possible implementation manner, the electronic device further includes a receiver; the receiver is configured to receive the relocation table sent by the network device.
[0083] In a sixth aspect, the present application provides a network device, which includes: a processor and a transmitter;
[0084] The processor is configured to execute instructions in computer-executable program code; when the processor executes the instructions, the instructions cause the electronic device to execute the method provided in the second aspect;
[0085] The transmitter is configured to send the relocation table to the electronic device.
[0086] In a seventh aspect, the present application provides a communication system, which includes a terminal device as described in any item of the third aspect, an electronic device as described in any item of the fourth aspect, and a network device. The network device is configured to receive the software control program and the relocation table provided by the electronic device; the network device may send the software control program and the relocation table to the terminal device.
[0087] In an eighth aspect, the present application provides a terminal device, which includes at least one processing element or chip configured to execute any implementation manner of the first aspect above.
[0088] In a ninth aspect, the present application provides a program, which is configured to execute any implementation manner of the first aspect above when executed by a processor.
[0089] In a tenth aspect, the present application provides a computer-readable storage medium, which includes the program of the ninth aspect.
[0090] In an eleventh aspect, the present application provides an electronic device, including at least one processing element or chip configured to execute any implementation of the above first aspect.
[0091] In a twelfth aspect, the present application provides a program that, when executed by a processor, is configured to execute any implementation of the above second aspect.
[0092] In a thirteenth aspect, the present application provides a computer-readable storage medium including the program of the twelfth aspect.
[0093] By obtaining a relocation table corresponding to an executable file, where the relocation table is used to indicate instructions with at least one offset to be corrected; determining, according to the relocation table, the positions of the instructions with the offsets to be corrected in the executable file, and determining the instance members corresponding to the instructions with the offsets to be corrected; and correcting the offsets of the instance members. When initially installing a system, or initially installing an application program, or updating the system, or updating the application program, an electronic device needs to correct the offsets of instance members in the instructions of the executable file. The electronic device first obtains the relocation table corresponding to the executable file, and then, based on the relocation table, finds the instructions with the offsets to be corrected, and thus can correct the offsets of the instance members in the instructions. The solution provided in this embodiment does not require adding a global variable for each instance member; only a relocation table needs to be provided for each instance member of the application program, and the offsets of the instance members can be corrected according to the information in the relocation table, that is, the instructions for compiling the instance members are corrected, reducing the code overhead, and also reducing the memory space and storage space occupied by the application program; and only the instructions of the instance members that need to be corrected need to be corrected. Since no additional instructions need to be added for accessing the instance members, the instruction overhead can be reduced. And, in the solution provided in this embodiment, since the relocation table can be independent of the application program, the code of the execution process of the solution provided in this embodiment does not need to be arranged in the application program. Therefore, if the solution provided in this embodiment occurs during the upgrade or update of the application program, the runtime overhead of the application program will not be increased. Further, since the relocation table can be independent of the application program, the execution process of the solution provided in this embodiment does not need to be executed during the running of the application program. BRIEF DESCRIPTION OF THE DRAWINGS
[0094] Figure 1 FIG. is a schematic diagram of an application scenario provided by an embodiment of the present application Figure 1 ;
[0095] Figure 2 FIG. is a schematic diagram of the compilation of a class provided in this embodiment Figure 1 ;
[0096] Figure 3Schematic diagram of the compilation of the class provided in this embodiment Figure 2 ;
[0097] Figure 4 Schematic diagram of the offset of instance members provided in this embodiment Figure 1 ;
[0098] Figure 5 Schematic diagram of the compilation of the class provided in this embodiment Figure 3 ;
[0099] Figure 6 Schematic diagram of the offset of instance members provided in this embodiment Figure 2 ;
[0100] Figure 7 Schematic diagram of the compilation of the class provided in this embodiment Figure 4 ;
[0101] Figure 8 Schematic diagram of the compilation of the class provided in this embodiment Figure 5 ;
[0102] Figure 9 Schematic diagram of the offset of instance members provided in this embodiment Figure 2 ;
[0103] Figure 10 Schematic diagram of the compilation of the class provided in this embodiment Figure 6 ;
[0104] Figure 11 Schematic diagram of the offset of instance members provided in this embodiment Figure 4 ;
[0105] Figure 12 Schematic diagram of the compilation of the class provided in this embodiment Figure 7 ;
[0106] Figure 13 Flow chart of a method for processing executable files provided in an embodiment of this application;
[0107] Figure 14 Schematic diagram of a decoupled class provided in an embodiment of this application;
[0108] Figure 15 Schematic diagram of an application scenario provided in an embodiment of this application Figure 2 ;
[0109] Figure 16 Schematic diagram of an application scenario provided in an embodiment of this application Figure 3 ;
[0110] Figure 17 Flow chart of another method for processing executable files provided in an embodiment of this application;
[0111] Figure 18 It is a schematic flowchart of another executable file processing method provided by an embodiment of the present application;
[0112] Figure 19 It is a schematic structural diagram of a processor provided by an embodiment of the present application;
[0113] Figure 20 It is a schematic structural diagram of another processor provided by an embodiment of the present application;
[0114] Figure 21 It is a schematic structural diagram of an electronic device provided by an embodiment of the present application;
[0115] Figure 22 It is a schematic structural diagram of a network device provided by an embodiment of the present application. Detailed implementation manners
[0116] The embodiments of the present application are applied to an electronic device. The electronic device includes but is not limited to: a terminal device, a network device, and a server. Among them, the terminal device may be a terminal device in the prior art or a terminal device that appears in the future; the network device may be a terminal device in the prior art or a network device that appears in the future.
[0117] Moreover, the manner in which the electronic device obtains the software control program and the relocation table from the network may adopt a communication system in the prior art, or a fifth-generation (5G) mobile communication network system, or other systems that may emerge in the future, or other communication systems. For example: wireless local area network (WLAN) system, global system of mobile communication (GSM) system, code division multiple access (CDMA) system, wideband code division multiple access (WCDMA) system, general packet radio service (GPRS), long term evolution (LTE) system, LTE frequency division duplex (FDD) system, LTE time division duplex (TDD), universal mobile telecommunication system (UMTS), worldwide interoperability for microwave access (WIMAX) communication system, new radio (NR), and so on.
[0118] The following explains some terms in this application to facilitate understanding by those skilled in the art. It should be noted that when the solution of the embodiment of this application is applied to an electronic device, the name of the electronic device may change, but this does not affect the implementation of the solution of the embodiment of this application.
[0119] 1) Electronic device, including but not limited to: terminal device, network device, server, intelligent device, etc. The electronic device can be various types of electronic devices in the prior art or electronic devices that will emerge in the future.
[0120] 2) A terminal device is a device that provides voice and / or data connectivity to users. In this application, the terminal device mainly refers to, but is not limited to, mobile terminals, vehicle terminals, in-vehicle terminals, vehicle equipment, public terminals, handheld devices with wireless communication functions, wearable devices, computing devices, etc. Among them, in-vehicle terminals include, but are not limited to, in-vehicle navigation devices, etc., and mobile terminals include, but are not limited to, mobile phones, wearable devices, tablet computers, etc. Exemplarily, the terminal device can also be a virtual reality (VR) terminal device, an augmented reality (AR) terminal device, a wireless terminal in industrial control, a wireless terminal in autonomous driving, a wireless terminal in telemedicine, a wireless terminal in smart grid, a wireless terminal in a smart city, a wireless terminal in a smart home, and so on.
[0121] 3) A network device, also known as a radio access network (RAN) device, is a device that connects a terminal device to a wireless network, and it includes devices in various communication systems; the network device may have various forms, such as macro base stations, micro base stations, relay stations, and access points, etc.; the network device includes, but is not limited to, network devices in the new radio network, network devices in the long-term evolution network. Exemplarily, the network device includes, but is not limited to: transmission reception point (TRP), next generation Node B (gNB), base transceiver station (BTS) in global system for mobile communication (GSM) or code division multiple access (CDMA), Node B (NB) in wideband code division multiple access (WCDMA) system, evolved Node B (eNB or eNodeB) in long-term evolution system, radio network controller (RNC), base station controller (BSC), HeNB (home evolved NodeB), or HNB (home Node B), baseband unit (BBU), etc.
[0122] 4) "Multiple" means two or more, and other quantifiers are similar. "And / or" describes the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist simultaneously, and B exists alone. The character " / " generally indicates that the associated objects before and after are in an "or" relationship.
[0123] 5) "Corresponding" can refer to an association relationship or a binding relationship. A corresponding to B means that there is an association relationship or a binding relationship between A and B.
[0124] It should be noted that the nouns or terms involved in the embodiments of the present application can be referred to each other and will not be elaborated here.
[0125] Figure 1 This is a schematic diagram of an application scenario provided by the embodiments of the present application Figure 1 As Figure 1 shown, in the solution of the embodiments of the present application, the electronic device 01 is, for example, a smart terminal or a handheld terminal. The network device 02 is, for example, a server. The network device 02 can generate an application program and send the application program to the electronic device 01; the electronic device 01 installs the application program. And a system (control system) is installed in the electronic device 01.
[0126] It can be known that various application programs can be arranged in the electronic device. Among them, the application program can also be called a software program or a software control program. In the code of the application program, there are usually multiple classes; there are instance members in the class. The instance members can be instance member variables, or the instance members can be instance member methods. For example, the instance member method is a virtual function.
[0127] And, in order to run the application program, a system (control system) is also set in the electronic device.
[0128] In one example, the network device for generating the application program can use a compiler to compile the application program; during the compilation process, the compiler can calculate the layout of each class in the application program, and then obtain the offset of the instance member. The compiler is used to save the offset of the instance member in the executable file; among them, the executable file is stored together with the application program.
[0129] The electronic device is installed with a system and application programs. In the scenario of updating the system of the electronic device, the following is introduced. When the instance members in the application program depend on the parent class in a specific version of the system, at this time, the parent class of the application program or the parent class of the parent class is defined in a specific version of the system. When the system is updated to obtain a new version of the system, since the parent class that the instance members in the application program depend on, or the parent class of the parent class that they depend on, is defined in a specific version of the system, it will cause the application program to fail to run properly in the new version of the system.
[0130] For example, the application program includes class b, and class b includes instance member c. The application program can run in the system of version A. Among them, the parent class of class b is class a, but class a is defined in the system of version A. Update the system of version A to obtain the system of version B. At this time, when the application program is run in the system of version B, since the instance member c of class b in the application program depends on class a, but class a is defined in the system of version A, the instance member c of the application program also depends on the system of version A. However, at this time, the system has been updated, resulting in the application program being unable to run properly in the system of version B.
[0131] The electronic device is installed with a system and application programs (application, APP). In the scenario of updating the application program of the electronic device, the following is introduced. The electronic device is installed with an application program, and the network device updates the application program. And the electronic device selects to update the application program. At this time, the classes and instance members in the code of the updated application program have been modified. The network device sends the updated application program to the electronic device, and the electronic device runs the updated application program, but the executable file corresponding to the application program in the electronic device has not been modified. Furthermore, when accessing the instance members of the application program according to the executable file, an access error will occur.
[0132] When initially installing the application program or initially installing the system, due to changes in the classes and instance members of the application, an access error will occur when accessing the instance members of the application program according to the executable file.
[0133] Furthermore, when initially installing the system, or initially installing the application program, or updating the system, or updating the application program, it is often necessary to adjust the classes in the system or application program. Furthermore, it will affect the layout of the instance members related to the classes, that is, the layout is adjusted. After the layout is adjusted, problems with access errors of the instance members will occur.
[0134] For example, Figure 2 Schematic diagram of the compilation of the class provided in this embodiment Figure 1 such as Figure 2As shown, the application includes class A (class A); for example, Figure 3 As shown, the superclass of class A is class S (class S). For example, Figure 2 As shown, class A defines Figure 3 Schematic of the compilation of the class provided in this embodiment Figure 2 objects m and n of type int, namely Public int m, Public int n; objects m and n are instance member variables in class A; and class A defines the instance member variable m as obj.m = 1. For example, Figure 3 As shown, class S defines instance member variables a and b, namely public int a, public int b. Compile the application, thereby compiling class A and class S, and an executable file corresponding to the application can be obtained. For example, Figure 2 As shown, the executable file includes the instruction "str w0,[x20,#16]" for accessing the instance member variable m; the immediate number #16 of the instruction "str w0,[x20,#16]" is the offset of the instance member variable m relative to the object base address, that is, the immediate number #16 is the offset of the instance member variable m. Figure 4 Schematic of the offset of the instance member provided in this embodiment Figure 1 For example, Figure 4 As shown, Figure 4 shows the object base address base and shows the offset offset of the instance member variable m. Figure 5 Schematic of the compilation of the class provided in this embodiment Figure 3 For example, Figure 5 As shown, modify class S and add an instance member variable c in class S, which is public int c. When the instance member variables in class S change, the layout of the instance members will change; Figure 6 Schematic of the offset of the instance member provided in this embodiment Figure 2 For example, Figure 6 As shown, Figure 6 shows the object base address base and shows the offset offset of the instance member variable c after class S is modified. At this time, the offset #16 used to indicate the instance member variable m in the instruction "str w0,[x20,#16]" does not change; thus, when using the instruction "str w0,[x20,#16]" in the executable file to access the instance member variable m, the instance member variable c will be accessed and the instance member variable m cannot be correctly accessed.
[0135] Among them, the instruction can also be called a machine instruction.
[0136] For another example, Figure 7Schematic of the compilation of the class provided in this embodiment Figure 4 , as Figure 7 shown, the application includes virtual functions, and the virtual function is defined as Public void testVtable; Figure 8 Schematic of the compilation of the class provided in this embodiment Figure 5 , as Figure 8 shown, the parent class of the virtual function is class S (class S). As Figure 7 shown, the instance member method Obj.f() is defined in the virtual function. As Figure 8 shown, two instance member methods are defined in class S, which are Public void f() and Public void g() respectively. Compile the application, thereby compiling the virtual function and class S, and an executable file corresponding to the application can be obtained. As Figure 7 shown, the executable file includes the instruction "ldr x1,[x0,#112]" for accessing the instance member method f; the immediate number #112 of the instruction "ldr x1,[x0,#112]" is the offset of the instance member method f relative to the object base address, that is, the immediate number #112 is the offset of the instance member method f. Figure 9 Schematic of the offset of the instance member provided in this embodiment Figure 2 , as Figure 9 shown, Figure 9 shows the object base address base and shows the offset offset of the instance member method f. Figure 10 Schematic of the compilation of the class provided in this embodiment Figure 6 , as Figure 10 shown, modify class S, and add an instance member method e, which is Public void e() in class S. When the instance member variables in class S change, the layout of the instance members will change; Figure 11 Schematic of the offset of the instance member provided in this embodiment Figure 4 , as Figure 11 shown, Figure 11 shows the object base address base and shows the offset offset of the instance member method e after class S is modified. At this time, the offset #112 indicating the instance member method f in the instruction "ldr x1,[x0,#112]" does not change; thus, when using the instruction "ldr x1,[x0,#112]" in the executable file to access the instance member method f, the instance member method e will be accessed, and the instance member method f cannot be correctly accessed.
[0137] According to the above analysis, when initially installing the system, or initially installing an application, or updating the system, or updating the application, it is necessary to adjust the offsets of instance members.
[0138] In one example, the global offset table (GOT) method or the procedure linkage table (PLT) method can be used to resolve symbol references between shared libs. However, these two methods are not suitable for processing object-oriented code, that is, they are not suitable for processing applications obtained in an object-oriented manner; furthermore, they cannot support the processing of instance member offsets and cannot support polymorphic features.
[0139] In one example, during the running of an application, a global variable can be configured for each instance member, and the global variable is used to store the offset value of the instance member; the global variable of the instance member is stored in the code of the application; furthermore, the global variable of each instance member can be adjusted, so as to adjust the offset of the adjusted instance member. However, the above method of setting global variables requires adding a global variable for each instance member, which increases the code overhead, increases the memory space and storage space occupied by the application. And to adjust the global variable of each instance member and obtain the global offset will increase a memory access operation and an addition operation, thus increasing the instruction overhead.
[0140] For example, Figure 12 Schematic of the compilation of the class provided in this embodiment Figure 7 , as Figure 12 shown, the Objective-C compiler LLVM (low level virtual machine) allocates a global variable for each instance member in each class of the application; the global variable is used to store the offset value of the instance member; when compiling the class, the underlying virtual machine calculates that the size of the base class is 40 bytes and records the base class size (instanceSize) at this time in the derived class MyClass. When adjusting the instance members of the class in the application, the size of the base class is increased to 48 bytes. Then, when running the application, when loading the MyClass class, it can be determined that the true size of the base class and the base class size recorded in the derived class MyClass are different; furthermore, it can be determined that the size of the base class has changed, and it can be determined that the size of the base class has increased by 8 bytes. Then, use runtime to traverse all instance members in MyClass, increase the value of all variables of each instance member by 8, and also increase the base class size (instanceSize) recorded in the MyClass class by 8.
[0141] As can be seen from the example, since a global variable needs to be added to each instance member, when correcting the offset of the instance member, the values of all variables of each instance member need to be increased by 8, thus increasing the memory space and storage space occupied by the application program; moreover, it is necessary to correct the global variables of each instance member, increase the values of all variables of each instance member by 8, and further increase the instruction overhead. In addition, the above process needs to correct the global variables of the instance members during the running of the application program, so the correction process will occupy the central processing unit (CPU), thereby reducing the startup performance of the application program.
[0142] The present application provides an executable file processing method and device, which can solve the above problems.
[0143] Figure 13 It is a schematic flow chart of an executable file processing method provided by an embodiment of the present application. As Figure 13 shown, the method includes:
[0144] 101. Obtain a relocation table corresponding to the executable file, where the relocation table is used to indicate at least one instruction with an offset to be corrected.
[0145] In one example, step 101 includes the following implementation manners.
[0146] The first implementation manner: When determining to update the system, send the information of the executable file to the network device; receive the relocation table corresponding to the information of the executable file sent by the network device.
[0147] The second implementation manner: Send the software version information to the network device; receive the relocation table corresponding to the software version information and the executable file sent by the network device.
[0148] In one example, an instruction includes an opcode and an operand, the opcode is used to indicate the instruction type, and the operand is used to indicate the data to be operated on and the unit address where the data is located.
[0149] Exemplarily, the execution subject of this embodiment may be an electronic device.
[0150] The electronic device is used to install a system and at least one application program. Both the system and the application program are composed of code; the code of the system includes at least one class, and the code of the application program includes at least one class. Each class has instance members. The instance members may be instance member variables, or the instance members may be instance member methods.
[0151] As can be seen from the previous introduction, when initially installing the system, or initially installing an application, or updating the system, or updating the application, it is necessary to correct the offsets of instance members.
[0152] Since the classes in the system do not need to be decoupled, the system does not need to be compiled in a decoupled manner; however, it is not excluded that in the future, there may be classes in the system that are decoupled classes. The classes in the application can be compiled in a decoupled manner, and the classes in the application can be divided into decoupled classes and non-decoupled classes. The solution provided in this embodiment is applicable to decoupled classes.
[0153] A decoupled class refers to a class in the application that needs to inherit from the classes in the system (excluding Object in the system); that is, the classes in the application need to inherit and depend on the classes in the system (excluding Object in the system).
[0154] A non-decoupled class refers to a class in the application that does not need to inherit from the classes in the system (excluding Object in the system); that is, the classes in the application do not need to inherit and depend on the classes in the system (excluding Object in the system).
[0155] Figure 14 This is a schematic diagram of a decoupled class provided for the embodiments of the present application. As Figure 14 shown, the system has an object Object, and the system also has classes A and B; the application includes classes C, D, E, and F. Classes A and B in the system both inherit from Object in the system. Class C in the application inherits from Object in the system, and class E in the application inherits from class C in the application; class D in the application inherits from class A in the system, and class F in the application inherits from class D in the application. Since the parent class of class D in the application is class A in the system, and the grandparent class of class F in the application is class A in the system, thus, classes D and F are decoupled classes. However, classes C and E do not inherit from the classes in the system, and classes C and E are non-decoupled classes. It can be seen that as decoupled classes, classes D and F will change with the change of class A in the system; as non-decoupled classes, classes C and E will not change with the change of classes A and B in the system.
[0156] In one example, since decoupled classes will change with the change of the system, it is necessary to recalculate and read in the offsets of the decoupled classes to avoid the situation where the software control program cannot run properly.
[0157] The application has at least one corresponding executable file; one executable file corresponds to one relocation table, that is, each executable file has a corresponding relocation table. The executable file includes at least one instruction, and the instruction includes an opcode and an operand; wherein, the opcode is used to indicate the instruction type of the instruction; the operand is used to indicate the data to be operated and the unit address where the data is located. It can be seen that the operand includes an immediate number, and the immediate number is the offset of the instance member corresponding to the instruction.
[0158] For example, the instruction "str w0,[x20,#16]"; the instruction "str w0,[x20,#16]" is an assembly instruction. str is the opcode; w0, x20, and #16 are all operands. w0 represents a general-purpose register, and w0 is a 32-bit register; x0 represents another general-purpose register, and x0 is a 64-bit register. x20 is the base address; #16 is the immediate number, and #16 represents that the offset of the instance member corresponding to the instruction is 16, that is, the offset of this instance member relative to the object base address is 16.
[0159] The instruction "str w0,[x20,#16]" means to save the data in the register w0 to the memory unit indicated by the address "x20 + 16". It can be seen that the data in the register w0 is the data to be operated, and "x20 + 16" indicates the unit address where the data is located.
[0160] In the embodiments of the present application, in order to accurately and quickly correct the offset of the instance member, a relocation table can be used to correct the offset of the instance member, wherein the relocation table is used to indicate at least one instruction whose offset needs to be corrected.
[0161] First, for an executable file that needs to be corrected, it is necessary to obtain the relocation table corresponding to the executable file. There are several ways to obtain the relocation table corresponding to the executable file.
[0162] First, introduce the application scenario for obtaining the relocation table corresponding to the executable file. Figure 15 This is a schematic diagram of an application scenario provided by the embodiments of the present application Figure 2 . As Figure 15 shown, the control device 11 generates an application; the control device 11 uploads the generated application or the updated application to the network device 12; the network device 14 provides an application service, and the application service is equivalent to an application store; the network device 14 provides the application service to the electronic device 13. Figure 16 This is a schematic diagram of an application scenario provided by the embodiments of the present application Figure 3 . As Figure 16As shown, a display interface for application services can be displayed on the electronic device 13, and the display information of multiple applications is displayed on this display interface; the display information is, for example, the icon of the application and the introduction of the application. For example, application A, application B, and application C are displayed on the display interface of the electronic device 13.
[0163] The electronic device 13 can automatically determine whether it is necessary to download the applications provided in the application service. When the electronic device 13 automatically determines that it is necessary to download the applications provided in the application service, the electronic device 13 obtains the applications provided in the application service provided by the control device 11 from the network device 12. For example, the control device 11 uploads the installation package of the generated application A to the network device 12; when the electronic device 13 automatically determines that it is necessary to download the application A provided in the application service, the electronic device 13 sends a fetch instruction to the network device 12; the network device 12 sends the installation package of the application A to the electronic device 13 according to the fetch instruction.
[0164] Alternatively, the user sends a trigger instruction to the electronic device 13; the electronic device 13 determines that it is necessary to download the applications provided in the application service according to the trigger instruction; the electronic device 13 obtains the applications provided in the application service provided by the control device 11 from the network device 12. For example, the control device 11 uploads the installation package of the generated application A to the network device 12; the electronic device 13 obtains the trigger instruction sent by the user, and this trigger instruction is used to instruct the electronic device 13 to download the application A; the electronic device 13 sends a fetch instruction to the network device 12; the network device 12 sends the installation package of the application A to the electronic device 13 according to the fetch instruction.
[0165] The first way to obtain the relocation table corresponding to the executable file is that when the electronic device determines to update the system and determines that it is necessary to correct the offset of the instance members of the executable file, it is necessary to obtain the relocation table corresponding to the executable file. At this time, the electronic device can send the information of the executable file to the network device; the information of the executable file is, for example, the identifier of the executable file; the network device has pre-generated or obtained the relocation table corresponding to the executable file, so it can send the relocation table corresponding to the information of the executable file to the electronic device.
[0166] Based on Figure 15 - 16 In the scenario shown, for example, the control device 11 for generating applications generates applications, executable files, and relocation tables. The control device 11 uploads the software installation package to the network device 12. The electronic device 13 automatically or according to the user's instruction sends the information of the executable file to the network device 12; the network device 12 sends the stored relocation table corresponding to the executable file to the electronic device 13.
[0167] The second way to obtain the relocation table corresponding to the executable file is introduced as follows. When the electronic device initially installs the application or updates the application, the electronic device can determine which version of the application needs to be installed, and then the electronic device sends the software version information of the application to the network device. The network device has already generated the application corresponding to the software version information in advance. The network device can compile the application using a compiler to obtain the executable file corresponding to the software version information; and, the network device generates the relocation table corresponding to the software version information, where the executable file and the relocation table correspond to each other.
[0168] Based on Figure 15 - 16 In the scenario shown, for example, the control device 11 for generating the application generates the application, the executable file, and the relocation table. To indicate that the relocation table is configured for the application, an indication identifier (which can also be called an "identifier") can be assigned to the application, and this indication identifier is used to indicate that the relocation table is configured for the application. It can be seen that when the control device 11 for generating the application generates the relocation table, an indication identifier is also generated at the same time, and the indication identifier is used to indicate that the relocation table is configured for the application; in one example, the indication identifier can also indicate the storage location of the relocation table. As Figure 15 shown, the control device 11 generates a software installation package, and the software installation package includes the application, the relocation table, and the indication identifier, and the indication identifier is used to indicate that the relocation table is configured for the application; the control device 11 uploads the software installation package to the network device 12. And, the control device 11 can notify the network device 14 that the application has been updated; as Figure 16 shown, the network device 14 is used to provide application services to the electronic device 13, and the application services are equivalent to an application store; thus, the network device 14 can display the application provided by the control device 11 in the application services, so that the electronic device 13 can choose by itself whether to obtain the software installation package from the network device 12 according to the content in the application services displayed by the network device 14. The electronic device 13 automatically or according to the user's instruction obtains the software installation package from the network device 12. Then, the electronic device 13 obtains the software installation package, and the software installation package includes the application, the relocation table, and the indication identifier. The electronic device 13 can determine that the application is configured with the relocation table according to the indication identifier.
[0169] Or, based on Figure 15 - 16 In the scenario shown, for another example, as Figure 15As shown, the control device 11 generates a software installation package, which includes an application program, a relocation table, and an indication flag. The indication flag is used to indicate that a relocation table is configured for the application program. The control device 11 uploads the software installation package to the network device 12. Moreover, the control device 11 can notify the network device 14 that the application program has been updated; as Figure 16 As shown, the network device 14 is used to provide an application service to the electronic device 13. The application service is equivalent to an application store. Thus, the network device 14 can display the application program provided by the control device 11 in the application service, enabling the electronic device 13 to choose whether to obtain the application program from the network device 12 according to the content in the application service displayed by the network device 14. The electronic device 13 automatically or according to the user's instruction obtains the application program from the network device 12. At this time, the network device 12 puts the indication flag into the application program, and the network device 12 sends the application program carrying the indication flag to the electronic device 13. Furthermore, the electronic device 13 can determine that the application program is configured with a relocation table according to the indication flag. The electronic device 13 determines whether to obtain the relocation table corresponding to the application program from the network device 12 according to the current state of the terminal device. For example, the electronic device 13 determines whether to obtain the relocation table according to the current version of the system. When the electronic device 13 determines that it needs to obtain the relocation table corresponding to the application program, the electronic device 13 sends a request for obtaining to the network device 12, and the network device 12 sends the relocation table to the electronic device 13.
[0170] The above indication flag can be invisibly stored in the network device and the terminal device. Then, after the electronic device obtains the application program, the relocation table, and the indication flag, the background of the electronic device can determine that the relocation table has been obtained according to the indication flag.
[0171] The third method for obtaining the relocation table corresponding to the executable file is introduced as follows. After the electronic device is initially started, similar to the second method above, the electronic device can determine which version of the application program needs to be installed, and then the electronic device sends the software version information of the application program to the network device. The network device has previously generated the application program corresponding to the software version information. The network device can compile the application program using a compiler to obtain the executable file corresponding to the software version information. Moreover, the network device generates the relocation table corresponding to the software version information, where the executable file and the relocation table are corresponding.
[0172] The fourth way to obtain the relocation table corresponding to the executable file is that when the system is initially installed, or the application program is initially installed, or the system is updated, or the application program is updated, the electronic device can obtain the relocation table corresponding to the executable file from other storage media. For example, the single device copies the relocation table from a removable hard disk.
[0173] 102. According to the relocation table, determine the position of the instruction with the offset to be corrected in the executable file, and determine the instance member corresponding to the instruction with the offset to be corrected.
[0174] Exemplarily, the relocation table can indicate the positions of at least one instruction with an offset to be corrected in the executable file. Then, the electronic device can directly determine which instructions need to be corrected according to the relocation table and determine the positions of these instructions in the executable file.
[0175] According to the previous introduction, the executable file includes at least one instruction, and the instruction includes an opcode and an operand; among them, the opcode is used to indicate the instruction type of the instruction; the operand is used to indicate the data to be operated and the unit address where the data is located. Among them, the operand includes an immediate number, and the immediate number is the offset of the instance member corresponding to the instruction.
[0176] It can be seen that one instruction is used to access one instance member, and the instruction corresponds to the instance member; after the electronic device finds the instruction with the offset to be corrected in the executable file, it can determine the instance member corresponding to the instruction.
[0177] 103. Correct the offset of the instance member.
[0178] Exemplarily, after the electronic device finds the instruction with the offset to be corrected and the instance member corresponding to the instruction, it can correct the offset of the instance member in the instruction.
[0179] For example, the executable file A includes the instruction "str w0,[x20,#16]". The relocation table indicates the position of this instruction in the executable file A. The electronic device can find the instruction "str w0,[x20,#16]", where #16 represents that the offset of the instance member corresponding to the instruction is 16; then, the electronic device can correct the offset 16.
[0180] In this embodiment, by obtaining a relocation table corresponding to an executable file, where the relocation table is used to indicate instructions with at least one offset to be corrected; according to the relocation table, determining the position of the instruction with the offset to be corrected in the executable file, and determining the instance member corresponding to the instruction with the offset to be corrected; correcting the offset of the instance member. When initially installing the system, or initially installing an application, or updating the system, or updating the application, the electronic device needs to correct the offset of the instance member in the instructions of the executable file. The electronic device first obtains the relocation table corresponding to the executable file, and then, based on the relocation table, finds the instruction with the offset to be corrected, and thus can correct the offset of the instance member in the instruction. The solution provided in this embodiment does not require adding a global variable for each instance member; only a relocation table needs to be provided for each instance member of the application, and the offset of the instance member can be corrected according to the information in the relocation table, that is, the instruction for compiling the instance member is corrected, reducing the code overhead, and also reducing the memory space and storage space occupied by the application; moreover, only the instructions of the instance members that need to be corrected need to be corrected. Since no additional instructions need to be added for accessing the instance members, the instruction overhead can be reduced. And, in the solution provided in this embodiment, since the relocation table can be independent of the application, the code of the execution process of the solution provided in this embodiment does not need to be arranged in the application. Further, if the solution provided in this embodiment occurs during the upgrade or update of the application, the runtime overhead of the application will not be increased. Further, since the relocation table can be independent of the application, the execution process of the solution provided in this embodiment does not need to be executed during the running of the application.
[0181] Figure 17 It is a schematic flowchart of another executable file processing method provided by an embodiment of the present application. As Figure 17 shown, the method includes:
[0182] 201. Obtain a relocation table corresponding to the executable file, where the relocation table is used to indicate instructions with at least one offset to be corrected.
[0183] In one example, the relocation table includes an instruction offset, an instruction type, and an instance member of the instruction; where the instruction offset is the offset of the instruction with the offset to be corrected in the executable file, and the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected.
[0184] Exemplarily, this step can refer to Figure 13 the step 101 shown. And, the following content is introduced.
[0185] In this embodiment, the control device for generating an application will also generate a relocation table corresponding to the executable file when generating the application and the executable file. First, the control device for generating an application compiles the application using a compiler, and can obtain the instruction offset, instruction type, and instance member corresponding to each instruction (i.e., machine instruction). Among them, the "instruction offset" refers to the offset of the instruction in the executable file (i.e., the offset of the instruction whose offset needs to be corrected in the executable file). The "instruction type" is used to indicate the encoding method when encoding the immediate number in the instruction whose offset needs to be corrected, and the encoding method includes the encoding position. The "instance member corresponding to the instruction" can be the information of the instance member; the information of the instance member, for example, includes a first identifier and a second identifier; the first identifier refers to the identifier of the instance member; the second identifier refers to the identifier of the class to which the instance member belongs, that is, the identifier of the class that the instance member inherits. The instruction offset, instruction type, and instance member corresponding to each instruction constitute the relocation information of each instruction. Furthermore, the control device for generating an application can put the relocation information of each instruction into a relocation table. Table 1 shows a structure of the relocation table.
[0186] Table 1 Relocation Table
[0187] offset classid fieldID / methodID Instruction Type
[0188] As shown in Table 1, the relocation table is at least composed of four parts, namely the instruction offset (offset in Table 1), the second identifier (classid in Table 1), the first identifier (fieldID / methodID in Table 1), and the instruction type. Among them, when the instance member is an instance member variable, the first identifier of the instance member is represented by fieldID; when the instance member is an instance member method, the first identifier of the instance member is represented by methodID.
[0189] Table 2 shows another structure of the relocation table.
[0190] Table 2 Relocation Table
[0191] offset fieldID / methodID Instruction Type
[0192] As shown in Table 1, the relocation table is at least composed of three parts, namely the instruction offset (offset in Table 2), the first identifier (fieldID / methodID in Table 2), and the instruction type. Among them, when the instance member is an instance member variable, the first identifier of the instance member is represented by fieldID; when the instance member is an instance member method, the first identifier of the instance member is represented by methodID.
[0193] Then, after obtaining the relocation table, the control device for generating the application uploads the application and the relocation table to the network device. Further, the network device waits for the terminal device that needs to install the application to obtain the relocation table.
[0194] When generating the application, the control device for generating the application compiles the application using a compiler to obtain a relocation table corresponding to the executable file. Since the application includes at least one instance member, the executable file includes instructions corresponding to each instance member; the instructions include the offset of the instance member; the offset of the instance member refers to the offset of the instance member relative to the object base address. When the control device initially generates the application, the offset of the instance member in the instructions refers to the original offset of the instance member.
[0195] Since the instructions include the offset of the instance member and the application and the executable file are published together, after the terminal device installs the application, the electronic device can determine the offset of the instance member in the instructions according to the instructions; the electronic device accesses the instance member in the application according to the offset of the instance member. It can be seen that the instructions are used to access the instance member.
[0196] Since the offset of the instance member is recorded in the instructions, when it is necessary to correct the instance member, it is necessary to correct (patch) the offset recorded in the instructions corresponding to the instance member, that is, to correct the instructions corresponding to the instance member.
[0197] 202. Determine the position of the instruction with the offset to be corrected in the executable file according to the relocation table, and determine the instance member corresponding to the instruction with the offset to be corrected.
[0198] Exemplarily, this step can refer to Figure 13 Step 102 shown in the figure, which will not be elaborated. And the following content is introduced.
[0199] In one example, after obtaining the relocation table, the electronic device can detect whether it is necessary to correct (patch) the instructions of the executable file.
[0200] In one example, the electronic device determines whether it is necessary to patch the instructions of the executable file corresponding to the application program according to the hash value. Exemplarily, each system library (the system's system library) has a hash value of metadata. For example, the metadata includes class metadata (ClassMetaData), instance member variable metadata (FieldMetaData), and instance member method metadata (MethodMetaData); for example, the class metadata (ClassMetaData) includes the size of the class, variable type (FieldType), variable name (FieldName), variable size (FieldSize), method name (MethodName), method signature (MethodSignature), virtual function table (vtable) pointer, and so on. However, the class metadata (ClassMetaData) does not include the implementation code of the instance member methods. And when patching the instructions of the executable file, it is necessary to record the name and hash value of the system library on which the instance members of the application program depend. Thus, after the system is upgraded, after the electronic device obtains the relocation table, the electronic device compares the hash value of the system library recorded in the application program with the hash value of the upgraded system (the hash value of the system library of the upgraded system). When the electronic device determines that the two are consistent, it determines that it is not necessary to patch the instructions of the executable file corresponding to the application program; when the electronic device determines that the two are inconsistent, it determines that it is necessary to patch the instructions of the executable file corresponding to the application program.
[0201] 203. Determine the instructions in the executable file according to the instruction offset corresponding to the instance member, where the instructions include a second offset of the instance member, and the second offset is the original offset of the instance member.
[0202] The electronic device can read each instance member in the application program. Since each instance member has a first identifier, and the relocation table has the first identifier of the instance member, and the relocation table includes the instruction offset, first identifier, and instruction type with a corresponding relationship; thus, the electronic device can read the instruction offset and instruction type corresponding to the first identifier of each instance member from the relocation table according to the first identifier of each instance member.
[0203] For example, Table 3 shows an example of a relocation table. As shown in Table 3, relocation information for two instance members is provided in the relocation table; the first identifier fieldID of the instance member variable M is B, the second identifier classid of the class inherited by the instance member variable M is A, the offset of the instruction of the instance member variable M is 12, and the instruction type of the instruction of the instance member variable M is 1; the first identifier methodID of the instance member method N is D, the second identifier classid of the class inherited by the instance member method N is C, the offset of the instruction of the instance member method N is 24, and the instruction type of the instruction of the instance member method N is 2.
[0204] Table 3 Relocation Table
[0205] offset classid fieldID / methodID Instruction Type 12 A B 1 24 C D 2
[0206] For each instance member indicated by the first identifier in the relocation table, the electronic device has read the instruction offset of the instance member from the relocation table; the instruction offset is the offset of the instruction corresponding to the instance member in the executable file. Thus, the electronic device can determine the instruction corresponding to the instance member from the executable file according to the instruction offset of the instance member.
[0207] For each instance member indicated by the first identifier in the relocation table, after the electronic device determines the instruction corresponding to the instance member from the executable file, since the instruction includes the original offset of the instance member, thus, the electronic device can read the original offset of the instance member from the instruction.
[0208] 203. Calculate the current actual offset of the instance member, where the actual offset is the offset after correcting the original member offset of the instance member.
[0209] In one example, step 203 includes the following implementation manners.
[0210] The first implementation manner of step 203: When the instance member is an instance member variable, starting from the highest-level class in the class inherited by the current instance member variable, traverse each class inherited by the current instance member variable, align and arrange the instance member variables in each class to obtain the offsets of the instance member variables in each class; determine the sum of the offset of the previous instance member variable before the current instance member variable, the size of the previous instance member variable, and the size of the space padding between the current instance member variable and the previous instance member variable as the actual offset of the current instance member variable.
[0211] The second implementation of step 203: When the instance member is a virtual function, starting from the highest-level class in the classes inherited by the current virtual function, traverse each class inherited by the current virtual function, calculate the offset of the pointer of the virtual function in each class, and obtain the virtual function table; determine the sum of the offset of the pointer of the previous virtual function before the current virtual function in the virtual function table and the length of the pointer of the current virtual function, which is the actual offset of the current virtual function.
[0212] Exemplarily, the electronic device also needs to calculate the actual offset of each instance member.
[0213] In one example, the electronic device determines the first identifier and the second identifier of each instance member according to the obtained relocation table. The first identifier is the identifier of the instance member, and the second identifier is the identifier of the class to which the instance member belongs. The electronic device determines the actual offset of the instance member according to the first identifier and the second identifier. In one example, since the first identifier indicates the instance member information of the instance member and the second identifier indicates the class information of the class inherited by the instance member, thus, the terminal device can calculate the actual offset of the instance member according to the instance member information of the instance member and the class information of the class inherited by the instance member.
[0214] In one example, when the instance member is an instance member variable, the instance member variable has a parent class, and this parent class also has its own parent class. Furthermore, the instance member variable corresponds to an inheritance chain, and this inheritance chain includes the parent class inherited by the instance member variable and the parent class inherited by this parent class itself. Thus, when calculating the actual offset of the instance member variable, it is necessary to start from the highest-level class in the classes inherited by the instance member variable, traverse each class inherited by the instance member variable, arrange the instance member variables in each class (at this time, the instance member variables have been aligned), and when arranging, the size of the space filling between adjacent instance member variables (that is, the size of the hole) can be determined. Then, the offset of each instance member variable is obtained. Sum up the offset of the previous instance member variable before the current instance member variable, the size of the previous instance member variable, and the size of the space filling between the current instance member variable and the previous instance member variable, and the actual offset of the instance member variable is obtained.
[0215] It should be noted that: for the previous instance member variable before the current instance member variable, if it is a primitive type (such as int), the size of the previous instance member variable is its own size; for the previous instance member variable before the current instance member variable, if it is a reference type, the size of the previous instance member variable is the size of the pointer corresponding to the previous instance member variable.
[0216] In another example, when the instance member is a virtual function, the virtual function has a parent class, and the parent class also has its own parent class. Consequently, the virtual function corresponds to an inheritance chain that includes the parent classes inherited by the virtual function and the parent classes inherited by that parent class. Therefore, when calculating the actual offset of the virtual function, it is necessary to traverse all the classes inherited by the virtual function to access the original data of the class, and then access the original function data (the original function data includes the function name and function type). Then, traverse the original function data. For the virtual functions in the original function data, calculate the offset of the pointer of the virtual function in each class to obtain the virtual function table. At this time, the virtual function table includes the offsets of each virtual function. Among them, the offset of each virtual function is calculated based on the pointers of the various virtual functions located before that virtual function. At this time, the sum of the offset of the pointer of the previous virtual function located before the current virtual function in the virtual function table and the length of the pointer of the current virtual function is used as the actual offset of the current virtual function.
[0217] For example, since the virtual function table stores the addresses of virtual functions (this address can be called a "pointer"); at this time, the actual offset of the current virtual function is N + 4, where N is the offset of the pointer of the previous virtual function located before the current virtual function in the virtual function table, and 4 is the length of the pointer of the current virtual function. It should be noted that each class has a corresponding virtual function table.
[0218] 204. Correct the original member offset of the instance member to the actual offset.
[0219] In one example, the relocation table includes an instruction type; the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected, and the encoding method includes an encoding position.
[0220] Step 204 specifically includes: according to the encoding method indicated by the instruction type, write the actual offset to the specific bit position in the instruction with the offset to be corrected, and the specific bit position is the bit position indicated by the encoding position in the encoding method.
[0221] Exemplarily, for each instance member indicated by the first identifier in the relocation table, the electronic device reads the instruction type of the instruction corresponding to the instance member from the relocation table; since the instruction type indicates the encoding method, thus, the terminal device can correct the offset of the instance member in the instruction according to the instruction type, that is, correct the original offset of the instance member. At this time, the relocation table includes the instruction type (the instruction type in the relocation table focuses on the encoding method of the immediate number). For example, the instruction type indicates that the instruction is of the instruction type such as str, ldr, etc., and different instruction types correspond to different encoding methods for the immediate number. The encoding method includes the encoding position. Influencing factors of the encoding method, for example, there are byte width (left shift, right shift); the byte width affects the encoding method, and further affects the encoding position.
[0222] It can be seen that the encoding position indicates a specific bit position, and the specific bit position is the bit position used to store the offset of the instance member. The electronic device can write the actual offset to the specific bit position in the instruction according to the encoding method indicated by the instruction type. Thus, the corrected instruction is obtained, and further, the corrected executable file is obtained.
[0223] For example, for the example shown in Table 2, the first identifier fieldID of the instance member variable M is B, the second identifier classid of the class inherited by the instance member variable M is A, the offset of the instruction of the instance member variable M is 16, and the instruction type of the instruction of the instance member variable M is 1. The terminal device can read that the first identifier fieldID of the instance member variable M in the software control program is B; the electronic device obtains the instruction offset of the instance member variable M as 12 from the relocation table according to the first identifier fieldID being B; the terminal device can find the instruction "str w0,[x20,#16]" according to the instruction offset 12, and this instruction "str w0,[x20,#16]" is the instruction that needs to be corrected and is the instruction used to indicate the instance member variable M. After the electronic device finds the instruction "str w0,[x20,#16]", it can read the immediate number 16 in the found instruction "str w0,[x20,#16]", and the immediate number 16 is the original offset of the instance member variable M. The terminal device needs to correct the immediate number 16; the electronic device corrects (patches) the immediate number 16 according to the encoding method indicated by the instruction type 1 of the instruction of the instance member variable M, and further, the terminal device corrects (patches) the instruction "str w0,[x20,#16]".
[0224] 205. Load and run the corrected executable file, where the corrected executable file runs according to the corrected instruction.
[0225] In one example, step 205 specifically includes: accessing instance members according to the corrected instructions.
[0226] Exemplarily, after correcting the instructions of the executable file according to the relocation table, the corrected executable file can be loaded, and then the corrected executable file can be run.
[0227] At this time, since the offset of the instance member in the instructions of the executable file has been corrected, at this time, the offset in the corrected instructions is the actual offset of the instance member, and this actual offset indicates the true position of the instance member. When running the corrected executable file, the corresponding instance member is accessed according to the corrected instructions in the executable file; furthermore, the instance member is correctly accessed.
[0228] For example, after correcting the instruction "str w0,[x20,#16]", for example, correcting the immediate number 16 to the immediate number 20; the immediate number 20 indicates the true offset of the instance member variable M, so that the real device can access the instance member variable M according to the immediate number 20.
[0229] In this embodiment, on the basis of the above embodiment, the control device or the network device can provide a relocation table, which includes the instruction offset, instruction type, and instance member information with a corresponding relationship. The instruction offset is the offset of the instruction in the executable file, and the instruction type indicates the encoding method. After the electronic device obtains the relocation table, it can read the instructions for the executable file according to the instruction offset in the relocation table; the instructions include the original offset of the instance member; the electronic device also needs to calculate the true offset of the instance member; then, according to the encoding method indicated by the instruction type in the relocation table, replace the offset in the access instruction with the true offset of the instance member, and then correct the instruction; the electronic device can correctly access the instance member according to the corrected instruction. Correcting the instructions for compiling the instance member reduces the code overhead and also reduces the memory space and storage space occupied by the application program; and only the instructions of the instance member that need to be corrected need to be corrected. Since no additional instructions need to be added for accessing the instance member, the instruction overhead can be reduced. In one example, the offset of the decoupled class in the application program is corrected. The decoupled class is a class affected by the system layout, that is, the decoupled class is a class that needs to inherit the classes in the system (excluding Object in the system).
[0230] Figure 18 It is a flowchart of another executable file processing method provided by the embodiment of the present application. As Figure 18 shown, the method includes:
[0231] 301. Generate a relocation table corresponding to the executable file according to the instruction of at least one offset to be corrected, where the relocation table is used to indicate the position of the instruction with the offset to be corrected in the executable file.
[0232] 302. Send the relocation table to the electronic device, where the relocation table is used to correct the offset of the instance member corresponding to the instruction with the offset to be corrected.
[0233] In one example, step 302 specifically includes:
[0234] Receive the information of the executable file sent by the electronic device during system update; send the relocation table corresponding to the information of the executable file to the electronic device; or, receive the software version information sent by the electronic device; send the relocation table corresponding to the software version information and the executable file to the electronic device.
[0235] In one example, the relocation table includes an instruction offset, an instruction type, and an instance member of the instruction; where the instruction offset is the offset of the instruction with the offset to be corrected in the executable file, and the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected.
[0236] In one example, the relocation table is specifically used to correct the offset of the instance member corresponding to the instruction with the offset to be corrected according to the current actual offset of the instance member; where the actual offset is the offset after correcting the original member offset of the instance member.
[0237] In one example, the instruction includes an opcode and an operand, the opcode is used to indicate the instruction type, and the operand is used to indicate the data to be operated on and the unit address where the data is located.
[0238] This embodiment can refer to the introduction of the above embodiment and will not be elaborated here.
[0239] In this embodiment, by obtaining a relocation table corresponding to an executable file, where the relocation table is used to indicate instructions with at least one offset to be corrected; according to the relocation table, determining the position of the instruction with the offset to be corrected in the executable file, and determining the instance member corresponding to the instruction with the offset to be corrected; and correcting the offset of the instance member. When initially installing the system, or initially installing an application, or updating the system, or updating the application, the electronic device needs to correct the offset of the instance member in the instructions of the executable file. The electronic device first obtains the relocation table corresponding to the executable file, and then, based on the relocation table, finds the instruction with the offset to be corrected, and can correct the offset of the instance member in the instruction. The solution provided in this embodiment does not require adding a global variable for each instance member; only a relocation table needs to be provided for each instance member of the application, and the offset of the instance member can be corrected according to the information in the relocation table, that is, the instruction for compiling the instance member is corrected, reducing the code overhead, and also reducing the memory space and storage space occupied by the application; and, only the instructions of the instance members that need to be corrected need to be corrected. Since no additional instructions need to be added for accessing the instance members, the instruction overhead can be reduced. And, in the solution provided in this embodiment, since the relocation table can be independent of the application, the code of the execution process of the solution provided in this embodiment does not need to be arranged in the application. Furthermore, if the solution provided in this embodiment occurs during the upgrade or update of the application, the runtime overhead of the application will not be increased. Further, since the relocation table can be independent of the application, the execution process of the solution provided in this embodiment does not need to be executed during the running of the application.
[0240] Figure 19 It is a schematic structural diagram of a processor provided by an embodiment of the present application. As Figure 19 shown, the processor 191 is applied to an electronic device, and the processor 191 is used for:
[0241] Obtaining a relocation table corresponding to an executable file, where the relocation table is used to indicate instructions with at least one offset to be corrected;
[0242] According to the relocation table, determining the position of the instruction with the offset to be corrected in the executable file, and determining the instance member corresponding to the instruction with the offset to be corrected;
[0243] Correcting the offset of the instance member.
[0244] In an example, when the processor 191 obtains the relocation table corresponding to the executable file, it is specifically used for:
[0245] When determining the update system, send the information of the executable file to the network device; receive the relocation table corresponding to the information of the executable file sent by the network device; or, send the software version information to the network device; receive the relocation table and the executable file corresponding to the software version information sent by the network device.
[0246] In one example, the relocation table includes an instruction offset, an instruction type, and an instance member of the instruction; wherein, the instruction offset is the offset of the instruction with the offset to be corrected in the executable file, and the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected.
[0247] In one example, before correcting the offset of the instance member, the processor 191 is further configured to:
[0248] Calculate the current actual offset of the instance member, where the actual offset is the offset after correcting the original member offset of the instance member;
[0249] When the processor corrects the offset of the instance member, specifically, it is configured to: correct the original member offset of the instance member to the actual offset.
[0250] In one example, when the instance member is an instance member variable, when the processor 191 calculates the actual offset of the instance member, specifically, it is configured to:
[0251] Starting from the highest-level class in the class inherited by the current instance member variable, traverse each class inherited by the current instance member variable, align and arrange the instance member variables in each class to obtain the offsets of the instance member variables in each class; determine the sum of the offset of the previous instance member variable before the current instance member variable, the size of the previous instance member variable, and the size of the space padding between the current instance member variable and the previous instance member variable, which is the actual offset of the current instance member variable.
[0252] In one example, when the instance member is a virtual function, when the processor 191 calculates the actual offset of the instance member, specifically, it is configured to:
[0253] Starting from the highest-level class in the class inherited by the current virtual function, traverse each class inherited by the current virtual function, calculate the offset of the pointer of the virtual function in each class to obtain the virtual function table; determine the sum of the offset of the pointer of the previous virtual function before the current virtual function in the virtual function table and the length of the pointer of the current virtual function, which is the actual offset of the current virtual function.
[0254] In one example, the relocation table includes an instruction type; the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected, and the encoding method includes an encoding position;
[0255] When the processor 191 corrects the original member offset of the instance member to the actual offset, it is specifically used for: according to the encoding method indicated by the instruction type, writing the actual offset to the specific bit position in the instruction with the offset to be corrected, and the specific bit position is the bit position indicated by the encoding position in the encoding method.
[0256] In one example, the instruction includes an opcode and an operand. The opcode is used to indicate the instruction type, and the operand is used to indicate the data to be operated on and the unit address where the data is located.
[0257] In one example, after the processor corrects the offset of the instance member, it is further used for:
[0258] Loading and running the corrected executable file, where the corrected executable file runs according to the corrected instructions.
[0259] In one example, when the processor loads and runs the corrected executable file, it is specifically used for: accessing the instance member according to the corrected instruction.
[0260] In this embodiment, the processor of this embodiment can be used to execute the technical solution of the above method, and its implementation principle and technical effect are similar, so details are not described here.
[0261] Figure 20 It is a schematic structural diagram of another processor provided by an embodiment of the present application. As Figure 20 shown, the processor 201 is applied to a network device. The processor 201 is used for: generating a relocation table corresponding to the executable file according to at least one instruction with an offset to be corrected, where the relocation table is used to indicate the position of the instruction with the offset to be corrected in the executable file; sending the relocation table to the electronic device, where the relocation table is used to correct the offset of the instance member corresponding to the instruction with the offset to be corrected.
[0262] In one example, when the processor 201 sends the relocation table to the electronic device, it is specifically used for:
[0263] Receiving the information of the executable file sent by the electronic device when updating the system; sending the relocation table corresponding to the information of the executable file to the electronic device; or, receiving the software version information sent by the electronic device; sending the relocation table corresponding to the software version information and the executable file to the electronic device.
[0264] In one example, the relocation table includes an instruction offset, an instruction type, and an instance member of the instruction; where the instruction offset is the offset of the instruction with the offset to be corrected in the executable file, and the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected.
[0265] In one example, a relocation table is specifically used to correct the offset of the instance member corresponding to the instruction whose offset to be corrected according to the current actual offset of the instance member; wherein, the actual offset is the offset after correcting the original member offset of the instance member.
[0266] In one example, an instruction includes an opcode and an operand. The opcode is used to indicate the instruction type, and the operand is used to indicate the data to be operated on and the unit address where the data is located.
[0267] In this embodiment, the processor of this embodiment can be used to execute the technical solution of the above method, and its implementation principle and technical effect are similar, which will not be elaborated here.
[0268] Figure 21 This is a schematic structural diagram of an electronic device provided by an embodiment of the present application. As Figure 21 shown, the electronic device includes: a transmitter 211, a receiver 212, and a processor 213.
[0269] The transmitter 211 is used to perform the transmission action of the electronic device. The receiver 212 is used to perform the reception action of the electronic device.
[0270] The processor 213 is used to execute the instructions in the computer-executable program code; when the processor executes the instructions, the instructions cause the electronic device to execute the above method embodiment.
[0271] In one example, the terminal device may further include a memory 214, and the memory 214 is used to store the program code and data of the terminal device.
[0272] In one example, the terminal device may further include a bus 215. Among them, the transmitter 211, the receiver 212, the processor 213, and the memory 214 can be interconnected through the bus 215; the bus 215 can be a PCI bus or an EISA bus, etc. The above bus 215 can be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. For the sake of convenience of representation, Figure 21 only a thick line is shown in the figure, but it does not mean that there is only one bus or one type of bus.
[0273] In the embodiments of the present application, the above embodiments can refer to and learn from each other, and the same or similar steps and terms will not be elaborated one by one.
[0274] Alternatively, some or all of the above modules can also be implemented by being embedded in a certain chip of the device in the form of an integrated circuit. And they can be implemented separately or integrated together. That is, the above modules can be configured into one or more integrated circuits for implementing the above method, for example: one or more Application Specific Integrated Circuits (ASICs), or, one or more digital signal processors (DSPs), or, one or more Field Programmable Gate Arrays (FPGAs), etc.
[0275] In this embodiment, the electronic device can be used to perform the actions of the electronic device in the method provided in the above embodiment, and its implementation principle and technical effects are similar, so details are not described here again.
[0276] Figure 22 It is a schematic structural diagram of a network device provided by an embodiment of the present application. As Figure 22 shown, the network device includes: a transmitter 221, a receiver 222, and a processor 223. The network device can be a control device.
[0277] The transmitter 221 is used to perform the sending action of the network device. The receiver 222 is used to perform the receiving action of the network device.
[0278] The processor 223 is used to execute the instructions in the computer-executable program code; when the processor executes the instructions, the instructions cause the network device to execute the above method embodiment.
[0279] In one example, the terminal device may further include a memory 224, and the memory 224 is used to store the program code and data of the terminal device.
[0280] In one example, the terminal device may further include a bus 225. Among them, the transmitter 221, the receiver 222, the processor 223, and the memory 224 can be interconnected through the bus 225; the bus 225 can be a PCI bus or an EISA bus, etc. The above bus 225 can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. For the sake of convenience of representation, Figure 22 only a thick line is used to represent it in the figure, but it does not mean that there is only one bus or one type of bus.
[0281] In the embodiments of the present application, the above embodiments can refer to and learn from each other, and the same or similar steps and terms will not be described in detail one by one.
[0282] Alternatively, some or all of the above modules can also be implemented by being embedded in a certain chip of the device in the form of an integrated circuit. And they can be implemented separately or integrated together. That is, the above modules can be configured as one or more integrated circuits for implementing the above methods, for example: one or more Application Specific Integrated Circuits (ASICs), or, one or more digital signal processors (DSPs), or, one or more Field Programmable Gate Arrays (FPGAs), etc.
[0283] In this embodiment, the network device can be used to perform the actions of the network device in the method provided in the above embodiment, and its implementation principle and technical effects are similar, which will not be elaborated here.
[0284] The embodiments of the present application provide a communication system, which includes the above electronic device and the above network device. The network device is used to receive the software control program and the relocation table provided by the electronic device; the network device can send the relocation table to the electronic device.
[0285] The embodiments of the present application provide a computer-readable storage medium, including instructions or programs, which when running on a computer, cause the computer to execute the steps of the electronic device in the method provided in the above embodiment.
[0286] The embodiments of the present application provide another computer-readable storage medium, including instructions or programs, which when running on a computer, cause the computer to execute the steps of the network device in the method provided in the above embodiment.
[0287] The embodiments of the present application provide another computer program product, including program code, which when the computer runs the program code, is used to execute the steps of the electronic device in the method provided in the above embodiment.
[0288] The embodiments of the present application provide another computer program product, including program code, which when the computer runs the program code, is used to execute the steps of the network device in the method provided in the above embodiment.
[0289] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the processes or functions according to the embodiments of the present application are generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center in a wired manner (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wirelessly (such as infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server or a data center that includes one or more integrated available media. The available medium can be a magnetic medium (such as a floppy disk, a hard disk, a magnetic tape), an optical medium (such as a DVD), or a semiconductor medium (such as a solid state disk (SSD)), etc.
[0290] Those skilled in the art should be able to realize that in one or more of the above examples, the functions described in the embodiments of the present application can be implemented by hardware, software, firmware, or any combination thereof. When implemented using software, these functions can be stored in a computer-readable medium or transmitted as one or more instructions or codes on a computer-readable medium. The computer-readable medium includes a computer storage medium and a communication medium, where the communication medium includes any medium that facilitates the transmission of a computer program from one place to another. The storage medium can be any available medium that can be accessed by a general-purpose or special-purpose computer.
[0291] As described above, the above are only specific embodiments of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed by the present application, and all should be covered by the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.
Claims
1. An executable file processing method, applied to an electronic device, characterized in that, The method includes: Obtaining a relocation table corresponding to an executable file, where the relocation table is used to indicate the offsets of instructions with at least one offset to be corrected in the executable file and the instance members corresponding to the instructions with the offset to be corrected; Determining the positions of the instructions with the offset to be corrected in the executable file according to the relocation table, and determining the instance members corresponding to the instructions with the offset to be corrected; Correcting the offsets of the instance members in the executable file.
2. The method according to claim 1, characterized in that, The obtaining of the relocation table corresponding to the executable file includes: When determining to update the system, sending information of the executable file to a network device; receiving the relocation table corresponding to the information of the executable file sent by the network device; Or, sending software version information to the network device; receiving the relocation table corresponding to the software version information and the executable file sent by the network device.
3. The method according to claim 1 or 2, characterized in that, The relocation table further includes an instruction type, and the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected.
4. The method according to claim 1 or 2, characterized in that, Before correcting the offsets of the instance members in the executable file, it further includes: Calculating the current actual offset of the instance member, where the actual offset is the offset after correcting the original member offset of the instance member; Correcting the offsets of the instance members in the executable file includes: correcting the original member offset of the instance member in the executable file to the actual offset.
5. The method according to claim 4, characterized in that, When the instance member is an instance member variable, calculating the actual offset of the instance member includes: Starting from the highest-level class in the class inherited by the current instance member variable, traversing each class inherited by the current instance member variable, aligning and arranging the instance member variables in each class to obtain the offsets of the instance member variables in each class; Determining that the sum of the offset of the previous instance member variable before the current instance member variable, the size of the previous instance member variable, and the size of the space padding between the current instance member variable and the previous instance member variable is the actual offset of the current instance member variable; When the instance member is a virtual function, calculating the actual offset of the instance member includes: Starting from the highest-level class in the class inherited by the current virtual function, traversing each class inherited by the current virtual function, and calculating the offsets of the pointers of the virtual functions in each class to obtain a virtual function table; Determining that the sum of the offset of the pointer of the previous virtual function before the current virtual function in the virtual function table and the length of the pointer of the current virtual function is the actual offset of the current virtual function.
6. The method according to claim 4, characterized in that, The relocation table includes an instruction type; the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected, and the encoding method includes an encoding position; Correcting the original member offset of the instance member in the executable file to the actual offset includes: Write the actual offset to specific bit positions in the instruction at the offset to be corrected in the executable file according to the encoding method indicated by the instruction type, where the specific bit positions are the bit positions indicated by the encoding positions in the encoding method.
7. The method according to any one of claims 1-2, 5-6, characterized in that, The instruction includes an opcode and an operand, where the opcode is used to indicate the instruction type, and the operand is used to indicate the data to be operated on and the unit address where the data is located.
8. The method according to any one of claims 1-2, 5-6, characterized in that, After correcting the offset of the instance member in the executable file, it further includes: Load and run the corrected executable file, where the corrected executable file runs according to the corrected instructions.
9. The method according to claim 8, characterized in that, The loading and running the corrected executable file includes: Access the instance member according to the corrected instruction.
10. An executable file processing method, applied to a network device, characterized in that, The method includes: Generate a relocation table corresponding to the executable file according to the instructions with at least one offset to be corrected, where the relocation table is used to indicate the offset of the instruction with the offset to be corrected in the executable file and the instance member corresponding to the instruction with the offset to be corrected; Send the relocation table to an electronic device so that the electronic device corrects the offset of the instance member corresponding to the instruction with the offset to be corrected in the executable file according to the relocation table.
11. The method according to claim 10, characterized in that,Sending the relocation table to the electronic device includes: Receive the information of the executable file sent by the electronic device during system update; send the relocation table corresponding to the information of the executable file to the electronic device; Or, receive the software version information sent by the electronic device; send the relocation table corresponding to the software version information and the executable file to the electronic device.
12. The method according to claim 10 or 11, characterized in that, The relocation table further includes an instruction type, which is used to indicate the encoding method when encoding the immediate value in the instruction with the offset to be corrected.
13. The method according to claim 10 or 11, characterized in that, The relocation table is specifically used to correct the offset of the instance member corresponding to the instruction with the offset to be corrected in the executable file according to the current actual offset of the instance member; Wherein, the actual offset is the offset after correcting the original member offset of the instance member.
14. The method according to claim 10 or 11, characterized in that, The instruction includes an opcode and an operand, where the opcode is used to indicate the instruction type, and the operand is used to indicate the data to be operated on and the unit address where the data is located.
15. A processor, characterized in that, The processor is applied to a terminal device, and the processor is used to: Obtain a relocation table corresponding to the executable file, where the relocation table is used to indicate the offset of at least one instruction with an offset to be corrected in the executable file and the instance member corresponding to the instruction with the offset to be corrected; Determine the position of the instruction with the offset to be corrected in the executable file according to the relocation table, and determine the instance member corresponding to the instruction with the offset to be corrected; Correct the offset of the instance member in the executable file.
16. The processor according to claim 15, characterized in that, When the processor obtains the relocation table corresponding to the executable file, it is specifically used to: When determining the update system, send the information of the executable file to the network device; receive the relocation table corresponding to the information of the executable file sent by the network device. Alternatively, send the software version information to the network device; receive the relocation table and the executable file corresponding to the software version information sent by the network device.
17. The processor according to claim 15 or 16, characterized in that, The relocation table further includes an instruction type, which is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected.
18. The processor according to claim 15 or 16, characterized in that, Before correcting the offset of the instance member in the executable file, the processor is further configured to: Calculate the current actual offset of the instance member, where the actual offset is the offset after correcting the original member offset of the instance member. When the processor corrects the offset of the instance member in the executable file, it is specifically configured to: correct the original member offset of the instance member in the executable file to the actual offset.
19. The processor according to claim 18, characterized in that, When the instance member is an instance member variable, when the processor calculates the actual offset of the instance member, it is specifically configured to: Starting from the highest-level class in the class inherited by the current instance member variable, traverse each class inherited by the current instance member variable, align and arrange the instance member variables in each class, and obtain the offsets of the instance member variables in each class. Determine that the sum of the offset of the previous instance member variable before the current instance member variable, the size of the previous instance member variable, and the size of the space padding between the current instance member variable and the previous instance member variable is the actual offset of the current instance member variable. When the instance member is a virtual function, when the processor calculates the actual offset of the instance member, it is specifically configured to: Starting from the highest-level class in the class inherited by the current virtual function, traverse each class inherited by the current virtual function, calculate the offsets of the pointers of the virtual functions in each class, and obtain the virtual function table. Determine that the sum of the offset of the pointer of the previous virtual function before the current virtual function in the virtual function table and the length of the pointer of the current virtual function is the actual offset of the current virtual function.
20. The processor according to claim 18, characterized in that, The relocation table includes an instruction type; the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected, and the encoding method includes an encoding position. When the processor corrects the original member offset of the instance member in the executable file to the actual offset, it is specifically configured to: According to the encoding method indicated by the instruction type, write the actual offset to the specific bit position in the instruction with the offset to be corrected in the executable file, and the specific bit position is the bit position indicated by the encoding position in the encoding method.
21. The processor according to any one of claims 15 - 16, 19 - 20, characterized in that, The instruction includes an opcode and an operand, the opcode is used to indicate the instruction type, and the operand is used to indicate the data to be operated on and the unit address where the data is located.
22. The processor according to any one of claims 15-16, 19-20, characterized in that, After correcting the offset of the instance member in the executable file, the processor is further configured to: Load and run the corrected executable file, where the corrected executable file runs according to the corrected instructions.
23. The processor according to claim 22, characterized in that, When the processor loads and runs the corrected executable file, it is specifically used for: Access the instance member according to the corrected instruction.
24. A processor, characterized in that, The processor is applied to a network device, and the processor is used for: Generate a relocation table corresponding to the executable file according to the instructions of at least one offset to be corrected, where the relocation table is used to indicate the offset of the instruction with the offset to be corrected in the executable file and the instance member corresponding to the instruction with the offset to be corrected; Send the relocation table to the electronic device, so that the electronic device corrects the offset of the instance member corresponding to the instruction with the offset to be corrected in the executable file according to the relocation table.
25. The processor according to claim 24, characterized in that, When the processor sends the relocation table to the electronic device, it is specifically used for: Receive the information of the executable file sent by the electronic device during system update; send the relocation table corresponding to the information of the executable file to the electronic device; Or, receive the software version information sent by the electronic device; send the relocation table corresponding to the software version information and the executable file to the electronic device.
26. The processor according to claim 24 or 25, characterized in that, The relocation table further includes an instruction type, and the instruction type is used to indicate the encoding method when encoding the immediate number in the instruction with the offset to be corrected.
27. The processor according to claim 24 or 25, characterized in that, The relocation table is specifically used for correcting the offset of the instance member corresponding to the instruction with the offset to be corrected in the executable file according to the current actual offset of the instance member; Wherein, the actual offset is the offset after correcting the original member offset of the instance member.
28. The processor according to claim 24 or 25, characterized in that, The instruction includes an opcode and an operand, the opcode is used to indicate the instruction type, and the operand is used to indicate the data to be operated and the unit address where the data is located.
29. An electronic device, characterized in that, The electronic device includes: a processor; The processor is used to execute the instructions in the computer-executable program code; when the processor executes the instructions, the instructions cause the electronic device to execute the method according to any one of claims 1 to 9.
30. The electronic device according to claim 29, characterized in that, The electronic device further includes a receiver; The receiver is used to receive the relocation table sent by the network device.
31. A network device, characterized in that, The electronic device includes: a processor and a transmitter; The processor is used to execute the instructions in the computer-executable program code; when the processor executes the instructions, the instructions cause the electronic device to execute the method according to any one of claims 10 to 14; The transmitter is used to send the relocation table to the electronic device.
32. A computer-readable storage medium, characterized in that, Including instructions, when running on a computer, causing the computer to execute the method according to any one of claims 1-9, or causing the computer to execute the method according to any one of claims 10-14.
33. A computer program product, comprising: A computer program, characterized in that when the computer program is executed by a processor, it is used to implement the method according to any one of claims 1-9, or used to implement the method according to any one of claims 10-14.
Citation Information
Patent Citations
Relocatable ELF file-based software quick loading method
CN106598674A