1 /* size.c -- report size of various sections of an executable file.
2 Copyright 1991, 92, 93, 94, 95, 96, 97, 1998 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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.
34 #include "libiberty.h"
40 /* Program options. */
46 int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output. */
50 /* Program exit status. */
53 static char *target = NULL;
55 /* Static declarations */
57 static void usage PARAMS ((FILE *, int));
58 static void display_file PARAMS ((char *filename));
59 static void display_bfd PARAMS ((bfd *));
60 static void display_archive PARAMS ((bfd *));
61 static int size_number PARAMS ((bfd_size_type));
63 static void lprint_number PARAMS ((int, bfd_size_type));
65 static void rprint_number PARAMS ((int, bfd_size_type));
66 static void print_berkeley_format PARAMS ((bfd *));
67 static void sysv_internal_sizer PARAMS ((bfd *, asection *, PTR));
68 static void sysv_internal_printer PARAMS ((bfd *, asection *, PTR));
69 static void print_sysv_format PARAMS ((bfd *));
70 static void print_sizes PARAMS ((bfd * file));
71 static void berkeley_sum PARAMS ((bfd *, sec_ptr, PTR));
74 usage (stream, status)
79 Usage: %s [-ABdoxV] [--format=berkeley|sysv] [--radix=8|10|16]\n\
80 [--target=bfdname] [--version] [--help] [file...]\n"), program_name);
82 fputs (_("default is --format=berkeley\n"), stream);
84 fputs (_("default is --format=sysv\n"), stream);
86 list_supported_targets (program_name, stream);
88 fprintf (stream, _("Report bugs to bug-gnu-utils@gnu.org\n"));
92 struct option long_options[] =
94 {"format", required_argument, 0, 200},
95 {"radix", required_argument, 0, 201},
96 {"target", required_argument, 0, 202},
97 {"version", no_argument, &show_version, 1},
98 {"help", no_argument, &show_help, 1},
99 {0, no_argument, 0, 0}
110 program_name = *argv;
111 xmalloc_set_program_name (program_name);
114 set_default_bfd_target ();
116 while ((c = getopt_long (argc, argv, "ABVdox", long_options,
120 case 200: /* --format */
132 fprintf (stderr, _("invalid argument to --format: %s\n"), optarg);
137 case 202: /* --target */
141 case 201: /* --radix */
142 #ifdef ANSI_LIBRARIES
143 temp = strtol (optarg, NULL, 10);
145 temp = atol (optarg);
159 printf (_("Invalid radix: %s\n"), optarg);
189 print_version ("size");
194 display_file ("a.out");
196 for (; optind < argc;)
197 display_file (argv[optind++]);
202 /* Display stats on file or archive member ABFD. */
210 if (bfd_check_format (abfd, bfd_archive))
211 /* An archive within an archive. */
214 if (bfd_check_format_matches (abfd, bfd_object, &matching))
221 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
223 bfd_nonfatal (bfd_get_filename (abfd));
224 list_matching_formats (matching);
230 if (bfd_check_format_matches (abfd, bfd_core, &matching))
232 CONST char *core_cmd;
235 fputs (" (core file", stdout);
237 core_cmd = bfd_core_file_failing_command (abfd);
239 printf (" invoked as %s", core_cmd);
245 bfd_nonfatal (bfd_get_filename (abfd));
247 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
249 list_matching_formats (matching);
257 display_archive (file)
260 bfd *arfile = (bfd *) NULL;
264 bfd_set_error (bfd_error_no_error);
266 arfile = bfd_openr_next_archived_file (file, arfile);
269 if (bfd_get_error () != bfd_error_no_more_archived_files)
271 bfd_nonfatal (bfd_get_filename (file));
277 display_bfd (arfile);
278 /* Don't close the archive elements; we need them for next_archive */
283 display_file (filename)
286 bfd *file = bfd_openr (filename, target);
289 bfd_nonfatal (filename);
294 if (bfd_check_format (file, bfd_archive) == true)
295 display_archive (file);
299 if (bfd_close (file) == false)
301 bfd_nonfatal (filename);
307 /* This is what lexical functions are for. */
315 (radix == decimal ? "%lu" :
316 ((radix == octal) ? "0%lo" : "0x%lx")),
317 (unsigned long) num);
319 return strlen (buffer);
324 /* This is not used. */
327 lprint_number (width, num)
333 (radix == decimal ? "%lu" :
334 ((radix == octal) ? "0%lo" : "0x%lx")),
335 (unsigned long) num);
337 printf ("%-*s", width, buffer);
343 rprint_number (width, num)
349 (radix == decimal ? "%lu" :
350 ((radix == octal) ? "0%lo" : "0x%lx")),
351 (unsigned long) num);
353 printf ("%*s", width, buffer);
356 static bfd_size_type bsssize;
357 static bfd_size_type datasize;
358 static bfd_size_type textsize;
361 berkeley_sum (abfd, sec, ignore)
369 flags = bfd_get_section_flags (abfd, sec);
370 if ((flags & SEC_ALLOC) == 0)
373 size = bfd_get_section_size_before_reloc (sec);
374 if ((flags & SEC_CODE) != 0 || (flags & SEC_READONLY) != 0)
376 else if ((flags & SEC_HAS_CONTENTS) != 0)
383 print_berkeley_format (abfd)
386 static int files_seen = 0;
393 bfd_map_over_sections (abfd, berkeley_sum, (PTR) NULL);
395 if (files_seen++ == 0)
397 /* Intel doesn't like bss/stk because they don't have core files. */
398 puts ((radix == octal) ? " text\t data\tbss/stk\t oct\t hex\tfilename" :
399 " text\t data\tbss/stk\t dec\t hex\tfilename");
401 puts ((radix == octal) ? " text\t data\t bss\t oct\t hex\tfilename" :
402 " text\t data\t bss\t dec\t hex\tfilename");
405 total = textsize + datasize + bsssize;
407 rprint_number (7, textsize);
409 rprint_number (7, datasize);
411 rprint_number (7, bsssize);
412 printf (((radix == octal) ? "\t%7lo\t%7lx\t" : "\t%7lu\t%7lx\t"),
413 (unsigned long) total, (unsigned long) total);
415 fputs (bfd_get_filename (abfd), stdout);
416 if (bfd_my_archive (abfd))
417 printf (" (ex %s)", bfd_get_filename (bfd_my_archive (abfd)));
420 /* I REALLY miss lexical functions! */
421 bfd_size_type svi_total = 0;
422 bfd_vma svi_maxvma = 0;
428 sysv_internal_sizer (file, sec, ignore)
433 bfd_size_type size = bfd_section_size (file, sec);
434 if (!bfd_is_abs_section (sec)
435 && !bfd_is_com_section (sec)
436 && !bfd_is_und_section (sec))
438 int namelen = strlen (bfd_section_name (file, sec));
439 if (namelen > svi_namelen)
440 svi_namelen = namelen;
443 if (bfd_section_vma (file, sec) > svi_maxvma)
444 svi_maxvma = bfd_section_vma (file, sec);
449 sysv_internal_printer (file, sec, ignore)
454 bfd_size_type size = bfd_section_size (file, sec);
455 if (!bfd_is_abs_section (sec)
456 && !bfd_is_com_section (sec)
457 && !bfd_is_und_section (sec))
461 printf ("%-*s ", svi_namelen, bfd_section_name (file, sec));
462 rprint_number (svi_sizelen, size);
464 rprint_number (svi_vmalen, bfd_section_vma (file, sec));
470 print_sysv_format (file)
473 /* size all of the columns */
477 bfd_map_over_sections (file, sysv_internal_sizer, (PTR) NULL);
478 svi_vmalen = size_number ((bfd_size_type)svi_maxvma);
479 if ((size_t) svi_vmalen < sizeof ("addr") - 1)
480 svi_vmalen = sizeof ("addr")-1;
482 svi_sizelen = size_number (svi_total);
483 if ((size_t) svi_sizelen < sizeof ("size") - 1)
484 svi_sizelen = sizeof ("size")-1;
487 printf ("%s ", bfd_get_filename (file));
488 if (bfd_my_archive (file))
489 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, (PTR) NULL);
495 printf ("%-*s ", svi_namelen, "Total");
496 rprint_number (svi_sizelen, svi_total);
505 print_berkeley_format (file);
507 print_sysv_format (file);