blob: 15d9da4abe5dfe76663adc0fb45ce72749fba1a8 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Print symbol information from ELF file in human-readable form.
Mark Wielaard587c4b32015-04-14 11:59:36 +02002 Copyright (C) 2000-2008, 2009, 2011, 2012, 2014, 2015 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>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000036#include <obstack.h>
37#include <search.h>
38#include <stdbool.h>
39#include <stdio.h>
40#include <stdio_ext.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44#include <sys/param.h>
45
46#include <system.h>
47#include "../libebl/libeblP.h"
48
49
50/* Name and version of program. */
51static void print_version (FILE *stream, struct argp_state *state);
Ulrich Drepperfdc93e12009-01-17 11:47:10 -080052ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000053
54/* Bug report address. */
Ulrich Drepperfdc93e12009-01-17 11:47:10 -080055ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000056
57
58/* Values for the parameters which have no short form. */
Ulrich Drepper2356ba02011-10-03 07:23:07 -040059#define OPT_DEFINED 0x100
60#define OPT_MARK_SPECIAL 0x101
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000061
62/* Definitions of arguments for argp functions. */
63static const struct argp_option options[] =
64{
65 { NULL, 0, NULL, 0, N_("Output selection:"), 0 },
66 { "debug-syms", 'a', NULL, 0, N_("Display debugger-only symbols"), 0 },
67 { "defined-only", OPT_DEFINED, NULL, 0, N_("Display only defined symbols"),
68 0 },
69 { "dynamic", 'D', NULL, 0,
70 N_("Display dynamic symbols instead of normal symbols"), 0 },
71 { "extern-only", 'g', NULL, 0, N_("Display only external symbols"), 0 },
72 { "undefined-only", 'u', NULL, 0, N_("Display only undefined symbols"), 0 },
73 { "print-armap", 's', NULL, 0,
74 N_("Include index for symbols from archive members"), 0 },
75
76 { NULL, 0, NULL, 0, N_("Output format:"), 0 },
77 { "print-file-name", 'A', NULL, 0,
78 N_("Print name of the input file before every symbol"), 0 },
79 { NULL, 'o', NULL, OPTION_HIDDEN, "Same as -A", 0 },
80 { "format", 'f', "FORMAT", 0,
81 N_("Use the output format FORMAT. FORMAT can be `bsd', `sysv' or `posix'. The default is `sysv'"),
82 0 },
83 { NULL, 'B', NULL, 0, N_("Same as --format=bsd"), 0 },
84 { "portability", 'P', NULL, 0, N_("Same as --format=posix"), 0 },
85 { "radix", 't', "RADIX", 0, N_("Use RADIX for printing symbol values"), 0 },
Ulrich Drepper2356ba02011-10-03 07:23:07 -040086 { "mark-special", OPT_MARK_SPECIAL, NULL, 0, N_("Mark special symbols"), 0 },
87 { "mark-weak", OPT_MARK_SPECIAL, NULL, OPTION_HIDDEN, "", 0 },
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000088 { "print-size", 'S', NULL, 0, N_("Print size of defined symbols"), 0 },
89
90 { NULL, 0, NULL, 0, N_("Output options:"), 0 },
91 { "numeric-sort", 'n', NULL, 0, N_("Sort symbols numerically by address"),
92 0 },
93 { "no-sort", 'p', NULL, 0, N_("Do not sort the symbols"), 0 },
94 { "reverse-sort", 'r', NULL, 0, N_("Reverse the sense of the sort"), 0 },
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -040095#ifdef USE_DEMANGLE
96 { "demangle", 'C', NULL, 0,
97 N_("Decode low-level symbol names into source code names"), 0 },
98#endif
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000099 { NULL, 0, NULL, 0, N_("Miscellaneous:"), 0 },
100 { NULL, 0, NULL, 0, NULL, 0 }
101};
102
103/* Short description of program. */
104static const char doc[] = N_("List symbols from FILEs (a.out by default).");
105
106/* Strings for arguments in help texts. */
107static const char args_doc[] = N_("[FILE...]");
108
109/* Prototype for option handler. */
110static error_t parse_opt (int key, char *arg, struct argp_state *state);
111
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500112/* Parser children. */
113static struct argp_child argp_children[] =
114 {
115 { &color_argp, 0, N_("Output formatting"), 2 },
116 { NULL, 0, NULL, 0}
117 };
118
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000119/* Data structure to communicate with argp functions. */
120static struct argp argp =
121{
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500122 options, parse_opt, args_doc, doc, argp_children, NULL, NULL
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000123};
124
125
126/* Print symbols in file named FNAME. */
127static int process_file (const char *fname, bool more_than_one);
128
129/* Handle content of archive. */
130static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
131 const char *suffix);
132
133/* Handle ELF file. */
134static int handle_elf (Elf *elf, const char *prefix, const char *fname,
135 const char *suffix);
136
137
138#define INTERNAL_ERROR(fname) \
Mark Wielaard587c4b32015-04-14 11:59:36 +0200139 error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s): %s"), \
140 fname, __LINE__, PACKAGE_VERSION, elf_errmsg (-1))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000141
142
143/* Internal representation of symbols. */
144typedef struct GElf_SymX
145{
146 GElf_Sym sym;
147 Elf32_Word xndx;
148 char *where;
149} GElf_SymX;
150
151
152/* User-selectable options. */
153
154/* The selected output format. */
155static enum
156{
157 format_sysv = 0,
158 format_bsd,
159 format_posix
160} format;
161
162/* Print defined, undefined, or both? */
163static bool hide_undefined;
164static bool hide_defined;
165
166/* Print local symbols also? */
167static bool hide_local;
168
169/* Nonzero if full filename should precede every symbol. */
170static bool print_file_name;
171
172/* If true print size of defined symbols in BSD format. */
173static bool print_size;
174
175/* If true print archive index. */
176static bool print_armap;
177
178/* If true reverse sorting. */
179static bool reverse_sort;
180
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400181#ifdef USE_DEMANGLE
182/* If true demangle symbols. */
183static bool demangle;
184#endif
185
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000186/* Type of the section we are printing. */
187static GElf_Word symsec_type = SHT_SYMTAB;
188
189/* Sorting selection. */
190static enum
191{
192 sort_name = 0,
193 sort_numeric,
194 sort_nosort
195} sort;
196
197/* Radix for printed numbers. */
198static enum
199{
200 radix_hex = 0,
201 radix_decimal,
202 radix_octal
203} radix;
204
Ulrich Drepper2356ba02011-10-03 07:23:07 -0400205/* If nonzero mark special symbols:
206 - weak symbols are distinguished from global symbols by adding
207 a `*' after the identifying letter for the symbol class and type.
208 - TLS symbols are distinguished from normal symbols by adding
209 a '@' after the identifying letter for the symbol class and type. */
210static bool mark_special;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000211
212
213int
214main (int argc, char *argv[])
215{
216 int remaining;
217 int result = 0;
218
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000219 /* We use no threads here which can interfere with handling a stream. */
220 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
221 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
222 (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER);
223
224 /* Set locale. */
225 (void) setlocale (LC_ALL, "");
226
227 /* Make sure the message catalog can be found. */
Ulrich Drepperb0243862007-06-06 00:09:36 +0000228 (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000229
230 /* Initialize the message catalog. */
Ulrich Drepperb0243862007-06-06 00:09:36 +0000231 (void) textdomain (PACKAGE_TARNAME);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000232
233 /* Parse and process arguments. */
234 (void) argp_parse (&argp, argc, argv, 0, &remaining, NULL);
235
236 /* Tell the library which version we are expecting. */
237 (void) elf_version (EV_CURRENT);
238
239 if (remaining == argc)
240 /* The user didn't specify a name so we use a.out. */
241 result = process_file ("a.out", false);
242 else
243 {
244 /* Process all the remaining files. */
245 const bool more_than_one = remaining + 1 < argc;
246
247 do
248 result |= process_file (argv[remaining], more_than_one);
249 while (++remaining < argc);
250 }
251
252 return result;
253}
254
255
256/* Print the version information. */
257static void
258print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
259{
Ulrich Drepperb0243862007-06-06 00:09:36 +0000260 fprintf (stream, "nm (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000261 fprintf (stream, gettext ("\
262Copyright (C) %s Red Hat, Inc.\n\
263This is free software; see the source for copying conditions. There is NO\n\
264warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
Ulrich Drepper3a64a302012-01-21 18:19:24 -0500265"), "2012");
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000266 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
267}
268
269
270/* Handle program arguments. */
271static error_t
272parse_opt (int key, char *arg,
273 struct argp_state *state __attribute__ ((unused)))
274{
275 switch (key)
276 {
277 case 'a':
278 /* XXX */
279 break;
280
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400281#ifdef USE_DEMANGLE
282 case 'C':
283 demangle = true;
284 break;
285#endif
286
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000287 case 'f':
288 if (strcmp (arg, "bsd") == 0)
289 format = format_bsd;
290 else if (strcmp (arg, "posix") == 0)
291 format = format_posix;
292 else
293 /* Be bug compatible. The BFD implementation also defaulted to
294 using the SysV format if nothing else matches. */
295 format = format_sysv;
296 break;
297
298 case 'g':
299 hide_local = true;
300 break;
301
302 case 'n':
303 sort = sort_numeric;
304 break;
305
306 case 'p':
307 sort = sort_nosort;
308 break;
309
310 case 't':
311 if (strcmp (arg, "10") == 0 || strcmp (arg, "d") == 0)
312 radix = radix_decimal;
313 else if (strcmp (arg, "8") == 0 || strcmp (arg, "o") == 0)
314 radix = radix_octal;
315 else
316 radix = radix_hex;
317 break;
318
319 case 'u':
320 hide_undefined = false;
321 hide_defined = true;
322 break;
323
324 case 'A':
325 case 'o':
326 print_file_name = true;
327 break;
328
329 case 'B':
330 format = format_bsd;
331 break;
332
333 case 'D':
334 symsec_type = SHT_DYNSYM;
335 break;
336
337 case 'P':
338 format = format_posix;
339 break;
340
341 case OPT_DEFINED:
342 hide_undefined = true;
343 hide_defined = false;
344 break;
345
Ulrich Drepper2356ba02011-10-03 07:23:07 -0400346 case OPT_MARK_SPECIAL:
347 mark_special = true;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000348 break;
349
350 case 'S':
351 print_size = true;
352 break;
353
354 case 's':
355 print_armap = true;
356 break;
357
358 case 'r':
359 reverse_sort = true;
360 break;
361
362 default:
363 return ARGP_ERR_UNKNOWN;
364 }
365 return 0;
366}
367
368
369/* Open the file and determine the type. */
370static int
371process_file (const char *fname, bool more_than_one)
372{
373 /* Open the file. */
374 int fd = open (fname, O_RDONLY);
375 if (fd == -1)
376 {
377 error (0, errno, gettext ("cannot open '%s'"), fname);
378 return 1;
379 }
380
381 /* Now get the ELF descriptor. */
382 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
383 if (elf != NULL)
384 {
385 if (elf_kind (elf) == ELF_K_ELF)
386 {
387 int result = handle_elf (elf, more_than_one ? "" : NULL,
388 fname, NULL);
389
390 if (elf_end (elf) != 0)
391 INTERNAL_ERROR (fname);
392
393 if (close (fd) != 0)
Ulrich Drepperd112ef82005-09-03 21:31:27 +0000394 error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000395
396 return result;
397 }
398 else if (elf_kind (elf) == ELF_K_AR)
399 {
400 int result = handle_ar (fd, elf, NULL, fname, NULL);
401
402 if (elf_end (elf) != 0)
403 INTERNAL_ERROR (fname);
404
405 if (close (fd) != 0)
Ulrich Drepperd112ef82005-09-03 21:31:27 +0000406 error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000407
408 return result;
409 }
410
411 /* We cannot handle this type. Close the descriptor anyway. */
412 if (elf_end (elf) != 0)
413 INTERNAL_ERROR (fname);
414 }
415
416 error (0, 0, gettext ("%s: File format not recognized"), fname);
417
418 return 1;
419}
420
421
422static int
423handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
424 const char *suffix)
425{
426 size_t fname_len = strlen (fname) + 1;
427 size_t prefix_len = prefix != NULL ? strlen (prefix) : 0;
428 char new_prefix[prefix_len + fname_len + 2];
429 size_t suffix_len = suffix != NULL ? strlen (suffix) : 0;
430 char new_suffix[suffix_len + 2];
431 Elf *subelf;
432 Elf_Cmd cmd = ELF_C_READ_MMAP;
433 int result = 0;
434
435 char *cp = new_prefix;
436 if (prefix != NULL)
437 cp = stpcpy (cp, prefix);
438 cp = stpcpy (cp, fname);
439 stpcpy (cp, "[");
440
441 cp = new_suffix;
442 if (suffix != NULL)
443 cp = stpcpy (cp, suffix);
444 stpcpy (cp, "]");
445
446 /* First print the archive index if this is wanted. */
447 if (print_armap)
448 {
449 Elf_Arsym *arsym = elf_getarsym (elf, NULL);
450
451 if (arsym != NULL)
452 {
453 Elf_Arhdr *arhdr = NULL;
454 size_t arhdr_off = 0; /* Note: 0 is no valid offset. */
455
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500456 fputs_unlocked (gettext("\nArchive index:\n"), stdout);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000457
458 while (arsym->as_off != 0)
459 {
460 if (arhdr_off != arsym->as_off
461 && (elf_rand (elf, arsym->as_off) != arsym->as_off
462 || (subelf = elf_begin (fd, cmd, elf)) == NULL
463 || (arhdr = elf_getarhdr (subelf)) == NULL))
464 {
465 error (0, 0, gettext ("invalid offset %zu for symbol %s"),
466 arsym->as_off, arsym->as_name);
Mark Wielaard0b799ad2014-12-26 16:12:52 +0100467 break;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000468 }
469
470 printf (gettext ("%s in %s\n"), arsym->as_name, arhdr->ar_name);
471
472 ++arsym;
473 }
474
475 if (elf_rand (elf, SARMAG) != SARMAG)
476 {
477 error (0, 0,
478 gettext ("cannot reset archive offset to beginning"));
479 return 1;
480 }
481 }
482 }
483
484 /* Process all the files contained in the archive. */
485 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
486 {
487 /* The the header for this element. */
488 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
489
490 /* Skip over the index entries. */
491 if (strcmp (arhdr->ar_name, "/") != 0
Mark Wielaard8fb260f2014-12-26 16:20:39 +0100492 && strcmp (arhdr->ar_name, "//") != 0
493 && strcmp (arhdr->ar_name, "/SYM64/") != 0)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000494 {
495 if (elf_kind (subelf) == ELF_K_ELF)
496 result |= handle_elf (subelf, new_prefix, arhdr->ar_name,
497 new_suffix);
498 else if (elf_kind (subelf) == ELF_K_AR)
499 result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name,
500 new_suffix);
501 else
502 {
503 error (0, 0, gettext ("%s%s%s: file format not recognized"),
504 new_prefix, arhdr->ar_name, new_suffix);
505 result = 1;
506 }
507 }
508
509 /* Get next archive element. */
510 cmd = elf_next (subelf);
511 if (elf_end (subelf) != 0)
512 INTERNAL_ERROR (fname);
513 }
514
515 return result;
516}
517
518
519/* Mapping of radix and binary class to length. */
520static const int length_map[2][3] =
521{
522 [ELFCLASS32 - 1] =
523 {
524 [radix_hex] = 8,
525 [radix_decimal] = 10,
526 [radix_octal] = 11
527 },
528 [ELFCLASS64 - 1] =
529 {
530 [radix_hex] = 16,
531 [radix_decimal] = 20,
532 [radix_octal] = 22
533 }
534};
535
536
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000537static int
538global_compare (const void *p1, const void *p2)
539{
540 const Dwarf_Global *g1 = (const Dwarf_Global *) p1;
541 const Dwarf_Global *g2 = (const Dwarf_Global *) p2;
542
543 return strcmp (g1->name, g2->name);
544}
545
546
547static void *global_root;
548
549
550static int
551get_global (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
552 void *arg __attribute__ ((unused)))
553{
554 tsearch (memcpy (xmalloc (sizeof (Dwarf_Global)), global,
555 sizeof (Dwarf_Global)),
556 &global_root, global_compare);
557
558 return DWARF_CB_OK;
559}
560
561
562struct local_name
563{
564 const char *name;
565 const char *file;
566 Dwarf_Word lineno;
567 Dwarf_Addr lowpc;
568 Dwarf_Addr highpc;
569};
570
571
572static int
573local_compare (const void *p1, const void *p2)
574{
575 struct local_name *g1 = (struct local_name *) p1;
576 struct local_name *g2 = (struct local_name *) p2;
577 int result;
578
579 result = strcmp (g1->name, g2->name);
580 if (result == 0)
581 {
582 if (g1->lowpc <= g2->lowpc && g1->highpc >= g2->highpc)
583 {
584 /* g2 is contained in g1. Update the data. */
585 g2->lowpc = g1->lowpc;
586 g2->highpc = g1->highpc;
587 result = 0;
588 }
589 else if (g2->lowpc <= g1->lowpc && g2->highpc >= g1->highpc)
590 {
591 /* g1 is contained in g2. Update the data. */
592 g1->lowpc = g2->lowpc;
593 g1->highpc = g2->highpc;
594 result = 0;
595 }
596 else
597 result = g1->lowpc < g2->lowpc ? -1 : 1;
598 }
599
600 return result;
601}
602
603
604static int
605get_var_range (Dwarf_Die *die, Dwarf_Word *lowpc, Dwarf_Word *highpc)
606{
607 Dwarf_Attribute locattr_mem;
608 Dwarf_Attribute *locattr = dwarf_attr (die, DW_AT_location, &locattr_mem);
609 if (locattr == NULL)
610 return 1;
611
Roland McGrath6724c902005-10-28 07:07:19 +0000612 Dwarf_Op *loc;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000613 size_t nloc;
Roland McGrath6724c902005-10-28 07:07:19 +0000614 if (dwarf_getlocation (locattr, &loc, &nloc) != 0)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000615 return 1;
616
617 /* Interpret the location expressions. */
618 // XXX For now just the simple one:
619 if (nloc == 1 && loc[0].atom == DW_OP_addr)
620 {
621 *lowpc = *highpc = loc[0].number;
622 return 0;
623 }
624
625 return 1;
626}
627
628
629
630static void *local_root;
631
632
633static void
634get_local_names (Dwarf *dbg)
635{
636 Dwarf_Off offset = 0;
637 Dwarf_Off old_offset;
638 size_t hsize;
639
640 while (dwarf_nextcu (dbg, old_offset = offset, &offset, &hsize, NULL, NULL,
641 NULL) == 0)
642 {
643 Dwarf_Die cudie_mem;
644 Dwarf_Die *cudie = dwarf_offdie (dbg, old_offset + hsize, &cudie_mem);
645
646 /* If we cannot get the CU DIE there is no need to go on with
647 this CU. */
648 if (cudie == NULL)
649 continue;
650 /* This better be a CU DIE. */
651 if (dwarf_tag (cudie) != DW_TAG_compile_unit)
652 continue;
653
654 /* Get the line information. */
655 Dwarf_Files *files;
656 size_t nfiles;
657 if (dwarf_getsrcfiles (cudie, &files, &nfiles) != 0)
658 continue;
659
660 Dwarf_Die die_mem;
661 Dwarf_Die *die = &die_mem;
662 if (dwarf_child (cudie, die) == 0)
663 /* Iterate over all immediate children of the CU DIE. */
664 do
665 {
666 int tag = dwarf_tag (die);
667 if (tag != DW_TAG_subprogram && tag != DW_TAG_variable)
668 continue;
669
670 /* We are interested in five attributes: name, decl_file,
671 decl_line, low_pc, and high_pc. */
672 Dwarf_Attribute attr_mem;
673 Dwarf_Attribute *attr = dwarf_attr (die, DW_AT_name, &attr_mem);
674 const char *name = dwarf_formstring (attr);
675 if (name == NULL)
676 continue;
677
678 Dwarf_Word fileidx;
679 attr = dwarf_attr (die, DW_AT_decl_file, &attr_mem);
680 if (dwarf_formudata (attr, &fileidx) != 0 || fileidx >= nfiles)
681 continue;
682
683 Dwarf_Word lineno;
684 attr = dwarf_attr (die, DW_AT_decl_line, &attr_mem);
685 if (dwarf_formudata (attr, &lineno) != 0 || lineno == 0)
686 continue;
687
688 Dwarf_Addr lowpc;
689 Dwarf_Addr highpc;
690 if (tag == DW_TAG_subprogram)
691 {
692 if (dwarf_lowpc (die, &lowpc) != 0
693 || dwarf_highpc (die, &highpc) != 0)
694 continue;
695 }
696 else
697 {
698 if (get_var_range (die, &lowpc, &highpc) != 0)
699 continue;
700 }
701
702 /* We have all the information. Create a record. */
703 struct local_name *newp
704 = (struct local_name *) xmalloc (sizeof (*newp));
705 newp->name = name;
706 newp->file = dwarf_filesrc (files, fileidx, NULL, NULL);
707 newp->lineno = lineno;
708 newp->lowpc = lowpc;
709 newp->highpc = highpc;
710
711 /* Since we cannot deallocate individual memory we do not test
712 for duplicates in the tree. This should not happen anyway. */
713 if (tsearch (newp, &local_root, local_compare) == NULL)
714 error (EXIT_FAILURE, errno,
715 gettext ("cannot create search tree"));
716 }
717 while (dwarf_siblingof (die, die) == 0);
718 }
719}
720
Roland McGrath468fe4d2008-12-11 21:00:12 -0800721/* Do elf_strptr, but return a backup string and never NULL. */
722static const char *
723sym_name (Elf *elf, GElf_Word strndx, GElf_Word st_name, char buf[], size_t n)
724{
725 const char *symstr = elf_strptr (elf, strndx, st_name);
726 if (symstr == NULL)
727 {
728 snprintf (buf, n, "[invalid st_name %#" PRIx32 "]", st_name);
729 symstr = buf;
730 }
731 return symstr;
732}
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000733
734/* Show symbols in SysV format. */
735static void
Marek Polacekc8920de2011-05-12 12:08:21 +0200736show_symbols_sysv (Ebl *ebl, GElf_Word strndx, const char *fullname,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000737 GElf_SymX *syms, size_t nsyms, int longest_name,
738 int longest_where)
739{
740 size_t shnum;
Ulrich Drepperf1894932009-06-13 15:55:42 -0700741 if (elf_getshdrnum (ebl->elf, &shnum) < 0)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000742 INTERNAL_ERROR (fullname);
743
744 bool scnnames_malloced = shnum * sizeof (const char *) > 128 * 1024;
745 const char **scnnames;
746 if (scnnames_malloced)
747 scnnames = (const char **) xmalloc (sizeof (const char *) * shnum);
748 else
749 scnnames = (const char **) alloca (sizeof (const char *) * shnum);
750 /* Get the section header string table index. */
751 size_t shstrndx;
Ulrich Drepperf1894932009-06-13 15:55:42 -0700752 if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000753 error (EXIT_FAILURE, 0,
754 gettext ("cannot get section header string table index"));
755
756 /* Cache the section names. */
757 Elf_Scn *scn = NULL;
758 size_t cnt = 1;
759 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
760 {
761 GElf_Shdr shdr_mem;
762
Mark Wielaard03d76f42013-11-09 16:45:22 +0100763 assert (elf_ndxscn (scn) == cnt);
764 cnt++;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000765
Roland McGrath468fe4d2008-12-11 21:00:12 -0800766 char *name = elf_strptr (ebl->elf, shstrndx,
767 gelf_getshdr (scn, &shdr_mem)->sh_name);
768 if (unlikely (name == NULL))
769 {
Mark Wielaard7df3d2c2012-12-11 22:27:05 +0100770 const size_t bufsz = sizeof "[invalid sh_name 0x12345678]";
Roland McGrath57bd66c2012-12-11 09:42:07 -0800771 name = alloca (bufsz);
772 snprintf (name, bufsz, "[invalid sh_name %#" PRIx32 "]",
Roland McGrath468fe4d2008-12-11 21:00:12 -0800773 gelf_getshdr (scn, &shdr_mem)->sh_name);
774 }
775 scnnames[elf_ndxscn (scn)] = name;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000776 }
777
778 int digits = length_map[gelf_getclass (ebl->elf) - 1][radix];
779
780 /* We always print this prolog. */
Marek Polacekc8920de2011-05-12 12:08:21 +0200781 printf (gettext ("\n\nSymbols from %s:\n\n"), fullname);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000782
783 /* The header line. */
784 printf (gettext ("%*s%-*s %-*s Class Type %-*s %*s Section\n\n"),
785 print_file_name ? (int) strlen (fullname) + 1: 0, "",
786 longest_name, sgettext ("sysv|Name"),
787 /* TRANS: the "sysv|" parts makes the string unique. */
788 digits, sgettext ("sysv|Value"),
789 /* TRANS: the "sysv|" parts makes the string unique. */
790 digits, sgettext ("sysv|Size"),
791 /* TRANS: the "sysv|" parts makes the string unique. */
792 longest_where, sgettext ("sysv|Line"));
793
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400794#ifdef USE_DEMANGLE
795 size_t demangle_buffer_len = 0;
796 char *demangle_buffer = NULL;
797#endif
798
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000799 /* Iterate over all symbols. */
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400800 for (cnt = 1; cnt < nsyms; ++cnt)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000801 {
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400802 /* In this format SECTION entries are not printed. */
803 if (GELF_ST_TYPE (syms[cnt].sym.st_info) == STT_SECTION)
804 continue;
805
Roland McGrath468fe4d2008-12-11 21:00:12 -0800806 char symstrbuf[50];
807 const char *symstr = sym_name (ebl->elf, strndx, syms[cnt].sym.st_name,
808 symstrbuf, sizeof symstrbuf);
809
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400810#ifdef USE_DEMANGLE
Jan Kratochvil7c6e7852014-01-15 21:16:57 +0100811 /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */
812 if (demangle && symstr[0] == '_' && symstr[1] == 'Z')
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400813 {
814 int status = -1;
815 char *dmsymstr = __cxa_demangle (symstr, demangle_buffer,
816 &demangle_buffer_len, &status);
817
818 if (status == 0)
819 symstr = dmsymstr;
820 }
821#endif
822
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000823 char symbindbuf[50];
824 char symtypebuf[50];
825 char secnamebuf[1024];
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400826 char addressbuf[(64 + 2) / 3 + 1];
827 char sizebuf[(64 + 2) / 3 + 1];
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000828
829 /* If we have to precede the line with the file name. */
830 if (print_file_name)
831 {
832 fputs_unlocked (fullname, stdout);
833 putchar_unlocked (':');
834 }
835
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400836 /* Covert the address. */
837 if (syms[cnt].sym.st_shndx == SHN_UNDEF)
838 addressbuf[0] = sizebuf[0] = '\0';
839 else
840 {
Mark Wielaardf48eb6b2014-01-23 00:56:41 +0100841 snprintf (addressbuf, sizeof (addressbuf),
842 (radix == radix_hex ? "%0*" PRIx64
843 : (radix == radix_decimal ? "%0*" PRId64
844 : "%0*" PRIo64)),
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400845 digits, syms[cnt].sym.st_value);
Mark Wielaardf48eb6b2014-01-23 00:56:41 +0100846 snprintf (sizebuf, sizeof (sizebuf),
847 (radix == radix_hex ? "%0*" PRIx64
848 : (radix == radix_decimal ? "%0*" PRId64
849 : "%0*" PRIo64)),
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400850 digits, syms[cnt].sym.st_size);
851 }
852
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000853 /* Print the actual string. */
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400854 printf ("%-*s|%s|%-6s|%-8s|%s|%*s|%s\n",
855 longest_name, symstr, addressbuf,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000856 ebl_symbol_binding_name (ebl,
857 GELF_ST_BIND (syms[cnt].sym.st_info),
858 symbindbuf, sizeof (symbindbuf)),
859 ebl_symbol_type_name (ebl, GELF_ST_TYPE (syms[cnt].sym.st_info),
860 symtypebuf, sizeof (symtypebuf)),
Ulrich Drepper66f4c372011-10-03 15:53:12 -0400861 sizebuf, longest_where, syms[cnt].where,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000862 ebl_section_name (ebl, syms[cnt].sym.st_shndx, syms[cnt].xndx,
863 secnamebuf, sizeof (secnamebuf), scnnames,
864 shnum));
865 }
866
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400867#ifdef USE_DEMANGLE
868 free (demangle_buffer);
869#endif
870
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000871 if (scnnames_malloced)
872 free (scnnames);
873}
874
875
876static char
Ulrich Drepper2356ba02011-10-03 07:23:07 -0400877class_type_char (Elf *elf, const GElf_Ehdr *ehdr, GElf_Sym *sym)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000878{
879 int local_p = GELF_ST_BIND (sym->st_info) == STB_LOCAL;
880
881 /* XXX Add support for architecture specific types and classes. */
882 if (sym->st_shndx == SHN_ABS)
883 return local_p ? 'a' : 'A';
884
885 if (sym->st_shndx == SHN_UNDEF)
886 /* Undefined symbols must be global. */
887 return 'U';
888
Ulrich Drepper2356ba02011-10-03 07:23:07 -0400889 char result = "NDTSFBD "[GELF_ST_TYPE (sym->st_info)];
890
891 if (result == 'D')
892 {
893 /* Special handling: unique data symbols. */
894 if (ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX
895 && GELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
896 result = 'u';
897 else
898 {
899 GElf_Shdr shdr_mem;
900 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (elf, sym->st_shndx),
901 &shdr_mem);
902 if (shdr != NULL)
903 {
904 if ((shdr->sh_flags & SHF_WRITE) == 0)
905 result = 'R';
906 else if (shdr->sh_type == SHT_NOBITS)
907 result = 'B';
908 }
909 }
910 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000911
912 return local_p ? tolower (result) : result;
913}
914
915
916static void
Ulrich Drepper2356ba02011-10-03 07:23:07 -0400917show_symbols_bsd (Elf *elf, const GElf_Ehdr *ehdr, GElf_Word strndx,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000918 const char *prefix, const char *fname, const char *fullname,
919 GElf_SymX *syms, size_t nsyms)
920{
921 int digits = length_map[gelf_getclass (elf) - 1][radix];
922
923 if (prefix != NULL && ! print_file_name)
924 printf ("\n%s:\n", fname);
925
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400926#ifdef USE_DEMANGLE
927 size_t demangle_buffer_len = 0;
928 char *demangle_buffer = NULL;
929#endif
930
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000931 /* Iterate over all symbols. */
932 for (size_t cnt = 0; cnt < nsyms; ++cnt)
933 {
Roland McGrath468fe4d2008-12-11 21:00:12 -0800934 char symstrbuf[50];
935 const char *symstr = sym_name (elf, strndx, syms[cnt].sym.st_name,
936 symstrbuf, sizeof symstrbuf);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000937
938 /* Printing entries with a zero-length name makes the output
939 not very well parseable. Since these entries don't carry
940 much information we leave them out. */
941 if (symstr[0] == '\0')
942 continue;
943
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500944 /* We do not print the entries for files. */
945 if (GELF_ST_TYPE (syms[cnt].sym.st_info) == STT_FILE)
946 continue;
947
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400948#ifdef USE_DEMANGLE
Jan Kratochvil7c6e7852014-01-15 21:16:57 +0100949 /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */
950 if (demangle && symstr[0] == '_' && symstr[1] == 'Z')
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -0400951 {
952 int status = -1;
953 char *dmsymstr = __cxa_demangle (symstr, demangle_buffer,
954 &demangle_buffer_len, &status);
955
956 if (status == 0)
957 symstr = dmsymstr;
958 }
959#endif
960
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000961 /* If we have to precede the line with the file name. */
962 if (print_file_name)
963 {
964 fputs_unlocked (fullname, stdout);
965 putchar_unlocked (':');
966 }
967
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500968 bool is_tls = GELF_ST_TYPE (syms[cnt].sym.st_info) == STT_TLS;
969 bool is_weak = GELF_ST_BIND (syms[cnt].sym.st_info) == STB_WEAK;
970 const char *marker = (mark_special
971 ? (is_tls ? "@" : (is_weak ? "*" : " ")) : "");
972
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000973 if (syms[cnt].sym.st_shndx == SHN_UNDEF)
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500974 {
975 const char *color = "";
976 if (color_mode)
977 {
978 if (is_tls)
979 color = color_undef_tls;
980 else if (is_weak)
981 color = color_undef_weak;
982 else
983 color = color_undef;
984 }
985
986 printf ("%*s %sU%s %s", digits, "", color, marker, symstr);
987 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000988 else
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -0500989 {
990 const char *color = "";
991 if (color_mode)
992 {
993 if (is_tls)
994 color = color_tls;
995 else if (is_weak)
996 color = color_weak;
997 else
998 color = color_symbol;
999 }
Mark Wielaardf48eb6b2014-01-23 00:56:41 +01001000 if (print_size && syms[cnt].sym.st_size != 0)
1001 {
1002#define HEXFMT "%6$s%2$0*1$" PRIx64 "%8$s %10$0*9$" PRIx64 " %7$s%3$c%4$s %5$s"
1003#define DECFMT "%6$s%2$*1$" PRId64 "%8$s %10$*9$" PRId64 " %7$s%3$c%4$s %5$s"
1004#define OCTFMT "%6$s%2$0*1$" PRIo64 "%8$s %10$0*9$" PRIo64 " %7$s%3$c%4$s %5$s"
1005 printf ((radix == radix_hex ? HEXFMT
1006 : (radix == radix_decimal ? DECFMT : OCTFMT)),
1007 digits, syms[cnt].sym.st_value,
1008 class_type_char (elf, ehdr, &syms[cnt].sym), marker,
1009 symstr,
1010 color_mode ? color_address : "",
1011 color,
1012 color_mode ? color_off : "",
1013 digits, (uint64_t) syms[cnt].sym.st_size);
1014#undef HEXFMT
1015#undef DECFMT
1016#undef OCTFMT
1017 }
1018 else
1019 {
1020#define HEXFMT "%6$s%2$0*1$" PRIx64 "%8$s %7$s%3$c%4$s %5$s"
1021#define DECFMT "%6$s%2$*1$" PRId64 "%8$s %7$s%3$c%4$s %5$s"
1022#define OCTFMT "%6$s%2$0*1$" PRIo64 "%8$s %7$s%3$c%4$s %5$s"
1023 printf ((radix == radix_hex ? HEXFMT
1024 : (radix == radix_decimal ? DECFMT : OCTFMT)),
1025 digits, syms[cnt].sym.st_value,
1026 class_type_char (elf, ehdr, &syms[cnt].sym), marker,
1027 symstr,
1028 color_mode ? color_address : "",
1029 color,
1030 color_mode ? color_off : "");
1031#undef HEXFMT
1032#undef DECFMT
1033#undef OCTFMT
1034 }
Ulrich Drepperc6b3d0c2012-01-21 18:14:39 -05001035 }
1036
1037 if (color_mode)
1038 fputs_unlocked (color_off, stdout);
1039 putchar_unlocked ('\n');
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001040 }
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001041
1042#ifdef USE_DEMANGLE
1043 free (demangle_buffer);
1044#endif
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001045}
1046
1047
1048static void
Ulrich Drepper2356ba02011-10-03 07:23:07 -04001049show_symbols_posix (Elf *elf, const GElf_Ehdr *ehdr, GElf_Word strndx,
1050 const char *prefix, const char *fullname, GElf_SymX *syms,
1051 size_t nsyms)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001052{
1053 if (prefix != NULL && ! print_file_name)
1054 printf ("%s:\n", fullname);
1055
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001056 int digits = length_map[gelf_getclass (elf) - 1][radix];
1057
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001058#ifdef USE_DEMANGLE
1059 size_t demangle_buffer_len = 0;
1060 char *demangle_buffer = NULL;
1061#endif
1062
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001063 /* Iterate over all symbols. */
1064 for (size_t cnt = 0; cnt < nsyms; ++cnt)
1065 {
Roland McGrath468fe4d2008-12-11 21:00:12 -08001066 char symstrbuf[50];
1067 const char *symstr = sym_name (elf, strndx, syms[cnt].sym.st_name,
1068 symstrbuf, sizeof symstrbuf);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001069
1070 /* Printing entries with a zero-length name makes the output
1071 not very well parseable. Since these entries don't carry
1072 much information we leave them out. */
1073 if (symstr[0] == '\0')
1074 continue;
1075
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001076#ifdef USE_DEMANGLE
Jan Kratochvil7c6e7852014-01-15 21:16:57 +01001077 /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */
1078 if (demangle && symstr[0] == '_' && symstr[1] == 'Z')
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001079 {
1080 int status = -1;
1081 char *dmsymstr = __cxa_demangle (symstr, demangle_buffer,
1082 &demangle_buffer_len, &status);
1083
1084 if (status == 0)
1085 symstr = dmsymstr;
1086 }
1087#endif
1088
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001089 /* If we have to precede the line with the file name. */
1090 if (print_file_name)
1091 {
1092 fputs_unlocked (fullname, stdout);
1093 putchar_unlocked (':');
1094 putchar_unlocked (' ');
1095 }
1096
Mark Wielaardf48eb6b2014-01-23 00:56:41 +01001097 printf ((radix == radix_hex
1098 ? "%s %c%s %0*" PRIx64 " %0*" PRIx64 "\n"
1099 : (radix == radix_decimal
1100 ? "%s %c%s %*" PRId64 " %*" PRId64 "\n"
1101 : "%s %c%s %0*" PRIo64 " %0*" PRIo64 "\n")),
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001102 symstr,
Ulrich Drepper2356ba02011-10-03 07:23:07 -04001103 class_type_char (elf, ehdr, &syms[cnt].sym),
1104 mark_special
1105 ? (GELF_ST_TYPE (syms[cnt].sym.st_info) == STT_TLS
1106 ? "@"
1107 : (GELF_ST_BIND (syms[cnt].sym.st_info) == STB_WEAK
1108 ? "*" : " "))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001109 : "",
1110 digits, syms[cnt].sym.st_value,
1111 digits, syms[cnt].sym.st_size);
1112 }
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001113
1114#ifdef USE_DEMANGLE
1115 free (demangle_buffer);
1116#endif
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001117}
1118
1119
1120/* Maximum size of memory we allocate on the stack. */
1121#define MAX_STACK_ALLOC 65536
1122
Roland McGrathcb6d8652007-08-23 08:10:54 +00001123static int
1124sort_by_address (const void *p1, const void *p2)
1125{
1126 GElf_SymX *s1 = (GElf_SymX *) p1;
1127 GElf_SymX *s2 = (GElf_SymX *) p2;
1128
1129 int result = (s1->sym.st_value < s2->sym.st_value
1130 ? -1 : (s1->sym.st_value == s2->sym.st_value ? 0 : 1));
1131
1132 return reverse_sort ? -result : result;
1133}
1134
1135static Elf_Data *sort_by_name_strtab;
1136
1137static int
1138sort_by_name (const void *p1, const void *p2)
1139{
1140 GElf_SymX *s1 = (GElf_SymX *) p1;
1141 GElf_SymX *s2 = (GElf_SymX *) p2;
1142
1143 const char *n1 = sort_by_name_strtab->d_buf + s1->sym.st_name;
1144 const char *n2 = sort_by_name_strtab->d_buf + s2->sym.st_name;
1145
1146 int result = strcmp (n1, n2);
1147
1148 return reverse_sort ? -result : result;
1149}
1150
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001151static void
1152show_symbols (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, Elf_Scn *xndxscn,
1153 GElf_Shdr *shdr, const char *prefix, const char *fname,
1154 const char *fullname)
1155{
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001156 /* Get the section header string table index. */
1157 size_t shstrndx;
Ulrich Drepperf1894932009-06-13 15:55:42 -07001158 if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001159 error (EXIT_FAILURE, 0,
1160 gettext ("cannot get section header string table index"));
1161
1162 /* The section is that large. */
1163 size_t size = shdr->sh_size;
1164 /* One entry is this large. */
1165 size_t entsize = shdr->sh_entsize;
1166
1167 /* Consistency checks. */
Mark Wielaard309e7122014-12-26 19:36:31 +01001168 if (entsize == 0
Mark Wielaarda2b964c2015-05-08 17:56:32 +02001169 || entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001170 error (0, 0,
Mark Wielaard309e7122014-12-26 19:36:31 +01001171 gettext ("%s: entry size in section %zd `%s' is not what we expect"),
1172 fullname, elf_ndxscn (scn),
1173 elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001174 else if (size % entsize != 0)
1175 error (0, 0,
Mark Wielaard309e7122014-12-26 19:36:31 +01001176 gettext ("%s: size of section %zd `%s' is not multiple of entry size"),
1177 fullname, elf_ndxscn (scn),
1178 elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001179
1180 /* Compute number of entries. Handle buggy entsize values. */
1181 size_t nentries = size / (entsize ?: 1);
1182
1183
1184#define obstack_chunk_alloc xmalloc
1185#define obstack_chunk_free free
1186 struct obstack whereob;
1187 obstack_init (&whereob);
1188
1189 /* Get a DWARF debugging descriptor. It's no problem if this isn't
1190 possible. We just won't print any line number information. */
1191 Dwarf *dbg = NULL;
1192 if (format == format_sysv)
1193 {
1194 dbg = dwarf_begin_elf (ebl->elf, DWARF_C_READ, NULL);
1195 if (dbg != NULL)
1196 {
1197 (void) dwarf_getpubnames (dbg, get_global, NULL, 0);
1198
1199 get_local_names (dbg);
1200 }
1201 }
1202
Mark Wielaardc08079a2015-06-27 22:07:01 +02001203 /* Get the data of the section. */
1204 Elf_Data *data = elf_getdata (scn, NULL);
1205 Elf_Data *xndxdata = elf_getdata (xndxscn, NULL);
1206 if (data == NULL || (xndxscn != NULL && xndxdata == NULL))
1207 INTERNAL_ERROR (fullname);
1208
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001209 /* Allocate the memory.
1210
1211 XXX We can use a dirty trick here. Since GElf_Sym == Elf64_Sym we
1212 can use the data memory instead of copying again if what we read
1213 is a 64 bit file. */
1214 GElf_SymX *sym_mem;
1215 if (nentries * sizeof (GElf_SymX) < MAX_STACK_ALLOC)
1216 sym_mem = (GElf_SymX *) alloca (nentries * sizeof (GElf_SymX));
1217 else
1218 sym_mem = (GElf_SymX *) xmalloc (nentries * sizeof (GElf_SymX));
1219
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001220 /* Iterate over all symbols. */
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001221#ifdef USE_DEMANGLE
1222 size_t demangle_buffer_len = 0;
1223 char *demangle_buffer = NULL;
1224#endif
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001225 int longest_name = 4;
1226 int longest_where = 4;
1227 size_t nentries_used = 0;
1228 for (size_t cnt = 0; cnt < nentries; ++cnt)
1229 {
1230 GElf_Sym *sym = gelf_getsymshndx (data, xndxdata, cnt,
1231 &sym_mem[nentries_used].sym,
1232 &sym_mem[nentries_used].xndx);
1233 if (sym == NULL)
1234 INTERNAL_ERROR (fullname);
1235
1236 /* Filter out administrative symbols without a name and those
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001237 deselected by the user with command line options. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001238 if ((hide_undefined && sym->st_shndx == SHN_UNDEF)
1239 || (hide_defined && sym->st_shndx != SHN_UNDEF)
1240 || (hide_local && GELF_ST_BIND (sym->st_info) == STB_LOCAL))
1241 continue;
1242
1243 sym_mem[nentries_used].where = "";
1244 if (format == format_sysv)
1245 {
1246 const char *symstr = elf_strptr (ebl->elf, shdr->sh_link,
1247 sym->st_name);
Roland McGrath468fe4d2008-12-11 21:00:12 -08001248 if (symstr == NULL)
1249 continue;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001250
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001251#ifdef USE_DEMANGLE
Jan Kratochvil7c6e7852014-01-15 21:16:57 +01001252 /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */
1253 if (demangle && symstr[0] == '_' && symstr[1] == 'Z')
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001254 {
1255 int status = -1;
1256 char *dmsymstr = __cxa_demangle (symstr, demangle_buffer,
1257 &demangle_buffer_len, &status);
1258
1259 if (status == 0)
1260 symstr = dmsymstr;
1261 }
1262#endif
1263
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001264 longest_name = MAX ((size_t) longest_name, strlen (symstr));
1265
1266 if (sym->st_shndx != SHN_UNDEF
1267 && GELF_ST_BIND (sym->st_info) != STB_LOCAL
1268 && global_root != NULL)
1269 {
1270 Dwarf_Global fake = { .name = symstr };
1271 Dwarf_Global **found = tfind (&fake, &global_root,
1272 global_compare);
1273 if (found != NULL)
1274 {
1275 Dwarf_Die die_mem;
1276 Dwarf_Die *die = dwarf_offdie (dbg, (*found)->die_offset,
1277 &die_mem);
1278
1279 Dwarf_Die cudie_mem;
1280 Dwarf_Die *cudie = NULL;
1281
1282 Dwarf_Addr lowpc;
1283 Dwarf_Addr highpc;
1284 if (die != NULL
1285 && dwarf_lowpc (die, &lowpc) == 0
1286 && lowpc <= sym->st_value
1287 && dwarf_highpc (die, &highpc) == 0
1288 && highpc > sym->st_value)
1289 cudie = dwarf_offdie (dbg, (*found)->cu_offset,
1290 &cudie_mem);
1291 if (cudie != NULL)
1292 {
1293 Dwarf_Line *line = dwarf_getsrc_die (cudie,
1294 sym->st_value);
1295 if (line != NULL)
1296 {
1297 /* We found the line. */
1298 int lineno;
1299 (void) dwarf_lineno (line, &lineno);
Mark Wielaarda3344c72015-05-06 12:45:49 +02001300 const char *file = dwarf_linesrc (line, NULL, NULL);
1301 file = (file != NULL) ? basename (file) : "???";
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001302 int n;
Mark Wielaarda3344c72015-05-06 12:45:49 +02001303 n = obstack_printf (&whereob, "%s:%d%c", file,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001304 lineno, '\0');
1305 sym_mem[nentries_used].where
1306 = obstack_finish (&whereob);
1307
1308 /* The return value of obstack_print included the
1309 NUL byte, so subtract one. */
1310 if (--n > (int) longest_where)
1311 longest_where = (size_t) n;
1312 }
1313 }
1314 }
1315 }
1316
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001317 /* Try to find the symbol among the local symbols. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001318 if (sym_mem[nentries_used].where[0] == '\0')
1319 {
1320 struct local_name fake =
1321 {
1322 .name = symstr,
1323 .lowpc = sym->st_value,
1324 .highpc = sym->st_value,
1325 };
1326 struct local_name **found = tfind (&fake, &local_root,
1327 local_compare);
1328 if (found != NULL)
1329 {
1330 /* We found the line. */
1331 int n = obstack_printf (&whereob, "%s:%" PRIu64 "%c",
1332 basename ((*found)->file),
1333 (*found)->lineno,
1334 '\0');
1335 sym_mem[nentries_used].where = obstack_finish (&whereob);
1336
1337 /* The return value of obstack_print included the
1338 NUL byte, so subtract one. */
1339 if (--n > (int) longest_where)
1340 longest_where = (size_t) n;
1341 }
1342 }
1343 }
1344
1345 /* We use this entry. */
1346 ++nentries_used;
1347 }
Ulrich Drepperb4a16cf2011-10-02 08:33:19 -04001348#ifdef USE_DEMANGLE
1349 free (demangle_buffer);
1350#endif
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001351 /* Now we know the exact number. */
1352 nentries = nentries_used;
1353
1354 /* Sort the entries according to the users wishes. */
1355 if (sort == sort_name)
Roland McGrathcb6d8652007-08-23 08:10:54 +00001356 {
1357 sort_by_name_strtab = elf_getdata (elf_getscn (ebl->elf, shdr->sh_link),
1358 NULL);
1359 qsort (sym_mem, nentries, sizeof (GElf_SymX), sort_by_name);
1360 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001361 else if (sort == sort_numeric)
1362 qsort (sym_mem, nentries, sizeof (GElf_SymX), sort_by_address);
1363
1364 /* Finally print according to the users selection. */
1365 switch (format)
1366 {
1367 case format_sysv:
Marek Polacekc8920de2011-05-12 12:08:21 +02001368 show_symbols_sysv (ebl, shdr->sh_link, fullname, sym_mem, nentries,
1369 longest_name, longest_where);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001370 break;
1371
1372 case format_bsd:
Ulrich Drepper2356ba02011-10-03 07:23:07 -04001373 show_symbols_bsd (ebl->elf, ehdr, shdr->sh_link, prefix, fname, fullname,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001374 sym_mem, nentries);
1375 break;
1376
1377 case format_posix:
1378 default:
1379 assert (format == format_posix);
Ulrich Drepper2356ba02011-10-03 07:23:07 -04001380 show_symbols_posix (ebl->elf, ehdr, shdr->sh_link, prefix, fullname,
1381 sym_mem, nentries);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001382 break;
1383 }
1384
1385 /* Free all memory. */
Mark Wielaard9beaa942015-06-18 11:07:41 +02001386 if (nentries * sizeof (sym_mem[0]) >= MAX_STACK_ALLOC)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001387 free (sym_mem);
1388
1389 obstack_free (&whereob, NULL);
1390
1391 if (dbg != NULL)
1392 {
1393 tdestroy (global_root, free);
1394 global_root = NULL;
1395
1396 tdestroy (local_root, free);
1397 local_root = NULL;
1398
1399 (void) dwarf_end (dbg);
1400 }
1401}
1402
1403
1404static int
1405handle_elf (Elf *elf, const char *prefix, const char *fname,
1406 const char *suffix)
1407{
1408 size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
1409 size_t suffix_len = suffix == NULL ? 0 : strlen (suffix);
1410 size_t fname_len = strlen (fname) + 1;
1411 char fullname[prefix_len + 1 + fname_len + suffix_len];
1412 char *cp = fullname;
1413 Elf_Scn *scn = NULL;
1414 int any = 0;
1415 int result = 0;
1416 GElf_Ehdr ehdr_mem;
1417 GElf_Ehdr *ehdr;
1418 Ebl *ebl;
1419
1420 /* Get the backend for this object file type. */
1421 ebl = ebl_openbackend (elf);
1422
1423 /* We need the ELF header in a few places. */
1424 ehdr = gelf_getehdr (elf, &ehdr_mem);
1425 if (ehdr == NULL)
1426 INTERNAL_ERROR (fullname);
1427
1428 /* If we are asked to print the dynamic symbol table and this is
1429 executable or dynamic executable, fail. */
1430 if (symsec_type == SHT_DYNSYM
1431 && ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
1432 {
1433 /* XXX Add machine specific object file types. */
1434 error (0, 0, gettext ("%s%s%s%s: Invalid operation"),
1435 prefix ?: "", prefix ? "(" : "", fname, prefix ? ")" : "");
1436 result = 1;
1437 goto out;
1438 }
1439
1440 /* Create the full name of the file. */
1441 if (prefix != NULL)
1442 cp = mempcpy (cp, prefix, prefix_len);
1443 cp = mempcpy (cp, fname, fname_len);
1444 if (suffix != NULL)
1445 memcpy (cp - 1, suffix, suffix_len + 1);
1446
1447 /* Find the symbol table.
1448
1449 XXX Can there be more than one? Do we print all? Currently we do. */
1450 while ((scn = elf_nextscn (elf, scn)) != NULL)
1451 {
1452 GElf_Shdr shdr_mem;
1453 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1454
1455 if (shdr == NULL)
1456 INTERNAL_ERROR (fullname);
1457
1458 if (shdr->sh_type == symsec_type)
1459 {
1460 Elf_Scn *xndxscn = NULL;
1461
1462 /* We have a symbol table. First make sure we remember this. */
1463 any = 1;
1464
1465 /* Look for an extended section index table for this section. */
1466 if (symsec_type == SHT_SYMTAB)
1467 {
1468 size_t scnndx = elf_ndxscn (scn);
1469
1470 while ((xndxscn = elf_nextscn (elf, xndxscn)) != NULL)
1471 {
1472 GElf_Shdr xndxshdr_mem;
1473 GElf_Shdr *xndxshdr = gelf_getshdr (xndxscn, &xndxshdr_mem);
1474
1475 if (xndxshdr == NULL)
1476 INTERNAL_ERROR (fullname);
1477
1478 if (xndxshdr->sh_type == SHT_SYMTAB_SHNDX
1479 && xndxshdr->sh_link == scnndx)
1480 break;
1481 }
1482 }
1483
1484 show_symbols (ebl, ehdr, scn, xndxscn, shdr, prefix, fname,
1485 fullname);
1486 }
1487 }
1488
1489 if (! any)
1490 {
1491 error (0, 0, gettext ("%s%s%s: no symbols"),
1492 prefix ?: "", prefix ? ":" : "", fname);
1493 result = 1;
1494 }
1495
1496 out:
1497 /* Close the ELF backend library descriptor. */
1498 ebl_closebackend (ebl);
1499
1500 return result;
1501}
Ulrich Drepper3cbdd382008-01-02 17:44:39 +00001502
1503
1504#include "debugpred.h"