Program execution method and device, electronic equipment and readable storage medium

By calculating the offset value of the embedded system kernel program and the initial memory address of the relocation symbol, and updating the global offset table, the problem that the kernel program can only run at a specified location is solved, and the program can run normally and be fault-tolerant at any location.

CN116069409BActive Publication Date: 2026-02-06BEIJING ESWIN COMPUTING TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202211718651.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-29
Publication Date
2026-02-06
Estimated Expiration
2042-12-29

AI Technical Summary

Technical Problem

In embedded systems, the kernel program runs in a fixed location, which means the program can only be downloaded to the location specified by the linker script and cannot run normally in other locations, causing the program to crash.

Method used

By calculating the offset between the target running address and the specified running address, the initial memory address of the relocation symbol is obtained, and the global offset table is updated, enabling the program to run normally at any location.

Benefits of technology

This achieves location independence for program execution, avoids program crashes caused by inconsistent download addresses, and improves the program's fault tolerance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116069409B_ABST
    Figure CN116069409B_ABST
Patent Text Reader

Abstract

Embodiments of the present application provide a program execution method and device, electronic equipment and a readable storage medium. The method comprises: obtaining a specified running address and a target running address of a target program; calculating an offset value between the specified running address and the target running address; if the offset value is not equal to 0, obtaining a first variable of a relocation symbol from a preset relocation table; obtaining an initial memory address of the relocation symbol from a global offset table based on the first variable; calculating a target address of the relocation symbol according to the initial memory address and the offset value, and updating a value of the relocation symbol in the global offset table based on the target address; and executing the target program according to the updated global offset table. The present application can ensure normal running of a program in the case that the target program is downloaded to any location, thereby realizing location independence of program running.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of computer, and in particular, to a program execution method and device, electronic equipment and readable storage medium. BACKGROUND

[0002] In an embedded system, the running position of a kernel program executed on a nor-flash is fixed, which is defined by a linking script in a linking stage. The kernel program can only be downloaded to a specified position in the linking script and then be executed. SUMMARY

[0003] Embodiments of the present application provide a program execution method and device, electronic equipment and readable storage medium, which can download a target program to any position and realize position independence of program running.

[0004] In a first aspect, embodiments of the present application provide a program execution method, which comprises: obtaining a specified running address and a target running address of a target program, calculating an offset value between the specified running address and the target running address, if the offset value is not equal to 0, obtaining a first variable of a relocation symbol from a preset relocation table, the first variable being used to indicate a table entry of the relocation symbol in a global offset table, obtaining an initial memory address of the relocation symbol from the global offset table based on the first variable, calculating a target address of the relocation symbol according to the initial memory address and the offset value, and updating a value of the relocation symbol in the global offset table based on the target address, and executing the target program according to the updated global offset table.

[0005] Optionally, before the obtaining the initial memory address of the relocation symbol from the global offset table based on the first variable, the method further comprises: if a data type of the relocation symbol is a first data type, obtaining a second variable of the relocation symbol from the relocation table, the second variable being a value of the relocation symbol in the relocation table, and updating the first variable according to the second variable and the offset value, wherein the updated first variable is a sum of the second variable and the offset value; and the obtaining the initial memory address of the relocation symbol from the global offset table based on the first variable comprises: obtaining the initial memory address of the relocation symbol from the global offset table based on the updated first variable.

[0006] Optionally, the obtaining the initial memory address of the relocation symbol from the global offset table based on the first variable comprises: if a data type of the relocation symbol is a second data type, obtaining the initial memory address of the relocation symbol from the global offset table according to the first variable and the offset value; the initial memory address is an address recorded in a table entry corresponding to a sum of the first variable and the offset value in the global offset table.

[0007] Optionally, before the obtaining the first variable of the relocation symbol from the preset relocation table, the method further comprises: obtaining a first storage address and a first loading address of the relocation table, and a second storage address and a second loading address of the global offset table, obtaining the relocation table according to the first storage address, and loading the relocation table into the static storage with the first loading address as a starting address, obtaining the global offset table according to the second storage address, and loading the global offset table into the static storage with the second loading address as a starting address.

[0008] Optionally, before the obtaining the first storage address and the first loading address of the relocation table, and the second storage address and the second loading address of the global offset table, the method further comprises: generating a first pointer variable according to the first storage address of the relocation table, the first pointer variable being used to indicate the first storage address, generating a second pointer variable according to the first loading address of the relocation table, the second pointer variable being used to indicate the first loading address, generating a third pointer variable according to the second storage address of the global offset table, the third pointer variable being used to indicate the second storage address, generating a fourth pointer variable according to the second loading address of the global offset table, the fourth pointer variable being used to indicate the second loading address, linking a code segment of the target program with the first pointer variable, the second pointer variable, the third pointer variable and the fourth pointer variable in a linking script, and downloading the code segment, the first pointer variable, the second pointer variable, the third pointer variable and the fourth pointer variable into a non-volatile flash memory based on the linking script; data in the non-volatile flash memory has a read-only attribute.

[0009] Optionally, before the obtaining the first storage address and the first load address of the relocation table and the second storage address and the second load address of the global offset table, the method further comprises: configuring a temporary address for a system stack of the target program in a static memory; the obtaining the first storage address and the first load address of the relocation table and the second storage address and the second load address of the global offset table comprises: running the target program based on the temporary address to obtain the first storage address and the first load address of the relocation table and the second storage address and the second load address of the global offset table; and before the executing the target program according to the updated global offset table, the method further comprises: obtaining a target memory address of the system stack, and switching a start address of the system stack from the temporary start address to the target memory address.

[0010] Optionally, the relocation table comprises a relocation table entry of each relocation symbol, the relocation table entry comprises a first bit field, a second bit field and a third bit field; the first bit field is used for recording the first variable; the second bit field is used for recording the second variable; and the third bit field is used for recording a third variable, the third variable comprises a first parameter used for indicating a data type of the relocation symbol and a second parameter used for indicating an index value of the relocation symbol in a symbol table.

[0011] In a second aspect, an embodiment of the present application provides a program execution device, the device comprises:

[0012] an address obtaining module, configured to obtain a specified running address and a target running address of a target program; an offset value calculating module, configured to calculate an offset value between the specified running address and the target running address; a first variable obtaining module, configured to, if the offset value is not equal to 0, obtain a first variable of a relocation symbol from a preset relocation table, the first variable being used for indicating a table entry of the relocation symbol in a global offset table; an initial memory address obtaining module, configured to obtain an initial memory address of the relocation symbol from the global offset table based on the first variable; a global offset table updating module, configured to calculate a target address of the relocation symbol according to the initial memory address and the offset value, and update a value of the relocation symbol in the global offset table based on the target address; and a program executing module, configured to execute the target program according to the updated global offset table.

[0013] In a third aspect, an embodiment of the present application provides an electronic device, comprising: a processor and a memory, the processor executes a computer program stored in the memory to implement the program execution method in the first aspect.

[0014] In a fourth aspect, an embodiment of the present application provides a readable storage medium, wherein the readable storage medium stores computer instructions, and the computer instructions are executed by a processor to implement the program execution method in the first aspect.

[0015] The embodiments of the present application have the following advantages:

[0016] The embodiments of the present application provide a program execution method, which can ensure normal running of a program in the case that the program is downloaded to an arbitrary location, thereby realizing location independence of program running, avoiding the problem that the program cannot run due to inconsistency between an actual download address of the program and a specified address in a link script, and improving fault tolerance of program running. BRIEF DESCRIPTION OF DRAWINGS

[0017] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following will briefly introduce the drawings needed to be used in the description of the embodiments of the present application. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.

[0018] Figure 1 A flow chart of a program execution method according to an embodiment of the present application is shown;

[0019] Figure 2 A flow chart of a program execution method according to another embodiment of the present application is shown;

[0020] Figure 3 A structural block diagram of a program execution apparatus according to an embodiment of the present application is shown;

[0021] Figure 4 A structural schematic diagram of an electronic device provided by an embodiment of the present application is shown. DETAILED DESCRIPTION

[0022] The technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are some of the embodiments of the present application, but not all the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor fall within the scope of protection of the present application. It should be noted that the acquisition of various data related processes in the embodiments of the present application is performed on the premise of complying with the corresponding data protection regulations and policies of the country where the data is located, and with the authorization of the corresponding device owner.

[0023] Method embodiments

[0024] Figure 1A step flow chart of an embodiment of a program execution method of the present application is shown. Refer to Figure 1 , which can specifically include the following steps:

[0025] Step 101, obtaining a specified running address and a target running address of a target program;

[0026] Step 102, calculating an offset value between the specified running address and the target running address;

[0027] Step 103, if the offset value is not equal to 0, obtaining a first variable of a relocation symbol from a preset relocation table;

[0028] Step 104, obtaining an initial memory address of the relocation symbol from the global offset table based on the first variable;

[0029] Step 105, calculating a target address of the relocation symbol according to the initial memory address and the offset value, and updating a value of the relocation symbol in the global offset table based on the target address;

[0030] Step 106, executing the target program according to the updated global offset table.

[0031] It should be noted that the target program in the embodiment of the present application mainly refers to a kernel program of an embedded system. Specifically, the target program can be obtained in the following manner. A compilation parameter, for example, -fpie, can be selected in a compilation stage, so as to compile an object file, i.e., an.o file, by using a.c or.cpp language, and then a linking script is used to determine a linking parameter, such as static linking (for example, -static, -no-dynamic-linker) and a linker option (for example, -pie), for script linking, so as to obtain the target program.

[0032] The specified running address refers to a program running address defined by a linking script in a linking stage. The target running address refers to an actual running address of the target program. As an example, the target running address can be obtained by using a first instruction pc value of a running target program recorded by a specified general register.

[0033] In this embodiment, the specified runtime address and the target runtime address may be the same or different. If the target runtime address is the same as the specified runtime address, it means that the target program has been downloaded to the address specified in the linker script. After downloading the target program according to the existing processing flow, the target program can run normally. If the target runtime address is different from the specified runtime address, it means that the target program has not been downloaded to the address specified in the linker script. In this case, the storage addresses of various symbols in the data segment of the target program, especially global symbols, have changed. The addresses recorded in the global offset table are no longer the actual storage addresses of the corresponding symbols. This leads to the inability to correctly read data during program execution, resulting in program crashes.

[0034] To achieve location independence of program execution and ensure that the program can run normally even when it is downloaded to an address other than the address specified in the linker script, this application provides a program execution method that can ensure the normal operation of the program even when the specified execution address of the target program is inconsistent with the target execution address.

[0035] Specifically, the offset between the target execution address and the specified execution address is calculated. If this offset is not equal to 0, it indicates that the actual execution address of the target program, i.e., the target execution address, is not the specified execution address in the linker script. In this case, the first variable of the relocation symbol is read from the preset relocation table. The preset data entries in the relocation table in the target program are used to record the addresses of the relocation symbols in the global offset table. Relocation symbols refer to external functions or global variables that need to be relocated during program execution. The first variable indicates the entry of the relocation symbol in the global offset table. The global offset table is a preset data entry in the target program used to store the exact addresses of global variables and external functions in the target program in memory.

[0036] Next, the initial memory address of the relocation symbol can be obtained from the global offset table based on the first variable. The initial memory address refers to the address of the relocation symbol in memory before the target execution address of the target program is offset.

[0037] It is understandable that when there is an offset between the target execution address and the specified execution address of the target program, the size and storage relationship of the data in the code segment and data segment of the target program do not change. Therefore, the target address of the relocation symbol can be calculated based on the initial memory address and the offset value, that is, the address of the relocation symbol in memory after the target execution address of the target program has shifted. Assuming the initial memory address of the relocation symbol is PC0, the target address is PC, and the offset between the target execution address and the specified execution address of the target program is offset, then PC = PC0 + offset.

[0038] The value of the relocation symbol in the global offset table is updated based on the target address, and in the process of executing the target program, data acquisition and / or function and variable calling can be performed by reading the updated address in the global offset table, thereby accurately ensuring the normal operation of the target program.

[0039] Considering that the relocation symbol has different data types, the compilation process of different data types can be different, and therefore the application provides different relocation methods for different types of relocation symbols.

[0040] In an optional embodiment of the application, before step 104, i.e., obtaining the initial memory address of the relocation symbol from the global offset table based on the first variable, the method can further include:

[0041] Step S11, if the data type of the relocation symbol is a first data type, obtaining a second variable of the relocation symbol from the relocation table, the second variable being the value of the relocation symbol in the relocation table;

[0042] Step S12, updating the first variable according to the second variable and the offset value, wherein the updated first variable is the sum of the second variable and the offset value;

[0043] For example, step 104, i.e., obtaining the initial memory address of the relocation symbol from the global offset table based on the first variable, can include:

[0044] Step S13, obtaining the initial memory address of the relocation symbol from the global offset table based on the updated first variable; or,

[0045] Step S21, if the data type of the relocation symbol is a second data type, obtaining the initial memory address of the relocation symbol from the global offset table according to the first variable and the offset value.

[0046] The second variable is the value of the relocation symbol in the relocation table.

[0047] When the data types of the relocation and symbol are different, in the embodiments of the application, each relocation symbol in the relocation table can be sequentially relocated according to the processing steps of steps S11 to S13 or step S21. In summary, for each data item, the method of obtaining the corresponding initial memory address is determined by judging the data type of the relocation symbol in the data item.

[0048] Specifically, Figure 2A flowchart of another embodiment of the program execution method of this application is shown. Here, N represents the number of data items in the relocation table, that is, the number of relocation symbols, and N is a positive integer. Each data item records the variable information of a relocation symbol, including but not limited to a first variable and a second variable. P(i) represents the sum of the second variable and the offset value in the i-th data item, and Q(i) represents the sum of the first variable and the offset value in the i-th data item.

[0049] If the data type of the relocation symbol is a first data type, then the first variable is updated according to the obtained second variable and offset value, i.e., as follows. Figure 2 As shown in steps S121-S122, the sum P(i) of the second pass and the offset value is calculated, and the first variable in the i-th data item is updated to P(i). Then, according to step S13, the initial memory address of the relocation symbol is obtained from the global offset table based on P(i). If the data type of the relocation symbol belongs to the second data type, then according to... Figure 2 The method described in step S211 directly calculates the sum of the first variable and the offset value Q(i). Then, according to step S212, based on the entry of the relocation symbol indicated by Q(i) in the global offset table, the initial memory address of the i-th relocation symbol is obtained from the global offset table. After executing the above steps S121-S122, S13 or steps S211-S212, the global offset table is updated as described in step 105 above.

[0050] The data relocation process ends when the method described in steps S121-S122, S13, 105 or S211-S212, 105 has been executed for each data item in the relocation table, i.e., when i is greater than N. Then the target program is executed according to the updated global offset table.

[0051] As an example, running a RISC-V processor such as Figure 2 In the process shown, if the relocation symbol is of type R_RISCV_RELATIVE (i.e., the first data type), the second variable of the relocation symbol is obtained from the relocation table, and the value of the first variable is updated to the sum of the second variable and the offset value. The initial memory address of the relocation symbol is then obtained from the global offset table based on the updated first variable. If the relocation symbol is of type R_RISCV_32 (i.e., the second data type), the sum of the first variable and the offset is calculated, and the initial memory address of the relocation symbol is obtained from the global offset table based on the sum of the two.

[0052] In an optional embodiment of this application, before step 103, i.e., before obtaining the first variable of the relocation symbol from the preset relocation table, the method further includes:

[0053] Step S300, obtaining a first storage address and a first load address of a relocation table, and a second storage address and a second load address of a global offset table;

[0054] Step S301, obtaining the relocation table according to the first storage address, and loading the relocation table into a static memory with the first load address as a starting address;

[0055] Step S302, obtaining the global offset table according to the second storage address, and loading the global offset table into the static memory with the second load address as a starting address.

[0056] The first storage address represents the start position and the end position of the relocation table in the memory layout of the target program, and the first load address represents the address of the relocation table to be loaded into the static memory after running the target program; the second storage address represents the start position and the end position of the global offset table in the memory layout of the target program, and the second load address represents the address of the global offset table to be loaded into the static memory after running the target program.

[0057] Before reading the relocation table to obtain the first variable and obtaining the initial memory address of the relocation symbol from the global offset table according to the first variable, the relocation table and the global offset table need to be loaded into the static memory.

[0058] Taking nor-flash as an example, before the target program is loaded into the static memory, the data segment (data), the code segment (text), the global offset table (got), and the relocation table (rely.dy) of the program are all downloaded onto the nor-flash. The data segment stores initialized global variables and static variables. The got segment saves the data related to the global offset table. The rely.dy segment saves the information related to the relocation table. The bss segment saves the data that is not initialized and has an initial value of 0.

[0059] Exemplarily, the first storage address of the relocation table can be represented as _srela and _erela, and _srela and _erela record the start position and the end position of the relocation table in the memory layout of the target program respectively, and the first load address can be represented as _e__rela__ronly.

[0060] The second storage address of the global offset table can be represented as _sgot and _egot, and _sgot and _egot record the start position and the end position of the global offset table in the memory layout of the target program respectively, and the first load address can be represented as _e__got__ronly.

[0061] The storage address of the data segment can be represented as _sdata and _edata, and the _sdata and _edata respectively record the start position and the end position of the data segment in the memory layout of the target program. The first load address can be represented as _e_data_ronly.

[0062] The storage address of the stack segment can be represented as _sbss and _ebss, and the _sbss and _ebss respectively record the start position and the end position of the stack segment in the memory layout of the target program.

[0063] When the program is running, after the first storage address and the first load address of the relocation table and the second storage address and the second load address of the global offset table are obtained, the relocation table and the global offset table are obtained according to the first storage address and the second storage address respectively, and the relocation table is loaded into the static storage memory with the first load address as the starting address. The global offset table is loaded into the static storage memory according to the second load address. Of course, in this process, similar to the relocation table and the global offset table, the data segment of the target program can also be loaded into the static storage memory (SRAM) through the corresponding storage address and load address, so that the code segment is executed on the norflash, and the data segment, the global offset table, the relocation table, etc. are loaded into the static storage memory.

[0064] In an optional embodiment of the present application, before step S300, that is, before the first storage address and the first load address of the relocation table and the second storage address and the second load address of the global offset table are obtained, the method further comprises:

[0065] Step S400, generating a first pointer variable according to the first storage address of the relocation table, the first pointer variable being used to indicate the first storage address;

[0066] Step S401, generating a second pointer variable according to the first load address of the relocation table, the second pointer variable being used to indicate the first load address;

[0067] Step S402, generating a third pointer variable according to the second storage address of the global offset table, the third pointer variable being used to indicate the second storage address;

[0068] Step S403, generating a fourth pointer variable according to the second load address of the global offset table, the fourth pointer variable being used to indicate the second load address;

[0069] Step S404, linking the code segment of the target program with the first pointer variable, the second pointer variable, the third pointer variable and the fourth pointer variable in the link script.

[0070] Step S405, downloading the code segment, the first pointer variable, the second pointer variable, the third pointer variable and the fourth pointer variable into the non-volatile flash memory based on the linking script; the data in the non-volatile flash memory has a read-only attribute.

[0071] The files of the initial stage target program are all downloaded to the non-volatile flash memory (nor flash), and other memory segments in the target program except the code segment are not loaded into the SRAM, so that the related data of the starting positions of the data segment, the global offset table, the relocation table and the stack segment cannot be obtained, and thus these segments cannot be loaded into the SRAM, and therefore it is necessary to generate the corresponding pointer variables according to the storage address information and the loading address information of the relocation table, the global offset table and the like in the linking script in the linking stage before the program is downloaded, establish the link between the pointer variables and the code segment, and then load the code segment, the first pointer variable, the second pointer variable, the third pointer variable and the fourth pointer variable and the data of other memory segments into the nor flash when the program is downloaded to the nor flash, so that the storage address and the loading address information of different memory information can be obtained by the code segment through the linked call pointer when the program runs, thereby realizing the correct loading of the data of the data segment, the global offset table and the relocation table.

[0072] Taking the loading address of the relocation table as an example, the first loading address of the relocation table is denoted as e_rela_ronly, the second pointer variable for indicating the first loading address of the relocation table is constructed as static const unit32_t_img_e_rela_ronly_PARA_HANDLE, and the first loading address is assigned to the second pointer variable, and then the link between the code segment and the second pointer variable is established in the code segment through the running of the linking script, and the code segment and the pointer variable and other memory segments are loaded to the nor flash, so that the first loading address of the relocation table can be obtained through the second pointer variable when the code segment is executed. By constructing the pointer variable, the address information indicated by the pointer variable has a read-only attribute, and the information with the read-only attribute can be combined with the code segment, and further this part of data can be accessed without being loaded into the sram, thereby obtaining the storage address and the loading address of the data segment, the global offset table and the relocation table.

[0073] In an optional embodiment of the present application, before the step S300, i.e., obtaining the first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table, the method further comprises:

[0074] allocating a temporary address for a system stack of the target program in the static memory;

[0075] The first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table can be obtained by running the target program based on the temporary address.

[0076] The first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table can be obtained by running the target program based on the temporary address.

[0077] The target program can be executed according to the updated global offset table.

[0078] The target memory address of the system stack is obtained, and the start address of the system stack is switched from the temporary start address to the target memory address.

[0079] Since the running address of the target program is offset, the start position of the system stack defined in the target program cannot be obtained before the data segment, the global offset table and the relocation table are loaded into the sram. Therefore, a temporary address is allocated for the system stack of the target program to run the target program, and the first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table are obtained. After the address information of the relocation symbol in the global offset table is updated, the target program is executed according to the running instruction of the program, the target memory address of the system stack corresponding to the instruction is obtained in the global offset table, and the start address of the system stack is switched to the target memory address, so that the correct running of the target program after the running address is offset is realized.

[0080] In an optional embodiment of the present application, the relocation table of the target program includes a relocation table entry of each relocation symbol, and the relocation table entry includes a first bit field, a second bit field and a third bit field. The first bit field is used to record the first variable, the second bit field is used to record the second variable, and the third bit field is used to record a third variable. The third variable includes a first parameter for indicating the data type of the relocation symbol and a second parameter for indicating the index value of the relocation symbol in the symbol table.

[0081] As an example, the data structure of a relocation table entry elf_rela32 is taken as an example. The data format (typedef struct) stored in the elf_rela32 table entry is as follows:

[0082]

[0083] Wherein, the Elf32_Addr r_offset can refer to the data of the first bit field, and is used to record the corresponding entry of the first variable, i.e., the relocation symbol, in the global offset table; the Elf32_Sword r_addend can refer to the data of the second bit field, and is used to record the corresponding value of the second variable, i.e., the relocation symbol, to execute the method as described in steps S100-S101; the Elf32_Word r_info can refer to the data of the third bit field, and is used to record the third variable, and specifically can record the values of two parts, the (r_info & 0xff) part identifies the type of the relocation symbol, including the first data type described in step S100 and the second data type described in step S200, and (r_info >> 8) identifies the index value of the relocation symbol in the symbol table, and the symbol table is used to record all variables and functions in the target program.

[0084] In summary, the embodiments of the present application provide a program execution method, which can guarantee the normal operation of the program in the case that the target program is downloaded to any location, thereby realizing the location independence of the program operation, avoiding the problem that the program cannot be operated due to the inconsistency between the actual download address of the program and the specified address in the link script, and improving the fault tolerance of the program operation.

[0085] Device embodiments

[0086] Figure 3 A structural block diagram of a program execution device of the embodiments of the present application is shown, and the device 200 can include:

[0087] The address acquisition module 201 is configured to acquire the specified running address and the target running address of the target program.

[0088] The offset value calculation module 202 is configured to calculate the offset value between the specified running address and the target running address.

[0089] The first variable acquisition module 203 is configured to acquire the first variable of the relocation symbol from a preset relocation table if the offset value is not equal to 0, and the first variable is used to indicate the entry of the relocation symbol in the global offset table.

[0090] The initial memory address acquisition module 204 is configured to acquire the initial memory address of the relocation symbol from the global offset table based on the first variable.

[0091] The global offset table updating module 205 is configured to calculate the target address of the relocation symbol according to the initial memory address and the offset value, and update the value of the relocation symbol in the global offset table based on the target address.

[0092] The program execution module 206 is configured to execute the target program according to the updated global offset table.

[0093] Optionally, the apparatus can further include:

[0094] The second variable obtaining module is configured to, before the first variable obtaining module obtains the initial memory address of the relocation symbol from the global offset table based on the first variable, obtain a second variable of the relocation symbol from the relocation table if a data type of the relocation symbol is a first data type, the second variable being a value of the relocation symbol in the relocation table.

[0095] The first variable updating module is configured to, before the first variable obtaining module obtains the initial memory address of the relocation symbol from the global offset table based on the first variable, update the first variable according to the second variable and the offset value, wherein the updated first variable is a sum of the second variable and the offset value.

[0096] Correspondingly, the initial memory address obtaining module can include:

[0097] The initial memory address first obtaining submodule is configured to obtain the initial memory address of the relocation symbol from the global offset table based on the updated first variable.

[0098] Optionally, the initial memory address obtaining module can include:

[0099] The initial memory address second obtaining submodule is configured to, if the data type of the relocation symbol is a second data type, obtain the initial memory address of the relocation symbol from the global offset table according to the first variable and the offset value, the initial memory address being an address recorded in a table entry corresponding to a sum of the first variable and the offset value in the global offset table.

[0100] Optionally, the apparatus can further include:

[0101] The storage address and load address obtaining module is configured to, before the first variable obtaining module obtains the first variable of the relocation symbol from the preset relocation table, obtain a first storage address and a first load address of the relocation table, and a second storage address and a second load address of the global offset table.

[0102] The relocation table loading module is configured to, before the first variable obtaining module obtains the first variable of the relocation symbol from the preset relocation table, obtain the relocation table according to the first storage address, and load the relocation table into the static storage memory with the first load address as a starting address.

[0103] The global offset table loading module is configured to obtain the global offset table according to the second storage address before obtaining the first variable of the relocation symbol from the preset relocation table, and load the global offset table into the static storage memory with the second loading address as a starting address.

[0104] Optionally, the apparatus can further include:

[0105] The first pointer variable generation module is configured to generate a first pointer variable according to the first storage address of the relocation table before obtaining the first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table, the first pointer variable being used to indicate the first storage address.

[0106] The second pointer variable generation module is configured to generate a second pointer variable according to the first loading address of the relocation table before obtaining the first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table, the second pointer variable being used to indicate the first loading address.

[0107] The third pointer variable generation module is configured to generate a third pointer variable according to the second storage address of the global offset table before obtaining the first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table, the third pointer variable being used to indicate the second storage address.

[0108] The fourth pointer variable generation module is configured to generate a fourth pointer variable according to the second loading address of the global offset table before obtaining the first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table, the fourth pointer variable being used to indicate the second loading address.

[0109] The code segment linking module is configured to link the code segment of the target program with the first pointer variable, the second pointer variable, the third pointer variable and the fourth pointer variable in a linking script before obtaining the first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table.

[0110] The non-volatile flash downloading submodule is configured to download the code segment, the first pointer variable, the second pointer variable, the third pointer variable and the fourth pointer variable into a non-volatile flash based on the linking script before obtaining the first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table, the data in the non-volatile flash having a read-only attribute.

[0111] Optionally, the apparatus can further include:

[0112] a temporary address allocation module configured to allocate a temporary address for a system stack of the target program in a static memory before the first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table are acquired;

[0113] Correspondingly, the storage address and loading address acquisition module can include:

[0114] a storage address and loading address acquisition sub-module configured to run the target program based on the temporary address to acquire the first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table;

[0115] The program execution module can include:

[0116] a system stack address switching sub-module configured to acquire a target memory address of the system stack and switch a start address of the system stack from the temporary start address to the target memory address.

[0117] For the apparatus embodiment, since it is basically similar to the method embodiment, it is described more simply, and the related parts refer to the part of the method embodiment.

[0118] Figure 4 is a structural block diagram of an electronic device 600 for program execution according to an embodiment of the present application. For example, the electronic device 600 can be a mobile phone, a computer, a digital broadcast terminal, a messaging device, a game console, a tablet device, a medical device, a fitness device, a personal digital assistant, etc.

[0119] Referring to Figure 4 , the electronic device 600 can include one or more of the following components: a processing component 602, a memory 604, a power supply component 606, a multimedia component 608, an audio component 610, an input / output (I / O) interface 612, a sensor component 614, and a communication component 616.

[0120] The processing component 602 usually controls overall operations of the electronic device 600, such as operations associated with displaying, making phone calls, data communications, camera operations and recording operations. The processing component 602 can include one or more processors 620 to execute instructions to complete all or part of the steps of the program execution method described above. In addition, the processing component 602 can include one or more modules to facilitate interaction between the processing component 602 and other components. For example, the processing component 602 can include a multimedia module to facilitate the interaction between the multimedia component 608 and the processing component 602.

[0121] Memory 604 is configured to store various types of data to support operations of electronic device 600. Examples of such data include instructions for any application or method operating on electronic device 600, contact data, phonebook data, messages, pictures, videos, and the like. Memory 604 can be implemented by any type of volatile or nonvolatile storage devices or a combination thereof such as static random access memory (SRAM), electrically erasable programmable read only memory (EEPROM), erasable programmable read only memory (EPROM), programmable read only memory (PROM), read only memory (ROM), magnetic storage, flash memory, magnetic or optical disks.

[0122] Power supply component 606 supplies electrical power for various components of electronic device 600. Power supply component 606 can include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing electrical power for electronic device 600.

[0123] Multimedia component 608 includes a screen providing an output interface between electronic device 600 and a user. In some embodiments, the screen can include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen can be implemented as a touch screen to receive an input signal from a user. The touch panel includes one or more touch sensors to sense a touch, a slide, and a gesture on the touch panel. The touch sensor can not only sense a boundary of a touching or a sliding action, but also detect duration and pressure related to the touching or sliding action. In some embodiments, multimedia component 608 includes a front camera and / or a back camera. The front and / or back camera can receive external multimedia data when electronic device 600 is in an operating mode, such as a photographing mode or a video mode. Each of the front and back camera can be a fixed optical lens system or have a focal length and optical zoom capability.

[0124] Audio component 610 is configured to output and / or input audio signals. For example, audio component 610 includes a microphone (MIC) configured to receive external audio signals when electronic device 600 is in an operating mode, such as a call mode, a recording mode, and a voice recognition mode. The received audio signals can be further stored in memory 604 or transmitted via communication component 616. In some embodiments, audio component 610 also includes a speaker for outputting audio signals.

[0125] I / O interface 612 provides an interface between processing component 602 and peripheral interface modules, which can be a keypad, a click wheel, buttons, and the like. The buttons can include, but are not limited to, a home button, a volume button, a start button, and a lock button.

[0126] The sensor component 614 includes one or more sensors for providing status assessments for various aspects of the electronic device 600. For example, the sensor component 614 can detect an open / closed position of the electronic device 600, relative positioning of components, such as a display and keypad of the device 600, a change in position of the electronic device 600 or a component of the electronic device 600, presence or absence of user contact with the electronic device 600, orientation or acceleration / deceleration / g-force and temperature changes of the electronic device 600. The sensor component 614 can include an optical sensor that is configured to detect ambient light, a proximity sensor configured to detect proximity of an object, a motion sensor configured to detect motion of the electronic device 600, a temperature sensor configured to detect temperature changes of the electronic device 600, a humidity sensor configured to detect humidity changes of the electronic device 600, an acceleration sensor configured to detect an acceleration of the electronic device 600, a gyroscope sensor configured to detect a rotation of the electronic device 600, a magnetic sensor configured to detect a magnetic field of the electronic device 600, a pressure sensor configured to detect pressure changes of the electronic device 600, or a chemical sensor configured to detect a chemical change of the electronic device 600.

[0127] The communication component 616 is configured to facilitate wired or wireless communication between the electronic device 600 and other devices. The electronic device 600 can access a wireless network based on a communication standard, such as WiFi, 2G, or 3G, or a combination thereof. In an example embodiment, the communication component 616 receives a broadcast signal or broadcast related information from an external broadcast management system via a broadcast channel. In an example embodiment, the communication component 616 further includes a Near Field Communication (NFC) module to facilitate short-range communication. For example, the NFC module can be implemented based on Radio Frequency Identification (RFID) techniques, Infrared Data Association (IrDA) techniques, Ultra-WideBand (UWB) techniques, Bluetooth (BT) techniques, and other techniques.

[0128] In an example embodiment, the electronic device 600 can be implemented using one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), controllers, micro-controllers, microprocessors, or other electronic elements for performing the above-described program execution methods.

[0129] In an example embodiment, a non-transitory computer-readable storage medium including instructions, such as the memory 604 including instructions, is also provided, which can be executed by the processor 620 of the electronic device 600 to complete the above-described methods. For example, the non-transitory computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disc, and an optical data storage device, etc.

[0130] A non-transitory computer readable storage medium, when instructions in the storage medium are executed by a processor of an electronic device (a server or a terminal), enable the processor to perform Figure 1 the program execution method.

[0131] The various embodiments are described in a progressive manner in the specification, each of which focuses on the differences from other embodiments, and the same or similar parts between the various embodiments can be referred to each other. For the device disclosed by the embodiments, since it corresponds to the method disclosed by the embodiments, the description is relatively simple, and the relevant parts can be referred to the method part.

[0132] Finally, it should also be noted that, in this paper, relationship terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between the entities or operations. Moreover, the terms "include", "contain" or any other variants thereof are intended to cover non-exclusive inclusion, so that the process, method, article or device including a series of elements not only includes those elements, but also includes other elements not explicitly listed or inherent to such process, method, article or device. Without more limitations, the element defined by the statement "including a" does not exclude the presence of other identical elements in the process, method, article or device including the element.

[0133] The above provides a kind of program execution method, device, electronic equipment and readable storage medium provided by the present application, the principle and implementation mode of the present application are described in this paper by specific examples, the above example is only for helping to understand the method of the present application and its core idea;For those skilled in the art, according to the idea of the present application, there will be changes in specific implementation mode and application range, and the above description should not be understood as the limitation of the present application.

Claims

1. A program execution method characterized by comprising: The method comprises: obtaining a specified running address and a target running address of a target program; calculating an offset value between the specified running address and the target running address; if the offset value is not equal to 0, obtaining a first variable of a relocation symbol from a preset relocation table, the first variable being used to indicate a table entry of the relocation symbol in a global offset table; if a data type of the relocation symbol is a first data type, obtaining a second variable of the relocation symbol from the relocation table, the second variable being a value of the relocation symbol in the relocation table; updating the first variable according to the second variable and the offset value, wherein the updated first variable is a sum of the second variable and the offset value; obtaining an initial memory address of the relocation symbol from the global offset table based on the first variable; calculating a target address of the relocation symbol according to the initial memory address and the offset value, and updating a value of the relocation symbol in the global offset table based on the target address; executing the target program according to the updated global offset table; the obtaining of the initial memory address of the relocation symbol from the global offset table based on the first variable comprises: the obtaining of the initial memory address of the relocation symbol from the global offset table based on the updated first variable.

2. The method of claim 1, wherein, the obtaining of the initial memory address of the relocation symbol from the global offset table based on the first variable comprises: if a data type of the relocation symbol is a second data type, obtaining the initial memory address of the relocation symbol from the global offset table according to the first variable and the offset value; the initial memory address being an address recorded in a table entry corresponding to a sum of the first variable and the offset value in the global offset table.

3. The method of claim 1, wherein, before the obtaining of the first variable of the relocation symbol from the preset relocation table, the method further comprises: obtaining a first storage address and a first loading address of the relocation table, and a second storage address and a second loading address of the global offset table; obtaining the relocation table according to the first storage address, and loading the relocation table into a static storage with the first loading address as a starting address; obtaining the global offset table according to the second storage address, and loading the global offset table into the static storage with the second loading address as a starting address.

4. The method of claim 3, wherein, before the obtaining of the first storage address and the first loading address of the relocation table, and the second storage address and the second loading address of the global offset table, the method further comprises: generating a first pointer variable according to the first storage address of the relocation table, the first pointer variable being used to indicate the first storage address; generating a second pointer variable according to the first loading address of the relocation table, the second pointer variable being used to indicate the first loading address; generating a third pointer variable according to the second storage address of the global offset table, the third pointer variable being used to indicate the second storage address; generating a fourth pointer variable according to the second loading address of the global offset table, the fourth pointer variable being used to indicate the second loading address; linking a code segment of the target program with the first pointer variable, the second pointer variable, the third pointer variable and the fourth pointer variable in a linking script; downloading the code segment, the first pointer variable, the second pointer variable, the third pointer variable and the fourth pointer variable into a non-volatile flash memory based on the linking script; data in the non-volatile flash memory has a read-only attribute.

5. The method of claim 3, wherein, Before the first storage address and the first loading address of the relocation table and the second storage address and the second loading address of the global offset table are obtained, the method further comprises: configuring a temporary start address for a system stack of the target program in a static memory; Before the target program is executed according to the updated global offset table, the method further comprises: obtaining a target memory address of the system stack, and switching the start address of the system stack from the temporary start address to the target memory address.

6. The method of claim 1, wherein, The relocation table comprises a relocation table entry of each relocation symbol, the relocation table entry comprising a first bit field, a second bit field and a third bit field; the first bit field is used for recording the first variable; the second bit field is used for recording the second variable; the third bit field is used for recording a third variable, the third variable comprising a first parameter for indicating a data type of the relocation symbol, and a second parameter for indicating an index value of the relocation symbol in a symbol table.

7. A program execution device characterized by comprising: The device comprises: an address obtaining module configured to obtain a specified running address and a target running address of a target program; an offset value calculating module configured to calculate an offset value between the specified running address and the target running address; a first variable obtaining module configured to, if the offset value is not equal to 0, obtain a first variable of a relocation symbol from a preset relocation table, the first variable being used for indicating a table entry of the relocation symbol in a global offset table; a second variable obtaining module configured to, before the initial memory address of the relocation symbol is obtained from the global offset table based on the first variable, obtain a second variable of the relocation symbol from the relocation table if a data type of the relocation symbol is a first data type, the second variable being a value of the relocation symbol in the relocation table; a first variable updating module configured to, before the initial memory address of the relocation symbol is obtained from the global offset table based on the first variable, update the first variable according to the second variable and the offset value, wherein the updated first variable is a sum of the second variable and the offset value; an initial memory address obtaining module configured to obtain the initial memory address of the relocation symbol from the global offset table based on the first variable; a global offset table updating module configured to calculate a target address of the relocation symbol according to the initial memory address and the offset value, and update a value of the relocation symbol in the global offset table based on the target address; a program executing module configured to execute the target program according to the updated global offset table; the initial memory address obtaining module comprises: An initial memory address first obtaining submodule is configured to obtain an initial memory address of the relocation symbol from the global offset table based on the updated first variable.

8. An electronic device, comprising: The application further discloses a computer readable storage medium storing the computer program. The application further discloses a processor and a memory, wherein the processor executes a computer program stored in the memory to realize the program execution method in any one of claims 1 to 6.

9. A readable storage medium, characterized by, The application further discloses a computer readable storage medium storing the computer program.

Citation Information

Patent Citations

  • Application program running method and device, terminal equipment and storage medium

    CN112130926A