CN111124550B - Program dynamic loading method and device and storage medium - Google Patents

Program dynamic loading method and device and storage medium Download PDF

Info

Publication number
CN111124550B
CN111124550B CN202010221160.4A CN202010221160A CN111124550B CN 111124550 B CN111124550 B CN 111124550B CN 202010221160 A CN202010221160 A CN 202010221160A CN 111124550 B CN111124550 B CN 111124550B
Authority
CN
China
Prior art keywords
relocation
address
data
program file
file
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202010221160.4A
Other languages
Chinese (zh)
Other versions
CN111124550A (en
Inventor
韩辉
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Acoinfo Technology Co ltd
Original Assignee
Beijing Acoinfo Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Acoinfo Technology Co ltd filed Critical Beijing Acoinfo Technology Co ltd
Priority to CN202010221160.4A priority Critical patent/CN111124550B/en
Publication of CN111124550A publication Critical patent/CN111124550A/en
Application granted granted Critical
Publication of CN111124550B publication Critical patent/CN111124550B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/445Program loading or initiating
    • G06F9/44505Configuring for program initiating, e.g. using registry, configuration files

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The invention provides a program dynamic loading method, a device and a storage medium, wherein the method comprises the following steps: compiling, namely compiling the source code into an address-independent program file by adopting a compiling parameter; a relocation preprocessing step, after the compilation is finished, computing a relocation address and a logic address which needs to be filled in the relocation address for the program file according to the type of the relocation item, wherein the preprocessed program file can be downloaded to any position of the FLASH; and a loading step, loading the data required by the program operation based on the calculated relocation address and the logic address required to be filled in the relocation address, and directly operating the program file in the FLASH. The method avoids the process that the APP file is copied to the RAM from the FLASH, improves the application starting speed, reduces the program volume, does not limit the application starting address or limit the application storage address, realizes the direct operation of the APP on the FLASH, is suitable for being operated on small-sized equipment such as an MCU (microprogrammed control unit), and supports the debugging of the dynamically loaded APP.

Description

Program dynamic loading method and device and storage medium
Technical Field
The invention relates to the technical field of computer program compilation, in particular to a method and a device for dynamically loading a program and a storage medium.
Background
The existing technical schemes for dynamically loading application programs in an operating system mainly include two types:
the first method adopts an integral loading mode: firstly, compiling and linking out an application program with a fixed running address; then, when the application program is dynamically loaded, the executable file of the application program is integrally copied to a memory area of a specified address; and finally executing the program codes from the memory.
Secondly, a dynamic file connection mode is adopted: firstly, compiling an address independent code by using gcc-shared-fPIC; then dynamically loading to 0x00000000 address; during operation, the internal data of the module is acquired by adopting the PC + offset, the address of the GOT table is acquired by adopting the PC + offset, and then the data and the code address between the modules are acquired from the GOT table.
The defects in the prior art are mainly as follows:
in the first way, the loading process needs to copy the APP executable file to the RAM, and the loading speed is slow. In the second mode, the code segment and the data segment must be loaded into the memory as a whole and relocated, since the executable file of the APP needs to be copied into the RAM for execution, the more complex APP cannot be loaded on the MCU in the RAM shortage, when the executable file of the APP is larger, dynamic loading cannot be realized due to memory limitations, there are multiple relocation methods in the original ELF file, the loader needs to perform corresponding search and operation according to different methods, and then modify the data of the corresponding address in the memory, the relocation efficiency is lower, in addition, since there is a lot of run-time irrelevant information in the ELF file generated by compiling, part is loading relevant data, part is debugging relevant data, the ELF has a larger volume, and occupies a lot of FLASH space.
Disclosure of Invention
The present invention provides the following technical solutions to overcome the above-mentioned drawbacks in the prior art.
A method for dynamic loading of a program, the method comprising:
compiling, namely compiling the source code into an address-independent program file by adopting a compiling parameter;
a relocation preprocessing step, namely calculating the program file according to the type of the relocation item to obtain a relocation address and a logic address needing to be filled in the relocation address;
and a loading step, after downloading the program file of the relocation preprocessing to the FLASH, loading the data required by the program operation based on the calculated relocation address and the logic address required to be filled in the relocation address, and directly operating the program file in the FLASH.
Furthermore, the operation of compiling the source code into the address-independent program file by using the compiling parameter includes: setting the compiling parameters of the compiler as follows: -fPIC and-mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9, -fPIC for generating a location-independent compiled code, -mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9 for generating a code with a code section separate from a data section, and the R9 register is designated as a code section register, wherein the program file comprises separate code and data segments and, after compilation, will invoke an elf2app tool to modify the variable access instructions of the data segments into an R9+ offset manner, the elf2app identifying different variable access instructions and a jump instruction, and calculating a target address according to the structures of the variable access instruction and the jump instruction, and generating a corresponding instruction accessed through the R9 register according to the target address to replace the existing corresponding instruction.
Furthermore, during relocation preprocessing, for all relocation items in the ELF, search and operation are performed according to the types of the relocation items, and the ELF relocation items are converted into relocation arrays and stored in an executable program file, where the relocation arrays include relocation addresses and logic addresses to which the addresses should be written. Further, the executable program file is processed after the relocation preprocessing is completed, and the processing comprises: deleting the joint-free area and the data of the executable program file, assembling the text segment and the data segment into a new executable program file, and recording the information required during loading into the file header of the new executable program file, wherein the new executable program file can be downloaded to any position of the FLASH.
Furthermore, in the loading process, the positions of the text field and the data field are calculated according to the information in the file header, the data field is copied, the data field starting address is set to R9, then the rest repositioning operation is executed according to the addresses of the text field and the data field and the repositioning array in the file header, and the program file is directly run in the FLASH after the repositioning operation is completed.
The invention also provides a program dynamic loading device, which comprises:
the compiling unit is used for compiling the source code into an address-independent program file by adopting compiling parameters;
the relocation preprocessing unit is used for calculating a relocation address and a logic address which needs to be filled in the relocation address according to the type of the relocation item;
and the loading unit loads data required by program operation based on the calculated relocation address and the logic address required to be filled in the relocation address after downloading the relocation preprocessed program file to the FLASH, and directly operates the program file in the FLASH.
Furthermore, the compiling the source code into the address-independent program file using the compiling parameter is as follows: setting the compiling parameters of the compiler as follows: -fPIC and-mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9, -fPIC for generating a location-independent compiled code, -mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9 for generating a code with a code section separate from a data section, and the R9 register is designated as a code section register, wherein the program file comprises separate code and data segments and, after compilation, will invoke an elf2app tool to modify the variable access instructions of the data segments into an R9+ offset manner, the elf2app identifying different variable access instructions and a jump instruction, and calculating a target address according to the structures of the variable access instruction and the jump instruction, and generating a corresponding instruction accessed through the R9 register according to the target address to replace the existing corresponding instruction.
Furthermore, during relocation preprocessing, for all relocation items in the ELF, search and operation are performed according to the types of the relocation items, and the ELF relocation items are converted into relocation arrays and stored in an executable program file, where the relocation arrays include relocation addresses and logic addresses to which the addresses should be written. Further, the executable program file is processed after the relocation preprocessing is completed, and the processing comprises: deleting the joint-free area and the data of the executable program file, assembling the text segment and the data segment into a new executable program file, and recording the information required during loading into the file header of the new executable program file, wherein the new executable program file can be downloaded to any position of the FLASH; in the loading process, the positions of the text field and the data field are calculated according to the information in the file header, the data field is copied, the data field initial address is set to R9, then the residual relocation operation is executed according to the addresses of the text field and the data field and the relocation array in the file header, and the program file is directly run in FLASH after the relocation operation is completed.
The invention also proposes a computer-readable storage medium having stored thereon computer program code which, when executed by a computer, performs any of the methods described above.
The invention has the technical effects that: the invention discloses a program dynamic loading method, which comprises the following steps: compiling, namely compiling the source code into an address-independent program file by adopting a compiling parameter; a relocation preprocessing step, namely calculating a relocation address and a logic address which needs to be filled in the relocation address for the program file in the FLASH according to the type of the relocation item; and loading, namely loading data required by program operation based on the calculated relocation address and the logic address required to be filled in the relocation address, and directly operating the program file in the FLASH. The following technical effects are achieved:
firstly, the dynamic loading process of the application program reduces the step of copying the code segment from the FLASH to the RAM, only the data segment needs to be copied, the loading and running speed is higher, and the RAM space required by program running is greatly reduced.
Secondly, most of the relocation work is completed in the construction stage of the application program, so that the program is faster in loading speed and occupies fewer resources.
Thirdly, compared with the ELF, the size of the application program reduces a lot of unnecessary data, the size is reduced, and the space of FLASH and RAM is saved.
Fourthly, the invention can realize the dynamic loading function on the MCU with extremely short resources, and realize the separation of the system and the application, so that the micro operating system can install the APP according to the requirements.
Fifth, the method of the present invention supports debugging dynamically loaded APPs.
Drawings
Other features, objects and advantages of the present application will become more apparent upon reading of the following detailed description of non-limiting embodiments thereof, made with reference to the accompanying drawings.
Fig. 1 is a flowchart of a program dynamic loading method according to an embodiment of the present invention.
Fig. 2 is a schematic structural diagram of a program dynamic loading apparatus according to an embodiment of the present invention.
Detailed Description
The present application will be described in further detail with reference to the following drawings and examples. It is to be understood that the specific embodiments described herein are merely illustrative of the relevant invention and not restrictive of the invention. It should be noted that, for convenience of description, only the portions related to the related invention are shown in the drawings.
It should be noted that the embodiments and features of the embodiments in the present application may be combined with each other without conflict. The present application will be described in detail below with reference to the embodiments with reference to the attached drawings.
Fig. 1 shows a program dynamic loading method of the present invention, which includes:
and a compiling step S101, compiling the source code into an address-independent program file by adopting the compiling parameter. The program described in the present invention mainly refers to a program applied on the handheld terminal, such as a program run on the MCU, and of course, programs on other devices are also applicable to the method of the present invention, such as a program file in the format of the compiled ELF.
And a relocation preprocessing step S102, calculating a relocation address and a logic address required to be filled in the relocation address for the program file according to the type of the relocation item. In the invention, the main relocation is completed after compiling, and the step mainly refers to that the command which can not be relocated in the compiling process is relocated.
And a loading step S103, after downloading the program file of the relocation preprocessing to the FLASH, loading the data required by the program operation based on the calculated relocation address and the logic address required to be filled in the relocation address, and directly operating the program file in the FLASH. The invention relates to a program file running in FLASH, which is a program in which a data part in the program file is copied into a memory, and the rest part is run in FLASH.
In one embodiment, the compiling the source code into the address-independent program file using the compiling parameter is as follows: setting the compiling parameters of the compiler as follows: -fPIC and-mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9, -fPIC for generating a location-independent compiled code, -mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9 for generating a code with a code section separate from a data section, and the R9 register is designated as a code section register, wherein the program file comprises separate code and data segments and, after compilation, will invoke an elf2app tool to modify the variable access instructions of the data segments into an R9+ offset manner, the elf2app identifying different variable access instructions and a jump instruction, and calculating a target address according to the structures of the variable access instruction and the jump instruction, and generating a corresponding instruction accessed through the R9 register according to the target address to replace the existing corresponding instruction. The compiler adopted in the invention is a GCC compiler, the compiled ELF code segment and the data segment are separated by the used compiling parameters, and meanwhile, an R9 register is reserved to be not occupied by other operations, in addition, after the compiling is finished, an ELF2app tool is called to modify a data segment variable access instruction into an R9+ offset mode, so that in a loading stage, the loader needs to set the R9 register as a loaded data segment starting address. This is one of the important points of the present invention.
In an embodiment, after the compilation is completed, in the relocation preprocessing, for all the relocation items in the ELF, the present invention searches and operates according to the types of the relocation items, converts the ELF relocation items into a relocation array and saves the relocation array in an executable program file, where the relocation array includes a relocation address and a logic address to which the address should be written. The finding and most of the operations are completed by using the elf2app tool, that is, when the relocation is performed, only the access addresses of the variable access instruction and the jump instruction which cannot be identified by the elf2app need to be saved in the executable program file by using a relocation array, wherein the relocation array comprises the relocation address and the logical address to which the address should be written. That is, the work that cannot be completed by the elf2app tool and most of the operation is completed is stored in the executable image (i.e., the executable file) in a simpler array (i.e., relocation array) mode, so that relocation can be completed only by simply processing the array content in the subsequent loading stage. The relocation array comprises two items of content, "relocation address" and "logical address" to which the address should be written, the loader calculates a running address (running address = R9+ logical address-DATA segment start address) according to the logical address and writes the "relocation address", in the present invention, the "relocation address" and the "logical address" to which the address should be written are recorded in a header file of the program (described in detail later) by using variable access instructions and access addresses of jump instructions which cannot be identified by the array, so that the program acquires the relocation array during execution, which facilitates to calculate the corresponding address quickly, so that the program starts execution quickly, which is another important invention point of the present invention.
In one embodiment, the executable program file is processed after the relocation pre-processing is completed, the processing includes: deleting the joint-free area and the data of the executable program file, assembling the text segment and the data segment into a new executable program file, and recording the information required during loading into the file header of the new executable program file, wherein the new executable program file can be downloaded to any position of the FLASH. Because most of the ELF positioning work is finished in the construction stage, and a large amount of original data for repositioning in the ELF are not needed in the operation stage, the data can be deleted from the final mirror image, the storage space occupied in the operation is further saved, the loading speed is improved, and the ELF data which can be deleted are as follows: symbol table, derived symbol table, string space, relocation table, ELF header, debug information, etc. Preferably, the method removes the joint-free area and data through ELF processing, assembles the text and data sections into a new app format, and records the information necessary for loading to an app file header, so that the size of the application program is reduced by a lot of unnecessary data compared with ELF, the size is reduced, and FLASH and RAM space is saved. This is another important inventive point of the present invention.
In one embodiment, in the loading process, the positions of the text field and the data field are calculated according to the information in the file header, the data field is copied, namely only the data field is copied into the RAM, the text field is not copied, the initial address of the data field is set to R9, then the rest repositioning operation is executed according to the addresses of the text field and the data field and the repositioning array in the file header, and the program file is directly run in the FLASH after the repositioning operation is completed. For example, in the loading phase, the loader needs to calculate the text and data segment locations from the app file header, copy the data segment, set the new data segment start address to R9, and then perform the remaining relocation operations based on the new text and data segment addresses and the relocation array in the app header. When in relocation, the related relocation items of the text field and the data field need to use different address conversion methods. The repositioning method comprises the following steps: firstly, judging whether the address is in a text section or a data section according to the logical address written into the relocation array by app2elf, if the logical address is greater than or equal to the initial logical address of the text section and less than 'the initial logical address of the text section + the length of the text section', indicating that the address is in the text section, and if the logical address is greater than or equal to the initial logical address of the data section and less than 'the initial logical address of the data section + the length of the data section', indicating that the address is in the data section; if the logical address is in the text segment, "run address = text segment start address + logical address-text segment start logical address in flash," if the run address is in the data segment, "run address = start address of data segment copy to ram (value of R9) + logical address-data segment start logical address", and finally write the calculated run address to "relocation address" in the relocation array entry. The invention can realize the dynamic loading function on the MCU with extremely short resources, realize the separation of the system and the application, ensure that the micro operating system can install the APP according to the requirements, and provide different positioning modes of the text section and the data section.
When the method is used for debugging a program, because an APP (application program) and an operating system are separately compiled, the APP code cannot be directly debugged when the operating system is debugged, because a debugger does not know the running address of the APP, the method can output the running address information of the APP at a terminal when the APP is started, wherein the running address information comprises address information such as a data section, a text section and the like, then input add-symbol-file at a gdb end to add the APP debugging information, so that the APP program can be debugged, and the add-symbol-file command format is as follows
and the add-system-file carries the elf file name-step text address-sdata data address of the debugging information. That is, the present invention supports debugging dynamically loaded apps, which is another inventive point of the present invention.
Compared with the dynamic loading technology of the existing operating system such as Linux, the method avoids the process that the APP file is copied from FLASH to RAM, improves the application starting speed, does not limit the application starting address or the application storage address, realizes the direct operation of the APP on the FLASH, and is suitable for being operated on small-sized equipment such as MCU.
Fig. 2 shows a program dynamic loading apparatus of the present invention, which includes:
the compiling unit 201 compiles the source code into an address-independent program file using the compiling parameter. The program described in the present invention mainly refers to a program applied on the handheld terminal, such as a program run on the MCU, and of course, programs on other devices are also applicable to the apparatus of the present invention, such as a program file in the format of the compiled ELF.
And the relocation preprocessing unit 202 calculates the relocation address and the logical address required to be filled in the relocation address according to the type of the relocation item for the program file. In the invention, the main relocation is completed after compiling, and the step mainly refers to that the command which can not be relocated in the compiling process is relocated.
The loading unit 203 loads the program file to be relocated to the FLASH, and then loads the data required by the program operation based on the calculated relocation address and the logic address required to be filled in the relocation address, and directly operates the program file in the FLASH. The invention relates to a program file running in FLASH, which is a program in which a data part in the program file is copied into a memory, and the rest part is run in FLASH.
In one embodiment, the compiling the source code into the address-independent program file using the compiling parameter is as follows: setting the compiling parameters of the compiler as follows: -fPIC and-mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9, -fPIC for generating a location-independent compiled code, -mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9 for generating a code with a code section separate from a data section, and the R9 register is designated as a code section register, wherein the program file comprises separate code and data segments and, after compilation, will invoke an elf2app tool to modify the variable access instructions of the data segments into an R9+ offset manner, the elf2app identifying different variable access instructions and a jump instruction, and calculating a target address according to the structures of the variable access instruction and the jump instruction, and generating a corresponding instruction accessed through the R9 register according to the target address to replace the existing corresponding instruction. The compiler adopted in the invention is a GCC compiler, the compiled ELF code segment and the data segment are separated by the used compiling parameters, and meanwhile, an R9 register is reserved to be not occupied by other operations, in addition, after the compiling is finished, an ELF2app tool is called to modify a data segment variable access instruction into an R9+ offset mode, so that in a loading stage, the loader needs to set the R9 register as a loaded data segment starting address. This is one of the important points of the present invention.
In one embodiment, after the compilation is completed, in the relocation preprocessing, for all the relocation items in the ELF, the lookup and operation are performed according to the types of the relocation items, the ELF relocation items are converted into relocation arrays and stored in the executable program file, the relocation arrays include relocation addresses and logical addresses to which the addresses should be written, that is, the lookup and most of the operations are completed by using an ELF2app tool, that is, in the relocation, only variable access instructions which cannot be recognized by the ELF2app and access addresses of jump instructions need to be stored in the executable program file by using the relocation arrays, and the relocation arrays include the relocation addresses and the logical addresses to which the addresses should be written. That is, the work that cannot be completed by the elf2app tool and most of the operation is completed is stored in the executable image (i.e., the executable file) in a simpler array (i.e., relocation array) mode, so that relocation can be completed only by simply processing the array content in the subsequent loading stage. The relocation array comprises two items of content, "relocation address" and "logical address" to which the address should be written, the loader calculates a running address (running address = R9+ logical address-DATA segment start address) according to the logical address and writes the "relocation address", in the present invention, the "relocation address" and the "logical address" to which the address should be written are recorded in a header file of the program (described in detail later) by using variable access instructions and access addresses of jump instructions which cannot be identified by the array, so that the program acquires the relocation array during execution, which facilitates to calculate the corresponding address quickly, so that the program starts execution quickly, which is another important invention point of the present invention.
In one embodiment, the executable program file is processed after the relocation pre-processing is completed, the processing includes: deleting the joint-free area and the data of the executable program file, assembling the text segment and the data segment into a new executable program file, and recording the information required during loading into the file header of the new executable program file, wherein the new executable program file can be downloaded to any position of the FLASH. Because most of the ELF positioning work is finished in the construction stage, and a large amount of original data for repositioning in the ELF are not needed in the operation stage, the data can be deleted from the final mirror image, the storage space occupied in the operation is further saved, the loading speed is improved, and the ELF data which can be deleted are as follows: symbol table, derived symbol table, string space, relocation table, ELF header, debug information, etc. Preferably, the method removes the joint-free area and data through ELF processing, assembles the text and data sections into a new app format, and records the information necessary for loading to an app file header, so that the size of the application program is reduced by a lot of unnecessary data compared with ELF, the size is reduced, and FLASH and RAM space is saved. This is another important inventive point of the present invention.
In one embodiment, in the loading process, the positions of the text field and the data field are calculated according to the information in the file header, the data field is copied, namely only the data field is copied into the RAM, the text field is not copied, the initial address of the data field is set to R9, then the rest repositioning operation is executed according to the addresses of the text field and the data field and the repositioning array in the file header, and the program file is directly run in the FLASH after the repositioning operation is completed. For example, in the loading phase, the loader needs to calculate the text and data segment locations from the app file header, copy the data segment, set the new data segment start address to R9, and then perform the remaining relocation operations based on the new text and data segment addresses and the relocation array in the app header. When in relocation, the related relocation items of the text field and the data field need to use different address conversion methods. The repositioning method comprises the following steps: firstly, judging whether the address is in a text section or a data section according to the logical address written into the relocation array by app2elf, if the logical address is greater than or equal to the initial logical address of the text section and less than 'the initial logical address of the text section + the length of the text section', indicating that the address is in the text section, and if the logical address is greater than or equal to the initial logical address of the data section and less than 'the initial logical address of the data section + the length of the data section', indicating that the address is in the data section; if the logical address is in the text segment, "run address = text segment start address + logical address-text segment start logical address in flash," if the run address is in the data segment, "run address = start address of data segment copy to ram (value of R9) + logical address-data segment start logical address", and finally write the calculated run address to "relocation address" in the relocation array entry. The invention can realize the dynamic loading function on the MCU with extremely short resources, realize the separation of the system and the application, ensure that the micro operating system can install the APP according to the requirements, and provide different positioning modes of the text section and the data section.
When the device is used for debugging programs, because an APP (application program) and an operating system are separately compiled, the APP code cannot be directly debugged when the operating system is debugged, because a debugger does not know the running address of the APP, the running address information of the APP is output at a terminal when the APP is started, the running address information comprises address information such as a data section, a text section and the like, then add APP debugging information is input at a gdb end, the APP program can be debugged, and the add-symbol-file command format is as follows
and the add-system-file carries the elf file name-step text address-sdata data address of the debugging information. That is, the present invention supports debugging dynamically loaded apps, which is another inventive point of the present invention.
Compared with the dynamic loading technology of the existing operating systems such as Linux, the device avoids the process that the APP file is copied to the RAM from the FLASH, improves the application starting speed, does not limit the application starting address or limit the application storage address, realizes the direct operation of the APP on the FLASH, and is suitable for being operated on small-sized equipment such as MCU.
For convenience of description, the above devices are described as being divided into various units by function, and are described separately. Of course, the functionality of the units may be implemented in one or more software and/or hardware when implementing the present application.
From the above description of the embodiments, it is clear to those skilled in the art that the present application can be implemented by software plus necessary general hardware platform. Based on such understanding, the technical solutions of the present application may be essentially or partially implemented in the form of a software product, which may be stored in a storage medium, such as a ROM/RAM, a magnetic disk, an optical disk, etc., and includes several instructions for enabling a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the method according to the embodiments or some parts of the embodiments of the present application.
Finally, it should be noted that: although the present invention has been described in detail with reference to the above embodiments, it should be understood by those skilled in the art that: modifications and equivalents may be made thereto without departing from the spirit and scope of the invention and it is intended to cover in the claims the invention as defined in the appended claims.

Claims (3)

1. A method for dynamically loading a program, the method comprising:
compiling, namely compiling the source code into an address-independent program file by adopting a compiling parameter;
a relocation preprocessing step, namely calculating the program file according to the type of the relocation item to obtain a relocation address and a logic address needing to be filled in the relocation address;
loading step, after downloading the program file of the relocation pretreatment to FLASH, loading the program operation required data based on the calculated relocation address and the logic address required to be filled in the relocation address, and directly operating the program file in FLASH;
the operation of compiling the source code into the address-independent program file by adopting the compiling parameters is as follows: setting the compiling parameters of the compiler as follows: -fPIC and-mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9, -fPIC for generating a location-independent compiled code, -mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9 for generating a code with a code section separate from a data section, and the R9 register is designated as a code section register, wherein the program file comprises separate code and data segments and, after compilation, modifies variable access instructions of the data segments into an R9+ offset manner by invoking an elf2app tool, the elf2app recognizing different variable access instructions and a jump instruction, calculating a target address according to the structures of the variable access instruction and the jump instruction, and generating a corresponding instruction accessed through an R9 register according to the target address to replace the existing corresponding instruction;
during relocation preprocessing, aiming at all relocation items in the ELF, searching and operating according to the types of the relocation items, converting the ELF relocation items into relocation arrays and storing the relocation arrays in an executable program file, wherein the relocation arrays comprise relocation addresses and logic addresses to which the addresses are written;
processing the executable program file after the relocation preprocessing is finished, wherein the processing comprises the following steps: deleting the joint-free area and the data of the executable program file, assembling the text segment and the data segment into a new executable program file, and recording the information required during loading into the file header of the new executable program file, wherein the new executable program file can be downloaded to any position of the FLASH;
in the loading process, the positions of the text field and the data field are calculated according to the information in the file header, the data field is copied, the data field initial address is set to R9, then the residual relocation operation is executed according to the addresses of the text field and the data field and the relocation array in the file header, and the program file is directly run in FLASH after the relocation operation is completed.
2. A program dynamic loading apparatus, comprising:
the compiling unit is used for compiling the source code into an address-independent program file by adopting compiling parameters;
the relocation preprocessing unit is used for calculating a relocation address and a logic address which needs to be filled in the relocation address according to the type of the relocation item;
the loading unit loads the data required by program operation based on the calculated relocation address and the logic address required to be filled in the relocation address after downloading the program file subjected to the relocation preprocessing to the FLASH, and directly operates the program file in the FLASH;
the operation of compiling the source code into the address-independent program file by adopting the compiling parameters is as follows: setting the compiling parameters of the compiler as follows: -fPIC and-mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9, -fPIC for generating a location-independent compiled code, -mno-pic-data-is-text-relative-msible-pic-base-mpic-register =9 for generating a code with a code section separate from a data section, and the R9 register is designated as a code section register, wherein the program file comprises separate code and data segments and, after compilation, modifies variable access instructions of the data segments into an R9+ offset manner by invoking an elf2app tool, the elf2app recognizing different variable access instructions and a jump instruction, calculating a target address according to the structures of the variable access instruction and the jump instruction, and generating a corresponding instruction accessed through an R9 register according to the target address to replace the existing corresponding instruction;
during relocation preprocessing, aiming at all relocation items in the ELF, searching and operating according to the types of the relocation items, converting the ELF relocation items into relocation arrays and storing the relocation arrays in an executable program file, wherein the relocation arrays comprise relocation addresses and logic addresses to which the addresses are written;
processing the executable program file after the relocation preprocessing is finished, wherein the processing comprises the following steps: deleting the joint-free area and the data of the executable program file, assembling the text segment and the data segment into a new executable program file, recording information required during loading into a file header of the new executable program file, and downloading the new executable file to any position of FLASH; in the loading process, the positions of the text field and the data field are calculated according to the information in the file header, the data field is copied, the data field initial address is set to R9, then the residual relocation operation is executed according to the addresses of the text field and the data field and the relocation array in the file header, and the program file is directly run in FLASH after the relocation operation is completed.
3. A computer-readable storage medium, characterized in that the storage medium has stored thereon computer program code which, when executed by a computer, performs the method of claim 1.
CN202010221160.4A 2020-03-26 2020-03-26 Program dynamic loading method and device and storage medium Active CN111124550B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010221160.4A CN111124550B (en) 2020-03-26 2020-03-26 Program dynamic loading method and device and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010221160.4A CN111124550B (en) 2020-03-26 2020-03-26 Program dynamic loading method and device and storage medium

Publications (2)

Publication Number Publication Date
CN111124550A CN111124550A (en) 2020-05-08
CN111124550B true CN111124550B (en) 2020-07-03

Family

ID=70493957

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010221160.4A Active CN111124550B (en) 2020-03-26 2020-03-26 Program dynamic loading method and device and storage medium

Country Status (1)

Country Link
CN (1) CN111124550B (en)

Families Citing this family (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111984411A (en) * 2020-08-18 2020-11-24 上海睿赛德电子科技有限公司 Application relocation method for resource-constrained platform
CN112130926B (en) * 2020-09-17 2023-10-24 深圳市兆珑科技有限公司 Application program running method, device, terminal equipment and storage medium
CN112328319A (en) * 2020-09-30 2021-02-05 北京空间飞行器总体设计部 Satellite-borne APP dynamic loading method supporting RAM file system
CN113961260B (en) * 2021-12-21 2022-04-15 比亚迪股份有限公司 System control software operation method, system, computer device and storage medium
CN114443051A (en) * 2022-01-21 2022-05-06 北京小米移动软件有限公司 Application program compiling and running method and device and storage medium
CN114721735B (en) * 2022-03-04 2023-05-23 珠海海奇半导体有限公司 Program dynamic loading method and device and electronic equipment
CN114895986B (en) * 2022-06-10 2023-06-20 展讯通信(深圳)有限公司 Relocating method and device for global variable
CN116909656B (en) * 2023-09-14 2023-12-08 北京中天星控科技开发有限公司 Method for realizing embedded dynamic APP loading based on elf file
CN117075960B (en) * 2023-10-17 2024-01-23 统信软件技术有限公司 Program reconstruction method, application cross-platform migration method, device and computing equipment

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
FR2883083A1 (en) * 2005-03-14 2006-09-15 Meiosys Soc Par Actions Simpli Program instruction inserting method for e.g. computer, involves executing inserted treatment according to result of test of connection data and in place of program instruction following connection instruction
CN104866345A (en) * 2015-05-21 2015-08-26 福建升腾资讯有限公司 Storage method of executable code under ARMv7m architecture
CN105808298A (en) * 2016-03-08 2016-07-27 大连楼兰科技股份有限公司 Dynamic module loading method for use under single-chip microcomputer
CN108829465A (en) * 2018-06-27 2018-11-16 北京计算机技术及应用研究所 A kind of local dynamic loading system and method based on direct read/write FLASH

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
FR2883083A1 (en) * 2005-03-14 2006-09-15 Meiosys Soc Par Actions Simpli Program instruction inserting method for e.g. computer, involves executing inserted treatment according to result of test of connection data and in place of program instruction following connection instruction
CN104866345A (en) * 2015-05-21 2015-08-26 福建升腾资讯有限公司 Storage method of executable code under ARMv7m architecture
CN105808298A (en) * 2016-03-08 2016-07-27 大连楼兰科技股份有限公司 Dynamic module loading method for use under single-chip microcomputer
CN108829465A (en) * 2018-06-27 2018-11-16 北京计算机技术及应用研究所 A kind of local dynamic loading system and method based on direct read/write FLASH

Also Published As

Publication number Publication date
CN111124550A (en) 2020-05-08

Similar Documents

Publication Publication Date Title
CN111124550B (en) Program dynamic loading method and device and storage medium
US9134966B2 (en) Management of mixed programming languages for a simulation environment
EP2487585B1 (en) Method and device for dynamically loading relocatable file
JP5415557B2 (en) User script code conversion for debugging
US10860300B2 (en) Direct function call substitution using preprocessor
US11579856B2 (en) Multi-chip compatible compiling method and device
KR20150024842A (en) Adaptive portable libraries
CN114816417B (en) Cross compiling method, device, computing equipment and storage medium
CN109739824B (en) Log obtaining method and device
CN108829465B (en) Local dynamic loading system and method based on direct read-write FLASH
CN110659088B (en) Method and system for expanding program under embedded environment
CN115700470A (en) File loading processing method and device, computer storage medium and electronic equipment
JP2006221630A (en) Log acquisition method and program, and storage medium
CN116303143A (en) Method for enhancing WebAssemblem memory security based on LLVM compiling framework
CN109144575B (en) Device and method for running application across operating systems, electronic equipment and memory
US9098317B2 (en) Optional retargeting of library references
CN114528141A (en) Method and device for repairing program defects in embedded system kernel mode
CN111984329B (en) Boot software standardized generation and execution method and system
KR100478463B1 (en) Dynamic Linking Method for Application Program
US8762973B2 (en) Information processing apparatus, method of controlling information processing apparatus, and program for executing intermediate code instructions
KR19980064734A (en) Language processing system and method for generating debug source files corresponding to optimized images
CN113031956A (en) Program compiling method and device and program running method and device
CN106897051B (en) Process cleaning method and device
US7743371B1 (en) System and method for dynamically loading object modules
CN102262545A (en) Program installing method and device

Legal Events

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