1 /* size.c -- report size of various sections of an executable file.
2 Copyright 1991, 92, 93, 94 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Extensions/incompatibilities:
21 o - BSD output has filenames at the end.
22 o - BSD output can appear in different radicies.
23 o - SysV output has less redundant whitespace. Filename comes at end.
24 o - SysV output doesn't show VMA which is always the same as the PMA.
25 o - We also handle core files.
26 o - We also handle archives.
27 If you write shell scripts which manipulate this info then you may be
28 out of luck; there's no --compatibility or --pedantic option.
40 /* Program options. */
46 int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output. */
50 /* Program exit status. */
54 extern char *program_version;
57 /* Forward declarations */
59 static void display_file PARAMS ((char *filename));
61 static void print_sizes PARAMS ((bfd * file));
63 static void berkeley_sum PARAMS ((bfd *, sec_ptr, PTR));
66 usage (stream, status)
71 Usage: %s [-ABdoxV] [--format=berkeley|sysv] [--radix=8|10|16]\n\
72 [--target=bfdname] [--version] [--help] [file...]\n", program_name);
74 fputs ("default is --format=berkeley\n", stream);
76 fputs ("default is --format=sysv\n", stream);
81 struct option long_options[] =
83 {"format", required_argument, 0, 200},
84 {"radix", required_argument, 0, 201},
85 {"target", required_argument, 0, 202},
86 {"version", no_argument, &show_version, 1},
87 {"help", no_argument, &show_help, 1},
88 {0, no_argument, 0, 0}
103 while ((c = getopt_long (argc, argv, "ABVdox", long_options,
107 case 200: /* --format */
119 fprintf (stderr, "invalid argument to --format: %s\n", optarg);
124 case 202: /* --target */
128 case 201: /* --radix */
129 #ifdef ANSI_LIBRARIES
130 temp = strtol (optarg, NULL, 10);
132 temp = atol (optarg);
146 printf ("Invalid radix: %s\n", optarg);
177 printf ("GNU %s version %s\n", program_name, program_version);
184 display_file ("a.out");
186 for (; optind < argc;)
187 display_file (argv[optind++]);
192 /* Display stats on file or archive member ABFD. */
200 if (bfd_check_format (abfd, bfd_archive))
201 /* An archive within an archive. */
204 if (bfd_check_format_matches (abfd, bfd_object, &matching))
211 if (bfd_error == file_ambiguously_recognized)
213 bfd_nonfatal (bfd_get_filename (abfd));
214 list_matching_formats (matching);
220 if (bfd_check_format_matches (abfd, bfd_core, &matching))
222 CONST char *core_cmd;
225 fputs (" (core file", stdout);
227 core_cmd = bfd_core_file_failing_command (abfd);
229 printf (" invoked as %s", core_cmd);
235 bfd_nonfatal (bfd_get_filename (abfd));
237 if (bfd_error == file_ambiguously_recognized)
239 list_matching_formats (matching);
247 display_archive (file)
250 bfd *arfile = (bfd *) NULL;
254 bfd_error = no_error;
256 arfile = bfd_openr_next_archived_file (file, arfile);
259 if (bfd_error != no_more_archived_files)
261 bfd_nonfatal (bfd_get_filename (file));
267 display_bfd (arfile);
268 /* Don't close the archive elements; we need them for next_archive */
273 display_file (filename)
276 bfd *file = bfd_openr (filename, target);
279 bfd_nonfatal (filename);
284 if (bfd_check_format (file, bfd_archive) == true)
285 display_archive (file);
289 if (bfd_close (file) == false)
291 bfd_nonfatal (filename);
297 /* This is what lexical functions are for. */
300 lprint_number (width, num)
304 printf ((radix == decimal ? "%-*lu\t" :
305 ((radix == octal) ? "%-*lo\t" : "%-*lx\t")),
306 width, (unsigned long) num);
310 rprint_number (width, num)
314 printf ((radix == decimal ? "%*lu\t" :
315 ((radix == octal) ? "%*lo\t" : "%*lx\t")),
316 width, (unsigned long) num);
319 static bfd_size_type bsssize;
320 static bfd_size_type datasize;
321 static bfd_size_type textsize;
324 berkeley_sum (abfd, sec, ignore)
331 size = bfd_get_section_size_before_reloc (sec);
332 if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
334 else if (bfd_get_section_flags (abfd, sec) & SEC_DATA)
336 else if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
341 print_berkeley_format (abfd)
344 static int files_seen = 0;
351 bfd_map_over_sections (abfd, berkeley_sum, (PTR) NULL);
353 if (files_seen++ == 0)
355 /* Intel doesn't like bss/stk because they don't have core files. */
356 puts ((radix == octal) ? "text\tdata\tbss/stk\toct\thex\tfilename" :
357 "text\tdata\tbss/stk\tdec\thex\tfilename");
359 puts ((radix == octal) ? "text\tdata\tbss\toct\thex\tfilename" :
360 "text\tdata\tbss\tdec\thex\tfilename");
363 total = textsize + datasize + bsssize;
365 lprint_number (7, textsize);
366 lprint_number (7, datasize);
367 lprint_number (7, bsssize);
368 printf (((radix == octal) ? "%-7lo\t%-7lx\t" : "%-7lu\t%-7lx\t"),
369 (unsigned long) total, (unsigned long) total);
371 fputs (bfd_get_filename (abfd), stdout);
372 if (bfd_my_archive (abfd))
373 printf (" (ex %s)", bfd_get_filename (bfd_my_archive (abfd)));
376 /* I REALLY miss lexical functions! */
377 bfd_size_type svi_total = 0;
380 sysv_internal_printer (file, sec, ignore)
385 bfd_size_type size = bfd_section_size (file, sec);
386 if (sec != &bfd_abs_section
387 && !bfd_is_com_section (sec)
388 && sec != &bfd_und_section)
392 printf ("%-12s", bfd_section_name (file, sec));
393 rprint_number (8, size);
395 rprint_number (8, bfd_section_vma (file, sec));
401 print_sysv_format (file)
406 printf ("%s ", bfd_get_filename (file));
407 if (file->my_archive)
408 printf (" (ex %s)", file->my_archive->filename);
410 puts (":\nsection\t\tsize\t addr");
411 bfd_map_over_sections (file, sysv_internal_printer, (PTR) NULL);
414 rprint_number (8, svi_total);
423 print_berkeley_format (file);
425 print_sysv_format (file);