Static library linking method, device and equipment

By finding and marking symbols that have not been explicitly called as global automatic segment markers during the static library linking process, the problem that these symbols cannot take effect in static library linking is solved, and the correct parsing and relocation of symbols is achieved.

CN120029623APending Publication Date: 2025-05-23ZHEJIANG UNIVIEW TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311573245.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-11-23
Publication Date
2025-05-23

AI Technical Summary

Technical Problem

During the static library linking process, symbols that are not explicitly called cannot take effect, especially those with special attributes such as _attribute_((constructor)) and _attribute_((destructor)).

Method used

By scanning the source files and static libraries, find and mark the target symbols that are not explicitly called as global automatic segment tags, enabling the compiler to recognize and add to the relocation table of the target file.

Benefits of technology

Ensure that symbols that are not explicitly called can take effect in the link-generated executable file, solving the problem that these symbols cannot be executed when static library links.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120029623A_ABST
    Figure CN120029623A_ABST
Patent Text Reader

Abstract

The invention provides a static library linking method, device and equipment, and the method comprises the steps: scanning a first target file obtained by compiling a source file, and a static library to be linked with the first target file, and obtaining scanning information; in the process of linking the first target file and the static library based on the scanning information, searching a target symbol marked by a global automatic segment in the static library; wherein the target symbol marked by the global automatic segment is not explicitly called; and carrying out symbol analysis and relocation processing on the first target file, the explicitly called symbols in the first target file and the target symbols, and linking to generate an executable file corresponding to the source file. According to the scheme, the problem that the target symbols which are not explicitly called in the static library linking process do not take effect can be solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a static library linking method, device and equipment. Background Art

[0002] Static library is a way to share code. Static library includes corresponding target files, and static library linking is the process of combining the functions, data, etc. in the corresponding target files in the static library with other modules in the application to generate an executable file.

[0003] At present, static libraries contain some symbols marked with special attributes, such as _attribute_((constructor)) attribute and _attribute_((destructor)) attribute. These symbols with special attributes are not explicitly called, so during the static library linking process, there will be no references to these symbols in the relocation information, resulting in these symbols being ineffective. Therefore, it is urgent to provide a static library linking method so that these symbols that are not explicitly called can be effective. Summary of the invention

[0004] The present application provides a static library linking method, device and equipment to solve the problem that target symbols that are not explicitly called during the static library linking process are ineffective.

[0005] In a first aspect, the present application provides a static library linking method, comprising:

[0006] Scanning a first target file obtained by compiling a source file and a static library to be linked with the first target file to obtain scanning information;

[0007] In the process of linking the first target file with the static library based on the scanning information, searching for target symbols marked by global automatic segments in the static library; wherein the target symbols marked by the global automatic segments are not explicitly called;

[0008] The first target file, the symbols explicitly called in the first target file and the target symbol are subjected to symbol resolution and relocation processing, and linked to generate an executable file corresponding to the source file.

[0009] In a possible implementation manner, performing symbol resolution and relocation processing on the first target file, the symbol explicitly called in the first target file, and the target symbol, and linking to generate an executable file corresponding to the source file, includes:

[0010] Performing symbol parsing and relocation processing on the first target file and the symbols explicitly called in the first target file to obtain first intermediate information after relocation;

[0011] Performing symbol parsing and relocation processing on the target symbol and the symbol called by the target symbol to obtain relocated second intermediate information;

[0012] The second intermediate information is inserted into the first intermediate information to obtain the executable file.

[0013] In a possible implementation manner, the target symbol includes a first target symbol of a first global automatic segment marker, and inserting the second intermediate information into the first intermediate information to obtain the executable file includes:

[0014] The second intermediate information is inserted before the entry function of the first intermediate information to obtain the executable file.

[0015] In a possible implementation manner, the target symbol includes a second target symbol of a second global automatic segment marker, and inserting the second intermediate information into the first intermediate information to obtain the executable file includes:

[0016] The second intermediate information is inserted into the entry function of the first intermediate information to obtain the executable file.

[0017] In a possible implementation manner, the target symbol includes a first target function of a first global automatic segment marker and / or a second target function of a second global automatic segment marker, and the method further includes:

[0018] Loading and executing environment initialization code, and initializing the execution environment based on the environment initialization code;

[0019] After the execution environment is initialized, the first target function is executed before the entry function of the first intermediate information, and / or the second target function is executed after the entry function.

[0020] In a possible implementation, the method further includes:

[0021] Performing global macro marking on the target symbol in the source code corresponding to the static library to be generated;

[0022] In the process of compiling the source code, the target symbol marked by the global macro is added to the global automatic segment in the corresponding target file to generate the static library.

[0023] In a possible implementation, the global macro includes a first global macro and / or a second global macro, and the target symbol marked by the global macro is added to the global automatic segment in the corresponding target file to generate the static library, including:

[0024] The first target symbol marked by the first global macro is added to the first global automatic segment in the corresponding target file, and / or the second target symbol marked by the second global macro is added to the second global automatic segment in the corresponding target file to generate the static library.

[0025] In a second aspect, the present application provides a linking device for a static library, comprising:

[0026] A scanning module, used for scanning a first target file obtained by compiling a source file and a static library to be linked with the first target file, to obtain scanning information;

[0027] A search module, used for searching the target symbol marked by the global automatic segment in the static library during the process of linking the first target file with the static library based on the scanning information; wherein the target symbol marked by the global automatic segment is not explicitly called;

[0028] The processing module is used to perform symbol resolution and relocation processing on the first target file, the symbols explicitly called in the first target file and the target symbols, and link to generate an executable file corresponding to the source file.

[0029] In a possible implementation manner, the processing module is specifically used to:

[0030] Performing symbol parsing and relocation processing on the first target file and the symbols explicitly called in the first target file to obtain first intermediate information after relocation;

[0031] Performing symbol parsing and relocation processing on the target symbol and the symbol called by the target symbol to obtain relocated second intermediate information;

[0032] The second intermediate information is inserted into the first intermediate information to obtain the executable file.

[0033] In a possible implementation manner, the target symbol includes a first target symbol marked by a first global automatic segment, and the processing module is specifically configured to:

[0034] The second intermediate information is inserted before the entry function of the first intermediate information to obtain the executable file.

[0035] In a possible implementation manner, the target symbol includes a second target symbol marked by a second global automatic segment, and the processing module is specifically configured to:

[0036] The second intermediate information is inserted into the entry function of the first intermediate information to obtain the executable file.

[0037] In a possible implementation manner, the target symbol includes a first target function of a first global automatic segment marker and / or a second target function of a second global automatic segment marker, and the processing module is further configured to:

[0038] Loading and executing environment initialization code, and initializing the execution environment based on the environment initialization code;

[0039] After the execution environment is initialized, the first target function is executed before the entry function of the first intermediate information, and / or the second target function is executed after the entry function.

[0040] In a possible implementation manner, the processing module is further configured to:

[0041] Performing global macro marking on the target symbol in the source code corresponding to the static library to be generated;

[0042] In the process of compiling the source code, the target symbol marked by the global macro is added to the global automatic segment in the corresponding target file to generate the static library.

[0043] In a possible implementation manner, the global macro includes a first global macro and / or a second global macro, and the processing module is further configured to:

[0044] The first target symbol marked by the first global macro is added to the first global automatic segment in the corresponding target file, and / or the second target symbol marked by the second global macro is added to the second global automatic segment in the corresponding target file to generate the static library.

[0045] In a third aspect, the present application provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the static library linking method as described in any one of the first aspects is implemented.

[0046] In a fourth aspect, the present application provides a non-transitory computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the static library linking method as described in any one of the first aspects is implemented.

[0047] The static library linking method, device and equipment provided in the present application first scans a first target file obtained by compiling a source file, and a static library to be linked with the first target file to obtain scanning information, and then searches for target symbols marked with global automatic segments in the static library during linking of the first target file and the static library based on the scanning information, wherein the target symbols marked with global automatic segments are not explicitly called. Since the target symbols that are not explicitly called are marked with global automatic segments, the target symbols can be added to the relocation table of the first target file by enabling the compiler to recognize the target symbols marked with global automatic segments. Therefore, when symbol resolution and relocation processing are performed on the first target file, the symbols explicitly called in the first target file, and the target symbols, the target symbols can take effect in the executable file generated by the link. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] In order to more clearly illustrate the technical solutions in the present application or the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0049] Figure 1 A flowchart of a static library linking method provided in an embodiment of the present application;

[0050] Figure 2 A static library processing flow chart provided for an embodiment of the present application;

[0051] Figure 3 A flowchart of symbol parsing and relocation processing provided in an embodiment of the present application;

[0052] Figure 4 A schematic diagram of the structure of a linking device for a static library provided in an embodiment of the present application;

[0053] Figure 5 A schematic diagram of the physical structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0054] In order to make the purpose, technical solutions and advantages of this application clearer, the technical solutions in this application will be clearly and completely described below in conjunction with the drawings in this application. Obviously, the described embodiments are part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0055] First, the basic concepts involved in this application are introduced.

[0056] Linking: Linking is a step in program compilation. Program compilation generally requires several steps: preprocessing, compilation, assembly and linking. Linking refers to the process of "gluing" all compiled target files to the corresponding library files to generate an executable file.

[0057] Static library: Static library is a way to share program code. When there are some common program codes that need to be used repeatedly, these codes can be compiled into "library" files; in the linking step, the linker will obtain the required code from the library file and copy it to the generated executable file. This library file is called a static library. The characteristic of static library linking is that the executable file contains a complete copy of the library code, so the executable file generated after linking does not need to rely on other library files to execute. The disadvantage is that there will be multiple redundant copies if it is used multiple times.

[0058] There are generally two ways to share public reusable code, namely compiling the program code to generate static library files or dynamic library files. When the executable program only uses a part of the library interface, or only one process program uses the library file, or the static library has an advantage over the dynamic library in terms of total memory usage, static library linking is usually chosen. This is because during the static library linking process, unused target files or uncalled function symbols will be ignored and not linked, thereby reducing the size of the executable file.

[0059] A static library can include multiple .o target files. In static library linking, the smallest target ignored by the linker by default is the .o target file, that is, the smallest unit used in the static library is the .o target file. In a static library, if all the symbols in a .o target file are not used, the .o target file will not be linked. In another case, you can use link options to link in sections. For example, in Linux (an operating system), you can use function segments (-ffunction-sections), data segments (-fdata-sections), etc. during the compilation phase, so that each function and data are linked as the smallest unit, and use -gc-sections to remove unused segments during the link phase to reduce memory.

[0060] However, there are also problems in the use of static libraries due to the above characteristics. For example, under Linux, the GNU Compiler Collection (GCC) defines some special attributes, such as __attribute__((constructor)) and __attribute__((destructor)). These attributes can be applied to functions. Although there is no explicit call in the executable program, the above-marked functions can be automatically executed before and after the program executes the entry function (the default is the main function). Generally, this marking function is applied in the application code. For example, if there are multiple modules in the application code and it supports trimming, without using compilation macros, the module registration function is marked with the above attributes so that the module initialization function is registered and the module is registered in the global module list only when the module is not trimmed. When the executable program is started, if there is a corresponding module in the global module list, the initialization and other actions of the corresponding module can be called.

[0061] If the static library contains functions with these attributes, the functions marked with the above attributes will not be executed during the linking of the executable program. This is because the functions marked with the above attributes will not be added to the relocation table of the corresponding target file during the linking of the static library, so they cannot take effect during the linking, which in turn affects a series of logical codes that depend on the operations in these functions.

[0062] There are currently several solutions to the problem that the functions marked with the _attribute_((constructor)) and _attribute_((destructor)) attributes in static libraries are not effective:

[0063] The first one is to use dynamic library linking. However, in some cases, static library linking has more advantages than dynamic library linking, such as smaller total memory usage. Therefore, directly using dynamic library linking may lead to an increase in total memory usage, which goes against the original intention of using static libraries.

[0064] The second method is to use the --whole-archive option when linking a static library, explicitly telling the linker to include all symbols of the static library. --whole-archive is a parameter of the GCC compiler, which is used to specify that the linker should include all the object files in a library file when linking, rather than only the object files referenced in the library file. However, this processing method will cause some unused symbols to be included, resulting in excessive memory usage and failure to take advantage of static library linking.

[0065] The third method is to explicitly call function symbols in the executable file. This method is not flexible to use, and when there are third-party libraries, it is difficult to explicitly call all symbols.

[0066] The fourth method is to provide a .o target file to the executable program. This processing method is very inflexible when multiple independent modules are finally integrated together. It cannot be controlled when there are third-party libraries. If the executable program is linked without using the relevant link options, all symbols of the entire target file will be included.

[0067] Based on this, an embodiment of the present application provides a static library linking method to solve the problem that functions that are not explicitly called cannot take effect during the static library linking process.

[0068] Figure 1 A flowchart of a static library linking method provided in an embodiment of the present application is shown in FIG. Figure 1 As shown, the method includes:

[0069] S11, scanning a first target file obtained by compiling a source file and a static library to be linked with the first target file to obtain scanning information.

[0070] In an embodiment of the present application, the first target file is a target file obtained by compiling a source file, and the first target file may contain references to other symbols, and the referenced symbols may be defined in a static library, so a static library link is required.

[0071] Initially, the first target file and the static library to be linked with the first target file are scanned. The number of static libraries to be linked with the first target file may be one or more, which is not limited in the embodiment of the present application. In the following embodiments, one is used as an example for introduction.

[0072] A static library may include one or more target files, wherein the first target file that needs to be linked and used may be all or part of the target files in the static library, or may be part of the functions or data in the target files in the static library, which is not limited in this embodiment of the present application.

[0073] Scanning the first target file and the static library to be linked with the first target file can obtain scanning information. For the first target file and the target files in the static library, each target file has a corresponding code segment, data segment, and bss segment (Block Started by Symbol, which refers to a memory area used to store uninitialized or initialized to 0 global variables and static variables in the program), and the scanning information may include the length, attributes, location and other information of each segment in each scanned target file.

[0074] S12, in the process of linking the first target file with the static library based on the scanning information, searching for target symbols marked by global automatic segment in the static library; wherein the target symbols marked by global automatic segment are not explicitly called.

[0075] Based on the scanning information, the first target file can be linked to the static library. For example, based on the length, attributes, location and other information of each segment in each target file, the code segment, data segment, bss segment, etc. can be merged and placed in the memory according to certain rules to realize address and space allocation.

[0076] In the process of linking the first target file with the static library based on the scanning information, the linker searches for the target symbol marked by the global automatic segment in the static library. In an embodiment of the present application, the target symbol marked by the global automatic segment is a symbol that is not explicitly called. Usually, in a static library, the symbol that is not explicitly called includes a symbol marked by the _attribute_((constructor)) attribute, and a symbol marked by the _attribute_((destructor)) attribute. Since this type of symbol is not explicitly called, it will not be added to the relocation table of the corresponding target file during the linking process and cannot be executed. Therefore, in an embodiment of the present application, this type of symbol that is not explicitly called is marked by the global automatic segment.

[0077] Such target symbols that are not explicitly called are marked by the global automatic section so that the target symbols can be added to the relocation table of the first target file.

[0078] S13, performing symbol parsing and relocation processing on the first target file, the symbols explicitly called in the first target file, and the target symbols, and linking to generate an executable file corresponding to the source file.

[0079] In the process of symbol resolution and relocation, the linker first traverses the relocation table of each target file, associates the referenced symbols with the defined symbols, and finds the actual address of each symbol. Then the linker performs relocation based on the symbol resolution structure and adjusts the relative addresses in each target file to absolute addresses, so that each target file can run correctly in memory.

[0080] In an embodiment of the present application, the first target file may include symbols that are explicitly called and symbols that are not explicitly called, and the symbols that are not explicitly called are target symbols. The first target file, the symbols that are explicitly called in the first target file, and the target symbols are subjected to symbol resolution and relocation processing. Since the target symbol is marked with a global automatic segment, the compiler can identify the target symbol marked with a global automatic segment, so the target symbol can be added to the relocation table of the first target file, and symbol resolution and relocation can be performed correctly, thereby linking and generating an executable file corresponding to the source file.

[0081] Based on any of the above embodiments, the scheme of the embodiments of the present application is further introduced below in conjunction with the accompanying drawings.

[0082] First combine Figure 2 This section describes the processing of static libraries.

[0083] Figure 2 The static library processing flow chart provided in the embodiment of the present application is as follows: Figure 2 As shown, including:

[0084] S21, performing global macro marking on the target symbols in the source code corresponding to the static library to be generated.

[0085] After obtaining the source code corresponding to the static library to be generated, the target symbol in the source code is globally macro-marked. The target symbol in the source code is a symbol that is not explicitly called. In some embodiments, the target symbol in the source code includes a symbol marked by the _attribute_((constructor())) attribute and / or a symbol marked by the _attribute_((destructor)) attribute.

[0086] An example of a global macro tag is as follows:

[0087] #define GLOBAL_AUTO_SYM_attribute_((section("global_auto_sym")))

[0088] This macro definition is used to tell the compiler to place the symbols using this definition in the global automatic segment (global_auto_sym segment).

[0089] In a possible implementation, the global macro tag includes a first global macro tag and / or a second global macro tag. That is, the first target symbol in the source code corresponding to the static library is marked with the first global macro tag, and the first target symbol may include, for example, a symbol marked with the _attribute_((constructor())) attribute; the second target symbol in the source code corresponding to the static library is marked with the second global macro tag, and the second target symbol may include, for example, a symbol marked with the _attribute_((destructor)) attribute.

[0090] S22, in the process of compiling the source code, adding the target symbol marked by the global macro to the global automatic segment in the corresponding target file to generate a static library.

[0091] After macro definition, during the compilation of source code, the target symbol marked by the global macro can be added to the global automatic segment in the corresponding target file based on the macro definition to generate a static library. In the generated static library, the symbols that are not explicitly called are the target symbols marked by the global automatic segment.

[0092] In a possible implementation, the global macro mark includes a first global macro mark and / or a second global macro mark. Therefore, the first target symbol marked by the first global macro can be added to the first global automatic segment in the corresponding target file, and / or the second target symbol marked by the second global macro can be added to the second global automatic segment in the corresponding target file to generate a static library. Among them, the first target symbol in the first global automatic segment can include a first target function and / or a first variable name, and the second target symbol in the second global automatic segment can include a second target function and / or a second variable name. The first target function is used to be executed before the entry function, and the second target function is used to be executed after the entry function.

[0093] After generating the static library, you can link the static library to generate the corresponding executable file. Figure 3 This section introduces the process.

[0094] Figure 3 A flowchart of symbol parsing and relocation processing provided in an embodiment of the present application, such as Figure 3 As shown, including:

[0095] S31, performing symbol parsing and relocation processing on the first target file and the symbols explicitly called in the first target file to obtain first intermediate information after relocation.

[0096] Since the target symbol is a symbol that is not explicitly called, the first target file and the target symbol are respectively subjected to symbol resolution and relocation processing.

[0097] For the first target file, the process of performing symbol resolution and relocation processing on the first target file and the symbols explicitly called in the first target file is to search the global symbol table for the definition of the symbols explicitly called in the first target file and associate them with the entries in the global symbol table, thereby linking the symbols explicitly called in the first target file to the correct address through relocation, and finally obtaining the first intermediate information after relocation.

[0098] S32, performing symbol parsing and relocation processing on the target symbol and the symbol called by the target symbol to obtain the relocated second intermediate information.

[0099] For the target symbol, the target symbol may also have a calling symbol. The process of symbol resolution and relocation for the target symbol and the symbol called by the target symbol is to find the definition of the symbol called by the target symbol in the global symbol table and associate it with the entry in the global symbol table, so as to link the symbol called by the target symbol to the correct address through relocation, and finally obtain the second intermediate information after relocation.

[0100] S33, inserting the second intermediate information into the first intermediate information to obtain an executable file.

[0101] After obtaining the first intermediate information and the second intermediate information, the second intermediate information needs to be inserted into a suitable position in the first intermediate information, so as to obtain an executable file.

[0102] In a possible implementation, the target symbol includes the first target symbol of the first global automatic segment marker, so the second intermediate information is the intermediate information obtained after the symbol resolution and relocation processing is performed on the first target symbol and the symbol called by the first target symbol. Then, the second intermediate information is inserted before the entry function of the first intermediate information to obtain an executable file.

[0103] In a possible implementation, the target symbol includes a second target symbol of a second global automatic segment tag, so the second intermediate information is intermediate information obtained after symbol resolution and relocation processing are performed on the second target symbol and the symbol called by the second target symbol. Then, the second intermediate information is inserted after the entry function of the first intermediate information to obtain an executable file.

[0104] After obtaining the executable file, the executable program can be executed. Specifically, firstly, the environment initialization code is loaded and executed, and the environment initialization code is used to initialize the execution environment.

[0105] After the execution environment is initialized based on the environment initialization code, the entry function of the first intermediate information and the function included in the target symbol can be executed.

[0106] In some embodiments, the target symbol includes a first target function of a first global automatic segment tag and / or a second target function of a second global automatic segment tag, so that after the execution environment is initialized, the first target function is executed before the entry function of the first intermediate information, and / or the second target function is executed after the entry function.

[0107] The linking method of the static library provided by the embodiment of the present application first scans the first target file obtained by compiling the source file, and the static library to be linked with the first target file, obtains the scanning information, and then in the process of linking the first target file with the static library based on the scanning information, searches for the target symbol marked by the global automatic segment in the static library, wherein the target symbol marked by the global automatic segment is not explicitly called, because the target symbol not explicitly called is marked by the global automatic segment, by making the compiler recognize the target symbol marked by the global automatic segment, so that the target symbol can be added to the relocation table of the first target file, and the symbols and target symbols explicitly called in the first target file and the first target file are subjected to symbol parsing and relocation processing, and the target symbol can also take effect in the executable file generated by the link. The scheme of the embodiment of the present application can solve the problem that the target symbol marked by the automatic execution is not effective when the static library is linked without including all the symbols of the static library, without explicitly calling the target symbol marked by the automatic execution, and without providing an entire .o target file, the target symbol marked by the global automatic segment can be automatically found when linking, thereby including this part in the executable file.

[0108] The linking device of the static library provided in the present application is described below. The linking device of the static library described below and the linking method of the static library described above can be referred to each other.

[0109] Figure 4 A schematic diagram of the structure of the linking device of the static library provided in the embodiment of the present application, such as Figure 4 As shown, the device comprises:

[0110] A scanning module 41 is used to scan a first target file obtained by compiling a source file and a static library to be linked with the first target file to obtain scanning information;

[0111] A search module 42 is used to search for a target symbol marked by a global automatic segment in the static library during linking of the first target file with the static library based on the scanning information; wherein the target symbol marked by the global automatic segment is not explicitly called;

[0112] The processing module 43 is used to perform symbol resolution and relocation processing on the first target file, the symbols explicitly called in the first target file and the target symbols, and link to generate an executable file corresponding to the source file.

[0113] In a possible implementation manner, the processing module 43 is specifically configured to:

[0114] Performing symbol parsing and relocation processing on the first target file and the symbols explicitly called in the first target file to obtain first intermediate information after relocation;

[0115] Performing symbol parsing and relocation processing on the target symbol and the symbol called by the target symbol to obtain relocated second intermediate information;

[0116] The second intermediate information is inserted into the first intermediate information to obtain the executable file.

[0117] In a possible implementation manner, the target symbol includes a first target symbol marked by a first global automatic segment, and the processing module 43 is specifically configured to:

[0118] The second intermediate information is inserted before the entry function of the first intermediate information to obtain the executable file.

[0119] In a possible implementation manner, the target symbol includes a second target symbol marked by a second global automatic segment, and the processing module 43 is specifically configured to:

[0120] The second intermediate information is inserted into the entry function of the first intermediate information to obtain the executable file.

[0121] In a possible implementation manner, the target symbol includes a first target function of a first global automatic segment marker and / or a second target function of a second global automatic segment marker, and the processing module 43 is further configured to:

[0122] Loading and executing environment initialization code, and initializing the execution environment based on the environment initialization code;

[0123] After the execution environment is initialized, the first target function is executed before the entry function of the first intermediate information, and / or the second target function is executed after the entry function.

[0124] In a possible implementation manner, the processing module 43 is further configured to:

[0125] Performing global macro marking on the target symbol in the source code corresponding to the static library to be generated;

[0126] In the process of compiling the source code, the target symbol marked by the global macro is added to the global automatic segment in the corresponding target file to generate the static library.

[0127] In a possible implementation manner, the global macro includes a first global macro and / or a second global macro, and the processing module 43 is further configured to:

[0128] The first target symbol marked by the first global macro is added to the first global automatic segment in the corresponding target file, and / or the second target symbol marked by the second global macro is added to the second global automatic segment in the corresponding target file to generate the static library.

[0129] Figure 5 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 5 As shown, the electronic device may include: a processor 510, a communication interface 520, a memory 530 and a communication bus 540, wherein the processor 510, the communication interface 520 and the memory 530 communicate with each other through the communication bus 540. The processor 510 may call the logic instructions in the memory 530 to execute the static library linking method, the method comprising: scanning the first target file obtained by compiling the source file and the static library to be linked with the first target file to obtain scanning information; in the process of linking the first target file with the static library based on the scanning information, searching for the target symbol marked by the global automatic segment in the static library; wherein the target symbol marked by the global automatic segment is not explicitly called; performing symbol resolution and relocation processing on the first target file, the symbol explicitly called in the first target file and the target symbol, and linking to generate an executable file corresponding to the source file.

[0130] In addition, the logic instructions in the above-mentioned memory 530 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art, and the computer software product is stored in a storage medium, including several instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0131] On the other hand, the present application also provides a computer program product, which includes a computer program, which can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the static library linking method provided by the above methods, the method including: scanning a first target file obtained by compiling a source file, and a static library to be linked with the first target file, to obtain scanning information; in the process of linking the first target file with the static library based on the scanning information, searching for target symbols marked by global automatic segments in the static library; wherein the target symbols marked by the global automatic segments are not explicitly called; performing symbol resolution and relocation processing on the first target file, the symbols explicitly called in the first target file, and the target symbol, and linking to generate an executable file corresponding to the source file.

[0132] On the other hand, the present application also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements a static library linking method provided by the above-mentioned methods, the method comprising: scanning a first target file obtained by compiling a source file, and a static library to be linked with the first target file, to obtain scanning information; in the process of linking the first target file with the static library based on the scanning information, searching for target symbols marked by global automatic segments in the static library; wherein the target symbols marked by the global automatic segments are not explicitly called; performing symbol resolution and relocation processing on the first target file, the symbols explicitly called in the first target file, and the target symbol, and linking to generate an executable file corresponding to the source file.

[0133] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0134] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0135] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.

Claims

1. A static library linking method, It is characterized in that include: Scanning a first target file obtained by compiling a source file and a static library to be linked with the first target file to obtain scanning information; In the process of linking the first target file with the static library based on the scanning information, searching for target symbols marked by global automatic segments in the static library; wherein the target symbols marked by the global automatic segments are not explicitly called; The first target file, the symbols explicitly called in the first target file and the target symbol are subjected to symbol resolution and relocation processing, and linked to generate an executable file corresponding to the source file.

2. The method according to claim 1, It is characterized in that The performing symbol parsing and relocation processing on the first target file, the symbols explicitly called in the first target file, and the target symbol, and linking to generate an executable file corresponding to the source file, includes: Performing symbol parsing and relocation processing on the first target file and the symbols explicitly called in the first target file to obtain first intermediate information after relocation; Performing symbol parsing and relocation processing on the target symbol and the symbol called by the target symbol to obtain relocated second intermediate information; The second intermediate information is inserted into the first intermediate information to obtain the executable file.

3. The method according to claim 2, It is characterized in that The target symbol includes a first target symbol marked by a first global automatic segment, and inserting the second intermediate information into the first intermediate information to obtain the executable file includes: The second intermediate information is inserted before the entry function of the first intermediate information to obtain the executable file.

4. The method according to claim 2, It is characterized in that The target symbol includes a second target symbol marked by a second global automatic segment, and the inserting the second intermediate information into the first intermediate information to obtain the executable file includes: The second intermediate information is inserted into the entry function of the first intermediate information to obtain the executable file.

5. The method according to any one of claims 2 to 4, It is characterized in that The target symbol includes a first target function of a first global automatic segment marker and / or a second target function of a second global automatic segment marker, and the method further includes: Loading and executing environment initialization code, and initializing the execution environment based on the environment initialization code; After the execution environment is initialized, the first target function is executed before the entry function of the first intermediate information, and / or the second target function is executed after the entry function.

6. The method according to any one of claims 1 to 4, It is characterized in that The method further comprises: Performing global macro marking on the target symbol in the source code corresponding to the static library to be generated; In the process of compiling the source code, the target symbol marked by the global macro is added to the global automatic segment in the corresponding target file to generate the static library.

7. The method according to claim 6, It is characterized in that The global macro includes a first global macro and / or a second global macro, and the target symbol marked by the global macro is added to the global automatic segment in the corresponding target file to generate the static library, including: The first target symbol marked by the first global macro is added to the first global automatic segment in the corresponding target file, and / or the second target symbol marked by the second global macro is added to the second global automatic segment in the corresponding target file to generate the static library.

8. A linking device for a static library, It is characterized in that include: A scanning module, used for scanning a first target file obtained by compiling a source file and a static library to be linked with the first target file, to obtain scanning information; A search module, used for searching the target symbol marked by the global automatic segment in the static library during the process of linking the first target file with the static library based on the scanning information; wherein the target symbol marked by the global automatic segment is not explicitly called; The processing module is used to perform symbol resolution and relocation processing on the first target file, the symbols explicitly called in the first target file and the target symbols, and link to generate an executable file corresponding to the source file.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, It is characterized in that When the processor executes the program, the static library linking method according to any one of claims 1 to 7 is implemented.

10. A non-transitory computer-readable storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed by a processor, the static library linking method according to any one of claims 1 to 7 is implemented.