blob: be91459c2ec4f57e03645b8ea2fa71ca7958989a [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Print symbol information from ELF file in human-readable form.
Mark Wielaardf48eb6b2014-01-23 00:56:41 +01002 Copyright (C) 2000-2008, 2009, 2011, 2012, 2014 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000010
Mark Wielaardde2ed972012-06-05 17:15:16 +020011 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000012 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000015
Mark Wielaardde2ed972012-06-05 17:15:16 +020016 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000018
19#ifdef HAVE_CONFIG_H
20# include <config.h>
21#endif
22
23#include <ar.h>
24#include <argp.h>
25#include <assert.h>
26#include <ctype.h>
27#include <dwarf.h>
28#include <errno.h>
29#include <error.h>
30#include <fcntl.h>
31#include <gelf.h>
32#include <inttypes.h>
33#include <libdw.h>
34#include <libintl.h>
35#include <locale.h>
36#include <mcheck.h>
37#include <obstack.h>
38#include <search.h>
39#include <stdbool.h>
40#include <stdio.h>
41#include <stdio_ext.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45#include <sys/param.h>
46
47#include <system.h>
48#include "../libebl/libeblP.h"
49
50
51/* Name and version of program. */
52static void print_version (FILE *stream, struct argp_state *state);
Ulrich Drepperfdc93e12009-01-17 11:47:10 -080053ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000054
55/* Bug report address. */
Ulrich Drepperfdc93e12009-01-17 11:47:10 -080056ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000057
58
59/* Values for the parameters which have no short form. */
Ulrich Drepper2356ba02011-10-03 07:23:07 -040060#define OPT_DEFINED 0x100
61#define OPT_MARK_SPECIAL 0x101
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000062
63/* Definitions of arguments for argp functions. */
64static const struct argp_option options[] =
65{
66 { NULL, 0, NULL, 0, N_("Output selection:"), 0 },
67 { "debug-syms", 'a', NULL, 0, N_("Display debugger-only symbols"), 0 },
68 { "defined-only", OPT_DEFINED, NULL, 0, N_("Display only defined symbols"),
69 0 },
70 { "dynamic", 'D', NULL, 0,
71 N_("Display dynamic symbols instead of normal symbols"), 0 },
72 { "extern-only", 'g', NULL, 0, N_("Display only external symbols"), 0 },
73 { "undefined-only", 'u', NULL, 0, N_("Display only undefined symbols"), 0 },
74 { "print-armap", 's', NULL, 0,
75 N_("Include index for symbols from archive members"), 0 },
76
77 { NULL, 0, NULL, 0, N_("Output format:"), 0 },
78 { "print-file-name", 'A', NULL, 0,
79 N_("Print name of the input file before every symbol"), 0 },
80 { NULL, 'o', NULL, OPTION_HIDDEN, "Same as -A", 0 },
81 { "format", 'f', "FORMAT", 0,
82 N_("Use the output format FORMAT. FORMAT can be `bsd', `sysv' or `posix'. The default is `sysv'"),
83 0 },
84 { NULL, 'B', NULL, 0, N_("Same as --format=bsd"), 0 },
85 { "portability", 'P', NULL, 0, N_("Same as --format=posix"), 0 },
86 { "radix", 't', "RADIX", 0, N_("Use RADIX for printing symbol values"), 0 },
Ulrich Drepper2356ba02011-10-03 07:23:07 -040087 { "mark-special", OPT_MARK_SPECIAL, NULL, 0, N_("Mark special symbols"), 0 },
88 { "mark-weak", OPT_MARK_SPECIAL, NULL, OPTION_HIDDEN, "", 0 },
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000089 { "print-size", 'S', NULL, 0, N_("Print size of defined symbols"), 0 },
90
91 { NULL, 0, NULL, 0, N_("Output options:"), 0 },
92 { "numeric-sort", 'n', NULL, 0, N_("Sort symbols numerically by address"),
93 0 },
94 { "no-sort", 'p', NULL, 0, N_("Do not sort the symbols"), 0 },
95 { "reverse-sort", 'r', NULL, 0, N_("Reverse the sense of the sort"), 0 },
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -040096#ifdef USE_DEMANGLE
97 { "demangle", 'C', NULL, 0,
98 N_("Decode low-level symbol names into source code names"), 0 },
99#endif
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000100 { NULL, 0, NULL, 0, N_("Miscellaneous:"), 0 },
101 { NULL, 0, NULL, 0, NULL, 0 }
102};
103
104/* Short description of program. */
105static const char doc[] = N_("List symbols from FILEs (a.out by default).");
106
107/* Strings for arguments in help texts. */
108static const char args_doc[] = N_("[FILE...]");
109
110/* Prototype for option handler. */
111static error_t parse_opt (int key, char *arg, struct argp_state *state);
112
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500113/* Parser children. */
114static struct argp_child argp_children[] =
115 {
116 { &color_argp, 0, N_("Output formatting"), 2 },
117 { NULL, 0, NULL, 0}
118 };
119
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000120/* Data structure to communicate with argp functions. */
121static struct argp argp =
122{
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500123 options, parse_opt, args_doc, doc, argp_children, NULL, NULL
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000124};
125
126
127/* Print symbols in file named FNAME. */
128static int process_file (const char *fname, bool more_than_one);
129
130/* Handle content of archive. */
131static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
132 const char *suffix);
133
134/* Handle ELF file. */
135static int handle_elf (Elf *elf, const char *prefix, const char *fname,
136 const char *suffix);
137
138
139#define INTERNAL_ERROR(fname) \
140 error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s-%s): %s"), \
Ulrich Drepperb0243862007-06-06 00:09:36 +0000141 fname, __LINE__, PACKAGE_VERSION, __DATE__, elf_errmsg (-1))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000142
143
144/* Internal representation of symbols. */
145typedef struct GElf_SymX
146{
147 GElf_Sym sym;
148 Elf32_Word xndx;
149 char *where;
150} GElf_SymX;
151
152
153/* User-selectable options. */
154
155/* The selected output format. */
156static enum
157{
158 format_sysv = 0,
159 format_bsd,
160 format_posix
161} format;
162
163/* Print defined, undefined, or both? */
164static bool hide_undefined;
165static bool hide_defined;
166
167/* Print local symbols also? */
168static bool hide_local;
169
170/* Nonzero if full filename should precede every symbol. */
171static bool print_file_name;
172
173/* If true print size of defined symbols in BSD format. */
174static bool print_size;
175
176/* If true print archive index. */
177static bool print_armap;
178
179/* If true reverse sorting. */
180static bool reverse_sort;
181
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400182#ifdef USE_DEMANGLE
183/* If true demangle symbols. */
184static bool demangle;
185#endif
186
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000187/* Type of the section we are printing. */
188static GElf_Word symsec_type = SHT_SYMTAB;
189
190/* Sorting selection. */
191static enum
192{
193 sort_name = 0,
194 sort_numeric,
195 sort_nosort
196} sort;
197
198/* Radix for printed numbers. */
199static enum
200{
201 radix_hex = 0,
202 radix_decimal,
203 radix_octal
204} radix;
205
Ulrich Drepper2356ba02011-10-03 07:23:07 -0400206/* If nonzero mark special symbols:
207 - weak symbols are distinguished from global symbols by adding
208 a `*' after the identifying letter for the symbol class and type.
209 - TLS symbols are distinguished from normal symbols by adding
210 a '@' after the identifying letter for the symbol class and type. */
211static bool mark_special;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000212
213
214int
215main (int argc, char *argv[])
216{
217 int remaining;
218 int result = 0;
219
220 /* Make memory leak detection possible. */
221 mtrace ();
222
223 /* We use no threads here which can interfere with handling a stream. */
224 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
225 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
226 (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER);
227
228 /* Set locale. */
229 (void) setlocale (LC_ALL, "");
230
231 /* Make sure the message catalog can be found. */
Ulrich Drepperb0243862007-06-06 00:09:36 +0000232 (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000233
234 /* Initialize the message catalog. */
Ulrich Drepperb0243862007-06-06 00:09:36 +0000235 (void) textdomain (PACKAGE_TARNAME);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000236
237 /* Parse and process arguments. */
238 (void) argp_parse (&argp, argc, argv, 0, &remaining, NULL);
239
240 /* Tell the library which version we are expecting. */
241 (void) elf_version (EV_CURRENT);
242
243 if (remaining == argc)
244 /* The user didn't specify a name so we use a.out. */
245 result = process_file ("a.out", false);
246 else
247 {
248 /* Process all the remaining files. */
249 const bool more_than_one = remaining + 1 < argc;
250
251 do
252 result |= process_file (argv[remaining], more_than_one);
253 while (++remaining < argc);
254 }
255
256 return result;
257}
258
259
260/* Print the version information. */
261static void
262print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
263{
Ulrich Drepperb0243862007-06-06 00:09:36 +0000264 fprintf (stream, "nm (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000265 fprintf (stream, gettext ("\
266Copyright (C) %s Red Hat, Inc.\n\
267This is free software; see the source for copying conditions. There is NO\n\
268warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
Ulrich Drepper3a64a302012-01-21 18:19:24 -0500269"), "2012");
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000270 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
271}
272
273
274/* Handle program arguments. */
275static error_t
276parse_opt (int key, char *arg,
277 struct argp_state *state __attribute__ ((unused)))
278{
279 switch (key)
280 {
281 case 'a':
282 /* XXX */
283 break;
284
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400285#ifdef USE_DEMANGLE
286 case 'C':
287 demangle = true;
288 break;
289#endif
290
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000291 case 'f':
292 if (strcmp (arg, "bsd") == 0)
293 format = format_bsd;
294 else if (strcmp (arg, "posix") == 0)
295 format = format_posix;
296 else
297 /* Be bug compatible. The BFD implementation also defaulted to
298 using the SysV format if nothing else matches. */
299 format = format_sysv;
300 break;
301
302 case 'g':
303 hide_local = true;
304 break;
305
306 case 'n':
307 sort = sort_numeric;
308 break;
309
310 case 'p':
311 sort = sort_nosort;
312 break;
313
314 case 't':
315 if (strcmp (arg, "10") == 0 || strcmp (arg, "d") == 0)
316 radix = radix_decimal;
317 else if (strcmp (arg, "8") == 0 || strcmp (arg, "o") == 0)
318 radix = radix_octal;
319 else
320 radix = radix_hex;
321 break;
322
323 case 'u':
324 hide_undefined = false;
325 hide_defined = true;
326 break;
327
328 case 'A':
329 case 'o':
330 print_file_name = true;
331 break;
332
333 case 'B':
334 format = format_bsd;
335 break;
336
337 case 'D':
338 symsec_type = SHT_DYNSYM;
339 break;
340
341 case 'P':
342 format = format_posix;
343 break;
344
345 case OPT_DEFINED:
346 hide_undefined = true;
347 hide_defined = false;
348 break;
349
Ulrich Drepper2356ba02011-10-03 07:23:07 -0400350 case OPT_MARK_SPECIAL:
351 mark_special = true;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000352 break;
353
354 case 'S':
355 print_size = true;
356 break;
357
358 case 's':
359 print_armap = true;
360 break;
361
362 case 'r':
363 reverse_sort = true;
364 break;
365
366 default:
367 return ARGP_ERR_UNKNOWN;
368 }
369 return 0;
370}
371
372
373/* Open the file and determine the type. */
374static int
375process_file (const char *fname, bool more_than_one)
376{
377 /* Open the file. */
378 int fd = open (fname, O_RDONLY);
379 if (fd == -1)
380 {
381 error (0, errno, gettext ("cannot open '%s'"), fname);
382 return 1;
383 }
384
385 /* Now get the ELF descriptor. */
386 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
387 if (elf != NULL)
388 {
389 if (elf_kind (elf) == ELF_K_ELF)
390 {
391 int result = handle_elf (elf, more_than_one ? "" : NULL,
392 fname, NULL);
393
394 if (elf_end (elf) != 0)
395 INTERNAL_ERROR (fname);
396
397 if (close (fd) != 0)
Ulrich Drepperd112ef82005-09-03 21:31:27 +0000398 error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000399
400 return result;
401 }
402 else if (elf_kind (elf) == ELF_K_AR)
403 {
404 int result = handle_ar (fd, elf, NULL, fname, NULL);
405
406 if (elf_end (elf) != 0)
407 INTERNAL_ERROR (fname);
408
409 if (close (fd) != 0)
Ulrich Drepperd112ef82005-09-03 21:31:27 +0000410 error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000411
412 return result;
413 }
414
415 /* We cannot handle this type. Close the descriptor anyway. */
416 if (elf_end (elf) != 0)
417 INTERNAL_ERROR (fname);
418 }
419
420 error (0, 0, gettext ("%s: File format not recognized"), fname);
421
422 return 1;
423}
424
425
426static int
427handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
428 const char *suffix)
429{
430 size_t fname_len = strlen (fname) + 1;
431 size_t prefix_len = prefix != NULL ? strlen (prefix) : 0;
432 char new_prefix[prefix_len + fname_len + 2];
433 size_t suffix_len = suffix != NULL ? strlen (suffix) : 0;
434 char new_suffix[suffix_len + 2];
435 Elf *subelf;
436 Elf_Cmd cmd = ELF_C_READ_MMAP;
437 int result = 0;
438
439 char *cp = new_prefix;
440 if (prefix != NULL)
441 cp = stpcpy (cp, prefix);
442 cp = stpcpy (cp, fname);
443 stpcpy (cp, "[");
444
445 cp = new_suffix;
446 if (suffix != NULL)
447 cp = stpcpy (cp, suffix);
448 stpcpy (cp, "]");
449
450 /* First print the archive index if this is wanted. */
451 if (print_armap)
452 {
453 Elf_Arsym *arsym = elf_getarsym (elf, NULL);
454
455 if (arsym != NULL)
456 {
457 Elf_Arhdr *arhdr = NULL;
458 size_t arhdr_off = 0; /* Note: 0 is no valid offset. */
459
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500460 fputs_unlocked (gettext("\nArchive index:\n"), stdout);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000461
462 while (arsym->as_off != 0)
463 {
464 if (arhdr_off != arsym->as_off
465 && (elf_rand (elf, arsym->as_off) != arsym->as_off
466 || (subelf = elf_begin (fd, cmd, elf)) == NULL
467 || (arhdr = elf_getarhdr (subelf)) == NULL))
468 {
469 error (0, 0, gettext ("invalid offset %zu for symbol %s"),
470 arsym->as_off, arsym->as_name);
Mark Wielaard0b799ad2014-12-26 16:12:52 +0100471 break;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000472 }
473
474 printf (gettext ("%s in %s\n"), arsym->as_name, arhdr->ar_name);
475
476 ++arsym;
477 }
478
479 if (elf_rand (elf, SARMAG) != SARMAG)
480 {
481 error (0, 0,
482 gettext ("cannot reset archive offset to beginning"));
483 return 1;
484 }
485 }
486 }
487
488 /* Process all the files contained in the archive. */
489 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
490 {
491 /* The the header for this element. */
492 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
493
494 /* Skip over the index entries. */
495 if (strcmp (arhdr->ar_name, "/") != 0
Mark Wielaard8fb260f2014-12-26 16:20:39 +0100496 && strcmp (arhdr->ar_name, "//") != 0
497 && strcmp (arhdr->ar_name, "/SYM64/") != 0)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000498 {
499 if (elf_kind (subelf) == ELF_K_ELF)
500 result |= handle_elf (subelf, new_prefix, arhdr->ar_name,
501 new_suffix);
502 else if (elf_kind (subelf) == ELF_K_AR)
503 result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name,
504 new_suffix);
505 else
506 {
507 error (0, 0, gettext ("%s%s%s: file format not recognized"),
508 new_prefix, arhdr->ar_name, new_suffix);
509 result = 1;
510 }
511 }
512
513 /* Get next archive element. */
514 cmd = elf_next (subelf);
515 if (elf_end (subelf) != 0)
516 INTERNAL_ERROR (fname);
517 }
518
519 return result;
520}
521
522
523/* Mapping of radix and binary class to length. */
524static const int length_map[2][3] =
525{
526 [ELFCLASS32 - 1] =
527 {
528 [radix_hex] = 8,
529 [radix_decimal] = 10,
530 [radix_octal] = 11
531 },
532 [ELFCLASS64 - 1] =
533 {
534 [radix_hex] = 16,
535 [radix_decimal] = 20,
536 [radix_octal] = 22
537 }
538};
539
540
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000541static int
542global_compare (const void *p1, const void *p2)
543{
544 const Dwarf_Global *g1 = (const Dwarf_Global *) p1;
545 const Dwarf_Global *g2 = (const Dwarf_Global *) p2;
546
547 return strcmp (g1->name, g2->name);
548}
549
550
551static void *global_root;
552
553
554static int
555get_global (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
556 void *arg __attribute__ ((unused)))
557{
558 tsearch (memcpy (xmalloc (sizeof (Dwarf_Global)), global,
559 sizeof (Dwarf_Global)),
560 &global_root, global_compare);
561
562 return DWARF_CB_OK;
563}
564
565
566struct local_name
567{
568 const char *name;
569 const char *file;
570 Dwarf_Word lineno;
571 Dwarf_Addr lowpc;
572 Dwarf_Addr highpc;
573};
574
575
576static int
577local_compare (const void *p1, const void *p2)
578{
579 struct local_name *g1 = (struct local_name *) p1;
580 struct local_name *g2 = (struct local_name *) p2;
581 int result;
582
583 result = strcmp (g1->name, g2->name);
584 if (result == 0)
585 {
586 if (g1->lowpc <= g2->lowpc && g1->highpc >= g2->highpc)
587 {
588 /* g2 is contained in g1. Update the data. */
589 g2->lowpc = g1->lowpc;
590 g2->highpc = g1->highpc;
591 result = 0;
592 }
593 else if (g2->lowpc <= g1->lowpc && g2->highpc >= g1->highpc)
594 {
595 /* g1 is contained in g2. Update the data. */
596 g1->lowpc = g2->lowpc;
597 g1->highpc = g2->highpc;
598 result = 0;
599 }
600 else
601 result = g1->lowpc < g2->lowpc ? -1 : 1;
602 }
603
604 return result;
605}
606
607
608static int
609get_var_range (Dwarf_Die *die, Dwarf_Word *lowpc, Dwarf_Word *highpc)
610{
611 Dwarf_Attribute locattr_mem;
612 Dwarf_Attribute *locattr = dwarf_attr (die, DW_AT_location, &locattr_mem);
613 if (locattr == NULL)
614 return 1;
615
Roland McGrath6724c902005-10-28 07:07:19 +0000616 Dwarf_Op *loc;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000617 size_t nloc;
Roland McGrath6724c902005-10-28 07:07:19 +0000618 if (dwarf_getlocation (locattr, &loc, &nloc) != 0)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000619 return 1;
620
621 /* Interpret the location expressions. */
622 // XXX For now just the simple one:
623 if (nloc == 1 && loc[0].atom == DW_OP_addr)
624 {
625 *lowpc = *highpc = loc[0].number;
626 return 0;
627 }
628
629 return 1;
630}
631
632
633
634static void *local_root;
635
636
637static void
638get_local_names (Dwarf *dbg)
639{
640 Dwarf_Off offset = 0;
641 Dwarf_Off old_offset;
642 size_t hsize;
643
644 while (dwarf_nextcu (dbg, old_offset = offset, &offset, &hsize, NULL, NULL,
645 NULL) == 0)
646 {
647 Dwarf_Die cudie_mem;
648 Dwarf_Die *cudie = dwarf_offdie (dbg, old_offset + hsize, &cudie_mem);
649
650 /* If we cannot get the CU DIE there is no need to go on with
651 this CU. */
652 if (cudie == NULL)
653 continue;
654 /* This better be a CU DIE. */
655 if (dwarf_tag (cudie) != DW_TAG_compile_unit)
656 continue;
657
658 /* Get the line information. */
659 Dwarf_Files *files;
660 size_t nfiles;
661 if (dwarf_getsrcfiles (cudie, &files, &nfiles) != 0)
662 continue;
663
664 Dwarf_Die die_mem;
665 Dwarf_Die *die = &die_mem;
666 if (dwarf_child (cudie, die) == 0)
667 /* Iterate over all immediate children of the CU DIE. */
668 do
669 {
670 int tag = dwarf_tag (die);
671 if (tag != DW_TAG_subprogram && tag != DW_TAG_variable)
672 continue;
673
674 /* We are interested in five attributes: name, decl_file,
675 decl_line, low_pc, and high_pc. */
676 Dwarf_Attribute attr_mem;
677 Dwarf_Attribute *attr = dwarf_attr (die, DW_AT_name, &attr_mem);
678 const char *name = dwarf_formstring (attr);
679 if (name == NULL)
680 continue;
681
682 Dwarf_Word fileidx;
683 attr = dwarf_attr (die, DW_AT_decl_file, &attr_mem);
684 if (dwarf_formudata (attr, &fileidx) != 0 || fileidx >= nfiles)
685 continue;
686
687 Dwarf_Word lineno;
688 attr = dwarf_attr (die, DW_AT_decl_line, &attr_mem);
689 if (dwarf_formudata (attr, &lineno) != 0 || lineno == 0)
690 continue;
691
692 Dwarf_Addr lowpc;
693 Dwarf_Addr highpc;
694 if (tag == DW_TAG_subprogram)
695 {
696 if (dwarf_lowpc (die, &lowpc) != 0
697 || dwarf_highpc (die, &highpc) != 0)
698 continue;
699 }
700 else
701 {
702 if (get_var_range (die, &lowpc, &highpc) != 0)
703 continue;
704 }
705
706 /* We have all the information. Create a record. */
707 struct local_name *newp
708 = (struct local_name *) xmalloc (sizeof (*newp));
709 newp->name = name;
710 newp->file = dwarf_filesrc (files, fileidx, NULL, NULL);
711 newp->lineno = lineno;
712 newp->lowpc = lowpc;
713 newp->highpc = highpc;
714
715 /* Since we cannot deallocate individual memory we do not test
716 for duplicates in the tree. This should not happen anyway. */
717 if (tsearch (newp, &local_root, local_compare) == NULL)
718 error (EXIT_FAILURE, errno,
719 gettext ("cannot create search tree"));
720 }
721 while (dwarf_siblingof (die, die) == 0);
722 }
723}
724
Roland McGrath468fe4d2008-12-11 21:00:12 -0800725/* Do elf_strptr, but return a backup string and never NULL. */
726static const char *
727sym_name (Elf *elf, GElf_Word strndx, GElf_Word st_name, char buf[], size_t n)
728{
729 const char *symstr = elf_strptr (elf, strndx, st_name);
730 if (symstr == NULL)
731 {
732 snprintf (buf, n, "[invalid st_name %#" PRIx32 "]", st_name);
733 symstr = buf;
734 }
735 return symstr;
736}
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000737
738/* Show symbols in SysV format. */
739static void
Marek Polacekc8920de2011-05-12 12:08:21 +0200740show_symbols_sysv (Ebl *ebl, GElf_Word strndx, const char *fullname,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000741 GElf_SymX *syms, size_t nsyms, int longest_name,
742 int longest_where)
743{
744 size_t shnum;
Ulrich Drepperf1894932009-06-13 15:55:42 -0700745 if (elf_getshdrnum (ebl->elf, &shnum) < 0)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000746 INTERNAL_ERROR (fullname);
747
748 bool scnnames_malloced = shnum * sizeof (const char *) > 128 * 1024;
749 const char **scnnames;
750 if (scnnames_malloced)
751 scnnames = (const char **) xmalloc (sizeof (const char *) * shnum);
752 else
753 scnnames = (const char **) alloca (sizeof (const char *) * shnum);
754 /* Get the section header string table index. */
755 size_t shstrndx;
Ulrich Drepperf1894932009-06-13 15:55:42 -0700756 if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000757 error (EXIT_FAILURE, 0,
758 gettext ("cannot get section header string table index"));
759
760 /* Cache the section names. */
761 Elf_Scn *scn = NULL;
762 size_t cnt = 1;
763 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
764 {
765 GElf_Shdr shdr_mem;
766
Mark Wielaard03d76f42013-11-09 16:45:22 +0100767 assert (elf_ndxscn (scn) == cnt);
768 cnt++;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000769
Roland McGrath468fe4d2008-12-11 21:00:12 -0800770 char *name = elf_strptr (ebl->elf, shstrndx,
771 gelf_getshdr (scn, &shdr_mem)->sh_name);
772 if (unlikely (name == NULL))
773 {
Mark Wielaard7df3d2c2012-12-11 22:27:05 +0100774 const size_t bufsz = sizeof "[invalid sh_name 0x12345678]";
Roland McGrath57bd66c2012-12-11 09:42:07 -0800775 name = alloca (bufsz);
776 snprintf (name, bufsz, "[invalid sh_name %#" PRIx32 "]",
Roland McGrath468fe4d2008-12-11 21:00:12 -0800777 gelf_getshdr (scn, &shdr_mem)->sh_name);
778 }
779 scnnames[elf_ndxscn (scn)] = name;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000780 }
781
782 int digits = length_map[gelf_getclass (ebl->elf) - 1][radix];
783
784 /* We always print this prolog. */
Marek Polacekc8920de2011-05-12 12:08:21 +0200785 printf (gettext ("\n\nSymbols from %s:\n\n"), fullname);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000786
787 /* The header line. */
788 printf (gettext ("%*s%-*s %-*s Class Type %-*s %*s Section\n\n"),
789 print_file_name ? (int) strlen (fullname) + 1: 0, "",
790 longest_name, sgettext ("sysv|Name"),
791 /* TRANS: the "sysv|" parts makes the string unique. */
792 digits, sgettext ("sysv|Value"),
793 /* TRANS: the "sysv|" parts makes the string unique. */
794 digits, sgettext ("sysv|Size"),
795 /* TRANS: the "sysv|" parts makes the string unique. */
796 longest_where, sgettext ("sysv|Line"));
797
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400798#ifdef USE_DEMANGLE
799 size_t demangle_buffer_len = 0;
800 char *demangle_buffer = NULL;
801#endif
802
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000803 /* Iterate over all symbols. */
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400804 for (cnt = 1; cnt < nsyms; ++cnt)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000805 {
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400806 /* In this format SECTION entries are not printed. */
807 if (GELF_ST_TYPE (syms[cnt].sym.st_info) == STT_SECTION)
808 continue;
809
Roland McGrath468fe4d2008-12-11 21:00:12 -0800810 char symstrbuf[50];
811 const char *symstr = sym_name (ebl->elf, strndx, syms[cnt].sym.st_name,
812 symstrbuf, sizeof symstrbuf);
813
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400814#ifdef USE_DEMANGLE
Jan Kratochvil7c6e7852014-01-15 21:16:57 +0100815 /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */
816 if (demangle && symstr[0] == '_' && symstr[1] == 'Z')
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400817 {
818 int status = -1;
819 char *dmsymstr = __cxa_demangle (symstr, demangle_buffer,
820 &demangle_buffer_len, &status);
821
822 if (status == 0)
823 symstr = dmsymstr;
824 }
825#endif
826
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000827 char symbindbuf[50];
828 char symtypebuf[50];
829 char secnamebuf[1024];
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400830 char addressbuf[(64 + 2) / 3 + 1];
831 char sizebuf[(64 + 2) / 3 + 1];
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000832
833 /* If we have to precede the line with the file name. */
834 if (print_file_name)
835 {
836 fputs_unlocked (fullname, stdout);
837 putchar_unlocked (':');
838 }
839
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400840 /* Covert the address. */
841 if (syms[cnt].sym.st_shndx == SHN_UNDEF)
842 addressbuf[0] = sizebuf[0] = '\0';
843 else
844 {
Mark Wielaardf48eb6b2014-01-23 00:56:41 +0100845 snprintf (addressbuf, sizeof (addressbuf),
846 (radix == radix_hex ? "%0*" PRIx64
847 : (radix == radix_decimal ? "%0*" PRId64
848 : "%0*" PRIo64)),
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400849 digits, syms[cnt].sym.st_value);
Mark Wielaardf48eb6b2014-01-23 00:56:41 +0100850 snprintf (sizebuf, sizeof (sizebuf),
851 (radix == radix_hex ? "%0*" PRIx64
852 : (radix == radix_decimal ? "%0*" PRId64
853 : "%0*" PRIo64)),
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400854 digits, syms[cnt].sym.st_size);
855 }
856
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000857 /* Print the actual string. */
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400858 printf ("%-*s|%s|%-6s|%-8s|%s|%*s|%s\n",
859 longest_name, symstr, addressbuf,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000860 ebl_symbol_binding_name (ebl,
861 GELF_ST_BIND (syms[cnt].sym.st_info),
862 symbindbuf, sizeof (symbindbuf)),
863 ebl_symbol_type_name (ebl, GELF_ST_TYPE (syms[cnt].sym.st_info),
864 symtypebuf, sizeof (symtypebuf)),
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400865 sizebuf, longest_where, syms[cnt].where,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000866 ebl_section_name (ebl, syms[cnt].sym.st_shndx, syms[cnt].xndx,
867 secnamebuf, sizeof (secnamebuf), scnnames,
868 shnum));
869 }
870
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400871#ifdef USE_DEMANGLE
872 free (demangle_buffer);
873#endif
874
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000875 if (scnnames_malloced)
876 free (scnnames);
877}
878
879
880static char
Ulrich Drepper2356ba02011-10-03 07:23:07 -0400881class_type_char (Elf *elf, const GElf_Ehdr *ehdr, GElf_Sym *sym)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000882{
883 int local_p = GELF_ST_BIND (sym->st_info) == STB_LOCAL;
884
885 /* XXX Add support for architecture specific types and classes. */
886 if (sym->st_shndx == SHN_ABS)
887 return local_p ? 'a' : 'A';
888
889 if (sym->st_shndx == SHN_UNDEF)
890 /* Undefined symbols must be global. */
891 return 'U';
892
Ulrich Drepper2356ba02011-10-03 07:23:07 -0400893 char result = "NDTSFBD "[GELF_ST_TYPE (sym->st_info)];
894
895 if (result == 'D')
896 {
897 /* Special handling: unique data symbols. */
898 if (ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX
899 && GELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
900 result = 'u';
901 else
902 {
903 GElf_Shdr shdr_mem;
904 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (elf, sym->st_shndx),
905 &shdr_mem);
906 if (shdr != NULL)
907 {
908 if ((shdr->sh_flags & SHF_WRITE) == 0)
909 result = 'R';
910 else if (shdr->sh_type == SHT_NOBITS)
911 result = 'B';
912 }
913 }
914 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000915
916 return local_p ? tolower (result) : result;
917}
918
919
920static void
Ulrich Drepper2356ba02011-10-03 07:23:07 -0400921show_symbols_bsd (Elf *elf, const GElf_Ehdr *ehdr, GElf_Word strndx,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000922 const char *prefix, const char *fname, const char *fullname,
923 GElf_SymX *syms, size_t nsyms)
924{
925 int digits = length_map[gelf_getclass (elf) - 1][radix];
926
927 if (prefix != NULL && ! print_file_name)
928 printf ("\n%s:\n", fname);
929
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400930#ifdef USE_DEMANGLE
931 size_t demangle_buffer_len = 0;
932 char *demangle_buffer = NULL;
933#endif
934
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000935 /* Iterate over all symbols. */
936 for (size_t cnt = 0; cnt < nsyms; ++cnt)
937 {
Roland McGrath468fe4d2008-12-11 21:00:12 -0800938 char symstrbuf[50];
939 const char *symstr = sym_name (elf, strndx, syms[cnt].sym.st_name,
940 symstrbuf, sizeof symstrbuf);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000941
942 /* Printing entries with a zero-length name makes the output
943 not very well parseable. Since these entries don't carry
944 much information we leave them out. */
945 if (symstr[0] == '\0')
946 continue;
947
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500948 /* We do not print the entries for files. */
949 if (GELF_ST_TYPE (syms[cnt].sym.st_info) == STT_FILE)
950 continue;
951
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400952#ifdef USE_DEMANGLE
Jan Kratochvil7c6e7852014-01-15 21:16:57 +0100953 /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */
954 if (demangle && symstr[0] == '_' && symstr[1] == 'Z')
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400955 {
956 int status = -1;
957 char *dmsymstr = __cxa_demangle (symstr, demangle_buffer,
958 &demangle_buffer_len, &status);
959
960 if (status == 0)
961 symstr = dmsymstr;
962 }
963#endif
964
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000965 /* If we have to precede the line with the file name. */
966 if (print_file_name)
967 {
968 fputs_unlocked (fullname, stdout);
969 putchar_unlocked (':');
970 }
971
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500972 bool is_tls = GELF_ST_TYPE (syms[cnt].sym.st_info) == STT_TLS;
973 bool is_weak = GELF_ST_BIND (syms[cnt].sym.st_info) == STB_WEAK;
974 const char *marker = (mark_special
975 ? (is_tls ? "@" : (is_weak ? "*" : " ")) : "");
976
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000977 if (syms[cnt].sym.st_shndx == SHN_UNDEF)
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500978 {
979 const char *color = "";
980 if (color_mode)
981 {
982 if (is_tls)
983 color = color_undef_tls;
984 else if (is_weak)
985 color = color_undef_weak;
986 else
987 color = color_undef;
988 }
989
990 printf ("%*s %sU%s %s", digits, "", color, marker, symstr);
991 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000992 else
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500993 {
994 const char *color = "";
995 if (color_mode)
996 {
997 if (is_tls)
998 color = color_tls;
999 else if (is_weak)
1000 color = color_weak;
1001 else
1002 color = color_symbol;
1003 }
Mark Wielaardf48eb6b2014-01-23 00:56:41 +01001004 if (print_size && syms[cnt].sym.st_size != 0)
1005 {
1006#define HEXFMT "%6$s%2$0*1$" PRIx64 "%8$s %10$0*9$" PRIx64 " %7$s%3$c%4$s %5$s"
1007#define DECFMT "%6$s%2$*1$" PRId64 "%8$s %10$*9$" PRId64 " %7$s%3$c%4$s %5$s"
1008#define OCTFMT "%6$s%2$0*1$" PRIo64 "%8$s %10$0*9$" PRIo64 " %7$s%3$c%4$s %5$s"
1009 printf ((radix == radix_hex ? HEXFMT
1010 : (radix == radix_decimal ? DECFMT : OCTFMT)),
1011 digits, syms[cnt].sym.st_value,
1012 class_type_char (elf, ehdr, &syms[cnt].sym), marker,
1013 symstr,
1014 color_mode ? color_address : "",
1015 color,
1016 color_mode ? color_off : "",
1017 digits, (uint64_t) syms[cnt].sym.st_size);
1018#undef HEXFMT
1019#undef DECFMT
1020#undef OCTFMT
1021 }
1022 else
1023 {
1024#define HEXFMT "%6$s%2$0*1$" PRIx64 "%8$s %7$s%3$c%4$s %5$s"
1025#define DECFMT "%6$s%2$*1$" PRId64 "%8$s %7$s%3$c%4$s %5$s"
1026#define OCTFMT "%6$s%2$0*1$" PRIo64 "%8$s %7$s%3$c%4$s %5$s"
1027 printf ((radix == radix_hex ? HEXFMT
1028 : (radix == radix_decimal ? DECFMT : OCTFMT)),
1029 digits, syms[cnt].sym.st_value,
1030 class_type_char (elf, ehdr, &syms[cnt].sym), marker,
1031 symstr,
1032 color_mode ? color_address : "",
1033 color,
1034 color_mode ? color_off : "");
1035#undef HEXFMT
1036#undef DECFMT
1037#undef OCTFMT
1038 }
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -05001039 }
1040
1041 if (color_mode)
1042 fputs_unlocked (color_off, stdout);
1043 putchar_unlocked ('\n');
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001044 }
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001045
1046#ifdef USE_DEMANGLE
1047 free (demangle_buffer);
1048#endif
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001049}
1050
1051
1052static void
Ulrich Drepper2356ba02011-10-03 07:23:07 -04001053show_symbols_posix (Elf *elf, const GElf_Ehdr *ehdr, GElf_Word strndx,
1054 const char *prefix, const char *fullname, GElf_SymX *syms,
1055 size_t nsyms)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001056{
1057 if (prefix != NULL && ! print_file_name)
1058 printf ("%s:\n", fullname);
1059
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001060 int digits = length_map[gelf_getclass (elf) - 1][radix];
1061
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001062#ifdef USE_DEMANGLE
1063 size_t demangle_buffer_len = 0;
1064 char *demangle_buffer = NULL;
1065#endif
1066
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001067 /* Iterate over all symbols. */
1068 for (size_t cnt = 0; cnt < nsyms; ++cnt)
1069 {
Roland McGrath468fe4d2008-12-11 21:00:12 -08001070 char symstrbuf[50];
1071 const char *symstr = sym_name (elf, strndx, syms[cnt].sym.st_name,
1072 symstrbuf, sizeof symstrbuf);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001073
1074 /* Printing entries with a zero-length name makes the output
1075 not very well parseable. Since these entries don't carry
1076 much information we leave them out. */
1077 if (symstr[0] == '\0')
1078 continue;
1079
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001080#ifdef USE_DEMANGLE
Jan Kratochvil7c6e7852014-01-15 21:16:57 +01001081 /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */
1082 if (demangle && symstr[0] == '_' && symstr[1] == 'Z')
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001083 {
1084 int status = -1;
1085 char *dmsymstr = __cxa_demangle (symstr, demangle_buffer,
1086 &demangle_buffer_len, &status);
1087
1088 if (status == 0)
1089 symstr = dmsymstr;
1090 }
1091#endif
1092
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001093 /* If we have to precede the line with the file name. */
1094 if (print_file_name)
1095 {
1096 fputs_unlocked (fullname, stdout);
1097 putchar_unlocked (':');
1098 putchar_unlocked (' ');
1099 }
1100
Mark Wielaardf48eb6b2014-01-23 00:56:41 +01001101 printf ((radix == radix_hex
1102 ? "%s %c%s %0*" PRIx64 " %0*" PRIx64 "\n"
1103 : (radix == radix_decimal
1104 ? "%s %c%s %*" PRId64 " %*" PRId64 "\n"
1105 : "%s %c%s %0*" PRIo64 " %0*" PRIo64 "\n")),
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001106 symstr,
Ulrich Drepper2356ba02011-10-03 07:23:07 -04001107 class_type_char (elf, ehdr, &syms[cnt].sym),
1108 mark_special
1109 ? (GELF_ST_TYPE (syms[cnt].sym.st_info) == STT_TLS
1110 ? "@"
1111 : (GELF_ST_BIND (syms[cnt].sym.st_info) == STB_WEAK
1112 ? "*" : " "))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001113 : "",
1114 digits, syms[cnt].sym.st_value,
1115 digits, syms[cnt].sym.st_size);
1116 }
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001117
1118#ifdef USE_DEMANGLE
1119 free (demangle_buffer);
1120#endif
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001121}
1122
1123
1124/* Maximum size of memory we allocate on the stack. */
1125#define MAX_STACK_ALLOC 65536
1126
Roland McGrathcb6d8652007-08-23 08:10:54 +00001127static int
1128sort_by_address (const void *p1, const void *p2)
1129{
1130 GElf_SymX *s1 = (GElf_SymX *) p1;
1131 GElf_SymX *s2 = (GElf_SymX *) p2;
1132
1133 int result = (s1->sym.st_value < s2->sym.st_value
1134 ? -1 : (s1->sym.st_value == s2->sym.st_value ? 0 : 1));
1135
1136 return reverse_sort ? -result : result;
1137}
1138
1139static Elf_Data *sort_by_name_strtab;
1140
1141static int
1142sort_by_name (const void *p1, const void *p2)
1143{
1144 GElf_SymX *s1 = (GElf_SymX *) p1;
1145 GElf_SymX *s2 = (GElf_SymX *) p2;
1146
1147 const char *n1 = sort_by_name_strtab->d_buf + s1->sym.st_name;
1148 const char *n2 = sort_by_name_strtab->d_buf + s2->sym.st_name;
1149
1150 int result = strcmp (n1, n2);
1151
1152 return reverse_sort ? -result : result;
1153}
1154
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001155static void
1156show_symbols (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, Elf_Scn *xndxscn,
1157 GElf_Shdr *shdr, const char *prefix, const char *fname,
1158 const char *fullname)
1159{
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001160 /* Get the section header string table index. */
1161 size_t shstrndx;
Ulrich Drepperf1894932009-06-13 15:55:42 -07001162 if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001163 error (EXIT_FAILURE, 0,
1164 gettext ("cannot get section header string table index"));
1165
1166 /* The section is that large. */
1167 size_t size = shdr->sh_size;
1168 /* One entry is this large. */
1169 size_t entsize = shdr->sh_entsize;
1170
1171 /* Consistency checks. */
1172 if (entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, ehdr->e_version))
1173 error (0, 0,
1174 gettext ("%s: entry size in section `%s' is not what we expect"),
1175 fullname, elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1176 else if (size % entsize != 0)
1177 error (0, 0,
1178 gettext ("%s: size of section `%s' is not multiple of entry size"),
1179 fullname, elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1180
1181 /* Compute number of entries. Handle buggy entsize values. */
1182 size_t nentries = size / (entsize ?: 1);
1183
1184
1185#define obstack_chunk_alloc xmalloc
1186#define obstack_chunk_free free
1187 struct obstack whereob;
1188 obstack_init (&whereob);
1189
1190 /* Get a DWARF debugging descriptor. It's no problem if this isn't
1191 possible. We just won't print any line number information. */
1192 Dwarf *dbg = NULL;
1193 if (format == format_sysv)
1194 {
1195 dbg = dwarf_begin_elf (ebl->elf, DWARF_C_READ, NULL);
1196 if (dbg != NULL)
1197 {
1198 (void) dwarf_getpubnames (dbg, get_global, NULL, 0);
1199
1200 get_local_names (dbg);
1201 }
1202 }
1203
1204 /* Allocate the memory.
1205
1206 XXX We can use a dirty trick here. Since GElf_Sym == Elf64_Sym we
1207 can use the data memory instead of copying again if what we read
1208 is a 64 bit file. */
1209 GElf_SymX *sym_mem;
1210 if (nentries * sizeof (GElf_SymX) < MAX_STACK_ALLOC)
1211 sym_mem = (GElf_SymX *) alloca (nentries * sizeof (GElf_SymX));
1212 else
1213 sym_mem = (GElf_SymX *) xmalloc (nentries * sizeof (GElf_SymX));
1214
1215 /* Get the data of the section. */
1216 Elf_Data *data = elf_getdata (scn, NULL);
1217 Elf_Data *xndxdata = elf_getdata (xndxscn, NULL);
1218 if (data == NULL || (xndxscn != NULL && xndxdata == NULL))
1219 INTERNAL_ERROR (fullname);
1220
1221 /* Iterate over all symbols. */
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001222#ifdef USE_DEMANGLE
1223 size_t demangle_buffer_len = 0;
1224 char *demangle_buffer = NULL;
1225#endif
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001226 int longest_name = 4;
1227 int longest_where = 4;
1228 size_t nentries_used = 0;
1229 for (size_t cnt = 0; cnt < nentries; ++cnt)
1230 {
1231 GElf_Sym *sym = gelf_getsymshndx (data, xndxdata, cnt,
1232 &sym_mem[nentries_used].sym,
1233 &sym_mem[nentries_used].xndx);
1234 if (sym == NULL)
1235 INTERNAL_ERROR (fullname);
1236
1237 /* Filter out administrative symbols without a name and those
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001238 deselected by the user with command line options. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001239 if ((hide_undefined && sym->st_shndx == SHN_UNDEF)
1240 || (hide_defined && sym->st_shndx != SHN_UNDEF)
1241 || (hide_local && GELF_ST_BIND (sym->st_info) == STB_LOCAL))
1242 continue;
1243
1244 sym_mem[nentries_used].where = "";
1245 if (format == format_sysv)
1246 {
1247 const char *symstr = elf_strptr (ebl->elf, shdr->sh_link,
1248 sym->st_name);
Roland McGrath468fe4d2008-12-11 21:00:12 -08001249 if (symstr == NULL)
1250 continue;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001251
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001252#ifdef USE_DEMANGLE
Jan Kratochvil7c6e7852014-01-15 21:16:57 +01001253 /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */
1254 if (demangle && symstr[0] == '_' && symstr[1] == 'Z')
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001255 {
1256 int status = -1;
1257 char *dmsymstr = __cxa_demangle (symstr, demangle_buffer,
1258 &demangle_buffer_len, &status);
1259
1260 if (status == 0)
1261 symstr = dmsymstr;
1262 }
1263#endif
1264
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001265 longest_name = MAX ((size_t) longest_name, strlen (symstr));
1266
1267 if (sym->st_shndx != SHN_UNDEF
1268 && GELF_ST_BIND (sym->st_info) != STB_LOCAL
1269 && global_root != NULL)
1270 {
1271 Dwarf_Global fake = { .name = symstr };
1272 Dwarf_Global **found = tfind (&fake, &global_root,
1273 global_compare);
1274 if (found != NULL)
1275 {
1276 Dwarf_Die die_mem;
1277 Dwarf_Die *die = dwarf_offdie (dbg, (*found)->die_offset,
1278 &die_mem);
1279
1280 Dwarf_Die cudie_mem;
1281 Dwarf_Die *cudie = NULL;
1282
1283 Dwarf_Addr lowpc;
1284 Dwarf_Addr highpc;
1285 if (die != NULL
1286 && dwarf_lowpc (die, &lowpc) == 0
1287 && lowpc <= sym->st_value
1288 && dwarf_highpc (die, &highpc) == 0
1289 && highpc > sym->st_value)
1290 cudie = dwarf_offdie (dbg, (*found)->cu_offset,
1291 &cudie_mem);
1292 if (cudie != NULL)
1293 {
1294 Dwarf_Line *line = dwarf_getsrc_die (cudie,
1295 sym->st_value);
1296 if (line != NULL)
1297 {
1298 /* We found the line. */
1299 int lineno;
1300 (void) dwarf_lineno (line, &lineno);
1301 int n;
1302 n = obstack_printf (&whereob, "%s:%d%c",
1303 basename (dwarf_linesrc (line,
1304 NULL,
1305 NULL)),
1306 lineno, '\0');
1307 sym_mem[nentries_used].where
1308 = obstack_finish (&whereob);
1309
1310 /* The return value of obstack_print included the
1311 NUL byte, so subtract one. */
1312 if (--n > (int) longest_where)
1313 longest_where = (size_t) n;
1314 }
1315 }
1316 }
1317 }
1318
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001319 /* Try to find the symbol among the local symbols. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001320 if (sym_mem[nentries_used].where[0] == '\0')
1321 {
1322 struct local_name fake =
1323 {
1324 .name = symstr,
1325 .lowpc = sym->st_value,
1326 .highpc = sym->st_value,
1327 };
1328 struct local_name **found = tfind (&fake, &local_root,
1329 local_compare);
1330 if (found != NULL)
1331 {
1332 /* We found the line. */
1333 int n = obstack_printf (&whereob, "%s:%" PRIu64 "%c",
1334 basename ((*found)->file),
1335 (*found)->lineno,
1336 '\0');
1337 sym_mem[nentries_used].where = obstack_finish (&whereob);
1338
1339 /* The return value of obstack_print included the
1340 NUL byte, so subtract one. */
1341 if (--n > (int) longest_where)
1342 longest_where = (size_t) n;
1343 }
1344 }
1345 }
1346
1347 /* We use this entry. */
1348 ++nentries_used;
1349 }
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001350#ifdef USE_DEMANGLE
1351 free (demangle_buffer);
1352#endif
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001353 /* Now we know the exact number. */
1354 nentries = nentries_used;
1355
1356 /* Sort the entries according to the users wishes. */
1357 if (sort == sort_name)
Roland McGrathcb6d8652007-08-23 08:10:54 +00001358 {
1359 sort_by_name_strtab = elf_getdata (elf_getscn (ebl->elf, shdr->sh_link),
1360 NULL);
1361 qsort (sym_mem, nentries, sizeof (GElf_SymX), sort_by_name);
1362 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001363 else if (sort == sort_numeric)
1364 qsort (sym_mem, nentries, sizeof (GElf_SymX), sort_by_address);
1365
1366 /* Finally print according to the users selection. */
1367 switch (format)
1368 {
1369 case format_sysv:
Marek Polacekc8920de2011-05-12 12:08:21 +02001370 show_symbols_sysv (ebl, shdr->sh_link, fullname, sym_mem, nentries,
1371 longest_name, longest_where);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001372 break;
1373
1374 case format_bsd:
Ulrich Drepper2356ba02011-10-03 07:23:07 -04001375 show_symbols_bsd (ebl->elf, ehdr, shdr->sh_link, prefix, fname, fullname,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001376 sym_mem, nentries);
1377 break;
1378
1379 case format_posix:
1380 default:
1381 assert (format == format_posix);
Ulrich Drepper2356ba02011-10-03 07:23:07 -04001382 show_symbols_posix (ebl->elf, ehdr, shdr->sh_link, prefix, fullname,
1383 sym_mem, nentries);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001384 break;
1385 }
1386
1387 /* Free all memory. */
1388 if (nentries * sizeof (GElf_Sym) >= MAX_STACK_ALLOC)
1389 free (sym_mem);
1390
1391 obstack_free (&whereob, NULL);
1392
1393 if (dbg != NULL)
1394 {
1395 tdestroy (global_root, free);
1396 global_root = NULL;
1397
1398 tdestroy (local_root, free);
1399 local_root = NULL;
1400
1401 (void) dwarf_end (dbg);
1402 }
1403}
1404
1405
1406static int
1407handle_elf (Elf *elf, const char *prefix, const char *fname,
1408 const char *suffix)
1409{
1410 size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
1411 size_t suffix_len = suffix == NULL ? 0 : strlen (suffix);
1412 size_t fname_len = strlen (fname) + 1;
1413 char fullname[prefix_len + 1 + fname_len + suffix_len];
1414 char *cp = fullname;
1415 Elf_Scn *scn = NULL;
1416 int any = 0;
1417 int result = 0;
1418 GElf_Ehdr ehdr_mem;
1419 GElf_Ehdr *ehdr;
1420 Ebl *ebl;
1421
1422 /* Get the backend for this object file type. */
1423 ebl = ebl_openbackend (elf);
1424
1425 /* We need the ELF header in a few places. */
1426 ehdr = gelf_getehdr (elf, &ehdr_mem);
1427 if (ehdr == NULL)
1428 INTERNAL_ERROR (fullname);
1429
1430 /* If we are asked to print the dynamic symbol table and this is
1431 executable or dynamic executable, fail. */
1432 if (symsec_type == SHT_DYNSYM
1433 && ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
1434 {
1435 /* XXX Add machine specific object file types. */
1436 error (0, 0, gettext ("%s%s%s%s: Invalid operation"),
1437 prefix ?: "", prefix ? "(" : "", fname, prefix ? ")" : "");
1438 result = 1;
1439 goto out;
1440 }
1441
1442 /* Create the full name of the file. */
1443 if (prefix != NULL)
1444 cp = mempcpy (cp, prefix, prefix_len);
1445 cp = mempcpy (cp, fname, fname_len);
1446 if (suffix != NULL)
1447 memcpy (cp - 1, suffix, suffix_len + 1);
1448
1449 /* Find the symbol table.
1450
1451 XXX Can there be more than one? Do we print all? Currently we do. */
1452 while ((scn = elf_nextscn (elf, scn)) != NULL)
1453 {
1454 GElf_Shdr shdr_mem;
1455 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1456
1457 if (shdr == NULL)
1458 INTERNAL_ERROR (fullname);
1459
1460 if (shdr->sh_type == symsec_type)
1461 {
1462 Elf_Scn *xndxscn = NULL;
1463
1464 /* We have a symbol table. First make sure we remember this. */
1465 any = 1;
1466
1467 /* Look for an extended section index table for this section. */
1468 if (symsec_type == SHT_SYMTAB)
1469 {
1470 size_t scnndx = elf_ndxscn (scn);
1471
1472 while ((xndxscn = elf_nextscn (elf, xndxscn)) != NULL)
1473 {
1474 GElf_Shdr xndxshdr_mem;
1475 GElf_Shdr *xndxshdr = gelf_getshdr (xndxscn, &xndxshdr_mem);
1476
1477 if (xndxshdr == NULL)
1478 INTERNAL_ERROR (fullname);
1479
1480 if (xndxshdr->sh_type == SHT_SYMTAB_SHNDX
1481 && xndxshdr->sh_link == scnndx)
1482 break;
1483 }
1484 }
1485
1486 show_symbols (ebl, ehdr, scn, xndxscn, shdr, prefix, fname,
1487 fullname);
1488 }
1489 }
1490
1491 if (! any)
1492 {
1493 error (0, 0, gettext ("%s%s%s: no symbols"),
1494 prefix ?: "", prefix ? ":" : "", fname);
1495 result = 1;
1496 }
1497
1498 out:
1499 /* Close the ELF backend library descriptor. */
1500 ebl_closebackend (ebl);
1501
1502 return result;
1503}
Ulrich Drepper3cbdd382008-01-02 17:44:39 +00001504
1505
1506#include "debugpred.h"