Program compiling and program loading method
By merging the code and data segments during the compilation of IoT device applications, and by compressing symbol tables and strings, the problem of high memory consumption in IoT devices is solved, and efficient and unified loading capabilities for applications are achieved.
Patent Information
- Application Number
- CN202110507123.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-05-10
- Publication Date
- 2026-02-13
- Estimated Expiration
- 2041-05-10
AI Technical Summary
When IoT devices load applications, existing technologies result in significant memory consumption, especially since the executable files of applications are large and occupy device memory. Furthermore, the different organization of target files generated by different compilers makes it impossible for RTOS systems to load them uniformly.
During application compilation, the code and data segments of multiple object files are merged to generate object code and object data segments, reducing header information; only the symbol table corresponding to the relocation table required during loading is retained, and strings of undefined symbols are compressed to generate an executable file in a uniform format.
It reduces the size of the application's executable file, decreases memory consumption, improves loading speed, and supports unified loading of target files generated by different compilers, enabling separate compilation and upgrades of the application.
Smart Images

Figure CN115328454B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of data processing, and particularly relates to a program compiling and program loading method. BACKGROUND
[0002] With the continuous development of Internet of Things technology, Internet of Things devices gradually enter people's life and work. Real-time operating system (RTOS) has the characteristics of timely response and high reliability, and is widely used as the operating system of Internet of Things devices.
[0003] The Internet of Things device can install an application program to realize a specific function. When installing the application program, the RTOS loads the executable file of the application program to the local. The larger the executable file of the application program is, the more memory of the Internet of Things device is occupied. SUMMARY
[0004] Aspects of the present application provide a program compiling and program loading method to reduce the memory occupation of the executable file of the application program.
[0005] The embodiment of the present application provides a program compiling method, comprising:
[0006] Obtaining a plurality of object files compiled from the source code of an application;
[0007] Respectively performing merging processing on the code segment and the data segment of the plurality of object files to obtain a target code segment and a target data segment;
[0008] According to the symbol positions in the target code segment and the target data segment, modifying the symbol table and the relocation table corresponding to the plurality of object files to obtain a first symbol table and a first relocation table;
[0009] Obtaining a second symbol table corresponding to the first relocation table from the first symbol table;
[0010] According to the target code segment, the target data segment, the second symbol table and the first relocation table, generating an executable file of the application.
[0011] The embodiment of the present application also provides a program loading method, comprising:
[0012] Obtaining an executable file of an application;
[0013] Performing parsing on the executable file to obtain a code segment, a data segment, a string table, a relocation table and a symbol table corresponding to the relocation table contained in the executable file;
[0014] According to the string table, the symbol table and the relocation table, instructions in the code segment that need to be relocated are relocated;
[0015] Based on the data segment, the instructions after relocation and the instructions in the code segment that do not need to be relocated are executed to load the application.
[0016] The embodiment of the present application further provides a computer device, comprising a memory and a processor; wherein the memory is used to store a computer program;
[0017] The processor is coupled to the memory and is used to execute the computer program to execute the steps in the program compiling and / or program loading method.
[0018] The embodiment of the present application further provides a computer program product, comprising a computer program; the computer program is executed by a processor to implement the program compiling and / or program loading method.
[0019] The embodiment of the present application further provides a computer readable storage medium storing computer instructions, when the computer instructions are executed by one or more processors, the one or more processors execute the steps in the program compiling and / or program loading method.
[0020] In the embodiment of the present application, when the application program is compiled, on one hand, the code segment and the data segment of the plurality of object files of the application source code are merged to obtain the target code segment and the target data segment, which can reduce the table header information; on the other hand, only the symbol table corresponding to the relocation table needed when the application is loaded is reserved, which can reduce the space consumption of the symbol table; therefore, it is helpful to reduce the size of the executable file of the application generated according to the target code segment, the target data segment, the relocation table and the symbol table corresponding to the relocation table, and further to reduce the memory consumption when the application is loaded. BRIEF DESCRIPTION OF DRAWINGS
[0021] The accompanying drawings, which are included to provide a further understanding of the present application, constitute a part of the present application and illustrate the illustrative embodiments of the present application and their description serve to explain the present application, and do not constitute improper limitations on the present application. In the drawings:
[0022] Figure 1a The flowchart of the program compiling method provided by the embodiment of the present application is shown;
[0023] Figure 1b The composition diagram of the object file and the executable file provided by the embodiment of the present application is shown;
[0024] Figure 2 The flowchart of the program loading method provided by the embodiment of the present application is shown;
[0025] Figure 3 A structural schematic diagram of a computer device provided by an embodiment of the present application is shown. DETAILED DESCRIPTION
[0026] In order to make the objects, technical solutions and advantages of the present application clearer, the technical solutions of the present application will be described clearly and completely below in connection with the embodiments of the present application and corresponding drawings. Obviously, the described embodiments are only some of the embodiments of the present application, but not all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by a person of ordinary skill in the art without any creative effort fall within the scope of protection of the present application.
[0027] A real-time operating system (RTOS) has the characteristics of timely response and high reliability, and is widely used as an operating system of an Internet of Things device. In actual applications, some RTOS systems need to compile user application code together with an operating system kernel as a complete executable file. The user application cannot be compiled alone, and cannot be upgraded separately by OTA.
[0028] Some RTOS systems do not support dynamic loading of SO files, and some RTOS systems support dynamic loading of.O files or ELF files, allowing users to compile user application programs separately into program and dynamically load them into the RTOS system for running.
[0029] However, in actual applications, for the RTOS system supporting dynamic loading of.O files, the code segment, data segment, symbol table, relocation table and string table of the code in the.O file need to be stored, which will increase the size of the.O file by about 1 / 3. On the other hand, the organization forms of the code segment, data segment, symbol table, relocation table and string table of the.O file generated by different compilers are different, which leads to the fact that the same set of code cannot be used to load.O files compiled by different compilers in the RTOS system.
[0030] For the RTOS system supporting dynamic loading of ELF files, the compiler needs to support position-independent ELF compilation, and some compilers do not support this feature. For the compilers supporting position-independent ELF compilation, the support methods are not uniform, which leads to the fact that the same set of code cannot be used to load ELF files compiled by different compilers in the RTOS system. At the same time, this scheme requires that C / C++ libraries be integrated into each ELF file. For a computer device, the more user applications loaded, the more space consumed.
[0031] From the above analysis, it can be seen that in the conventional scheme, the application file loaded by the RTOS system is large, which leads to a large memory consumption.
[0032] In some embodiments of the present application, in order to solve the problem of large memory space occupied by existing application loading, during the compilation of an application, on the one hand, the code segments and data segments of a plurality of object files obtained by compiling the source code of the application are merged to obtain target code segments and target data segments, which can reduce the table header information; on the other hand, only the symbol table corresponding to the relocation table required during the loading of the application is retained, which can reduce the space consumption of the symbol table; therefore, it is helpful to reduce the size of the executable file of the application generated according to the target code segments, the target data segments, the relocation table and the symbol table corresponding to the relocation table, and to reduce the memory consumption during the loading of the application.
[0033] The technical solutions provided by the embodiments of the present application are described in detail below with reference to the accompanying drawings.
[0034] It should be noted that the same reference numerals represent the same objects in the following drawings and embodiments, and therefore, once an object is defined in one drawing or embodiment, it does not need to be further discussed in subsequent drawings and embodiments.
[0035] Figure 1a A flowchart of a program compilation method provided by an embodiment of the present application is shown in FIG. 1. As shown in FIG. 1, the method comprises the following steps. Figure 1a
[0036] 101. Obtain a plurality of object files obtained by compiling the source code of an application.
[0037] 102. Merge the code segments and data segments of the plurality of object files respectively to obtain target code segments and target data segments.
[0038] 103. Modify the symbol table and the relocation table corresponding to the plurality of object files according to the symbol positions in the target code segments and the target data segments to obtain a first symbol table and a first relocation table.
[0039] 104. Obtain a second symbol table corresponding to the first relocation table from the first symbol table.
[0040] 105. Generate an executable file of the application according to the target code segments, the target data segments, the second symbol table and the first relocation table.
[0041] In the present embodiment, the application refers to an application to be compiled. The application can be one or more software functional modules. The plurality refers to two or more. After the development of the application software is completed, the source code of the application needs to be compiled to generate an executable file, which can be loaded by a computer device to realize the function of the application. The larger the executable file of the application program is, the more memory of the computer device it occupies.
[0042] In the embodiment, in order to reduce the memory space consumption when the computer device loads the application, in step 101, a plurality of object files of the application can be obtained when the source code of the application is compiled. The plurality refers to two or more. The object file can be a.O file or an.obj file, etc. For the computer device with the operating system of RTOS, the object file can be a.O file.
[0043] Optionally, the source code (such as a.c file, etc.) of the application can be preprocessed, such as macro definition checking and expansion, preprocessing instruction, comment replacement space, header file, etc.; further, the preprocessed file (such as an.i file, etc.) can be subjected to lexical analysis, semantic analysis, syntax analysis and optimization to generate corresponding assembly code (such as an.s file, etc.); further, the assembler can convert the assembly code into instructions executable by the machine to obtain the object file (such as an.O file, etc.).
[0044] In the embodiment, in order to reduce the size of the executable file of the application, in step 102, the code segments and data segments of the plurality of object files can be subjected to merging processing respectively to obtain target code segments and target data segments, so that the plurality of object files can be merged into one object file to reduce the table header information.
[0045] For the application, the source code file can refer to or depend on another source code file, which results in the reference relationship between the plurality of object files obtained by compiling the source code of the application. Based on this, the plurality of object files can be linked to obtain the intermediate file of the application before step 102. The main task of the linking is to correctly connect the mutually referenced parts between the object files and the modules in the object files to obtain the intermediate file. The file type of the intermediate file can be consistent with that of the plurality of object files. For example, the plurality of object files are.O files, and the intermediate file obtained by linking the plurality of object files can also be an.O file.
[0046] Optionally, the plurality of object files can be linked in a partial linking manner to obtain the intermediate file of the application. The operation of linking the plurality of object files can be completed by the compiler.
[0047] Further, the code segments and data segments corresponding to the plurality of object files can be obtained from the intermediate file.
[0048] Since different compilers generate target files in different organizations, i.e., different organizations of code segments, data segments, symbol tables, relocation tables, and string tables in the target files, the operating system (such as an RTOS system) of the computer device cannot use the same set of codes to load the target files generated by different compilers. To solve this problem, in the embodiments of the present application, a correspondence between the compiler identifiers and the target file formats can be preset, so that when the code segments and the data segments corresponding to the plurality of target files are obtained from the intermediate file, the compiler identifier used to compile the source code of the application can be obtained; and the compiler identifier is matched in the preset correspondence between the compiler identifiers and the target file formats to obtain the target file formats of the plurality of target files; and then the code segments and the data segments corresponding to the plurality of target files are obtained from the intermediate file according to the target file formats of the plurality of target files.
[0049] In the embodiments of the present application, the implementation of obtaining the compiler identifier used to compile the source code of the application is not limited. In some embodiments, the compiler can be improved, for example, the compiler can compile the source code of the application to obtain a plurality of target files; and the identifier of the compiler is encapsulated in the plurality of target files. In this way, the identifier of the compiler carried in the plurality of target files can be parsed subsequently as the compiler identifier used to compile the source code of the application.
[0050] In other embodiments, the compiler identifier used to compile the application can also be set by a technician. Optionally, a human-computer interaction interface can be provided. The interaction interface can include a compiler identifier setting control; the technician can set the compiler identifier used to compile the application through the control. Optionally, the user can trigger the compiler identifier setting control to select the compiler identifier. For the computer device, the compiler identifiers to be selected can be displayed in response to the interactive operation on the compiler identifier setting control; and the compiler identifier selected by the interactive operation is determined as the compiler identifier used to compile the application in response to the interactive operation on the compiler identifier, and the like.
[0051] Further, after the compiler identifier used to compile the application is obtained, the compiler identifier is matched in the preset correspondence between the compiler identifiers and the target file formats to obtain the target file formats of the plurality of target files; and then the code segments and the data segments corresponding to the plurality of target files are obtained from the intermediate file according to the target file formats of the plurality of target files.
[0052] Since the linking of the plurality of target files can comb the inter-reference relationship between the modules in the plurality of target files, i.e., the linking relationship; therefore, when the code segments and the data segments of the plurality of target files are processed respectively, the code segments of the plurality of target files can be processed according to the linking relationship between the code segments in the intermediate file, to obtain the target code segment; and the data segments of the plurality of target files can be processed according to the linking relationship between the data segments in the intermediate file, to obtain the target data segment.
[0053] In some embodiments, the linking relationship of the plurality of target files can also be directly combed to obtain the linking relationship between the code segments and the linking relationship between the data segments of the plurality of target files, and further, the code segments of the plurality of target files can be processed according to the linking relationship between the code segments, to obtain the target code segment; and the data segments of the plurality of target files can be processed according to the linking relationship between the code segments, to obtain the target data segment.
[0054] The data segment of the target file can include a Data segment and a BSS (Block Started by Symbol) segment. The BSS segment refers to a block of memory region used to store global variables in the program that are not initialized or have initial value 0, which belongs to static memory allocation. The Data segment refers to a block of memory region used to store global variables in the program that are initialized, which belongs to static memory allocation.
[0055] In the embodiment, when the data segments of the plurality of target files are processed, for the embodiment of first linking the plurality of target files to obtain the intermediate file, the Data segments of the plurality of target files can be processed according to the linking relationship between the Data segments in the intermediate file, to obtain the merged Data segment; and the BSS segments of the plurality of target files can be processed according to the linking relationship between the BSS segments in the intermediate file, to obtain the merged BSS segment. The merged Data segment and the merged BSS segment constitute the target data segment.
[0056] Since the code segments and the data segments of the plurality of target files are processed respectively, the symbol positions in the code segments and the data segments are changed, and based on this, in step 103, the symbol table and the relocation table corresponding to the plurality of target files can be modified according to the symbol positions in the target code segment and the target data segment, to obtain the modified symbol table and the modified relocation table. In the embodiment, for convenience of description, the modified symbol table and the modified relocation table obtained in step 103 are defined as the first symbol table and the first relocation table respectively.
[0057] The symbol table and the relocation table corresponding to the plurality of target files refer to the symbol table and the relocation table including the symbol table and the relocation table in each target file. For the embodiment of linking the plurality of target files to obtain the intermediate file, the symbol table and the relocation table in the plurality of target files can also be linked to obtain one symbol table and one relocation table when the plurality of target files are linked. The linked symbol table and the relocation table include the symbol table and the relocation table in the plurality of target files. In this embodiment, the symbol table and the relocation table corresponding to the plurality of target files in step 103 refer to the symbol table and the relocation table after the linking, i.e., the symbol table and the relocation table in the intermediate file.
[0058] For the embodiment of directly linking the plurality of target files, the symbol table and the relocation table in the plurality of target files can also be combined into one symbol table and one relocation table according to the linking relationship between the plurality of target files. The symbol table and the relocation table corresponding to the plurality of target files in step 103 refer to the symbol table and the relocation table after the combination of the symbol table and the relocation table in the plurality of target files.
[0059] Further, considering that when the executable file of the application is loaded, the symbol corresponding to the instruction without relocation can be directly executed, and the symbol corresponding to the instruction requiring relocation needs to be relocated based on the symbol table corresponding to the relocation symbol. Based on this, in step 104, the second symbol table corresponding to the first relocation table can be obtained from the first symbol table. Optionally, the other symbol tables in the first symbol table except the second symbol table can be deleted.
[0060] Optionally, the dependence relationship between the symbols recorded in the first relocation table can be used to determine the symbol depended by the relocation symbol, and the symbol table corresponding to the relocation symbol and the symbol depended by the relocation symbol recorded in the first relocation table can be obtained from the first symbol table as the second symbol table.
[0061] After the target code segment, the target data segment, the first relocation table, and the second symbol table corresponding to the first relocation table are obtained, in step 105, the executable file of the application can be generated according to the target code segment, the target data segment, the second symbol table, and the first relocation table.
[0062] Optionally, the second symbol table and the first relocation table can be copied to the file in which the target code segment and the target data segment are located to obtain the executable file of the application.
[0063] In this embodiment, during application compilation, on the one hand, the code segments and data segments of multiple object files compiled from the application source code are merged to obtain target code segments and target data segments, which reduces header information and memory consumption of the header; on the other hand, only the symbol table corresponding to the relocation table required when the application is loaded is retained, which reduces the space consumption of the symbol table. Therefore, it helps to reduce the size of the executable file of the application generated subsequently based on the target code segments, target data segments, relocation tables, and the symbol tables corresponding to the relocation tables, thereby helping to reduce memory consumption when loading the application.
[0064] In addition, this embodiment can process target files generated by various compilers into executable files with a unified format. For computer devices that load applications, the same set of code can be used to load executable files corresponding to target files generated by different compilers.
[0065] In this embodiment, the target file may include defined symbols and undefined symbols. Defined symbols typically refer to defined functions, global variables, etc., symbols defined by the application developer. Undefined symbols refer to symbols whose type is undefined, specifically symbols referenced by the target file whose values are not defined in the target file. Undefined symbols that the application depends on may include undefined symbols that the application directly depends on and undefined symbols that it indirectly depends on. Undefined symbols that the application directly depends on refer to undefined symbols contained in the application's target file. Undefined symbols that the application indirectly depends on refer to undefined symbols associated with undefined symbols contained in the application's target file. For example, undefined symbols in other target files that are associated with undefined symbols contained in the target file.
[0066] When a computer loads an application, for code segments corresponding to defined symbols, the actual load address of the code segment corresponding to the defined symbol can be determined based on the definition of the symbol. However, the actual load address of code segments corresponding to undefined symbols needs to be relocated. Based on this, in this embodiment, the undefined symbols that the application depends on can also be determined according to the symbol types recorded in the first symbol table; and the strings corresponding to the undefined symbols can be obtained from the string tables corresponding to multiple target files; then, the strings corresponding to the undefined symbols are compressed to obtain a processed string table, which helps to reduce the memory consumption of the strings.
[0067] Optionally, such as Figure 1b As shown, a hash algorithm can be used to compress strings corresponding to undefined symbols, resulting in a processed string table. For example, a hash algorithm can be used to compress strings corresponding to undefined symbols to a set number of bytes, such as 4 bytes.
[0068] In the embodiment of the present application, since the code segment corresponding to the defined symbol is defined, the actual loading address of the code segment corresponding to the defined symbol can be determined according to the definition of the defined symbol, thus the string corresponding to the undefined symbol can be compressed and the string corresponding to the defined symbol can be deleted to obtain the processed string table. In this way, on the one hand, the string corresponding to the undefined symbol is compressed, which can reduce the memory consumption of the string corresponding to the undefined symbol; on the other hand, the string corresponding to the defined symbol which is not needed is deleted, which can further reduce the memory consumption of the string corresponding to the defined symbol, and then reduce the memory consumption of the string table in the subsequent executable file, which is helpful to further reduce the size of the executable file and then reduce the memory consumption when the application is loaded.
[0069] Correspondingly, as shown in Figure 1b When the executable file is generated, the first relocation table, the second symbol table corresponding to the first relocation table, the first relocation table and the processed string table can be copied to the file where the target code segment and the target data segment are located to obtain the executable file of the application. Wherein, Figure 1b In the embodiment, the processed string table is illustrated as a hash table, but it does not constitute a limitation.
[0070] In this way, for the generated executable file, on the one hand, the code segment and the data segment of the plurality of object files compiled from the source code of the application are merged to obtain the target code segment and the target data segment, which can reduce the table header information and the memory consumption of the table header; on the other hand, only the symbol table corresponding to the relocation table required when the application is loaded is retained, which can reduce the space consumption of the symbol table; in addition, the string corresponding to the undefined symbol is compressed, which can reduce the memory consumption of the string corresponding to the undefined symbol; the string corresponding to the defined symbol which is not needed is deleted, which can further reduce the memory consumption of the string corresponding to the defined symbol. Therefore, the embodiment can reduce the memory consumption of the string table in the executable file from multiple aspects, which is helpful to further reduce the size of the executable file and then reduce the memory consumption when the application is loaded.
[0071] For the computer device loading the application, since the memory consumption is less when the application is loaded, the application loading speed of the computer device can be improved.
[0072] In addition, for the application program used in the RTOS system, the program compiling method provided in the embodiment of the present application can realize the separate compilation of the application, and does not need to be compiled together with the RTOS into an executable file, which can realize the separate upgrade and loading of the application.
[0073] Correspondingly, for the device compiling the application, after obtaining the executable file of the application, the executable file can be published. The terminal can load the executable file to realize loading of the application. In the embodiment of the present application, the terminal can dynamically load the executable file and run the code logic of the executable file. Correspondingly, the embodiment of the present application further provides a program loading method. The program loading method provided by the embodiment of the present application is exemplarily described below.
[0074] Figure 2 A flowchart of the program loading method provided by the embodiment of the present application is shown in FIG. 2. As shown in FIG. 2, the method comprises the following steps. Figure 2
[0075] 201. Obtain the executable file of the application.
[0076] 202. Analyze the executable file to obtain the code segment, data segment, string table, relocation table and symbol table corresponding to the relocation table contained in the executable file.
[0077] 203. According to the string table, symbol table and relocation table, relocate the instructions in the code segment that need to be relocated.
[0078] 204. Based on the data segment, execute the instructions after relocation and the instructions in the code segment that do not need to be relocated to load the application.
[0079] In the embodiment, the computer device can load the executable file of the application, run the code logic of the executable file and realize the function corresponding to the application. For example, the executable file can be loaded to broadcast weather, news, music or realize other functions.
[0080] To realize loading of the executable file, in step 201, the executable file of the application can be obtained; and in step 202, the executable file is analyzed to obtain the code segment, data segment, string table, relocation table and symbol table corresponding to the relocation table contained in the executable file.
[0081] Optionally, before analyzing the executable file, the legality of the executable file can also be checked. Optionally, the file header format of the executable file can be obtained; if the file header format of the executable file conforms to the set file header format, it is determined that the executable file passes the legality check and enters step 202 described above. If the file header format of the executable file does not conform to the set file header format, the loading is ended.
[0082] In the embodiment, the code segment mainly refers to a target code segment obtained by merging a plurality of code segments of the target files of the application after compilation; and the data segment mainly refers to a target data segment obtained by merging a plurality of data segments of the target files of the application after compilation. For the specific implementation of the merging of the code segments and the data segments of the plurality of target files, refer to the related content of the above embodiment, which will not be repeated here.
[0083] The relocation table refers to the first relocation table obtained by modifying the relocation tables corresponding to the plurality of target files according to the symbol positions of the target code segment and the target data segment. Correspondingly, in the embodiment, the symbol table corresponding to the relocation table is the second symbol table corresponding to the first relocation table. For the specific implementation of obtaining the second symbol table, refer to the related content of the above embodiment, which will not be repeated here.
[0084] Further, in step 203, the instructions in the code segment that need to be relocated are relocated according to the string table, the symbol table and the relocation table; and in step 204, the instructions in the code segment that do not need to be relocated and the relocated instructions are executed based on the data points in the memory, thereby realizing the loading of the application.
[0085] In the embodiment, in order to realize the dynamic loading of the application, before step 203, for the computer device that does not support Execute In Place (XIP), the code segment, the data segment, the string table, the relocation table and the symbol table can be stored in the memory.
[0086] Optionally, the memory can be applied according to the total size of the code segment, the data segment, the string table, the relocation table and the symbol table; and the code segment, the data segment, the string table, the relocation table and the symbol table can be stored in the applied memory.
[0087] Correspondingly, after the application is loaded, the memory occupied by the code segment, the string table, the relocation table and the symbol table corresponding to the relocation table can be released.
[0088] For the computer device that supports XIP, since the application program can be directly run in the Flash, it is not necessary to read the code into the system RAM, therefore, before step 203, the data segment, the string table, the relocation table and the symbol table can be stored in the memory; and the code segment can be stored in the Flash.
[0089] Optionally, the memory can be applied according to the total size of the data segment, the string table, the relocation table and the symbol table; and the data segment, the string table, the relocation table and the symbol table can be stored in the applied memory.
[0090] Correspondingly, after the application is loaded, the memory occupied by the string table, the relocation table and the symbol table corresponding to the relocation table can be released.
[0091] In the embodiment, on the one hand, the code section and the data section in the executable file of the application are obtained by merging the code section and the data section of the plurality of object files compiled from the source code of the application, so that the table header information can be reduced, and the memory consumption of the table header can be reduced, and on the other hand, the symbol table in the executable file only retains the symbol table corresponding to the relocation table required when the application is loaded, so that the space consumption of the symbol table can be reduced, which is helpful to further reduce the size of the executable file, and thus the memory consumption of the terminal when the application is dynamically loaded can be reduced.
[0092] In the embodiment, in order to further reduce the size of the executable file, the string of the undefined symbol depended by the application can also be compressed. The specific implementation of compressing the string of the undefined symbol depended by the application can refer to the related content of the above embodiment, which will not be described here. Accordingly, in the embodiment, the string table includes the compressed string of the undefined symbol depended by the application. The compressed string of the undefined symbol can be one or more characters, and the number of characters is less than that before compression. For the implementation of compressing the string of the undefined symbol by using the hash algorithm, the string table can be a string hash table.
[0093] Based on the string table composed of the compressed string of the undefined symbol depended by the application, when the instruction in the code section that needs to be relocated is relocated, the actual loading address of the code section corresponding to the undefined symbol can be found by the operating system according to the compressed string recorded in the string table.
[0094] The string table (string-table) stores the symbol name, such as the symbol name with a length greater than 8 bytes. Taking the symbol name as an example, in the object file, the size of the structure corresponding to the symbol is fixed, and a part of bytes (such as 8 bytes) can be left to store the symbol name. If the symbol name is relatively short and is less than or equal to the bytes used to store the symbol name in the symbol table, the symbol name is directly stored in the structure of the symbol table; if the length of the symbol name is greater than the bytes used to store the symbol name in the symbol table, the symbol name is stored in the string table (string-table), and then the offset address of the symbol name in the string table is recorded in the symbol table used to store the symbol name. In the embodiment, the string table stores the compressed string of the undefined symbol, that is, the compressed string of the symbol name of the undefined symbol. Based on this, in the embodiment, the compressed string of the undefined symbol corresponding to the compressed string of the undefined symbol in the string table can be determined according to the offset address of the undefined symbol recorded in the symbol table, and the actual loading address of the code section corresponding to the undefined symbol can be found by the operating system according to the compressed string recorded in the string table.
[0095] Further, a new symbol table can be created according to the actual load address of the code segment corresponding to the undefined symbol and the symbol table; and the instructions in the code segment that need to be relocated are relocated according to the new symbol table and the relocation table.
[0096] Optionally, the load address of the code segment corresponding to the undefined symbol recorded in the symbol table can be modified according to the actual load address of the code segment corresponding to the undefined symbol to obtain a new symbol table.
[0097] For the symbol table, the main attributes include the symbol name and the offset address (relative address) of the symbol, etc. In the embodiment, the actual load address of the code segment corresponding to the defined symbol can be found by the operating system according to the name of the defined symbol in the symbol table; then, the offset address of the undefined symbol in the symbol table is modified to the actual load address (i.e. absolute address) of the code segment corresponding to the undefined symbol according to the actual load address of the code segment corresponding to the undefined symbol and the actual load address of the code segment corresponding to the defined symbol to obtain a new symbol table.
[0098] Correspondingly, when the instructions in the code segment that need to be relocated are relocated, the name of the relocation symbol can be determined according to the symbol index in the relocation table; the actual load address of the code segment corresponding to the relocation symbol is obtained by querying the new symbol table according to the name of the relocation symbol; then, the instructions in the code segment that need to be relocated are relocated by using the actual load address of the code segment corresponding to the relocation symbol according to the relocation type in the relocation table.
[0099] The relocation type in the relocation table can be absolute positioning or relative positioning. If the relocation type is absolute positioning, the relocation address of the instruction that needs to be relocated can be obtained by adding the actual load address of the code segment corresponding to the relocation symbol to the address of the instruction that needs to be relocated. If the relocation type is relative positioning, the relocation address of the instruction that needs to be relocated can be obtained by adding the actual load address of the code segment corresponding to the relocation symbol to the difference between the address of the instruction that needs to be relocated and the address of the next instruction of the instruction.
[0100] Further, the instructions after relocation and the instructions in the code segment that do not need to be relocated can be executed based on the data segment in the memory to implement application loading.
[0101] The program loading method provided in the embodiment can be applied to dynamic loading and static loading, and is especially suitable for dynamic loading of a single-process system. For example, the program loading method can be applied to dynamic loading / unloading of an application during the running of a single-process RTOS system.
[0102] It should be noted that the execution subject of each step of the method provided in the above embodiments can be the same device, or the method can also be executed by different devices as the execution subject. For example, the execution subject of steps 101 and 102 can be device A; for another example, the execution subject of step 101 can be device A, and the execution subject of step 102 can be device B; and the like.
[0103] In addition, in some of the processes described in the above embodiments and the accompanying drawings, a plurality of operations appearing in a certain order are included, but it should be clearly understood that these operations can be executed in the order appearing in the text or in parallel, and the serial numbers of the operations, such as 101, 102, etc., are only used to distinguish different operations, and the serial numbers themselves do not represent any execution order. In addition, these processes can include more or fewer operations, and the operations can be executed in sequence or in parallel.
[0104] Correspondingly, the embodiments of the present application also provide a computer readable storage medium storing computer instructions, which, when executed by one or more processors, cause the one or more processors to perform the steps of the above program compilation and / or program loading method.
[0105] The embodiments of the present application also provide a computer program product, comprising: a computer program; and the computer program is executed by a processor to implement the above program compilation and / or program loading method.
[0106] Figure 3 A structural schematic diagram of a computer device provided by the embodiments of the present application is shown in FIG. 1. As shown in the figure, the computer device can include a memory 30a and a processor 30b; wherein the memory 30a is configured to store a computer program. Figure 3
[0107] In some embodiments, the computer device is a program compilation device, such as a computer, etc. Correspondingly, the processor 30b is coupled to the memory 30a and is configured to execute the computer program to: obtain a plurality of object files compiled from source code of an application; perform merging processing on code segments and data segments of the plurality of object files respectively to obtain target code segments and target data segments; modify symbol tables and relocation tables corresponding to the plurality of object files according to symbol positions in the target code segments and the target data segments to obtain a first symbol table and a first relocation table; obtain a second symbol table corresponding to the first relocation table from the first symbol table; and generate an executable file of the application according to the target code segments, the target data segments, the second symbol table and the first relocation table.
[0108] Optionally, the processor 30b is further configured to: link the plurality of object files to obtain an intermediate file of the application before the code segments and the data segments of the plurality of object files are processed respectively; and obtain the code segments and the data segments corresponding to the plurality of object files respectively from the intermediate file.
[0109] Correspondingly, when the processor 30b processes the code segments and the data segments of the plurality of object files respectively, the processor 30b is specifically configured to: process the code segments of the plurality of object files according to the linking relationship between the code segments in the intermediate file to obtain target code segments; and process the data segments of the plurality of object files according to the linking relationship between the data segments in the intermediate file to obtain target data segments.
[0110] Optionally, when the processor 30b obtains the code segments and the data segments corresponding to the plurality of object files respectively from the intermediate file, the processor 30b is specifically configured to: obtain a compiler identifier for compiling source code of the application; match the compiler identifier in a preset correspondence between the compiler identifier and a target file format to obtain a target file format of the plurality of object files; and obtain the code segments and the data segments corresponding to the plurality of object files respectively from the intermediate file according to the target file format of the plurality of object files.
[0111] In some embodiments, when the processor 30b obtains the second symbol table corresponding to the first relocation table from the first symbol table, the processor 30b is specifically configured to: determine a symbol relied on by a relocation symbol according to a dependency relationship between the symbols recorded in the first relocation table; and obtain, from the first symbol table, a symbol table corresponding to the relocation symbol and the symbol relied on by the relocation symbol recorded in the first relocation table as the second symbol table.
[0112] Optionally, the processor 30b is further configured to: determine an undefined symbol relied on by the application according to a symbol type recorded in the first symbol table; obtain a string corresponding to the undefined symbol from a plurality of string tables corresponding to the plurality of object files; and perform compression processing on the string corresponding to the undefined symbol to obtain a processed string table.
[0113] Optionally, when the processor 30b performs compression processing on the string corresponding to the undefined symbol, the processor 30b is specifically configured to: perform compression processing on the string corresponding to the undefined symbol by using a hash algorithm to obtain the processed string table.
[0114] Optionally, when the processor 30b performs compression processing on the string corresponding to the undefined symbol, the processor 30b is specifically configured to: perform compression processing on the string corresponding to the undefined symbol and delete a string corresponding to a defined symbol to obtain the processed string table.
[0115] In some embodiments, the processor 30b is specifically configured to copy the second symbol table, the first relocation table and the processed string table to a file in which the target code section and the target data section are located, to obtain the executable file of the application when generating the executable file of the application.
[0116] The computer device provided in the embodiments can, when compiling an application program, on the one hand, perform merging processing on the code section and the data section of a plurality of target files of the application source code, to obtain a target code section and a target data section, thereby reducing the header information; on the other hand, only the symbol table corresponding to the relocation table required when loading the application is retained, thereby reducing the space consumption of the symbol table; therefore, the size of the executable file of the application generated according to the target code section, the target data section, the relocation table and the symbol table corresponding to the relocation table can be reduced, and the memory consumption when loading the application can be reduced.
[0117] In some embodiments, the computer device provided in the embodiments can be implemented as a program loading device, such as a smart terminal device. For example, a smart phone, a computer, a wearable device or an Internet of Things device, etc. The Internet of Things device can be a smart sound box, a smart television, a smart refrigerator or a smart robot, etc. Correspondingly, in the embodiments, the processor 30b is coupled to the memory 30a and is configured to execute a computer program to: obtain an executable file of an application; parse the executable file to obtain a code section, a data section, a string table, a relocation table and a symbol table corresponding to the relocation table included in the executable file; perform relocation on instructions in the code section that need to be relocated according to the string table, the symbol table and the relocation table; and execute the instructions in the code section that do not need to be relocated and the relocated instructions based on the data section, to load the application.
[0118] Optionally, the string table includes a compressed string of an undefined symbol on which the application depends. Correspondingly, the processor 30b is specifically configured to: find an actual loading address of a code section corresponding to the undefined symbol through an operating system according to the compressed string recorded in the string table when performing relocation on the instructions in the code section that need to be relocated; create a new symbol table according to the actual loading address of the code section corresponding to the undefined symbol and the symbol table; and perform relocation on the instructions in the code section that need to be relocated according to the new symbol table and the relocation table.
[0119] Optionally, the processor 30b is specifically configured to: find an actual loading address of a code section corresponding to a defined symbol through an operating system according to the name of the defined symbol in the symbol table when creating the new symbol table; and modify the offset address of the symbol corresponding to the symbol table according to the actual loading address of the code section corresponding to the undefined symbol and the actual loading address of the code section corresponding to the defined symbol, to obtain the new symbol table.
[0120] Correspondingly, the processor 30b is configured to, when relocating the instruction needing relocation in the code segment, specifically: determine the name of the relocation symbol according to the symbol index in the relocation table; query the new symbol table according to the name of the relocation symbol to obtain the actual loading address of the code segment corresponding to the relocation symbol; and relocate the instruction needing relocation in the code segment according to the relocation type in the relocation table and the actual loading address of the code segment corresponding to the relocation symbol.
[0121] In the embodiment of the present application, the processor 30b is further configured to: store the code segment, the data segment, the string table, the relocation table and the symbol table into the memory 30a1 before relocating the instruction needing relocation in the code segment; or store the data segment, the string table, the relocation table and the symbol table into the memory 30a1, and store the code segment into the flash memory 30a2.
[0122] Correspondingly, the processor 30b is further configured to: release the memory occupied by the code segment, the string table, the relocation table and the symbol table after loading the application; or release the memory occupied by the string table, the relocation table and the symbol table after loading the application.
[0123] The computer device provided in the embodiment has the following advantages. On the one hand, the code segment and the data segment in the executable file of the application are obtained by merging the code segment and the data segment of the plurality of object files compiled from the source code of the application, so that the table header information can be reduced, and the memory consumption of the table header can be reduced; on the other hand, the symbol table in the executable file only retains the symbol table corresponding to the relocation table needed when the application is loaded, so that the space consumption of the symbol table can be reduced, which is helpful to further reduce the size of the executable file, and thus the memory consumption of the terminal when dynamically loading the application can be reduced.
[0124] In some optional embodiments, as shown in Figure 3 The computer device can further include optional components such as a communication component 30c, a power supply component 30d, a display component 30e and an audio component 30f. Figure 3 The computer device is only schematically shown with some components, and does not mean that the computer device must include Figure 3 all the components shown, nor does it mean that the computer device can only include Figure 3 the components shown.
[0125] In embodiments of the present application, the memory is configured to store a computer program and can be configured to store other various data to support operations on the device where it is located. Among them, the processor can execute the computer program stored in the memory to realize the corresponding control logic. The memory can be realized by any type of volatile or non-volatile storage device or their combination, 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 disk or optical disk.
[0126] In embodiments of the present application, the processor can be any hardware processing device that can execute the above-mentioned method logic. Alternatively, the processor can be a central processing unit (CPU), a graphics processing unit (GPU), or a microcontroller unit (MCU); it can also be a programmable device such as a field-programmable gate array (FPGA), a programmable array logic (PAL), a general array logic (GAL), a complex programmable logic device (CPLD), etc.; or an advanced reduced instruction set (RISC) processor (Advanced RISC Machines, ARM) or a system on chip (SOC), etc., but not limited to this.
[0127] In embodiments of the present application, the communication component is configured to facilitate wired or wireless communication between the device where it is located and other devices. The device where the communication component is located can access a wireless network based on a communication standard, such as WiFi, 2G or 3G, 4G, 5G or their combination. In an exemplary embodiment, the communication component receives broadcast signals or broadcast-related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the communication component can also be implemented based on near field communication (NFC) technology, radio frequency identification (RFID) technology, infrared data association (IrDA) technology, ultra wide band (UWB) technology, Bluetooth (BT) technology or other technologies.
[0128] In an embodiment of the present application, the display component can include a liquid crystal display (LCD) and a touch panel (TP). If the display component includes the touch panel, the display component 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.
[0129] In an embodiment of the present application, the power component is configured to provide power to various components of the device in which the power component is located. The power component can include a power management system, one or more power sources, and other components associated with generating, managing and distributing power to the device in which the power component is located.
[0130] In an embodiment of the present application, the audio component can be configured to output and / or input audio signals. For example, the audio component includes a microphone (MIC) that is configured to receive an external audio signal when the device in which the audio component is located is in an operation mode, such as a call mode, a recording mode and a voice recognition mode. The received audio signal can be further stored in a memory or transmitted via the communication component. In some embodiments, the audio component also includes a speaker for outputting audio signals. For example, for a device with a language interaction function, voice interaction with a user can be implemented through the audio component, etc.
[0131] It should be noted that the "first", "second", and the like descriptions herein are used to distinguish different messages, devices, modules, etc., and do not represent the order or limit the "first" and "second" to be different types.
[0132] Those skilled in the art should understand that the embodiments of the present application can be provided as a method, a system, or a computer program product. Therefore, the present application can take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can take the form of a computer program product implemented on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0133] The computer program instructions can also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart and / or block diagram block or blocks.
[0134] These computer program instructions can also be stored in a computer readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer readable memory produce an article of manufacture including instructions which implement the function specified in the flowchart and / or block diagram block or blocks.
[0135] These computer program instructions can also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart and / or block diagram block or blocks.
[0136] In one typical configuration, the computing device includes one or more processors (CPUs), input / output interfaces, network interfaces, and memory.
[0137] The memory can include non-persistent memory and / or volatile memory, such as random access memory (RAM) and / or cache memory, for storing, in general, data and / or program instructions. The memory can also include non-volatile memory, such as read-only memory (ROM), electrically programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), flash memory, or non-volatile random access memory (NVRAM) for storing, in general, data and / or program instructions. The memory is an example of computer readable media.
[0138] Computer-readable media includes permanent and non-permanent, movable and non-movable media that can implement information storage by any method or technology. The information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette, magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information accessible to a computing device. According to the definition herein, computer-readable media does not include transitory media such as modulated data signals and carriers.
[0139] It should also be noted that the terms "comprising", "containing", or any other variant thereof are intended to cover a non-exclusive inclusion, such that a process, method, article or apparatus that comprises a list of elements does not only include those elements, but can also include other elements not expressly listed or inherent to such process, method, article or apparatus. Without more limitations, the element defined by the statement "comprising a" does not exclude the presence of additional identical elements in the process, method, article or apparatus that includes the element.
[0140] The above only describes the embodiments of the present application and is not intended to limit the present application. For those skilled in the art, the present application can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. within the spirit and principle of the present application shall be included in the scope of claims of the present application.
Claims
1. A program compiling method characterized by comprising: The method comprises the following steps: obtaining a plurality of object files compiled from source code of an application; performing merging processing on code segments and data segments of the plurality of object files respectively to obtain target code segments and target data segments; modifying symbol tables and relocation tables corresponding to the plurality of object files according to symbol positions in the target code segments and the target data segments to obtain a first symbol table and a first relocation table; obtaining a second symbol table corresponding to the first relocation table from the first symbol table; generating an executable file of the application according to the target code segments, the target data segments, the second symbol table and the first relocation table.
2. The method of claim 1, wherein, Before performing the merging processing on the code segments and the data segments of the plurality of object files respectively, the method further comprises the following steps: linking the plurality of object files to obtain an intermediate file of the application; obtaining code segments and data segments corresponding to the plurality of object files respectively from the intermediate file; the merging processing on the code segments and the data segments of the plurality of object files respectively to obtain the target code segments and the target data segments comprises the following steps: performing the merging processing on the code segments of the plurality of object files according to a linking relationship between the code segments in the intermediate file to obtain the target code segments; performing the merging processing on the data segments of the plurality of object files according to a linking relationship between the data segments in the intermediate file to obtain the target data segments.
3. The method of claim 2, wherein, The obtaining of the code segments and the data segments corresponding to the plurality of object files respectively from the intermediate file comprises the following steps: obtaining an identifier of a compiler for compiling the source code of the application; matching the identifier of the compiler in a preset corresponding relationship between identifiers of compilers and formats of object files to obtain formats of object files of the plurality of object files; obtaining the code segments and the data segments corresponding to the plurality of object files respectively from the intermediate file according to the formats of object files of the plurality of object files.
4. The method of claim 1, wherein, The obtaining of the second symbol table corresponding to the first relocation table from the first symbol table comprises the following steps: determining symbols depended by a relocation symbol according to a dependency relationship between symbols recorded in the first relocation table; obtaining, from the first symbol table, symbol tables corresponding to the relocation symbol recorded in the first relocation table and the symbols depended by the relocation symbol as the second symbol table.
5. The method of claim 1, wherein, The method further comprises the following steps: determining an undefined symbol depended by the application according to a symbol type recorded in the first symbol table; obtaining a character string corresponding to the undefined symbol from a string table corresponding to the plurality of object files; performing compression processing on the character string corresponding to the undefined symbol to obtain a processed string table.
6. The method of claim 5, wherein, The generating of the executable file of the application according to the target code segments, the target data segments, the second symbol table and the first relocation table comprises the following steps: copying the second symbol table, the first relocation table and the processed string table to files in which the target code segments and the target data segments are located to obtain the executable file of the application.
7. A program loading method characterized by comprising: The method comprises the following steps: obtaining an executable file of an application; The executable file is parsed to obtain a target code segment, a target data segment, a string table, a first relocation table and a second symbol table corresponding to the first relocation table obtained from a first symbol table, wherein the target code segment is obtained by merging code segments of a plurality of object files compiled from source code of the application; The target data segment is obtained by merging data segments of the plurality of object files; the first symbol table and the first relocation table are obtained by modifying symbol tables and relocation tables corresponding to the plurality of object files according to symbol positions of the target code segment and the target data segment; According to the string table, the second symbol table and the first relocation table, the instructions in the target code segment that need to be relocated are relocated; Based on the target data segment, the instructions after relocation and the instructions in the target code segment that do not need to be relocated are executed to load the application.
8. The method of claim 7, wherein, The string table includes a compressed string of an undefined symbol on which the application depends; the method further comprises: According to the string table, the second symbol table and the first relocation table, the instructions in the target code segment that need to be relocated are relocated, comprising: According to the compressed string recorded in the string table, the actual loading address of the code segment corresponding to the undefined symbol is found through the operating system; According to the actual loading address of the code segment corresponding to the undefined symbol and the second symbol table, a new symbol table is created; According to the new symbol table and the first relocation table, the instructions in the target code segment that need to be relocated are relocated.
9. The method of claim 8, wherein, According to the actual loading address of the code segment corresponding to the undefined symbol and the second symbol table, a new symbol table is created, comprising: According to the name of the defined symbol in the second symbol table, the actual loading address of the code segment corresponding to the defined symbol is found through the operating system; According to the actual loading address of the code segment corresponding to the undefined symbol and the actual loading address of the code segment corresponding to the defined symbol, the offset address of the corresponding symbol in the second symbol table is modified to obtain the new symbol table.
10. The method of claim 8, wherein, According to the new symbol table and the first relocation table, the instructions in the target code segment that need to be relocated are relocated, comprising: According to the symbol index in the first relocation table, the name of the relocation symbol is determined; According to the name of the relocation symbol, the new symbol table is queried to obtain the actual loading address of the code segment corresponding to the relocation symbol; According to the relocation type in the first relocation table, the actual loading address of the code segment corresponding to the relocation symbol is used to relocate the instructions in the target code segment that need to be relocated.
11. An electronic device, comprising: comprising: a memory and a processor; wherein the memory is configured to store a computer program; the processor is coupled to the memory and is configured to execute the computer program to perform the steps in the method of any one of claims 1-10.
12. A computer readable storage medium having stored thereon computer instructions, wherein, When the computer instructions are executed by one or more processors, the one or more processors are caused to perform the steps of the method of any of claims 1-10.
13. A computer program product, characterised in that, A computer program is included, when the computer program is executed by one or more processors, the one or more processors are caused to perform the steps of the method of any of claims 1-10.
Citation Information
Patent Citations
Application program dynamic loading method based on microkernel operating system
CN101470619A