Method, device, equipment and storage medium for generating executable files
By fabricating symbol definitions during the symbol parsing process, the compilation failure problem caused by the lack of library files in code collaborative development is solved, the generation and debugging of executable files is realized, and the compilation process is optimized.
Patent Information
- Application Number
- CN202411154889.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-22
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2044-08-22
AI Technical Summary
In code collaborative development and open source environments, developers often fail to compile due to a lack of relevant library files and cannot generate executable files, which affects code debugging.
By fictitious symbol definitions during the symbol resolution process, the linker is used to construct the target symbol definition in the target file to avoid throwing undefined symbol errors, thereby completing the link and generating an executable file.
In the absence of some library files, the executable file was successfully generated, optimizing the compilation process and allowing developers to debug and sort out the code logic.
Smart Images

Figure CN118656083B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer software technology, and in particular to an executable file generation method, device, equipment and storage medium. Background Art
[0002] Nowadays, code collaborative development and code open source culture are becoming more and more popular in modern software development. However, developers often encounter various compilation problems during the compilation process in the local environment. The most common one is the problem of linking errors in the final stage of compilation. The root cause is the lack of relevant libraries.
[0003] The lack of relevant library files will cause the generated binary file to fail to link. In the process of setting up the environment, for example, after the developer pulls the code from GitHub and compiles the source code, the compilation often fails due to the lack of corresponding library files. The reason for the lack of code may be that the author of the repository only open-sourced part of the code, or it may be caused by the mismatch of the local environment. Ultimately, because only part of the code is open-source or the library file is missing, the link fails, and then the compilation and generation of the executable file fails. Therefore, even normal code logic cannot be debugged. According to the current complexity of the code, if it cannot be debugged, it is difficult to sort out the code logic.
[0004] Therefore, how to generate an executable file when some library files are missing is a problem that needs to be solved in the art. Summary of the invention
[0005] In view of this, the purpose of the present invention is to provide an executable file generation method, device, equipment and storage medium, which generates an executable file by fictitiously implementing a symbol instead of throwing an error of undefined symbol during symbol resolution, thereby optimizing the executable file generation process. The specific scheme is as follows:
[0006] In a first aspect, the present application provides a method for generating an executable file, comprising:
[0007] Preprocessing the source code, compiling the preprocessed source code to obtain a corresponding assembly file, and generating a target file corresponding to the source code based on the assembly file;
[0008] Using a linker to read the target file, and parsing the target file to obtain symbols corresponding to the symbol references in the target file;
[0009] Traversing the target file and the corresponding library file to obtain a symbol definition corresponding to the symbol, and matching the symbol with the symbol definition;
[0010] If there is a target symbol that fails to match, the linker is used to construct a target symbol definition corresponding to the target symbol in the target file, and the linker is used to link the target file based on the target symbol definition to obtain a corresponding executable file.
[0011] Optionally, compiling the preprocessed source code to obtain a corresponding assembly file, and generating a target file corresponding to the source code based on the assembly file, includes:
[0012] After reading and analyzing the preprocessed source code using a compiler, the source code is translated into an assembly language to generate the assembly file according to the assembly language;
[0013] The assembly file is read by an assembler, and the assembly file is translated into a machine language, so as to generate the target file corresponding to the source code according to the machine language.
[0014] Optionally, the process of translating the source code into an assembly language to generate the assembly file according to the assembly language further includes:
[0015] Determine the symbol reference in the source code, and mark the symbol reference as unresolved;
[0016] Accordingly, the target file is parsed to obtain symbols corresponding to the symbol references in the target file, including:
[0017] The symbol reference marked in the target file is parsed to obtain the symbol corresponding to the symbol reference.
[0018] Optionally, constructing a target symbol definition corresponding to the target symbol in the target file by using the linker includes:
[0019] The symbol type of the target symbol is determined, and the linker is used to construct a target symbol definition corresponding to the target symbol in the target file according to the symbol type.
[0020] Optionally, constructing a target symbol definition corresponding to the target symbol in the target file according to the symbol type by using the linker includes:
[0021] If the symbol type is a global variable reference symbol, constructing a corresponding global variable definition in the target file using the linker, and initializing the global variable definition;
[0022] If the symbol type is a function reference symbol, a reference function corresponding to the target symbol is determined, and a corresponding function implementation is constructed in the target file according to function parameters of the reference function.
[0023] Optionally, after constructing the target symbol definition corresponding to the target symbol in the target file by using the linker, the method further includes:
[0024] Insert the corresponding target function address into the startup address of the target file based on the target function; the target function is a function used to output the target symbol definition.
[0025] Optionally, the process of compiling the preprocessed source code to obtain a corresponding assembly file further includes:
[0026] The preset symbol construction function of the linker is opened according to preset parameters, so as to utilize the linker to construct the target symbol definition corresponding to the target symbol in the target file based on the preset symbol construction function.
[0027] In a second aspect, the present application provides an executable file generation device, comprising:
[0028] A code compiling module, used to preprocess the source code, compile the preprocessed source code to obtain a corresponding assembly file, and generate a target file corresponding to the source code based on the assembly file;
[0029] A file parsing module, used for reading the target file by using a linker, and parsing the target file to obtain symbols corresponding to symbol references in the target file;
[0030] A symbol matching module, used for traversing the target file and the corresponding library file to obtain the symbol definition corresponding to the symbol, and matching the symbol with the symbol definition;
[0031] A definition construction module is used to construct a target symbol definition corresponding to the target symbol in the target file using the linker if there is a target symbol that fails to match, and to link the target file based on the target symbol definition using the linker to obtain a corresponding executable file.
[0032] In a third aspect, the present application provides an electronic device, comprising a processor and a memory; wherein the memory is used to store a computer program, and the computer program is loaded and executed by the processor to implement the aforementioned executable file generation method.
[0033] In a fourth aspect, the present application provides a computer-readable storage medium for storing a computer program, which implements the aforementioned executable file generation method when executed by a processor.
[0034] In the present application, the source code is preprocessed, and the preprocessed source code is compiled to obtain the corresponding assembly file, and after the target file corresponding to the source code is generated based on the assembly file, the target file is read by the linker, and the target file is parsed to obtain the symbol corresponding to the symbol reference in the target file, and then the target file and the corresponding library file are traversed to obtain the symbol definition corresponding to the above symbol, and the symbol and the symbol definition are matched. If there is a target symbol that fails to match, the linker is used to construct the target symbol definition corresponding to the target symbol in the target file, and the linker is used to link the target file based on the target symbol definition to obtain the corresponding executable file. In this way, in the process of symbol parsing in the first stage of linking, if it is found that the linker cannot find the definition of a symbol, the error of the symbol is not thrown at this time, but a symbol implementation is fictitious according to the definition of this symbol, so as to complete the linking and generate the executable file, avoiding the linking failure and the failure of compiling and generating the executable file due to only part of the code being open source or the lack of library files, which is conducive to developers to debug based on this executable file in the case of missing files, and optimizes the generation process of the executable file. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.
[0036] Figure 1 A flow chart of an executable file generation method provided by this application;
[0037] Figure 2 A flowchart of executable file compilation provided for this application;
[0038] Figure 3 A flow chart of an executable file generation method provided by this application;
[0039] Figure 4 A flow chart of a specific method for generating an executable file provided in this application;
[0040] Figure 5 An executable file generation embodiment effect diagram provided by this application;
[0041] Figure 6 A schematic diagram of the structure of an executable file generating device provided by the present application;
[0042] Figure 7 A structural diagram of an electronic device provided for this application. DETAILED DESCRIPTION
[0043] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0044] Currently, developers often encounter various compilation problems when compiling codes in the local environment. For example, the lack of relevant library files will cause the generated binary file to fail at link time, and ultimately the inability to generate executable files will affect the developer's debugging of some logically complete codes. However, in the process of symbol resolution, if the definition of a symbol cannot be found, the present application can fictionalize an implementation of the symbol based on the definition of the symbol, thereby completing the link and generating the executable file, thereby optimizing the executable file generation process.
[0045] See also Figure 1 As shown, an embodiment of the present invention discloses a method for generating an executable file, comprising:
[0046] Step S11, preprocessing the source code, compiling the preprocessed source code to obtain a corresponding assembly file, and generating a target file corresponding to the source code based on the assembly file.
[0047] In this embodiment, it is understood that a compiled programming language requires a clear compilation process when compiling to convert source code such as C / C++ into machine language, and this process is usually performed before the source code is run. Figure 2 As shown, taking C++ as an example, when compiling the source code into an executable file, the compiler is first used to read the preprocessed source code and analyze it, and then the source code is translated into assembly language to generate an assembly file based on the assembly language, and the assembler is used to read the assembly file and translate the assembly file into machine language to generate a target file corresponding to the source code based on the machine language.
[0048] That is to say, in this embodiment, the source code needs to be preprocessed first, and the C++ source code is read and processed by the preprocessor. The main tasks include deleting comments, expanding macro definitions, inserting included files, processing conditional compilation instructions, etc., to obtain a file without macro definitions, comments, conditional compilation instructions, etc., and then the preprocessed file is read by the compiler, and lexical analysis, syntax analysis, and semantic analysis are performed on it. Finally, the code is translated into assembly language to generate an assembly file, and the assembler is used to read the assembly language file generated by the compiler and translate it into machine language. At this time, the machine language is an instruction sequence executable by the central processing unit to generate a target file based on the above machine language.
[0049] Based on the above code compilation process, it can be understood that for codes lacking library files, the above steps can be executed normally, but in the subsequent linking stage, it may fail because the relocation symbol address cannot find a specific implementation. Therefore, in this embodiment, in the process of translating the source code into assembly language to generate an assembly file according to the assembly language, such as Figure 3 As shown, it is also necessary to determine the symbol references in the source code. At this time, since the compiler does not know the exact definition location of these symbols when compiling a single source file, it is necessary to mark the above symbol references as unresolved symbols. It should also be pointed out that in the process of compiling the preprocessed source code to obtain the corresponding assembly file, the preset symbol construction function of the linker can also be turned on according to the preset parameters, so that the linker can use the preset symbol construction function to construct the target symbol definition corresponding to the target symbol in the target file.
[0050] Step S12: using a linker to read the target file, and parsing the target file to obtain symbols corresponding to the symbol references in the target file.
[0051] In this embodiment, Figure 3 As shown, after obtaining the target file corresponding to the source file, all target files can be read through the linker, and the symbol references in the target file are parsed to obtain the symbol corresponding to the symbol reference in the target file. The symbol here is the symbol corresponding to the symbol reference obtained by parsing the marked symbol reference in the target file in the previous step. It can be understood that the above symbol reference is usually a reference to other functions or global variables.
[0052] Step S13, traversing the target file and the corresponding library file to obtain the symbol definition corresponding to the symbol, and matching the symbol with the symbol definition.
[0053] In this embodiment, Figure 3As shown, the linker can be used to traverse all target files and library files to find the symbol definition corresponding to the above symbol, and match the symbol reference with the corresponding symbol definition.
[0054] Step S14: If there is a target symbol that fails to match, the linker is used to construct a target symbol definition corresponding to the target symbol in the target file, and the linker is used to link the target file based on the target symbol definition to obtain a corresponding executable file.
[0055] In this embodiment, if there is a target symbol that fails to match after the above matching process, the linker is used to construct a target symbol definition corresponding to the target symbol in the target file, and the linker is used to link the target file based on the target symbol definition to obtain the corresponding executable file. In addition, it can be understood that the definitions of all the above-mentioned fictitious symbols are implemented in the same target file, and then the linker can complete the work of relocating and generating the executable file through the process of parsing symbol references, relocating symbol addresses, and generating executable files, and combine various target files into one executable file.
[0056] This embodiment first preprocesses the source code, and compiles the preprocessed source code to obtain the corresponding assembly file, and after generating the target file corresponding to the source code based on the assembly file, the target file is read by the linker, and the target file is parsed to obtain the symbol corresponding to the symbol reference in the target file, and then the target file and the corresponding library file are traversed to obtain the symbol definition corresponding to the above symbol, and the symbol and the symbol definition are matched, if there is a target symbol that fails to match, the target symbol definition corresponding to the target symbol is constructed in the target file by using the preset symbol construction function pre-opened by the linker, and the target file is linked to obtain the corresponding executable file based on the target symbol definition by using the linker. In this way, when it is found that the linker cannot find the definition of a certain symbol during the symbol parsing process in the first stage of the link, the error of the symbol is not thrown, but a symbol implementation is fictitious according to the definition of the symbol, so as to complete the link and generate the executable file, avoiding the link failure and the failure of compiling and generating the executable file due to only part of the code being open source or lack of library files, which is conducive to developers to debug based on this executable file in the case of missing files, and optimizes the generation process of the executable file.
[0057] Based on the above embodiment, it can be known that the present application can construct symbol definitions corresponding to undefined symbols during the symbol resolution process, thereby completing the link generation of executable files. Next, the construction process of the above symbol definitions will be described in detail in this embodiment. Figure 4 As shown, the embodiment of the present application discloses a method for generating an executable file, including:
[0058] Step S21, determining the symbol type of the target symbol, and constructing a target symbol definition corresponding to the target symbol in the target file according to the symbol type using a linker.
[0059] In this embodiment, when the linker is used to construct the target symbol definition corresponding to the target symbol, the symbol type of the target symbol can be determined first, and then the corresponding symbol construction can be performed. Specifically: if the symbol type is a global variable reference symbol, the linker is used to construct the corresponding global variable definition in the target file and initialize the global variable definition; if the symbol type is a function reference symbol, the reference function corresponding to the target symbol is determined, and the corresponding function implementation is constructed in the target file according to the function parameters of the reference function. In other words, if the linker cannot find the definition of a symbol in the target file, the linker constructs the corresponding definition according to the type of the symbol. If the symbol is a reference to a global variable, the linker constructs a definition of a global variable and performs default initialization on it; if the symbol is a reference to a function, the linker constructs an implementation of a function, and the parameters, function name, and return type of the constructed function are exactly the same as those of the reference, and a string is output in the corresponding function body to prompt the user that this is a function fictitious by the linker.
[0060] Step S22: inserting a corresponding target function address into the startup address of the target file based on the target function; the target function is a function used to output the target symbol definition.
[0061] In this embodiment, after the symbol definition is fictitious in the above steps, it is necessary to insert the corresponding target function address at the start address of the target file based on the target function; wherein the above target function is a function for outputting the target symbol definition. Specifically, a linker can be used to insert a function address at the address started by _start, so as to use this function to output all function definitions and global variables fictitious by the linker, so that developers can check whether the debugged function is fictitious by the linker.
[0062] Step S23: Using the linker to link the target file based on the target symbol definition to obtain a corresponding executable file.
[0063] For a more specific processing procedure of the above step S23, reference may be made to the corresponding contents disclosed in the above embodiments, which will not be described in detail here.
[0064] In this embodiment, the symbol type of the target symbol is first determined, and the target symbol definition corresponding to the target symbol is constructed in the target file by using the linker according to the symbol type, and then the corresponding target function address is inserted into the startup address of the target file based on the target function, so as to link the target file based on the target symbol definition to obtain the corresponding executable file. Through the above technical scheme, in this embodiment, after the source code is preprocessed, compiled, assembled, and the target file is generated, in the symbol parsing process of the linking stage, if it is found that the definition of the symbol is not found in all the target files and library files, the implementation of the fictitious symbol is realized, and the developer is reminded that the definition of this symbol is fictitious. In this way, based on the above compilation method for missing code links, the missing function definition and variable definition are fictitious, and these definitions are generated in the same target file, and finally the linker links all the target files and library files to generate an executable file. The developer can debug based on this executable file, sort out the logic of the code and can distinguish which definitions are implemented by the linker.
[0065] See also Figure 5 As shown, the embodiment of the present application also discloses a specific executable file generation embodiment based on the above embodiment, including:
[0066] If the input main.c source code is as follows:
[0067] void initial_env();
[0068] void parse_input(char* input);
[0069] void public_func();
[0070] void private_implement(int input);
[0071] extern int globalVar;
[0072] int main(int argc, char *argv[]) {
[0073] initial_env();
[0074] parse_input(argv[1]);
[0075] public_func();
[0076] private_implement(globalVar);
[0077] return 0;
[0078] };
[0079] At this time, if the implementation of the private_implement function in the above code is not open source, when the developer compiles the above code, an error will be reported in the linking process, because it is found that the function implementation of private_implement cannot be found during symbol resolution. Therefore, according to the executable file generation method in the above embodiment, first when the compiler compiles the code, the function of the linker fictitious symbol implementation is turned on by specifying parameters, and then the compiler compiles the code, generates the target file through preprocessing, compilation, and assembly, and then links, and the linker parses the symbols initial_env, parse_input, public_func, private_implement, and globalVar in the target file. At this time, the linker will traverse all the generated target files and library files, find the definitions of these symbols, and match the symbol references with the corresponding definitions. If initial_env, parse_input, and public_func can all be found in other target files, but private_implement and globalVar cannot be found, the linker will generate a C code file named implement_by_linker.c. First, the output_func function is generated in the implement_by_linker.c file. The function implementation is as follows:
[0080] void output_func() {
[0081] printf("---variable mock by linker\n");
[0082] printf("globalVar\n");
[0083] };
[0084] The above function can prompt the user which global variables are implemented by the linker;
[0085] At the same time, define global variables:
[0086] Int globalVal = 0;
[0087] And generate the implementation of private_implement in it. The code implementation is as follows:
[0088] void private_implement(char* input) {
[0089] printf("private_implement function is mocked by linker\n");
[0090] };
[0091] After that, symbol resolution continues. If no other symbols are found, implement_by_linker.c will be compiled to generate the target file implement_by_linker.o. Then the linker will relink all the target files and library files to generate the executable file. In this way, based on the executable file generated above, developers can do debugging work and sort out the logic of most of the codes, optimizing the generation process of the executable file.
[0092] See also Figure 6 As shown, the embodiment of the present application also discloses an executable file generating device, including:
[0093] A code compiling module 11 is used to preprocess the source code, compile the preprocessed source code to obtain a corresponding assembly file, and generate a target file corresponding to the source code based on the assembly file;
[0094] A file parsing module 12, used for reading the target file by using a linker, and parsing the target file to obtain symbols corresponding to symbol references in the target file;
[0095] Among them, the above-mentioned file parsing module 12 can specifically read the target file corresponding to the source code, that is, the initial file generated by compiling the source code, before using the linker to link one or more target files obtained by the above-mentioned source code through the compiler or assembler and the library files corresponding to the target files to generate the corresponding executable file, and then parse the undefined symbol references in the above-mentioned target file through the linker, so as to determine the symbols referenced in the target file. It can be understood that the above-mentioned symbol reference is a way of expressing the reference to the class, interface, field or method during the source file compilation period when the Java class in the source file is compiled into a class file, specifically describing the referenced target with a set of symbols, because and because the compiler does not know the exact definition location of these symbols when compiling a single source file, these symbols can be marked as unresolved in the compilation stage for subsequent search. For example, when a class references a static field or method of another class, this reference exists in the class file in the form of a symbol reference, and until it is run, this symbol reference will be resolved as a direct reference.
[0096] A symbol matching module 13 is used to traverse the target file and the corresponding library file to obtain the symbol definition corresponding to the symbol, and match the symbol with the symbol definition;
[0097] Specifically, the symbol matching module 13 can traverse all target files through a linker, and traverse all corresponding library files according to the library file link corresponding to the target file, so as to find the definition of the undefined symbol corresponding to the symbol reference, and match the symbol reference with the corresponding definition. For example, after the linker parses the symbols initial_env, parse_input, public_func, private_implement, and globalVar in the target file, it can traverse all target files and corresponding library files to find the definition of the above symbols, so as to match initial_env, parse_input, public_func, private_implement, and globalVar with the corresponding definition. At this time, in a specific embodiment, if the above-mentioned symbolic references can find corresponding specific implementations in the target file, the linker is directly used to link the target file and the library file to generate the corresponding executable file; if initial_env, parse_input, and public_func can be found in other target files. Specific implementations, but private_implement and globalVar do not have corresponding symbolic specific implementations, the linker is used to generate a symbol definition corresponding to the private_implement and globalVar constructs, so that the linker can be used to link the target file and the library file according to the definition of the construct to generate the corresponding executable file.
[0098] The definition construction module 14 is used to use the linker to construct a target symbol definition corresponding to the target symbol in the target file if there is a target symbol that fails to match, and use the linker to link the target file based on the target symbol definition to obtain a corresponding executable file.
[0099] This embodiment first preprocesses the source code, and compiles the preprocessed source code to obtain the corresponding assembly file, then generates the target file corresponding to the source code based on the assembly file, uses a linker to read the target file, and parses the target file to obtain the symbol corresponding to the symbol reference in the target file, and traverses the target file and the corresponding library file to obtain the symbol definition corresponding to the above symbol, and matches the symbol with the symbol definition, if there is a target symbol that fails to match, then the linker is used to construct the target symbol definition corresponding to the target symbol in the target file, and the linker is used to link the target file based on the target symbol definition to obtain the corresponding executable file. In this way, in the process of symbol parsing in the first stage of linking, if it is found that the linker cannot find the definition of a certain symbol, the error of the symbol is not thrown at this time, but a symbol implementation is fictitious according to the definition of this symbol, so as to complete the linking and generate the executable file, avoiding the linking failure and the failure of compiling and generating the executable file due to only part of the code being open source or the lack of library files, which is conducive to developers to debug based on this executable file in the case of missing files, and optimizes the generation process of the executable file.
[0100] In some specific embodiments, the code compiling module 11 specifically includes:
[0101] A code assembly unit, configured to read the preprocessed source code using a compiler, analyze the preprocessed source code, and then translate the source code into an assembly language, so as to generate the assembly file according to the assembly language;
[0102] The file generating unit is used to read the assembly file by using an assembler, and translate the assembly file into a machine language, so as to generate the target file corresponding to the source code according to the machine language.
[0103] In some specific embodiments, the code compiling module 11 further includes:
[0104] A symbol marking unit, used for determining the symbol reference in the source code and marking the symbol reference as unresolved;
[0105] Correspondingly, the file parsing module 12 specifically includes:
[0106] The symbol parsing unit is used to parse the symbol reference marked in the target file to obtain the symbol corresponding to the symbol reference.
[0107] In some specific embodiments, the definition construction module 14 specifically includes:
[0108] The definition construction submodule is used to determine the symbol type of the target symbol, and use the linker to construct the target symbol definition corresponding to the target symbol in the target file according to the symbol type.
[0109] In some specific embodiments, the definition construction submodule specifically includes:
[0110] A first definition construction unit, configured to construct a corresponding global variable definition in the target file by using the linker if the symbol type is a global variable reference symbol, and to initialize the global variable definition;
[0111] The second definition construction unit is used to determine the reference function corresponding to the target symbol if the symbol type is a function reference symbol, and construct a corresponding function implementation in the target file according to function parameters of the reference function.
[0112] In some specific embodiments, the definition construction module 14 further includes:
[0113] A function insertion unit is used to insert a corresponding target function address into the startup address of the target file based on the target function; the target function is a function used to output the target symbol definition.
[0114] In some specific embodiments, the code compiling module 11 further includes:
[0115] The function activation unit is used to activate the preset symbol construction function of the linker according to preset parameters, so as to utilize the linker to construct the target symbol definition corresponding to the target symbol in the target file based on the preset symbol construction function.
[0116] Furthermore, the present application also discloses an electronic device. Figure 7 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content in the diagram cannot be regarded as any limitation on the scope of use of the present application.
[0117] Figure 7 The present invention provides a schematic diagram of the structure of an electronic device 20 provided in an embodiment of the present application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 is used to store a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the executable file generation method disclosed in any of the aforementioned embodiments. In addition, the electronic device 20 in this embodiment may specifically be an electronic computer.
[0118] In this embodiment, the power supply 23 is used to provide working voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and the external device, and the communication protocol it follows is any communication protocol that can be applied to the technical solution of the present application, and is not specifically limited here; the input and output interface 25 is used to obtain external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs and is not specifically limited here.
[0119] In addition, the memory 22, as a carrier for storing resources, can be a read-only memory, a random access memory, a disk or an optical disk, etc. The resources stored thereon can include an operating system 221, a computer program 222, etc., and the storage method can be temporary storage or permanent storage.
[0120] The operating system 221 is used to manage and control the hardware devices and computer program 222 on the electronic device 20, which can be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program that can be used to complete the executable file generation method executed by the electronic device 20 disclosed in any of the aforementioned embodiments, the computer program 222 can further include a computer program that can be used to complete other specific tasks.
[0121] Furthermore, the present application also discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, the aforementioned disclosed executable file generation method is implemented. The specific steps of the method can refer to the corresponding contents disclosed in the aforementioned embodiments, and will not be repeated here.
[0122] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0123] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0124] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0125] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0126] The technical solution provided by the present application is introduced in detail above. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea. At the same time, for general technicians in this field, according to the idea of the present application, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A method for generating an executable file, It is characterized in that include: Preprocessing the source code, compiling the preprocessed source code to obtain a corresponding assembly file, and generating a target file corresponding to the source code based on the assembly file; Using a linker to read the target file, and parsing the target file to obtain symbols corresponding to the symbol references in the target file; Traversing the target file and the corresponding library file to obtain a symbol definition corresponding to the symbol, and matching the symbol with the symbol definition; If there is a target symbol that fails to match, construct a target symbol definition corresponding to the target symbol in the target file using the linker, and link the target file based on the target symbol definition using the linker to obtain a corresponding executable file; Wherein, constructing the target symbol definition corresponding to the target symbol in the target file by using the linker includes: Determine the symbol type of the target symbol, and construct a target symbol definition corresponding to the target symbol in the target file using the linker according to the symbol type; Correspondingly, the step of constructing a target symbol definition corresponding to the target symbol in the target file according to the symbol type by using the linker includes: If the symbol type is a global variable reference symbol, constructing a corresponding global variable definition in the target file using the linker, and initializing the global variable definition; If the symbol type is a function reference symbol, determining the reference function corresponding to the target symbol, and constructing a corresponding function implementation in the target file according to the function parameters of the reference function; Furthermore, the process of compiling the preprocessed source code to obtain the corresponding assembly file also includes: The preset symbol construction function of the linker is opened according to preset parameters, so as to utilize the linker to construct the target symbol definition corresponding to the target symbol in the target file based on the preset symbol construction function.
2. The method for generating an executable file according to claim 1, It is characterized in that The compiling the preprocessed source code to obtain a corresponding assembly file, and generating a target file corresponding to the source code based on the assembly file, includes: After reading and analyzing the preprocessed source code using a compiler, the source code is translated into an assembly language to generate the assembly file according to the assembly language; The assembly file is read by an assembler, and the assembly file is translated into a machine language, so as to generate the target file corresponding to the source code according to the machine language.
3. The method for generating an executable file according to claim 2, It is characterized in that The process of translating the source code into assembly language to generate the assembly file according to the assembly language also includes: Determine the symbol reference in the source code, and mark the symbol reference as unresolved; Accordingly, the target file is parsed to obtain symbols corresponding to the symbol references in the target file, including: The symbol reference marked in the target file is parsed to obtain the symbol corresponding to the symbol reference.
4. The method for generating an executable file according to any one of claims 1 to 3, It is characterized in that After constructing the target symbol definition corresponding to the target symbol in the target file by using the linker, the method further includes: Insert the corresponding target function address into the startup address of the target file based on the target function; the target function is a function used to output the target symbol definition.
5. An executable file generating device, It is characterized in that include: A code compiling module, used to preprocess the source code, compile the preprocessed source code to obtain a corresponding assembly file, and generate a target file corresponding to the source code based on the assembly file; A file parsing module, used for reading the target file by using a linker, and parsing the target file to obtain symbols corresponding to symbol references in the target file; A symbol matching module, used for traversing the target file and the corresponding library file to obtain the symbol definition corresponding to the symbol, and matching the symbol with the symbol definition; A definition construction module is used for constructing a target symbol definition corresponding to the target symbol in the target file by using the linker if there is a target symbol that fails to match, and linking the target file based on the target symbol definition by using the linker to obtain a corresponding executable file; The definition construction module specifically includes: A definition construction submodule is used to determine the symbol type of the target symbol, and use the linker to construct a target symbol definition corresponding to the target symbol in the target file according to the symbol type; Correspondingly, the definition construction submodule specifically includes: A first definition construction unit, configured to construct a corresponding global variable definition in the target file by using the linker if the symbol type is a global variable reference symbol, and to initialize the global variable definition; A second definition construction unit is used to determine the reference function corresponding to the target symbol if the symbol type is a function reference symbol, and construct a corresponding function implementation in the target file according to function parameters of the reference function; Furthermore, the code compilation module further includes: The function activation unit is used to activate the preset symbol construction function of the linker according to preset parameters, so as to utilize the linker to construct the target symbol definition corresponding to the target symbol in the target file based on the preset symbol construction function.
6. An electronic device, It is characterized in that The electronic device comprises a processor and a memory; wherein the memory is used to store a computer program, and the computer program is loaded and executed by the processor to implement the executable file generation method according to any one of claims 1 to 4.
7. A computer-readable storage medium, It is characterized in that Used to store a computer program, which, when executed by a processor, implements the executable file generation method according to any one of claims 1 to 4.
Citation Information
Patent Citations
Software defect detecting method based on dynamic symbolic execution
CN106354630A
A file generation method and device, a file reading method and device, equipment and a storage medium
CN113434734A
Software automatic testing method based on function level selection symbolization mixed execution
CN114138669A
Application program compatible mode operation method and apparatus, and computing device
CN115658388A
Intelligent contract denial of service vulnerability detection method based on symbolic execution constraint optimization
CN117150497A
