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 setlocale (LC_MESSAGES, "");
111 bindtextdomain (PACKAGE, LOCALEDIR);
112 textdomain (PACKAGE);
114 program_name = *argv;
115 xmalloc_set_program_name (program_name);
118 set_default_bfd_target ();
120 while ((c = getopt_long (argc, argv, "ABVdox", long_options,
124 case 200: /* --format */
136 fprintf (stderr, _("invalid argument to --format: %s\n"), optarg);
141 case 202: /* --target */
145 case 201: /* --radix */
146 #ifdef ANSI_LIBRARIES
147 temp = strtol (optarg, NULL, 10);
149 temp = atol (optarg);
163 printf (_("Invalid radix: %s\n"), optarg);
193 print_version ("size");
198 display_file ("a.out");
200 for (; optind < argc;)
201 display_file (argv[optind++]);
206 /* Display stats on file or archive member ABFD. */
214 if (bfd_check_format (abfd, bfd_archive))
215 /* An archive within an archive. */
218 if (bfd_check_format_matches (abfd, bfd_object, &matching))
225 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
227 bfd_nonfatal (bfd_get_filename (abfd));
228 list_matching_formats (matching);
234 if (bfd_check_format_matches (abfd, bfd_core, &matching))
236 CONST char *core_cmd;
239 fputs (" (core file", stdout);
241 core_cmd = bfd_core_file_failing_command (abfd);
243 printf (" invoked as %s", core_cmd);
249 bfd_nonfatal (bfd_get_filename (abfd));
251 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
253 list_matching_formats (matching);
261 display_archive (file)
264 bfd *arfile = (bfd *) NULL;
268 bfd_set_error (bfd_error_no_error);
270 arfile = bfd_openr_next_archived_file (file, arfile);
273 if (bfd_get_error () != bfd_error_no_more_archived_files)
275 bfd_nonfatal (bfd_get_filename (file));
281 display_bfd (arfile);
282 /* Don't close the archive elements; we need them for next_archive */
287 display_file (filename)
290 bfd *file = bfd_openr (filename, target);
293 bfd_nonfatal (filename);
298 if (bfd_check_format (file, bfd_archive) == true)
299 display_archive (file);
303 if (bfd_close (file) == false)
305 bfd_nonfatal (filename);
311 /* This is what lexical functions are for. */
319 (radix == decimal ? "%lu" :
320 ((radix == octal) ? "0%lo" : "0x%lx")),
321 (unsigned long) num);
323 return strlen (buffer);
328 /* This is not used. */
331 lprint_number (width, num)
337 (radix == decimal ? "%lu" :
338 ((radix == octal) ? "0%lo" : "0x%lx")),
339 (unsigned long) num);
341 printf ("%-*s", width, buffer);
347 rprint_number (width, num)
353 (radix == decimal ? "%lu" :
354 ((radix == octal) ? "0%lo" : "0x%lx")),
355 (unsigned long) num);
357 printf ("%*s", width, buffer);
360 static bfd_size_type bsssize;
361 static bfd_size_type datasize;
362 static bfd_size_type textsize;
365 berkeley_sum (abfd, sec, ignore)
373 flags = bfd_get_section_flags (abfd, sec);
374 if ((flags & SEC_ALLOC) == 0)
377 size = bfd_get_section_size_before_reloc (sec);
378 if ((flags & SEC_CODE) != 0 || (flags & SEC_READONLY) != 0)
380 else if ((flags & SEC_HAS_CONTENTS) != 0)
387 print_berkeley_format (abfd)
390 static int files_seen = 0;
397 bfd_map_over_sections (abfd, berkeley_sum, (PTR) NULL);
399 if (files_seen++ == 0)
401 /* Intel doesn't like bss/stk because they don't have core files. */
402 puts ((radix == octal) ? " text\t data\tbss/stk\t oct\t hex\tfilename" :
403 " text\t data\tbss/stk\t dec\t hex\tfilename");
405 puts ((radix == octal) ? " text\t data\t bss\t oct\t hex\tfilename" :
406 " text\t data\t bss\t dec\t hex\tfilename");
409 total = textsize + datasize + bsssize;
411 rprint_number (7, textsize);
413 rprint_number (7, datasize);
415 rprint_number (7, bsssize);
416 printf (((radix == octal) ? "\t%7lo\t%7lx\t" : "\t%7lu\t%7lx\t"),
417 (unsigned long) total, (unsigned long) total);
419 fputs (bfd_get_filename (abfd), stdout);
420 if (bfd_my_archive (abfd))
421 printf (" (ex %s)", bfd_get_filename (bfd_my_archive (abfd)));
424 /* I REALLY miss lexical functions! */
425 bfd_size_type svi_total = 0;
426 bfd_vma svi_maxvma = 0;
432 sysv_internal_sizer (file, sec, ignore)
437 bfd_size_type size = bfd_section_size (file, sec);
438 if (!bfd_is_abs_section (sec)
439 && !bfd_is_com_section (sec)
440 && !bfd_is_und_section (sec))
442 int namelen = strlen (bfd_section_name (file, sec));
443 if (namelen > svi_namelen)
444 svi_namelen = namelen;
447 if (bfd_section_vma (file, sec) > svi_maxvma)
448 svi_maxvma = bfd_section_vma (file, sec);
453 sysv_internal_printer (file, sec, ignore)
458 bfd_size_type size = bfd_section_size (file, sec);
459 if (!bfd_is_abs_section (sec)
460 && !bfd_is_com_section (sec)
461 && !bfd_is_und_section (sec))
465 printf ("%-*s ", svi_namelen, bfd_section_name (file, sec));
466 rprint_number (svi_sizelen, size);
468 rprint_number (svi_vmalen, bfd_section_vma (file, sec));
474 print_sysv_format (file)
477 /* size all of the columns */
481 bfd_map_over_sections (file, sysv_internal_sizer, (PTR) NULL);
482 svi_vmalen = size_number ((bfd_size_type)svi_maxvma);
483 if ((size_t) svi_vmalen < sizeof ("addr") - 1)
484 svi_vmalen = sizeof ("addr")-1;
486 svi_sizelen = size_number (svi_total);
487 if ((size_t) svi_sizelen < sizeof ("size") - 1)
488 svi_sizelen = sizeof ("size")-1;
491 printf ("%s ", bfd_get_filename (file));
492 if (bfd_my_archive (file))
493 printf (" (ex %s)", bfd_get_filename (bfd_my_archive (file)));
495 printf (":\n%-*s %*s %*s\n", svi_namelen, "section",
496 svi_sizelen, "size", svi_vmalen, "addr");
497 bfd_map_over_sections (file, sysv_internal_printer, (PTR) NULL);
499 printf ("%-*s ", svi_namelen, "Total");
500 rprint_number (svi_sizelen, svi_total);
509 print_berkeley_format (file);
511 print_sysv_format (file);