Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame^] | 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. |
| 5 | |
| 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. |
| 10 | |
| 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. |
| 15 | |
| 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/>. */ |
| 18 | |
| 19 | #ifdef HAVE_CONFIG_H |
| 20 | # include <config.h> |
| 21 | #endif |
| 22 | |
| 23 | #include <argp.h> |
| 24 | #include <error.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <gelf.h> |
| 27 | #include <inttypes.h> |
| 28 | #include <libelf.h> |
| 29 | #include <libintl.h> |
| 30 | #include <locale.h> |
| 31 | #include <stdbool.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdio_ext.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | #include <system.h> |
| 39 | #include <printversion.h> |
| 40 | |
| 41 | /* Name and version of program. */ |
| 42 | ARGP_PROGRAM_VERSION_HOOK_DEF = print_version; |
| 43 | |
| 44 | /* Bug report address. */ |
| 45 | ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT; |
| 46 | |
| 47 | |
| 48 | /* Values for the parameters which have no short form. */ |
| 49 | #define OPT_FORMAT 0x100 |
| 50 | #define OPT_RADIX 0x101 |
| 51 | |
| 52 | /* Definitions of arguments for argp functions. */ |
| 53 | static const struct argp_option options[] = |
| 54 | { |
| 55 | { NULL, 0, NULL, 0, N_("Output format:"), 0 }, |
| 56 | { "format", OPT_FORMAT, "FORMAT", 0, |
| 57 | N_("Use the output format FORMAT. FORMAT can be `bsd' or `sysv'. " |
| 58 | "The default is `bsd'"), 0 }, |
| 59 | { NULL, 'A', NULL, 0, N_("Same as `--format=sysv'"), 0 }, |
| 60 | { NULL, 'B', NULL, 0, N_("Same as `--format=bsd'"), 0 }, |
| 61 | { "radix", OPT_RADIX, "RADIX", 0, N_("Use RADIX for printing symbol values"), |
| 62 | 0}, |
| 63 | { NULL, 'd', NULL, 0, N_("Same as `--radix=10'"), 0 }, |
| 64 | { NULL, 'o', NULL, 0, N_("Same as `--radix=8'"), 0 }, |
| 65 | { NULL, 'x', NULL, 0, N_("Same as `--radix=16'"), 0 }, |
| 66 | { NULL, 'f', NULL, 0, |
| 67 | N_("Similar to `--format=sysv' output but in one line"), 0 }, |
| 68 | |
| 69 | { NULL, 0, NULL, 0, N_("Output options:"), 0 }, |
| 70 | { NULL, 'F', NULL, 0, |
| 71 | N_("Print size and permission flags for loadable segments"), 0 }, |
| 72 | { "totals", 't', NULL, 0, N_("Display the total sizes (bsd only)"), 0 }, |
| 73 | { NULL, 0, NULL, 0, NULL, 0 } |
| 74 | }; |
| 75 | |
| 76 | /* Short description of program. */ |
| 77 | static const char doc[] = N_("\ |
| 78 | List section sizes of FILEs (a.out by default)."); |
| 79 | |
| 80 | /* Strings for arguments in help texts. */ |
| 81 | static const char args_doc[] = N_("[FILE...]"); |
| 82 | |
| 83 | /* Prototype for option handler. */ |
| 84 | static error_t parse_opt (int key, char *arg, struct argp_state *state); |
| 85 | |
| 86 | /* Data structure to communicate with argp functions. */ |
| 87 | static struct argp argp = |
| 88 | { |
| 89 | options, parse_opt, args_doc, doc, NULL, NULL, NULL |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | /* Print symbols in file named FNAME. */ |
| 94 | static int process_file (const char *fname); |
| 95 | |
| 96 | /* Handle content of archive. */ |
| 97 | static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname); |
| 98 | |
| 99 | /* Handle ELF file. */ |
| 100 | static void handle_elf (Elf *elf, const char *fullname, const char *fname); |
| 101 | |
| 102 | /* Show total size. */ |
| 103 | static void show_bsd_totals (void); |
| 104 | |
| 105 | #define INTERNAL_ERROR(fname) \ |
| 106 | error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s): %s"), \ |
| 107 | fname, __LINE__, PACKAGE_VERSION, elf_errmsg (-1)) |
| 108 | |
| 109 | |
| 110 | /* User-selectable options. */ |
| 111 | |
| 112 | /* The selected output format. */ |
| 113 | static enum |
| 114 | { |
| 115 | format_bsd = 0, |
| 116 | format_sysv, |
| 117 | format_sysv_one_line, |
| 118 | format_segments |
| 119 | } format; |
| 120 | |
| 121 | /* Radix for printed numbers. */ |
| 122 | static enum |
| 123 | { |
| 124 | radix_decimal = 0, |
| 125 | radix_hex, |
| 126 | radix_octal |
| 127 | } radix; |
| 128 | |
| 129 | |
| 130 | /* Mapping of radix and binary class to length. */ |
| 131 | static const int length_map[2][3] = |
| 132 | { |
| 133 | [ELFCLASS32 - 1] = |
| 134 | { |
| 135 | [radix_hex] = 8, |
| 136 | [radix_decimal] = 10, |
| 137 | [radix_octal] = 11 |
| 138 | }, |
| 139 | [ELFCLASS64 - 1] = |
| 140 | { |
| 141 | [radix_hex] = 16, |
| 142 | [radix_decimal] = 20, |
| 143 | [radix_octal] = 22 |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | /* True if total sizes should be printed. */ |
| 148 | static bool totals; |
| 149 | /* To print the total sizes in a reasonable format remember the higest |
| 150 | "class" of ELF binaries processed. */ |
| 151 | static int totals_class; |
| 152 | |
| 153 | |
| 154 | int |
| 155 | main (int argc, char *argv[]) |
| 156 | { |
| 157 | int remaining; |
| 158 | int result = 0; |
| 159 | |
| 160 | /* We use no threads here which can interfere with handling a stream. */ |
| 161 | __fsetlocking (stdin, FSETLOCKING_BYCALLER); |
| 162 | __fsetlocking (stdout, FSETLOCKING_BYCALLER); |
| 163 | __fsetlocking (stderr, FSETLOCKING_BYCALLER); |
| 164 | |
| 165 | /* Set locale. */ |
| 166 | setlocale (LC_ALL, ""); |
| 167 | |
| 168 | /* Make sure the message catalog can be found. */ |
| 169 | bindtextdomain (PACKAGE_TARNAME, LOCALEDIR); |
| 170 | |
| 171 | /* Initialize the message catalog. */ |
| 172 | textdomain (PACKAGE_TARNAME); |
| 173 | |
| 174 | /* Parse and process arguments. */ |
| 175 | argp_parse (&argp, argc, argv, 0, &remaining, NULL); |
| 176 | |
| 177 | |
| 178 | /* Tell the library which version we are expecting. */ |
| 179 | elf_version (EV_CURRENT); |
| 180 | |
| 181 | if (remaining == argc) |
| 182 | /* The user didn't specify a name so we use a.out. */ |
| 183 | result = process_file ("a.out"); |
| 184 | else |
| 185 | /* Process all the remaining files. */ |
| 186 | do |
| 187 | result |= process_file (argv[remaining]); |
| 188 | while (++remaining < argc); |
| 189 | |
| 190 | /* Print the total sizes but only if the output format is BSD and at |
| 191 | least one file has been correctly read (i.e., we recognized the |
| 192 | class). */ |
| 193 | if (totals && format == format_bsd && totals_class != 0) |
| 194 | show_bsd_totals (); |
| 195 | |
| 196 | return result; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /* Handle program arguments. */ |
| 201 | static error_t |
| 202 | parse_opt (int key, char *arg, |
| 203 | struct argp_state *state __attribute__ ((unused))) |
| 204 | { |
| 205 | switch (key) |
| 206 | { |
| 207 | case 'd': |
| 208 | radix = radix_decimal; |
| 209 | break; |
| 210 | |
| 211 | case 'f': |
| 212 | format = format_sysv_one_line; |
| 213 | break; |
| 214 | |
| 215 | case 'o': |
| 216 | radix = radix_octal; |
| 217 | break; |
| 218 | |
| 219 | case 'x': |
| 220 | radix = radix_hex; |
| 221 | break; |
| 222 | |
| 223 | case 'A': |
| 224 | format = format_sysv; |
| 225 | break; |
| 226 | |
| 227 | case 'B': |
| 228 | format = format_bsd; |
| 229 | break; |
| 230 | |
| 231 | case 'F': |
| 232 | format = format_segments; |
| 233 | break; |
| 234 | |
| 235 | case OPT_FORMAT: |
| 236 | if (strcmp (arg, "bsd") == 0 || strcmp (arg, "berkeley") == 0) |
| 237 | format = format_bsd; |
| 238 | else if (likely (strcmp (arg, "sysv") == 0)) |
| 239 | format = format_sysv; |
| 240 | else |
| 241 | error (EXIT_FAILURE, 0, gettext ("Invalid format: %s"), arg); |
| 242 | break; |
| 243 | |
| 244 | case OPT_RADIX: |
| 245 | if (strcmp (arg, "x") == 0 || strcmp (arg, "16") == 0) |
| 246 | radix = radix_hex; |
| 247 | else if (strcmp (arg, "d") == 0 || strcmp (arg, "10") == 0) |
| 248 | radix = radix_decimal; |
| 249 | else if (strcmp (arg, "o") == 0 || strcmp (arg, "8") == 0) |
| 250 | radix = radix_octal; |
| 251 | else |
| 252 | error (EXIT_FAILURE, 0, gettext ("Invalid radix: %s"), arg); |
| 253 | break; |
| 254 | |
| 255 | case 't': |
| 256 | totals = true; |
| 257 | break; |
| 258 | |
| 259 | default: |
| 260 | return ARGP_ERR_UNKNOWN; |
| 261 | } |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /* Open the file and determine the type. */ |
| 267 | static int |
| 268 | process_file (const char *fname) |
| 269 | { |
| 270 | int fd = open (fname, O_RDONLY); |
| 271 | if (unlikely (fd == -1)) |
| 272 | { |
| 273 | error (0, errno, gettext ("cannot open '%s'"), fname); |
| 274 | return 1; |
| 275 | } |
| 276 | |
| 277 | /* Now get the ELF descriptor. */ |
| 278 | Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL); |
| 279 | if (likely (elf != NULL)) |
| 280 | { |
| 281 | if (elf_kind (elf) == ELF_K_ELF) |
| 282 | { |
| 283 | handle_elf (elf, NULL, fname); |
| 284 | |
| 285 | if (unlikely (elf_end (elf) != 0)) |
| 286 | INTERNAL_ERROR (fname); |
| 287 | |
| 288 | if (unlikely (close (fd) != 0)) |
| 289 | error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | else if (likely (elf_kind (elf) == ELF_K_AR)) |
| 294 | { |
| 295 | int result = handle_ar (fd, elf, NULL, fname); |
| 296 | |
| 297 | if (unlikely (close (fd) != 0)) |
| 298 | error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname); |
| 299 | |
| 300 | return result; |
| 301 | } |
| 302 | |
| 303 | /* We cannot handle this type. Close the descriptor anyway. */ |
| 304 | if (unlikely (elf_end (elf) != 0)) |
| 305 | INTERNAL_ERROR (fname); |
| 306 | } |
| 307 | |
| 308 | if (unlikely (close (fd) != 0)) |
| 309 | error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname); |
| 310 | |
| 311 | error (0, 0, gettext ("%s: file format not recognized"), fname); |
| 312 | |
| 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /* Print the BSD-style header. This is done exactly once. */ |
| 318 | static void |
| 319 | print_header (Elf *elf) |
| 320 | { |
| 321 | static int done; |
| 322 | |
| 323 | if (! done) |
| 324 | { |
| 325 | int ddigits = length_map[gelf_getclass (elf) - 1][radix_decimal]; |
| 326 | int xdigits = length_map[gelf_getclass (elf) - 1][radix_hex]; |
| 327 | |
| 328 | printf ("%*s %*s %*s %*s %*s %s\n", |
| 329 | ddigits - 2, sgettext ("bsd|text"), |
| 330 | ddigits - 2, sgettext ("bsd|data"), |
| 331 | ddigits - 2, sgettext ("bsd|bss"), |
| 332 | ddigits - 2, sgettext ("bsd|dec"), |
| 333 | xdigits - 2, sgettext ("bsd|hex"), |
| 334 | sgettext ("bsd|filename")); |
| 335 | |
| 336 | done = 1; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | |
| 341 | static int |
| 342 | handle_ar (int fd, Elf *elf, const char *prefix, const char *fname) |
| 343 | { |
| 344 | size_t prefix_len = prefix == NULL ? 0 : strlen (prefix); |
| 345 | size_t fname_len = strlen (fname) + 1; |
| 346 | char new_prefix[prefix_len + 1 + fname_len]; |
| 347 | char *cp = new_prefix; |
| 348 | |
| 349 | /* Create the full name of the file. */ |
| 350 | if (prefix != NULL) |
| 351 | { |
| 352 | cp = mempcpy (cp, prefix, prefix_len); |
| 353 | *cp++ = ':'; |
| 354 | } |
| 355 | memcpy (cp, fname, fname_len); |
| 356 | |
| 357 | /* Process all the files contained in the archive. */ |
| 358 | int result = 0; |
| 359 | Elf *subelf; |
| 360 | Elf_Cmd cmd = ELF_C_READ_MMAP; |
| 361 | while ((subelf = elf_begin (fd, cmd, elf)) != NULL) |
| 362 | { |
| 363 | /* The the header for this element. */ |
| 364 | Elf_Arhdr *arhdr = elf_getarhdr (subelf); |
| 365 | |
| 366 | if (elf_kind (subelf) == ELF_K_ELF) |
| 367 | handle_elf (subelf, new_prefix, arhdr->ar_name); |
| 368 | else if (likely (elf_kind (subelf) == ELF_K_AR)) |
| 369 | result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name); |
| 370 | /* else signal error??? */ |
| 371 | |
| 372 | /* Get next archive element. */ |
| 373 | cmd = elf_next (subelf); |
| 374 | if (unlikely (elf_end (subelf) != 0)) |
| 375 | INTERNAL_ERROR (fname); |
| 376 | } |
| 377 | |
| 378 | if (unlikely (elf_end (elf) != 0)) |
| 379 | INTERNAL_ERROR (fname); |
| 380 | |
| 381 | return result; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | /* Show sizes in SysV format. */ |
| 386 | static void |
| 387 | show_sysv (Elf *elf, const char *prefix, const char *fname, |
| 388 | const char *fullname) |
| 389 | { |
| 390 | int maxlen = 10; |
| 391 | const int digits = length_map[gelf_getclass (elf) - 1][radix]; |
| 392 | |
| 393 | /* Get the section header string table index. */ |
| 394 | size_t shstrndx; |
| 395 | if (unlikely (elf_getshdrstrndx (elf, &shstrndx) < 0)) |
| 396 | error (EXIT_FAILURE, 0, |
| 397 | gettext ("cannot get section header string table index")); |
| 398 | |
| 399 | /* First round over the sections: determine the longest section name. */ |
| 400 | Elf_Scn *scn = NULL; |
| 401 | while ((scn = elf_nextscn (elf, scn)) != NULL) |
| 402 | { |
| 403 | GElf_Shdr shdr_mem; |
| 404 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); |
| 405 | |
| 406 | if (shdr == NULL) |
| 407 | INTERNAL_ERROR (fullname); |
| 408 | |
| 409 | /* Ignore all sections which are not used at runtime. */ |
| 410 | const char *name = elf_strptr (elf, shstrndx, shdr->sh_name); |
| 411 | if (name != NULL && (shdr->sh_flags & SHF_ALLOC) != 0) |
| 412 | maxlen = MAX (maxlen, (int) strlen (name)); |
| 413 | } |
| 414 | |
| 415 | fputs_unlocked (fname, stdout); |
| 416 | if (prefix != NULL) |
| 417 | printf (gettext (" (ex %s)"), prefix); |
| 418 | printf (":\n%-*s %*s %*s\n", |
| 419 | maxlen, sgettext ("sysv|section"), |
| 420 | digits - 2, sgettext ("sysv|size"), |
| 421 | digits, sgettext ("sysv|addr")); |
| 422 | |
| 423 | /* Iterate over all sections. */ |
| 424 | GElf_Off total = 0; |
| 425 | while ((scn = elf_nextscn (elf, scn)) != NULL) |
| 426 | { |
| 427 | GElf_Shdr shdr_mem; |
| 428 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); |
| 429 | |
| 430 | /* Ignore all sections which are not used at runtime. */ |
| 431 | if ((shdr->sh_flags & SHF_ALLOC) != 0) |
| 432 | { |
| 433 | printf ((radix == radix_hex |
| 434 | ? "%-*s %*" PRIx64 " %*" PRIx64 "\n" |
| 435 | : (radix == radix_decimal |
| 436 | ? "%-*s %*" PRId64 " %*" PRId64 "\n" |
| 437 | : "%-*s %*" PRIo64 " %*" PRIo64 "\n")), |
| 438 | maxlen, elf_strptr (elf, shstrndx, shdr->sh_name), |
| 439 | digits - 2, shdr->sh_size, |
| 440 | digits, shdr->sh_addr); |
| 441 | |
| 442 | total += shdr->sh_size; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (radix == radix_hex) |
| 447 | printf ("%-*s %*" PRIx64 "\n\n\n", maxlen, sgettext ("sysv|Total"), |
| 448 | digits - 2, total); |
| 449 | else if (radix == radix_decimal) |
| 450 | printf ("%-*s %*" PRId64 "\n\n\n", maxlen, sgettext ("sysv|Total"), |
| 451 | digits - 2, total); |
| 452 | else |
| 453 | printf ("%-*s %*" PRIo64 "\n\n\n", maxlen, sgettext ("sysv|Total"), |
| 454 | digits - 2, total); |
| 455 | } |
| 456 | |
| 457 | |
| 458 | /* Show sizes in SysV format in one line. */ |
| 459 | static void |
| 460 | show_sysv_one_line (Elf *elf) |
| 461 | { |
| 462 | /* Get the section header string table index. */ |
| 463 | size_t shstrndx; |
| 464 | if (unlikely (elf_getshdrstrndx (elf, &shstrndx) < 0)) |
| 465 | error (EXIT_FAILURE, 0, |
| 466 | gettext ("cannot get section header string table index")); |
| 467 | |
| 468 | /* Iterate over all sections. */ |
| 469 | GElf_Off total = 0; |
| 470 | bool first = true; |
| 471 | Elf_Scn *scn = NULL; |
| 472 | while ((scn = elf_nextscn (elf, scn)) != NULL) |
| 473 | { |
| 474 | GElf_Shdr shdr_mem; |
| 475 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); |
| 476 | |
| 477 | /* Ignore all sections which are not used at runtime. */ |
| 478 | if ((shdr->sh_flags & SHF_ALLOC) == 0) |
| 479 | continue; |
| 480 | |
| 481 | if (! first) |
| 482 | fputs_unlocked (" + ", stdout); |
| 483 | first = false; |
| 484 | |
| 485 | printf ((radix == radix_hex ? "%" PRIx64 "(%s)" |
| 486 | : (radix == radix_decimal ? "%" PRId64 "(%s)" |
| 487 | : "%" PRIo64 "(%s)")), |
| 488 | shdr->sh_size, elf_strptr (elf, shstrndx, shdr->sh_name)); |
| 489 | |
| 490 | total += shdr->sh_size; |
| 491 | } |
| 492 | |
| 493 | if (radix == radix_hex) |
| 494 | printf (" = %#" PRIx64 "\n", total); |
| 495 | else if (radix == radix_decimal) |
| 496 | printf (" = %" PRId64 "\n", total); |
| 497 | else |
| 498 | printf (" = %" PRIo64 "\n", total); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | /* Variables to add up the sizes of all files. */ |
| 503 | static uintmax_t total_textsize; |
| 504 | static uintmax_t total_datasize; |
| 505 | static uintmax_t total_bsssize; |
| 506 | |
| 507 | |
| 508 | /* Show sizes in BSD format. */ |
| 509 | static void |
| 510 | show_bsd (Elf *elf, const char *prefix, const char *fname, |
| 511 | const char *fullname) |
| 512 | { |
| 513 | GElf_Off textsize = 0; |
| 514 | GElf_Off datasize = 0; |
| 515 | GElf_Off bsssize = 0; |
| 516 | const int ddigits = length_map[gelf_getclass (elf) - 1][radix_decimal]; |
| 517 | const int xdigits = length_map[gelf_getclass (elf) - 1][radix_hex]; |
| 518 | |
| 519 | /* Iterate over all sections. */ |
| 520 | Elf_Scn *scn = NULL; |
| 521 | while ((scn = elf_nextscn (elf, scn)) != NULL) |
| 522 | { |
| 523 | GElf_Shdr shdr_mem; |
| 524 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); |
| 525 | |
| 526 | if (shdr == NULL) |
| 527 | INTERNAL_ERROR (fullname); |
| 528 | |
| 529 | /* Ignore all sections which are not marked as loaded. */ |
| 530 | if ((shdr->sh_flags & SHF_ALLOC) == 0) |
| 531 | continue; |
| 532 | |
| 533 | if ((shdr->sh_flags & SHF_WRITE) == 0) |
| 534 | textsize += shdr->sh_size; |
| 535 | else if (shdr->sh_type == SHT_NOBITS) |
| 536 | bsssize += shdr->sh_size; |
| 537 | else |
| 538 | datasize += shdr->sh_size; |
| 539 | } |
| 540 | |
| 541 | printf ("%*" PRId64 " %*" PRId64 " %*" PRId64 " %*" PRId64 " %*" |
| 542 | PRIx64 " %s", |
| 543 | ddigits - 2, textsize, |
| 544 | ddigits - 2, datasize, |
| 545 | ddigits - 2, bsssize, |
| 546 | ddigits - 2, textsize + datasize + bsssize, |
| 547 | xdigits - 2, textsize + datasize + bsssize, |
| 548 | fname); |
| 549 | if (prefix != NULL) |
| 550 | printf (gettext (" (ex %s)"), prefix); |
| 551 | fputs_unlocked ("\n", stdout); |
| 552 | |
| 553 | total_textsize += textsize; |
| 554 | total_datasize += datasize; |
| 555 | total_bsssize += bsssize; |
| 556 | |
| 557 | totals_class = MAX (totals_class, gelf_getclass (elf)); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | /* Show total size. */ |
| 562 | static void |
| 563 | show_bsd_totals (void) |
| 564 | { |
| 565 | int ddigits = length_map[totals_class - 1][radix_decimal]; |
| 566 | int xdigits = length_map[totals_class - 1][radix_hex]; |
| 567 | |
| 568 | printf ("%*" PRIuMAX " %*" PRIuMAX " %*" PRIuMAX " %*" PRIuMAX " %*" |
| 569 | PRIxMAX " %s", |
| 570 | ddigits - 2, total_textsize, |
| 571 | ddigits - 2, total_datasize, |
| 572 | ddigits - 2, total_bsssize, |
| 573 | ddigits - 2, total_textsize + total_datasize + total_bsssize, |
| 574 | xdigits - 2, total_textsize + total_datasize + total_bsssize, |
| 575 | gettext ("(TOTALS)\n")); |
| 576 | } |
| 577 | |
| 578 | |
| 579 | /* Show size and permission of loadable segments. */ |
| 580 | static void |
| 581 | show_segments (Elf *elf, const char *fullname) |
| 582 | { |
| 583 | size_t phnum; |
| 584 | if (elf_getphdrnum (elf, &phnum) != 0) |
| 585 | INTERNAL_ERROR (fullname); |
| 586 | |
| 587 | GElf_Off total = 0; |
| 588 | bool first = true; |
| 589 | for (size_t cnt = 0; cnt < phnum; ++cnt) |
| 590 | { |
| 591 | GElf_Phdr phdr_mem; |
| 592 | GElf_Phdr *phdr; |
| 593 | |
| 594 | phdr = gelf_getphdr (elf, cnt, &phdr_mem); |
| 595 | if (phdr == NULL) |
| 596 | INTERNAL_ERROR (fullname); |
| 597 | |
| 598 | if (phdr->p_type != PT_LOAD) |
| 599 | /* Only load segments. */ |
| 600 | continue; |
| 601 | |
| 602 | if (! first) |
| 603 | fputs_unlocked (" + ", stdout); |
| 604 | first = false; |
| 605 | |
| 606 | printf (radix == radix_hex ? "%" PRIx64 "(%c%c%c)" |
| 607 | : (radix == radix_decimal ? "%" PRId64 "(%c%c%c)" |
| 608 | : "%" PRIo64 "(%c%c%c)"), |
| 609 | phdr->p_memsz, |
| 610 | (phdr->p_flags & PF_R) == 0 ? '-' : 'r', |
| 611 | (phdr->p_flags & PF_W) == 0 ? '-' : 'w', |
| 612 | (phdr->p_flags & PF_X) == 0 ? '-' : 'x'); |
| 613 | |
| 614 | total += phdr->p_memsz; |
| 615 | } |
| 616 | |
| 617 | if (radix == radix_hex) |
| 618 | printf (" = %#" PRIx64 "\n", total); |
| 619 | else if (radix == radix_decimal) |
| 620 | printf (" = %" PRId64 "\n", total); |
| 621 | else |
| 622 | printf (" = %" PRIo64 "\n", total); |
| 623 | } |
| 624 | |
| 625 | |
| 626 | static void |
| 627 | handle_elf (Elf *elf, const char *prefix, const char *fname) |
| 628 | { |
| 629 | size_t prefix_len = prefix == NULL ? 0 : strlen (prefix); |
| 630 | size_t fname_len = strlen (fname) + 1; |
| 631 | char fullname[prefix_len + 1 + fname_len]; |
| 632 | char *cp = fullname; |
| 633 | |
| 634 | /* Create the full name of the file. */ |
| 635 | if (prefix != NULL) |
| 636 | { |
| 637 | cp = mempcpy (cp, prefix, prefix_len); |
| 638 | *cp++ = ':'; |
| 639 | } |
| 640 | memcpy (cp, fname, fname_len); |
| 641 | |
| 642 | if (format == format_sysv) |
| 643 | show_sysv (elf, prefix, fname, fullname); |
| 644 | else if (format == format_sysv_one_line) |
| 645 | show_sysv_one_line (elf); |
| 646 | else if (format == format_segments) |
| 647 | show_segments (elf, fullname); |
| 648 | else |
| 649 | { |
| 650 | print_header (elf); |
| 651 | |
| 652 | show_bsd (elf, prefix, fname, fullname); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | |
| 657 | #include "debugpred.h" |