singleuse.pl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2019 - 2021, 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_easy_option_by_id' => 'API',
  77. 'curl_easy_option_by_name' => 'API',
  78. 'curl_easy_option_next' => 'API',
  79. 'curl_escape' => 'API',
  80. 'curl_formadd' => 'API',
  81. 'curl_formfree' => 'API',
  82. 'curl_formget' => 'API',
  83. 'curl_free' => 'API',
  84. 'curl_getdate' => 'API',
  85. 'curl_getenv' => 'API',
  86. 'curl_global_cleanup' => 'API',
  87. 'curl_global_init' => 'API',
  88. 'curl_global_init_mem' => 'API',
  89. 'curl_global_sslset' => 'API',
  90. 'curl_maprintf' => 'API',
  91. 'curl_mfprintf' => 'API',
  92. 'curl_mime_addpart' => 'API',
  93. 'curl_mime_data' => 'API',
  94. 'curl_mime_data_cb' => 'API',
  95. 'curl_mime_encoder' => 'API',
  96. 'curl_mime_filedata' => 'API',
  97. 'curl_mime_filename' => 'API',
  98. 'curl_mime_free' => 'API',
  99. 'curl_mime_headers' => 'API',
  100. 'curl_mime_init' => 'API',
  101. 'curl_mime_name' => 'API',
  102. 'curl_mime_subparts' => 'API',
  103. 'curl_mime_type' => 'API',
  104. 'curl_mprintf' => 'API',
  105. 'curl_msnprintf' => 'API',
  106. 'curl_msprintf' => 'API',
  107. 'curl_multi_add_handle' => 'API',
  108. 'curl_multi_assign' => 'API',
  109. 'curl_multi_cleanup' => 'API',
  110. 'curl_multi_fdset' => 'API',
  111. 'curl_multi_info_read' => 'API',
  112. 'curl_multi_init' => 'API',
  113. 'curl_multi_perform' => 'API',
  114. 'curl_multi_remove_handle' => 'API',
  115. 'curl_multi_setopt' => 'API',
  116. 'curl_multi_socket' => 'API',
  117. 'curl_multi_socket_action' => 'API',
  118. 'curl_multi_socket_all' => 'API',
  119. 'curl_multi_poll' => 'API',
  120. 'curl_multi_strerror' => 'API',
  121. 'curl_multi_timeout' => 'API',
  122. 'curl_multi_wait' => 'API',
  123. 'curl_multi_wakeup' => 'API',
  124. 'curl_mvaprintf' => 'API',
  125. 'curl_mvfprintf' => 'API',
  126. 'curl_mvprintf' => 'API',
  127. 'curl_mvsnprintf' => 'API',
  128. 'curl_mvsprintf' => 'API',
  129. 'curl_pushheader_byname' => 'API',
  130. 'curl_pushheader_bynum' => 'API',
  131. 'curl_share_cleanup' => 'API',
  132. 'curl_share_init' => 'API',
  133. 'curl_share_setopt' => 'API',
  134. 'curl_share_strerror' => 'API',
  135. 'curl_slist_append' => 'API',
  136. 'curl_slist_free_all' => 'API',
  137. 'curl_strequal' => 'API',
  138. 'curl_strnequal' => 'API',
  139. 'curl_unescape' => 'API',
  140. 'curl_url' => 'API',
  141. 'curl_url_cleanup' => 'API',
  142. 'curl_url_dup' => 'API',
  143. 'curl_url_get' => 'API',
  144. 'curl_url_set' => 'API',
  145. 'curl_url_strerror' => 'API',
  146. 'curl_version' => 'API',
  147. 'curl_version_info' => 'API',
  148. # the following functions are provided globally in debug builds
  149. 'curl_easy_perform_ev' => 'debug-build',
  150. );
  151. open(N, "nm $file|") ||
  152. die;
  153. my %exist;
  154. my %uses;
  155. my $file;
  156. while (<N>) {
  157. my $l = $_;
  158. chomp $l;
  159. if($l =~ /^([0-9a-z_-]+)\.o:/) {
  160. $file = $1;
  161. }
  162. if($l =~ /^([0-9a-f]+) T (.*)/) {
  163. my ($name)=($2);
  164. #print "Define $name in $file\n";
  165. $file =~ s/^libcurl_la-//;
  166. $exist{$name} = $file;
  167. }
  168. elsif($l =~ /^ U (.*)/) {
  169. my ($name)=($1);
  170. #print "Uses $name in $file\n";
  171. $uses{$name} .= "$file, ";
  172. }
  173. }
  174. close(N);
  175. my $err;
  176. for(sort keys %exist) {
  177. #printf "%s is defined in %s, used by: %s\n", $_, $exist{$_}, $uses{$_};
  178. if(!$uses{$_}) {
  179. # this is a symbol with no "global" user
  180. if($_ =~ /^curl_dbg_/) {
  181. # we ignore the memdebug symbols
  182. }
  183. elsif($_ =~ /^curl_/) {
  184. if(!$api{$_}) {
  185. # not present in the API, or for debug-builds
  186. print STDERR "Bad curl-prefix: $_\n";
  187. $err++;
  188. }
  189. }
  190. elsif($wl{$_}) {
  191. #print "$_ is WL\n";
  192. }
  193. else {
  194. printf "%s is defined in %s, but not used outside\n", $_, $exist{$_};
  195. $err++;
  196. }
  197. }
  198. elsif($_ =~ /^curl_/) {
  199. # global prefix, make sure it is "blessed"
  200. if(!$api{$_}) {
  201. # not present in the API, or for debug-builds
  202. if($_ !~ /^curl_dbg_/) {
  203. # ignore the memdebug symbols
  204. print STDERR "Bad curl-prefix $_\n";
  205. $err++;
  206. }
  207. }
  208. }
  209. }
  210. exit $err;