1 /* size.c -- report size of various sections of an executable file.
2 Copyright 1991, 1992 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. */
21 /* Extensions/incompatibilities:
22 o - BSD output has filenames at the end.
23 o - BSD output can appear in different radicies.
24 o - SysV output has less redundant whitespace. Filename comes at end.
25 o - SysV output doesn't show VMA which is always the same as the PMA.
26 o - We also handle core files.
27 o - We also handle archives.
28 If you write shell scripts which manipulate this info then you may be
29 out of luck; there's no +predantic switch.
40 /* Various program options */
42 enum {decimal, octal, hex} radix = decimal;
43 int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output */
50 extern char *program_version;
51 extern char *program_name;
54 /* Forward declarations */
57 display_file PARAMS ((char *filename));
60 print_sizes PARAMS ((bfd *file));
62 /** main and like trivia */
67 fprintf (stderr, "size %s\nUsage: %s -{dox}{AB}V files ...\n",
68 program_version, program_name);
69 fputs("\t+radix={8|10|16} -- select appropriate output radix.\n\
70 \t-d -- output in decimal\n\
71 \t-o -- output in octal\n\
72 \t-x -- output in hex", stderr);
73 fputs("\t+format={Berkeley|SysV} -- select display format.\n\
74 \t-A -- SysV(AT&T) format\n\
75 \t-B -- BSD format", stderr);
77 fputs("\t (Default is +format=Berkeley)", stderr);
79 fputs("\t (Default is +format=SysV)", stderr);
81 fputs("\t-V, +version -- display program version, etc.\n\
82 \t+help -- this message\n", stderr);
86 struct option long_options[] = {{"radix", no_argument, 0, 0},
87 {"format", required_argument, 0, 0},
88 {"version", no_argument, &show_version, 1},
89 {"target", optional_argument, NULL, 0},
90 {"help", no_argument, &show_help, 1},
91 {0, no_argument, 0, 0}};
99 int c; /* sez which option char */
100 int option_index = 0;
101 extern int optind; /* steps thru options */
102 program_name = *argv;
106 while ((c = getopt_long(argc, argv, "ABVdox", long_options,
107 &option_index)) != EOF)
110 if (!strcmp("format",(long_options[option_index]).name)) {
112 case 'B': case 'b': berkeley_format = 1; break;
113 case 'S': case 's': berkeley_format = 0; break;
114 default: printf("Unknown option to +format: %s\n", optarg);
120 if (!strcmp("target",(long_options[option_index]).name)) {
125 if (!strcmp("radix",(long_options[option_index]).name)) {
126 #ifdef ANSI_LIBRARIES
127 temp = strtol(optarg, NULL, 10);
132 case 10: radix = decimal; break;
133 case 8: radix = octal; break;
134 case 16: radix = hex; break;
135 default: printf("Unknown radix: %s\n", optarg);
140 case 'A': berkeley_format = 0; break;
141 case 'B': berkeley_format = 1; break;
142 case 'V': show_version = 1; break;
143 case 'd': radix = decimal; break;
144 case 'x': radix = hex; break;
145 case 'o': radix = octal; break;
149 if (show_version) printf("%s version %s\n", program_name, program_version);
150 if (show_help) usage();
153 display_file ("a.out");
155 for (; optind < argc;)
156 display_file (argv[optind++]);
161 /** Display a file's stats */
167 CONST char *core_cmd;
169 if (bfd_check_format(abfd, bfd_archive)) return;
171 if (bfd_check_format(abfd, bfd_object)) {
176 if (bfd_check_format(abfd, bfd_core)) {
178 fputs(" (core file", stdout);
180 core_cmd = bfd_core_file_failing_command(abfd);
181 if (core_cmd) printf(" invoked as %s", core_cmd);
187 printf("Unknown file format: %s.", bfd_get_filename(abfd));
198 display_file(filename)
201 bfd *file, *arfile = (bfd *) NULL;
203 file = bfd_openr (filename, target);
205 bfd_perror (filename);
210 if (bfd_check_format(file, bfd_archive) == true) {
213 bfd_error = no_error;
215 arfile = bfd_openr_next_archived_file (file, arfile);
216 if (arfile == NULL) {
217 if (bfd_error != no_more_archived_files) {
218 bfd_perror (bfd_get_filename (file));
224 display_bfd (arfile);
225 /* Don't close the archive elements; we need them for next_archive */
234 /* This is what lexical functions are for */
236 lprint_number (width, num)
240 printf ((radix == decimal ? "%-*ld\t" :
241 ((radix == octal) ? "%-*lo\t" : "%-*lx\t")), width, (long)num);
245 rprint_number(width, num)
249 printf ((radix == decimal ? "%*ld\t" :
250 ((radix == octal) ? "%*lo\t" : "%*lx\t")), width, (long)num);
253 static char *bss_section_name = ".bss";
254 static char *data_section_name = ".data";
255 static char *stack_section_name = ".stack";
256 static char *text_section_name = ".text";
258 void print_berkeley_format(abfd)
261 static int files_seen = 0;
262 sec_ptr bsssection = NULL;
263 sec_ptr datasection = NULL;
264 sec_ptr textsection = NULL;
265 bfd_size_type bsssize = 0;
266 bfd_size_type datasize = 0;
267 bfd_size_type textsize = 0;
268 bfd_size_type total = 0;
271 if ((textsection = bfd_get_section_by_name (abfd, text_section_name))
273 textsize = bfd_get_section_size_before_reloc (textsection);
276 if ((datasection = bfd_get_section_by_name (abfd, data_section_name))
278 datasize = bfd_get_section_size_before_reloc ( datasection);
281 if (bfd_get_format (abfd) == bfd_object) {
282 if ((bsssection = bfd_get_section_by_name (abfd, bss_section_name))
284 bsssize = bfd_section_size(abfd, bsssection);
287 if ((bsssection = bfd_get_section_by_name (abfd, stack_section_name))
289 bsssize = bfd_section_size(abfd, bsssection);
293 if (files_seen++ == 0)
294 #if 0 /* intel doesn't like bss/stk b/c they don't gave core files */
295 puts((radix == octal) ? "text\tdata\tbss/stk\toct\thex\tfilename" :
296 "text\tdata\tbss/stk\tdec\thex\tfilename");
298 puts((radix == octal) ? "text\tdata\tbss\toct\thex\tfilename" :
299 "text\tdata\tbss\tdec\thex\tfilename");
302 total = textsize + datasize + bsssize;
304 lprint_number (7, textsize);
305 lprint_number (7, datasize);
306 lprint_number (7, bsssize);
307 printf (((radix == octal) ? "%-7lo\t%-7lx\t" : "%-7ld\t%-7lx\t"),
308 (long)total, (long)total);
310 fputs(bfd_get_filename(abfd), stdout);
311 if (abfd->my_archive) printf (" (ex %s)", abfd->my_archive->filename);
314 /* I REALLY miss lexical functions! */
315 bfd_size_type svi_total = 0;
318 sysv_internal_printer(file, sec, ignore)
323 bfd_size_type size = bfd_section_size (file, sec);
324 if (sec!= &bfd_abs_section
325 && sec!= &bfd_com_section
326 && sec!=&bfd_und_section)
331 printf ("%-12s", bfd_section_name(file, sec));
332 rprint_number (8, size);
334 rprint_number (8, bfd_section_vma(file, sec));
341 print_sysv_format(file)
346 printf ("%s ", bfd_get_filename (file));
347 if (file->my_archive) printf (" (ex %s)", file->my_archive->filename);
349 puts(":\nsection\t\tsize\t addr");
350 bfd_map_over_sections (file, sysv_internal_printer, (PTR)NULL);
353 rprint_number(8, svi_total);
354 printf("\n"); printf("\n");
362 print_berkeley_format(file);
363 else print_sysv_format(file);