1 /* Print size information from ELF file.
2 Copyright (C) 2000-2007,2009,2012,2014,2015 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
6 This file 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 3 of the License, or
9 (at your option) any later version.
11 elfutils is distributed in the hope that it will be useful, but
12 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, see <http://www.gnu.org/licenses/>. */
33 #include <stdio_ext.h>
37 #include <sys/param.h>
42 /* Name and version of program. */
43 static void print_version (FILE *stream, struct argp_state *state);
44 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
46 /* Bug report address. */
47 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
50 /* Values for the parameters which have no short form. */
51 #define OPT_FORMAT 0x100
52 #define OPT_RADIX 0x101
54 /* Definitions of arguments for argp functions. */
55 static const struct argp_option options[] =
57 { NULL, 0, NULL, 0, N_("Output format:"), 0 },
58 { "format", OPT_FORMAT, "FORMAT", 0,
59 N_("Use the output format FORMAT. FORMAT can be `bsd' or `sysv'. "
60 "The default is `bsd'"), 0 },
61 { NULL, 'A', NULL, 0, N_("Same as `--format=sysv'"), 0 },
62 { NULL, 'B', NULL, 0, N_("Same as `--format=bsd'"), 0 },
63 { "radix", OPT_RADIX, "RADIX", 0, N_("Use RADIX for printing symbol values"),
65 { NULL, 'd', NULL, 0, N_("Same as `--radix=10'"), 0 },
66 { NULL, 'o', NULL, 0, N_("Same as `--radix=8'"), 0 },
67 { NULL, 'x', NULL, 0, N_("Same as `--radix=16'"), 0 },
69 N_("Similar to `--format=sysv' output but in one line"), 0 },
71 { NULL, 0, NULL, 0, N_("Output options:"), 0 },
73 N_("Print size and permission flags for loadable segments"), 0 },
74 { "totals", 't', NULL, 0, N_("Display the total sizes (bsd only)"), 0 },
75 { NULL, 0, NULL, 0, NULL, 0 }
78 /* Short description of program. */
79 static const char doc[] = N_("\
80 List section sizes of FILEs (a.out by default).");
82 /* Strings for arguments in help texts. */
83 static const char args_doc[] = N_("[FILE...]");
85 /* Prototype for option handler. */
86 static error_t parse_opt (int key, char *arg, struct argp_state *state);
88 /* Data structure to communicate with argp functions. */
89 static struct argp argp =
91 options, parse_opt, args_doc, doc, NULL, NULL, NULL
95 /* Print symbols in file named FNAME. */
96 static int process_file (const char *fname);
98 /* Handle content of archive. */
99 static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname);
101 /* Handle ELF file. */
102 static void handle_elf (Elf *elf, const char *fullname, const char *fname);
104 /* Show total size. */
105 static void show_bsd_totals (void);
107 #define INTERNAL_ERROR(fname) \
108 error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s): %s"), \
109 fname, __LINE__, PACKAGE_VERSION, elf_errmsg (-1))
112 /* User-selectable options. */
114 /* The selected output format. */
119 format_sysv_one_line,
123 /* Radix for printed numbers. */
132 /* Mapping of radix and binary class to length. */
133 static const int length_map[2][3] =
138 [radix_decimal] = 10,
144 [radix_decimal] = 20,
149 /* True if total sizes should be printed. */
151 /* To print the total sizes in a reasonable format remember the higest
152 "class" of ELF binaries processed. */
153 static int totals_class;
157 main (int argc, char *argv[])
162 /* We use no threads here which can interfere with handling a stream. */
163 __fsetlocking (stdin, FSETLOCKING_BYCALLER);
164 __fsetlocking (stdout, FSETLOCKING_BYCALLER);
165 __fsetlocking (stderr, FSETLOCKING_BYCALLER);
168 setlocale (LC_ALL, "");
170 /* Make sure the message catalog can be found. */
171 bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
173 /* Initialize the message catalog. */
174 textdomain (PACKAGE_TARNAME);
176 /* Parse and process arguments. */
177 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
180 /* Tell the library which version we are expecting. */
181 elf_version (EV_CURRENT);
183 if (remaining == argc)
184 /* The user didn't specify a name so we use a.out. */
185 result = process_file ("a.out");
187 /* Process all the remaining files. */
189 result |= process_file (argv[remaining]);
190 while (++remaining < argc);
192 /* Print the total sizes but only if the output format is BSD and at
193 least one file has been correctly read (i.e., we recognized the
195 if (totals && format == format_bsd && totals_class != 0)
202 /* Print the version information. */
204 print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
206 fprintf (stream, "size (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
207 fprintf (stream, gettext ("\
208 Copyright (C) %s Red Hat, Inc.\n\
209 This is free software; see the source for copying conditions. There is NO\n\
210 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
212 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
216 /* Handle program arguments. */
218 parse_opt (int key, char *arg,
219 struct argp_state *state __attribute__ ((unused)))
224 radix = radix_decimal;
228 format = format_sysv_one_line;
240 format = format_sysv;
248 format = format_segments;
252 if (strcmp (arg, "bsd") == 0 || strcmp (arg, "berkeley") == 0)
254 else if (likely (strcmp (arg, "sysv") == 0))
255 format = format_sysv;
257 error (EXIT_FAILURE, 0, gettext ("Invalid format: %s"), arg);
261 if (strcmp (arg, "x") == 0 || strcmp (arg, "16") == 0)
263 else if (strcmp (arg, "d") == 0 || strcmp (arg, "10") == 0)
264 radix = radix_decimal;
265 else if (strcmp (arg, "o") == 0 || strcmp (arg, "8") == 0)
268 error (EXIT_FAILURE, 0, gettext ("Invalid radix: %s"), arg);
276 return ARGP_ERR_UNKNOWN;
282 /* Open the file and determine the type. */
284 process_file (const char *fname)
286 int fd = open (fname, O_RDONLY);
287 if (unlikely (fd == -1))
289 error (0, errno, gettext ("cannot open '%s'"), fname);
293 /* Now get the ELF descriptor. */
294 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
295 if (likely (elf != NULL))
297 if (elf_kind (elf) == ELF_K_ELF)
299 handle_elf (elf, NULL, fname);
301 if (unlikely (elf_end (elf) != 0))
302 INTERNAL_ERROR (fname);
304 if (unlikely (close (fd) != 0))
305 error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
309 else if (likely (elf_kind (elf) == ELF_K_AR))
311 int result = handle_ar (fd, elf, NULL, fname);
313 if (unlikely (close (fd) != 0))
314 error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
319 /* We cannot handle this type. Close the descriptor anyway. */
320 if (unlikely (elf_end (elf) != 0))
321 INTERNAL_ERROR (fname);
324 if (unlikely (close (fd) != 0))
325 error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
327 error (0, 0, gettext ("%s: file format not recognized"), fname);
333 /* Print the BSD-style header. This is done exactly once. */
335 print_header (Elf *elf)
341 int ddigits = length_map[gelf_getclass (elf) - 1][radix_decimal];
342 int xdigits = length_map[gelf_getclass (elf) - 1][radix_hex];
344 printf ("%*s %*s %*s %*s %*s %s\n",
345 ddigits - 2, sgettext ("bsd|text"),
346 ddigits - 2, sgettext ("bsd|data"),
347 ddigits - 2, sgettext ("bsd|bss"),
348 ddigits - 2, sgettext ("bsd|dec"),
349 xdigits - 2, sgettext ("bsd|hex"),
350 sgettext ("bsd|filename"));
358 handle_ar (int fd, Elf *elf, const char *prefix, const char *fname)
360 size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
361 size_t fname_len = strlen (fname) + 1;
362 char new_prefix[prefix_len + 1 + fname_len];
363 char *cp = new_prefix;
365 /* Create the full name of the file. */
368 cp = mempcpy (cp, prefix, prefix_len);
371 memcpy (cp, fname, fname_len);
373 /* Process all the files contained in the archive. */
376 Elf_Cmd cmd = ELF_C_READ_MMAP;
377 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
379 /* The the header for this element. */
380 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
382 if (elf_kind (subelf) == ELF_K_ELF)
383 handle_elf (subelf, new_prefix, arhdr->ar_name);
384 else if (likely (elf_kind (subelf) == ELF_K_AR))
385 result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name);
386 /* else signal error??? */
388 /* Get next archive element. */
389 cmd = elf_next (subelf);
390 if (unlikely (elf_end (subelf) != 0))
391 INTERNAL_ERROR (fname);
394 if (unlikely (elf_end (elf) != 0))
395 INTERNAL_ERROR (fname);
401 /* Show sizes in SysV format. */
403 show_sysv (Elf *elf, const char *prefix, const char *fname,
404 const char *fullname)
407 const int digits = length_map[gelf_getclass (elf) - 1][radix];
409 /* Get the section header string table index. */
411 if (unlikely (elf_getshdrstrndx (elf, &shstrndx) < 0))
412 error (EXIT_FAILURE, 0,
413 gettext ("cannot get section header string table index"));
415 /* First round over the sections: determine the longest section name. */
417 while ((scn = elf_nextscn (elf, scn)) != NULL)
420 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
423 INTERNAL_ERROR (fullname);
425 /* Ignore all sections which are not used at runtime. */
426 const char *name = elf_strptr (elf, shstrndx, shdr->sh_name);
427 if (name != NULL && (shdr->sh_flags & SHF_ALLOC) != 0)
428 maxlen = MAX (maxlen, (int) strlen (name));
431 fputs_unlocked (fname, stdout);
433 printf (gettext (" (ex %s)"), prefix);
434 printf (":\n%-*s %*s %*s\n",
435 maxlen, sgettext ("sysv|section"),
436 digits - 2, sgettext ("sysv|size"),
437 digits, sgettext ("sysv|addr"));
439 /* Iterate over all sections. */
441 while ((scn = elf_nextscn (elf, scn)) != NULL)
444 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
446 /* Ignore all sections which are not used at runtime. */
447 if ((shdr->sh_flags & SHF_ALLOC) != 0)
449 printf ((radix == radix_hex
450 ? "%-*s %*" PRIx64 " %*" PRIx64 "\n"
451 : (radix == radix_decimal
452 ? "%-*s %*" PRId64 " %*" PRId64 "\n"
453 : "%-*s %*" PRIo64 " %*" PRIo64 "\n")),
454 maxlen, elf_strptr (elf, shstrndx, shdr->sh_name),
455 digits - 2, shdr->sh_size,
456 digits, shdr->sh_addr);
458 total += shdr->sh_size;
462 if (radix == radix_hex)
463 printf ("%-*s %*" PRIx64 "\n\n\n", maxlen, sgettext ("sysv|Total"),
465 else if (radix == radix_decimal)
466 printf ("%-*s %*" PRId64 "\n\n\n", maxlen, sgettext ("sysv|Total"),
469 printf ("%-*s %*" PRIo64 "\n\n\n", maxlen, sgettext ("sysv|Total"),
474 /* Show sizes in SysV format in one line. */
476 show_sysv_one_line (Elf *elf)
478 /* Get the section header string table index. */
480 if (unlikely (elf_getshdrstrndx (elf, &shstrndx) < 0))
481 error (EXIT_FAILURE, 0,
482 gettext ("cannot get section header string table index"));
484 /* Iterate over all sections. */
488 while ((scn = elf_nextscn (elf, scn)) != NULL)
491 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
493 /* Ignore all sections which are not used at runtime. */
494 if ((shdr->sh_flags & SHF_ALLOC) == 0)
498 fputs_unlocked (" + ", stdout);
501 printf ((radix == radix_hex ? "%" PRIx64 "(%s)"
502 : (radix == radix_decimal ? "%" PRId64 "(%s)"
503 : "%" PRIo64 "(%s)")),
504 shdr->sh_size, elf_strptr (elf, shstrndx, shdr->sh_name));
506 total += shdr->sh_size;
509 if (radix == radix_hex)
510 printf (" = %#" PRIx64 "\n", total);
511 else if (radix == radix_decimal)
512 printf (" = %" PRId64 "\n", total);
514 printf (" = %" PRIo64 "\n", total);
518 /* Variables to add up the sizes of all files. */
519 static uintmax_t total_textsize;
520 static uintmax_t total_datasize;
521 static uintmax_t total_bsssize;
524 /* Show sizes in BSD format. */
526 show_bsd (Elf *elf, const char *prefix, const char *fname,
527 const char *fullname)
529 GElf_Off textsize = 0;
530 GElf_Off datasize = 0;
531 GElf_Off bsssize = 0;
532 const int ddigits = length_map[gelf_getclass (elf) - 1][radix_decimal];
533 const int xdigits = length_map[gelf_getclass (elf) - 1][radix_hex];
535 /* Iterate over all sections. */
537 while ((scn = elf_nextscn (elf, scn)) != NULL)
540 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
543 INTERNAL_ERROR (fullname);
545 /* Ignore all sections which are not marked as loaded. */
546 if ((shdr->sh_flags & SHF_ALLOC) == 0)
549 if ((shdr->sh_flags & SHF_WRITE) == 0)
550 textsize += shdr->sh_size;
551 else if (shdr->sh_type == SHT_NOBITS)
552 bsssize += shdr->sh_size;
554 datasize += shdr->sh_size;
557 printf ("%*" PRId64 " %*" PRId64 " %*" PRId64 " %*" PRId64 " %*"
559 ddigits - 2, textsize,
560 ddigits - 2, datasize,
561 ddigits - 2, bsssize,
562 ddigits - 2, textsize + datasize + bsssize,
563 xdigits - 2, textsize + datasize + bsssize,
566 printf (gettext (" (ex %s)"), prefix);
567 fputs_unlocked ("\n", stdout);
569 total_textsize += textsize;
570 total_datasize += datasize;
571 total_bsssize += bsssize;
573 totals_class = MAX (totals_class, gelf_getclass (elf));
577 /* Show total size. */
579 show_bsd_totals (void)
581 int ddigits = length_map[totals_class - 1][radix_decimal];
582 int xdigits = length_map[totals_class - 1][radix_hex];
584 printf ("%*" PRIuMAX " %*" PRIuMAX " %*" PRIuMAX " %*" PRIuMAX " %*"
586 ddigits - 2, total_textsize,
587 ddigits - 2, total_datasize,
588 ddigits - 2, total_bsssize,
589 ddigits - 2, total_textsize + total_datasize + total_bsssize,
590 xdigits - 2, total_textsize + total_datasize + total_bsssize,
591 gettext ("(TOTALS)\n"));
595 /* Show size and permission of loadable segments. */
597 show_segments (Elf *elf, const char *fullname)
600 if (elf_getphdrnum (elf, &phnum) != 0)
601 INTERNAL_ERROR (fullname);
605 for (size_t cnt = 0; cnt < phnum; ++cnt)
610 phdr = gelf_getphdr (elf, cnt, &phdr_mem);
612 INTERNAL_ERROR (fullname);
614 if (phdr->p_type != PT_LOAD)
615 /* Only load segments. */
619 fputs_unlocked (" + ", stdout);
622 printf (radix == radix_hex ? "%" PRIx64 "(%c%c%c)"
623 : (radix == radix_decimal ? "%" PRId64 "(%c%c%c)"
624 : "%" PRIo64 "(%c%c%c)"),
626 (phdr->p_flags & PF_R) == 0 ? '-' : 'r',
627 (phdr->p_flags & PF_W) == 0 ? '-' : 'w',
628 (phdr->p_flags & PF_X) == 0 ? '-' : 'x');
630 total += phdr->p_memsz;
633 if (radix == radix_hex)
634 printf (" = %#" PRIx64 "\n", total);
635 else if (radix == radix_decimal)
636 printf (" = %" PRId64 "\n", total);
638 printf (" = %" PRIo64 "\n", total);
643 handle_elf (Elf *elf, const char *prefix, const char *fname)
645 size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
646 size_t fname_len = strlen (fname) + 1;
647 char fullname[prefix_len + 1 + fname_len];
650 /* Create the full name of the file. */
653 cp = mempcpy (cp, prefix, prefix_len);
656 memcpy (cp, fname, fname_len);
658 if (format == format_sysv)
659 show_sysv (elf, prefix, fname, fullname);
660 else if (format == format_sysv_one_line)
661 show_sysv_one_line (elf);
662 else if (format == format_segments)
663 show_segments (elf, fullname);
668 show_bsd (elf, prefix, fname, fullname);
673 #include "debugpred.h"