44use object:: read:: archive:: { ArchiveFile , ArchiveMember } ;
55use object:: { Object , ObjectSymbol , Symbol , SymbolKind , SymbolScope , SymbolSection } ;
66use std:: collections:: { BTreeMap , BTreeSet } ;
7- use std:: fs;
8- use std:: path:: Path ;
7+ use std:: { fs, path:: Path } ;
98
109const USAGE : & str = "Usage:
1110
12- symbol-check check-duplicates PATHS...
13- symbol-check check-core-syms PATHS...
14- " ;
11+ symbol-check check-duplicates ARCHIVE ...
12+ symbol-check check-core-syms ARCHIVE ...
13+
14+ Note that multiple archives may be specified but they are checked independently
15+ rather than as a group." ;
1516
1617fn main ( ) {
18+ // Create a `&str` vec so we can match on it.
1719 let args = std:: env:: args ( ) . collect :: < Vec < _ > > ( ) ;
1820 let args_ref = args. iter ( ) . map ( String :: as_str) . collect :: < Vec < _ > > ( ) ;
1921
@@ -73,8 +75,8 @@ fn verify_no_duplicates(path: impl AsRef<Path>) {
7375 let mut dups = Vec :: new ( ) ;
7476
7577 for_each_symbol ( path, |sym, member| {
78+ // Only check defined globals
7679 if !sym. is_global ( ) || sym. is_undefined ( ) {
77- // Only check defined globals
7880 return ;
7981 }
8082
@@ -91,18 +93,20 @@ fn verify_no_duplicates(path: impl AsRef<Path>) {
9193 } ) ;
9294
9395 if cfg ! ( windows) {
96+ // Ignore literal constants
9497 let allowed_dup_pfx = [ "__real@" , "__xmm@" ] ;
9598 dups. retain ( |sym| !allowed_dup_pfx. iter ( ) . any ( |pfx| sym. name . starts_with ( pfx) ) ) ;
9699 }
97100
98101 if !dups. is_empty ( ) {
99102 dups. sort_unstable_by ( |a, b| a. name . cmp ( & b. name ) ) ;
100- panic ! ( "Found duplicate symbols: {dups:#?}" ) ;
103+ panic ! ( "found duplicate symbols: {dups:#?}" ) ;
101104 }
102105
103106 println ! ( "success: no duplicate symbols found" ) ;
104107}
105108
109+ /// Ensure that there are no references to symbols from `core` that aren't also (somehow) defined.
106110fn verify_core_symbols ( path : impl AsRef < Path > ) {
107111 println ! (
108112 "Checking for references to core at {}" ,
@@ -113,6 +117,7 @@ fn verify_core_symbols(path: impl AsRef<Path>) {
113117 let mut undefined = Vec :: new ( ) ;
114118
115119 for_each_symbol ( path, |sym, member| {
120+ // Find only symbols from `core`
116121 if !sym. name ( ) . unwrap ( ) . contains ( "_ZN4core" ) {
117122 return ;
118123 }
@@ -125,27 +130,25 @@ fn verify_core_symbols(path: impl AsRef<Path>) {
125130 }
126131 } ) ;
127132
133+ // Discard any symbols that are defined somewhere in the archive
128134 undefined. retain ( |sym| !defined. contains ( & sym. name ) ) ;
129135
130136 if !undefined. is_empty ( ) {
131137 undefined. sort_unstable_by ( |a, b| a. name . cmp ( & b. name ) ) ;
132- panic ! ( "Found undefined symbols from ` core` : {undefined:#?}" ) ;
138+ panic ! ( "found undefined symbols from core: {undefined:#?}" ) ;
133139 }
134140
135141 println ! ( "success: no undefined references to core found" ) ;
136142}
137143
138144/// For a given archive path, do something with each symbol.
139145fn for_each_symbol ( path : impl AsRef < Path > , mut f : impl FnMut ( Symbol , & ArchiveMember ) ) {
140- let archive_data = fs:: read ( path) . expect ( "reading file failed" ) ;
141- let x = ArchiveFile :: parse ( archive_data. as_slice ( ) ) . expect ( "archive parse failed" ) ;
142- for member in x. members ( ) {
143- let member = member. unwrap ( ) ;
144- let data = member. data ( & * archive_data) . unwrap ( ) ;
145- let obj = object:: File :: parse ( data) . expect ( "object parse failed" ) ;
146-
147- for sym in obj. symbols ( ) {
148- f ( sym, & member) ;
149- }
146+ let data = fs:: read ( path) . expect ( "reading file failed" ) ;
147+ let archive = ArchiveFile :: parse ( data. as_slice ( ) ) . expect ( "archive parse failed" ) ;
148+ for member in archive. members ( ) {
149+ let member = member. expect ( "failed to access member" ) ;
150+ let obj_data = member. data ( & * data) . expect ( "failed to access object" ) ;
151+ let obj = object:: File :: parse ( obj_data) . expect ( "failed to parse object" ) ;
152+ obj. symbols ( ) . for_each ( |sym| f ( sym, & member) ) ;
150153 }
151154}
0 commit comments