Commit | Line | Data |
---|---|---|
770cde30 | 1 | /* size.c -- report size of various sections of an executable file. |
9d04d618 | 2 | Copyright 1991, 92, 93, 94, 95, 96, 97, 1998 Free Software Foundation, Inc. |
770cde30 JG |
3 | |
4 | This file is part of GNU Binutils. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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 | |
9d04d618 | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
cef35d48 | 19 | \f |
2fa0b342 DHW |
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 | |
cef35d48 | 28 | out of luck; there's no --compatibility or --pedantic option. |
2fa0b342 | 29 | */ |
770cde30 | 30 | |
2fa0b342 DHW |
31 | #include "bfd.h" |
32 | #include "getopt.h" | |
cef35d48 | 33 | #include "bucomm.h" |
9d04d618 | 34 | #include "libiberty.h" |
2fa0b342 | 35 | |
2fa0b342 DHW |
36 | #ifndef BSD_DEFAULT |
37 | #define BSD_DEFAULT 1 | |
38 | #endif | |
39 | ||
cef35d48 | 40 | /* Program options. */ |
2fa0b342 | 41 | |
cef35d48 DM |
42 | enum |
43 | { | |
44 | decimal, octal, hex | |
45 | } radix = decimal; | |
46 | int berkeley_format = BSD_DEFAULT; /* 0 means use AT&T-style output. */ | |
2fa0b342 DHW |
47 | int show_version = 0; |
48 | int show_help = 0; | |
49 | ||
cef35d48 | 50 | /* Program exit status. */ |
770cde30 JG |
51 | int return_code = 0; |
52 | ||
9d04d618 | 53 | static char *target = NULL; |
e2fe2df4 | 54 | |
9d04d618 | 55 | /* Static declarations */ |
e2fe2df4 | 56 | |
9d04d618 | 57 | static void usage PARAMS ((FILE *, int)); |
cef35d48 | 58 | static void display_file PARAMS ((char *filename)); |
9d04d618 TT |
59 | static void display_bfd PARAMS ((bfd *)); |
60 | static void display_archive PARAMS ((bfd *)); | |
61 | static int size_number PARAMS ((bfd_size_type)); | |
62 | #if 0 | |
63 | static void lprint_number PARAMS ((int, bfd_size_type)); | |
64 | #endif | |
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 *)); | |
cef35d48 | 70 | static void print_sizes PARAMS ((bfd * file)); |
cef35d48 DM |
71 | static void berkeley_sum PARAMS ((bfd *, sec_ptr, PTR)); |
72 | \f | |
9d04d618 | 73 | static void |
cef35d48 DM |
74 | usage (stream, status) |
75 | FILE *stream; | |
76 | int status; | |
2fa0b342 | 77 | { |
9d04d618 | 78 | fprintf (stream, _("\ |
d2442698 | 79 | Usage: %s [-ABdoxV] [--format=berkeley|sysv] [--radix=8|10|16]\n\ |
9d04d618 | 80 | [--target=bfdname] [--version] [--help] [file...]\n"), program_name); |
2fa0b342 | 81 | #if BSD_DEFAULT |
9d04d618 | 82 | fputs (_("default is --format=berkeley\n"), stream); |
2fa0b342 | 83 | #else |
9d04d618 | 84 | fputs (_("default is --format=sysv\n"), stream); |
2fa0b342 | 85 | #endif |
9d04d618 TT |
86 | list_supported_targets (program_name, stream); |
87 | if (status == 0) | |
88 | fprintf (stream, _("Report bugs to bug-gnu-utils@gnu.org\n")); | |
cef35d48 | 89 | exit (status); |
2fa0b342 DHW |
90 | } |
91 | ||
cef35d48 DM |
92 | struct option long_options[] = |
93 | { | |
94 | {"format", required_argument, 0, 200}, | |
95 | {"radix", required_argument, 0, 201}, | |
96 | {"target", required_argument, 0, 202}, | |
d2442698 | 97 | {"version", no_argument, &show_version, 1}, |
cef35d48 | 98 | {"help", no_argument, &show_help, 1}, |
d2442698 DM |
99 | {0, no_argument, 0, 0} |
100 | }; | |
2fa0b342 DHW |
101 | |
102 | int | |
103 | main (argc, argv) | |
104 | int argc; | |
105 | char **argv; | |
106 | { | |
107 | int temp; | |
cef35d48 | 108 | int c; |
d2442698 | 109 | |
19ac4b08 TT |
110 | setlocale (LC_MESSAGES, ""); |
111 | bindtextdomain (PACKAGE, LOCALEDIR); | |
112 | textdomain (PACKAGE); | |
113 | ||
2fa0b342 | 114 | program_name = *argv; |
9d04d618 | 115 | xmalloc_set_program_name (program_name); |
2fa0b342 | 116 | |
cef35d48 | 117 | bfd_init (); |
9d04d618 | 118 | set_default_bfd_target (); |
cef35d48 DM |
119 | |
120 | while ((c = getopt_long (argc, argv, "ABVdox", long_options, | |
121 | (int *) 0)) != EOF) | |
122 | switch (c) | |
123 | { | |
124 | case 200: /* --format */ | |
125 | switch (*optarg) | |
126 | { | |
127 | case 'B': | |
128 | case 'b': | |
129 | berkeley_format = 1; | |
130 | break; | |
131 | case 'S': | |
132 | case 's': | |
133 | berkeley_format = 0; | |
134 | break; | |
135 | default: | |
9d04d618 | 136 | fprintf (stderr, _("invalid argument to --format: %s\n"), optarg); |
cef35d48 DM |
137 | usage (stderr, 1); |
138 | } | |
2fa0b342 | 139 | break; |
2fa0b342 | 140 | |
cef35d48 | 141 | case 202: /* --target */ |
2fa0b342 DHW |
142 | target = optarg; |
143 | break; | |
2fa0b342 | 144 | |
cef35d48 | 145 | case 201: /* --radix */ |
2fa0b342 | 146 | #ifdef ANSI_LIBRARIES |
cef35d48 | 147 | temp = strtol (optarg, NULL, 10); |
2fa0b342 | 148 | #else |
cef35d48 | 149 | temp = atol (optarg); |
2fa0b342 | 150 | #endif |
cef35d48 DM |
151 | switch (temp) |
152 | { | |
153 | case 10: | |
154 | radix = decimal; | |
155 | break; | |
156 | case 8: | |
157 | radix = octal; | |
158 | break; | |
159 | case 16: | |
160 | radix = hex; | |
161 | break; | |
162 | default: | |
9d04d618 | 163 | printf (_("Invalid radix: %s\n"), optarg); |
cef35d48 DM |
164 | usage (stderr, 1); |
165 | } | |
d2442698 DM |
166 | break; |
167 | ||
cef35d48 DM |
168 | case 'A': |
169 | berkeley_format = 0; | |
170 | break; | |
171 | case 'B': | |
172 | berkeley_format = 1; | |
173 | break; | |
174 | case 'V': | |
175 | show_version = 1; | |
176 | break; | |
177 | case 'd': | |
178 | radix = decimal; | |
179 | break; | |
180 | case 'x': | |
181 | radix = hex; | |
182 | break; | |
183 | case 'o': | |
184 | radix = octal; | |
185 | break; | |
186 | case 0: | |
187 | break; | |
188 | case '?': | |
189 | usage (stderr, 1); | |
190 | } | |
191 | ||
192 | if (show_version) | |
9d04d618 | 193 | print_version ("size"); |
cef35d48 DM |
194 | if (show_help) |
195 | usage (stdout, 0); | |
2fa0b342 | 196 | |
2fa0b342 DHW |
197 | if (optind == argc) |
198 | display_file ("a.out"); | |
199 | else | |
200 | for (; optind < argc;) | |
201 | display_file (argv[optind++]); | |
202 | ||
770cde30 | 203 | return return_code; |
2fa0b342 DHW |
204 | } |
205 | \f | |
cef35d48 | 206 | /* Display stats on file or archive member ABFD. */ |
2fa0b342 | 207 | |
9d04d618 | 208 | static void |
2fa0b342 DHW |
209 | display_bfd (abfd) |
210 | bfd *abfd; | |
211 | { | |
cef35d48 | 212 | char **matching; |
2fa0b342 | 213 | |
cef35d48 DM |
214 | if (bfd_check_format (abfd, bfd_archive)) |
215 | /* An archive within an archive. */ | |
216 | return; | |
2fa0b342 | 217 | |
cef35d48 DM |
218 | if (bfd_check_format_matches (abfd, bfd_object, &matching)) |
219 | { | |
220 | print_sizes (abfd); | |
221 | printf ("\n"); | |
222 | return; | |
223 | } | |
2fa0b342 | 224 | |
9d04d618 | 225 | if (bfd_get_error () == bfd_error_file_ambiguously_recognized) |
cef35d48 DM |
226 | { |
227 | bfd_nonfatal (bfd_get_filename (abfd)); | |
228 | list_matching_formats (matching); | |
229 | free (matching); | |
230 | return_code = 3; | |
231 | return; | |
232 | } | |
2fa0b342 | 233 | |
cef35d48 DM |
234 | if (bfd_check_format_matches (abfd, bfd_core, &matching)) |
235 | { | |
236 | CONST char *core_cmd; | |
2fa0b342 | 237 | |
cef35d48 DM |
238 | print_sizes (abfd); |
239 | fputs (" (core file", stdout); | |
2fa0b342 | 240 | |
cef35d48 DM |
241 | core_cmd = bfd_core_file_failing_command (abfd); |
242 | if (core_cmd) | |
243 | printf (" invoked as %s", core_cmd); | |
2fa0b342 | 244 | |
cef35d48 DM |
245 | puts (")\n"); |
246 | return; | |
247 | } | |
248 | ||
249 | bfd_nonfatal (bfd_get_filename (abfd)); | |
250 | ||
9d04d618 | 251 | if (bfd_get_error () == bfd_error_file_ambiguously_recognized) |
cef35d48 DM |
252 | { |
253 | list_matching_formats (matching); | |
254 | free (matching); | |
255 | } | |
2fa0b342 | 256 | |
cef35d48 | 257 | return_code = 3; |
2fa0b342 DHW |
258 | } |
259 | ||
e2fe2df4 | 260 | static void |
cef35d48 DM |
261 | display_archive (file) |
262 | bfd *file; | |
2fa0b342 | 263 | { |
cef35d48 | 264 | bfd *arfile = (bfd *) NULL; |
2fa0b342 | 265 | |
cef35d48 DM |
266 | for (;;) |
267 | { | |
9d04d618 | 268 | bfd_set_error (bfd_error_no_error); |
2fa0b342 | 269 | |
cef35d48 DM |
270 | arfile = bfd_openr_next_archived_file (file, arfile); |
271 | if (arfile == NULL) | |
272 | { | |
9d04d618 | 273 | if (bfd_get_error () != bfd_error_no_more_archived_files) |
cef35d48 DM |
274 | { |
275 | bfd_nonfatal (bfd_get_filename (file)); | |
276 | return_code = 2; | |
277 | } | |
278 | break; | |
279 | } | |
2fa0b342 DHW |
280 | |
281 | display_bfd (arfile); | |
282 | /* Don't close the archive elements; we need them for next_archive */ | |
283 | } | |
cef35d48 DM |
284 | } |
285 | ||
286 | static void | |
287 | display_file (filename) | |
288 | char *filename; | |
289 | { | |
290 | bfd *file = bfd_openr (filename, target); | |
291 | if (file == NULL) | |
292 | { | |
293 | bfd_nonfatal (filename); | |
294 | return_code = 1; | |
295 | return; | |
296 | } | |
297 | ||
298 | if (bfd_check_format (file, bfd_archive) == true) | |
299 | display_archive (file); | |
2fa0b342 DHW |
300 | else |
301 | display_bfd (file); | |
302 | ||
cef35d48 DM |
303 | if (bfd_close (file) == false) |
304 | { | |
305 | bfd_nonfatal (filename); | |
306 | return_code = 1; | |
307 | return; | |
308 | } | |
2fa0b342 DHW |
309 | } |
310 | \f | |
cef35d48 DM |
311 | /* This is what lexical functions are for. */ |
312 | ||
9d04d618 TT |
313 | static int |
314 | size_number (num) | |
315 | bfd_size_type num; | |
316 | { | |
317 | char buffer[40]; | |
318 | sprintf (buffer, | |
319 | (radix == decimal ? "%lu" : | |
320 | ((radix == octal) ? "0%lo" : "0x%lx")), | |
321 | (unsigned long) num); | |
322 | ||
323 | return strlen (buffer); | |
324 | } | |
325 | ||
326 | #if 0 | |
327 | ||
328 | /* This is not used. */ | |
329 | ||
330 | static void | |
2fa0b342 | 331 | lprint_number (width, num) |
e2fe2df4 PB |
332 | int width; |
333 | bfd_size_type num; | |
2fa0b342 | 334 | { |
9d04d618 TT |
335 | char buffer[40]; |
336 | sprintf (buffer, | |
337 | (radix == decimal ? "%lu" : | |
338 | ((radix == octal) ? "0%lo" : "0x%lx")), | |
339 | (unsigned long) num); | |
340 | ||
341 | printf ("%-*s", width, buffer); | |
2fa0b342 DHW |
342 | } |
343 | ||
9d04d618 TT |
344 | #endif |
345 | ||
346 | static void | |
cef35d48 | 347 | rprint_number (width, num) |
e2fe2df4 PB |
348 | int width; |
349 | bfd_size_type num; | |
2fa0b342 | 350 | { |
9d04d618 TT |
351 | char buffer[40]; |
352 | sprintf (buffer, | |
353 | (radix == decimal ? "%lu" : | |
354 | ((radix == octal) ? "0%lo" : "0x%lx")), | |
355 | (unsigned long) num); | |
356 | ||
357 | printf ("%*s", width, buffer); | |
2fa0b342 DHW |
358 | } |
359 | ||
cef35d48 DM |
360 | static bfd_size_type bsssize; |
361 | static bfd_size_type datasize; | |
362 | static bfd_size_type textsize; | |
2fa0b342 | 363 | |
cef35d48 DM |
364 | static void |
365 | berkeley_sum (abfd, sec, ignore) | |
366 | bfd *abfd; | |
367 | sec_ptr sec; | |
368 | PTR ignore; | |
369 | { | |
9d04d618 | 370 | flagword flags; |
cef35d48 DM |
371 | bfd_size_type size; |
372 | ||
9d04d618 TT |
373 | flags = bfd_get_section_flags (abfd, sec); |
374 | if ((flags & SEC_ALLOC) == 0) | |
375 | return; | |
376 | ||
cef35d48 | 377 | size = bfd_get_section_size_before_reloc (sec); |
9d04d618 | 378 | if ((flags & SEC_CODE) != 0 || (flags & SEC_READONLY) != 0) |
cef35d48 | 379 | textsize += size; |
9d04d618 | 380 | else if ((flags & SEC_HAS_CONTENTS) != 0) |
cef35d48 | 381 | datasize += size; |
9d04d618 | 382 | else |
cef35d48 DM |
383 | bsssize += size; |
384 | } | |
385 | ||
9d04d618 | 386 | static void |
cef35d48 DM |
387 | print_berkeley_format (abfd) |
388 | bfd *abfd; | |
2fa0b342 | 389 | { |
770cde30 | 390 | static int files_seen = 0; |
cef35d48 DM |
391 | bfd_size_type total; |
392 | ||
393 | bsssize = 0; | |
394 | datasize = 0; | |
395 | textsize = 0; | |
396 | ||
397 | bfd_map_over_sections (abfd, berkeley_sum, (PTR) NULL); | |
770cde30 JG |
398 | |
399 | if (files_seen++ == 0) | |
cef35d48 DM |
400 | #if 0 |
401 | /* Intel doesn't like bss/stk because they don't have core files. */ | |
9d04d618 TT |
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"); | |
770cde30 | 404 | #else |
9d04d618 TT |
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"); | |
770cde30 | 407 | #endif |
cef35d48 | 408 | |
2fa0b342 | 409 | total = textsize + datasize + bsssize; |
cef35d48 | 410 | |
9d04d618 TT |
411 | rprint_number (7, textsize); |
412 | putchar ('\t'); | |
413 | rprint_number (7, datasize); | |
414 | putchar ('\t'); | |
415 | rprint_number (7, bsssize); | |
416 | printf (((radix == octal) ? "\t%7lo\t%7lx\t" : "\t%7lu\t%7lx\t"), | |
cef35d48 | 417 | (unsigned long) total, (unsigned long) total); |
2fa0b342 | 418 | |
cef35d48 | 419 | fputs (bfd_get_filename (abfd), stdout); |
0c5dc23c DM |
420 | if (bfd_my_archive (abfd)) |
421 | printf (" (ex %s)", bfd_get_filename (bfd_my_archive (abfd))); | |
2fa0b342 DHW |
422 | } |
423 | ||
424 | /* I REALLY miss lexical functions! */ | |
e2fe2df4 | 425 | bfd_size_type svi_total = 0; |
9d04d618 TT |
426 | bfd_vma svi_maxvma = 0; |
427 | int svi_namelen = 0; | |
428 | int svi_vmalen = 0; | |
429 | int svi_sizelen = 0; | |
2fa0b342 | 430 | |
9d04d618 TT |
431 | static void |
432 | sysv_internal_sizer (file, sec, ignore) | |
433 | bfd *file; | |
434 | sec_ptr sec; | |
435 | PTR ignore; | |
436 | { | |
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)) | |
441 | { | |
442 | int namelen = strlen (bfd_section_name (file, sec)); | |
443 | if (namelen > svi_namelen) | |
444 | svi_namelen = namelen; | |
445 | ||
446 | svi_total += size; | |
447 | if (bfd_section_vma (file, sec) > svi_maxvma) | |
448 | svi_maxvma = bfd_section_vma (file, sec); | |
449 | } | |
450 | } | |
451 | ||
452 | static void | |
cef35d48 | 453 | sysv_internal_printer (file, sec, ignore) |
2fa0b342 DHW |
454 | bfd *file; |
455 | sec_ptr sec; | |
770cde30 | 456 | PTR ignore; |
2fa0b342 | 457 | { |
e2fe2df4 | 458 | bfd_size_type size = bfd_section_size (file, sec); |
9d04d618 | 459 | if (!bfd_is_abs_section (sec) |
cef35d48 | 460 | && !bfd_is_com_section (sec) |
9d04d618 | 461 | && !bfd_is_und_section (sec)) |
cef35d48 DM |
462 | { |
463 | svi_total += size; | |
464 | ||
9d04d618 TT |
465 | printf ("%-*s ", svi_namelen, bfd_section_name (file, sec)); |
466 | rprint_number (svi_sizelen, size); | |
467 | printf (" "); | |
468 | rprint_number (svi_vmalen, bfd_section_vma (file, sec)); | |
cef35d48 DM |
469 | printf ("\n"); |
470 | } | |
2fa0b342 DHW |
471 | } |
472 | ||
9d04d618 | 473 | static void |
cef35d48 | 474 | print_sysv_format (file) |
2fa0b342 DHW |
475 | bfd *file; |
476 | { | |
9d04d618 | 477 | /* size all of the columns */ |
2fa0b342 | 478 | svi_total = 0; |
9d04d618 TT |
479 | svi_maxvma = 0; |
480 | svi_namelen = 0; | |
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; | |
2fa0b342 | 485 | |
9d04d618 TT |
486 | svi_sizelen = size_number (svi_total); |
487 | if ((size_t) svi_sizelen < sizeof ("size") - 1) | |
488 | svi_sizelen = sizeof ("size")-1; | |
489 | ||
490 | svi_total = 0; | |
2fa0b342 | 491 | printf ("%s ", bfd_get_filename (file)); |
6f9dff07 DM |
492 | if (bfd_my_archive (file)) |
493 | printf (" (ex %s)", bfd_get_filename (bfd_my_archive (file))); | |
2fa0b342 | 494 | |
9d04d618 TT |
495 | printf (":\n%-*s %*s %*s\n", svi_namelen, "section", |
496 | svi_sizelen, "size", svi_vmalen, "addr"); | |
cef35d48 | 497 | bfd_map_over_sections (file, sysv_internal_printer, (PTR) NULL); |
2fa0b342 | 498 | |
9d04d618 TT |
499 | printf ("%-*s ", svi_namelen, "Total"); |
500 | rprint_number (svi_sizelen, svi_total); | |
cef35d48 | 501 | printf ("\n\n"); |
2fa0b342 DHW |
502 | } |
503 | ||
e2fe2df4 | 504 | static void |
cef35d48 | 505 | print_sizes (file) |
2fa0b342 DHW |
506 | bfd *file; |
507 | { | |
508 | if (berkeley_format) | |
cef35d48 DM |
509 | print_berkeley_format (file); |
510 | else | |
511 | print_sysv_format (file); | |
2fa0b342 | 512 | } |