@@ -11,27 +11,6 @@ use crate::{
1111 obj:: { ObjInfo , ObjSection , SymbolRef } ,
1212} ;
1313
14- /// Compare the addresses and sizes of each symbol in the BSS sections.
15- pub fn diff_bss_section (
16- left : & ObjSection ,
17- right : & ObjSection ,
18- ) -> Result < ( ObjSectionDiff , ObjSectionDiff ) > {
19- let deadline = Instant :: now ( ) + Duration :: from_secs ( 5 ) ;
20- let left_sizes = left. symbols . iter ( ) . map ( |s| ( s. section_address , s. size ) ) . collect :: < Vec < _ > > ( ) ;
21- let right_sizes = right. symbols . iter ( ) . map ( |s| ( s. section_address , s. size ) ) . collect :: < Vec < _ > > ( ) ;
22- let ops = capture_diff_slices_deadline (
23- Algorithm :: Patience ,
24- & left_sizes,
25- & right_sizes,
26- Some ( deadline) ,
27- ) ;
28- let match_percent = get_diff_ratio ( & ops, left_sizes. len ( ) , right_sizes. len ( ) ) * 100.0 ;
29- Ok ( (
30- ObjSectionDiff { symbols : vec ! [ ] , data_diff : vec ! [ ] , match_percent : Some ( match_percent) } ,
31- ObjSectionDiff { symbols : vec ! [ ] , data_diff : vec ! [ ] , match_percent : Some ( match_percent) } ,
32- ) )
33- }
34-
3514pub fn diff_bss_symbol (
3615 left_obj : & ObjInfo ,
3716 right_obj : & ObjInfo ,
@@ -65,6 +44,8 @@ pub fn no_diff_symbol(_obj: &ObjInfo, symbol_ref: SymbolRef) -> ObjSymbolDiff {
6544pub fn diff_data_section (
6645 left : & ObjSection ,
6746 right : & ObjSection ,
47+ left_section_diff : & ObjSectionDiff ,
48+ right_section_diff : & ObjSectionDiff ,
6849) -> Result < ( ObjSectionDiff , ObjSectionDiff ) > {
6950 let deadline = Instant :: now ( ) + Duration :: from_secs ( 5 ) ;
7051 let left_max = left. symbols . iter ( ) . map ( |s| s. section_address + s. size ) . max ( ) . unwrap_or ( 0 ) ;
@@ -73,7 +54,6 @@ pub fn diff_data_section(
7354 let right_data = & right. data [ ..right_max as usize ] ;
7455 let ops =
7556 capture_diff_slices_deadline ( Algorithm :: Patience , left_data, right_data, Some ( deadline) ) ;
76- let match_percent = get_diff_ratio ( & ops, left_data. len ( ) , right_data. len ( ) ) * 100.0 ;
7757
7858 let mut left_diff = Vec :: < ObjDataDiff > :: new ( ) ;
7959 let mut right_diff = Vec :: < ObjDataDiff > :: new ( ) ;
@@ -143,18 +123,11 @@ pub fn diff_data_section(
143123 }
144124 }
145125
146- Ok ( (
147- ObjSectionDiff {
148- symbols : vec ! [ ] ,
149- data_diff : left_diff,
150- match_percent : Some ( match_percent) ,
151- } ,
152- ObjSectionDiff {
153- symbols : vec ! [ ] ,
154- data_diff : right_diff,
155- match_percent : Some ( match_percent) ,
156- } ,
157- ) )
126+ let ( mut left_section_diff, mut right_section_diff) =
127+ diff_generic_section ( left, right, left_section_diff, right_section_diff) ?;
128+ left_section_diff. data_diff = left_diff;
129+ right_section_diff. data_diff = right_diff;
130+ Ok ( ( left_section_diff, right_section_diff) )
158131}
159132
160133pub fn diff_data_symbol (
@@ -195,21 +168,24 @@ pub fn diff_data_symbol(
195168 ) )
196169}
197170
198- /// Compare the text sections of two object files.
199- /// This essentially adds up the match percentage of each symbol in the text section.
200- pub fn diff_text_section (
171+ /// Compares a section of two object files.
172+ /// This essentially adds up the match percentage of each symbol in the section.
173+ pub fn diff_generic_section (
201174 left : & ObjSection ,
202175 _right : & ObjSection ,
203176 left_diff : & ObjSectionDiff ,
204177 _right_diff : & ObjSectionDiff ,
205178) -> Result < ( ObjSectionDiff , ObjSectionDiff ) > {
206- let match_percent = left
207- . symbols
208- . iter ( )
209- . zip ( left_diff. symbols . iter ( ) )
210- . map ( |( s, d) | d. match_percent . unwrap_or ( 0.0 ) * s. size as f32 )
211- . sum :: < f32 > ( )
212- / left. size as f32 ;
179+ let match_percent = if left_diff. symbols . iter ( ) . all ( |d| d. match_percent == Some ( 100.0 ) ) {
180+ 100.0 // Avoid fp precision issues
181+ } else {
182+ left. symbols
183+ . iter ( )
184+ . zip ( left_diff. symbols . iter ( ) )
185+ . map ( |( s, d) | d. match_percent . unwrap_or ( 0.0 ) * s. size as f32 )
186+ . sum :: < f32 > ( )
187+ / left. size as f32
188+ } ;
213189 Ok ( (
214190 ObjSectionDiff { symbols : vec ! [ ] , data_diff : vec ! [ ] , match_percent : Some ( match_percent) } ,
215191 ObjSectionDiff { symbols : vec ! [ ] , data_diff : vec ! [ ] , match_percent : Some ( match_percent) } ,
0 commit comments