singleuse.pl 7.1 KB

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