System source code automatic cutting method and device, equipment, storage medium and product

By obtaining the compilation option key-value pairs and Makefile files of the Linux system, extracting dependencies and function call relationships, and automatically trimming the Linux kernel source code, the problem of coarse granularity in existing technologies is solved, achieving fine-grained kernel trimming and security improvement.

CN119292606APending Publication Date: 2025-01-10DONGFENG MOTOR GRP
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411360232.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-27
Publication Date
2025-01-10

AI Technical Summary

Technical Problem

Existing Linux system source code trimming methods cannot meet the fine-grained customization needs of embedded devices. They are too coarse-grained and cannot effectively reduce the kernel size on devices with limited hardware resources.

Method used

By obtaining the compilation option key-value pairs in the system configuration file and the Makefile, the dependencies are extracted, the target source code set for the customized kernel is selected, and automatic pruning is performed based on the function call relationship to remove redundant functions.

Benefits of technology

It achieves fine-grained trimming of the Linux kernel source code, reducing kernel size, improving compilation speed and security, and meeting the customization needs of embedded devices.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119292606A_ABST
    Figure CN119292606A_ABST
Patent Text Reader

Abstract

The invention discloses a system source code automatic clipping method and device, equipment, a storage medium and a product, and relates to the technical field of system optimizing.The method comprises the steps that a compiling option key value pair in a system configuration file and a Makefile in a system source code are obtained; according to the compiling option key value pair, extracting a dependency relationship between the compiling option name and the Makefile from the Makefile; selecting a target source code set corresponding to the customized kernel from the system source codes according to the dependency relationship; and determining an inter-function calling relationship in the target source code set, and automatically cutting the target source code set through the inter-function calling relationship. According to the method and the device, the native module-level clipping is reproduced logically, so that functions which are not in the kernel execution path are removed from the Linux kernel source code obtained after clipping, the size of the Linux kernel is reduced, the compiling speed is increased, and the safety of the Linux kernel is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of system optimization, and in particular to a method, device, equipment, storage medium and product for automatic system source code trimming. Background Art

[0002] Embedded operating systems are the infrastructure for automotive intelligence. The Linux system has become one of the mainstream choices for embedded operating systems due to its open source code, good usability and scalability. The current automotive industry is highly competitive, and all manufacturers want to reduce hardware costs as much as possible without affecting performance. However, due to the large scale of the Linux system, if it is to be used on embedded devices with limited hardware resources, the source code of the Linux system needs to be tailored accordingly.

[0003] Since the Linux system adopts a modular design concept and stores the codes of different modules in different source code directories, it has a good foundation for tailoring. However, the basic unit of the tailoring mechanism in the current Linux system is the functional module, which has a coarse granularity and cannot fully meet the refined customization needs of embedded systems. Summary of the invention

[0004] The main purpose of this application is to provide a system source code automatic tailoring method, device, equipment, storage medium and product, aiming to solve the technical problem that the existing Linux system source code tailoring method cannot fully meet the embedded refined customization requirements.

[0005] To achieve the above object, the present application provides a method for automatically trimming system source code, the method comprising the following steps:

[0006] Get the compilation option key-value pairs in the system configuration file and the Makefile file in the system source code;

[0007] Extracting the dependency relationship between the compile option name and the Makefile file from the Makefile file according to the compile option key-value pair;

[0008] Selecting a target source code set corresponding to a customized kernel from the system source code according to the dependency relationship, wherein the customized kernel is constructed based on user requirements;

[0009] Determine the calling relationship between functions in the target source code set, and automatically trim the target source code set according to the calling relationship between functions.

[0010] In one embodiment, the step of obtaining the compile option key-value pairs in the system configuration file and the Makefile file in the system source code includes:

[0011] After analyzing the system configuration file line by line, a plurality of compile option names and compile option values ​​corresponding to the compile option names are obtained, and the compile option names and the compile option values ​​are combined into a compile option key-value pair;

[0012] The root directory and subdirectories of the system source code are scanned and searched to obtain the Makefile file in the system source code.

[0013] In one embodiment, the step of extracting the dependency relationship between the compile option name and the Makefile file from the Makefile file according to the compile option key-value pair comprises:

[0014] The Makefile file is caused to perform a preprocessing operation according to the compile option key-value pair to obtain a preprocessed Makefile file, wherein the preprocessing operation includes a comment removal operation, a variable parsing operation, and a conditional statement judgment operation;

[0015] Extract the dependency relationship between the compile option name and the Makefile file from the preprocessed Makefile file.

[0016] In one embodiment, the step of selecting a target source code set corresponding to the customized kernel from the system source code according to the dependency relationship includes:

[0017] Traversing all the compile option names in the compile option key-value pairs, and determining a list of source code file path names corresponding to all the compile option names in the compile option key-value pairs based on the dependency relationship;

[0018] A target source code set corresponding to the customized kernel is selected from the system source code according to the source code file path name list.

[0019] In one embodiment, the step of determining the calling relationship between functions in the target source code set includes:

[0020] Extracting function contents in the target source code set according to a function definition start line and a function definition end line in the target source code set;

[0021] The function content is analyzed according to the function call grammar rules to obtain the function call relationship between the functions in the target source code set.

[0022] In one embodiment, the step of automatically trimming the target source code set according to the calling relationship between functions includes:

[0023] Detecting and screening the target source code set through the calling relationship between the functions to obtain redundant functions, where the redundant functions are functions that are not called by other functions;

[0024] The location information of the redundant function in the target source code set is determined based on the starting line number and the ending line number of the redundant function, and the target source code set is automatically trimmed according to the location information.

[0025] In addition, to achieve the above purpose, the present application also proposes a system source code automatic trimming device, the system source code automatic trimming device comprising:

[0026] The data acquisition module is used to obtain the compilation option key-value pairs in the system configuration file and the Makefile file in the system source code;

[0027] A relationship extraction module, used for extracting the dependency relationship between the compilation option name and the Makefile file from the Makefile file according to the compilation option key-value pair;

[0028] A data selection module, used for selecting a target source code set corresponding to a customized kernel from the system source code according to the dependency relationship, wherein the customized kernel is constructed based on user requirements;

[0029] The automatic trimming module is used to determine the calling relationship between functions in the target source code set, and automatically trim the target source code set according to the calling relationship between functions.

[0030] In addition, to achieve the above-mentioned purpose, the present application also proposes a system source code automatic trimming device, which includes: a memory, a processor, and a system source code automatic trimming program stored in the memory and executable on the processor, wherein the system source code automatic trimming program is configured to implement the steps of the system source code automatic trimming method described above.

[0031] In addition, to achieve the above-mentioned purpose, the present application also proposes a storage medium, which is a computer-readable storage medium, and on which is stored a system source code automatic trimming program, and when the system source code automatic trimming program is executed by a processor, the steps of the system source code automatic trimming method described above are implemented.

[0032] In addition, to achieve the above objectives, the present invention also provides a computer program product, which includes a system source code automatic trimming program, and when the system source code automatic trimming program is executed by a processor, it implements the steps of the system source code automatic trimming method described above.

[0033] The present application obtains the compile option key-value pairs in the system configuration file and the Makefile file in the system source code; extracts the dependency relationship between the compile option name and the Makefile file from the Makefile file according to the compile option key-value pairs; selects the target source code set corresponding to the customized kernel from the system source code according to the dependency relationship, and the customized kernel is constructed based on user needs; determines the function call relationship in the target source code set, and automatically trims the target source code set through the function call relationship. Compared with the traditional system source code trimming method, since the above method of the present application establishes the target source code set through the system configuration file and the dependency relationship between the compile option name and the system source code, the original module-level trimming is logically reproduced, and the subsequent function call relationship analysis is only performed within the target source code set, so that the Linux kernel source code obtained after trimming removes the functions that are not in the kernel execution path, which not only reduces the size of the Linux kernel, speeds up the compilation speed, but also increases the security of the Linux kernel, thereby meeting the user's embedded refined customization needs for the Linux system. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] Figure 1 A schematic diagram of the structure of a system source code automatic trimming device for a hardware operating environment involved in an embodiment of the present application;

[0035] Figure 2 This is a flow chart of the first embodiment of the system source code automatic trimming method of the present application;

[0036] Figure 3 This is a flow chart of the second embodiment of the system source code automatic trimming method of the present application;

[0037] Figure 4 This is a flow chart of the third embodiment of the system source code automatic trimming method of the present application;

[0038] Figure 5 This is a structural block diagram of the first embodiment of the system source code automatic trimming device of the present application.

[0039] The realization of the purpose, functional features and advantages of this application will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION

[0040] It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0041] Reference Figure 1 , Figure 1 This is a schematic diagram of the structure of a system source code automatic trimming device for the hardware operating environment involved in the embodiment of the present application.

[0042] like Figure 1 As shown, the system source code automatic trimming device may include: a processor 1001, such as a central processing unit (CPU), a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. Among them, the communication bus 1002 is used to realize the connection and communication between these components. The user interface 1003 may include a display screen (Display), an input unit such as a keyboard (Keyboard), and the optional user interface 1003 may also include a standard wired interface and a wireless interface. The network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a wireless fidelity (Wireless-Fidelity, Wi-Fi) interface). The memory 1005 may be a high-speed random access memory (Random Access Memory, RAM), or a stable non-volatile memory (Non-Volatile Memory, NVM), such as a disk storage. The memory 1005 may also be a storage device independent of the aforementioned processor 1001.

[0043] Those skilled in the art will understand that Figure 1 The structure shown in the figure does not constitute a limitation on the system source code automatic trimming device, and may include more or less components than those shown in the figure, or combine certain components, or arrange the components differently.

[0044] like Figure 1 As shown, the memory 1005 as a storage medium may include an operating system, a network communication module, a user interface module, and a system source code automatic trimming program.

[0045] exist Figure 1 In the system source code automatic tailoring device shown, the network interface 1004 is mainly used for data communication with the network server; the user interface 1003 is mainly used for data interaction with the user; the processor 1001 and the memory 1005 in the system source code automatic tailoring device of the present application can be set in the system source code automatic tailoring device, and the system source code automatic tailoring device calls the system source code automatic tailoring program stored in the memory 1005 through the processor 1001, and executes the system source code automatic tailoring method provided in the embodiment of the present application.

[0046] The present application embodiment provides a method for automatically cutting system source code. Figure 2 , Figure 2 This is a flow chart of the first embodiment of the system source code automatic trimming method of the present application.

[0047] In this embodiment, the system source code automatic trimming method includes the following steps:

[0048] Step S10: Obtain the compile option key-value pairs in the system configuration file and the Makefile file in the system source code.

[0049] It should be noted that the execution subject of the method of this embodiment can be a terminal device with data extraction, data processing and program running functions, such as a smart phone, a computer, etc., or an electronic device with the same or similar functions, such as the above-mentioned system source code automatic tailoring device. The following takes the system source code automatic tailoring device (hereinafter referred to as the tailoring device) as an example to illustrate this embodiment and the following embodiments.

[0050] It is understandable that the above-mentioned system configuration file refers to the kernel configuration file in the Linux system. This file can be generated by the user through the make tool when compiling the Linux kernel. The system configuration file contains various options and parameters when the kernel is compiled. Specifically, the .config file is generated by kernel configuration tools (such as make menuconfig, makexconfig, make oldconfig, etc.). These tools provide a graphical interface or text interface that allows users to select the kernel functions they need. Once the user has completed the configuration selection, the configuration tool will generate or update the .config file to reflect these selections. The above-mentioned system source code refers to the kernel source code in the Linux system.

[0051] It should be understood that the Makefile file is the core configuration file in the Linux kernel compilation process, which is responsible for instructing how to compile and link the kernel source code to generate the final kernel image (such as vmlinux) or module (.ko file).

[0052] In the specific implementation, the specific file set corresponding to a compilation option changes with the compilation option value, so in order to obtain the accurate dependency relationship between compilation options and files, the compilation option value in the .config file will be used during the Makefile file analysis process. The compilation options and values ​​saved in it are extracted and saved to a map named config_y_map<string,string> The key of the map is the compile option name, and the value is the compile option value. First, read the .config file, analyze it line by line, and determine whether the first character of a line is "#". If so, skip the line. If not, save the string between "CONFIG_" and "=" as the compile option name, and the string after "=" as the compile option value, and then save the two as a key-value pair in the map.

[0053] Step S20: extracting the dependency relationship between the compile option name and the Makefile file from the Makefile file according to the compile option key-value pair.

[0054] It should be understood that after obtaining the compile option key-value pairs in the .config file, the Makefile file in the Linux system source code can be processed and analyzed, so as to extract the dependency relationship between the compile options and the files.

[0055] Step S30: selecting a target source code set corresponding to a customized kernel from the system source code according to the dependency relationship, wherein the customized kernel is constructed based on user requirements.

[0056] In the specific implementation, the target source code set can be extracted by combining the compilation options extracted from the Makefile file with the dependencies between files. The dependencies between the compilation option names and files will be stored in compile_file_map, compile_option_y_map and compile_option_n_map. Among them, compile_file_map is a map <string,list <string>> type data structure, whose key is the compile option name and value is a list of C source code files corresponding to the compile option name. The data types of compile_option_y_map and compile_option_n_map are the same as compile_file_map, which store the compile options and their dependent files that are affected by the compile option values. This is because the existence of conditional statements in the Makefile statement will cause the set of files corresponding to the compile option parameters to change with the compile option values. compile_option_y_map stores the file list corresponding to the compile option parameter in the conditional statement when the compile option parameter is y, and compile_option_n_map stores the file list corresponding to the compile option parameter in the conditional statement when the compile option parameter is n or empty.

[0057] Step S40: determining the calling relationship between functions in the target source code set, and automatically trimming the target source code set according to the calling relationship between functions.

[0058] It should be noted that the present embodiment includes three parts: a Makefile file analysis module, a C source code file analysis module and an automatic trimming module. The Makefile file analysis module takes the Linux kernel source code to be trimmed and the configured .config file as input, analyzes and processes the Makefile file in the source code to extract the compilation option name and the dependency relationship between files. The automatic trimming module determines the source code range required for the customized kernel according to the compilation option name and the dependency relationship between files and a specific configuration file. Then, the C source code file analysis module extracts the calling relationship between functions from this file set. Finally, the automatic trimming module detects and deletes the functions within the source code range required for the customized kernel according to the calling relationship between functions, and finally saves them in the physical source file, i.e., obtains the trimmed Linux kernel source code.

[0059] More specifically, after completing module-level trimming, the source code range required to build the custom kernel is obtained and saved in needed_files_list. Before performing function-level trimming, the C source code analysis module needs to be used to extract the function call relationships in the retained source code file range. The C source code analysis module saves the functions defined in each file in needed_files_list in a map called file_function_map. <string,map<string,MFunction> > type data structure, the map key is the file path name, the value is the function name and function information structure composed of function_map, it will save all the functions defined in a file, the map key is the function name, the value is the function definition related information. MFunction data structure saves a function including function prototype, calling function list and function called list and other related information.

[0060] This embodiment obtains the compile option key-value pairs in the system configuration file and the Makefile file in the system source code; extracts the dependency relationship between the compile option name and the Makefile file from the Makefile file according to the compile option key-value pairs; selects the target source code set corresponding to the customized kernel from the system source code according to the dependency relationship, and the customized kernel is constructed based on user needs; determines the function call relationship in the target source code set, and automatically trims the target source code set through the function call relationship. Compared with the traditional system source code trimming method, since the above method of this embodiment logically reproduces the native module-level trimming after establishing the target source code set through the system configuration file and the dependency relationship between the compile option name and the system source code, the subsequent function call relationship analysis will only be performed within the scope of the target source code set, so that the Linux kernel source code obtained after trimming removes the functions that are not in the kernel execution path, which not only reduces the size of the Linux kernel and speeds up the compilation speed, but also increases the security of the Linux kernel, thereby meeting the user's embedded refined customization needs for the Linux system.

[0061] refer to Figure 3 , Figure 3 This is a flow chart of the second embodiment of the system source code automatic trimming method of the present application.

[0062] In a feasible implementation manner, the step S10 may include:

[0063] Step S101: analyzing the system configuration file line by line to obtain a number of compilation option names and compilation option values ​​corresponding to the compilation option names, and combining the compilation option names and the compilation option values ​​into a compilation option key-value pair.

[0064] In the specific implementation, the above-mentioned compile option key-value pairs can be obtained based on the following steps. Initialize the compile option key-value pair table config_y_map to be empty, create an input file stream object fin according to the .config file path filePath, if fin is not open, prompt an error and return, if fin is not ended, execute the following loop: read the current line of the file corresponding to fin into the string curline, if curline does not start with '#' and contains '=', extract the character substrings on the left and right sides of '=' in curline as key and value respectively, and convert the key-value pair<key,value> Add it to the compilation option key-value pair table config_y_map, and repeat this cycle. Finally, the above compilation option key-value pairs are stored in config_y_map.

[0065] Step S102: Scan and search the root directory and subdirectories of the system source code to obtain the Makefile file in the system source code.

[0066] In a feasible implementation manner, the step S20 may include:

[0067] Step S201: performing a preprocessing operation on the Makefile file according to the compile option key-value pair to obtain a preprocessed Makefile file, wherein the preprocessing operation includes a comment removal operation, a variable parsing operation and a conditional statement judgment operation.

[0068] Step S202: extracting the dependency relationship between the compilation option name and the Makefile file from the preprocessed Makefile file.

[0069] In the specific implementation, the entire analysis process is divided into Makefile file preprocessing, variable parsing, conditional judgment statement processing and dependency extraction. Makefile file preprocessing will remove comments in the Makefile file. The variable parsing module will identify assignment statements and save variables in the Makefile file, and replace variable values ​​when referencing variables. The conditional judgment statement processing module will process the conditions for the conditional judgment statement to analyze the accurate execution path. The dependency extraction module will use the above modules to extract the dependency between the compilation option name and the file.

[0070] In a feasible implementation manner, the step S30 may include:

[0071] Step S301: traverse all the compile option names in the compile option key-value pairs, and determine a list of source code file path names corresponding to all the compile option names in the compile option key-value pairs based on the dependency relationship.

[0072] Step S302: selecting a target source code set corresponding to a customized kernel from the system source code according to the source code file path name list.

[0073] In the specific implementation, after obtaining the above dependencies, you can use the required compilation options saved in config_y_map to obtain the source code range required to build the custom kernel (that is, the above target source code set). Save the path set of the source code files required to build the custom kernel to a list called needed_files_list. <sting>In a data structure of type. When obtaining needed_files_list, first traverse each compile option in config_y_map, search for the compile option name in compile_file_map and compile_option_y_map, and if there is an element corresponding to the compile option name in the map, save the C source code file path name list that the compile option depends on to needed_files_list. This step extracts the source code file range corresponding to the compile option value y. Next, it is necessary to extract the source code file corresponding to the compile option value n or the compile option does not exist in config_y_map. Traverse compile_option_n_map to determine whether a compile option name exists in config_y_map. If not, add the file path name list corresponding to the compile option to needed_files_list. At this point, the complete target source code set required to build a custom kernel is obtained, and module-level automatic trimming in a logical sense is achieved. The above target source code set can be determined based on the following steps. Use config_ite to point to the first element in config_y_map. If config_ite is not the end mark, execute the following loop: If compile_file_map contains the compile option value corresponding to config_ite, add the dependent file list corresponding to the config_ite compile option name in compile_file_map to needed_files_list; if compile_option_y_map contains the compile option value corresponding to config_ite, add the dependent file list corresponding to the config_ite compile option name in compile_option_y_map to needed_files_list. Use compile_n_ite to point to the first element of compile_option_n_map. If compile_n_ite is not the end mark, execute the following loop: If the compile option name of compile_n_ite does not exist in config_y_map, add the dependent file list of compile_n_ite to needed_files_list.

[0074] This embodiment obtains several compilation option names and compilation option values ​​corresponding to the compilation option names by analyzing the system configuration file line by line, and combines the compilation option names and the compilation option values ​​into compilation option key-value pairs; scans and searches the root directory and subdirectory of the system source code to obtain the Makefile file in the system source code; performs a preprocessing operation on the Makefile file according to the compilation option key-value pairs to obtain a preprocessed Makefile file, wherein the preprocessing operation includes a comment removal operation, a variable parsing operation and a conditional statement judgment operation; extracts the dependency relationship between the compilation option name and the Makefile file from the preprocessed Makefile file; traverses all the compilation option names in the compilation option key-value pairs, and determines a source code file path name list corresponding to all the compilation option names in the compilation option key-value pairs based on the dependency relationship; and selects a target source code set corresponding to a customized kernel from the system source code according to the source code file path name list. Compared with traditional system source code trimming, the above method of this embodiment establishes the target source code set through the system configuration file and the dependency relationship between the compilation option name and the file, and then automatically trims the target source code set layer by layer from top to bottom according to the dependency relationship, thereby logically reproducing the original module-level trimming.

[0075] refer to Figure 4 , Figure 4 This is a flowchart of the third embodiment of the system source code automatic trimming method of the present application.

[0076] In a feasible implementation manner, the step S40 may include:

[0077] Step S401: extracting function contents in the target source code set according to the function definition start line and the function definition end line in the target source code set.

[0078] In the specific implementation, in order to accurately match the function definition by calling the function name, the functions with global scope are saved in a map called global_function_map<string,MFunction*> In the data structure of the type, that is, the function definition is not modified by the "static" keyword. The lines where "(" and ")" are located are initially regarded as possible lines for function definitions, and then it is determined whether the next non-blank character of ")" is "{". If so, it is further determined whether the string before "(" is a specific keyword in the C language; if not, it is the starting line of the function definition, and the complete information of the function is further extracted. The syntax for using the while, if, for, and switch keywords in the C language is similar to that of function definitions, and "{" is used after a pair of parentheses, so they must be distinguished. In the Linux source code, function definitions have good writing standards. The "{" and "}" in the function body are the first characters of the line, and the syntax in the function body is All sentences are indented with "\t". Therefore, after determining that a certain line is the start line of the function definition, the end line can be determined by the fact that the first character of the line is "}", and the function content between "{" and "}" can be determined. After determining the function definition, get the function name named by numbers, uppercase and lowercase letters, and underscores before "(". Then get the string before the function name, and use the content between the modifier and the function name as the return type. The function modifiers used here to distinguish the return type are inline, static, and extern. At the same time, separate the string between "(" and ")" with "," to get the parameter list of the function.

[0079] Step S402: Analyze the function content according to the function call grammar rules to obtain the function call relationship between the functions in the target source code set.

[0080] In a specific implementation, it is necessary to analyze the contents in "{" and "}" to extract the function names or function pointer names of other functions called by a function. According to the function call syntax rules, "(" can be used to preliminarily determine that a certain place may be the place where the function call occurs, and then determine whether the identifier before "(" is a keyword in the C language. If not, it can be regarded as a function name or a function pointer name. At the same time, find the "")" that matches the "(" and extract the actual parameter list separated by "",. When encountering nested parentheses, that is, the parameter of the function call may also be a function call, and there may also be "", in the nested parentheses, a variable is needed to record the number of "(" encountered character by character. When encountering "(" brackets, the variable is increased by 1, and when encountering ")" brackets, the variable is reduced by 1. If and only if the variable is 1, the encountered ")" and "", are symbols of the corresponding level. Each "(" that appears in the function body is analyzed according to the above method until the end of the function body, and the called function name and actual parameter list are saved in the Call data structure. So far, all the information of a function definition and its calling function list have been obtained, and these information have been saved in MFunction, and MFunction is stored in file_f unction_map in the function definition map corresponding to the file name. At the same time, when it is checked that the string before the return type does not contain the "static" keyword, the address of MFunction needs to be saved in the global function global_function_map. The above analysis method is used for each file in needed_files_list to obtain each file and the functions defined therein and the global functions. It should be noted that the function call name saved in the call_func_list of MFunction at this time may be an actual function or a function pointer name. When the saved call function name is an actual function, the direct call relationship is extracted. When the saved call function name is a function pointer name, further analysis of the function pointer is required to obtain the accurate function call relationship. The specific implementation of function definition and direct function call relationship extraction is as follows. 1. Read the file corresponding to filePath, and curline points to the first line.2. If curline is not the end-of-line marker, execute the following loop: 2.1. If there is a '(' in the current line, then: 2.1.1. Find the ')' that matches the '(' and get the next non-blank character nextchar; 2.1.2. If nextchar is '{', then: 2.1.2.1. Get the named identifier string nameablestr1 before the '('; 2.1.2.2. If nameablestr1 is not a C language keyword, then: 2.1.2.2.1. Use nameablestr1 as the function name, extract and save the return type of the function , parameter list, modifiers to a MFunction structure named mfunction; 2.1.2.3. If the first character of the line is not '}', execute the following loop: 2.1.2.3.1. Save the line to a string functionbody; 2.1.2.3.2. Jump to the next line and go to 2.1.2.3 to continue the loop; 2.1.2.4. Get the position pos of the first '(' in functionbody; 2.1.2.5. If pos is a valid position in the string, execute the following loop: 2.1.2.5.1. Get the position of pos 2.1.2.5.2. If nameablestr2 is not a C language keyword, then: 2.1.2.5.2.1. Save nameablestr2, the actual parameter list between '(' and ')' to a Call structure named call; 2.1.2.5.3. Add call to the call_func_list of mfunction; 2.1.2.5.4. pos points to the position of the next '(' in functionbody, go to 2.1.2.5 to continue the loop; 2.1.2.6. Save nameablestr1 and mfunction to function_map; 2.1.2.7. If the modifier of the function does not contain static, then: 2.1.2.7.1. Save the addresses of nameablestr1 and mfunction to global_function_map; 2.2. curline jumps to the next line, go to step 2 to continue the loop. 3. Save filePath and function_map to file_function_map. 4. End and return.

[0081] Function pointers and their aliases use a map called functionPointer_map <string,list <string>> type of data structure, where the map key is the function pointer name, and the list is the alias of the function pointer, that is, the actual function name assigned to the function pointer. The function pointer defined in each file is saved using the above data structure and a map named file_functionPointer_map is used. <string,map<string,list <string>>>The data structure of type saves the file name and its corresponding functionPointer_map. The function pointer definition extraction process is as follows: 1. Read the file corresponding to filePath into the string filecontent. 2. Get the position pos of the first '(' or '=' in filecontent. 3. Initialize a functionPointer_map. 4. If pos is a valid position in the string, execute the following loop: 4.1. If the character at the pos position is '(' and the next character is '*', then: 4.1.1. Get the nameable string nameablestr1 after the '*'; 4.1.2. If the two characters after nameablestr1 are "")(", then: 4.1.2.1. Save nameablestr1 as the function pointer name in functionPointer_map; 4.1.2.2. If the current position character is not '=' or ';', execute the following loop: 4.1.2.2.1. Jump to the next character and go to 4.1.2.2 to continue the loop; 4.1.2.3. If the current position character is '=', then: 4.1.2.2.2. Get the nameable string after '=' as It is a function pointer alias and is added to the value of the corresponding function pointer name in functionPointer_map; otherwise, go to 4.1.3; 4.1.3. If the character at the pos position is '=' and the next one is not '=', then: 4.1.3.1. Get the nameable string nameablestr2 before '='; 4.1.3.2. If functionPointer_map contains nameablestr2, then: 4.1.3.2.1. Get the nameable string after '=' as the function pointer alias and add it to the value of the corresponding function pointer name in functionPointer_map; 4.2. pos points to the next '(' or '=' in filecontent, go to step 4 to continue the loop. 5. Save filePath and functionPointer_map to file_functionPointer_map. 6. End and return.

[0082] First, read the contents of a file into a string. When encountering "(", further determine whether the next character is "*". If so, skip the middle namable character and further determine whether the two characters after the namable character are "")(". If so, save the middle namable character as the function pointer name. When the function pointer is defined, it may be initialized and assigned at the same time, so it is necessary to determine whether there is "=" after the last "")" of the function pointer, and the next character of the first "=" cannot also be "=". If the assignment symbol is found, take the namable string after "=" as the function pointer alias and save it to the alias list of the corresponding function pointer name in the map. Since the function pointer can be modified and assigned after definition, when extracting the function pointer definition, the function pointer modification assignment must also be parsed. When encountering "=" and the next character is not "=", further determine whether the namable string before "=" is the extracted function pointer name. If so, take the namable string after "=" as the function pointer alias and save it to the alias list of the corresponding function pointer name in the map. So far, the definition of the function pointer and the explicitly assigned function pointer alias in the source code file have been extracted. Since the function pointer may also be implicitly assigned through a function call, the function pointer alias implicitly assigned through a function call should be extracted based on the previously obtained function definition and function call list. According to the function call name saved in the Call data structure, the MFunction defined by the original function is found, and the parameter list of the MFunction is judged one by one to see whether it is a function pointer. If a parameter is a function pointer, the corresponding actual parameter in Call is used as the function pointer alias. At the same time, this function pointer alias is saved in the function alias list of the corresponding function pointer. The specific implementation process is as follows. 1. Use filefunction_ite to point to the first element in file_function_map.2. When filefunction_ite is not the end element marker of file_function_map, execute the following loop: 2.1. Use function_ite to point to the first element in function_map in filefunction_ite; 2.2. When function_ite is not the end element marker of function_map, execute the following loop: 2.2.1. Use call_ite to point to the first Call element in call_func_list in function_ite; 2.2.2. call_ite is not the end element marker of call_func_list, execute the following loop: 2.2.2.1. calledFunction points to the corresponding MFunction in filefunction_ite or global_function_map; 2.2.2.2. If calledFunction is not empty, then: 2.2.2.2.1. Use parameter_ite to point to the first element in parameter_list of calledFunction; 2.2.2.2.2. If parameter_ite is not pa The end element marker of call_list, then execute the following loop: 2.2.2.2.2.1. If parameter_ite is a function pointer, then: 2.2.2.2.2.1.1. Extract the function pointer name functionpointername in parameter_ite; 2.2.2.2.2.1.2. Get the actual parameter argument at the corresponding position in the argument_list of call_ite; 2.2.2.2.2.1.3. Add argument to the corresponding function in functionPointer_map npointername's function pointer alias list; 2.2.2.2.2.2, parameter_ite points to the next element in parameter_list, go to 2.2.2.2.2 to continue the loop; 2.2.2.3, call_ite points to the next element, go to 2.2.2 to continue the loop; 2.2.3, function_ite points to the next element in function_map, go to 2.2 to continue the loop; 2.3, filefunction_ite points to the next element in file_function_map, go to step 2 to continue the loop. 3. End and return.

[0083] The function called by a function may be a function defined in this file or a global function defined in another file. Therefore, when looking for the original definition of the called function, you need to search in this file first. If there is no function definition of the corresponding function name in this file, then search in global_function_map. The above method is used to analyze each calling function of each function MFunction, and the function pointer alias assigned by the function call is collected. At this point, the implicitly assigned function pointer alias has also been completely obtained. After obtaining the direct function call relationship, function pointer definition and pointer alias, it is necessary to replace the function pointer name in the function call list with the original function name it points to, and complete the extraction of the indirect function call relationship. The calling function list of each function MFunction is judged one by one. If the calling function name is not a function defined in the file or a global function, it is a function pointer name, and the function pointer name is searched in the function pointer map of the corresponding file, and the function pointer name is replaced with the alias list corresponding to the function pointer name. Since a function pointer may correspond to multiple function names, that is, it has multiple function pointer aliases, when replacing a function pointer alias, each function pointer alias and actual parameter list should be added as a Call to the calling function list, and the Call object corresponding to the original function pointer alias in the calling function list should be deleted. So far, the direct and indirect function call relationships have been fully obtained, and the specific implementation process is as follows. 1. Use filefunction_ite to point to the first element in file_function_map.2. If filefunction_ite is not the end element marker of file_function_map, execute the following loop: 2.1. Use function_ite to point to the first element in function_map in filefunction_ite: 2.2. If function_ite is not the end element marker of function_map, execute the following loop: 2.2.1. Use call_ite to point to the first element in call_func_list in function_ite; 2.2.2. If call_ite is not the end element marker of call_func_list, execute the following loop: 2.2.2.1. calledFunction points to the corresponding MFunction in filefunction_ite or global_function_map; 2.2.2.2. If calledFunction is empty, then: 2.2.2.2.1. In fi Search the function pointer alias list alias_list corresponding to callName of call_ite in le_functionPointer_map; 2.2.2.2.2. If the corresponding alias_list is found, then: 2.2.2.2.2.1. Add each element in alias_list to the actual parameter list of call_ite to form a new Call object and add it to the call_func_list of function_ite; 2.2.2.2.2.2. Delete the original Call object in call_func_list; 2.2.2.3. call_ite points to the next element in call_func_list, go to 2.2.2 to continue the loop; 2.2.3. function_ite points to the next element in function_map, go to 2.2 to continue the loop; 2.3. filefunction_ite points to the next element in file_function_map, go to step 2 to continue the loop. 3. End and return.

[0084] Step S403: detecting and screening the target source code set through the calling relationship between the functions to obtain redundant functions, where the redundant functions are functions that are not called by other functions.

[0085] In the specific implementation, after the C source code module analysis is completed, the MFunction only saves the list of functions called by the function. In order to find redundant functions that are not called by other functions, it is also necessary to extract the called function list of a function. For each MFunction in file_function_map, traverse each calling function name in the calling relationship between calling functions, first search for the calling function name in the function_map corresponding to this file, if there is no such function definition in this file, then search in global_function_map. When the MFunction corresponding to the called function is found, the address corresponding to the MFunction that initiated the call is saved in the called function list of the called MFunction, which is described as follows. 1. Use filefunction_ite to point to the first function_map element in file_function_map.2. If filefunction_ite is not an end marker, execute the following loop: 2.1. Use function_ite to point to the first MFunction element in the function_map of filefunction_ite; 2.2. If function_ite is not an end marker, execute the following loop: 2.2.1. Use call_ite to point to each calling function in the calling function list in function_ite; 2.2.2. If call_ite is not an end marker, execute the following loop: 2.2.2.1. Initialize a calledFunction pointing to the MFunction type; 2.2.2.2. If filefunction_ite contains the calling function name of call_ite, then: 2.2.2.2.1. calledFunction points to function_i te corresponding to the MFunction of the called function name; otherwise, go to 2.2.2.3; 2.2.2.3, if global_function_map contains the called function name of the call_ite element, then: 2.2.2.3.1, calledFunction points to the MFunction corresponding to the called function name in global_function_map; 2.2.2.4, if calledFunction is not empty, then: 2.2.2.4.1, add calledFunction to the called function list of function_ite; 2.2.2.5, call_ite points to the next element, go to 2.2.2 to continue the loop; 2.2.3, function_ite points to the next element, go to 2.2 to continue the loop; 2.3, filefunction_ite points to the next element, go to step 2 to continue the loop. 3, end and return.

[0086] Step S404: determining the location information of the redundant function in the target source code set based on the starting line number and the ending line number of the redundant function, and automatically trimming the target source code set according to the location information.

[0087] In the specific implementation, after fully obtaining the calling and called relationships of each function, traverse each MFunction in each function_map in file_function_map, and save the functions whose calling function list and called function list are both empty to a list named delete_function_list.<MFunction*> In the data structure of type. The delete_function_list contains functions that will not call other functions nor be called by other functions. Deleting them will not affect the construction and normal use of the kernel. After completing the collection of redundant functions in a file, for each MFunction saved in the delete_function_list, its location information in the physical source file will be found through its saved starting line number and ending line number, and " / *" will be added before the starting line and "* / " will be added after the ending line. These redundant functions will be deleted by commenting. The implementation process is as follows. 1. Use filefunction_ite to point to the first function_map element in file_function_map.2. If filefunction_ite is not an end marker, execute the following loop: 2.1. Use function_ite to point to the first MFunction element in the function_map of filefunction_ite; 2.2. If function_ite is not an end marker, execute the following loop: 2.2.1. If both the calling function list and the called function list of function_ite are empty, then: 2.2.1.1. Add function_ite to delete_function_list; 2.3. function_ite points to the next element, go to step 2 to continue the loop; 2.4. Read the C file according to the file path of filefunction_ite, and curline points to the first line; 2.5. Get delete_func tion_list; 2.6. If curline is not the end mark, execute the following loop: 2.6.1. If curline is the start line of delete_ite, then: 2.6.1.1. Add " / *" before curline; otherwise, go to 2.6.2; 2.6.2. If curline is the end line of delete_ite, then: 2.6.2.1. Add "* / " after curline; 2.6.2.2. delete_ite points to the next element in delete_function_list; 2.6.3. curline jumps to the next line, go to 2.6 to continue the loop; 2.7. Save the trimmed file content to the corresponding physical file; 2.8. filefunction_ite points to the next element, go to step 2 to continue the loop. 3. End and return.

[0088] This embodiment extracts the function content in the target source code set according to the function definition start line and function definition end line in the target source code set; analyzes the function content according to the function call grammar rule to obtain the function call relationship corresponding to each function in the target source code set; detects and screens the target source code set through the function call relationship to obtain redundant functions, which are functions that are not called by other functions; determines the position information of the redundant function in the target source code set based on the start line number and end line number of the redundant function, and automatically trims the target source code set according to the position information. Compared with the traditional system source code trimming method, the function call relationship analysis of the above method in this embodiment will only be performed within the scope of the required target source code set, so that some functions existing in the reserved source code file that will only be called by the trimmed file will be obtained, thereby realizing function-level automatic trimming, providing a more fine-grained, reliable and efficient trimming solution for the embedded Linux system, so that the Linux system can meet the resource requirements of the embedded hardware environment while being further streamlined, greatly reducing the trimming difficulty of developers and improving the efficiency of embedded transplantation development of the Linux system.

[0089] In addition, an embodiment of the present application further provides a storage medium, on which a system source code automatic trimming program is stored. When the system source code automatic trimming program is executed by a processor, the steps of the system source code automatic trimming method described above are implemented.

[0090] Reference Figure 5 , Figure 5 This is a structural block diagram of the first embodiment of the system source code automatic trimming device of the present application.

[0091] like Figure 5 As shown, the system source code automatic trimming device proposed in the embodiment of the present application includes:

[0092] The data acquisition module 501 is used to acquire the compile option key-value pairs in the system configuration file and the Makefile file in the system source code;

[0093] A relationship extraction module 502 is used to extract the dependency relationship between the compile option name and the Makefile file from the Makefile file according to the compile option key-value pair;

[0094] A data selection module 503 is used to select a target source code set corresponding to a customized kernel from the system source code according to the dependency relationship, wherein the customized kernel is constructed based on user requirements;

[0095] The automatic trimming module 504 is used to determine the calling relationship between functions in the target source code set, and automatically trim the target source code set according to the calling relationship between functions.

[0096] This embodiment obtains the compile option key-value pairs in the system configuration file and the Makefile file in the system source code; extracts the dependency relationship between the compile option name and the Makefile file from the Makefile file according to the compile option key-value pairs; selects the target source code set corresponding to the customized kernel from the system source code according to the dependency relationship, and the customized kernel is constructed based on user needs; determines the function call relationship in the target source code set, and automatically trims the target source code set through the function call relationship. Compared with the traditional system source code trimming method, since the above method of this embodiment logically reproduces the native module-level trimming after establishing the target source code set through the system configuration file and the dependency relationship between the compile option name and the system source code, the subsequent function call relationship analysis will only be performed within the scope of the target source code set, so that the Linux kernel source code obtained after trimming removes the functions that are not in the kernel execution path, which not only reduces the size of the Linux kernel and speeds up the compilation speed, but also increases the security of the Linux kernel, thereby meeting the user's embedded refined customization needs for the Linux system.

[0097] Based on the first embodiment of the system source code automatic trimming device described above, a second embodiment of the system source code automatic trimming device described above is proposed.

[0098] In this embodiment, the data acquisition module 501 is also used to analyze the system configuration file line by line to obtain a number of compilation option names and compilation option values ​​corresponding to the compilation option names, and combine the compilation option names and the compilation option values ​​into a compilation option key-value pair; scan and search the root directory and subdirectory of the system source code to obtain the Makefile file in the system source code.

[0099] Furthermore, the relationship extraction module 502 is also used to perform a preprocessing operation on the Makefile file according to the compilation option key-value pair to obtain a preprocessed Makefile file, wherein the preprocessing operation includes a comment removal operation, a variable parsing operation and a conditional statement judgment operation; and extract the dependency relationship between the compilation option name and the Makefile file from the preprocessed Makefile file.

[0100] Furthermore, the data selection module 503 is also used to traverse all compilation option names in the compilation option key-value pairs, and determine a list of source code file path names corresponding to all compilation option names in the compilation option key-value pairs based on the dependency relationship; and select a target source code set corresponding to the customized kernel from the system source code according to the source code file path name list.

[0101] Furthermore, the automatic trimming module 504 is also used to extract the function content in the target source code set according to the function definition start line and the function definition end line in the target source code set; analyze the function content according to the function call syntax rules to obtain the function call relationship corresponding to each function in the target source code set.

[0102] Furthermore, the automatic trimming module 504 is also used to detect and screen the target source code set through the calling relationship between the functions to obtain redundant functions, where the redundant functions are functions that are not called by other functions; determine the position information of the redundant functions in the target source code set based on the starting line number and ending line number of the redundant functions, and automatically trim the target source code set according to the position information.

[0103] Other embodiments or specific implementations of the system source code automatic trimming device of the present application can refer to the above-mentioned method embodiments, which will not be repeated here.

[0104] It should be noted that, in this article, the term "includes", "comprising" or any other variation thereof is intended to cover non-exclusive inclusion, so that a process, method, article or system 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 system. In the absence of further restrictions, an element defined by the sentence "comprising a ..." does not exclude the existence of other identical elements in the process, method, article or system including the element.

[0105] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.

[0106] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as a read-only memory / random access memory, a disk, or an optical disk), and includes a number of instructions for a terminal device (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present application.

[0107] The above are only preferred embodiments of the present application, and are not intended to limit the patent scope of the present application. Any equivalent structure or equivalent process transformation made using the contents of the present application specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present application.< / string> < / string> < / sting> < / string>

Claims

1. A method for automatically cutting system source code, characterized in that: The method comprises the following steps: Get the compilation option key-value pairs in the system configuration file and the Makefile file in the system source code; Extracting the dependency relationship between the compile option name and the Makefile file from the Makefile file according to the compile option key-value pair; Selecting a target source code set corresponding to a customized kernel from the system source code according to the dependency relationship, wherein the customized kernel is constructed based on user requirements; Determine the calling relationship between functions in the target source code set, and automatically trim the target source code set according to the calling relationship between functions.

2. The system source code automatic tailoring method according to claim 1, characterized in that: The step of obtaining the compile option key-value pairs in the system configuration file and the Makefile file in the system source code includes: After analyzing the system configuration file line by line, a plurality of compile option names and compile option values ​​corresponding to the compile option names are obtained, and the compile option names and the compile option values ​​are combined into a compile option key-value pair; The root directory and subdirectories of the system source code are scanned and searched to obtain the Makefile file in the system source code.

3. The system source code automatic tailoring method according to claim 1, characterized in that: The step of extracting the dependency relationship between the compile option name and the Makefile file from the Makefile file according to the compile option key-value pair comprises: The Makefile file is caused to perform a preprocessing operation according to the compile option key-value pair to obtain a preprocessed Makefile file, wherein the preprocessing operation includes a comment removal operation, a variable parsing operation, and a conditional statement judgment operation; Extract the dependency relationship between the compile option name and the Makefile file from the preprocessed Makefile file.

4. The system source code automatic tailoring method according to claim 1, characterized in that: The step of selecting a target source code set corresponding to the customized kernel from the system source code according to the dependency relationship includes: Traversing all the compile option names in the compile option key-value pairs, and determining a list of source code file path names corresponding to all the compile option names in the compile option key-value pairs based on the dependency relationship; A target source code set corresponding to the customized kernel is selected from the system source code according to the source code file path name list.

5. The system source code automatic tailoring method according to claim 1, characterized in that: The step of determining the calling relationship between functions in the target source code set includes: Extracting function contents in the target source code set according to a function definition start line and a function definition end line in the target source code set; The function content is analyzed according to the function call grammar rules to obtain the function call relationship between the functions in the target source code set.

6. The system source code automatic tailoring method according to claim 1, characterized in that: The step of automatically trimming the target source code set according to the calling relationship between the functions includes: Detecting and screening the target source code set through the calling relationship between the functions to obtain redundant functions, where the redundant functions are functions that are not called by other functions; The location information of the redundant function in the target source code set is determined based on the starting line number and the ending line number of the redundant function, and the target source code set is automatically trimmed according to the location information.

7. A system source code automatic cutting device, characterized in that: The system source code automatic cutting device comprises: The data acquisition module is used to obtain the compilation option key-value pairs in the system configuration file and the Makefile file in the system source code; A relationship extraction module, used for extracting the dependency relationship between the compilation option name and the Makefile file from the Makefile file according to the compilation option key-value pair; A data selection module, used for selecting a target source code set corresponding to a customized kernel from the system source code according to the dependency relationship, wherein the customized kernel is constructed based on user requirements; The automatic trimming module is used to determine the calling relationship between functions in the target source code set, and automatically trim the target source code set according to the calling relationship between functions.

8. A system source code automatic cutting device, characterized in that: The device comprises: a memory, a processor, and a system source code automatic trimming program stored in the memory and executable on the processor, wherein the system source code automatic trimming program is configured to implement the steps of the system source code automatic trimming method according to any one of claims 1 to 6.

9. A storage medium, characterized in that: The storage medium is a computer-readable storage medium, and a system source code automatic trimming program is stored on the storage medium. When the system source code automatic trimming program is executed by a processor, the steps of the system source code automatic trimming method according to any one of claims 1 to 6 are implemented.

10. A computer program product, characterized in that The computer program product comprises a system source code automatic trimming program, and when the system source code automatic trimming program is executed by a processor, the steps of the system source code automatic trimming method according to any one of claims 1 to 6 are implemented.