1+ """Module for comparing ELF files parsed by 16-bit and 32-bit ELF parsers.
2+
3+ This script defines utility functions and classes to map and compare variables
4+ from ELF files parsed by the two parsers. Differences and similarities between
5+ the variable mappings are analyzed and displayed.
6+ """
7+
18from pyx2cscope .parser .generic_parser import GenericParser
29from pyx2cscope .parser .elf16_parser import Elf16Parser
310
411
512class VariableInfo :
13+ """Represents detailed information about a variable extracted from an ELF file."""
614 def __init__ (self , name , type , byte_size , address , array_size ):
15+ """Initialize a VariableInfo object.
16+
17+ :param name: Name of the variable
18+ :param type: Data type of the variable
19+ :param byte_size: Size of the variable in bytes
20+ :param address: Memory address of the variable
21+ :param array_size: Size of the array if the variable is an array
22+ """
723 self .name = name
824 self .type = type
925 self .byte_size = byte_size
1026 self .address = address
1127 self .array_size = array_size
1228
1329 def __repr__ (self ):
14- return f"VariableInfo(name={ self .name } , type={ self .type } , byte_size={ self .byte_size } , address={ self .address } , array_size={ self .array_size } )"
30+ """Return a string representation of the VariableInfo object."""
31+ return (f"VariableInfo(name={ self .name } , type={ self .type } , "
32+ f"byte_size={ self .byte_size } , address={ self .address } , "
33+ f"array_size={ self .array_size } )" )
1534
1635
1736def compare_dicts_intelligently (dict1 , dict2 ):
18- # Exclude pointer type variables from comparison
37+ """Compare two dictionaries of variables and analyze similarities and differences.
38+
39+ Variables with pointer types are excluded from the comparison.
40+ Differences are categorized by address, type, and array size.
41+
42+ :param dict1: First dictionary of variables
43+ :param dict2: Second dictionary of variables
44+ """
1945 dict1 = {key : value for key , value in dict1 .items () if not value .type .startswith ("pointer" )}
2046 dict2 = {key : value for key , value in dict2 .items () if not value .type .startswith ("pointer" )}
2147
22- common_keys = set ( dict1 .keys ()). intersection ( set ( dict2 .keys ()) )
23- unique_to_dict1 = set ( dict1 .keys ()) - set ( dict2 .keys () )
24- unique_to_dict2 = set ( dict2 .keys ()) - set ( dict1 .keys () )
48+ common_keys = dict1 .keys () & dict2 .keys ()
49+ unique_to_dict1 = dict1 .keys () - dict2 .keys ()
50+ unique_to_dict2 = dict2 .keys () - dict1 .keys ()
2551
2652 same_values = []
2753 different_values = []
2854
55+ def compare_variable_properties (var1 , var2 ):
56+ """Compare the properties of two variables and identify differences.
57+
58+ :param var1: Variable from dict1
59+ :param var2: Variable from dict2
60+ :return: A dictionary of differences
61+ """
62+ differences = {}
63+ if var1 .address != var2 .address :
64+ differences ["address_diff" ] = abs (var1 .address - var2 .address )
65+ if var1 .type != var2 .type :
66+ differences ["type_diff" ] = (var1 .type , var2 .type )
67+ if var1 .array_size != var2 .array_size :
68+ differences ["array_size_diff" ] = (var1 .array_size , var2 .array_size )
69+ return differences
70+
2971 for key in common_keys :
30- var1 = dict1 [key ]
31- var2 = dict2 [key ]
72+ differences = compare_variable_properties (dict1 [key ], dict2 [key ])
73+ if differences :
74+ different_values .append ({
75+ "key" : key ,
76+ "in_dict1" : dict1 [key ],
77+ "in_dict2" : dict2 [key ],
78+ "differences" : differences ,
79+ })
80+ else :
81+ same_values .append ({"key" : key , "in_dict1" : dict1 [key ], "in_dict2" : dict2 [key ]})
3282
33- address_match = var1 .address == var2 .address
34- type_match = var1 .type == var2 .type
35- array_size_match = var1 .array_size == var2 .array_size
83+ print_summary (unique_to_dict1 , dict1 , unique_to_dict2 , dict2 , different_values , same_values , len (common_keys ))
3684
37- if address_match and type_match and array_size_match :
38- same_values .append ({"key" : key , "in_dict1" : var1 , "in_dict2" : var2 })
39- else :
40- differences = {}
41- if not address_match :
42- differences ["address_diff" ] = abs (var1 .address - var2 .address )
43- if not type_match :
44- differences ["type_diff" ] = (var1 .type , var2 .type )
45- if not array_size_match :
46- differences ["array_size_diff" ] = (var1 .array_size , var2 .array_size )
47-
48- different_values .append (
49- {
50- "key" : key ,
51- "in_dict1" : var1 ,
52- "in_dict2" : var2 ,
53- "differences" : differences ,
54- }
55- )
5685
86+ def print_summary (unique_to_dict1 , dict1 , unique_to_dict2 , dict2 , different_values , same_values , common_count ):
87+ """Print a summary of the comparison results.
88+
89+ :param unique_to_dict1: Keys unique to the first dictionary
90+ :param dict1: The first dictionary
91+ :param unique_to_dict2: Keys unique to the second dictionary
92+ :param dict2: The second dictionary
93+ :param different_values: List of keys with differing values
94+ :param same_values: List of keys with identical values
95+ :param common_count: Number of common keys
96+ """
5797 print ("Keys only in dict1:" )
5898 for key in unique_to_dict1 :
5999 print (f"{ key } : { dict1 [key ]} " )
@@ -67,40 +107,31 @@ def compare_dicts_intelligently(dict1, dict2):
67107 print (f"{ diff ['key' ]} :" )
68108 print (f" in dict1: { diff ['in_dict1' ]} " )
69109 print (f" in dict2: { diff ['in_dict2' ]} " )
70- if "address_diff" in diff ["differences" ]:
71- print (f" Address difference: { diff ['differences' ]['address_diff' ]} " )
72- if "type_diff" in diff ["differences" ]:
73- print (f" Type difference: { diff ['differences' ]['type_diff' ]} " )
74- if "byte_size_diff" in diff ["differences" ]:
75- print (f" Byte size difference: { diff ['differences' ]['byte_size_diff' ]} " )
76- if "array_size_diff" in diff ["differences" ]:
77- print (f" Array size difference: { diff ['differences' ]['array_size_diff' ]} " )
110+ for k , v in diff ["differences" ].items ():
111+ print (f" { k .replace ('_' , ' ' ).capitalize ()} : { v } " )
78112
79113 print (f"\n Total variables in dict1: { len (dict1 )} " )
80114 print (f"Total variables in dict2: { len (dict2 )} " )
81- print (f"Variables in both parsers: { len ( common_keys ) } " )
115+ print (f"Variables in both parsers: { common_count } " )
82116 print (f"Different values: { len (different_values )} " )
83117 print (f"Same values: { len (same_values )} " )
84118
85119
86120def run_parsers_and_compare (elf_file_path ):
87- # Parse the ELF file with both 32-bit and 16-bit parsers
121+ """Run both ELF parsers and compare the variable maps they produce.
122+
123+ :param elf_file_path: Path to the ELF file to parse
124+ """
88125 elf32_parser = GenericParser (elf_file_path )
89126 elf16_parser = Elf16Parser (elf_file_path )
90127
91- # Generate variable maps for each parser
92128 variable_map_32 = elf32_parser ._map_variables ()
93129 variable_map_16 = elf16_parser ._map_variables ()
94130
95- # print("variable_map_16", variable_map_16)
96- # print("variable_map_32", variable_map_32)
97- # Compare the two dictionaries
98131 compare_dicts_intelligently (variable_map_16 , variable_map_32 )
99132
100133
101134# Usage example
102- elf_file = r"C:\Users\m67250\OneDrive - Microchip Technology Inc\Desktop\elfparser_Decoding\Unified.X\dist\default\production\Unified.X.production.elf"
103- elf_file = r"C:\Users\m67250\Downloads\mcapp_pmsm_zsmtlf(1)\mcapp_pmsm_zsmtlf\project\mcapp_pmsm.X\dist\default\production\mcapp_pmsm.X.production.elf"
104- #elf_file = r"C:\_DESKTOP\_Projects\33ak_MCLV48V300W_FOC_PLL\project\pmsm.X\dist\default\production\pmsm.X.production.elf"
105- #elf_file = r"C:\Users\m67250\OneDrive - Microchip Technology Inc\Desktop\Training_Domel\motorbench_demo_domel.X\dist\default\production\motorbench_demo_domel.X.production.elf"
106- run_parsers_and_compare (elf_file )
135+ if __name__ == "__main__" :
136+ elf_file = r"elf_file"
137+ run_parsers_and_compare (elf_file )
0 commit comments