Coverage Report

Created: 2026-04-04 08:16

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