1 /* Print size information from ELF file.
2 Copyright (C) 2000-2007,2009,2012,2014 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/>. */
34 #include <stdio_ext.h>
38 #include <sys/param.h>
43 /* Name and version of program. */
44 static void print_version (FILE *stream, struct argp_state *state);
45 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
47 /* Bug report address. */
48 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
51 /* Values for the parameters which have no short form. */
52 #define OPT_FORMAT 0x100
53 #define OPT_RADIX 0x101
55 /* Definitions of arguments for argp functions. */
56 static const struct argp_option options[] =
58 { NULL, 0, NULL, 0, N_("Output format:"), 0 },
59 { "format", OPT_FORMAT, "FORMAT", 0,
60 N_("Use the output format FORMAT. FORMAT can be `bsd' or `sysv'. "
61 "The default is `bsd'"), 0 },
62 { NULL, 'A', NULL, 0, N_("Same as `--format=sysv'"), 0 },
63 { NULL, 'B', NULL, 0, N_("Same as `--format=bsd'"), 0 },
64 { "radix", OPT_RADIX, "RADIX", 0, N_("Use RADIX for printing symbol values"),
66 { NULL, 'd', NULL, 0, N_("Same as `--radix=10'"), 0 },
67 { NULL, 'o', NULL, 0, N_("Same as `--radix=8'"), 0 },
68 { NULL, 'x', NULL, 0, N_("Same as `--radix=16'"), 0 },
70 N_("Similar to `--format=sysv' output but in one line"), 0 },
72 { NULL, 0, NULL, 0, N_("Output options:"), 0 },
74 N_("Print size and permission flags for loadable segments"), 0 },
75 { "totals", 't', NULL, 0, N_("Display the total sizes (bsd only)"), 0 },
76 { NULL, 0, NULL, 0, NULL, 0 }
79 /* Short description of program. */
80 static const char doc[] = N_("\
81 List section sizes of FILEs (a.out by default).");
83 /* Strings for arguments in help texts. */
84 static const char args_doc[] = N_("[FILE...]");
86 /* Prototype for option handler. */
87 static error_t parse_opt (int key, char *arg, struct argp_state *state);
89 /* Data structure to communicate with argp functions. */
90 static struct argp argp =
92 options, parse_opt, args_doc, doc, NULL, NULL, NULL
96 /* Print symbols in file named FNAME. */
97 static int process_file (const char *fname);
99 /* Handle content of archive. */
100 static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname);
102 /* Handle ELF file. */
103 static void handle_elf (Elf *elf, const char *fullname, const char *fname);
105 /* Show total size. */
106 static void show_bsd_totals (void);
108 #define INTERNAL_ERROR(fname) \
109 error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s-%s): %s"), \
110 fname, __LINE__, PACKAGE_VERSION, __DATE__, elf_errmsg (-1))
113 /* User-selectable options. */
115 /* The selected output format. */
120 format_sysv_one_line,
124 /* Radix for printed numbers. */
133 /* Mapping of radix and binary class to length. */
134 static const int length_map[2][3] =
139 [radix_decimal] = 10,
145 [radix_decimal] = 20,
150 /* True if total sizes should be printed. */
152 /* To print the total sizes in a reasonable format remember the higest
153 "class" of ELF binaries processed. */
154 static int totals_class;
158 main (int argc, char *argv[])
163 /* Make memory leak detection possible. */
166 /* We use no threads here which can interfere with handling a stream. */
167 __fsetlocking (stdin, FSETLOCKING_BYCALLER);
168 __fsetlocking (stdout, FSETLOCKING_BYCALLER);
169 __fsetlocking (stderr, FSETLOCKING_BYCALLER);
172 setlocale (LC_ALL, "");
174 /* Make sure the message catalog can be found. */
175 bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
177 /* Initialize the message catalog. */
178 textdomain (PACKAGE_TARNAME);
180 /* Parse and process arguments. */
181 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
184 /* Tell the library which version we are expecting. */
185 elf_version (EV_CURRENT);
187 if (remaining == argc)
188 /* The user didn't specify a name so we use a.out. */
189 result = process_file ("a.out");
191 /* Process all the remaining files. */
193 result |= process_file (argv[remaining]);
194 while (++remaining < argc);
196 /* Print the total sizes but only if the output format is BSD and at
197 least one file has been correctly read (i.e., we recognized the
199 if (totals && format == format_bsd && totals_class != 0)
206 /* Print the version information. */
208 print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
210 fprintf (stream, "size (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
211 fprintf (stream, gettext ("\
212 Copyright (C) %s Red Hat, Inc.\n\
213 This is free software; see the source for copying conditions. There is NO\n\
214 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
216 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
220 /* Handle program arguments. */
222 parse_opt (int key, char *arg,
223 struct argp_state *state __attribute__ ((unused)))
228 radix = radix_decimal;
232 format = format_sysv_one_line;
244 format = format_sysv;
252 format = format_segments;
256 if (strcmp (arg, "bsd") == 0 || strcmp (arg, "berkeley") == 0)
258 else if (likely (strcmp (arg, "sysv") == 0))
259 format = format_sysv;
261 error (EXIT_FAILURE, 0, gettext ("Invalid format: %s"), arg);
265 if (strcmp (arg, "x") == 0 || strcmp (arg, "16") == 0)
267 else if (strcmp (arg, "d") == 0 || strcmp (arg, "10") == 0)
268 radix = radix_decimal;
269 else if (strcmp (arg, "o") == 0 || strcmp (arg, "8") == 0)
272 error (EXIT_FAILURE, 0, gettext ("Invalid radix: %s"), arg);
280 return ARGP_ERR_UNKNOWN;
286 /* Open the file and determine the type. */
288 process_file (const char *fname)
290 int fd = open (fname, O_RDONLY);
291 if (unlikely (fd == -1))
293 error (0, errno, gettext ("cannot open '%s'"), fname);
297 /* Now get the ELF descriptor. */
298 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
299 if (likely (elf != NULL))
301 if (elf_kind (elf) == ELF_K_ELF)
303 handle_elf (elf, NULL, fname);
305 if (unlikely (elf_end (elf) != 0))
306 INTERNAL_ERROR (fname);
308 if (unlikely (close (fd) != 0))
309 error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
313 else if (likely (elf_kind (elf) == ELF_K_AR))
315 int result = handle_ar (fd, elf, NULL, fname);
317 if (unlikely (close (fd) != 0))
318 error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
323 /* We cannot handle this type. Close the descriptor anyway. */
324 if (unlikely (elf_end (elf) != 0))
325 INTERNAL_ERROR (fname);
328 if (unlikely (close (fd) != 0))
329 error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
331 error (0, 0, gettext ("%s: file format not recognized"), fname);
337 /* Print the BSD-style header. This is done exactly once. */
339 print_header (Elf *elf)
345 int ddigits = length_map[gelf_getclass (elf) - 1][radix_decimal];
346 int xdigits = length_map[gelf_getclass (elf) - 1][radix_hex];
348 printf ("%*s %*s %*s %*s %*s %s\n",
349 ddigits - 2, sgettext ("bsd|text"),
350 ddigits - 2, sgettext ("bsd|data"),
351 ddigits - 2, sgettext ("bsd|bss"),
352 ddigits - 2, sgettext ("bsd|dec"),
353 xdigits - 2, sgettext ("bsd|hex"),
354 sgettext ("bsd|filename"));
362 handle_ar (int fd, Elf *elf, const char *prefix, const char *fname)
364 size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
365 size_t fname_len = strlen (fname) + 1;
366 char new_prefix[prefix_len + 1 + fname_len];
367 char *cp = new_prefix;
369 /* Create the full name of the file. */
372 cp = mempcpy (cp, prefix, prefix_len);
375 memcpy (cp, fname, fname_len);
377 /* Process all the files contained in the archive. */
380 Elf_Cmd cmd = ELF_C_READ_MMAP;
381 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
383 /* The the header for this element. */
384 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
386 if (elf_kind (subelf) == ELF_K_ELF)
387 handle_elf (subelf, new_prefix, arhdr->ar_name);
388 else if (likely (elf_kind (subelf) == ELF_K_AR))
389 result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name);
390 /* else signal error??? */
392 /* Get next archive element. */
393 cmd = elf_next (subelf);
394 if (unlikely (elf_end (subelf) != 0))
395 INTERNAL_ERROR (fname);
398 if (unlikely (elf_end (elf) != 0))
399 INTERNAL_ERROR (fname);
405 /* Show sizes in SysV format. */
407 show_sysv (Elf *elf, const char *prefix, const char *fname,
408 const char *fullname)
411 const int digits = length_map[gelf_getclass (elf) - 1][radix];
413 /* Get the section header string table index. */
415 if (unlikely (elf_getshdrstrndx (elf, &shstrndx) < 0))
416 error (EXIT_FAILURE, 0,
417 gettext ("cannot get section header string table index"));
419 /* First round over the sections: determine the longest section name. */
421 while ((scn = elf_nextscn (elf, scn)) != NULL)
424 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
427 INTERNAL_ERROR (fullname);
429 /* Ignore all sections which are not used at runtime. */
430 if ((shdr->sh_flags & SHF_ALLOC) != 0)
431 maxlen = MAX (maxlen,
432 (int) strlen (elf_strptr (elf, shstrndx,
436 fputs_unlocked (fname, stdout);
438 printf (gettext (" (ex %s)"), prefix);
439 printf (":\n%-*s %*s %*s\n",
440 maxlen, sgettext ("sysv|section"),
441 digits - 2, sgettext ("sysv|size"),
442 digits, sgettext ("sysv|addr"));
444 /* Iterate over all sections. */
446 while ((scn = elf_nextscn (elf, scn)) != NULL)
449 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
451 /* Ignore all sections which are not used at runtime. */
452 if ((shdr->sh_flags & SHF_ALLOC) != 0)
454 printf ((radix == radix_hex
455 ? "%-*s %*" PRIx64 " %*" PRIx64 "\n"
456 : (radix == radix_decimal
457 ? "%-*s %*" PRId64 " %*" PRId64 "\n"
458 : "%-*s %*" PRIo64 " %*" PRIo64 "\n")),
459 maxlen, elf_strptr (elf, shstrndx, shdr->sh_name),
460 digits - 2, shdr->sh_size,
461 digits, shdr->sh_addr);
463 total += shdr->sh_size;
467 if (radix == radix_hex)
468 printf ("%-*s %*" PRIx64 "\n\n\n", maxlen, sgettext ("sysv|Total"),
470 else if (radix == radix_decimal)
471 printf ("%-*s %*" PRId64 "\n\n\n", maxlen, sgettext ("sysv|Total"),
474 printf ("%-*s %*" PRIo64 "\n\n\n", maxlen, sgettext ("sysv|Total"),
479 /* Show sizes in SysV format in one line. */
481 show_sysv_one_line (Elf *elf)
483 /* Get the section header string table index. */
485 if (unlikely (elf_getshdrstrndx (elf, &shstrndx) < 0))
486 error (EXIT_FAILURE, 0,
487 gettext ("cannot get section header string table index"));
489 /* Iterate over all sections. */
493 while ((scn = elf_nextscn (elf, scn)) != NULL)
496 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
498 /* Ignore all sections which are not used at runtime. */
499 if ((shdr->sh_flags & SHF_ALLOC) == 0)
503 fputs_unlocked (" + ", stdout);
506 printf ((radix == radix_hex ? "%" PRIx64 "(%s)"
507 : (radix == radix_decimal ? "%" PRId64 "(%s)"
508 : "%" PRIo64 "(%s)")),
509 shdr->sh_size, elf_strptr (elf, shstrndx, shdr->sh_name));
511 total += shdr->sh_size;
514 if (radix == radix_hex)
515 printf (" = %#" PRIx64 "\n", total);
516 else if (radix == radix_decimal)
517 printf (" = %" PRId64 "\n", total);
519 printf (" = %" PRIo64 "\n", total);
523 /* Variables to add up the sizes of all files. */
524 static uintmax_t total_textsize;
525 static uintmax_t total_datasize;
526 static uintmax_t total_bsssize;
529 /* Show sizes in BSD format. */
531 show_bsd (Elf *elf, const char *prefix, const char *fname,
532 const char *fullname)
534 GElf_Off textsize = 0;
535 GElf_Off datasize = 0;
536 GElf_Off bsssize = 0;
537 const int ddigits = length_map[gelf_getclass (elf) - 1][radix_decimal];
538 const int xdigits = length_map[gelf_getclass (elf) - 1][radix_hex];
540 /* Iterate over all sections. */
542 while ((scn = elf_nextscn (elf, scn)) != NULL)
545 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
548 INTERNAL_ERROR (fullname);
550 /* Ignore all sections which are not marked as loaded. */
551 if ((shdr->sh_flags & SHF_ALLOC) == 0)
554 if ((shdr->sh_flags & SHF_WRITE) == 0)
555 textsize += shdr->sh_size;
556 else if (shdr->sh_type == SHT_NOBITS)
557 bsssize += shdr->sh_size;
559 datasize += shdr->sh_size;
562 printf ("%*" PRId64 " %*" PRId64 " %*" PRId64 " %*" PRId64 " %*"
564 ddigits - 2, textsize,
565 ddigits - 2, datasize,
566 ddigits - 2, bsssize,
567 ddigits - 2, textsize + datasize + bsssize,
568 xdigits - 2, textsize + datasize + bsssize,
571 printf (gettext (" (ex %s)"), prefix);
572 fputs_unlocked ("\n", stdout);
574 total_textsize += textsize;
575 total_datasize += datasize;
576 total_bsssize += bsssize;
578 totals_class = MAX (totals_class, gelf_getclass (elf));
582 /* Show total size. */
584 show_bsd_totals (void)
586 int ddigits = length_map[totals_class - 1][radix_decimal];
587 int xdigits = length_map[totals_class - 1][radix_hex];
589 printf ("%*" PRIuMAX " %*" PRIuMAX " %*" PRIuMAX " %*" PRIuMAX " %*"
591 ddigits - 2, total_textsize,
592 ddigits - 2, total_datasize,
593 ddigits - 2, total_bsssize,
594 ddigits - 2, total_textsize + total_datasize + total_bsssize,
595 xdigits - 2, total_textsize + total_datasize + total_bsssize,
596 gettext ("(TOTALS)\n"));
600 /* Show size and permission of loadable segments. */
602 show_segments (Elf *elf, const char *fullname)
605 GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
607 INTERNAL_ERROR (fullname);
611 for (size_t cnt = 0; cnt < ehdr->e_phnum; ++cnt)
616 phdr = gelf_getphdr (elf, cnt, &phdr_mem);
618 INTERNAL_ERROR (fullname);
620 if (phdr->p_type != PT_LOAD)
621 /* Only load segments. */
625 fputs_unlocked (" + ", stdout);
628 printf (radix == radix_hex ? "%" PRIx64 "(%c%c%c)"
629 : (radix == radix_decimal ? "%" PRId64 "(%c%c%c)"
630 : "%" PRIo64 "(%c%c%c)"),
632 (phdr->p_flags & PF_R) == 0 ? '-' : 'r',
633 (phdr->p_flags & PF_W) == 0 ? '-' : 'w',
634 (phdr->p_flags & PF_X) == 0 ? '-' : 'x');
636 total += phdr->p_memsz;
639 if (radix == radix_hex)
640 printf (" = %#" PRIx64 "\n", total);
641 else if (radix == radix_decimal)
642 printf (" = %" PRId64 "\n", total);
644 printf (" = %" PRIo64 "\n", total);
649 handle_elf (Elf *elf, const char *prefix, const char *fname)
651 size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
652 size_t fname_len = strlen (fname) + 1;
653 char fullname[prefix_len + 1 + fname_len];
656 /* Create the full name of the file. */
659 cp = mempcpy (cp, prefix, prefix_len);
662 memcpy (cp, fname, fname_len);
664 if (format == format_sysv)
665 show_sysv (elf, prefix, fname, fullname);
666 else if (format == format_sysv_one_line)
667 show_sysv_one_line (elf);
668 else if (format == format_segments)
669 show_segments (elf, fullname);
674 show_bsd (elf, prefix, fname, fullname);
679 #include "debugpred.h"