A method for analyzing C / C++ bare machine program stack of TI C6000 series chip
Patent Information
- Application Number
- CN202311595889.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-27
- Publication Date
- 2026-09-01
- Estimated Expiration
- 2043-11-27
AI Technical Summary
该系列DSP芯片开发时一般使用片内RAM,其片内RAM较小,例如TMS320C6713B只有256KBytes,这必定会限制C/C++程序栈的大小,会极大提高栈溢出的风险
[0018]与现有技术相比,本发明的有益效果包括:
Smart Images

Figure CN117762779B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of software testing technology, specifically relating to a method for analyzing the use of the C / C++ bare-metal program stack of TI C6000 series chips. Background Technology
[0002] The C6000 series chips are a series of high-speed floating-point DSP chips from TI, widely used in various industrial control equipment. These DSP chips typically utilize on-chip RAM during development, which is relatively small; for example, the TMS320C6713B has only 256 KB of RAM. This inevitably limits the size of the C / C++ program stack, significantly increasing the risk of stack overflow. Therefore, analyzing stack usage in bare-metal C / C++ programs for the C6000 series chips becomes a necessary part of software testing. Summary of the Invention
[0003] (a) Technical problems to be solved
[0004] The purpose of this invention is to fully analyze the call relationships and stack usage information of each function in the bare-metal C / C++ program of the TI C6000 series chip, and store them in the form of serialized files and HTML files. This invention can not only intuitively display the stack usage of each function call chain, but also allow testers to further analyze the results.
[0005] (II) Technical Solution
[0006] This invention proposes a method for analyzing the stack usage of C / C++ bare-metal programs for TI C6000 series chips. It primarily relies on Java parsing of OUT files to analyze function calls and stack usage in the C / C++ bare-metal programs of the TI C6000 series chips. Specifically, it includes the following steps:
[0007] Step 1: Compile the C / C++ bare-metal program using the TI Integrated Development Environment to generate a *.out file, write a Java program, and use Runtime to call the TI C6000 series chip disassembler dis6x to parse the *.out file and generate an assembly file *.asm. Set the top-level function in the Java program.
[0008] Step 2: Use Java's BufferedReader to read the *.asm file line by line. Break down the information string by spaces. When parsing assembly information without instruction data, it indicates the acquisition of Label information or segment information. Store this information in the Label information list line by line, and simultaneously obtain the program address and function name of the current assembly information. When parsing assembly information containing instruction data, it indicates the acquisition of assembly instruction information. Break down the assembly information by spaces, and obtain the program address, instruction data, assembly instruction, and assembly parameters of the current assembly instruction. When an assembly instruction is parsed as ".word", the current line of assembly information is data information of the data segment, and this line of assembly information is not processed further. Store the parsed information in the assembly information list line by line.
[0009] Step 3: Use Java to parse the Label information list, skipping useless segments, data information, and Labels whose names contain the "$" character. Create an address-Label hash table with "program address-Label" as the key-value pair. Store the Labels in the function Label list according to the order of the Label information list. Set the start address of the function corresponding to each function Label to the program address of the function Label. Subtract 4 from the program address of the next function Label, useless segment, or data information to get the end address of the function corresponding to the current function Label.
[0010] Step 4: Use Java to parse the assembly information list, parse the assembly instruction information to call other functions, when parsing the "B" instruction, determine if the assembly parameters of the current assembly instruction contain function labels, parse out the called function name and the program address of the called function according to the assembly instruction information format, and store the program address of the "B" instruction and the called function address into the jump instruction list;
[0011] Step 5: Use Java to parse the assembly information list, perform preprocessing for stack usage of each function, parse the assembly instructions and assembly parameters. When the "SUB" instruction is parsed, the parameter string is decomposed by "," and the original assembly parameter list is replaced. When the "STW" instruction is parsed, the parameter string is decomposed by "," and the original assembly parameter list is replaced. When the "ADDK" instruction is parsed, the parameter string is decomposed by "," and the original assembly parameter list is replaced.
[0012] Step 6: Obtain each function label in the order of the function label list. Between the start address and end address of the function corresponding to the function label, find the assembly instruction that belongs to the jump instruction list and store it in the calling function list of the current function.
[0013] Step 7: Obtain the function labels in the order of the function label list. Parse the assembly instructions and parameters in the first 16 assembly instructions of the function corresponding to each label. If the assembly instruction is "SUB" and there are 3 assembly parameters, check if the first and third parameters are "B15" and if the second parameter is an immediate value. If both are true, it indicates that the current assembly instruction is a stack operation instruction, and the second parameter is the stack size. If the "STW" instruction is found in the first 16 assembly instructions of the function corresponding to each label and there are 2 parameters, check if the second parameter is "*B15--[N]", where N represents an immediate value. If true, it indicates that... This instruction is a stack operation instruction. The immediate value in the second parameter multiplied by 4 is the stack size. If the "ADDK" instruction is found in the first 16 assembly instructions of the function corresponding to Label, and it has two parameters, check if the first parameter is a negative immediate value and if the second parameter is "B15". If both are true, it indicates that this instruction is a stack operation instruction. Changing the first parameter to a positive number gives the stack size. After the function corresponding to Label finds the stack operation instruction, the parsing of the current function's assembly instructions stops, and the stack size is recorded in the function. If more than 16 assembly instructions are passed, or if no stack operation instruction is found even after reaching the end address of the function, it indicates that this function has no stack operation.
[0014] Step 8: Starting from each top-level function, perform a depth-first traversal of the function call list for each function to form a function call tree. Accumulate the function stack size of each branch of the call tree. When a function call has already appeared in a branch, stop the function call analysis for that branch; otherwise, continue until there are no more function calls. Through depth-first traversal starting from the top-level function, form the call tree and stack information for each top-level function. Serialize the function call tree and the stack size of each function and save them to a file for further analysis.
[0015] Step 9: Using Java's javax.xml and org.w3c.dom libraries, generate an HTML file based on function call relationships and stack size.
[0016] Furthermore, in step 1, the top-level function includes _main and various interrupt functions.
[0017] (III) Beneficial Effects
[0018] Compared with the prior art, the beneficial effects of the present invention include:
[0019] Improve analysis efficiency: Automated analysis can greatly improve analysis efficiency and reduce the time and cost of manual analysis.
[0020] Increased analysis depth: By parsing assembly files, the call relationships and stack usage within library functions can be analyzed more thoroughly, enabling a more complete stack analysis of the program.
[0021] Analysis result readability: Using HTML files to explicitly analyze the results allows testers to quickly understand the stack usage of each function call chain in the program.
[0022] Improve code quality: Based on the analysis of stack usage, design the stack size reasonably to avoid overflow risks and resource waste.
[0023] Scalability and cross-platform compatibility: Java is a cross-platform language with excellent extensibility, allowing for easy addition of new features and modules to adapt to ever-changing software testing needs; at the same time, Java can directly serialize and store parsed results as files, facilitating further data analysis. Attached Figure Description
[0024] Figure 1 A flowchart illustrating the method for analyzing function calls and stack usage;
[0025] Figure 2 This is a schematic diagram of the ASM file structure;
[0026] Figure 3 This is a schematic diagram of the data structure for program function calls and stack information;
[0027] Figure 4 This is an HTML diagram illustrating program function calls and stack information. Detailed Implementation
[0028] To better understand the purpose, technical solution, and function of this invention, the invention will be described in further detail below with reference to the accompanying drawings. However, this invention can be implemented in many different ways as defined and covered by the claims. The accompanying drawings, which constitute a part of this application, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an improper limitation of the invention.
[0029] This invention proposes a method for analyzing function calls and stack usage in C / C++ bare-metal programs of TI C6000 series chips using Java-based OUT file parsing. The method involves writing a Java program to call the TI C6000 series chip disassembler, dis6x, to parse the OUT file generated by the TI Integrated Development Environment (CCS), obtaining the assembly file of the C / C++ bare-metal program. Then, a Java program is used to parse the assembly file, obtaining the call relationships and stack usage of each function in the program. The parsed function call relationships and stack usage information are saved in a tree structure as a serialized file and an HTML file. The stack usage of each function call chain can be viewed using a browser or custom software.
[0030] The main process of the technical solution of this invention is as follows: Figure 1 As shown, the method proposed in this invention for analyzing function calls and stack usage in C / C++ bare-metal programs of TIC6000 series chips based on Java parsing of OUT files, taking the TI TMS320C6713B chip as an example, includes the following steps:
[0031] (1) Compile the bare-metal C / C++ program using the TI Integrated Development Environment (CCS), generate the *.out file, write the Java program, use Runtime to call the TI C6000 series chip disassembler dis6x, parse the *.out file to generate the assembly file *.asm; set the top-level function: _main, and various interrupt functions in the Java program.
[0032] (2) Use Java's BufferedReader to read *.asm files line by line. Figure 2 This is a diagram of the ASM file structure. The information string is broken down by spaces. When assembly information without instruction data is encountered, it indicates the acquisition of Label information or segment information, which is stored in the Label information list line by line. Simultaneously, the program address and function name of the current assembly instruction are obtained. When assembly information containing instruction data is encountered, it indicates the acquisition of assembly instruction information. The assembly information is broken down by spaces to obtain the program address, instruction data, assembly instruction, and assembly parameters of the current assembly instruction. When an assembly instruction is parsed as ".word", the current line of assembly information is data information from the data segment, and this line of assembly information is not processed further. The parsed information is then stored in the assembly information list line by line.
[0033] (3) Use Java to parse the Label information list, skipping useless segments (names starting with "."), data information (names starting with "."), and Labels whose names contain the "$" character (function internal Labels, custom assembly Labels, etc.). Establish an address-Label hash table with "program address-Label" as the key-value pair. Store the Labels in the function Label list according to the order of the Label information list; set the start address of the function corresponding to each function Label to the program address of the function Label. Subtract 4 from the program address of the next function Label, useless segment, or data information to obtain the end address of the function corresponding to the current function Label.
[0034] (4) Use Java to parse the assembly information list and parse the assembly instruction information to call other functions. When the "B" instruction is parsed, if the assembly parameter of the current assembly instruction contains a function label, parse out the called function name and the program address of the called function according to the assembly instruction information format, and store the program address of the "B" instruction and the address of the called function in the jump instruction list.
[0035] (5) Use Java to parse the assembly information list and perform preprocessing parsing for stack usage of each function. Parse the assembly instructions and assembly parameters. When parsing the "SUB" instruction, decompose the parameter string with "," and replace the original assembly parameter list. When parsing the "STW" instruction, decompose the parameter string with "," and replace the original assembly parameter list. When parsing the "ADDK" instruction, decompose the parameter string with "," and replace the original assembly parameter list.
[0036] (6) Obtain each function label in the order of the function label list. Between the start address and end address of the function corresponding to the function label, find the assembly instruction that belongs to the jump instruction list and store it in the calling function list of the current function.
[0037] (7) Obtain the function Labels in the order of the function Label list. Parse the assembly instructions and parameters in the first 16 assembly instructions of the function corresponding to the function Label. If the assembly instruction is "SUB" and there are 3 assembly parameters, check if the first and third parameters are "B15" and if the second parameter is an immediate value. If both are true, it indicates that the current assembly instruction is a stack operation instruction, and the second parameter is the stack size. If the "STW" instruction is found in the first 16 assembly instructions of the function corresponding to the function Label and there are 2 parameters, check if the second parameter is "*B15--[N]" (N represents an immediate value). If true, it indicates that the instruction is a stack operation instruction. This instruction is a stack operation instruction. The immediate value in the second parameter multiplied by 4 is the stack size. If the "ADDK" instruction is found in the first 16 assembly instructions of the function corresponding to Label, and it has two parameters, check if the first parameter is a negative immediate value and if the second parameter is "B15". If both are true, it indicates that this instruction is a stack operation instruction. Changing the first parameter to a positive number indicates the stack size. After the function corresponding to Label finds the stack operation instruction, it stops parsing the assembly instructions for the current function and records the stack size in the function. If more than 16 assembly instructions are found, or if no stack operation instruction is found after reaching the end address of the function, it indicates that this function has no stack operation.
[0038] (8) Starting from each top-level function, perform a depth-first traversal of the function call list to form a function call tree. Accumulate the stack size of each branch of the call tree. Stop analyzing function calls for a branch when the function call to that branch has already appeared in that branch; otherwise, continue until no function call is found. Through this depth-first traversal starting from the top-level function, the call tree and stack information for each top-level function are formed. The function call tree and the stack size of each function are serialized and stored in a file for further analysis. Figure 3 This is a schematic diagram of a serialized data structure.
[0039] (9) Using Java's javax.xml and org.w3c.dom libraries, generate HTML files based on function call relationships and cumulative stack size. Figure 4 This is an HTML diagram illustrating program function calls and stack information. The diagram shows the call tree of the _main function. In the function, "(A|B)" indicates that A represents the stack bytes used by the current function, and B represents the cumulative stack usage size of the call tree. _funa exhibits self-calling and is displayed using a different colored font.
[0040] This method not only improves the analysis efficiency of C / C++ bare-metal programs for TI C6000 series chips, but also parses and displays the internal call relationships and stack usage information of library functions used in C / C++ programs, avoiding design flaws and improving code quality.
[0041] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the technical principles of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A method for analyzing the use of the C / C++ bare-metal program stack in TI C6000 series chips, characterized in that, Includes the following steps: Step 1: Compile the C / C++ bare-metal program using the TI Integrated Development Environment to generate a *.out file, write a Java program, and use Runtime to call the TI C6000 series chip disassembler dis6x to parse the *.out file and generate an assembly file *.asm. Set the top-level function in the Java program. Step 2: Use Java's BufferedReader to read the *.asm file line by line. Break down the information string by spaces. When parsing assembly information without instruction data, it indicates that Label information or segment information has been obtained. Store this information in the Label information list line by line, and simultaneously obtain the program address and function name of the current assembly information. When parsing assembly information containing instruction data, it indicates that assembly instruction information has been obtained. Break down the assembly information by spaces, and obtain the program address, instruction data, assembly instruction, and assembly parameters of the current assembly instruction. When an assembly instruction is parsed as ".word", the current line of assembly information is data information of the data segment, and this line of assembly information is not processed further. Store the parsed information in the assembly information list line by line. Step 3: Use Java to parse the Label information list, skipping useless segments, data information, and Labels whose names contain the "$" character. Create an address-Label hash table with "program address-Label" as the key-value pair. Store the Labels in the function Label list according to the order of the Label information list. Set the start address of the function corresponding to each function Label to the program address of the function Label. Subtract 4 from the program address of the next function Label, useless segment, or data information to get the end address of the function corresponding to the current function Label. Step 4: Use Java to parse the assembly information list, parse the assembly instruction information to call other functions, when parsing the "B" instruction, determine if the assembly parameters of the current assembly instruction contain function labels, parse out the called function name and the program address of the called function according to the assembly instruction information format, and store the program address of the "B" instruction and the address of the called function in the jump instruction list; Step 5: Use Java to parse the assembly information list, perform preprocessing for stack usage of each function, parse the assembly instructions and assembly parameters. When parsing the "SUB" instruction, decompose the parameter string by commas and replace the original assembly parameter list; when parsing the "STW" instruction, decompose the parameter string by commas and replace the original assembly parameter list; when parsing the "ADDK" instruction, decompose the parameter string by commas and replace the original assembly parameter list. Step 6: Obtain each function label in the order of the function label list. Between the start address and end address of the function corresponding to the function label, find the assembly instruction that belongs to the jump instruction list and store it in the calling function list of the current function. Step 7: Obtain the function labels in the order of the function label list. Parse the assembly instructions and parameters in the first 16 assembly instructions of the function corresponding to each label. If the assembly instruction is "SUB" and there are 3 assembly parameters, check if the first and third parameters are "B15" and if the second parameter is an immediate value. If both are true, it indicates that the current assembly instruction is a stack operation instruction, and the second parameter is the stack size. If the "STW" instruction is found in the first 16 assembly instructions of the function corresponding to each label and there are 2 parameters, check if the second parameter is "*B15--[N]", where N represents an immediate value. If true, it indicates... This instruction is a stack operation instruction. The immediate value in the second parameter multiplied by 4 is the stack size. If the "ADDK" instruction is found in the first 16 assembly instructions of the function corresponding to Label, and it has two parameters, check if the first parameter is a negative immediate value and if the second parameter is "B15". If both are true, it indicates that this instruction is a stack operation instruction. Changing the first parameter to a positive number gives the stack size. After the function corresponding to Label finds the stack operation instruction, the parsing of the current function's assembly instructions stops, and the stack size is recorded in the function. If more than 16 assembly instructions are passed, or if no stack operation instruction is found even after reaching the end address of the function, it indicates that this function has no stack operation. Step 8: Starting from each top-level function, perform a depth-first traversal of the function call list for each function to form a function call tree. Accumulate the function stack size of each branch of the call tree. When a function call has already appeared in a branch, stop the function call analysis for that branch; otherwise, continue until there are no more function calls. Through depth-first traversal starting from the top-level function, form the call tree and stack information for each top-level function. Serialize the function call tree and the stack size of each function and save them to a file for further analysis. Step 9: Using Java's javax.xml and org.w3c.dom libraries, generate an HTML file based on function call relationships and stack size.
2. The method for analyzing the use of the C / C++ bare-metal program stack of a TI C6000 series chip according to claim 1, characterized in that, In step 1, the top-level function includes _main and various interrupt functions.