singleuse.pl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2019 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. ###########################################################################
  23. #
  24. # This script is aimed to help scan for and detect globally declared functions
  25. # that are not used from other source files.
  26. #
  27. # Use it like this:
  28. #
  29. # $ ./scripts/singleuse.pl lib/.libs/libcurl.a
  30. #
  31. # Be aware that it might cause false positives due to various build options.
  32. #
  33. my $file = $ARGV[0];
  34. my %wl = (
  35. 'Curl_none_cert_status_request' => 'multiple TLS backends',
  36. 'Curl_none_check_cxn' => 'multiple TLS backends',
  37. 'Curl_none_cleanup' => 'multiple TLS backends',
  38. 'Curl_none_close_all' => 'multiple TLS backends',
  39. 'Curl_none_data_pending' => 'multiple TLS backends',
  40. 'Curl_none_engines_list' => 'multiple TLS backends',
  41. 'Curl_none_init' => 'multiple TLS backends',
  42. 'Curl_none_md5sum' => 'multiple TLS backends',
  43. 'Curl_none_random' => 'multiple TLS backends',
  44. 'Curl_none_session_free' => 'multiple TLS backends',
  45. 'Curl_none_set_engine' => 'multiple TLS backends',
  46. 'Curl_none_set_engine_default' => 'multiple TLS backends',
  47. 'Curl_none_shutdown' => 'multiple TLS backends',
  48. 'Curl_multi_dump' => 'debug build only',
  49. 'Curl_parse_port' => 'UNITTEST',
  50. 'Curl_shuffle_addr' => 'UNITTEST',
  51. 'de_cleanup' => 'UNITTEST',
  52. 'doh_decode' => 'UNITTEST',
  53. 'doh_encode' => 'UNITTEST',
  54. 'Curl_auth_digest_get_pair' => 'by digest_sspi',
  55. 'curlx_uztoso' => 'cmdline tool use',
  56. 'curlx_uztoul' => 'by krb5_sspi',
  57. 'curlx_uitous' => 'by schannel',
  58. 'Curl_islower' => 'by curl_fnmatch',
  59. 'getaddressinfo' => 'UNITTEST',
  60. );
  61. my %api = (
  62. 'curl_easy_cleanup' => 'API',
  63. 'curl_easy_duphandle' => 'API',
  64. 'curl_easy_escape' => 'API',
  65. 'curl_easy_getinfo' => 'API',
  66. 'curl_easy_init' => 'API',
  67. 'curl_easy_pause' => 'API',
  68. 'curl_easy_perform' => 'API',
  69. 'curl_easy_recv' => 'API',
  70. 'curl_easy_reset' => 'API',
  71. 'curl_easy_send' => 'API',
  72. 'curl_easy_setopt' => 'API',
  73. 'curl_easy_strerror' => 'API',
  74. 'curl_easy_unescape' => 'API',
  75. 'curl_easy_upkeep' => 'API',
  76. 'curl_escape' => 'API',
  77. 'curl_formadd' => 'API',
  78. 'curl_formfree' => 'API',
  79. 'curl_formget' => 'API',
  80. 'curl_free' => 'API',
  81. 'curl_getdate' => 'API',
  82. 'curl_getenv' => 'API',
  83. 'curl_global_cleanup' => 'API',
  84. 'curl_global_init' => 'API',
  85. 'curl_global_init_mem' => 'API',
  86. 'curl_global_sslset' => 'API',
  87. 'curl_maprintf' => 'API',
  88. 'curl_mfprintf' => 'API',
  89. 'curl_mime_addpart' => 'API',
  90. 'curl_mime_data' => 'API',
  91. 'curl_mime_data_cb' => 'API',
  92. 'curl_mime_encoder' => 'API',
  93. 'curl_mime_filedata' => 'API',
  94. 'curl_mime_filename' => 'API',
  95. 'curl_mime_free' => 'API',
  96. 'curl_mime_headers' => 'API',
  97. 'curl_mime_init' => 'API',
  98. 'curl_mime_name' => 'API',
  99. 'curl_mime_subparts' => 'API',
  100. 'curl_mime_type' => 'API',
  101. 'curl_mprintf' => 'API',
  102. 'curl_msnprintf' => 'API',
  103. 'curl_msprintf' => 'API',
  104. 'curl_multi_add_handle' => 'API',
  105. 'curl_multi_assign' => 'API',
  106. 'curl_multi_cleanup' => 'API',
  107. 'curl_multi_fdset' => 'API',
  108. 'curl_multi_info_read' => 'API',
  109. 'curl_multi_init' => 'API',
  110. 'curl_multi_perform' => 'API',
  111. 'curl_multi_remove_handle' => 'API',
  112. 'curl_multi_setopt' => 'API',
  113. 'curl_multi_socket' => 'API',
  114. 'curl_multi_socket_action' => 'API',
  115. 'curl_multi_socket_all' => 'API',
  116. 'curl_multi_poll' => 'API',
  117. 'curl_multi_strerror' => 'API',
  118. 'curl_multi_timeout' => 'API',
  119. 'curl_multi_wait' => 'API',
  120. 'curl_multi_wakeup' => 'API',
  121. 'curl_mvaprintf' => 'API',
  122. 'curl_mvfprintf' => 'API',
  123. 'curl_mvprintf' => 'API',
  124. 'curl_mvsnprintf' => 'API',
  125. 'curl_mvsprintf' => 'API',
  126. 'curl_pushheader_byname' => 'API',
  127. 'curl_pushheader_bynum' => 'API',
  128. 'curl_share_cleanup' => 'API',
  129. 'curl_share_init' => 'API',
  130. 'curl_share_setopt' => 'API',
  131. 'curl_share_strerror' => 'API',
  132. 'curl_slist_append' => 'API',
  133. 'curl_slist_free_all' => 'API',
  134. 'curl_strequal' => 'API',
  135. 'curl_strnequal' => 'API',
  136. 'curl_unescape' => 'API',
  137. 'curl_url' => 'API',
  138. 'curl_url_cleanup' => 'API',
  139. 'curl_url_dup' => 'API',
  140. 'curl_url_get' => 'API',
  141. 'curl_url_set' => 'API',
  142. 'curl_version' => 'API',
  143. 'curl_version_info' => 'API',
  144. # the following functions are provided globally in debug builds
  145. 'curl_easy_perform_ev' => 'debug-build',
  146. );
  147. open(N, "nm $file|") ||
  148. die;
  149. my %exist;
  150. my %uses;
  151. my $file;
  152. while (<N>) {
  153. my $l = $_;
  154. chomp $l;
  155. if($l =~ /^([0-9a-z_-]+)\.o:/) {
  156. $file = $1;
  157. }
  158. if($l =~ /^([0-9a-f]+) T (.*)/) {
  159. my ($name)=($2);
  160. #print "Define $name in $file\n";
  161. $file =~ s/^libcurl_la-//;
  162. $exist{$name} = $file;
  163. }
  164. elsif($l =~ /^ U (.*)/) {
  165. my ($name)=($1);
  166. #print "Uses $name in $file\n";
  167. $uses{$name} .= "$file, ";
  168. }
  169. }
  170. close(N);
  171. my $err;
  172. for(sort keys %exist) {
  173. #printf "%s is defined in %s, used by: %s\n", $_, $exist{$_}, $uses{$_};
  174. if(!$uses{$_}) {
  175. # this is a symbol with no "global" user
  176. if($_ =~ /^curl_dbg_/) {
  177. # we ignore the memdebug symbols
  178. }
  179. elsif($_ =~ /^curl_/) {
  180. if(!$api{$_}) {
  181. # not present in the API, or for debug-builds
  182. print STDERR "Bad curl-prefix: $_\n";
  183. $err++;
  184. }
  185. }
  186. elsif($wl{$_}) {
  187. #print "$_ is WL\n";
  188. }
  189. else {
  190. printf "%s is defined in %s, but not used outside\n", $_, $exist{$_};
  191. $err++;
  192. }
  193. }
  194. elsif($_ =~ /^curl_/) {
  195. # global prefix, make sure it is "blessed"
  196. if(!$api{$_}) {
  197. # not present in the API, or for debug-builds
  198. if($_ !~ /^curl_dbg_/) {
  199. # ignore the memdebug symbols
  200. print STDERR "Bad curl-prefix $_\n";
  201. $err++;
  202. }
  203. }
  204. }
  205. }
  206. exit $err;