size.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* size.c -- report size of various sections of an executable file.
  2. Copyright (C) 1991-2015 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. /* Extensions/incompatibilities:
  17. o - BSD output has filenames at the end.
  18. o - BSD output can appear in different radicies.
  19. o - SysV output has less redundant whitespace. Filename comes at end.
  20. o - SysV output doesn't show VMA which is always the same as the PMA.
  21. o - We also handle core files.
  22. o - We also handle archives.
  23. If you write shell scripts which manipulate this info then you may be
  24. out of luck; there's no --compatibility or --pedantic option. */
  25. #include "sysdep.h"
  26. #include "bfd.h"
  27. #include "libiberty.h"
  28. #include "getopt.h"
  29. #include "bucomm.h"
  30. #ifndef BSD_DEFAULT
  31. #define BSD_DEFAULT 1
  32. #endif
  33. /* Program options. */
  34. static enum
  35. {
  36. decimal, octal, hex
  37. }
  38. radix = decimal;
  39. /* 0 means use AT&T-style output. */
  40. static int berkeley_format = BSD_DEFAULT;
  41. static int show_version = 0;
  42. static int show_help = 0;
  43. static int show_totals = 0;
  44. static int show_common = 0;
  45. static bfd_size_type common_size;
  46. static bfd_size_type total_bsssize;
  47. static bfd_size_type total_datasize;
  48. static bfd_size_type total_textsize;
  49. /* Program exit status. */
  50. static int return_code = 0;
  51. static char *target = NULL;
  52. /* Forward declarations. */
  53. static void display_file (char *);
  54. static void rprint_number (int, bfd_size_type);
  55. static void print_sizes (bfd * file);
  56. static void
  57. usage (FILE *stream, int status)
  58. {
  59. fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
  60. fprintf (stream, _(" Displays the sizes of sections inside binary files\n"));
  61. fprintf (stream, _(" If no input file(s) are specified, a.out is assumed\n"));
  62. fprintf (stream, _(" The options are:\n\
  63. -A|-B --format={sysv|berkeley} Select output style (default is %s)\n\
  64. -o|-d|-x --radix={8|10|16} Display numbers in octal, decimal or hex\n\
  65. -t --totals Display the total sizes (Berkeley only)\n\
  66. --common Display total size for *COM* syms\n\
  67. --target=<bfdname> Set the binary file format\n\
  68. @<file> Read options from <file>\n\
  69. -h --help Display this information\n\
  70. -v --version Display the program's version\n\
  71. \n"),
  72. #if BSD_DEFAULT
  73. "berkeley"
  74. #else
  75. "sysv"
  76. #endif
  77. );
  78. list_supported_targets (program_name, stream);
  79. if (REPORT_BUGS_TO[0] && status == 0)
  80. fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  81. exit (status);
  82. }
  83. #define OPTION_FORMAT (200)
  84. #define OPTION_RADIX (OPTION_FORMAT + 1)
  85. #define OPTION_TARGET (OPTION_RADIX + 1)
  86. static struct option long_options[] =
  87. {
  88. {"common", no_argument, &show_common, 1},
  89. {"format", required_argument, 0, OPTION_FORMAT},
  90. {"radix", required_argument, 0, OPTION_RADIX},
  91. {"target", required_argument, 0, OPTION_TARGET},
  92. {"totals", no_argument, &show_totals, 1},
  93. {"version", no_argument, &show_version, 1},
  94. {"help", no_argument, &show_help, 1},
  95. {0, no_argument, 0, 0}
  96. };
  97. int main (int, char **);
  98. int
  99. main (int argc, char **argv)
  100. {
  101. int temp;
  102. int c;
  103. #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  104. setlocale (LC_MESSAGES, "");
  105. #endif
  106. #if defined (HAVE_SETLOCALE)
  107. setlocale (LC_CTYPE, "");
  108. #endif
  109. bindtextdomain (PACKAGE, LOCALEDIR);
  110. textdomain (PACKAGE);
  111. program_name = *argv;
  112. xmalloc_set_program_name (program_name);
  113. bfd_set_error_program_name (program_name);
  114. expandargv (&argc, &argv);
  115. bfd_init ();
  116. set_default_bfd_target ();
  117. while ((c = getopt_long (argc, argv, "ABHhVvdfotx", long_options,
  118. (int *) 0)) != EOF)
  119. switch (c)
  120. {
  121. case OPTION_FORMAT:
  122. switch (*optarg)
  123. {
  124. case 'B':
  125. case 'b':
  126. berkeley_format = 1;
  127. break;
  128. case 'S':
  129. case 's':
  130. berkeley_format = 0;
  131. break;
  132. default:
  133. non_fatal (_("invalid argument to --format: %s"), optarg);
  134. usage (stderr, 1);
  135. }
  136. break;
  137. case OPTION_TARGET:
  138. target = optarg;
  139. break;
  140. case OPTION_RADIX:
  141. #ifdef ANSI_LIBRARIES
  142. temp = strtol (optarg, NULL, 10);
  143. #else
  144. temp = atol (optarg);
  145. #endif
  146. switch (temp)
  147. {
  148. case 10:
  149. radix = decimal;
  150. break;
  151. case 8:
  152. radix = octal;
  153. break;
  154. case 16:
  155. radix = hex;
  156. break;
  157. default:
  158. non_fatal (_("Invalid radix: %s\n"), optarg);
  159. usage (stderr, 1);
  160. }
  161. break;
  162. case 'A':
  163. berkeley_format = 0;
  164. break;
  165. case 'B':
  166. berkeley_format = 1;
  167. break;
  168. case 'v':
  169. case 'V':
  170. show_version = 1;
  171. break;
  172. case 'd':
  173. radix = decimal;
  174. break;
  175. case 'x':
  176. radix = hex;
  177. break;
  178. case 'o':
  179. radix = octal;
  180. break;
  181. case 't':
  182. show_totals = 1;
  183. break;
  184. case 'f': /* FIXME : For sysv68, `-f' means `full format', i.e.
  185. `[fname:] M(.text) + N(.data) + O(.bss) + P(.comment) = Q'
  186. where `fname: ' appears only if there are >= 2 input files,
  187. and M, N, O, P, Q are expressed in decimal by default,
  188. hexa or octal if requested by `-x' or `-o'.
  189. Just to make things interesting, Solaris also accepts -f,
  190. which prints out the size of each allocatable section, the
  191. name of the section, and the total of the section sizes. */
  192. /* For the moment, accept `-f' silently, and ignore it. */
  193. break;
  194. case 0:
  195. break;
  196. case 'h':
  197. case 'H':
  198. case '?':
  199. usage (stderr, 1);
  200. }
  201. if (show_version)
  202. print_version ("size");
  203. if (show_help)
  204. usage (stdout, 0);
  205. if (optind == argc)
  206. display_file ("a.out");
  207. else
  208. for (; optind < argc;)
  209. display_file (argv[optind++]);
  210. if (show_totals && berkeley_format)
  211. {
  212. bfd_size_type total = total_textsize + total_datasize + total_bsssize;
  213. rprint_number (7, total_textsize);
  214. putchar('\t');
  215. rprint_number (7, total_datasize);
  216. putchar('\t');
  217. rprint_number (7, total_bsssize);
  218. printf (((radix == octal) ? "\t%7lo\t%7lx\t" : "\t%7lu\t%7lx\t"),
  219. (unsigned long) total, (unsigned long) total);
  220. fputs ("(TOTALS)\n", stdout);
  221. }
  222. return return_code;
  223. }
  224. /* Total size required for common symbols in ABFD. */
  225. static void
  226. calculate_common_size (bfd *abfd)
  227. {
  228. asymbol **syms = NULL;
  229. long storage, symcount;
  230. common_size = 0;
  231. if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC | HAS_SYMS)) != HAS_SYMS)
  232. return;
  233. storage = bfd_get_symtab_upper_bound (abfd);
  234. if (storage < 0)
  235. bfd_fatal (bfd_get_filename (abfd));
  236. if (storage)
  237. syms = (asymbol **) xmalloc (storage);
  238. symcount = bfd_canonicalize_symtab (abfd, syms);
  239. if (symcount < 0)
  240. bfd_fatal (bfd_get_filename (abfd));
  241. while (--symcount >= 0)
  242. {
  243. asymbol *sym = syms[symcount];
  244. if (bfd_is_com_section (sym->section)
  245. && (sym->flags & BSF_SECTION_SYM) == 0)
  246. common_size += sym->value;
  247. }
  248. free (syms);
  249. }
  250. /* Display stats on file or archive member ABFD. */
  251. static void
  252. display_bfd (bfd *abfd)
  253. {
  254. char **matching;
  255. if (bfd_check_format (abfd, bfd_archive))
  256. /* An archive within an archive. */
  257. return;
  258. if (bfd_check_format_matches (abfd, bfd_object, &matching))
  259. {
  260. print_sizes (abfd);
  261. printf ("\n");
  262. return;
  263. }
  264. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  265. {
  266. bfd_nonfatal (bfd_get_filename (abfd));
  267. list_matching_formats (matching);
  268. free (matching);
  269. return_code = 3;
  270. return;
  271. }
  272. if (bfd_check_format_matches (abfd, bfd_core, &matching))
  273. {
  274. const char *core_cmd;
  275. print_sizes (abfd);
  276. fputs (" (core file", stdout);
  277. core_cmd = bfd_core_file_failing_command (abfd);
  278. if (core_cmd)
  279. printf (" invoked as %s", core_cmd);
  280. puts (")\n");
  281. return;
  282. }
  283. bfd_nonfatal (bfd_get_filename (abfd));
  284. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  285. {
  286. list_matching_formats (matching);
  287. free (matching);
  288. }
  289. return_code = 3;
  290. }
  291. static void
  292. display_archive (bfd *file)
  293. {
  294. bfd *arfile = (bfd *) NULL;
  295. bfd *last_arfile = (bfd *) NULL;
  296. for (;;)
  297. {
  298. bfd_set_error (bfd_error_no_error);
  299. arfile = bfd_openr_next_archived_file (file, arfile);
  300. if (arfile == NULL)
  301. {
  302. if (bfd_get_error () != bfd_error_no_more_archived_files)
  303. {
  304. bfd_nonfatal (bfd_get_filename (file));
  305. return_code = 2;
  306. }
  307. break;
  308. }
  309. display_bfd (arfile);
  310. if (last_arfile != NULL)
  311. {
  312. bfd_close (last_arfile);
  313. /* PR 17512: file: a244edbc. */
  314. if (last_arfile == arfile)
  315. return;
  316. }
  317. last_arfile = arfile;
  318. }
  319. if (last_arfile != NULL)
  320. bfd_close (last_arfile);
  321. }
  322. static void
  323. display_file (char *filename)
  324. {
  325. bfd *file;
  326. if (get_file_size (filename) < 1)
  327. {
  328. return_code = 1;
  329. return;
  330. }
  331. file = bfd_openr (filename, target);
  332. if (file == NULL)
  333. {
  334. bfd_nonfatal (filename);
  335. return_code = 1;
  336. return;
  337. }
  338. if (bfd_check_format (file, bfd_archive))
  339. display_archive (file);
  340. else
  341. display_bfd (file);
  342. if (!bfd_close (file))
  343. {
  344. bfd_nonfatal (filename);
  345. return_code = 1;
  346. return;
  347. }
  348. }
  349. static int
  350. size_number (bfd_size_type num)
  351. {
  352. char buffer[40];
  353. sprintf (buffer,
  354. (radix == decimal ? "%" BFD_VMA_FMT "u" :
  355. ((radix == octal) ? "0%" BFD_VMA_FMT "o" : "0x%" BFD_VMA_FMT "x")),
  356. num);
  357. return strlen (buffer);
  358. }
  359. static void
  360. rprint_number (int width, bfd_size_type num)
  361. {
  362. char buffer[40];
  363. sprintf (buffer,
  364. (radix == decimal ? "%" BFD_VMA_FMT "u" :
  365. ((radix == octal) ? "0%" BFD_VMA_FMT "o" : "0x%" BFD_VMA_FMT "x")),
  366. num);
  367. printf ("%*s", width, buffer);
  368. }
  369. static bfd_size_type bsssize;
  370. static bfd_size_type datasize;
  371. static bfd_size_type textsize;
  372. static void
  373. berkeley_sum (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec,
  374. void *ignore ATTRIBUTE_UNUSED)
  375. {
  376. flagword flags;
  377. bfd_size_type size;
  378. flags = bfd_get_section_flags (abfd, sec);
  379. if ((flags & SEC_ALLOC) == 0)
  380. return;
  381. size = bfd_get_section_size (sec);
  382. if ((flags & SEC_CODE) != 0 || (flags & SEC_READONLY) != 0)
  383. textsize += size;
  384. else if ((flags & SEC_HAS_CONTENTS) != 0)
  385. datasize += size;
  386. else
  387. bsssize += size;
  388. }
  389. static void
  390. print_berkeley_format (bfd *abfd)
  391. {
  392. static int files_seen = 0;
  393. bfd_size_type total;
  394. bsssize = 0;
  395. datasize = 0;
  396. textsize = 0;
  397. bfd_map_over_sections (abfd, berkeley_sum, NULL);
  398. bsssize += common_size;
  399. if (files_seen++ == 0)
  400. puts ((radix == octal) ? " text\t data\t bss\t oct\t hex\tfilename" :
  401. " text\t data\t bss\t dec\t hex\tfilename");
  402. total = textsize + datasize + bsssize;
  403. if (show_totals)
  404. {
  405. total_textsize += textsize;
  406. total_datasize += datasize;
  407. total_bsssize += bsssize;
  408. }
  409. rprint_number (7, textsize);
  410. putchar ('\t');
  411. rprint_number (7, datasize);
  412. putchar ('\t');
  413. rprint_number (7, bsssize);
  414. printf (((radix == octal) ? "\t%7lo\t%7lx\t" : "\t%7lu\t%7lx\t"),
  415. (unsigned long) total, (unsigned long) total);
  416. fputs (bfd_get_filename (abfd), stdout);
  417. if (bfd_my_archive (abfd))
  418. printf (" (ex %s)", bfd_get_filename (bfd_my_archive (abfd)));
  419. }
  420. /* I REALLY miss lexical functions! */
  421. bfd_size_type svi_total = 0;
  422. bfd_vma svi_maxvma = 0;
  423. int svi_namelen = 0;
  424. int svi_vmalen = 0;
  425. int svi_sizelen = 0;
  426. static void
  427. sysv_internal_sizer (bfd *file ATTRIBUTE_UNUSED, sec_ptr sec,
  428. void *ignore ATTRIBUTE_UNUSED)
  429. {
  430. bfd_size_type size = bfd_section_size (file, sec);
  431. if ( ! bfd_is_abs_section (sec)
  432. && ! bfd_is_com_section (sec)
  433. && ! bfd_is_und_section (sec))
  434. {
  435. int namelen = strlen (bfd_section_name (file, sec));
  436. if (namelen > svi_namelen)
  437. svi_namelen = namelen;
  438. svi_total += size;
  439. if (bfd_section_vma (file, sec) > svi_maxvma)
  440. svi_maxvma = bfd_section_vma (file, sec);
  441. }
  442. }
  443. static void
  444. sysv_one_line (const char *name, bfd_size_type size, bfd_vma vma)
  445. {
  446. printf ("%-*s ", svi_namelen, name);
  447. rprint_number (svi_sizelen, size);
  448. printf (" ");
  449. rprint_number (svi_vmalen, vma);
  450. printf ("\n");
  451. }
  452. static void
  453. sysv_internal_printer (bfd *file ATTRIBUTE_UNUSED, sec_ptr sec,
  454. void *ignore ATTRIBUTE_UNUSED)
  455. {
  456. bfd_size_type size = bfd_section_size (file, sec);
  457. if ( ! bfd_is_abs_section (sec)
  458. && ! bfd_is_com_section (sec)
  459. && ! bfd_is_und_section (sec))
  460. {
  461. svi_total += size;
  462. sysv_one_line (bfd_section_name (file, sec),
  463. size,
  464. bfd_section_vma (file, sec));
  465. }
  466. }
  467. static void
  468. print_sysv_format (bfd *file)
  469. {
  470. /* Size all of the columns. */
  471. svi_total = 0;
  472. svi_maxvma = 0;
  473. svi_namelen = 0;
  474. bfd_map_over_sections (file, sysv_internal_sizer, NULL);
  475. if (show_common)
  476. {
  477. if (svi_namelen < (int) sizeof ("*COM*") - 1)
  478. svi_namelen = sizeof ("*COM*") - 1;
  479. svi_total += common_size;
  480. }
  481. svi_vmalen = size_number ((bfd_size_type)svi_maxvma);
  482. if ((size_t) svi_vmalen < sizeof ("addr") - 1)
  483. svi_vmalen = sizeof ("addr")-1;
  484. svi_sizelen = size_number (svi_total);
  485. if ((size_t) svi_sizelen < sizeof ("size") - 1)
  486. svi_sizelen = sizeof ("size")-1;
  487. svi_total = 0;
  488. printf ("%s ", bfd_get_filename (file));
  489. if (bfd_my_archive (file))
  490. printf (" (ex %s)", bfd_get_filename (bfd_my_archive (file)));
  491. printf (":\n%-*s %*s %*s\n", svi_namelen, "section",
  492. svi_sizelen, "size", svi_vmalen, "addr");
  493. bfd_map_over_sections (file, sysv_internal_printer, NULL);
  494. if (show_common)
  495. {
  496. svi_total += common_size;
  497. sysv_one_line ("*COM*", common_size, 0);
  498. }
  499. printf ("%-*s ", svi_namelen, "Total");
  500. rprint_number (svi_sizelen, svi_total);
  501. printf ("\n\n");
  502. }
  503. static void
  504. print_sizes (bfd *file)
  505. {
  506. if (show_common)
  507. calculate_common_size (file);
  508. if (berkeley_format)
  509. print_berkeley_format (file);
  510. else
  511. print_sysv_format (file);
  512. }