1 /* nm.c -- Describe symbol table of a rel file. 2 Copyright (C) 1991-2025 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 "getopt.h" 24 #include "aout/stab_gnu.h" 25 #include "aout/ranlib.h" 26 #include "demangle.h" 27 #include "libiberty.h" 28 #include "elf-bfd.h" 29 #include "elf/common.h" 30 #define DO_NOT_DEFINE_AOUTHDR 31 #define DO_NOT_DEFINE_FILHDR 32 #define DO_NOT_DEFINE_LINENO 33 #define DO_NOT_DEFINE_SCNHDR 34 #include "coff/external.h" 35 #include "coff/internal.h" 36 #include "libcoff.h" 37 #include "bucomm.h" 38 #include "demanguse.h" 39 #include "plugin-api.h" 40 #include "plugin.h" 41 #include "safe-ctype.h" 42 43 #ifndef streq 44 #define streq(a,b) (strcmp ((a),(b)) == 0) 45 #endif 46 47 /* When sorting by size, we use this structure to hold the size and a 48 pointer to the minisymbol. */ 49 50 struct size_sym 51 { 52 const void *minisym; 53 bfd_vma size; 54 }; 55 56 /* line number related info cached in bfd usrdata. */ 57 58 struct lineno_cache 59 { 60 asection **secs; 61 arelent ***relocs; 62 long *relcount; 63 asymbol **syms; 64 long symcount; 65 unsigned int seccount; 66 }; 67 68 struct extended_symbol_info 69 { 70 symbol_info *sinfo; 71 bfd_vma ssize; 72 elf_symbol_type *elfinfo; 73 coff_symbol_type *coffinfo; 74 /* FIXME: We should add more fields for Type, Line, Section. */ 75 }; 76 #define SYM_VALUE(sym) (sym->sinfo->value) 77 #define SYM_TYPE(sym) (sym->sinfo->type) 78 #define SYM_STAB_NAME(sym) (sym->sinfo->stab_name) 79 #define SYM_STAB_DESC(sym) (sym->sinfo->stab_desc) 80 #define SYM_STAB_OTHER(sym) (sym->sinfo->stab_other) 81 #define SYM_SIZE(sym) \ 82 (sym->elfinfo \ 83 && sym->elfinfo->internal_elf_sym.st_size \ 84 ? sym->elfinfo->internal_elf_sym.st_size \ 85 : sym->coffinfo \ 86 && ISFCN (sym->coffinfo->native->u.syment.n_type) \ 87 && sym->coffinfo->native->u.syment.n_numaux \ 88 && sym->coffinfo->native[1].u.auxent.x_sym.x_misc.x_fsize \ 89 ? sym->coffinfo->native[1].u.auxent.x_sym.x_misc.x_fsize \ 90 : sym->ssize) 91 92 /* The output formatting functions. */ 93 static void print_object_filename_bsd (const char *); 94 static void print_object_filename_sysv (const char *); 95 static void print_object_filename_posix (const char *); 96 static void do_not_print_object_filename (const char *); 97 98 static void print_archive_filename_bsd (const char *); 99 static void print_archive_filename_sysv (const char *); 100 static void print_archive_filename_posix (const char *); 101 static void do_not_print_archive_filename (const char *); 102 103 static void print_archive_member_bsd (const char *, const char *); 104 static void print_archive_member_sysv (const char *, const char *); 105 static void print_archive_member_posix (const char *, const char *); 106 static void do_not_print_archive_member (const char *, const char *); 107 108 static void print_symbol_filename_bsd (bfd *, bfd *); 109 static void print_symbol_filename_sysv (bfd *, bfd *); 110 static void print_symbol_filename_posix (bfd *, bfd *); 111 static void do_not_print_symbol_filename (bfd *, bfd *); 112 113 static void print_symbol_info_bsd (struct extended_symbol_info *, bfd *); 114 static void print_symbol_info_sysv (struct extended_symbol_info *, bfd *); 115 static void print_symbol_info_posix (struct extended_symbol_info *, bfd *); 116 static void just_print_symbol_name (struct extended_symbol_info *, bfd *); 117 118 static void print_value (bfd *, bfd_vma); 119 120 /* Support for different output formats. */ 121 struct output_fns 122 { 123 /* Print the name of an object file given on the command line. */ 124 void (*print_object_filename) (const char *); 125 126 /* Print the name of an archive file given on the command line. */ 127 void (*print_archive_filename) (const char *); 128 129 /* Print the name of an archive member file. */ 130 void (*print_archive_member) (const char *, const char *); 131 132 /* Print the name of the file (and archive, if there is one) 133 containing a symbol. */ 134 void (*print_symbol_filename) (bfd *, bfd *); 135 136 /* Print a line of information about a symbol. */ 137 void (*print_symbol_info) (struct extended_symbol_info *, bfd *); 138 }; 139 140 /* Indices in `formats'. */ 141 enum formats 142 { 143 FORMAT_BSD = 0, 144 FORMAT_SYSV, 145 FORMAT_POSIX, 146 FORMAT_JUST_SYMBOLS, 147 FORMAT_MAX 148 }; 149 150 #define FORMAT_DEFAULT FORMAT_BSD 151 152 static const struct output_fns formats[FORMAT_MAX] = 153 { 154 {print_object_filename_bsd, 155 print_archive_filename_bsd, 156 print_archive_member_bsd, 157 print_symbol_filename_bsd, 158 print_symbol_info_bsd}, 159 {print_object_filename_sysv, 160 print_archive_filename_sysv, 161 print_archive_member_sysv, 162 print_symbol_filename_sysv, 163 print_symbol_info_sysv}, 164 {print_object_filename_posix, 165 print_archive_filename_posix, 166 print_archive_member_posix, 167 print_symbol_filename_posix, 168 print_symbol_info_posix}, 169 {do_not_print_object_filename, 170 do_not_print_archive_filename, 171 do_not_print_archive_member, 172 do_not_print_symbol_filename, 173 just_print_symbol_name} 174 }; 175 176 177 /* The output format to use. */ 178 static const struct output_fns *format = &formats[FORMAT_DEFAULT]; 179 static unsigned int print_format = FORMAT_DEFAULT; 180 static char print_format_string[10]; 181 182 /* Command options. */ 183 184 static int do_demangle = 0; /* Pretty print C++ symbol names. */ 185 static int external_only = 0; /* Print external symbols only. */ 186 static int defined_only = 0; /* Print defined symbols only. */ 187 static int non_weak = 0; /* Ignore weak symbols. */ 188 static int no_sort = 0; /* Don't sort; print syms in order found. */ 189 static int print_debug_syms = 0;/* Print debugger-only symbols too. */ 190 static int print_armap = 0; /* Describe __.SYMDEF data in archive files. */ 191 static int print_size = 0; /* Print size of defined symbols. */ 192 static int reverse_sort = 0; /* Sort in downward(alpha or numeric) order. */ 193 static int sort_numerically = 0;/* Sort in numeric rather than alpha order. */ 194 static int sort_by_size = 0; /* Sort by size of symbol. */ 195 static int undefined_only = 0; /* Print undefined symbols only. */ 196 static int dynamic = 0; /* Print dynamic symbols. */ 197 static int show_version = 0; /* Show the version number. */ 198 static int show_synthetic = 0; /* Display synthesized symbols too. */ 199 static int line_numbers = 0; /* Print line numbers for symbols. */ 200 static int allow_special_symbols = 0; /* Allow special symbols. */ 201 static int with_symbol_versions = -1; /* Output symbol version information. */ 202 static int quiet = 0; /* Suppress "no symbols" diagnostic. */ 203 204 /* The characters to use for global and local ifunc symbols. */ 205 #if DEFAULT_F_FOR_IFUNC_SYMBOLS 206 static const char * ifunc_type_chars = "Ff"; 207 #else 208 static const char * ifunc_type_chars = NULL; 209 #endif 210 211 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS; 212 213 /* When to print the names of files. Not mutually exclusive in SYSV format. */ 214 static int filename_per_file = 0; /* Once per file, on its own line. */ 215 static int filename_per_symbol = 0; /* Once per symbol, at start of line. */ 216 217 static int print_width = 0; 218 static int print_radix = 16; 219 /* Print formats for printing stab info. */ 220 static char other_format[] = "%02x"; 221 static char desc_format[] = "%04x"; 222 223 static char *target = NULL; 224 #if BFD_SUPPORTS_PLUGINS 225 static const char *plugin_target = "plugin"; 226 #else 227 static const char *plugin_target = NULL; 228 #endif 229 230 typedef enum unicode_display_type 231 { 232 unicode_default = 0, 233 unicode_locale, 234 unicode_escape, 235 unicode_hex, 236 unicode_highlight, 237 unicode_invalid 238 } unicode_display_type; 239 240 static unicode_display_type unicode_display = unicode_default; 241 242 enum long_option_values 243 { 244 OPTION_TARGET = 200, 245 OPTION_PLUGIN, 246 OPTION_SIZE_SORT, 247 OPTION_RECURSE_LIMIT, 248 OPTION_NO_RECURSE_LIMIT, 249 OPTION_IFUNC_CHARS, 250 OPTION_UNICODE, 251 OPTION_QUIET 252 }; 253 254 static struct option long_options[] = 255 { 256 {"debug-syms", no_argument, &print_debug_syms, 1}, 257 {"demangle", optional_argument, 0, 'C'}, 258 {"dynamic", no_argument, &dynamic, 1}, 259 {"extern-only", no_argument, &external_only, 1}, 260 {"format", required_argument, 0, 'f'}, 261 {"help", no_argument, 0, 'h'}, 262 {"ifunc-chars", required_argument, 0, OPTION_IFUNC_CHARS}, 263 {"just-symbols", no_argument, 0, 'j'}, 264 {"line-numbers", no_argument, 0, 'l'}, 265 {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */ 266 {"no-demangle", no_argument, &do_demangle, 0}, 267 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT}, 268 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT}, 269 {"no-sort", no_argument, 0, 'p'}, 270 {"numeric-sort", no_argument, 0, 'n'}, 271 {"plugin", required_argument, 0, OPTION_PLUGIN}, 272 {"portability", no_argument, 0, 'P'}, 273 {"print-armap", no_argument, &print_armap, 1}, 274 {"print-file-name", no_argument, 0, 'o'}, 275 {"print-size", no_argument, 0, 'S'}, 276 {"quiet", no_argument, 0, OPTION_QUIET}, 277 {"radix", required_argument, 0, 't'}, 278 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT}, 279 {"recursion-limit", no_argument, NULL, OPTION_RECURSE_LIMIT}, 280 {"reverse-sort", no_argument, &reverse_sort, 1}, 281 {"size-sort", no_argument, 0, OPTION_SIZE_SORT}, 282 {"special-syms", no_argument, &allow_special_symbols, 1}, 283 {"synthetic", no_argument, &show_synthetic, 1}, 284 {"target", required_argument, 0, OPTION_TARGET}, 285 {"defined-only", no_argument, 0, 'U'}, 286 {"undefined-only", no_argument, 0, 'u'}, 287 {"unicode", required_argument, NULL, OPTION_UNICODE}, 288 {"version", no_argument, &show_version, 1}, 289 {"no-weak", no_argument, 0, 'W'}, 290 {"with-symbol-versions", no_argument, &with_symbol_versions, 1}, 291 {"without-symbol-versions", no_argument, &with_symbol_versions, 0}, 292 {0, no_argument, 0, 0} 293 }; 294 295 /* Some error-reporting functions. */ 297 298 ATTRIBUTE_NORETURN static void 299 usage (FILE *stream, int status) 300 { 301 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name); 302 fprintf (stream, _(" List symbols in [file(s)] (a.out by default).\n")); 303 fprintf (stream, _(" The options are:\n")); 304 fprintf (stream, _("\ 305 -a, --debug-syms Display debugger-only symbols\n")); 306 fprintf (stream, _("\ 307 -A, --print-file-name Print name of the input file before every symbol\n")); 308 fprintf (stream, _("\ 309 -B Same as --format=bsd\n")); 310 fprintf (stream, _("\ 311 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n")); 312 display_demangler_styles (stream, _("\ 313 STYLE can be ")); 314 fprintf (stream, _("\ 315 --no-demangle Do not demangle low-level symbol names\n")); 316 fprintf (stream, _("\ 317 --recurse-limit Enable a demangling recursion limit. (default)\n")); 318 fprintf (stream, _("\ 319 --no-recurse-limit Disable a demangling recursion limit.\n")); 320 fprintf (stream, _("\ 321 -D, --dynamic Display dynamic symbols instead of normal symbols\n")); 322 fprintf (stream, _("\ 323 -e (ignored)\n")); 324 fprintf (stream, _("\ 325 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\ 326 `sysv', `posix' or 'just-symbols'.\n\ 327 The default is `bsd'\n")); 328 fprintf (stream, _("\ 329 -g, --extern-only Display only external symbols\n")); 330 fprintf (stream, _("\ 331 --ifunc-chars=CHARS Characters to use when displaying ifunc symbols\n")); 332 fprintf (stream, _("\ 333 -j, --just-symbols Same as --format=just-symbols\n")); 334 fprintf (stream, _("\ 335 -l, --line-numbers Use debugging information to find a filename and\n\ 336 line number for each symbol\n")); 337 fprintf (stream, _("\ 338 -n, --numeric-sort Sort symbols numerically by address\n")); 339 fprintf (stream, _("\ 340 -o Same as -A\n")); 341 fprintf (stream, _("\ 342 -p, --no-sort Do not sort the symbols\n")); 343 fprintf (stream, _("\ 344 -P, --portability Same as --format=posix\n")); 345 fprintf (stream, _("\ 346 -r, --reverse-sort Reverse the sense of the sort\n")); 347 #if BFD_SUPPORTS_PLUGINS 348 fprintf (stream, _("\ 349 --plugin NAME Load the specified plugin\n")); 350 #endif 351 fprintf (stream, _("\ 352 -S, --print-size Print size of defined symbols\n")); 353 fprintf (stream, _("\ 354 -s, --print-armap Include index for symbols from archive members\n")); 355 fprintf (stream, _("\ 356 --quiet Suppress \"no symbols\" diagnostic\n")); 357 fprintf (stream, _("\ 358 --size-sort Sort symbols by size\n")); 359 fprintf (stream, _("\ 360 --special-syms Include special symbols in the output\n")); 361 fprintf (stream, _("\ 362 --synthetic Display synthetic symbols as well\n")); 363 fprintf (stream, _("\ 364 -t, --radix=RADIX Use RADIX for printing symbol values\n")); 365 fprintf (stream, _("\ 366 --target=BFDNAME Specify the target object format as BFDNAME\n")); 367 fprintf (stream, _("\ 368 -u, --undefined-only Display only undefined symbols\n")); 369 fprintf (stream, _("\ 370 -U, --defined-only Display only defined symbols\n")); 371 fprintf (stream, _("\ 372 --unicode={default|show|invalid|hex|escape|highlight}\n\ 373 Specify how to treat UTF-8 encoded unicode characters\n")); 374 fprintf (stream, _("\ 375 -W, --no-weak Ignore weak symbols\n")); 376 fprintf (stream, _("\ 377 --without-symbol-versions Do not display version strings after symbol names\n")); 378 fprintf (stream, _("\ 379 -X 32_64 (ignored)\n")); 380 fprintf (stream, _("\ 381 @FILE Read options from FILE\n")); 382 fprintf (stream, _("\ 383 -h, --help Display this information\n")); 384 fprintf (stream, _("\ 385 -V, --version Display this program's version number\n")); 386 387 list_supported_targets (program_name, stream); 388 if (REPORT_BUGS_TO[0] && status == 0) 389 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO); 390 exit (status); 391 } 392 393 /* Set the radix for the symbol value and size according to RADIX. */ 394 395 static void 396 set_print_radix (char *radix) 397 { 398 switch (*radix) 399 { 400 case 'x': print_radix = 16; break; 401 case 'd': print_radix = 10; break; 402 case 'o': print_radix = 8; break; 403 404 default: 405 fatal (_("%s: invalid radix"), radix); 406 } 407 408 other_format[3] = desc_format[3] = *radix; 409 } 410 411 static void 412 set_output_format (char *f) 413 { 414 int i; 415 416 switch (*f) 417 { 418 case 'b': 419 case 'B': 420 i = FORMAT_BSD; 421 break; 422 case 'p': 423 case 'P': 424 i = FORMAT_POSIX; 425 break; 426 case 's': 427 case 'S': 428 i = FORMAT_SYSV; 429 break; 430 case 'j': 431 case 'J': 432 i = FORMAT_JUST_SYMBOLS; 433 break; 434 default: 435 fatal (_("%s: invalid output format"), f); 436 } 437 format = &formats[i]; 438 print_format = i; 439 } 440 441 static const char * 443 get_elf_symbol_type (unsigned int type) 444 { 445 static char *bufp; 446 447 switch (type) 448 { 449 case STT_NOTYPE: return "NOTYPE"; 450 case STT_OBJECT: return "OBJECT"; 451 case STT_FUNC: return "FUNC"; 452 case STT_SECTION: return "SECTION"; 453 case STT_FILE: return "FILE"; 454 case STT_COMMON: return "COMMON"; 455 case STT_TLS: return "TLS"; 456 } 457 458 free (bufp); 459 if (type >= STT_LOPROC && type <= STT_HIPROC) 460 bufp = xasprintf (_("<processor specific>: %d"), type); 461 else if (type >= STT_LOOS && type <= STT_HIOS) 462 bufp = xasprintf (_("<OS specific>: %d"), type); 463 else 464 bufp = xasprintf (_("<unknown>: %d"), type); 465 return bufp; 466 } 467 468 static const char * 469 get_coff_symbol_type (const struct internal_syment *sym) 470 { 471 static char *bufp; 472 473 switch (sym->n_sclass) 474 { 475 case C_BLOCK: return "Block"; 476 case C_FILE: return "File"; 477 case C_LINE: return "Line"; 478 } 479 480 if (!sym->n_type) 481 return "None"; 482 483 switch (DTYPE(sym->n_type)) 484 { 485 case DT_FCN: return "Function"; 486 case DT_PTR: return "Pointer"; 487 case DT_ARY: return "Array"; 488 } 489 490 free (bufp); 491 bufp = xasprintf (_("<unknown>: %d/%d"), sym->n_sclass, sym->n_type); 492 return bufp; 493 } 494 495 /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT. 497 The conversion format is controlled by the unicode_display variable. 498 Returns the number of characters added to OUT. 499 Returns the number of bytes consumed from IN in CONSUMED. 500 Always consumes at least one byte and displays at least one character. */ 501 502 static unsigned int 503 display_utf8 (const unsigned char * in, char * out, unsigned int * consumed) 504 { 505 char * orig_out = out; 506 unsigned int nchars = 0; 507 unsigned int j; 508 509 if (unicode_display == unicode_default) 510 goto invalid; 511 512 if (in[0] < 0xc0) 513 goto invalid; 514 515 if ((in[1] & 0xc0) != 0x80) 516 goto invalid; 517 518 if ((in[0] & 0x20) == 0) 519 { 520 nchars = 2; 521 goto valid; 522 } 523 524 if ((in[2] & 0xc0) != 0x80) 525 goto invalid; 526 527 if ((in[0] & 0x10) == 0) 528 { 529 nchars = 3; 530 goto valid; 531 } 532 533 if ((in[3] & 0xc0) != 0x80) 534 goto invalid; 535 536 nchars = 4; 537 538 valid: 539 switch (unicode_display) 540 { 541 case unicode_locale: 542 /* Copy the bytes into the output buffer as is. */ 543 memcpy (out, in, nchars); 544 out += nchars; 545 break; 546 547 case unicode_invalid: 548 case unicode_hex: 549 *out++ = unicode_display == unicode_hex ? '<' : '{'; 550 *out++ = '0'; 551 *out++ = 'x'; 552 for (j = 0; j < nchars; j++) 553 out += sprintf (out, "%02x", in [j]); 554 *out++ = unicode_display == unicode_hex ? '>' : '}'; 555 break; 556 557 case unicode_highlight: 558 if (isatty (1)) 559 out += sprintf (out, "\x1B[31;47m"); /* Red. */ 560 /* Fall through. */ 561 case unicode_escape: 562 switch (nchars) 563 { 564 case 2: 565 out += sprintf (out, "\\u%02x%02x", 566 ((in[0] & 0x1c) >> 2), 567 ((in[0] & 0x03) << 6) | (in[1] & 0x3f)); 568 break; 569 570 case 3: 571 out += sprintf (out, "\\u%02x%02x", 572 ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2), 573 ((in[1] & 0x03) << 6) | ((in[2] & 0x3f))); 574 break; 575 576 case 4: 577 out += sprintf (out, "\\u%02x%02x%02x", 578 ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2), 579 ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2), 580 ((in[2] & 0x03) << 6) | ((in[3] & 0x3f))); 581 break; 582 default: 583 /* URG. */ 584 break; 585 } 586 587 if (unicode_display == unicode_highlight && isatty (1)) 588 out += sprintf (out, "\x1B[0m"); /* Default colour. */ 589 break; 590 591 default: 592 /* URG */ 593 break; 594 } 595 596 * consumed = nchars; 597 return out - orig_out; 598 599 invalid: 600 /* Not a valid UTF-8 sequence. */ 601 *out = *in; 602 * consumed = 1; 603 return 1; 604 } 605 606 /* Convert any UTF-8 encoded characters in NAME into the form specified by 607 unicode_display. Also converts control characters. Returns a static 608 buffer if conversion was necessary. 609 Code stolen from objdump.c:sanitize_string(). */ 610 611 static const char * 612 convert_utf8 (const char * in) 613 { 614 static char * buffer = NULL; 615 static size_t buffer_len = 0; 616 const char * original = in; 617 char * out; 618 619 /* Paranoia. */ 620 if (in == NULL) 621 return ""; 622 623 /* See if any conversion is necessary. 624 In the majority of cases it will not be needed. */ 625 do 626 { 627 unsigned char c = *in++; 628 629 if (c == 0) 630 return original; 631 632 if (ISCNTRL (c)) 633 break; 634 635 if (unicode_display != unicode_default && c >= 0xc0) 636 break; 637 } 638 while (1); 639 640 /* Copy the input, translating as needed. */ 641 in = original; 642 /* For 2 char unicode, max out is 12 (colour escapes) + 6, ie. 9 per in 643 For hex, max out is 8 for 2 char unicode, ie. 4 per in. 644 3 and 4 char unicode produce less output for input. */ 645 size_t max_needed = strlen (in) * 9 + 1; 646 if (buffer_len < max_needed) 647 { 648 buffer_len = max_needed; 649 free (buffer); 650 buffer = xmalloc (buffer_len); 651 } 652 653 out = buffer; 654 do 655 { 656 unsigned char c = *in++; 657 658 if (c == 0) 659 break; 660 661 if (ISCNTRL (c)) 662 { 663 *out++ = '^'; 664 *out++ = c + 0x40; 665 } 666 else if (unicode_display != unicode_default && c >= 0xc0) 667 { 668 unsigned int num_consumed; 669 670 out += display_utf8 ((const unsigned char *) --in, out, &num_consumed); 671 in += num_consumed; 672 } 673 else 674 *out++ = c; 675 } 676 while (1); 677 678 *out = 0; 679 return buffer; 680 } 681 682 /* Print symbol name NAME, read from ABFD, with printf format FORM, 683 demangling it if requested. */ 684 685 static void 686 print_symname (const char *form, struct extended_symbol_info *info, 687 const char *name, bfd *abfd) 688 { 689 char *alloc = NULL; 690 char *atname = NULL; 691 692 if (name == NULL) 693 name = info->sinfo->name; 694 695 if (!with_symbol_versions 696 && bfd_get_flavour (abfd) == bfd_target_elf_flavour) 697 { 698 char *atver = strchr (name, '@'); 699 700 if (atver) 701 { 702 /* PR 32467 - Corrupt binaries might include an @ character in a 703 symbol name. Since non-versioned symbol names can be in 704 read-only memory (via memory mapping of a file's contents) we 705 cannot just replace the @ character with a NUL. Instead we 706 create a truncated copy of the name. */ 707 atname = xstrdup (name); 708 atname [atver - name] = 0; 709 name = atname; 710 } 711 } 712 713 if (do_demangle && *name) 714 { 715 alloc = bfd_demangle (abfd, name, demangle_flags); 716 if (alloc != NULL) 717 name = alloc; 718 } 719 720 if (unicode_display != unicode_default) 721 name = convert_utf8 (name); 722 723 if (info != NULL && info->elfinfo && with_symbol_versions) 724 { 725 const char *version_string; 726 bool hidden; 727 728 version_string 729 = bfd_get_symbol_version_string (abfd, &info->elfinfo->symbol, 730 false, &hidden); 731 if (version_string && version_string[0]) 732 { 733 const char *at = "@@"; 734 if (hidden || bfd_is_und_section (info->elfinfo->symbol.section)) 735 at = "@"; 736 alloc = reconcat (alloc, name, at, version_string, NULL); 737 if (alloc != NULL) 738 name = alloc; 739 } 740 } 741 printf (form, name); 742 743 free (atname); 744 free (alloc); 745 } 746 747 static void 748 print_symdef_entry (bfd *abfd) 749 { 750 symindex idx = BFD_NO_MORE_SYMBOLS; 751 carsym *thesym; 752 bool everprinted = false; 753 754 for (idx = bfd_get_next_mapent (abfd, idx, &thesym); 755 idx != BFD_NO_MORE_SYMBOLS; 756 idx = bfd_get_next_mapent (abfd, idx, &thesym)) 757 { 758 if (!everprinted) 759 { 760 printf (_("\nArchive index:\n")); 761 everprinted = true; 762 } 763 if (thesym->name != NULL) 764 { 765 print_symname ("%s", NULL, thesym->name, abfd); 766 bfd *elt = bfd_get_elt_at_index (abfd, idx); 767 if (elt) 768 printf (" in %s\n", bfd_get_filename (elt)); 769 else 770 printf ("\n"); 771 } 772 } 773 } 774 775 777 /* True when we can report missing plugin error. */ 778 bool report_plugin_err = true; 779 780 /* Choose which symbol entries to print; 781 compact them downward to get rid of the rest. 782 Return the number of symbols to be printed. */ 783 784 static long 785 filter_symbols (bfd *abfd, bool is_dynamic, void *minisyms, 786 long symcount, unsigned int size) 787 { 788 bfd_byte *from, *fromend, *to; 789 asymbol *store; 790 791 store = bfd_make_empty_symbol (abfd); 792 if (store == NULL) 793 bfd_fatal (bfd_get_filename (abfd)); 794 795 from = (bfd_byte *) minisyms; 796 fromend = from + symcount * size; 797 to = (bfd_byte *) minisyms; 798 799 for (; from < fromend; from += size) 800 { 801 int keep = 0; 802 asymbol *sym; 803 804 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store); 805 if (sym == NULL) 806 continue; 807 808 if (bfd_lto_slim_symbol_p (abfd, sym->name) 809 && report_plugin_err) 810 { 811 report_plugin_err = false; 812 non_fatal (_("%s: plugin needed to handle lto object"), 813 bfd_get_filename (abfd)); 814 } 815 816 if (undefined_only) 817 keep = bfd_is_und_section (sym->section); 818 else if (external_only) 819 /* PR binutls/12753: Unique symbols are global too. */ 820 keep = ((sym->flags & (BSF_GLOBAL 821 | BSF_WEAK 822 | BSF_GNU_UNIQUE)) != 0 823 || bfd_is_und_section (sym->section) 824 || bfd_is_com_section (sym->section)); 825 else if (non_weak) 826 keep = ((sym->flags & BSF_WEAK) == 0); 827 else 828 keep = 1; 829 830 if (keep 831 && ! print_debug_syms 832 && (sym->flags & BSF_DEBUGGING) != 0) 833 keep = 0; 834 835 if (keep 836 && sort_by_size 837 && (bfd_is_abs_section (sym->section) 838 || bfd_is_und_section (sym->section))) 839 keep = 0; 840 841 if (keep 842 && defined_only) 843 { 844 if (bfd_is_und_section (sym->section)) 845 keep = 0; 846 } 847 848 if (keep 849 && bfd_is_target_special_symbol (abfd, sym) 850 && ! allow_special_symbols) 851 keep = 0; 852 853 if (keep) 854 { 855 if (to != from) 856 memcpy (to, from, size); 857 to += size; 858 } 859 } 860 861 return (to - (bfd_byte *) minisyms) / size; 862 } 863 864 /* These globals are used to pass information into the sorting 866 routines. */ 867 static bfd *sort_bfd; 868 static bool sort_dynamic; 869 static asymbol *sort_x; 870 static asymbol *sort_y; 871 872 /* Symbol-sorting predicates */ 873 #define valueof(x) ((x)->section->vma + (x)->value) 874 875 /* Numeric sorts. Undefined symbols are always considered "less than" 876 defined symbols with zero values. Common symbols are not treated 877 specially -- i.e., their sizes are used as their "values". */ 878 879 static int 880 non_numeric_forward (const void *P_x, const void *P_y) 881 { 882 asymbol *x, *y; 883 const char *xn, *yn; 884 885 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x); 886 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y); 887 if (x == NULL || y == NULL) 888 bfd_fatal (bfd_get_filename (sort_bfd)); 889 890 xn = bfd_asymbol_name (x); 891 yn = bfd_asymbol_name (y); 892 893 if (yn == NULL) 894 return xn != NULL; 895 if (xn == NULL) 896 return -1; 897 898 /* Solaris 2.5 has a bug in strcoll. 899 strcoll returns invalid values when confronted with empty strings. */ 900 if (*yn == '\0') 901 return *xn != '\0'; 902 if (*xn == '\0') 903 return -1; 904 905 return strcoll (xn, yn); 906 } 907 908 static int 909 non_numeric_reverse (const void *x, const void *y) 910 { 911 return - non_numeric_forward (x, y); 912 } 913 914 static int 915 numeric_forward (const void *P_x, const void *P_y) 916 { 917 asymbol *x, *y; 918 asection *xs, *ys; 919 920 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x); 921 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y); 922 if (x == NULL || y == NULL) 923 bfd_fatal (bfd_get_filename (sort_bfd)); 924 925 xs = bfd_asymbol_section (x); 926 ys = bfd_asymbol_section (y); 927 928 if (bfd_is_und_section (xs)) 929 { 930 if (! bfd_is_und_section (ys)) 931 return -1; 932 } 933 else if (bfd_is_und_section (ys)) 934 return 1; 935 else if (valueof (x) != valueof (y)) 936 return valueof (x) < valueof (y) ? -1 : 1; 937 938 return non_numeric_forward (P_x, P_y); 939 } 940 941 static int 942 numeric_reverse (const void *x, const void *y) 943 { 944 return - numeric_forward (x, y); 945 } 946 947 static int (*(sorters[2][2])) (const void *, const void *) = 948 { 949 { non_numeric_forward, non_numeric_reverse }, 950 { numeric_forward, numeric_reverse } 951 }; 952 953 /* This sort routine is used by sort_symbols_by_size. It is similar 954 to numeric_forward, but when symbols have the same value it sorts 955 by section VMA. This simplifies the sort_symbols_by_size code 956 which handles symbols at the end of sections. Also, this routine 957 tries to sort file names before other symbols with the same value. 958 That will make the file name have a zero size, which will make 959 sort_symbols_by_size choose the non file name symbol, leading to 960 more meaningful output. For similar reasons, this code sorts 961 gnu_compiled_* and gcc2_compiled before other symbols with the same 962 value. */ 963 964 static int 965 size_forward1 (const void *P_x, const void *P_y) 966 { 967 asymbol *x, *y; 968 asection *xs, *ys; 969 const char *xn, *yn; 970 size_t xnl, ynl; 971 int xf, yf; 972 973 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x); 974 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y); 975 if (x == NULL || y == NULL) 976 bfd_fatal (bfd_get_filename (sort_bfd)); 977 978 xs = bfd_asymbol_section (x); 979 ys = bfd_asymbol_section (y); 980 981 if (bfd_is_und_section (xs)) 982 abort (); 983 if (bfd_is_und_section (ys)) 984 abort (); 985 986 if (valueof (x) != valueof (y)) 987 return valueof (x) < valueof (y) ? -1 : 1; 988 989 if (xs->vma != ys->vma) 990 return xs->vma < ys->vma ? -1 : 1; 991 992 xn = bfd_asymbol_name (x); 993 yn = bfd_asymbol_name (y); 994 xnl = strlen (xn); 995 ynl = strlen (yn); 996 997 /* The symbols gnu_compiled and gcc2_compiled convey even less 998 information than the file name, so sort them out first. */ 999 1000 xf = (strstr (xn, "gnu_compiled") != NULL 1001 || strstr (xn, "gcc2_compiled") != NULL); 1002 yf = (strstr (yn, "gnu_compiled") != NULL 1003 || strstr (yn, "gcc2_compiled") != NULL); 1004 1005 if (xf && ! yf) 1006 return -1; 1007 if (! xf && yf) 1008 return 1; 1009 1010 /* We use a heuristic for the file name. It may not work on non 1011 Unix systems, but it doesn't really matter; the only difference 1012 is precisely which symbol names get printed. */ 1013 1014 #define file_symbol(s, sn, snl) \ 1015 (((s)->flags & BSF_FILE) != 0 \ 1016 || ((snl) > 2 \ 1017 && (sn)[(snl) - 2] == '.' \ 1018 && ((sn)[(snl) - 1] == 'o' \ 1019 || (sn)[(snl) - 1] == 'a'))) 1020 1021 xf = file_symbol (x, xn, xnl); 1022 yf = file_symbol (y, yn, ynl); 1023 1024 if (xf && ! yf) 1025 return -1; 1026 if (! xf && yf) 1027 return 1; 1028 1029 return non_numeric_forward (P_x, P_y); 1030 } 1031 1032 /* This sort routine is used by sort_symbols_by_size. It is sorting 1033 an array of size_sym structures into size order. */ 1034 1035 static int 1036 size_forward2 (const void *P_x, const void *P_y) 1037 { 1038 const struct size_sym *x = (const struct size_sym *) P_x; 1039 const struct size_sym *y = (const struct size_sym *) P_y; 1040 1041 if (x->size < y->size) 1042 return reverse_sort ? 1 : -1; 1043 else if (x->size > y->size) 1044 return reverse_sort ? -1 : 1; 1045 else 1046 return sorters[0][reverse_sort] (x->minisym, y->minisym); 1047 } 1048 1049 /* Sort the symbols by size. ELF and COFF may provide a size but for other 1050 formats we have to make a guess by assuming that the difference between 1051 the address of a symbol and the address of the next higher symbol is the 1052 size. */ 1053 1054 static long 1055 sort_symbols_by_size (bfd *abfd, bool is_dynamic, void *minisyms, 1056 long symcount, unsigned int size, 1057 struct size_sym **symsizesp) 1058 { 1059 struct size_sym *symsizes; 1060 bfd_byte *from, *fromend; 1061 asymbol *sym = NULL; 1062 asymbol *store_sym, *store_next; 1063 1064 qsort (minisyms, symcount, size, size_forward1); 1065 1066 /* We are going to return a special set of symbols and sizes to 1067 print. */ 1068 symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym)); 1069 *symsizesp = symsizes; 1070 1071 /* Note that filter_symbols has already removed all absolute and 1072 undefined symbols. Here we remove all symbols whose size winds 1073 up as zero. */ 1074 from = (bfd_byte *) minisyms; 1075 fromend = from + symcount * size; 1076 1077 store_sym = sort_x; 1078 store_next = sort_y; 1079 1080 if (from < fromend) 1081 { 1082 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, (const void *) from, 1083 store_sym); 1084 if (sym == NULL) 1085 bfd_fatal (bfd_get_filename (abfd)); 1086 } 1087 1088 for (; from < fromend; from += size) 1089 { 1090 asymbol *next; 1091 asection *sec; 1092 bfd_vma sz; 1093 asymbol *temp; 1094 const elf_symbol_type *elfsym; 1095 const coff_symbol_type *coffsym; 1096 1097 if (from + size < fromend) 1098 { 1099 next = bfd_minisymbol_to_symbol (abfd, 1100 is_dynamic, 1101 (const void *) (from + size), 1102 store_next); 1103 if (next == NULL) 1104 bfd_fatal (bfd_get_filename (abfd)); 1105 } 1106 else 1107 next = NULL; 1108 1109 sec = bfd_asymbol_section (sym); 1110 1111 /* Synthetic symbols don't have a full type set of data available, thus 1112 we can't rely on that information for the symbol size. Ditto for 1113 bfd/section.c:global_syms like *ABS*. */ 1114 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0 1115 && (elfsym = elf_symbol_from (sym)) != NULL 1116 && elfsym->internal_elf_sym.st_size != 0) 1117 sz = elfsym->internal_elf_sym.st_size; 1118 else if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0 1119 && (coffsym = coff_symbol_from (sym)) != NULL 1120 && ISFCN (coffsym->native->u.syment.n_type) 1121 && coffsym->native->u.syment.n_numaux != 0 1122 && coffsym->native[1].u.auxent.x_sym.x_misc.x_fsize != 0) 1123 sz = coffsym->native[1].u.auxent.x_sym.x_misc.x_fsize; 1124 else if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0 1125 && bfd_is_com_section (sec)) 1126 sz = sym->value; 1127 else 1128 { 1129 if (from + size < fromend 1130 && sec == bfd_asymbol_section (next)) 1131 sz = valueof (next) - valueof (sym); 1132 else 1133 sz = (bfd_section_vma (sec) 1134 + bfd_section_size (sec) 1135 - valueof (sym)); 1136 } 1137 1138 if (sz != 0) 1139 { 1140 symsizes->minisym = (const void *) from; 1141 symsizes->size = sz; 1142 ++symsizes; 1143 } 1144 1145 sym = next; 1146 1147 temp = store_sym; 1148 store_sym = store_next; 1149 store_next = temp; 1150 } 1151 1152 symcount = symsizes - *symsizesp; 1153 1154 /* We must now sort again by size. */ 1155 qsort ((void *) *symsizesp, symcount, sizeof (struct size_sym), size_forward2); 1156 1157 return symcount; 1158 } 1159 1160 /* This function is used to get the relocs for a particular section. 1161 It is called via bfd_map_over_sections. */ 1162 1163 static void 1164 get_relocs (bfd *abfd, asection *sec, void *dataarg) 1165 { 1166 struct lineno_cache *data = (struct lineno_cache *) dataarg; 1167 1168 *data->secs = sec; 1169 *data->relocs = NULL; 1170 *data->relcount = 0; 1171 1172 if ((sec->flags & SEC_RELOC) != 0) 1173 { 1174 long relsize = bfd_get_reloc_upper_bound (abfd, sec); 1175 if (relsize > 0) 1176 { 1177 *data->relocs = (arelent **) xmalloc (relsize); 1178 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs, 1179 data->syms); 1180 } 1181 } 1182 1183 ++data->secs; 1184 ++data->relocs; 1185 ++data->relcount; 1186 } 1187 1188 static void 1189 free_lineno_cache (bfd *abfd) 1190 { 1191 struct lineno_cache *lc = bfd_usrdata (abfd); 1192 1193 if (lc) 1194 { 1195 if (lc->relocs) 1196 for (unsigned int i = 0; i < lc->seccount; i++) 1197 free (lc->relocs[i]); 1198 free (lc->relcount); 1199 free (lc->relocs); 1200 free (lc->secs); 1201 free (lc->syms); 1202 free (lc); 1203 bfd_set_usrdata (abfd, NULL); 1204 } 1205 } 1206 1207 /* Print a single symbol. */ 1208 1209 static void 1210 print_symbol (bfd * abfd, 1211 asymbol * sym, 1212 bfd_vma ssize, 1213 bfd * archive_bfd) 1214 { 1215 symbol_info syminfo; 1216 struct extended_symbol_info info; 1217 1218 format->print_symbol_filename (archive_bfd, abfd); 1219 1220 bfd_get_symbol_info (abfd, sym, &syminfo); 1221 1222 /* PR 22967 - Distinguish between local and global ifunc symbols. */ 1223 if (syminfo.type == 'i' 1224 && sym->flags & BSF_GNU_INDIRECT_FUNCTION) 1225 { 1226 if (ifunc_type_chars == NULL || ifunc_type_chars[0] == 0) 1227 ; /* Change nothing. */ 1228 else if (sym->flags & BSF_GLOBAL) 1229 syminfo.type = ifunc_type_chars[0]; 1230 else if (ifunc_type_chars[1] != 0) 1231 syminfo.type = ifunc_type_chars[1]; 1232 } 1233 1234 info.sinfo = &syminfo; 1235 info.ssize = ssize; 1236 /* Synthetic symbols do not have a full symbol type set of data available. 1237 Nor do bfd/section.c:global_syms like *ABS*. */ 1238 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) != 0) 1239 { 1240 info.elfinfo = NULL; 1241 info.coffinfo = NULL; 1242 } 1243 else 1244 { 1245 info.elfinfo = elf_symbol_from (sym); 1246 info.coffinfo = coff_symbol_from (sym); 1247 } 1248 1249 format->print_symbol_info (&info, abfd); 1250 1251 const char *symname = bfd_asymbol_name (sym); 1252 if (line_numbers && symname != NULL && symname[0] != 0) 1253 { 1254 struct lineno_cache *lc = bfd_usrdata (abfd); 1255 const char *filename, *functionname; 1256 unsigned int lineno; 1257 1258 /* We need to get the canonical symbols in order to call 1259 bfd_find_nearest_line. This is inefficient, but, then, you 1260 don't have to use --line-numbers. */ 1261 if (lc == NULL) 1262 { 1263 lc = xcalloc (1, sizeof (*lc)); 1264 bfd_set_usrdata (abfd, lc); 1265 } 1266 if (lc->syms == NULL && lc->symcount == 0) 1267 { 1268 long symsize = bfd_get_symtab_upper_bound (abfd); 1269 if (symsize <= 0) 1270 lc->symcount = -1; 1271 else 1272 { 1273 lc->syms = xmalloc (symsize); 1274 lc->symcount = bfd_canonicalize_symtab (abfd, lc->syms); 1275 } 1276 } 1277 1278 if (lc->symcount <= 0) 1279 ; 1280 else if (bfd_is_und_section (bfd_asymbol_section (sym))) 1281 { 1282 unsigned int i; 1283 1284 /* For an undefined symbol, we try to find a reloc for the 1285 symbol, and print the line number of the reloc. */ 1286 if (lc->relocs == NULL) 1287 { 1288 unsigned int seccount = bfd_count_sections (abfd); 1289 lc->seccount = seccount; 1290 lc->secs = xmalloc (seccount * sizeof (*lc->secs)); 1291 lc->relocs = xmalloc (seccount * sizeof (*lc->relocs)); 1292 lc->relcount = xmalloc (seccount * sizeof (*lc->relcount)); 1293 1294 struct lineno_cache rinfo = *lc; 1295 bfd_map_over_sections (abfd, get_relocs, &rinfo); 1296 } 1297 1298 for (i = 0; i < lc->seccount; i++) 1299 { 1300 long j; 1301 1302 for (j = 0; j < lc->relcount[i]; j++) 1303 { 1304 arelent *r; 1305 1306 r = lc->relocs[i][j]; 1307 if (r->sym_ptr_ptr != NULL 1308 && (*r->sym_ptr_ptr)->section == sym->section 1309 && (*r->sym_ptr_ptr)->value == sym->value 1310 && bfd_asymbol_name (*r->sym_ptr_ptr) != NULL 1311 && strcmp (symname, 1312 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0 1313 && bfd_find_nearest_line (abfd, lc->secs[i], lc->syms, 1314 r->address, &filename, 1315 &functionname, &lineno) 1316 && filename != NULL) 1317 { 1318 /* We only print the first one we find. */ 1319 printf ("\t%s:%u", filename, lineno); 1320 i = lc->seccount; 1321 break; 1322 } 1323 } 1324 } 1325 } 1326 else if (bfd_asymbol_section (sym)->owner == abfd) 1327 { 1328 if ((bfd_find_line (abfd, lc->syms, sym, &filename, &lineno) 1329 || bfd_find_nearest_line (abfd, bfd_asymbol_section (sym), 1330 lc->syms, sym->value, &filename, 1331 &functionname, &lineno)) 1332 && filename != NULL 1333 && lineno != 0) 1334 printf ("\t%s:%u", filename, lineno); 1335 } 1336 } 1337 1338 putchar ('\n'); 1339 } 1340 1341 /* Print the symbols when sorting by size. */ 1343 1344 static void 1345 print_size_symbols (bfd *abfd, 1346 bool is_dynamic, 1347 struct size_sym *symsizes, 1348 long symcount, 1349 bfd *archive_bfd) 1350 { 1351 asymbol *store; 1352 struct size_sym *from; 1353 struct size_sym *fromend; 1354 1355 store = bfd_make_empty_symbol (abfd); 1356 if (store == NULL) 1357 bfd_fatal (bfd_get_filename (abfd)); 1358 1359 from = symsizes; 1360 fromend = from + symcount; 1361 1362 for (; from < fromend; from++) 1363 { 1364 asymbol *sym; 1365 1366 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from->minisym, store); 1367 if (sym == NULL) 1368 bfd_fatal (bfd_get_filename (abfd)); 1369 1370 print_symbol (abfd, sym, from->size, archive_bfd); 1371 } 1372 } 1373 1374 1375 /* Print the symbols of ABFD that are held in MINISYMS. 1377 1378 If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. 1379 1380 SYMCOUNT is the number of symbols in MINISYMS. 1381 1382 SIZE is the size of a symbol in MINISYMS. */ 1383 1384 static void 1385 print_symbols (bfd *abfd, 1386 bool is_dynamic, 1387 void *minisyms, 1388 long symcount, 1389 unsigned int size, 1390 bfd *archive_bfd) 1391 { 1392 asymbol *store; 1393 bfd_byte *from; 1394 bfd_byte *fromend; 1395 1396 store = bfd_make_empty_symbol (abfd); 1397 if (store == NULL) 1398 bfd_fatal (bfd_get_filename (abfd)); 1399 1400 from = (bfd_byte *) minisyms; 1401 fromend = from + symcount * size; 1402 1403 for (; from < fromend; from += size) 1404 { 1405 asymbol *sym; 1406 1407 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store); 1408 if (sym == NULL) 1409 bfd_fatal (bfd_get_filename (abfd)); 1410 1411 print_symbol (abfd, sym, (bfd_vma) 0, archive_bfd); 1412 } 1413 } 1414 1415 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */ 1416 1417 static void 1418 display_rel_file (bfd *abfd, bfd *archive_bfd) 1419 { 1420 long symcount; 1421 void *minisyms; 1422 unsigned int size; 1423 struct size_sym *symsizes; 1424 asymbol *synthsyms = NULL; 1425 1426 if (! dynamic) 1427 { 1428 if (!(bfd_get_file_flags (abfd) & HAS_SYMS)) 1429 { 1430 if (!quiet) 1431 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd)); 1432 return; 1433 } 1434 } 1435 1436 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size); 1437 if (symcount <= 0) 1438 { 1439 if (!quiet) 1440 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd)); 1441 return; 1442 } 1443 1444 if (show_synthetic && size == sizeof (asymbol *)) 1445 { 1446 asymbol **static_syms = NULL; 1447 asymbol **dyn_syms = NULL; 1448 long static_count = 0; 1449 long dyn_count = 0; 1450 long synth_count; 1451 1452 if (dynamic) 1453 { 1454 dyn_count = symcount; 1455 dyn_syms = (asymbol **) minisyms; 1456 } 1457 else 1458 { 1459 long storage = bfd_get_dynamic_symtab_upper_bound (abfd); 1460 1461 static_count = symcount; 1462 static_syms = (asymbol **) minisyms; 1463 1464 if (storage > 0) 1465 { 1466 dyn_syms = (asymbol **) xmalloc (storage); 1467 dyn_count = bfd_canonicalize_dynamic_symtab (abfd, dyn_syms); 1468 if (dyn_count < 0) 1469 dyn_count = 0; 1470 } 1471 } 1472 1473 synth_count = bfd_get_synthetic_symtab (abfd, static_count, static_syms, 1474 dyn_count, dyn_syms, &synthsyms); 1475 if (synth_count > 0) 1476 { 1477 asymbol **symp; 1478 long i; 1479 1480 minisyms = xrealloc (minisyms, 1481 (symcount + synth_count + 1) * sizeof (*symp)); 1482 symp = (asymbol **) minisyms + symcount; 1483 for (i = 0; i < synth_count; i++) 1484 *symp++ = synthsyms + i; 1485 *symp = 0; 1486 symcount += synth_count; 1487 } 1488 if (!dynamic && dyn_syms != NULL) 1489 free (dyn_syms); 1490 } 1491 1492 /* lto_type is set to lto_non_ir_object when a bfd is loaded with a 1493 compiler LTO plugin. */ 1494 if (bfd_get_lto_type (abfd) == lto_slim_ir_object) 1495 { 1496 report_plugin_err = false; 1497 non_fatal (_("%s: plugin needed to handle lto object"), 1498 bfd_get_filename (abfd)); 1499 } 1500 1501 /* Discard the symbols we don't want to print. 1502 It's OK to do this in place; we'll free the storage anyway 1503 (after printing). */ 1504 1505 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size); 1506 1507 symsizes = NULL; 1508 if (! no_sort) 1509 { 1510 sort_bfd = abfd; 1511 sort_dynamic = dynamic; 1512 sort_x = bfd_make_empty_symbol (abfd); 1513 sort_y = bfd_make_empty_symbol (abfd); 1514 if (sort_x == NULL || sort_y == NULL) 1515 bfd_fatal (bfd_get_filename (abfd)); 1516 1517 if (! sort_by_size) 1518 qsort (minisyms, symcount, size, 1519 sorters[sort_numerically][reverse_sort]); 1520 else 1521 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount, 1522 size, &symsizes); 1523 } 1524 1525 if (! sort_by_size) 1526 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd); 1527 else 1528 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd); 1529 1530 free_lineno_cache (abfd); 1531 free (synthsyms); 1532 free (minisyms); 1533 free (symsizes); 1534 } 1535 1536 /* Construct a formatting string for printing symbol values. */ 1537 1538 static void 1539 set_print_format (bfd *file) 1540 { 1541 print_width = bfd_get_arch_size (file); 1542 1543 if (print_width == -1) 1544 { 1545 /* PR binutils/4292 1546 Guess the target's bitsize based on its name. 1547 We assume here than any 64-bit format will include 1548 "64" somewhere in its name. The only known exception 1549 is the MMO object file format. */ 1550 if (strstr (bfd_get_target (file), "64") != NULL 1551 || strcmp (bfd_get_target (file), "mmo") == 0) 1552 print_width = 64; 1553 else 1554 print_width = 32; 1555 } 1556 1557 char *p = print_format_string; 1558 *p++ = '%'; 1559 if (print_format == FORMAT_POSIX || print_format == FORMAT_JUST_SYMBOLS) 1560 { 1561 /* POSIX compatible output does not have any padding. */ 1562 } 1563 else if (print_width == 32) 1564 { 1565 *p++ = '0'; 1566 *p++ = '8'; 1567 } 1568 else /* print_width == 64. */ 1569 { 1570 *p++ = '0'; 1571 *p++ = '1'; 1572 *p++ = '6'; 1573 } 1574 1575 if (print_width == 32) 1576 { 1577 switch (print_radix) 1578 { 1579 case 8: strcpy (p, PRIo32); break; 1580 case 10: strcpy (p, PRId32); break; 1581 case 16: strcpy (p, PRIx32); break; 1582 } 1583 } 1584 else 1585 { 1586 switch (print_radix) 1587 { 1588 case 8: strcpy (p, PRIo64); break; 1589 case 10: strcpy (p, PRId64); break; 1590 case 16: strcpy (p, PRIx64); break; 1591 } 1592 } 1593 } 1594 1595 static void 1596 display_archive (bfd *file) 1597 { 1598 format->print_archive_filename (bfd_get_filename (file)); 1599 1600 if (print_armap) 1601 print_symdef_entry (file); 1602 1603 bfd *last_arfile = NULL; 1604 for (;;) 1605 { 1606 bfd *arfile = bfd_openr_next_archived_file (file, last_arfile); 1607 if (arfile == NULL 1608 || arfile == last_arfile) 1609 { 1610 if (arfile != NULL) 1611 bfd_set_error (bfd_error_malformed_archive); 1612 if (bfd_get_error () != bfd_error_no_more_archived_files) 1613 bfd_nonfatal (bfd_get_filename (file)); 1614 break; 1615 } 1616 1617 if (last_arfile != NULL) 1618 bfd_close (last_arfile); 1619 1620 char **matching; 1621 if (bfd_check_format_matches (arfile, bfd_object, &matching)) 1622 { 1623 set_print_format (arfile); 1624 format->print_archive_member (bfd_get_filename (file), 1625 bfd_get_filename (arfile)); 1626 display_rel_file (arfile, file); 1627 } 1628 else 1629 { 1630 bfd_nonfatal (bfd_get_filename (arfile)); 1631 if (bfd_get_error () == bfd_error_file_ambiguously_recognized) 1632 list_matching_formats (matching); 1633 } 1634 1635 last_arfile = arfile; 1636 } 1637 1638 if (last_arfile != NULL) 1639 bfd_close (last_arfile); 1640 } 1641 1642 static bool 1643 display_file (char *filename) 1644 { 1645 bool retval = true; 1646 bfd *file; 1647 char **matching; 1648 1649 if (get_file_size (filename) < 1) 1650 return false; 1651 1652 file = bfd_openr (filename, target ? target : plugin_target); 1653 if (file == NULL) 1654 { 1655 bfd_nonfatal (filename); 1656 return false; 1657 } 1658 1659 /* If printing line numbers, decompress the debug sections. */ 1660 if (line_numbers) 1661 file->flags |= BFD_DECOMPRESS; 1662 1663 if (bfd_check_format (file, bfd_archive)) 1664 { 1665 display_archive (file); 1666 } 1667 else if (bfd_check_format_matches (file, bfd_object, &matching)) 1668 { 1669 set_print_format (file); 1670 format->print_object_filename (filename); 1671 display_rel_file (file, NULL); 1672 } 1673 else 1674 { 1675 bfd_nonfatal (filename); 1676 if (bfd_get_error () == bfd_error_file_ambiguously_recognized) 1677 list_matching_formats (matching); 1678 retval = false; 1679 } 1680 1681 if (!bfd_close (file)) 1682 retval = false; 1683 1684 return retval; 1685 } 1686 1687 /* The following 3 groups of functions are called unconditionally, 1689 once at the start of processing each file of the appropriate type. 1690 They should check `filename_per_file' and `filename_per_symbol', 1691 as appropriate for their output format, to determine whether to 1692 print anything. */ 1693 1694 /* Print the name of an object file given on the command line. */ 1696 1697 static void 1698 print_object_filename_bsd (const char *filename) 1699 { 1700 if (filename_per_file && !filename_per_symbol) 1701 printf ("\n%s:\n", filename); 1702 } 1703 1704 static void 1705 print_object_filename_sysv (const char *filename) 1706 { 1707 if (undefined_only) 1708 printf (_("\n\nUndefined symbols from %s:\n\n"), filename); 1709 else 1710 printf (_("\n\nSymbols from %s:\n\n"), filename); 1711 if (print_width == 32) 1712 printf (_("\ 1713 Name Value Class Type Size Line Section\n\n")); 1714 else 1715 printf (_("\ 1716 Name Value Class Type Size Line Section\n\n")); 1717 } 1718 1719 static void 1720 print_object_filename_posix (const char *filename) 1721 { 1722 if (filename_per_file && !filename_per_symbol) 1723 printf ("%s:\n", filename); 1724 } 1725 1726 static void 1727 do_not_print_object_filename (const char *filename ATTRIBUTE_UNUSED) 1728 { 1729 } 1730 1731 /* Print the name of an archive file given on the command line. */ 1733 1734 static void 1735 print_archive_filename_bsd (const char *filename) 1736 { 1737 if (filename_per_file) 1738 printf ("\n%s:\n", filename); 1739 } 1740 1741 static void 1742 print_archive_filename_sysv (const char *filename ATTRIBUTE_UNUSED) 1743 { 1744 } 1745 1746 static void 1747 print_archive_filename_posix (const char *filename ATTRIBUTE_UNUSED) 1748 { 1749 } 1750 1751 static void 1752 do_not_print_archive_filename (const char *filename ATTRIBUTE_UNUSED) 1753 { 1754 } 1755 1756 /* Print the name of an archive member file. */ 1758 1759 static void 1760 print_archive_member_bsd (const char *archive ATTRIBUTE_UNUSED, 1761 const char *filename) 1762 { 1763 if (!filename_per_symbol) 1764 printf ("\n%s:\n", filename); 1765 } 1766 1767 static void 1768 print_archive_member_sysv (const char *archive, const char *filename) 1769 { 1770 if (undefined_only) 1771 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename); 1772 else 1773 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename); 1774 if (print_width == 32) 1775 printf (_("\ 1776 Name Value Class Type Size Line Section\n\n")); 1777 else 1778 printf (_("\ 1779 Name Value Class Type Size Line Section\n\n")); 1780 } 1781 1782 static void 1783 print_archive_member_posix (const char *archive, const char *filename) 1784 { 1785 if (!filename_per_symbol) 1786 printf ("%s[%s]:\n", archive, filename); 1787 } 1788 1789 static void 1790 do_not_print_archive_member (const char *archive ATTRIBUTE_UNUSED, 1791 const char *filename ATTRIBUTE_UNUSED) 1792 { 1793 } 1794 1795 1796 /* Print the name of the file (and archive, if there is one) 1798 containing a symbol. */ 1799 1800 static void 1801 print_symbol_filename_bsd (bfd *archive_bfd, bfd *abfd) 1802 { 1803 if (filename_per_symbol) 1804 { 1805 if (archive_bfd) 1806 printf ("%s:", bfd_get_filename (archive_bfd)); 1807 printf ("%s:", bfd_get_filename (abfd)); 1808 } 1809 } 1810 1811 static void 1812 print_symbol_filename_sysv (bfd *archive_bfd, bfd *abfd) 1813 { 1814 if (filename_per_symbol) 1815 { 1816 if (archive_bfd) 1817 printf ("%s:", bfd_get_filename (archive_bfd)); 1818 printf ("%s:", bfd_get_filename (abfd)); 1819 } 1820 } 1821 1822 static void 1823 print_symbol_filename_posix (bfd *archive_bfd, bfd *abfd) 1824 { 1825 if (filename_per_symbol) 1826 { 1827 if (archive_bfd) 1828 printf ("%s[%s]: ", bfd_get_filename (archive_bfd), 1829 bfd_get_filename (abfd)); 1830 else 1831 printf ("%s: ", bfd_get_filename (abfd)); 1832 } 1833 } 1834 1835 static void 1836 do_not_print_symbol_filename (bfd *archive_bfd ATTRIBUTE_UNUSED, 1837 bfd *abfd ATTRIBUTE_UNUSED) 1838 { 1839 } 1840 1841 1842 /* Print a symbol value. */ 1844 1845 static void 1846 print_value (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma val) 1847 { 1848 switch (print_width) 1849 { 1850 case 32: 1851 printf (print_format_string, (uint32_t) val); 1852 break; 1853 1854 case 64: 1855 printf (print_format_string, (uint64_t) val); 1856 break; 1857 1858 default: 1859 fatal (_("Print width has not been initialized (%d)"), print_width); 1860 break; 1861 } 1862 } 1863 1864 /* Print a line of information about a symbol. */ 1865 1866 static void 1867 print_symbol_info_bsd (struct extended_symbol_info *info, bfd *abfd) 1868 { 1869 if (bfd_is_undefined_symclass (SYM_TYPE (info))) 1870 { 1871 if (print_width == 64) 1872 printf (" "); 1873 printf (" "); 1874 } 1875 else 1876 { 1877 /* Normally we print the value of the symbol. If we are printing the 1878 size or sorting by size then we print its size, except for the 1879 (weird) special case where both flags are defined, in which case we 1880 print both values. This conforms to documented behaviour. */ 1881 if (sort_by_size && !print_size) 1882 print_value (abfd, SYM_SIZE (info)); 1883 else 1884 print_value (abfd, SYM_VALUE (info)); 1885 if (print_size && SYM_SIZE (info)) 1886 { 1887 printf (" "); 1888 print_value (abfd, SYM_SIZE (info)); 1889 } 1890 } 1891 1892 printf (" %c", SYM_TYPE (info)); 1893 1894 if (SYM_TYPE (info) == '-') 1895 { 1896 /* A stab. */ 1897 printf (" "); 1898 printf (other_format, SYM_STAB_OTHER (info)); 1899 printf (" "); 1900 printf (desc_format, SYM_STAB_DESC (info)); 1901 printf (" %5s", SYM_STAB_NAME (info)); 1902 } 1903 print_symname (" %s", info, NULL, abfd); 1904 } 1905 1906 static void 1907 print_symbol_info_sysv (struct extended_symbol_info *info, bfd *abfd) 1908 { 1909 print_symname ("%-20s|", info, NULL, abfd); 1910 1911 if (bfd_is_undefined_symclass (SYM_TYPE (info))) 1912 { 1913 if (print_width == 32) 1914 printf (" "); 1915 else 1916 printf (" "); 1917 } 1918 else 1919 print_value (abfd, SYM_VALUE (info)); 1920 1921 printf ("| %c |", SYM_TYPE (info)); 1922 1923 if (SYM_TYPE (info) == '-') 1924 { 1925 /* A stab. */ 1926 printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type. */ 1927 printf (desc_format, SYM_STAB_DESC (info)); /* Size. */ 1928 printf ("| |"); /* Line, Section. */ 1929 } 1930 else 1931 { 1932 /* Type, Size, Line, Section */ 1933 if (info->elfinfo) 1934 printf ("%18s|", 1935 get_elf_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info))); 1936 else if (info->coffinfo) 1937 printf ("%18s|", 1938 get_coff_symbol_type (&info->coffinfo->native->u.syment)); 1939 else 1940 printf (" |"); 1941 1942 if (SYM_SIZE (info)) 1943 print_value (abfd, SYM_SIZE (info)); 1944 else 1945 { 1946 if (print_width == 32) 1947 printf (" "); 1948 else 1949 printf (" "); 1950 } 1951 1952 if (info->elfinfo) 1953 printf("| |%s", info->elfinfo->symbol.section->name); 1954 else if (info->coffinfo) 1955 printf("| |%s", info->coffinfo->symbol.section->name); 1956 else 1957 printf("| |"); 1958 } 1959 } 1960 1961 static void 1962 print_symbol_info_posix (struct extended_symbol_info *info, bfd *abfd) 1963 { 1964 print_symname ("%s ", info, NULL, abfd); 1965 printf ("%c ", SYM_TYPE (info)); 1966 1967 if (bfd_is_undefined_symclass (SYM_TYPE (info))) 1968 printf (" "); 1969 else 1970 { 1971 print_value (abfd, SYM_VALUE (info)); 1972 printf (" "); 1973 if (SYM_SIZE (info)) 1974 print_value (abfd, SYM_SIZE (info)); 1975 } 1976 } 1977 1978 static void 1979 just_print_symbol_name (struct extended_symbol_info *info, bfd *abfd) 1980 { 1981 print_symname ("%s", info, NULL, abfd); 1982 } 1983 1984 int 1986 main (int argc, char **argv) 1987 { 1988 int c; 1989 int retval; 1990 1991 #ifdef HAVE_LC_MESSAGES 1992 setlocale (LC_MESSAGES, ""); 1993 #endif 1994 setlocale (LC_CTYPE, ""); 1995 setlocale (LC_COLLATE, ""); 1996 bindtextdomain (PACKAGE, LOCALEDIR); 1997 textdomain (PACKAGE); 1998 1999 program_name = *argv; 2000 xmalloc_set_program_name (program_name); 2001 bfd_set_error_program_name (program_name); 2002 #if BFD_SUPPORTS_PLUGINS 2003 bfd_plugin_set_program_name (program_name); 2004 #endif 2005 2006 expandargv (&argc, &argv); 2007 2008 if (bfd_init () != BFD_INIT_MAGIC) 2009 fatal (_("fatal error: libbfd ABI mismatch")); 2010 set_default_bfd_target (); 2011 2012 while ((c = getopt_long (argc, argv, "aABCDef:gHhjJlnopPrSst:uUvVvWX:", 2013 long_options, (int *) 0)) != EOF) 2014 { 2015 switch (c) 2016 { 2017 case 'a': 2018 print_debug_syms = 1; 2019 break; 2020 case 'A': 2021 case 'o': 2022 filename_per_symbol = 1; 2023 break; 2024 case 'B': /* For MIPS compatibility. */ 2025 set_output_format ("bsd"); 2026 break; 2027 case 'C': 2028 do_demangle = 1; 2029 if (optarg != NULL) 2030 { 2031 enum demangling_styles style; 2032 2033 style = cplus_demangle_name_to_style (optarg); 2034 if (style == unknown_demangling) 2035 fatal (_("unknown demangling style `%s'"), 2036 optarg); 2037 2038 cplus_demangle_set_style (style); 2039 } 2040 break; 2041 case OPTION_RECURSE_LIMIT: 2042 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT; 2043 break; 2044 case OPTION_NO_RECURSE_LIMIT: 2045 demangle_flags |= DMGL_NO_RECURSE_LIMIT; 2046 break; 2047 case OPTION_QUIET: 2048 quiet = 1; 2049 break; 2050 case 'D': 2051 dynamic = 1; 2052 break; 2053 case 'e': 2054 /* Ignored for HP/UX compatibility. */ 2055 break; 2056 case 'f': 2057 set_output_format (optarg); 2058 break; 2059 case 'g': 2060 external_only = 1; 2061 break; 2062 case 'H': 2063 case 'h': 2064 usage (stdout, 0); 2065 case 'l': 2066 line_numbers = 1; 2067 break; 2068 case 'n': 2069 case 'v': 2070 no_sort = 0; 2071 sort_numerically = 1; 2072 sort_by_size = 0; 2073 break; 2074 case 'p': 2075 no_sort = 1; 2076 sort_numerically = 0; 2077 sort_by_size = 0; 2078 break; 2079 case OPTION_SIZE_SORT: 2080 no_sort = 0; 2081 sort_numerically = 0; 2082 sort_by_size = 1; 2083 break; 2084 case 'P': 2085 set_output_format ("posix"); 2086 break; 2087 case 'j': 2088 set_output_format ("just-symbols"); 2089 break; 2090 case 'r': 2091 reverse_sort = 1; 2092 break; 2093 case 's': 2094 print_armap = 1; 2095 break; 2096 case 'S': 2097 print_size = 1; 2098 break; 2099 case 't': 2100 set_print_radix (optarg); 2101 break; 2102 case 'u': 2103 undefined_only = 1; 2104 defined_only = 0; 2105 break; 2106 case 'U': 2107 defined_only = 1; 2108 undefined_only = 0; 2109 break; 2110 2111 case OPTION_UNICODE: 2112 if (streq (optarg, "default") || streq (optarg, "d")) 2113 unicode_display = unicode_default; 2114 else if (streq (optarg, "locale") || streq (optarg, "l")) 2115 unicode_display = unicode_locale; 2116 else if (streq (optarg, "escape") || streq (optarg, "e")) 2117 unicode_display = unicode_escape; 2118 else if (streq (optarg, "invalid") || streq (optarg, "i")) 2119 unicode_display = unicode_invalid; 2120 else if (streq (optarg, "hex") || streq (optarg, "x")) 2121 unicode_display = unicode_hex; 2122 else if (streq (optarg, "highlight") || streq (optarg, "h")) 2123 unicode_display = unicode_highlight; 2124 else 2125 fatal (_("invalid argument to -U/--unicode: %s"), optarg); 2126 break; 2127 2128 case 'V': 2129 show_version = 1; 2130 break; 2131 case 'W': 2132 non_weak = 1; 2133 break; 2134 case 'X': 2135 /* Ignored for (partial) AIX compatibility. On AIX, the 2136 argument has values 32, 64, or 32_64, and specifies that 2137 only 32-bit, only 64-bit, or both kinds of objects should 2138 be examined. The default is 32. So plain AIX nm on a 2139 library archive with both kinds of objects will ignore 2140 the 64-bit ones. For GNU nm, the default is and always 2141 has been -X 32_64, and other options are not supported. */ 2142 if (strcmp (optarg, "32_64") != 0) 2143 fatal (_("Only -X 32_64 is supported")); 2144 break; 2145 2146 case OPTION_TARGET: /* --target */ 2147 target = optarg; 2148 break; 2149 2150 case OPTION_PLUGIN: /* --plugin */ 2151 #if BFD_SUPPORTS_PLUGINS 2152 bfd_plugin_set_plugin (optarg); 2153 #else 2154 fatal (_("sorry - this program has been built without plugin support\n")); 2155 #endif 2156 break; 2157 2158 case OPTION_IFUNC_CHARS: 2159 ifunc_type_chars = optarg; 2160 break; 2161 2162 case 0: /* A long option that just sets a flag. */ 2163 break; 2164 2165 default: 2166 usage (stderr, 1); 2167 } 2168 } 2169 2170 if (show_version) 2171 print_version ("nm"); 2172 2173 if (sort_by_size && undefined_only) 2174 { 2175 non_fatal (_("Using the --size-sort and --undefined-only options together")); 2176 non_fatal (_("will produce no output, since undefined symbols have no size.")); 2177 return 0; 2178 } 2179 2180 /* OK, all options now parsed. If no filename specified, do a.out. */ 2181 if (optind == argc) 2182 return !display_file ("a.out"); 2183 2184 retval = 0; 2185 2186 if (argc - optind > 1) 2187 filename_per_file = 1; 2188 2189 /* We were given several filenames to do. */ 2190 while (optind < argc) 2191 { 2192 if (!display_file (argv[optind++])) 2193 retval++; 2194 } 2195 2196 exit (retval); 2197 return retval; 2198 } 2199