1 /* nm.c -- Describe symbol table of a rel file. 2 Copyright (C) 1991-2016 Free Software Foundation, Inc. 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 3 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 18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 19 02110-1301, USA. */ 20 21 #include "sysdep.h" 22 #include "bfd.h" 23 #include "progress.h" 24 #include "getopt.h" 25 #include "aout/stab_gnu.h" 26 #include "aout/ranlib.h" 27 #include "demangle.h" 28 #include "libiberty.h" 29 #include "elf-bfd.h" 30 #include "elf/common.h" 31 #define DO_NOT_DEFINE_AOUTHDR 32 #define DO_NOT_DEFINE_FILHDR 33 #define DO_NOT_DEFINE_LINENO 34 #define DO_NOT_DEFINE_SCNHDR 35 #include "coff/external.h" 36 #include "coff/internal.h" 37 #include "libcoff.h" 38 #include "bucomm.h" 39 #include "plugin.h" 40 41 #ifdef __MACH__ 42 #undef HAVE_SBRK 43 #endif 44 45 /* When sorting by size, we use this structure to hold the size and a 46 pointer to the minisymbol. */ 47 48 struct size_sym 49 { 50 const void *minisym; 51 bfd_vma size; 52 }; 53 54 /* When fetching relocs, we use this structure to pass information to 55 get_relocs. */ 56 57 struct get_relocs_info 58 { 59 asection **secs; 60 arelent ***relocs; 61 long *relcount; 62 asymbol **syms; 63 }; 64 65 struct extended_symbol_info 66 { 67 symbol_info *sinfo; 68 bfd_vma ssize; 69 elf_symbol_type *elfinfo; 70 coff_symbol_type *coffinfo; 71 /* FIXME: We should add more fields for Type, Line, Section. */ 72 }; 73 #define SYM_NAME(sym) (sym->sinfo->name) 74 #define SYM_VALUE(sym) (sym->sinfo->value) 75 #define SYM_TYPE(sym) (sym->sinfo->type) 76 #define SYM_STAB_NAME(sym) (sym->sinfo->stab_name) 77 #define SYM_STAB_DESC(sym) (sym->sinfo->stab_desc) 78 #define SYM_STAB_OTHER(sym) (sym->sinfo->stab_other) 79 #define SYM_SIZE(sym) \ 80 (sym->elfinfo ? sym->elfinfo->internal_elf_sym.st_size: sym->ssize) 81 82 /* The output formatting functions. */ 83 static void print_object_filename_bsd (char *); 84 static void print_object_filename_sysv (char *); 85 static void print_object_filename_posix (char *); 86 static void print_archive_filename_bsd (char *); 87 static void print_archive_filename_sysv (char *); 88 static void print_archive_filename_posix (char *); 89 static void print_archive_member_bsd (char *, const char *); 90 static void print_archive_member_sysv (char *, const char *); 91 static void print_archive_member_posix (char *, const char *); 92 static void print_symbol_filename_bsd (bfd *, bfd *); 93 static void print_symbol_filename_sysv (bfd *, bfd *); 94 static void print_symbol_filename_posix (bfd *, bfd *); 95 static void print_value (bfd *, bfd_vma); 96 static void print_symbol_info_bsd (struct extended_symbol_info *, bfd *); 97 static void print_symbol_info_sysv (struct extended_symbol_info *, bfd *); 98 static void print_symbol_info_posix (struct extended_symbol_info *, bfd *); 99 100 /* Support for different output formats. */ 101 struct output_fns 102 { 103 /* Print the name of an object file given on the command line. */ 104 void (*print_object_filename) (char *); 105 106 /* Print the name of an archive file given on the command line. */ 107 void (*print_archive_filename) (char *); 108 109 /* Print the name of an archive member file. */ 110 void (*print_archive_member) (char *, const char *); 111 112 /* Print the name of the file (and archive, if there is one) 113 containing a symbol. */ 114 void (*print_symbol_filename) (bfd *, bfd *); 115 116 /* Print a line of information about a symbol. */ 117 void (*print_symbol_info) (struct extended_symbol_info *, bfd *); 118 }; 119 120 static struct output_fns formats[] = 121 { 122 {print_object_filename_bsd, 123 print_archive_filename_bsd, 124 print_archive_member_bsd, 125 print_symbol_filename_bsd, 126 print_symbol_info_bsd}, 127 {print_object_filename_sysv, 128 print_archive_filename_sysv, 129 print_archive_member_sysv, 130 print_symbol_filename_sysv, 131 print_symbol_info_sysv}, 132 {print_object_filename_posix, 133 print_archive_filename_posix, 134 print_archive_member_posix, 135 print_symbol_filename_posix, 136 print_symbol_info_posix} 137 }; 138 139 /* Indices in `formats'. */ 140 #define FORMAT_BSD 0 141 #define FORMAT_SYSV 1 142 #define FORMAT_POSIX 2 143 #define FORMAT_DEFAULT FORMAT_BSD 144 145 /* The output format to use. */ 146 static struct output_fns *format = &formats[FORMAT_DEFAULT]; 147 148 /* Command options. */ 149 150 static int do_demangle = 0; /* Pretty print C++ symbol names. */ 151 static int external_only = 0; /* Print external symbols only. */ 152 static int defined_only = 0; /* Print defined symbols only. */ 153 static int no_sort = 0; /* Don't sort; print syms in order found. */ 154 static int print_debug_syms = 0;/* Print debugger-only symbols too. */ 155 static int print_armap = 0; /* Describe __.SYMDEF data in archive files. */ 156 static int print_size = 0; /* Print size of defined symbols. */ 157 static int reverse_sort = 0; /* Sort in downward(alpha or numeric) order. */ 158 static int sort_numerically = 0;/* Sort in numeric rather than alpha order. */ 159 static int sort_by_size = 0; /* Sort by size of symbol. */ 160 static int undefined_only = 0; /* Print undefined symbols only. */ 161 static int dynamic = 0; /* Print dynamic symbols. */ 162 static int show_version = 0; /* Show the version number. */ 163 static int show_stats = 0; /* Show statistics. */ 164 static int show_synthetic = 0; /* Display synthesized symbols too. */ 165 static int line_numbers = 0; /* Print line numbers for symbols. */ 166 static int allow_special_symbols = 0; /* Allow special symbols. */ 167 168 /* When to print the names of files. Not mutually exclusive in SYSV format. */ 169 static int filename_per_file = 0; /* Once per file, on its own line. */ 170 static int filename_per_symbol = 0; /* Once per symbol, at start of line. */ 171 172 /* Print formats for printing a symbol value. */ 173 static char value_format_32bit[] = "%08lx"; 174 #if BFD_HOST_64BIT_LONG 175 static char value_format_64bit[] = "%016lx"; 176 #elif BFD_HOST_64BIT_LONG_LONG 177 #ifndef __MSVCRT__ 178 static char value_format_64bit[] = "%016llx"; 179 #else 180 static char value_format_64bit[] = "%016I64x"; 181 #endif 182 #endif 183 static int print_width = 0; 184 static int print_radix = 16; 185 /* Print formats for printing stab info. */ 186 static char other_format[] = "%02x"; 187 static char desc_format[] = "%04x"; 188 189 static char *target = NULL; 190 #if BFD_SUPPORTS_PLUGINS 191 static const char *plugin_target = "plugin"; 192 #else 193 static const char *plugin_target = NULL; 194 #endif 195 196 /* Used to cache the line numbers for a BFD. */ 197 static bfd *lineno_cache_bfd; 198 static bfd *lineno_cache_rel_bfd; 199 200 #define OPTION_TARGET 200 201 #define OPTION_PLUGIN (OPTION_TARGET + 1) 202 #define OPTION_SIZE_SORT (OPTION_PLUGIN + 1) 203 204 static struct option long_options[] = 205 { 206 {"debug-syms", no_argument, &print_debug_syms, 1}, 207 {"demangle", optional_argument, 0, 'C'}, 208 {"dynamic", no_argument, &dynamic, 1}, 209 {"extern-only", no_argument, &external_only, 1}, 210 {"format", required_argument, 0, 'f'}, 211 {"help", no_argument, 0, 'h'}, 212 {"line-numbers", no_argument, 0, 'l'}, 213 {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */ 214 {"no-demangle", no_argument, &do_demangle, 0}, 215 {"no-sort", no_argument, 0, 'p'}, 216 {"numeric-sort", no_argument, 0, 'n'}, 217 {"plugin", required_argument, 0, OPTION_PLUGIN}, 218 {"portability", no_argument, 0, 'P'}, 219 {"print-armap", no_argument, &print_armap, 1}, 220 {"print-file-name", no_argument, 0, 'o'}, 221 {"print-size", no_argument, 0, 'S'}, 222 {"radix", required_argument, 0, 't'}, 223 {"reverse-sort", no_argument, &reverse_sort, 1}, 224 {"size-sort", no_argument, 0, OPTION_SIZE_SORT}, 225 {"special-syms", no_argument, &allow_special_symbols, 1}, 226 {"stats", no_argument, &show_stats, 1}, 227 {"synthetic", no_argument, &show_synthetic, 1}, 228 {"target", required_argument, 0, OPTION_TARGET}, 229 {"defined-only", no_argument, &defined_only, 1}, 230 {"undefined-only", no_argument, &undefined_only, 1}, 231 {"version", no_argument, &show_version, 1}, 232 {0, no_argument, 0, 0} 233 }; 234 235 /* Some error-reporting functions. */ 237 238 static void 239 usage (FILE *stream, int status) 240 { 241 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name); 242 fprintf (stream, _(" List symbols in [file(s)] (a.out by default).\n")); 243 fprintf (stream, _(" The options are:\n\ 244 -a, --debug-syms Display debugger-only symbols\n\ 245 -A, --print-file-name Print name of the input file before every symbol\n\ 246 -B Same as --format=bsd\n\ 247 -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\ 248 The STYLE, if specified, can be `auto' (the default),\n\ 249 `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\ 250 or `gnat'\n\ 251 --no-demangle Do not demangle low-level symbol names\n\ 252 -D, --dynamic Display dynamic symbols instead of normal symbols\n\ 253 --defined-only Display only defined symbols\n\ 254 -e (ignored)\n\ 255 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\ 256 `sysv' or `posix'. The default is `bsd'\n\ 257 -g, --extern-only Display only external symbols\n\ 258 -l, --line-numbers Use debugging information to find a filename and\n\ 259 line number for each symbol\n\ 260 -n, --numeric-sort Sort symbols numerically by address\n\ 261 -o Same as -A\n\ 262 -p, --no-sort Do not sort the symbols\n\ 263 -P, --portability Same as --format=posix\n\ 264 -r, --reverse-sort Reverse the sense of the sort\n")); 265 #if BFD_SUPPORTS_PLUGINS 266 fprintf (stream, _("\ 267 --plugin NAME Load the specified plugin\n")); 268 #endif 269 fprintf (stream, _("\ 270 -S, --print-size Print size of defined symbols\n\ 271 -s, --print-armap Include index for symbols from archive members\n\ 272 --size-sort Sort symbols by size\n\ 273 --special-syms Include special symbols in the output\n\ 274 --synthetic Display synthetic symbols as well\n\ 275 -t, --radix=RADIX Use RADIX for printing symbol values\n\ 276 --target=BFDNAME Specify the target object format as BFDNAME\n\ 277 -u, --undefined-only Display only undefined symbols\n\ 278 -X 32_64 (ignored)\n\ 279 @FILE Read options from FILE\n\ 280 -h, --help Display this information\n\ 281 -V, --version Display this program's version number\n\ 282 \n")); 283 list_supported_targets (program_name, stream); 284 if (REPORT_BUGS_TO[0] && status == 0) 285 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO); 286 exit (status); 287 } 288 289 /* Set the radix for the symbol value and size according to RADIX. */ 290 291 static void 292 set_print_radix (char *radix) 293 { 294 switch (*radix) 295 { 296 case 'x': 297 break; 298 case 'd': 299 case 'o': 300 if (*radix == 'd') 301 print_radix = 10; 302 else 303 print_radix = 8; 304 value_format_32bit[4] = *radix; 305 #if BFD_HOST_64BIT_LONG 306 value_format_64bit[5] = *radix; 307 #elif BFD_HOST_64BIT_LONG_LONG 308 #ifndef __MSVCRT__ 309 value_format_64bit[6] = *radix; 310 #else 311 value_format_64bit[7] = *radix; 312 #endif 313 #endif 314 other_format[3] = desc_format[3] = *radix; 315 break; 316 default: 317 fatal (_("%s: invalid radix"), radix); 318 } 319 } 320 321 static void 322 set_output_format (char *f) 323 { 324 int i; 325 326 switch (*f) 327 { 328 case 'b': 329 case 'B': 330 i = FORMAT_BSD; 331 break; 332 case 'p': 333 case 'P': 334 i = FORMAT_POSIX; 335 break; 336 case 's': 337 case 'S': 338 i = FORMAT_SYSV; 339 break; 340 default: 341 fatal (_("%s: invalid output format"), f); 342 } 343 format = &formats[i]; 344 } 345 346 static const char * 348 get_elf_symbol_type (unsigned int type) 349 { 350 static char buff [32]; 351 352 switch (type) 353 { 354 case STT_NOTYPE: return "NOTYPE"; 355 case STT_OBJECT: return "OBJECT"; 356 case STT_FUNC: return "FUNC"; 357 case STT_SECTION: return "SECTION"; 358 case STT_FILE: return "FILE"; 359 case STT_COMMON: return "COMMON"; 360 case STT_TLS: return "TLS"; 361 default: 362 if (type >= STT_LOPROC && type <= STT_HIPROC) 363 sprintf (buff, _("<processor specific>: %d"), type); 364 else if (type >= STT_LOOS && type <= STT_HIOS) 365 sprintf (buff, _("<OS specific>: %d"), type); 366 else 367 sprintf (buff, _("<unknown>: %d"), type); 368 return buff; 369 } 370 } 371 372 static const char * 373 get_coff_symbol_type (const struct internal_syment *sym) 374 { 375 static char buff [32]; 376 377 switch (sym->n_sclass) 378 { 379 case C_BLOCK: return "Block"; 380 case C_FILE: return "File"; 381 case C_LINE: return "Line"; 382 } 383 384 if (!sym->n_type) 385 return "None"; 386 387 switch (DTYPE(sym->n_type)) 388 { 389 case DT_FCN: return "Function"; 390 case DT_PTR: return "Pointer"; 391 case DT_ARY: return "Array"; 392 } 393 394 sprintf (buff, _("<unknown>: %d/%d"), sym->n_sclass, sym->n_type); 395 return buff; 396 } 397 398 /* Print symbol name NAME, read from ABFD, with printf format FORM, 400 demangling it if requested. */ 401 402 static void 403 print_symname (const char *form, const char *name, bfd *abfd) 404 { 405 if (do_demangle && *name) 406 { 407 char *res = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS); 408 409 if (res != NULL) 410 { 411 printf (form, res); 412 free (res); 413 return; 414 } 415 } 416 417 printf (form, name); 418 } 419 420 static void 421 print_symdef_entry (bfd *abfd) 422 { 423 symindex idx = BFD_NO_MORE_SYMBOLS; 424 carsym *thesym; 425 bfd_boolean everprinted = FALSE; 426 427 for (idx = bfd_get_next_mapent (abfd, idx, &thesym); 428 idx != BFD_NO_MORE_SYMBOLS; 429 idx = bfd_get_next_mapent (abfd, idx, &thesym)) 430 { 431 bfd *elt; 432 if (!everprinted) 433 { 434 printf (_("\nArchive index:\n")); 435 everprinted = TRUE; 436 } 437 elt = bfd_get_elt_at_index (abfd, idx); 438 if (elt == NULL) 439 bfd_fatal ("bfd_get_elt_at_index"); 440 if (thesym->name != (char *) NULL) 441 { 442 print_symname ("%s", thesym->name, abfd); 443 printf (" in %s\n", bfd_get_filename (elt)); 444 } 445 } 446 } 447 448 /* Choose which symbol entries to print; 450 compact them downward to get rid of the rest. 451 Return the number of symbols to be printed. */ 452 453 static long 454 filter_symbols (bfd *abfd, bfd_boolean is_dynamic, void *minisyms, 455 long symcount, unsigned int size) 456 { 457 bfd_byte *from, *fromend, *to; 458 asymbol *store; 459 460 store = bfd_make_empty_symbol (abfd); 461 if (store == NULL) 462 bfd_fatal (bfd_get_filename (abfd)); 463 464 from = (bfd_byte *) minisyms; 465 fromend = from + symcount * size; 466 to = (bfd_byte *) minisyms; 467 468 for (; from < fromend; from += size) 469 { 470 int keep = 0; 471 asymbol *sym; 472 473 PROGRESS (1); 474 475 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, (const void *) from, store); 476 if (sym == NULL) 477 bfd_fatal (bfd_get_filename (abfd)); 478 479 if (strcmp (sym->name, "__gnu_lto_slim") == 0) 480 non_fatal (_("%s: plugin needed to handle lto object"), 481 bfd_get_filename (abfd)); 482 483 if (undefined_only) 484 keep = bfd_is_und_section (sym->section); 485 else if (external_only) 486 /* PR binutls/12753: Unique symbols are global too. */ 487 keep = ((sym->flags & (BSF_GLOBAL 488 | BSF_WEAK 489 | BSF_GNU_UNIQUE)) != 0 490 || bfd_is_und_section (sym->section) 491 || bfd_is_com_section (sym->section)); 492 else 493 keep = 1; 494 495 if (keep 496 && ! print_debug_syms 497 && (sym->flags & BSF_DEBUGGING) != 0) 498 keep = 0; 499 500 if (keep 501 && sort_by_size 502 && (bfd_is_abs_section (sym->section) 503 || bfd_is_und_section (sym->section))) 504 keep = 0; 505 506 if (keep 507 && defined_only) 508 { 509 if (bfd_is_und_section (sym->section)) 510 keep = 0; 511 } 512 513 if (keep 514 && bfd_is_target_special_symbol (abfd, sym) 515 && ! allow_special_symbols) 516 keep = 0; 517 518 if (keep) 519 { 520 if (to != from) 521 memcpy (to, from, size); 522 to += size; 523 } 524 } 525 526 return (to - (bfd_byte *) minisyms) / size; 527 } 528 529 /* These globals are used to pass information into the sorting 531 routines. */ 532 static bfd *sort_bfd; 533 static bfd_boolean sort_dynamic; 534 static asymbol *sort_x; 535 static asymbol *sort_y; 536 537 /* Symbol-sorting predicates */ 538 #define valueof(x) ((x)->section->vma + (x)->value) 539 540 /* Numeric sorts. Undefined symbols are always considered "less than" 541 defined symbols with zero values. Common symbols are not treated 542 specially -- i.e., their sizes are used as their "values". */ 543 544 static int 545 non_numeric_forward (const void *P_x, const void *P_y) 546 { 547 asymbol *x, *y; 548 const char *xn, *yn; 549 550 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x); 551 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y); 552 if (x == NULL || y == NULL) 553 bfd_fatal (bfd_get_filename (sort_bfd)); 554 555 xn = bfd_asymbol_name (x); 556 yn = bfd_asymbol_name (y); 557 558 if (yn == NULL) 559 return xn != NULL; 560 if (xn == NULL) 561 return -1; 562 563 #ifdef HAVE_STRCOLL 564 /* Solaris 2.5 has a bug in strcoll. 565 strcoll returns invalid values when confronted with empty strings. */ 566 if (*yn == '\0') 567 return *xn != '\0'; 568 if (*xn == '\0') 569 return -1; 570 571 return strcoll (xn, yn); 572 #else 573 return strcmp (xn, yn); 574 #endif 575 } 576 577 static int 578 non_numeric_reverse (const void *x, const void *y) 579 { 580 return - non_numeric_forward (x, y); 581 } 582 583 static int 584 numeric_forward (const void *P_x, const void *P_y) 585 { 586 asymbol *x, *y; 587 asection *xs, *ys; 588 589 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x); 590 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y); 591 if (x == NULL || y == NULL) 592 bfd_fatal (bfd_get_filename (sort_bfd)); 593 594 xs = bfd_get_section (x); 595 ys = bfd_get_section (y); 596 597 if (bfd_is_und_section (xs)) 598 { 599 if (! bfd_is_und_section (ys)) 600 return -1; 601 } 602 else if (bfd_is_und_section (ys)) 603 return 1; 604 else if (valueof (x) != valueof (y)) 605 return valueof (x) < valueof (y) ? -1 : 1; 606 607 return non_numeric_forward (P_x, P_y); 608 } 609 610 static int 611 numeric_reverse (const void *x, const void *y) 612 { 613 return - numeric_forward (x, y); 614 } 615 616 static int (*(sorters[2][2])) (const void *, const void *) = 617 { 618 { non_numeric_forward, non_numeric_reverse }, 619 { numeric_forward, numeric_reverse } 620 }; 621 622 /* This sort routine is used by sort_symbols_by_size. It is similar 623 to numeric_forward, but when symbols have the same value it sorts 624 by section VMA. This simplifies the sort_symbols_by_size code 625 which handles symbols at the end of sections. Also, this routine 626 tries to sort file names before other symbols with the same value. 627 That will make the file name have a zero size, which will make 628 sort_symbols_by_size choose the non file name symbol, leading to 629 more meaningful output. For similar reasons, this code sorts 630 gnu_compiled_* and gcc2_compiled before other symbols with the same 631 value. */ 632 633 static int 634 size_forward1 (const void *P_x, const void *P_y) 635 { 636 asymbol *x, *y; 637 asection *xs, *ys; 638 const char *xn, *yn; 639 size_t xnl, ynl; 640 int xf, yf; 641 642 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x); 643 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y); 644 if (x == NULL || y == NULL) 645 bfd_fatal (bfd_get_filename (sort_bfd)); 646 647 xs = bfd_get_section (x); 648 ys = bfd_get_section (y); 649 650 if (bfd_is_und_section (xs)) 651 abort (); 652 if (bfd_is_und_section (ys)) 653 abort (); 654 655 if (valueof (x) != valueof (y)) 656 return valueof (x) < valueof (y) ? -1 : 1; 657 658 if (xs->vma != ys->vma) 659 return xs->vma < ys->vma ? -1 : 1; 660 661 xn = bfd_asymbol_name (x); 662 yn = bfd_asymbol_name (y); 663 xnl = strlen (xn); 664 ynl = strlen (yn); 665 666 /* The symbols gnu_compiled and gcc2_compiled convey even less 667 information than the file name, so sort them out first. */ 668 669 xf = (strstr (xn, "gnu_compiled") != NULL 670 || strstr (xn, "gcc2_compiled") != NULL); 671 yf = (strstr (yn, "gnu_compiled") != NULL 672 || strstr (yn, "gcc2_compiled") != NULL); 673 674 if (xf && ! yf) 675 return -1; 676 if (! xf && yf) 677 return 1; 678 679 /* We use a heuristic for the file name. It may not work on non 680 Unix systems, but it doesn't really matter; the only difference 681 is precisely which symbol names get printed. */ 682 683 #define file_symbol(s, sn, snl) \ 684 (((s)->flags & BSF_FILE) != 0 \ 685 || ((sn)[(snl) - 2] == '.' \ 686 && ((sn)[(snl) - 1] == 'o' \ 687 || (sn)[(snl) - 1] == 'a'))) 688 689 xf = file_symbol (x, xn, xnl); 690 yf = file_symbol (y, yn, ynl); 691 692 if (xf && ! yf) 693 return -1; 694 if (! xf && yf) 695 return 1; 696 697 return non_numeric_forward (P_x, P_y); 698 } 699 700 /* This sort routine is used by sort_symbols_by_size. It is sorting 701 an array of size_sym structures into size order. */ 702 703 static int 704 size_forward2 (const void *P_x, const void *P_y) 705 { 706 const struct size_sym *x = (const struct size_sym *) P_x; 707 const struct size_sym *y = (const struct size_sym *) P_y; 708 709 if (x->size < y->size) 710 return reverse_sort ? 1 : -1; 711 else if (x->size > y->size) 712 return reverse_sort ? -1 : 1; 713 else 714 return sorters[0][reverse_sort] (x->minisym, y->minisym); 715 } 716 717 /* Sort the symbols by size. ELF provides a size but for other formats 718 we have to make a guess by assuming that the difference between the 719 address of a symbol and the address of the next higher symbol is the 720 size. */ 721 722 static long 723 sort_symbols_by_size (bfd *abfd, bfd_boolean is_dynamic, void *minisyms, 724 long symcount, unsigned int size, 725 struct size_sym **symsizesp) 726 { 727 struct size_sym *symsizes; 728 bfd_byte *from, *fromend; 729 asymbol *sym = NULL; 730 asymbol *store_sym, *store_next; 731 732 qsort (minisyms, symcount, size, size_forward1); 733 734 /* We are going to return a special set of symbols and sizes to 735 print. */ 736 symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym)); 737 *symsizesp = symsizes; 738 739 /* Note that filter_symbols has already removed all absolute and 740 undefined symbols. Here we remove all symbols whose size winds 741 up as zero. */ 742 from = (bfd_byte *) minisyms; 743 fromend = from + symcount * size; 744 745 store_sym = sort_x; 746 store_next = sort_y; 747 748 if (from < fromend) 749 { 750 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, (const void *) from, 751 store_sym); 752 if (sym == NULL) 753 bfd_fatal (bfd_get_filename (abfd)); 754 } 755 756 for (; from < fromend; from += size) 757 { 758 asymbol *next; 759 asection *sec; 760 bfd_vma sz; 761 asymbol *temp; 762 763 if (from + size < fromend) 764 { 765 next = bfd_minisymbol_to_symbol (abfd, 766 is_dynamic, 767 (const void *) (from + size), 768 store_next); 769 if (next == NULL) 770 bfd_fatal (bfd_get_filename (abfd)); 771 } 772 else 773 next = NULL; 774 775 sec = bfd_get_section (sym); 776 777 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) 778 sz = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; 779 else if (bfd_is_com_section (sec)) 780 sz = sym->value; 781 else 782 { 783 if (from + size < fromend 784 && sec == bfd_get_section (next)) 785 sz = valueof (next) - valueof (sym); 786 else 787 sz = (bfd_get_section_vma (abfd, sec) 788 + bfd_section_size (abfd, sec) 789 - valueof (sym)); 790 } 791 792 if (sz != 0) 793 { 794 symsizes->minisym = (const void *) from; 795 symsizes->size = sz; 796 ++symsizes; 797 } 798 799 sym = next; 800 801 temp = store_sym; 802 store_sym = store_next; 803 store_next = temp; 804 } 805 806 symcount = symsizes - *symsizesp; 807 808 /* We must now sort again by size. */ 809 qsort ((void *) *symsizesp, symcount, sizeof (struct size_sym), size_forward2); 810 811 return symcount; 812 } 813 814 /* This function is used to get the relocs for a particular section. 815 It is called via bfd_map_over_sections. */ 816 817 static void 818 get_relocs (bfd *abfd, asection *sec, void *dataarg) 819 { 820 struct get_relocs_info *data = (struct get_relocs_info *) dataarg; 821 822 *data->secs = sec; 823 824 if ((sec->flags & SEC_RELOC) == 0) 825 { 826 *data->relocs = NULL; 827 *data->relcount = 0; 828 } 829 else 830 { 831 long relsize; 832 833 relsize = bfd_get_reloc_upper_bound (abfd, sec); 834 if (relsize < 0) 835 bfd_fatal (bfd_get_filename (abfd)); 836 837 *data->relocs = (arelent **) xmalloc (relsize); 838 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs, 839 data->syms); 840 if (*data->relcount < 0) 841 bfd_fatal (bfd_get_filename (abfd)); 842 } 843 844 ++data->secs; 845 ++data->relocs; 846 ++data->relcount; 847 } 848 849 /* Print a single symbol. */ 850 851 static void 852 print_symbol (bfd * abfd, 853 asymbol * sym, 854 bfd_vma ssize, 855 bfd * archive_bfd, 856 bfd_boolean is_synthetic) 857 { 858 symbol_info syminfo; 859 struct extended_symbol_info info; 860 861 PROGRESS (1); 862 863 format->print_symbol_filename (archive_bfd, abfd); 864 865 bfd_get_symbol_info (abfd, sym, &syminfo); 866 867 info.sinfo = &syminfo; 868 info.ssize = ssize; 869 /* Synthetic symbols do not have a full symbol type set of data available. */ 870 if (is_synthetic) 871 { 872 info.elfinfo = NULL; 873 info.coffinfo = NULL; 874 } 875 else 876 { 877 info.elfinfo = elf_symbol_from (abfd, sym); 878 info.coffinfo = coff_symbol_from (sym); 879 } 880 881 format->print_symbol_info (&info, abfd); 882 883 if (line_numbers) 884 { 885 static asymbol **syms; 886 static long symcount; 887 const char *filename, *functionname; 888 unsigned int lineno; 889 890 /* We need to get the canonical symbols in order to call 891 bfd_find_nearest_line. This is inefficient, but, then, you 892 don't have to use --line-numbers. */ 893 if (abfd != lineno_cache_bfd && syms != NULL) 894 { 895 free (syms); 896 syms = NULL; 897 } 898 if (syms == NULL) 899 { 900 long symsize; 901 902 symsize = bfd_get_symtab_upper_bound (abfd); 903 if (symsize < 0) 904 bfd_fatal (bfd_get_filename (abfd)); 905 syms = (asymbol **) xmalloc (symsize); 906 symcount = bfd_canonicalize_symtab (abfd, syms); 907 if (symcount < 0) 908 bfd_fatal (bfd_get_filename (abfd)); 909 lineno_cache_bfd = abfd; 910 } 911 912 if (bfd_is_und_section (bfd_get_section (sym))) 913 { 914 static asection **secs; 915 static arelent ***relocs; 916 static long *relcount; 917 static unsigned int seccount; 918 unsigned int i; 919 const char *symname; 920 921 /* For an undefined symbol, we try to find a reloc for the 922 symbol, and print the line number of the reloc. */ 923 if (abfd != lineno_cache_rel_bfd && relocs != NULL) 924 { 925 for (i = 0; i < seccount; i++) 926 if (relocs[i] != NULL) 927 free (relocs[i]); 928 free (secs); 929 free (relocs); 930 free (relcount); 931 secs = NULL; 932 relocs = NULL; 933 relcount = NULL; 934 } 935 936 if (relocs == NULL) 937 { 938 struct get_relocs_info rinfo; 939 940 seccount = bfd_count_sections (abfd); 941 942 secs = (asection **) xmalloc (seccount * sizeof *secs); 943 relocs = (arelent ***) xmalloc (seccount * sizeof *relocs); 944 relcount = (long *) xmalloc (seccount * sizeof *relcount); 945 946 rinfo.secs = secs; 947 rinfo.relocs = relocs; 948 rinfo.relcount = relcount; 949 rinfo.syms = syms; 950 bfd_map_over_sections (abfd, get_relocs, (void *) &rinfo); 951 lineno_cache_rel_bfd = abfd; 952 } 953 954 symname = bfd_asymbol_name (sym); 955 for (i = 0; i < seccount; i++) 956 { 957 long j; 958 959 for (j = 0; j < relcount[i]; j++) 960 { 961 arelent *r; 962 963 r = relocs[i][j]; 964 if (r->sym_ptr_ptr != NULL 965 && (*r->sym_ptr_ptr)->section == sym->section 966 && (*r->sym_ptr_ptr)->value == sym->value 967 && strcmp (symname, 968 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0 969 && bfd_find_nearest_line (abfd, secs[i], syms, 970 r->address, &filename, 971 &functionname, &lineno) 972 && filename != NULL) 973 { 974 /* We only print the first one we find. */ 975 printf ("\t%s:%u", filename, lineno); 976 i = seccount; 977 break; 978 } 979 } 980 } 981 } 982 else if (bfd_get_section (sym)->owner == abfd) 983 { 984 if ((bfd_find_line (abfd, syms, sym, &filename, &lineno) 985 || bfd_find_nearest_line (abfd, bfd_get_section (sym), 986 syms, sym->value, &filename, 987 &functionname, &lineno)) 988 && filename != NULL 989 && lineno != 0) 990 printf ("\t%s:%u", filename, lineno); 991 } 992 } 993 994 putchar ('\n'); 995 } 996 997 /* Print the symbols when sorting by size. */ 999 1000 static void 1001 print_size_symbols (bfd * abfd, 1002 bfd_boolean is_dynamic, 1003 struct size_sym * symsizes, 1004 long symcount, 1005 long synth_count, 1006 bfd * archive_bfd) 1007 { 1008 asymbol *store; 1009 struct size_sym *from; 1010 struct size_sym *fromend; 1011 struct size_sym *fromsynth; 1012 1013 store = bfd_make_empty_symbol (abfd); 1014 if (store == NULL) 1015 bfd_fatal (bfd_get_filename (abfd)); 1016 1017 from = symsizes; 1018 fromend = from + symcount; 1019 fromsynth = symsizes + (symcount - synth_count); 1020 1021 for (; from < fromend; from++) 1022 { 1023 asymbol *sym; 1024 1025 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from->minisym, store); 1026 if (sym == NULL) 1027 bfd_fatal (bfd_get_filename (abfd)); 1028 1029 print_symbol (abfd, sym, from->size, archive_bfd, from >= fromsynth); 1030 } 1031 } 1032 1033 1034 /* Print the symbols of ABFD that are held in MINISYMS. 1036 1037 If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. 1038 1039 SYMCOUNT is the number of symbols in MINISYMS and SYNTH_COUNT 1040 is the number of these that are synthetic. Synthetic symbols, 1041 if any are present, always come at the end of the MINISYMS. 1042 1043 SIZE is the size of a symbol in MINISYMS. */ 1044 1045 static void 1046 print_symbols (bfd * abfd, 1047 bfd_boolean is_dynamic, 1048 void * minisyms, 1049 long symcount, 1050 long synth_count, 1051 unsigned int size, 1052 bfd * archive_bfd) 1053 { 1054 asymbol *store; 1055 bfd_byte *from; 1056 bfd_byte *fromend; 1057 bfd_byte *fromsynth; 1058 1059 store = bfd_make_empty_symbol (abfd); 1060 if (store == NULL) 1061 bfd_fatal (bfd_get_filename (abfd)); 1062 1063 from = (bfd_byte *) minisyms; 1064 fromend = from + symcount * size; 1065 fromsynth = (bfd_byte *) minisyms + ((symcount - synth_count) * size); 1066 1067 for (; from < fromend; from += size) 1068 { 1069 asymbol *sym; 1070 1071 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store); 1072 if (sym == NULL) 1073 bfd_fatal (bfd_get_filename (abfd)); 1074 1075 print_symbol (abfd, sym, (bfd_vma) 0, archive_bfd, from >= fromsynth); 1076 } 1077 } 1078 1079 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */ 1080 1081 static void 1082 display_rel_file (bfd *abfd, bfd *archive_bfd) 1083 { 1084 long symcount; 1085 long synth_count = 0; 1086 void *minisyms; 1087 unsigned int size; 1088 struct size_sym *symsizes; 1089 1090 if (! dynamic) 1091 { 1092 if (!(bfd_get_file_flags (abfd) & HAS_SYMS)) 1093 { 1094 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd)); 1095 return; 1096 } 1097 } 1098 1099 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size); 1100 if (symcount < 0) 1101 { 1102 if (dynamic && bfd_get_error () == bfd_error_no_symbols) 1103 { 1104 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd)); 1105 return; 1106 } 1107 1108 bfd_fatal (bfd_get_filename (abfd)); 1109 } 1110 1111 if (symcount == 0) 1112 { 1113 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd)); 1114 return; 1115 } 1116 1117 if (show_synthetic && size == sizeof (asymbol *)) 1118 { 1119 asymbol *synthsyms; 1120 asymbol **static_syms = NULL; 1121 asymbol **dyn_syms = NULL; 1122 long static_count = 0; 1123 long dyn_count = 0; 1124 1125 if (dynamic) 1126 { 1127 dyn_count = symcount; 1128 dyn_syms = (asymbol **) minisyms; 1129 } 1130 else 1131 { 1132 long storage = bfd_get_dynamic_symtab_upper_bound (abfd); 1133 1134 static_count = symcount; 1135 static_syms = (asymbol **) minisyms; 1136 1137 if (storage > 0) 1138 { 1139 dyn_syms = (asymbol **) xmalloc (storage); 1140 dyn_count = bfd_canonicalize_dynamic_symtab (abfd, dyn_syms); 1141 if (dyn_count < 0) 1142 bfd_fatal (bfd_get_filename (abfd)); 1143 } 1144 } 1145 1146 synth_count = bfd_get_synthetic_symtab (abfd, static_count, static_syms, 1147 dyn_count, dyn_syms, &synthsyms); 1148 if (synth_count > 0) 1149 { 1150 asymbol **symp; 1151 void *new_mini; 1152 long i; 1153 1154 new_mini = xmalloc ((symcount + synth_count + 1) * sizeof (*symp)); 1155 symp = (asymbol **) new_mini; 1156 memcpy (symp, minisyms, symcount * sizeof (*symp)); 1157 symp += symcount; 1158 for (i = 0; i < synth_count; i++) 1159 *symp++ = synthsyms + i; 1160 *symp = 0; 1161 minisyms = new_mini; 1162 symcount += synth_count; 1163 } 1164 } 1165 1166 /* Discard the symbols we don't want to print. 1167 It's OK to do this in place; we'll free the storage anyway 1168 (after printing). */ 1169 1170 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size); 1171 1172 symsizes = NULL; 1173 if (! no_sort) 1174 { 1175 sort_bfd = abfd; 1176 sort_dynamic = dynamic; 1177 sort_x = bfd_make_empty_symbol (abfd); 1178 sort_y = bfd_make_empty_symbol (abfd); 1179 if (sort_x == NULL || sort_y == NULL) 1180 bfd_fatal (bfd_get_filename (abfd)); 1181 1182 if (! sort_by_size) 1183 qsort (minisyms, symcount, size, 1184 sorters[sort_numerically][reverse_sort]); 1185 else 1186 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount, 1187 size, &symsizes); 1188 } 1189 1190 if (! sort_by_size) 1191 print_symbols (abfd, dynamic, minisyms, symcount, synth_count, size, archive_bfd); 1192 else 1193 print_size_symbols (abfd, dynamic, symsizes, symcount, synth_count, archive_bfd); 1194 1195 free (minisyms); 1196 free (symsizes); 1197 } 1198 1199 static void 1200 set_print_width (bfd *file) 1201 { 1202 print_width = bfd_get_arch_size (file); 1203 1204 if (print_width == -1) 1205 { 1206 /* PR binutils/4292 1207 Guess the target's bitsize based on its name. 1208 We assume here than any 64-bit format will include 1209 "64" somewhere in its name. The only known exception 1210 is the MMO object file format. */ 1211 if (strstr (bfd_get_target (file), "64") != NULL 1212 || strcmp (bfd_get_target (file), "mmo") == 0) 1213 print_width = 64; 1214 else 1215 print_width = 32; 1216 } 1217 } 1218 1219 static void 1220 display_archive (bfd *file) 1221 { 1222 bfd *arfile = NULL; 1223 bfd *last_arfile = NULL; 1224 char **matching; 1225 1226 format->print_archive_filename (bfd_get_filename (file)); 1227 1228 if (print_armap) 1229 print_symdef_entry (file); 1230 1231 for (;;) 1232 { 1233 PROGRESS (1); 1234 1235 arfile = bfd_openr_next_archived_file (file, arfile); 1236 1237 if (arfile == NULL) 1238 { 1239 if (bfd_get_error () != bfd_error_no_more_archived_files) 1240 bfd_fatal (bfd_get_filename (file)); 1241 break; 1242 } 1243 1244 if (bfd_check_format_matches (arfile, bfd_object, &matching)) 1245 { 1246 set_print_width (arfile); 1247 format->print_archive_member (bfd_get_filename (file), 1248 bfd_get_filename (arfile)); 1249 display_rel_file (arfile, file); 1250 } 1251 else 1252 { 1253 bfd_nonfatal (bfd_get_filename (arfile)); 1254 if (bfd_get_error () == bfd_error_file_ambiguously_recognized) 1255 { 1256 list_matching_formats (matching); 1257 free (matching); 1258 } 1259 } 1260 1261 if (last_arfile != NULL) 1262 { 1263 bfd_close (last_arfile); 1264 lineno_cache_bfd = NULL; 1265 lineno_cache_rel_bfd = NULL; 1266 if (arfile == last_arfile) 1267 return; 1268 } 1269 last_arfile = arfile; 1270 } 1271 1272 if (last_arfile != NULL) 1273 { 1274 bfd_close (last_arfile); 1275 lineno_cache_bfd = NULL; 1276 lineno_cache_rel_bfd = NULL; 1277 } 1278 } 1279 1280 static bfd_boolean 1281 display_file (char *filename) 1282 { 1283 bfd_boolean retval = TRUE; 1284 bfd *file; 1285 char **matching; 1286 1287 if (get_file_size (filename) < 1) 1288 return FALSE; 1289 1290 file = bfd_openr (filename, target ? target : plugin_target); 1291 if (file == NULL) 1292 { 1293 bfd_nonfatal (filename); 1294 return FALSE; 1295 } 1296 1297 /* If printing line numbers, decompress the debug sections. */ 1298 if (line_numbers) 1299 file->flags |= BFD_DECOMPRESS; 1300 1301 if (bfd_check_format (file, bfd_archive)) 1302 { 1303 display_archive (file); 1304 } 1305 else if (bfd_check_format_matches (file, bfd_object, &matching)) 1306 { 1307 set_print_width (file); 1308 format->print_object_filename (filename); 1309 display_rel_file (file, NULL); 1310 } 1311 else 1312 { 1313 bfd_nonfatal (filename); 1314 if (bfd_get_error () == bfd_error_file_ambiguously_recognized) 1315 { 1316 list_matching_formats (matching); 1317 free (matching); 1318 } 1319 retval = FALSE; 1320 } 1321 1322 if (!bfd_close (file)) 1323 bfd_fatal (filename); 1324 1325 lineno_cache_bfd = NULL; 1326 lineno_cache_rel_bfd = NULL; 1327 1328 return retval; 1329 } 1330 1331 /* The following 3 groups of functions are called unconditionally, 1333 once at the start of processing each file of the appropriate type. 1334 They should check `filename_per_file' and `filename_per_symbol', 1335 as appropriate for their output format, to determine whether to 1336 print anything. */ 1337 1338 /* Print the name of an object file given on the command line. */ 1340 1341 static void 1342 print_object_filename_bsd (char *filename) 1343 { 1344 if (filename_per_file && !filename_per_symbol) 1345 printf ("\n%s:\n", filename); 1346 } 1347 1348 static void 1349 print_object_filename_sysv (char *filename) 1350 { 1351 if (undefined_only) 1352 printf (_("\n\nUndefined symbols from %s:\n\n"), filename); 1353 else 1354 printf (_("\n\nSymbols from %s:\n\n"), filename); 1355 if (print_width == 32) 1356 printf (_("\ 1357 Name Value Class Type Size Line Section\n\n")); 1358 else 1359 printf (_("\ 1360 Name Value Class Type Size Line Section\n\n")); 1361 } 1362 1363 static void 1364 print_object_filename_posix (char *filename) 1365 { 1366 if (filename_per_file && !filename_per_symbol) 1367 printf ("%s:\n", filename); 1368 } 1369 1370 /* Print the name of an archive file given on the command line. */ 1372 1373 static void 1374 print_archive_filename_bsd (char *filename) 1375 { 1376 if (filename_per_file) 1377 printf ("\n%s:\n", filename); 1378 } 1379 1380 static void 1381 print_archive_filename_sysv (char *filename ATTRIBUTE_UNUSED) 1382 { 1383 } 1384 1385 static void 1386 print_archive_filename_posix (char *filename ATTRIBUTE_UNUSED) 1387 { 1388 } 1389 1390 /* Print the name of an archive member file. */ 1392 1393 static void 1394 print_archive_member_bsd (char *archive ATTRIBUTE_UNUSED, 1395 const char *filename) 1396 { 1397 if (!filename_per_symbol) 1398 printf ("\n%s:\n", filename); 1399 } 1400 1401 static void 1402 print_archive_member_sysv (char *archive, const char *filename) 1403 { 1404 if (undefined_only) 1405 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename); 1406 else 1407 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename); 1408 if (print_width == 32) 1409 printf (_("\ 1410 Name Value Class Type Size Line Section\n\n")); 1411 else 1412 printf (_("\ 1413 Name Value Class Type Size Line Section\n\n")); 1414 } 1415 1416 static void 1417 print_archive_member_posix (char *archive, const char *filename) 1418 { 1419 if (!filename_per_symbol) 1420 printf ("%s[%s]:\n", archive, filename); 1421 } 1422 1423 /* Print the name of the file (and archive, if there is one) 1425 containing a symbol. */ 1426 1427 static void 1428 print_symbol_filename_bsd (bfd *archive_bfd, bfd *abfd) 1429 { 1430 if (filename_per_symbol) 1431 { 1432 if (archive_bfd) 1433 printf ("%s:", bfd_get_filename (archive_bfd)); 1434 printf ("%s:", bfd_get_filename (abfd)); 1435 } 1436 } 1437 1438 static void 1439 print_symbol_filename_sysv (bfd *archive_bfd, bfd *abfd) 1440 { 1441 if (filename_per_symbol) 1442 { 1443 if (archive_bfd) 1444 printf ("%s:", bfd_get_filename (archive_bfd)); 1445 printf ("%s:", bfd_get_filename (abfd)); 1446 } 1447 } 1448 1449 static void 1450 print_symbol_filename_posix (bfd *archive_bfd, bfd *abfd) 1451 { 1452 if (filename_per_symbol) 1453 { 1454 if (archive_bfd) 1455 printf ("%s[%s]: ", bfd_get_filename (archive_bfd), 1456 bfd_get_filename (abfd)); 1457 else 1458 printf ("%s: ", bfd_get_filename (abfd)); 1459 } 1460 } 1461 1462 /* Print a symbol value. */ 1464 1465 static void 1466 print_value (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma val) 1467 { 1468 switch (print_width) 1469 { 1470 case 32: 1471 printf (value_format_32bit, (unsigned long) val); 1472 break; 1473 1474 case 64: 1475 #if BFD_HOST_64BIT_LONG || BFD_HOST_64BIT_LONG_LONG 1476 printf (value_format_64bit, val); 1477 #else 1478 /* We have a 64 bit value to print, but the host is only 32 bit. */ 1479 if (print_radix == 16) 1480 bfd_fprintf_vma (abfd, stdout, val); 1481 else 1482 { 1483 char buf[30]; 1484 char *s; 1485 1486 s = buf + sizeof buf; 1487 *--s = '\0'; 1488 while (val > 0) 1489 { 1490 *--s = (val % print_radix) + '0'; 1491 val /= print_radix; 1492 } 1493 while ((buf + sizeof buf - 1) - s < 16) 1494 *--s = '0'; 1495 printf ("%s", s); 1496 } 1497 #endif 1498 break; 1499 1500 default: 1501 fatal (_("Print width has not been initialized (%d)"), print_width); 1502 break; 1503 } 1504 } 1505 1506 /* Print a line of information about a symbol. */ 1507 1508 static void 1509 print_symbol_info_bsd (struct extended_symbol_info *info, bfd *abfd) 1510 { 1511 if (bfd_is_undefined_symclass (SYM_TYPE (info))) 1512 { 1513 if (print_width == 64) 1514 printf (" "); 1515 printf (" "); 1516 } 1517 else 1518 { 1519 /* Normally we print the value of the symbol. If we are printing the 1520 size or sorting by size then we print its size, except for the 1521 (weird) special case where both flags are defined, in which case we 1522 print both values. This conforms to documented behaviour. */ 1523 if (sort_by_size && !print_size) 1524 print_value (abfd, SYM_SIZE (info)); 1525 else 1526 print_value (abfd, SYM_VALUE (info)); 1527 if (print_size && SYM_SIZE (info)) 1528 { 1529 printf (" "); 1530 print_value (abfd, SYM_SIZE (info)); 1531 } 1532 } 1533 1534 printf (" %c", SYM_TYPE (info)); 1535 1536 if (SYM_TYPE (info) == '-') 1537 { 1538 /* A stab. */ 1539 printf (" "); 1540 printf (other_format, SYM_STAB_OTHER (info)); 1541 printf (" "); 1542 printf (desc_format, SYM_STAB_DESC (info)); 1543 printf (" %5s", SYM_STAB_NAME (info)); 1544 } 1545 print_symname (" %s", SYM_NAME (info), abfd); 1546 } 1547 1548 static void 1549 print_symbol_info_sysv (struct extended_symbol_info *info, bfd *abfd) 1550 { 1551 print_symname ("%-20s|", SYM_NAME (info), abfd); 1552 1553 if (bfd_is_undefined_symclass (SYM_TYPE (info))) 1554 { 1555 if (print_width == 32) 1556 printf (" "); 1557 else 1558 printf (" "); 1559 } 1560 else 1561 print_value (abfd, SYM_VALUE (info)); 1562 1563 printf ("| %c |", SYM_TYPE (info)); 1564 1565 if (SYM_TYPE (info) == '-') 1566 { 1567 /* A stab. */ 1568 printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type. */ 1569 printf (desc_format, SYM_STAB_DESC (info)); /* Size. */ 1570 printf ("| |"); /* Line, Section. */ 1571 } 1572 else 1573 { 1574 /* Type, Size, Line, Section */ 1575 if (info->elfinfo) 1576 printf ("%18s|", 1577 get_elf_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info))); 1578 else if (info->coffinfo) 1579 printf ("%18s|", 1580 get_coff_symbol_type (&info->coffinfo->native->u.syment)); 1581 else 1582 printf (" |"); 1583 1584 if (SYM_SIZE (info)) 1585 print_value (abfd, SYM_SIZE (info)); 1586 else 1587 { 1588 if (print_width == 32) 1589 printf (" "); 1590 else 1591 printf (" "); 1592 } 1593 1594 if (info->elfinfo) 1595 printf("| |%s", info->elfinfo->symbol.section->name); 1596 else if (info->coffinfo) 1597 printf("| |%s", info->coffinfo->symbol.section->name); 1598 else 1599 printf("| |"); 1600 } 1601 } 1602 1603 static void 1604 print_symbol_info_posix (struct extended_symbol_info *info, bfd *abfd) 1605 { 1606 print_symname ("%s ", SYM_NAME (info), abfd); 1607 printf ("%c ", SYM_TYPE (info)); 1608 1609 if (bfd_is_undefined_symclass (SYM_TYPE (info))) 1610 printf (" "); 1611 else 1612 { 1613 print_value (abfd, SYM_VALUE (info)); 1614 printf (" "); 1615 if (SYM_SIZE (info)) 1616 print_value (abfd, SYM_SIZE (info)); 1617 } 1618 } 1619 1620 int 1622 main (int argc, char **argv) 1623 { 1624 int c; 1625 int retval; 1626 1627 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) 1628 setlocale (LC_MESSAGES, ""); 1629 #endif 1630 #if defined (HAVE_SETLOCALE) 1631 setlocale (LC_CTYPE, ""); 1632 setlocale (LC_COLLATE, ""); 1633 #endif 1634 bindtextdomain (PACKAGE, LOCALEDIR); 1635 textdomain (PACKAGE); 1636 1637 program_name = *argv; 1638 xmalloc_set_program_name (program_name); 1639 bfd_set_error_program_name (program_name); 1640 #if BFD_SUPPORTS_PLUGINS 1641 bfd_plugin_set_program_name (program_name); 1642 #endif 1643 1644 START_PROGRESS (program_name, 0); 1645 1646 expandargv (&argc, &argv); 1647 1648 bfd_init (); 1649 set_default_bfd_target (); 1650 1651 while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uvVvX:", 1652 long_options, (int *) 0)) != EOF) 1653 { 1654 switch (c) 1655 { 1656 case 'a': 1657 print_debug_syms = 1; 1658 break; 1659 case 'A': 1660 case 'o': 1661 filename_per_symbol = 1; 1662 break; 1663 case 'B': /* For MIPS compatibility. */ 1664 set_output_format ("bsd"); 1665 break; 1666 case 'C': 1667 do_demangle = 1; 1668 if (optarg != NULL) 1669 { 1670 enum demangling_styles style; 1671 1672 style = cplus_demangle_name_to_style (optarg); 1673 if (style == unknown_demangling) 1674 fatal (_("unknown demangling style `%s'"), 1675 optarg); 1676 1677 cplus_demangle_set_style (style); 1678 } 1679 break; 1680 case 'D': 1681 dynamic = 1; 1682 break; 1683 case 'e': 1684 /* Ignored for HP/UX compatibility. */ 1685 break; 1686 case 'f': 1687 set_output_format (optarg); 1688 break; 1689 case 'g': 1690 external_only = 1; 1691 break; 1692 case 'H': 1693 case 'h': 1694 usage (stdout, 0); 1695 case 'l': 1696 line_numbers = 1; 1697 break; 1698 case 'n': 1699 case 'v': 1700 no_sort = 0; 1701 sort_numerically = 1; 1702 sort_by_size = 0; 1703 break; 1704 case 'p': 1705 no_sort = 1; 1706 sort_numerically = 0; 1707 sort_by_size = 0; 1708 break; 1709 case OPTION_SIZE_SORT: 1710 no_sort = 0; 1711 sort_numerically = 0; 1712 sort_by_size = 1; 1713 break; 1714 case 'P': 1715 set_output_format ("posix"); 1716 break; 1717 case 'r': 1718 reverse_sort = 1; 1719 break; 1720 case 's': 1721 print_armap = 1; 1722 break; 1723 case 'S': 1724 print_size = 1; 1725 break; 1726 case 't': 1727 set_print_radix (optarg); 1728 break; 1729 case 'u': 1730 undefined_only = 1; 1731 break; 1732 case 'V': 1733 show_version = 1; 1734 break; 1735 case 'X': 1736 /* Ignored for (partial) AIX compatibility. On AIX, the 1737 argument has values 32, 64, or 32_64, and specifies that 1738 only 32-bit, only 64-bit, or both kinds of objects should 1739 be examined. The default is 32. So plain AIX nm on a 1740 library archive with both kinds of objects will ignore 1741 the 64-bit ones. For GNU nm, the default is and always 1742 has been -X 32_64, and other options are not supported. */ 1743 if (strcmp (optarg, "32_64") != 0) 1744 fatal (_("Only -X 32_64 is supported")); 1745 break; 1746 1747 case OPTION_TARGET: /* --target */ 1748 target = optarg; 1749 break; 1750 1751 case OPTION_PLUGIN: /* --plugin */ 1752 #if BFD_SUPPORTS_PLUGINS 1753 bfd_plugin_set_plugin (optarg); 1754 #else 1755 fatal (_("sorry - this program has been built without plugin support\n")); 1756 #endif 1757 break; 1758 1759 case 0: /* A long option that just sets a flag. */ 1760 break; 1761 1762 default: 1763 usage (stderr, 1); 1764 } 1765 } 1766 1767 if (show_version) 1768 print_version ("nm"); 1769 1770 if (sort_by_size && undefined_only) 1771 { 1772 non_fatal (_("Using the --size-sort and --undefined-only options together")); 1773 non_fatal (_("will produce no output, since undefined symbols have no size.")); 1774 return 0; 1775 } 1776 1777 /* OK, all options now parsed. If no filename specified, do a.out. */ 1778 if (optind == argc) 1779 return !display_file ("a.out"); 1780 1781 retval = 0; 1782 1783 if (argc - optind > 1) 1784 filename_per_file = 1; 1785 1786 /* We were given several filenames to do. */ 1787 while (optind < argc) 1788 { 1789 PROGRESS (1); 1790 if (!display_file (argv[optind++])) 1791 retval++; 1792 } 1793 1794 END_PROGRESS (program_name); 1795 1796 #ifdef HAVE_SBRK 1797 if (show_stats) 1798 { 1799 char *lim = (char *) sbrk (0); 1800 1801 non_fatal (_("data size %ld"), (long) (lim - (char *) &environ)); 1802 } 1803 #endif 1804 1805 exit (retval); 1806 return retval; 1807 } 1808