A compiling method, device, terminal and storage medium for Lua code embedding

By converting Lua code into ELF target files and automatically generating C language files, the adaptation problem on devices of different manufacturers is solved, and efficient embedding and cross-platform adaptation of Lua code is achieved, reducing development costs and improving efficiency.

CN114385184BActive Publication Date: 2025-03-21SUZHOU MAXNET NETWORK SECURITY TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111577467.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-22
Publication Date
2025-03-21
Estimated Expiration
2041-12-22

AI Technical Summary

Technical Problem

When integrating applications developed in C language into equipment of different manufacturers, the requirements of each manufacturer are different, which requires time to communicate with the manufacturer for adaptation, which increases development costs.

Method used

By converting the Lua code file into an ELF target file, and automatically generate a C language file to store the Lua code content, and finally compiling it into an ELF target file, the embedding of Lua code is realized.

Benefits of technology

It realizes efficient embedding of Lua code, avoids the increase in the number of files in the application package, reduces the limitations on file read and write permissions, and simplifies the cross-platform adaptation process, reduces development costs and improves development efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114385184B_ABST
    Figure CN114385184B_ABST
Patent Text Reader

Abstract

The present invention discloses a compilation method, device, terminal and storage medium for Lua code embedding. The present invention can efficiently embed Lua code into various cross-platform applications, and during the execution of the method, the embedding of Lua code does not increase the number of files in the program package of the application, because the system avoids the restriction of the permission of the program to read and write files. At the same time, the method based on the present invention can conveniently realize the automated compilation process, reduce the development cost of the enterprise, and improve the development efficiency of the enterprise.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to a compilation scheme for embedding Lua code, and in particular to a compilation method, device, terminal and storage medium for embedding Lua code into a cross-platform application, belonging to the field of computer program compilation. Background Art

[0002] Lua is a small embedded scripting language designed to provide flexible expansion and customization capabilities for applications by being flexibly embedded in them. Lua is written in standard C language and can be compiled and run on almost all operating systems and platforms. Due to its advantages such as fast speed and strong scalability, Lua is widely used in various applications.

[0003] In the actual project development process, sometimes you need to run applications developed in C language on devices from different manufacturers. Due to the above advantages of Lua, the technicians usually deal with it by reading the Lua code file and loading it for execution when the program starts.

[0004] However, in the process of integrating applications into devices, the requirements of various manufacturers are different. Some manufacturers will limit the number of files and permissions in the program package in the device. At this time, if you want to integrate and run the application (using Lua code) smoothly on such devices, it is necessary for technicians to spend time communicating with the manufacturer and adapting. In the real environment, each manufacturer will have its own set of tool chains. If there are many similar devices that need to be integrated and adapted, it will greatly increase the development cost of the application.

[0005] In summary, how to propose a new compilation solution for embedding Lua code into cross-platform applications based on the existing technology to solve the shortcomings and defects in the existing technology has become an urgent problem to be solved by technical personnel in this field. Summary of the invention

[0006] In view of the above-mentioned defects in the prior art, the present invention proposes a compilation method, device, terminal and storage medium for embedding Lua code into a cross-platform application, as follows.

[0007] A compilation method for Lua code embedding, comprising:

[0008] According to the file format and architecture of the first ELF target file obtained, the Lua code file is converted into a second ELF target file, and symbolic variables representing the starting address and the ending address of the second ELF target file are respectively obtained;

[0009] Automatically generate a C language file and implement a function, wherein the Lua code file content is stored in the storage area of ​​the function, the function is called before the application needs to load the program code of the Lua code, and the C language file is compiled into a third ELF target file;

[0010] The second ELF target file and the third ELF target file are linked into an application program to complete the embedding of the Lua code file.

[0011] Preferably, the converting of the Lua code file into a second ELF target file according to the format and architecture of the acquired first ELF target file, and respectively acquiring symbolic variables representing the start address and the end address of the second ELF target file include:

[0012] Randomly select an ELF file generated during the application compilation process as the first ELF target file;

[0013] Obtain the file format and architecture of the first ELF object file;

[0014] Converting the Lua code file into a second ELF target file having the file format and architecture of the first ELF target file, wherein the content of the Lua code file is present in a data segment of the second ELF target file, and the data segment is marked as DS;

[0015] Check the symbol table of the second ELF target file to obtain two symbol variables therein, one symbol variable represents the starting address of the data segment DS, recorded as pStartAddr; and the other symbol variable represents the ending address of the data segment DS, recorded as pEndAddr.

[0016] Preferably, the automatic generation of a C language file and implementation of a function, wherein the Lua code file content is stored in the storage area of ​​the function, the function is called before the application needs to load the program code of the Lua code, and the C language file is compiled into a third ELF target file, including:

[0017] Automatically generate a C language file, implement a function in the C language file, the function of the function is to calculate and return a storage area according to pStartAddr and pEndAddr, and the storage area stores the content of the Lua code file;

[0018] Before the application needs to load the program code of the Lua code, calling the storage area returned by the function;

[0019] Compile the C language file into a third ELF object file.

[0020] A compiling device for Lua code embedding, comprising:

[0021] The target file preparation module is used to convert the Lua code file into a second ELF target file according to the file format and architecture of the first ELF target file, and respectively obtain symbolic variables representing the starting address and the ending address of the second ELF target file;

[0022] A target file compiling module, for automatically generating a C language file and implementing a function, wherein the Lua code file content is stored in the storage area of ​​the function, and the function is called before the application needs to load the program code of the Lua code, and the C language file is compiled into a third ELF target file;

[0023] A code embedding module is used to link the second ELF target file and the third ELF target file into an application program to complete the embedding of the Lua code file.

[0024] Preferably, the target file preparation module includes:

[0025] A first target file selection unit, used for arbitrarily selecting an ELF file generated in the process of compiling an application as a first ELF target file;

[0026] A first target file processing unit, used for obtaining the file format and architecture of the first ELF target file;

[0027] A second target file generating unit is used for converting the Lua code file into a second ELF target file having the file format and architecture of the first ELF target file, wherein the content of the Lua code file exists in a data segment of the second ELF target file and the data segment is marked as DS;

[0028] The second target file processing unit is used to check the symbol table of the second ELF target file and obtain two symbol variables therein, one symbol variable represents the starting address of the data segment DS, recorded as pStartAddr; and the other symbol variable represents the ending address of the data segment DS, recorded as pEndAddr.

[0029] Preferably, the target file compiling module includes:

[0030] A C language file generating unit, used for automatically generating a C language file, implementing a function in the C language file, the function of the function is to calculate and return a storage area according to pStartAddr and pEndAddr, and the storage area stores the content of the Lua code file;

[0031] A function calling unit, used for calling the storage area returned by the function before the application needs to load the program code of the Lua code;

[0032] The third target file compiling unit is used to compile the C language file into a third ELF target file.

[0033] A terminal comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps in the compilation method for Lua code embedding as described above when executing the computer program.

[0034] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps in the compilation method for Lua code embedding as described above are implemented.

[0035] The advantages of the present invention are mainly reflected in the following aspects:

[0036] The present invention discloses a compilation method for embedding Lua code, which can efficiently embed Lua code into various cross-platform applications, and during the execution of the method, the embedding of Lua code does not increase the number of files in the program package of the application, because the system avoids the restriction of the permission of the program to read and write files. At the same time, the method based on the present invention can conveniently realize the automated compilation process, reduce the development cost of the enterprise, and improve the development efficiency of the enterprise.

[0037] Corresponding to the method, a compiling device, terminal and storage medium for Lua code embedding of the present invention can efficiently and accurately embed Lua code into cross-platform applications with a systematic and standardized processing flow, and the overall adaptability and versatility of the solution are strong.

[0038] The present invention also provides a reference for other related problems in the same field, and can be expanded and extended based on this, and applied to other technical solutions related to Lua code embedding in the same field, and has a very broad application prospect.

[0039] The specific implementation modes of the present invention are further described below in combination with the accompanying drawings of the embodiments, so that the technical solutions of the present invention are easier to understand and grasp. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] Figure 1 It is a schematic diagram of the process of the present invention;

[0041] Figure 2 It is a schematic diagram of the structure of the device of the present invention. DETAILED DESCRIPTION

[0042] The present invention proposes a compilation method, device, terminal and storage medium for embedding Lua code into a cross-platform application, so that the application does not need to rely on external Lua code files, and finally only needs to provide an application file to the manufacturer for integration; it also makes the operator no longer need to spend too much time on cross-platform adaptation, which greatly improves development efficiency and reduces development costs. The specific scheme is as follows.

[0043] On the one hand, the present invention relates to a compiling method for Lua code embedding, the process is as follows Figure 1 As shown, including:

[0044] S1. According to the file format and architecture of the acquired first ELF target file, the Lua code file is converted into a second ELF target file, and symbolic variables representing the start address and the end address of the second ELF target file are respectively obtained.

[0045] This step can be concretely described as the following process.

[0046] S11. Randomly select an ELF file generated during the application compilation process as the first ELF target file (.o file).

[0047] S12. Use the objdump program in the tool chain to obtain the file format and architecture of the first ELF target file.

[0048] S13, using the objcopy program in the tool chain to convert the Lua code file into a second ELF target file having the file format and architecture of the first ELF target file, the content of the Lua code file exists in the data segment of the second ELF target file, and the data segment is marked as DS.

[0049] S14. Use the objdump program in the tool chain to view the symbol table of the second ELF target file, and obtain two symbol variables therein, one symbol variable represents the starting address of the data segment DS, recorded as pStartAddr; and the other symbol variable represents the ending address of the data segment DS, recorded as pEndAddr.

[0050] S2. Automatically generate a C language file and implement a function, wherein the Lua code file content is stored in the storage area of ​​the function, and the function is called before the application needs to load the program code of the Lua code, and the C language file is compiled into a third ELF target file.

[0051] This step can be concretely described as the following process.

[0052] S21, automatically generate a C language file, implement a function in the C language file, the function of the function is to calculate and return a storage area according to pStartAddr and pEndAddr, and the storage area stores the content of the Lua code file;

[0053] S22, before the application needs to load the program code of the Lua code, calling the storage area returned by the function;

[0054] S23, using the gcc program in the tool chain to compile the C language file into a third ELF target file.

[0055] S3, linking the second ELF target file and the third ELF target file into an application to complete the embedding of the Lua code file.

[0056] It should be noted here that, in the process of linking the second ELF target file and the third ELF target file into an application, other ELF target files generated in the compilation process will also be linked into the application.

[0057] In summary, the compilation method for embedding Lua code of the present invention can efficiently embed Lua code into various cross-platform applications, and during the execution of the method, the embedding of Lua code does not increase the number of files in the program package of the application, because the system avoids the restriction of the authority of the program to read and write files. At the same time, the method based on the present invention can conveniently realize the automated compilation process, reduce the development cost of the enterprise, and improve the development efficiency of the enterprise.

[0058] On the other hand, a compiling device for Lua code embedding has a structure as follows: Figure 2 As shown, including:

[0059] The target file preparation module is used to convert the Lua code file into a second ELF target file according to the file format and architecture of the first ELF target file, and respectively obtain symbolic variables representing the starting address and the ending address of the second ELF target file;

[0060] A target file compiling module, for automatically generating a C language file and implementing a function, wherein the Lua code file content is stored in the storage area of ​​the function, and the function is called before the application needs to load the program code of the Lua code, and the C language file is compiled into a third ELF target file;

[0061] A code embedding module is used to link the second ELF target file and the third ELF target file into an application program to complete the embedding of the Lua code file.

[0062] The target file preparation module includes:

[0063] A first target file selection unit, used for arbitrarily selecting an ELF file generated in the process of compiling an application as a first ELF target file;

[0064] A first target file processing unit, used for obtaining the file format and architecture of the first ELF target file;

[0065] A second target file generating unit is used for converting the Lua code file into a second ELF target file having the file format and architecture of the first ELF target file, wherein the content of the Lua code file exists in a data segment of the second ELF target file and the data segment is marked as DS;

[0066] The second target file processing unit is used to check the symbol table of the second ELF target file and obtain two symbol variables therein, one symbol variable represents the starting address of the data segment DS, recorded as pStartAddr; and the other symbol variable represents the ending address of the data segment DS, recorded as pEndAddr.

[0067] The target file compilation module includes:

[0068] A C language file generating unit, used for automatically generating a C language file, implementing a function in the C language file, the function of the function is to calculate and return a storage area according to pStartAddr and pEndAddr, and the storage area stores the content of the Lua code file;

[0069] A function calling unit, used for calling the storage area returned by the function before the application needs to load the program code of the Lua code;

[0070] The third target file compiling unit is used to compile the C language file into a third ELF target file.

[0071] In another aspect, the present invention further relates to a terminal, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the steps in the compilation method for Lua code embedding as described above are implemented, such as steps S1 to S3. Alternatively, when the processor executes the computer program, the functions of each module / unit in the above-mentioned device embodiments are implemented, such as Figure 2 The functions of each module / unit shown.

[0072] On the other hand, the present invention also relates to a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps in the compilation method for Lua code embedding as described above are implemented.

[0073] Among them, the readable storage medium can be a computer storage medium or a communication medium. The communication medium includes any medium that facilitates the transmission of a computer program from one place to another. The computer storage medium can be any available medium that can be accessed by a general or special-purpose computer. For example, a readable storage medium is coupled to a processor so that the processor can read information from the readable storage medium and write information to the readable storage medium. Of course, the readable storage medium can also be a component of the processor. The processor and the readable storage medium can be located in an application-specific integrated circuit (ASIC). In addition, the ASIC can be located in a user device. Of course, the processor and the readable storage medium can also exist in a communication device as discrete components. The readable storage medium can be a read-only memory (ROM), a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, and an optical data storage device, etc.

[0074] Corresponding to the content of the method, a compilation device, terminal and storage medium for Lua code embedding of the present invention can embed Lua code into cross-platform applications efficiently and accurately with a systematic and standardized processing flow, and the overall adaptability and versatility of the solution are strong.

[0075] The present invention also provides a reference for other related problems in the same field, and can be expanded and extended based on this, and applied to other technical solutions related to Lua code embedding in the same field, and has a very broad application prospect.

[0076] It is obvious to those skilled in the art that the present invention is not limited to the details of the exemplary embodiments described above, and that the present invention can be implemented in other specific forms without departing from the spirit and essential features of the present invention. Therefore, the embodiments should be regarded as exemplary and non-limiting from any point of view, and the scope of the present invention is defined by the appended claims rather than the above description, and it is intended that all changes falling within the meaning and scope of the equivalent elements of the claims are included in the present invention, and any reference numerals in the claims should not be regarded as limiting the claims involved.

[0077] In addition, it should be understood that although the present specification is described according to implementation modes, not every implementation mode contains only one independent technical solution. This description of the specification is only for the sake of clarity. Those skilled in the art should regard the specification as a whole. The technical solutions in each embodiment may also be appropriately combined to form other implementation modes that can be understood by those skilled in the art.

Claims

1. A compilation method for Lua code embedding, characterized in that: include: Randomly select an ELF file generated during the application compilation process as the first ELF target file; Obtain the file format and architecture of the first ELF target file, convert the Lua code file into a second ELF target file, and respectively obtain symbolic variables representing a starting address and an ending address of the second ELF target file; Automatically generate a C language file and implement a function, wherein the Lua code file content is stored in the storage area of ​​the function, the function is called before the application needs to load the program code of the Lua code, and the C language file is compiled into a third ELF target file; The second ELF object file and the third ELF object file are linked into an application program to complete the embedding of the Lua code file; Check the symbol table of the second ELF target file, obtain two symbol variables therein, one symbol variable represents the starting address of the data segment DS, recorded as pStartAddr; the other symbol variable represents the ending address of the data segment DS, recorded as pEndAddr; the automatic generation of a C language file and implementation of a function, the storage area of ​​the function stores the content of the Lua code file, the function is called before the application needs to load the program code of the Lua code, and the C language file is compiled into a third ELF target file, including: Automatically generate a C language file, implement a function in the C language file, the function of the function is to calculate and return a storage area according to pStartAddr and pEndAddr, and the storage area stores the content of the Lua code file; Before the application needs to load the program code of the Lua code, calling the storage area returned by the function; Compile the C language file into a third ELF object file.

2. The compiling method for Lua code embedding according to claim 1, characterized in that: The method converts the Lua code file into a second ELF target file according to the format and architecture of the first ELF target file, and respectively obtains symbolic variables representing the starting address and the ending address of the second ELF target file, including: Randomly select an ELF file generated during the application compilation process as the first ELF target file; Obtain the file format and architecture of the first ELF object file; Converting the Lua code file into a second ELF target file having the file format and architecture of the first ELF target file, wherein the content of the Lua code file is present in a data segment of the second ELF target file, and the data segment is marked as DS; Check the symbol table of the second ELF target file to obtain two symbol variables therein, one symbol variable represents the starting address of the data segment DS, recorded as pStartAddr; and the other symbol variable represents the ending address of the data segment DS, recorded as pEndAddr.

3. A compiling device for Lua code embedding, characterized in that: include: The target file preparation module randomly selects an ELF file generated in the application compilation process as the first ELF target file, is used to obtain the file format and architecture of the first ELF target file, converts the Lua code file into a second ELF target file, and respectively obtains symbolic variables representing the starting address and the ending address of the second ELF target file; A target file compiling module, for automatically generating a C language file and implementing a function, wherein the Lua code file content is stored in the storage area of ​​the function, and the function is called before the application needs to load the program code of the Lua code, and the C language file is compiled into a third ELF target file; A code embedding module is used to link the second ELF target file and the third ELF target file into an application program to complete the embedding of the Lua code file; check the symbol table of the second ELF target file to obtain two symbol variables therein, one symbol variable represents the starting address of the data segment DS, and is recorded as pStartAddr; A symbolic variable represents the end address of the data segment DS, denoted as pEndAddr; The target file compilation module includes: A C language file generating unit, used for automatically generating a C language file, implementing a function in the C language file, the function of the function is to calculate and return a storage area according to pStartAddr and pEndAddr, and the storage area stores the content of the Lua code file; A function calling unit, used for calling the storage area returned by the function before the application needs to load the program code of the Lua code; The third target file compiling unit is used to compile the C language file into a third ELF target file.

4. The compiling device for Lua code embedding according to claim 3, characterized in that: The target file preparation module includes: A first target file selection unit, used for arbitrarily selecting an ELF file generated in the process of compiling an application as a first ELF target file; A first target file processing unit, used for obtaining the file format and architecture of the first ELF target file; A second target file generating unit is used for converting the Lua code file into a second ELF target file having the file format and architecture of the first ELF target file, wherein the content of the Lua code file exists in a data segment of the second ELF target file and the data segment is marked as DS; The second target file processing unit is used to check the symbol table of the second ELF target file and obtain two symbol variables therein, one symbol variable represents the starting address of the data segment DS, recorded as pStartAddr; and the other symbol variable represents the ending address of the data segment DS, recorded as pEndAddr.

5. A terminal comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps in the compiling method for Lua code embedding according to any one of claims 1 to 2 are implemented.

6. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps in the compiling method for Lua code embedding according to any one of claims 1 to 2 are implemented.

Citation Information

Patent Citations

  • Method and device for loading ELF (Executable and Linking Format) file

    CN105224370A

  • Method and apparatus for loading ELF file of linux system in windows system

    US20190087208A1