* stack.c (print_frame_info): When checking PC_IN_CALL_DUMMY,
[deliverable/binutils-gdb.git] / binutils / size.c
CommitLineData
770cde30 1/* size.c -- report size of various sections of an executable file.
e2fe2df4 2 Copyright 1991, 1992 Free Software Foundation, Inc.
770cde30
JG
3
4This file is part of GNU Binutils.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
2fa0b342
DHW
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
d2442698 29 out of luck; there's no --predantic option.
2fa0b342 30*/
770cde30 31
2fa0b342 32#include "bfd.h"
770cde30 33#include "sysdep.h"
2fa0b342
DHW
34#include "getopt.h"
35
2fa0b342
DHW
36#ifndef BSD_DEFAULT
37#define BSD_DEFAULT 1
38#endif
39
2fa0b342
DHW
40/* Various program options */
41
42enum {decimal, octal, hex} radix = decimal;
43int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output */
44int show_version = 0;
45int show_help = 0;
46
770cde30
JG
47int return_code = 0;
48
2fa0b342
DHW
49/* IMPORTS */
50extern char *program_version;
51extern char *program_name;
52extern char *target;
e2fe2df4
PB
53
54/* Forward declarations */
55
56static void
57display_file PARAMS ((char *filename));
58
59static void
60print_sizes PARAMS ((bfd *file));
2fa0b342
DHW
61\f
62/** main and like trivia */
63
64void
65usage ()
66{
d2442698
DM
67 fprintf (stderr, "size %s\n\
68Usage: %s [-ABdoxV] [--format=berkeley|sysv] [--radix=8|10|16]\n\
69 [--target=bfdname] [--version] [--help] [file...]\n",
70 program_version, program_name);
2fa0b342 71#if BSD_DEFAULT
d2442698 72 fputs (" (default is --format=berkeley)\n", stderr);
2fa0b342 73#else
d2442698 74 fputs (" (default is --format=sysv)\n", stderr);
2fa0b342 75#endif
d2442698 76 exit (1);
2fa0b342
DHW
77}
78
d2442698
DM
79struct option long_options[] = {
80 {"format", required_argument, 0, 200},
81 {"radix", required_argument, 0, 201},
82 {"target", required_argument, 0, 202},
83 {"version", no_argument, &show_version, 1},
84 {"help", no_argument, &show_help, 1},
85 {0, no_argument, 0, 0}
86};
2fa0b342
DHW
87
88int
89main (argc, argv)
90 int argc;
91 char **argv;
92{
93 int temp;
94 int c; /* sez which option char */
2fa0b342 95 extern int optind; /* steps thru options */
d2442698 96
2fa0b342
DHW
97 program_name = *argv;
98
770cde30
JG
99 bfd_init();
100
2fa0b342 101 while ((c = getopt_long(argc, argv, "ABVdox", long_options,
d2442698 102 (int *) 0)) != EOF)
2fa0b342 103 switch(c) {
d2442698 104 case 200: /* --format */
2fa0b342
DHW
105 switch(*optarg) {
106 case 'B': case 'b': berkeley_format = 1; break;
107 case 'S': case 's': berkeley_format = 0; break;
d2442698 108 default: fprintf(stderr, "invalid argument to --format: %s\n", optarg);
2fa0b342
DHW
109 usage();
110 }
111 break;
2fa0b342 112
d2442698 113 case 202: /* --target */
2fa0b342
DHW
114 target = optarg;
115 break;
2fa0b342 116
d2442698 117 case 201: /* --radix */
2fa0b342
DHW
118#ifdef ANSI_LIBRARIES
119 temp = strtol(optarg, NULL, 10);
120#else
121 temp = atol(optarg);
122#endif
123 switch(temp) {
124 case 10: radix = decimal; break;
125 case 8: radix = octal; break;
126 case 16: radix = hex; break;
127 default: printf("Unknown radix: %s\n", optarg);
128 usage();
129 }
d2442698
DM
130 break;
131
2fa0b342
DHW
132 case 'A': berkeley_format = 0; break;
133 case 'B': berkeley_format = 1; break;
134 case 'V': show_version = 1; break;
135 case 'd': radix = decimal; break;
136 case 'x': radix = hex; break;
137 case 'o': radix = octal; break;
138 case '?': usage();
139 }
140
141 if (show_version) printf("%s version %s\n", program_name, program_version);
142 if (show_help) usage();
143
2fa0b342
DHW
144 if (optind == argc)
145 display_file ("a.out");
146 else
147 for (; optind < argc;)
148 display_file (argv[optind++]);
149
770cde30 150 return return_code;
2fa0b342
DHW
151}
152\f
153/** Display a file's stats */
154
155void
156display_bfd (abfd)
157 bfd *abfd;
158{
770cde30 159 CONST char *core_cmd;
2fa0b342
DHW
160
161 if (bfd_check_format(abfd, bfd_archive)) return;
162
163 if (bfd_check_format(abfd, bfd_object)) {
164 print_sizes(abfd);
165 goto done;
166 }
167
168 if (bfd_check_format(abfd, bfd_core)) {
169 print_sizes(abfd);
170 fputs(" (core file", stdout);
171
172 core_cmd = bfd_core_file_failing_command(abfd);
173 if (core_cmd) printf(" invoked as %s", core_cmd);
174
175 puts(")");
176 goto done;
177 }
178
179 printf("Unknown file format: %s.", bfd_get_filename(abfd));
770cde30 180 return_code = 3;
2fa0b342
DHW
181
182 done:
183
184
185 printf("\n");
186 return;
187}
188
e2fe2df4 189static void
2fa0b342
DHW
190display_file(filename)
191 char *filename;
192{
193 bfd *file, *arfile = (bfd *) NULL;
194
195 file = bfd_openr (filename, target);
196 if (file == NULL) {
d2442698 197 fprintf (stderr, "%s: ", program_name);
2fa0b342 198 bfd_perror (filename);
770cde30 199 return_code = 1;
2fa0b342
DHW
200 return;
201 }
202
203 if (bfd_check_format(file, bfd_archive) == true) {
204 for(;;) {
205
206 bfd_error = no_error;
207
208 arfile = bfd_openr_next_archived_file (file, arfile);
209 if (arfile == NULL) {
770cde30 210 if (bfd_error != no_more_archived_files) {
d2442698 211 fprintf (stderr, "%s: ", program_name);
2fa0b342 212 bfd_perror (bfd_get_filename (file));
770cde30
JG
213 return_code = 2;
214 }
2fa0b342
DHW
215 return;
216 }
217
218 display_bfd (arfile);
219 /* Don't close the archive elements; we need them for next_archive */
220 }
221 }
222 else
223 display_bfd (file);
224
225 bfd_close (file);
226}
227\f
228/* This is what lexical functions are for */
229void
230lprint_number (width, num)
e2fe2df4
PB
231 int width;
232 bfd_size_type num;
2fa0b342 233{
d2442698
DM
234 printf ((radix == decimal ? "%-*lu\t" :
235 ((radix == octal) ? "%-*lo\t" : "%-*lx\t")),
236 width, (unsigned long)num);
2fa0b342
DHW
237}
238
239void
240rprint_number(width, num)
e2fe2df4
PB
241 int width;
242 bfd_size_type num;
2fa0b342 243{
d2442698
DM
244 printf ((radix == decimal ? "%*lu\t" :
245 ((radix == octal) ? "%*lo\t" : "%*lx\t")),
246 width, (unsigned long)num);
2fa0b342
DHW
247}
248
249static char *bss_section_name = ".bss";
250static char *data_section_name = ".data";
251static char *stack_section_name = ".stack";
252static char *text_section_name = ".text";
253
254void print_berkeley_format(abfd)
255bfd *abfd;
256{
770cde30 257 static int files_seen = 0;
2fa0b342
DHW
258 sec_ptr bsssection = NULL;
259 sec_ptr datasection = NULL;
260 sec_ptr textsection = NULL;
e2fe2df4
PB
261 bfd_size_type bsssize = 0;
262 bfd_size_type datasize = 0;
263 bfd_size_type textsize = 0;
264 bfd_size_type total = 0;
2fa0b342
DHW
265
266
267 if ((textsection = bfd_get_section_by_name (abfd, text_section_name))
268 != NULL) {
770cde30 269 textsize = bfd_get_section_size_before_reloc (textsection);
2fa0b342
DHW
270 }
271
272 if ((datasection = bfd_get_section_by_name (abfd, data_section_name))
273 != NULL) {
770cde30 274 datasize = bfd_get_section_size_before_reloc ( datasection);
2fa0b342
DHW
275 }
276
277 if (bfd_get_format (abfd) == bfd_object) {
278 if ((bsssection = bfd_get_section_by_name (abfd, bss_section_name))
279 != NULL) {
280 bsssize = bfd_section_size(abfd, bsssection);
281 }
282 } else {
283 if ((bsssection = bfd_get_section_by_name (abfd, stack_section_name))
284 != NULL) {
285 bsssize = bfd_section_size(abfd, bsssection);
286 }
287 }
770cde30
JG
288
289 if (files_seen++ == 0)
290#if 0 /* intel doesn't like bss/stk b/c they don't gave core files */
291 puts((radix == octal) ? "text\tdata\tbss/stk\toct\thex\tfilename" :
292 "text\tdata\tbss/stk\tdec\thex\tfilename");
293#else
294 puts((radix == octal) ? "text\tdata\tbss\toct\thex\tfilename" :
295 "text\tdata\tbss\tdec\thex\tfilename");
296#endif
2fa0b342
DHW
297
298 total = textsize + datasize + bsssize;
299
300 lprint_number (7, textsize);
301 lprint_number (7, datasize);
302 lprint_number (7, bsssize);
d2442698
DM
303 printf (((radix == octal) ? "%-7lo\t%-7lx\t" : "%-7lu\t%-7lx\t"),
304 (unsigned long)total, (unsigned long)total);
2fa0b342
DHW
305
306 fputs(bfd_get_filename(abfd), stdout);
307 if (abfd->my_archive) printf (" (ex %s)", abfd->my_archive->filename);
308}
309
310/* I REALLY miss lexical functions! */
e2fe2df4 311bfd_size_type svi_total = 0;
2fa0b342
DHW
312
313void
770cde30 314sysv_internal_printer(file, sec, ignore)
2fa0b342
DHW
315 bfd *file;
316 sec_ptr sec;
770cde30 317 PTR ignore;
2fa0b342 318{
e2fe2df4 319 bfd_size_type size = bfd_section_size (file, sec);
770cde30 320 if (sec!= &bfd_abs_section
d2442698 321 && ! bfd_is_com_section (sec)
770cde30
JG
322 && sec!=&bfd_und_section)
323 {
324
325 svi_total += size;
2fa0b342 326
770cde30
JG
327 printf ("%-12s", bfd_section_name(file, sec));
328 rprint_number (8, size);
329 printf(" ");
330 rprint_number (8, bfd_section_vma(file, sec));
331 printf ("\n");
332 }
333
2fa0b342
DHW
334}
335
336void
337print_sysv_format(file)
338 bfd *file;
339{
340 svi_total = 0;
341
342 printf ("%s ", bfd_get_filename (file));
343 if (file->my_archive) printf (" (ex %s)", file->my_archive->filename);
344
345 puts(":\nsection\t\tsize\t addr");
770cde30 346 bfd_map_over_sections (file, sysv_internal_printer, (PTR)NULL);
2fa0b342
DHW
347
348 printf("Total ");
349 rprint_number(8, svi_total);
350 printf("\n"); printf("\n");
351}
352
e2fe2df4 353static void
2fa0b342
DHW
354print_sizes(file)
355 bfd *file;
356{
357 if (berkeley_format)
358 print_berkeley_format(file);
359 else print_sysv_format(file);
360}
This page took 0.130373 seconds and 4 git commands to generate.