Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Print symbol information from ELF file in human-readable form. |
| 2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Red Hat, Inc. |
| 3 | Written by Ulrich Drepper <drepper@redhat.com>, 2000. |
| 4 | |
| 5 | This program is Open Source software; you can redistribute it and/or |
| 6 | modify it under the terms of the Open Software License version 1.0 as |
| 7 | published by the Open Source Initiative. |
| 8 | |
| 9 | You should have received a copy of the Open Software License along |
| 10 | with this program; if not, you may obtain a copy of the Open Software |
| 11 | License version 1.0 from http://www.opensource.org/licenses/osl.php or |
| 12 | by writing the Open Source Initiative c/o Lawrence Rosen, Esq., |
| 13 | 3001 King Ranch Road, Ukiah, CA 95482. */ |
| 14 | |
| 15 | #ifdef HAVE_CONFIG_H |
| 16 | # include <config.h> |
| 17 | #endif |
| 18 | |
| 19 | #include <ar.h> |
| 20 | #include <argp.h> |
| 21 | #include <assert.h> |
| 22 | #include <ctype.h> |
| 23 | #include <dwarf.h> |
| 24 | #include <errno.h> |
| 25 | #include <error.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <gelf.h> |
| 28 | #include <inttypes.h> |
| 29 | #include <libdw.h> |
| 30 | #include <libintl.h> |
| 31 | #include <locale.h> |
| 32 | #include <mcheck.h> |
| 33 | #include <obstack.h> |
| 34 | #include <search.h> |
| 35 | #include <stdbool.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdio_ext.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <unistd.h> |
| 41 | #include <sys/param.h> |
| 42 | |
| 43 | #include <system.h> |
| 44 | #include "../libebl/libeblP.h" |
| 45 | |
| 46 | |
| 47 | /* Name and version of program. */ |
| 48 | static void print_version (FILE *stream, struct argp_state *state); |
| 49 | void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version; |
| 50 | |
| 51 | /* Bug report address. */ |
| 52 | const char *argp_program_bug_address = PACKAGE_BUGREPORT; |
| 53 | |
| 54 | |
| 55 | /* Values for the parameters which have no short form. */ |
| 56 | #define OPT_DEFINED 0x100 |
| 57 | #define OPT_MARK_WEAK 0x101 |
| 58 | |
| 59 | /* Definitions of arguments for argp functions. */ |
| 60 | static const struct argp_option options[] = |
| 61 | { |
| 62 | { NULL, 0, NULL, 0, N_("Output selection:"), 0 }, |
| 63 | { "debug-syms", 'a', NULL, 0, N_("Display debugger-only symbols"), 0 }, |
| 64 | { "defined-only", OPT_DEFINED, NULL, 0, N_("Display only defined symbols"), |
| 65 | 0 }, |
| 66 | { "dynamic", 'D', NULL, 0, |
| 67 | N_("Display dynamic symbols instead of normal symbols"), 0 }, |
| 68 | { "extern-only", 'g', NULL, 0, N_("Display only external symbols"), 0 }, |
| 69 | { "undefined-only", 'u', NULL, 0, N_("Display only undefined symbols"), 0 }, |
| 70 | { "print-armap", 's', NULL, 0, |
| 71 | N_("Include index for symbols from archive members"), 0 }, |
| 72 | |
| 73 | { NULL, 0, NULL, 0, N_("Output format:"), 0 }, |
| 74 | { "print-file-name", 'A', NULL, 0, |
| 75 | N_("Print name of the input file before every symbol"), 0 }, |
| 76 | { NULL, 'o', NULL, OPTION_HIDDEN, "Same as -A", 0 }, |
| 77 | { "format", 'f', "FORMAT", 0, |
| 78 | N_("Use the output format FORMAT. FORMAT can be `bsd', `sysv' or `posix'. The default is `sysv'"), |
| 79 | 0 }, |
| 80 | { NULL, 'B', NULL, 0, N_("Same as --format=bsd"), 0 }, |
| 81 | { "portability", 'P', NULL, 0, N_("Same as --format=posix"), 0 }, |
| 82 | { "radix", 't', "RADIX", 0, N_("Use RADIX for printing symbol values"), 0 }, |
| 83 | { "mark-weak", OPT_MARK_WEAK, NULL, 0, N_("Mark weak symbols"), 0 }, |
| 84 | { "print-size", 'S', NULL, 0, N_("Print size of defined symbols"), 0 }, |
| 85 | |
| 86 | { NULL, 0, NULL, 0, N_("Output options:"), 0 }, |
| 87 | { "numeric-sort", 'n', NULL, 0, N_("Sort symbols numerically by address"), |
| 88 | 0 }, |
| 89 | { "no-sort", 'p', NULL, 0, N_("Do not sort the symbols"), 0 }, |
| 90 | { "reverse-sort", 'r', NULL, 0, N_("Reverse the sense of the sort"), 0 }, |
| 91 | { NULL, 0, NULL, 0, N_("Miscellaneous:"), 0 }, |
| 92 | { NULL, 0, NULL, 0, NULL, 0 } |
| 93 | }; |
| 94 | |
| 95 | /* Short description of program. */ |
| 96 | static const char doc[] = N_("List symbols from FILEs (a.out by default)."); |
| 97 | |
| 98 | /* Strings for arguments in help texts. */ |
| 99 | static const char args_doc[] = N_("[FILE...]"); |
| 100 | |
| 101 | /* Prototype for option handler. */ |
| 102 | static error_t parse_opt (int key, char *arg, struct argp_state *state); |
| 103 | |
| 104 | /* Data structure to communicate with argp functions. */ |
| 105 | static struct argp argp = |
| 106 | { |
| 107 | options, parse_opt, args_doc, doc, NULL, NULL, NULL |
| 108 | }; |
| 109 | |
| 110 | |
| 111 | /* Print symbols in file named FNAME. */ |
| 112 | static int process_file (const char *fname, bool more_than_one); |
| 113 | |
| 114 | /* Handle content of archive. */ |
| 115 | static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname, |
| 116 | const char *suffix); |
| 117 | |
| 118 | /* Handle ELF file. */ |
| 119 | static int handle_elf (Elf *elf, const char *prefix, const char *fname, |
| 120 | const char *suffix); |
| 121 | |
| 122 | |
| 123 | #define INTERNAL_ERROR(fname) \ |
| 124 | error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s-%s): %s"), \ |
| 125 | fname, __LINE__, VERSION, __DATE__, elf_errmsg (-1)) |
| 126 | |
| 127 | |
| 128 | /* Internal representation of symbols. */ |
| 129 | typedef struct GElf_SymX |
| 130 | { |
| 131 | GElf_Sym sym; |
| 132 | Elf32_Word xndx; |
| 133 | char *where; |
| 134 | } GElf_SymX; |
| 135 | |
| 136 | |
| 137 | /* User-selectable options. */ |
| 138 | |
| 139 | /* The selected output format. */ |
| 140 | static enum |
| 141 | { |
| 142 | format_sysv = 0, |
| 143 | format_bsd, |
| 144 | format_posix |
| 145 | } format; |
| 146 | |
| 147 | /* Print defined, undefined, or both? */ |
| 148 | static bool hide_undefined; |
| 149 | static bool hide_defined; |
| 150 | |
| 151 | /* Print local symbols also? */ |
| 152 | static bool hide_local; |
| 153 | |
| 154 | /* Nonzero if full filename should precede every symbol. */ |
| 155 | static bool print_file_name; |
| 156 | |
| 157 | /* If true print size of defined symbols in BSD format. */ |
| 158 | static bool print_size; |
| 159 | |
| 160 | /* If true print archive index. */ |
| 161 | static bool print_armap; |
| 162 | |
| 163 | /* If true reverse sorting. */ |
| 164 | static bool reverse_sort; |
| 165 | |
| 166 | /* Type of the section we are printing. */ |
| 167 | static GElf_Word symsec_type = SHT_SYMTAB; |
| 168 | |
| 169 | /* Sorting selection. */ |
| 170 | static enum |
| 171 | { |
| 172 | sort_name = 0, |
| 173 | sort_numeric, |
| 174 | sort_nosort |
| 175 | } sort; |
| 176 | |
| 177 | /* Radix for printed numbers. */ |
| 178 | static enum |
| 179 | { |
| 180 | radix_hex = 0, |
| 181 | radix_decimal, |
| 182 | radix_octal |
| 183 | } radix; |
| 184 | |
| 185 | /* If nonzero weak symbols are distinguished from global symbols by adding |
| 186 | a `*' after the identifying letter for the symbol class and type. */ |
| 187 | static bool mark_weak; |
| 188 | |
| 189 | |
| 190 | int |
| 191 | main (int argc, char *argv[]) |
| 192 | { |
| 193 | int remaining; |
| 194 | int result = 0; |
| 195 | |
| 196 | /* Make memory leak detection possible. */ |
| 197 | mtrace (); |
| 198 | |
| 199 | /* We use no threads here which can interfere with handling a stream. */ |
| 200 | (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER); |
| 201 | (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER); |
| 202 | (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER); |
| 203 | |
| 204 | /* Set locale. */ |
| 205 | (void) setlocale (LC_ALL, ""); |
| 206 | |
| 207 | /* Make sure the message catalog can be found. */ |
| 208 | (void) bindtextdomain (PACKAGE, LOCALEDIR); |
| 209 | |
| 210 | /* Initialize the message catalog. */ |
| 211 | (void) textdomain (PACKAGE); |
| 212 | |
| 213 | /* Parse and process arguments. */ |
| 214 | (void) argp_parse (&argp, argc, argv, 0, &remaining, NULL); |
| 215 | |
| 216 | /* Tell the library which version we are expecting. */ |
| 217 | (void) elf_version (EV_CURRENT); |
| 218 | |
| 219 | if (remaining == argc) |
| 220 | /* The user didn't specify a name so we use a.out. */ |
| 221 | result = process_file ("a.out", false); |
| 222 | else |
| 223 | { |
| 224 | /* Process all the remaining files. */ |
| 225 | const bool more_than_one = remaining + 1 < argc; |
| 226 | |
| 227 | do |
| 228 | result |= process_file (argv[remaining], more_than_one); |
| 229 | while (++remaining < argc); |
| 230 | } |
| 231 | |
| 232 | return result; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | /* Print the version information. */ |
| 237 | static void |
| 238 | print_version (FILE *stream, struct argp_state *state __attribute__ ((unused))) |
| 239 | { |
| 240 | fprintf (stream, "nm (%s) %s\n", PACKAGE_NAME, VERSION); |
| 241 | fprintf (stream, gettext ("\ |
| 242 | Copyright (C) %s Red Hat, Inc.\n\ |
| 243 | This is free software; see the source for copying conditions. There is NO\n\ |
| 244 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ |
| 245 | "), "2005"); |
| 246 | fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper"); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | /* Handle program arguments. */ |
| 251 | static error_t |
| 252 | parse_opt (int key, char *arg, |
| 253 | struct argp_state *state __attribute__ ((unused))) |
| 254 | { |
| 255 | switch (key) |
| 256 | { |
| 257 | case 'a': |
| 258 | /* XXX */ |
| 259 | break; |
| 260 | |
| 261 | case 'f': |
| 262 | if (strcmp (arg, "bsd") == 0) |
| 263 | format = format_bsd; |
| 264 | else if (strcmp (arg, "posix") == 0) |
| 265 | format = format_posix; |
| 266 | else |
| 267 | /* Be bug compatible. The BFD implementation also defaulted to |
| 268 | using the SysV format if nothing else matches. */ |
| 269 | format = format_sysv; |
| 270 | break; |
| 271 | |
| 272 | case 'g': |
| 273 | hide_local = true; |
| 274 | break; |
| 275 | |
| 276 | case 'n': |
| 277 | sort = sort_numeric; |
| 278 | break; |
| 279 | |
| 280 | case 'p': |
| 281 | sort = sort_nosort; |
| 282 | break; |
| 283 | |
| 284 | case 't': |
| 285 | if (strcmp (arg, "10") == 0 || strcmp (arg, "d") == 0) |
| 286 | radix = radix_decimal; |
| 287 | else if (strcmp (arg, "8") == 0 || strcmp (arg, "o") == 0) |
| 288 | radix = radix_octal; |
| 289 | else |
| 290 | radix = radix_hex; |
| 291 | break; |
| 292 | |
| 293 | case 'u': |
| 294 | hide_undefined = false; |
| 295 | hide_defined = true; |
| 296 | break; |
| 297 | |
| 298 | case 'A': |
| 299 | case 'o': |
| 300 | print_file_name = true; |
| 301 | break; |
| 302 | |
| 303 | case 'B': |
| 304 | format = format_bsd; |
| 305 | break; |
| 306 | |
| 307 | case 'D': |
| 308 | symsec_type = SHT_DYNSYM; |
| 309 | break; |
| 310 | |
| 311 | case 'P': |
| 312 | format = format_posix; |
| 313 | break; |
| 314 | |
| 315 | case OPT_DEFINED: |
| 316 | hide_undefined = true; |
| 317 | hide_defined = false; |
| 318 | break; |
| 319 | |
| 320 | case OPT_MARK_WEAK: |
| 321 | mark_weak = true; |
| 322 | break; |
| 323 | |
| 324 | case 'S': |
| 325 | print_size = true; |
| 326 | break; |
| 327 | |
| 328 | case 's': |
| 329 | print_armap = true; |
| 330 | break; |
| 331 | |
| 332 | case 'r': |
| 333 | reverse_sort = true; |
| 334 | break; |
| 335 | |
| 336 | default: |
| 337 | return ARGP_ERR_UNKNOWN; |
| 338 | } |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /* Open the file and determine the type. */ |
| 344 | static int |
| 345 | process_file (const char *fname, bool more_than_one) |
| 346 | { |
| 347 | /* Open the file. */ |
| 348 | int fd = open (fname, O_RDONLY); |
| 349 | if (fd == -1) |
| 350 | { |
| 351 | error (0, errno, gettext ("cannot open '%s'"), fname); |
| 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | /* Now get the ELF descriptor. */ |
| 356 | Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL); |
| 357 | if (elf != NULL) |
| 358 | { |
| 359 | if (elf_kind (elf) == ELF_K_ELF) |
| 360 | { |
| 361 | int result = handle_elf (elf, more_than_one ? "" : NULL, |
| 362 | fname, NULL); |
| 363 | |
| 364 | if (elf_end (elf) != 0) |
| 365 | INTERNAL_ERROR (fname); |
| 366 | |
| 367 | if (close (fd) != 0) |
Ulrich Drepper | d112ef8 | 2005-09-03 21:31:27 +0000 | [diff] [blame^] | 368 | error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 369 | |
| 370 | return result; |
| 371 | } |
| 372 | else if (elf_kind (elf) == ELF_K_AR) |
| 373 | { |
| 374 | int result = handle_ar (fd, elf, NULL, fname, NULL); |
| 375 | |
| 376 | if (elf_end (elf) != 0) |
| 377 | INTERNAL_ERROR (fname); |
| 378 | |
| 379 | if (close (fd) != 0) |
Ulrich Drepper | d112ef8 | 2005-09-03 21:31:27 +0000 | [diff] [blame^] | 380 | error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 381 | |
| 382 | return result; |
| 383 | } |
| 384 | |
| 385 | /* We cannot handle this type. Close the descriptor anyway. */ |
| 386 | if (elf_end (elf) != 0) |
| 387 | INTERNAL_ERROR (fname); |
| 388 | } |
| 389 | |
| 390 | error (0, 0, gettext ("%s: File format not recognized"), fname); |
| 391 | |
| 392 | return 1; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | static int |
| 397 | handle_ar (int fd, Elf *elf, const char *prefix, const char *fname, |
| 398 | const char *suffix) |
| 399 | { |
| 400 | size_t fname_len = strlen (fname) + 1; |
| 401 | size_t prefix_len = prefix != NULL ? strlen (prefix) : 0; |
| 402 | char new_prefix[prefix_len + fname_len + 2]; |
| 403 | size_t suffix_len = suffix != NULL ? strlen (suffix) : 0; |
| 404 | char new_suffix[suffix_len + 2]; |
| 405 | Elf *subelf; |
| 406 | Elf_Cmd cmd = ELF_C_READ_MMAP; |
| 407 | int result = 0; |
| 408 | |
| 409 | char *cp = new_prefix; |
| 410 | if (prefix != NULL) |
| 411 | cp = stpcpy (cp, prefix); |
| 412 | cp = stpcpy (cp, fname); |
| 413 | stpcpy (cp, "["); |
| 414 | |
| 415 | cp = new_suffix; |
| 416 | if (suffix != NULL) |
| 417 | cp = stpcpy (cp, suffix); |
| 418 | stpcpy (cp, "]"); |
| 419 | |
| 420 | /* First print the archive index if this is wanted. */ |
| 421 | if (print_armap) |
| 422 | { |
| 423 | Elf_Arsym *arsym = elf_getarsym (elf, NULL); |
| 424 | |
| 425 | if (arsym != NULL) |
| 426 | { |
| 427 | Elf_Arhdr *arhdr = NULL; |
| 428 | size_t arhdr_off = 0; /* Note: 0 is no valid offset. */ |
| 429 | |
| 430 | puts (gettext("\nArchive index:")); |
| 431 | |
| 432 | while (arsym->as_off != 0) |
| 433 | { |
| 434 | if (arhdr_off != arsym->as_off |
| 435 | && (elf_rand (elf, arsym->as_off) != arsym->as_off |
| 436 | || (subelf = elf_begin (fd, cmd, elf)) == NULL |
| 437 | || (arhdr = elf_getarhdr (subelf)) == NULL)) |
| 438 | { |
| 439 | error (0, 0, gettext ("invalid offset %zu for symbol %s"), |
| 440 | arsym->as_off, arsym->as_name); |
| 441 | continue; |
| 442 | } |
| 443 | |
| 444 | printf (gettext ("%s in %s\n"), arsym->as_name, arhdr->ar_name); |
| 445 | |
| 446 | ++arsym; |
| 447 | } |
| 448 | |
| 449 | if (elf_rand (elf, SARMAG) != SARMAG) |
| 450 | { |
| 451 | error (0, 0, |
| 452 | gettext ("cannot reset archive offset to beginning")); |
| 453 | return 1; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /* Process all the files contained in the archive. */ |
| 459 | while ((subelf = elf_begin (fd, cmd, elf)) != NULL) |
| 460 | { |
| 461 | /* The the header for this element. */ |
| 462 | Elf_Arhdr *arhdr = elf_getarhdr (subelf); |
| 463 | |
| 464 | /* Skip over the index entries. */ |
| 465 | if (strcmp (arhdr->ar_name, "/") != 0 |
| 466 | && strcmp (arhdr->ar_name, "//") != 0) |
| 467 | { |
| 468 | if (elf_kind (subelf) == ELF_K_ELF) |
| 469 | result |= handle_elf (subelf, new_prefix, arhdr->ar_name, |
| 470 | new_suffix); |
| 471 | else if (elf_kind (subelf) == ELF_K_AR) |
| 472 | result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name, |
| 473 | new_suffix); |
| 474 | else |
| 475 | { |
| 476 | error (0, 0, gettext ("%s%s%s: file format not recognized"), |
| 477 | new_prefix, arhdr->ar_name, new_suffix); |
| 478 | result = 1; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /* Get next archive element. */ |
| 483 | cmd = elf_next (subelf); |
| 484 | if (elf_end (subelf) != 0) |
| 485 | INTERNAL_ERROR (fname); |
| 486 | } |
| 487 | |
| 488 | return result; |
| 489 | } |
| 490 | |
| 491 | |
| 492 | /* Mapping of radix and binary class to length. */ |
| 493 | static const int length_map[2][3] = |
| 494 | { |
| 495 | [ELFCLASS32 - 1] = |
| 496 | { |
| 497 | [radix_hex] = 8, |
| 498 | [radix_decimal] = 10, |
| 499 | [radix_octal] = 11 |
| 500 | }, |
| 501 | [ELFCLASS64 - 1] = |
| 502 | { |
| 503 | [radix_hex] = 16, |
| 504 | [radix_decimal] = 20, |
| 505 | [radix_octal] = 22 |
| 506 | } |
| 507 | }; |
| 508 | |
| 509 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 510 | static int |
| 511 | global_compare (const void *p1, const void *p2) |
| 512 | { |
| 513 | const Dwarf_Global *g1 = (const Dwarf_Global *) p1; |
| 514 | const Dwarf_Global *g2 = (const Dwarf_Global *) p2; |
| 515 | |
| 516 | return strcmp (g1->name, g2->name); |
| 517 | } |
| 518 | |
| 519 | |
| 520 | static void *global_root; |
| 521 | |
| 522 | |
| 523 | static int |
| 524 | get_global (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global, |
| 525 | void *arg __attribute__ ((unused))) |
| 526 | { |
| 527 | tsearch (memcpy (xmalloc (sizeof (Dwarf_Global)), global, |
| 528 | sizeof (Dwarf_Global)), |
| 529 | &global_root, global_compare); |
| 530 | |
| 531 | return DWARF_CB_OK; |
| 532 | } |
| 533 | |
| 534 | |
| 535 | struct local_name |
| 536 | { |
| 537 | const char *name; |
| 538 | const char *file; |
| 539 | Dwarf_Word lineno; |
| 540 | Dwarf_Addr lowpc; |
| 541 | Dwarf_Addr highpc; |
| 542 | }; |
| 543 | |
| 544 | |
| 545 | static int |
| 546 | local_compare (const void *p1, const void *p2) |
| 547 | { |
| 548 | struct local_name *g1 = (struct local_name *) p1; |
| 549 | struct local_name *g2 = (struct local_name *) p2; |
| 550 | int result; |
| 551 | |
| 552 | result = strcmp (g1->name, g2->name); |
| 553 | if (result == 0) |
| 554 | { |
| 555 | if (g1->lowpc <= g2->lowpc && g1->highpc >= g2->highpc) |
| 556 | { |
| 557 | /* g2 is contained in g1. Update the data. */ |
| 558 | g2->lowpc = g1->lowpc; |
| 559 | g2->highpc = g1->highpc; |
| 560 | result = 0; |
| 561 | } |
| 562 | else if (g2->lowpc <= g1->lowpc && g2->highpc >= g1->highpc) |
| 563 | { |
| 564 | /* g1 is contained in g2. Update the data. */ |
| 565 | g1->lowpc = g2->lowpc; |
| 566 | g1->highpc = g2->highpc; |
| 567 | result = 0; |
| 568 | } |
| 569 | else |
| 570 | result = g1->lowpc < g2->lowpc ? -1 : 1; |
| 571 | } |
| 572 | |
| 573 | return result; |
| 574 | } |
| 575 | |
| 576 | |
| 577 | static int |
| 578 | get_var_range (Dwarf_Die *die, Dwarf_Word *lowpc, Dwarf_Word *highpc) |
| 579 | { |
| 580 | Dwarf_Attribute locattr_mem; |
| 581 | Dwarf_Attribute *locattr = dwarf_attr (die, DW_AT_location, &locattr_mem); |
| 582 | if (locattr == NULL) |
| 583 | return 1; |
| 584 | |
| 585 | Dwarf_Loc *loc; |
| 586 | size_t nloc; |
| 587 | if (dwarf_getloclist (locattr, &loc, &nloc) != 0) |
| 588 | return 1; |
| 589 | |
| 590 | /* Interpret the location expressions. */ |
| 591 | // XXX For now just the simple one: |
| 592 | if (nloc == 1 && loc[0].atom == DW_OP_addr) |
| 593 | { |
| 594 | *lowpc = *highpc = loc[0].number; |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | return 1; |
| 599 | } |
| 600 | |
| 601 | |
| 602 | |
| 603 | static void *local_root; |
| 604 | |
| 605 | |
| 606 | static void |
| 607 | get_local_names (Dwarf *dbg) |
| 608 | { |
| 609 | Dwarf_Off offset = 0; |
| 610 | Dwarf_Off old_offset; |
| 611 | size_t hsize; |
| 612 | |
| 613 | while (dwarf_nextcu (dbg, old_offset = offset, &offset, &hsize, NULL, NULL, |
| 614 | NULL) == 0) |
| 615 | { |
| 616 | Dwarf_Die cudie_mem; |
| 617 | Dwarf_Die *cudie = dwarf_offdie (dbg, old_offset + hsize, &cudie_mem); |
| 618 | |
| 619 | /* If we cannot get the CU DIE there is no need to go on with |
| 620 | this CU. */ |
| 621 | if (cudie == NULL) |
| 622 | continue; |
| 623 | /* This better be a CU DIE. */ |
| 624 | if (dwarf_tag (cudie) != DW_TAG_compile_unit) |
| 625 | continue; |
| 626 | |
| 627 | /* Get the line information. */ |
| 628 | Dwarf_Files *files; |
| 629 | size_t nfiles; |
| 630 | if (dwarf_getsrcfiles (cudie, &files, &nfiles) != 0) |
| 631 | continue; |
| 632 | |
| 633 | Dwarf_Die die_mem; |
| 634 | Dwarf_Die *die = &die_mem; |
| 635 | if (dwarf_child (cudie, die) == 0) |
| 636 | /* Iterate over all immediate children of the CU DIE. */ |
| 637 | do |
| 638 | { |
| 639 | int tag = dwarf_tag (die); |
| 640 | if (tag != DW_TAG_subprogram && tag != DW_TAG_variable) |
| 641 | continue; |
| 642 | |
| 643 | /* We are interested in five attributes: name, decl_file, |
| 644 | decl_line, low_pc, and high_pc. */ |
| 645 | Dwarf_Attribute attr_mem; |
| 646 | Dwarf_Attribute *attr = dwarf_attr (die, DW_AT_name, &attr_mem); |
| 647 | const char *name = dwarf_formstring (attr); |
| 648 | if (name == NULL) |
| 649 | continue; |
| 650 | |
| 651 | Dwarf_Word fileidx; |
| 652 | attr = dwarf_attr (die, DW_AT_decl_file, &attr_mem); |
| 653 | if (dwarf_formudata (attr, &fileidx) != 0 || fileidx >= nfiles) |
| 654 | continue; |
| 655 | |
| 656 | Dwarf_Word lineno; |
| 657 | attr = dwarf_attr (die, DW_AT_decl_line, &attr_mem); |
| 658 | if (dwarf_formudata (attr, &lineno) != 0 || lineno == 0) |
| 659 | continue; |
| 660 | |
| 661 | Dwarf_Addr lowpc; |
| 662 | Dwarf_Addr highpc; |
| 663 | if (tag == DW_TAG_subprogram) |
| 664 | { |
| 665 | if (dwarf_lowpc (die, &lowpc) != 0 |
| 666 | || dwarf_highpc (die, &highpc) != 0) |
| 667 | continue; |
| 668 | } |
| 669 | else |
| 670 | { |
| 671 | if (get_var_range (die, &lowpc, &highpc) != 0) |
| 672 | continue; |
| 673 | } |
| 674 | |
| 675 | /* We have all the information. Create a record. */ |
| 676 | struct local_name *newp |
| 677 | = (struct local_name *) xmalloc (sizeof (*newp)); |
| 678 | newp->name = name; |
| 679 | newp->file = dwarf_filesrc (files, fileidx, NULL, NULL); |
| 680 | newp->lineno = lineno; |
| 681 | newp->lowpc = lowpc; |
| 682 | newp->highpc = highpc; |
| 683 | |
| 684 | /* Since we cannot deallocate individual memory we do not test |
| 685 | for duplicates in the tree. This should not happen anyway. */ |
| 686 | if (tsearch (newp, &local_root, local_compare) == NULL) |
| 687 | error (EXIT_FAILURE, errno, |
| 688 | gettext ("cannot create search tree")); |
| 689 | } |
| 690 | while (dwarf_siblingof (die, die) == 0); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | |
| 695 | /* Show symbols in SysV format. */ |
| 696 | static void |
| 697 | show_symbols_sysv (Ebl *ebl, GElf_Word strndx, |
| 698 | const char *prefix, const char *fname, const char *fullname, |
| 699 | GElf_SymX *syms, size_t nsyms, int longest_name, |
| 700 | int longest_where) |
| 701 | { |
| 702 | size_t shnum; |
| 703 | if (elf_getshnum (ebl->elf, &shnum) < 0) |
| 704 | INTERNAL_ERROR (fullname); |
| 705 | |
| 706 | bool scnnames_malloced = shnum * sizeof (const char *) > 128 * 1024; |
| 707 | const char **scnnames; |
| 708 | if (scnnames_malloced) |
| 709 | scnnames = (const char **) xmalloc (sizeof (const char *) * shnum); |
| 710 | else |
| 711 | scnnames = (const char **) alloca (sizeof (const char *) * shnum); |
| 712 | /* Get the section header string table index. */ |
| 713 | size_t shstrndx; |
| 714 | if (elf_getshstrndx (ebl->elf, &shstrndx) < 0) |
| 715 | error (EXIT_FAILURE, 0, |
| 716 | gettext ("cannot get section header string table index")); |
| 717 | |
| 718 | /* Cache the section names. */ |
| 719 | Elf_Scn *scn = NULL; |
| 720 | size_t cnt = 1; |
| 721 | while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) |
| 722 | { |
| 723 | GElf_Shdr shdr_mem; |
| 724 | |
| 725 | assert (elf_ndxscn (scn) == cnt++); |
| 726 | |
| 727 | scnnames[elf_ndxscn (scn)] |
| 728 | = elf_strptr (ebl->elf, shstrndx, |
| 729 | gelf_getshdr (scn, &shdr_mem)->sh_name); |
| 730 | } |
| 731 | |
| 732 | int digits = length_map[gelf_getclass (ebl->elf) - 1][radix]; |
| 733 | |
| 734 | /* We always print this prolog. */ |
| 735 | if (prefix == NULL || 1) |
| 736 | printf (gettext ("\n\nSymbols from %s:\n\n"), fullname); |
| 737 | else |
| 738 | printf (gettext ("\n\nSymbols from %s[%s]:\n\n"), prefix, fname); |
| 739 | |
| 740 | /* The header line. */ |
| 741 | printf (gettext ("%*s%-*s %-*s Class Type %-*s %*s Section\n\n"), |
| 742 | print_file_name ? (int) strlen (fullname) + 1: 0, "", |
| 743 | longest_name, sgettext ("sysv|Name"), |
| 744 | /* TRANS: the "sysv|" parts makes the string unique. */ |
| 745 | digits, sgettext ("sysv|Value"), |
| 746 | /* TRANS: the "sysv|" parts makes the string unique. */ |
| 747 | digits, sgettext ("sysv|Size"), |
| 748 | /* TRANS: the "sysv|" parts makes the string unique. */ |
| 749 | longest_where, sgettext ("sysv|Line")); |
| 750 | |
| 751 | /* Which format string to use (different radix for numbers). */ |
| 752 | const char *fmtstr; |
| 753 | if (radix == radix_hex) |
| 754 | fmtstr = "%-*s|%0*" PRIx64 "|%-6s|%-8s|%*" PRIx64 "|%*s|%s\n"; |
| 755 | else if (radix == radix_decimal) |
| 756 | fmtstr = "%-*s|%*" PRId64 "|%-6s|%-8s|%*" PRId64 "|%*s|%s\n"; |
| 757 | else |
| 758 | fmtstr = "%-*s|%0*" PRIo64 "|%-6s|%-8s|%*" PRIo64 "|%*s|%s\n"; |
| 759 | |
| 760 | /* Iterate over all symbols. */ |
| 761 | for (cnt = 0; cnt < nsyms; ++cnt) |
| 762 | { |
| 763 | const char *symstr = elf_strptr (ebl->elf, strndx, |
| 764 | syms[cnt].sym.st_name); |
| 765 | char symbindbuf[50]; |
| 766 | char symtypebuf[50]; |
| 767 | char secnamebuf[1024]; |
| 768 | |
| 769 | /* If we have to precede the line with the file name. */ |
| 770 | if (print_file_name) |
| 771 | { |
| 772 | fputs_unlocked (fullname, stdout); |
| 773 | putchar_unlocked (':'); |
| 774 | } |
| 775 | |
| 776 | /* Print the actual string. */ |
| 777 | printf (fmtstr, |
| 778 | longest_name, symstr, |
| 779 | digits, syms[cnt].sym.st_value, |
| 780 | ebl_symbol_binding_name (ebl, |
| 781 | GELF_ST_BIND (syms[cnt].sym.st_info), |
| 782 | symbindbuf, sizeof (symbindbuf)), |
| 783 | ebl_symbol_type_name (ebl, GELF_ST_TYPE (syms[cnt].sym.st_info), |
| 784 | symtypebuf, sizeof (symtypebuf)), |
| 785 | digits, syms[cnt].sym.st_size, longest_where, syms[cnt].where, |
| 786 | ebl_section_name (ebl, syms[cnt].sym.st_shndx, syms[cnt].xndx, |
| 787 | secnamebuf, sizeof (secnamebuf), scnnames, |
| 788 | shnum)); |
| 789 | } |
| 790 | |
| 791 | if (scnnames_malloced) |
| 792 | free (scnnames); |
| 793 | } |
| 794 | |
| 795 | |
| 796 | static char |
| 797 | class_type_char (GElf_Sym *sym) |
| 798 | { |
| 799 | int local_p = GELF_ST_BIND (sym->st_info) == STB_LOCAL; |
| 800 | |
| 801 | /* XXX Add support for architecture specific types and classes. */ |
| 802 | if (sym->st_shndx == SHN_ABS) |
| 803 | return local_p ? 'a' : 'A'; |
| 804 | |
| 805 | if (sym->st_shndx == SHN_UNDEF) |
| 806 | /* Undefined symbols must be global. */ |
| 807 | return 'U'; |
| 808 | |
| 809 | char result = "NDTSFB "[GELF_ST_TYPE (sym->st_info)]; |
| 810 | |
| 811 | return local_p ? tolower (result) : result; |
| 812 | } |
| 813 | |
| 814 | |
| 815 | static void |
| 816 | show_symbols_bsd (Elf *elf, GElf_Word strndx, |
| 817 | const char *prefix, const char *fname, const char *fullname, |
| 818 | GElf_SymX *syms, size_t nsyms) |
| 819 | { |
| 820 | int digits = length_map[gelf_getclass (elf) - 1][radix]; |
| 821 | |
| 822 | if (prefix != NULL && ! print_file_name) |
| 823 | printf ("\n%s:\n", fname); |
| 824 | |
| 825 | static const char *const fmtstrs[] = |
| 826 | { |
| 827 | [radix_hex] = "%0*" PRIx64 " %c%s %s\n", |
| 828 | [radix_decimal] = "%*" PRId64 " %c%s %s\n", |
| 829 | [radix_octal] = "%0*" PRIo64 " %c%s %s\n" |
| 830 | }; |
| 831 | static const char *const sfmtstrs[] = |
| 832 | { |
| 833 | [radix_hex] = "%2$0*1$" PRIx64 " %7$0*6$" PRIx64 " %3$c%4$s %5$s\n", |
| 834 | [radix_decimal] = "%2$*1$" PRId64 " %7$*6$" PRId64 " %3$c%4$s %5$s\n", |
| 835 | [radix_octal] = "%2$0*1$" PRIo64 " %7$0*6$" PRIo64 " %3$c%4$s %5$s\n" |
| 836 | }; |
| 837 | |
| 838 | /* Iterate over all symbols. */ |
| 839 | for (size_t cnt = 0; cnt < nsyms; ++cnt) |
| 840 | { |
| 841 | const char *symstr = elf_strptr (elf, strndx, syms[cnt].sym.st_name); |
| 842 | |
| 843 | /* Printing entries with a zero-length name makes the output |
| 844 | not very well parseable. Since these entries don't carry |
| 845 | much information we leave them out. */ |
| 846 | if (symstr[0] == '\0') |
| 847 | continue; |
| 848 | |
| 849 | /* If we have to precede the line with the file name. */ |
| 850 | if (print_file_name) |
| 851 | { |
| 852 | fputs_unlocked (fullname, stdout); |
| 853 | putchar_unlocked (':'); |
| 854 | } |
| 855 | |
| 856 | if (syms[cnt].sym.st_shndx == SHN_UNDEF) |
| 857 | printf ("%*s U%s %s\n", |
| 858 | digits, "", |
| 859 | mark_weak |
| 860 | ? (GELF_ST_BIND (syms[cnt].sym.st_info) == STB_WEAK |
| 861 | ? "*" : " ") |
| 862 | : "", |
| 863 | elf_strptr (elf, strndx, syms[cnt].sym.st_name)); |
| 864 | else |
| 865 | printf (print_size ? sfmtstrs[radix] : fmtstrs[radix], |
| 866 | digits, syms[cnt].sym.st_value, |
| 867 | class_type_char (&syms[cnt].sym), |
| 868 | mark_weak |
| 869 | ? (GELF_ST_BIND (syms[cnt].sym.st_info) == STB_WEAK |
| 870 | ? "*" : " ") |
| 871 | : "", |
| 872 | elf_strptr (elf, strndx, syms[cnt].sym.st_name), |
| 873 | digits, (uint64_t) syms[cnt].sym.st_size); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | |
| 878 | static void |
| 879 | show_symbols_posix (Elf *elf, GElf_Word strndx, const char *prefix, |
| 880 | const char *fullname, GElf_SymX *syms, size_t nsyms) |
| 881 | { |
| 882 | if (prefix != NULL && ! print_file_name) |
| 883 | printf ("%s:\n", fullname); |
| 884 | |
| 885 | const char *fmtstr; |
| 886 | if (radix == radix_hex) |
| 887 | fmtstr = "%s %c%s %0*" PRIx64 " %0*" PRIx64 "\n"; |
| 888 | else if (radix == radix_decimal) |
| 889 | fmtstr = "%s %c%s %*" PRId64 " %*" PRId64 "\n"; |
| 890 | else |
| 891 | fmtstr = "%s %c%s %0*" PRIo64 " %0*" PRIo64 "\n"; |
| 892 | |
| 893 | int digits = length_map[gelf_getclass (elf) - 1][radix]; |
| 894 | |
| 895 | /* Iterate over all symbols. */ |
| 896 | for (size_t cnt = 0; cnt < nsyms; ++cnt) |
| 897 | { |
| 898 | const char *symstr = elf_strptr (elf, strndx, syms[cnt].sym.st_name); |
| 899 | |
| 900 | /* Printing entries with a zero-length name makes the output |
| 901 | not very well parseable. Since these entries don't carry |
| 902 | much information we leave them out. */ |
| 903 | if (symstr[0] == '\0') |
| 904 | continue; |
| 905 | |
| 906 | /* If we have to precede the line with the file name. */ |
| 907 | if (print_file_name) |
| 908 | { |
| 909 | fputs_unlocked (fullname, stdout); |
| 910 | putchar_unlocked (':'); |
| 911 | putchar_unlocked (' '); |
| 912 | } |
| 913 | |
| 914 | printf (fmtstr, |
| 915 | symstr, |
| 916 | class_type_char (&syms[cnt].sym), |
| 917 | mark_weak |
| 918 | ? (GELF_ST_BIND (syms[cnt].sym.st_info) == STB_WEAK ? "*" : " ") |
| 919 | : "", |
| 920 | digits, syms[cnt].sym.st_value, |
| 921 | digits, syms[cnt].sym.st_size); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | |
| 926 | /* Maximum size of memory we allocate on the stack. */ |
| 927 | #define MAX_STACK_ALLOC 65536 |
| 928 | |
| 929 | static void |
| 930 | show_symbols (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, Elf_Scn *xndxscn, |
| 931 | GElf_Shdr *shdr, const char *prefix, const char *fname, |
| 932 | const char *fullname) |
| 933 | { |
| 934 | int sort_by_name (const void *p1, const void *p2) |
| 935 | { |
| 936 | GElf_SymX *s1 = (GElf_SymX *) p1; |
| 937 | GElf_SymX *s2 = (GElf_SymX *) p2; |
| 938 | int result; |
| 939 | |
| 940 | result = strcmp (elf_strptr (ebl->elf, shdr->sh_link, s1->sym.st_name), |
| 941 | elf_strptr (ebl->elf, shdr->sh_link, s2->sym.st_name)); |
| 942 | |
| 943 | return reverse_sort ? -result : result; |
| 944 | } |
| 945 | |
| 946 | int sort_by_address (const void *p1, const void *p2) |
| 947 | { |
| 948 | GElf_SymX *s1 = (GElf_SymX *) p1; |
| 949 | GElf_SymX *s2 = (GElf_SymX *) p2; |
| 950 | |
| 951 | int result = (s1->sym.st_value < s2->sym.st_value |
| 952 | ? -1 : (s1->sym.st_value == s2->sym.st_value ? 0 : 1)); |
| 953 | |
| 954 | return reverse_sort ? -result : result; |
| 955 | } |
| 956 | |
| 957 | /* Get the section header string table index. */ |
| 958 | size_t shstrndx; |
| 959 | if (elf_getshstrndx (ebl->elf, &shstrndx) < 0) |
| 960 | error (EXIT_FAILURE, 0, |
| 961 | gettext ("cannot get section header string table index")); |
| 962 | |
| 963 | /* The section is that large. */ |
| 964 | size_t size = shdr->sh_size; |
| 965 | /* One entry is this large. */ |
| 966 | size_t entsize = shdr->sh_entsize; |
| 967 | |
| 968 | /* Consistency checks. */ |
| 969 | if (entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, ehdr->e_version)) |
| 970 | error (0, 0, |
| 971 | gettext ("%s: entry size in section `%s' is not what we expect"), |
| 972 | fullname, elf_strptr (ebl->elf, shstrndx, shdr->sh_name)); |
| 973 | else if (size % entsize != 0) |
| 974 | error (0, 0, |
| 975 | gettext ("%s: size of section `%s' is not multiple of entry size"), |
| 976 | fullname, elf_strptr (ebl->elf, shstrndx, shdr->sh_name)); |
| 977 | |
| 978 | /* Compute number of entries. Handle buggy entsize values. */ |
| 979 | size_t nentries = size / (entsize ?: 1); |
| 980 | |
| 981 | |
| 982 | #define obstack_chunk_alloc xmalloc |
| 983 | #define obstack_chunk_free free |
| 984 | struct obstack whereob; |
| 985 | obstack_init (&whereob); |
| 986 | |
| 987 | /* Get a DWARF debugging descriptor. It's no problem if this isn't |
| 988 | possible. We just won't print any line number information. */ |
| 989 | Dwarf *dbg = NULL; |
| 990 | if (format == format_sysv) |
| 991 | { |
| 992 | dbg = dwarf_begin_elf (ebl->elf, DWARF_C_READ, NULL); |
| 993 | if (dbg != NULL) |
| 994 | { |
| 995 | (void) dwarf_getpubnames (dbg, get_global, NULL, 0); |
| 996 | |
| 997 | get_local_names (dbg); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | /* Allocate the memory. |
| 1002 | |
| 1003 | XXX We can use a dirty trick here. Since GElf_Sym == Elf64_Sym we |
| 1004 | can use the data memory instead of copying again if what we read |
| 1005 | is a 64 bit file. */ |
| 1006 | GElf_SymX *sym_mem; |
| 1007 | if (nentries * sizeof (GElf_SymX) < MAX_STACK_ALLOC) |
| 1008 | sym_mem = (GElf_SymX *) alloca (nentries * sizeof (GElf_SymX)); |
| 1009 | else |
| 1010 | sym_mem = (GElf_SymX *) xmalloc (nentries * sizeof (GElf_SymX)); |
| 1011 | |
| 1012 | /* Get the data of the section. */ |
| 1013 | Elf_Data *data = elf_getdata (scn, NULL); |
| 1014 | Elf_Data *xndxdata = elf_getdata (xndxscn, NULL); |
| 1015 | if (data == NULL || (xndxscn != NULL && xndxdata == NULL)) |
| 1016 | INTERNAL_ERROR (fullname); |
| 1017 | |
| 1018 | /* Iterate over all symbols. */ |
| 1019 | int longest_name = 4; |
| 1020 | int longest_where = 4; |
| 1021 | size_t nentries_used = 0; |
| 1022 | for (size_t cnt = 0; cnt < nentries; ++cnt) |
| 1023 | { |
| 1024 | GElf_Sym *sym = gelf_getsymshndx (data, xndxdata, cnt, |
| 1025 | &sym_mem[nentries_used].sym, |
| 1026 | &sym_mem[nentries_used].xndx); |
| 1027 | if (sym == NULL) |
| 1028 | INTERNAL_ERROR (fullname); |
| 1029 | |
| 1030 | /* Filter out administrative symbols without a name and those |
| 1031 | deselected by ther user with command line options. */ |
| 1032 | if ((hide_undefined && sym->st_shndx == SHN_UNDEF) |
| 1033 | || (hide_defined && sym->st_shndx != SHN_UNDEF) |
| 1034 | || (hide_local && GELF_ST_BIND (sym->st_info) == STB_LOCAL)) |
| 1035 | continue; |
| 1036 | |
| 1037 | sym_mem[nentries_used].where = ""; |
| 1038 | if (format == format_sysv) |
| 1039 | { |
| 1040 | const char *symstr = elf_strptr (ebl->elf, shdr->sh_link, |
| 1041 | sym->st_name); |
| 1042 | |
| 1043 | longest_name = MAX ((size_t) longest_name, strlen (symstr)); |
| 1044 | |
| 1045 | if (sym->st_shndx != SHN_UNDEF |
| 1046 | && GELF_ST_BIND (sym->st_info) != STB_LOCAL |
| 1047 | && global_root != NULL) |
| 1048 | { |
| 1049 | Dwarf_Global fake = { .name = symstr }; |
| 1050 | Dwarf_Global **found = tfind (&fake, &global_root, |
| 1051 | global_compare); |
| 1052 | if (found != NULL) |
| 1053 | { |
| 1054 | Dwarf_Die die_mem; |
| 1055 | Dwarf_Die *die = dwarf_offdie (dbg, (*found)->die_offset, |
| 1056 | &die_mem); |
| 1057 | |
| 1058 | Dwarf_Die cudie_mem; |
| 1059 | Dwarf_Die *cudie = NULL; |
| 1060 | |
| 1061 | Dwarf_Addr lowpc; |
| 1062 | Dwarf_Addr highpc; |
| 1063 | if (die != NULL |
| 1064 | && dwarf_lowpc (die, &lowpc) == 0 |
| 1065 | && lowpc <= sym->st_value |
| 1066 | && dwarf_highpc (die, &highpc) == 0 |
| 1067 | && highpc > sym->st_value) |
| 1068 | cudie = dwarf_offdie (dbg, (*found)->cu_offset, |
| 1069 | &cudie_mem); |
| 1070 | if (cudie != NULL) |
| 1071 | { |
| 1072 | Dwarf_Line *line = dwarf_getsrc_die (cudie, |
| 1073 | sym->st_value); |
| 1074 | if (line != NULL) |
| 1075 | { |
| 1076 | /* We found the line. */ |
| 1077 | int lineno; |
| 1078 | (void) dwarf_lineno (line, &lineno); |
| 1079 | int n; |
| 1080 | n = obstack_printf (&whereob, "%s:%d%c", |
| 1081 | basename (dwarf_linesrc (line, |
| 1082 | NULL, |
| 1083 | NULL)), |
| 1084 | lineno, '\0'); |
| 1085 | sym_mem[nentries_used].where |
| 1086 | = obstack_finish (&whereob); |
| 1087 | |
| 1088 | /* The return value of obstack_print included the |
| 1089 | NUL byte, so subtract one. */ |
| 1090 | if (--n > (int) longest_where) |
| 1091 | longest_where = (size_t) n; |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /* Try to find the symol among the local symbols. */ |
| 1098 | if (sym_mem[nentries_used].where[0] == '\0') |
| 1099 | { |
| 1100 | struct local_name fake = |
| 1101 | { |
| 1102 | .name = symstr, |
| 1103 | .lowpc = sym->st_value, |
| 1104 | .highpc = sym->st_value, |
| 1105 | }; |
| 1106 | struct local_name **found = tfind (&fake, &local_root, |
| 1107 | local_compare); |
| 1108 | if (found != NULL) |
| 1109 | { |
| 1110 | /* We found the line. */ |
| 1111 | int n = obstack_printf (&whereob, "%s:%" PRIu64 "%c", |
| 1112 | basename ((*found)->file), |
| 1113 | (*found)->lineno, |
| 1114 | '\0'); |
| 1115 | sym_mem[nentries_used].where = obstack_finish (&whereob); |
| 1116 | |
| 1117 | /* The return value of obstack_print included the |
| 1118 | NUL byte, so subtract one. */ |
| 1119 | if (--n > (int) longest_where) |
| 1120 | longest_where = (size_t) n; |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | /* We use this entry. */ |
| 1126 | ++nentries_used; |
| 1127 | } |
| 1128 | /* Now we know the exact number. */ |
| 1129 | nentries = nentries_used; |
| 1130 | |
| 1131 | /* Sort the entries according to the users wishes. */ |
| 1132 | if (sort == sort_name) |
| 1133 | qsort (sym_mem, nentries, sizeof (GElf_SymX), sort_by_name); |
| 1134 | else if (sort == sort_numeric) |
| 1135 | qsort (sym_mem, nentries, sizeof (GElf_SymX), sort_by_address); |
| 1136 | |
| 1137 | /* Finally print according to the users selection. */ |
| 1138 | switch (format) |
| 1139 | { |
| 1140 | case format_sysv: |
| 1141 | show_symbols_sysv (ebl, shdr->sh_link, prefix, fname, |
| 1142 | fullname, sym_mem, nentries, longest_name, |
| 1143 | longest_where); |
| 1144 | break; |
| 1145 | |
| 1146 | case format_bsd: |
| 1147 | show_symbols_bsd (ebl->elf, shdr->sh_link, prefix, fname, fullname, |
| 1148 | sym_mem, nentries); |
| 1149 | break; |
| 1150 | |
| 1151 | case format_posix: |
| 1152 | default: |
| 1153 | assert (format == format_posix); |
| 1154 | show_symbols_posix (ebl->elf, shdr->sh_link, prefix, fullname, sym_mem, |
| 1155 | nentries); |
| 1156 | break; |
| 1157 | } |
| 1158 | |
| 1159 | /* Free all memory. */ |
| 1160 | if (nentries * sizeof (GElf_Sym) >= MAX_STACK_ALLOC) |
| 1161 | free (sym_mem); |
| 1162 | |
| 1163 | obstack_free (&whereob, NULL); |
| 1164 | |
| 1165 | if (dbg != NULL) |
| 1166 | { |
| 1167 | tdestroy (global_root, free); |
| 1168 | global_root = NULL; |
| 1169 | |
| 1170 | tdestroy (local_root, free); |
| 1171 | local_root = NULL; |
| 1172 | |
| 1173 | (void) dwarf_end (dbg); |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | |
| 1178 | static int |
| 1179 | handle_elf (Elf *elf, const char *prefix, const char *fname, |
| 1180 | const char *suffix) |
| 1181 | { |
| 1182 | size_t prefix_len = prefix == NULL ? 0 : strlen (prefix); |
| 1183 | size_t suffix_len = suffix == NULL ? 0 : strlen (suffix); |
| 1184 | size_t fname_len = strlen (fname) + 1; |
| 1185 | char fullname[prefix_len + 1 + fname_len + suffix_len]; |
| 1186 | char *cp = fullname; |
| 1187 | Elf_Scn *scn = NULL; |
| 1188 | int any = 0; |
| 1189 | int result = 0; |
| 1190 | GElf_Ehdr ehdr_mem; |
| 1191 | GElf_Ehdr *ehdr; |
| 1192 | Ebl *ebl; |
| 1193 | |
| 1194 | /* Get the backend for this object file type. */ |
| 1195 | ebl = ebl_openbackend (elf); |
| 1196 | |
| 1197 | /* We need the ELF header in a few places. */ |
| 1198 | ehdr = gelf_getehdr (elf, &ehdr_mem); |
| 1199 | if (ehdr == NULL) |
| 1200 | INTERNAL_ERROR (fullname); |
| 1201 | |
| 1202 | /* If we are asked to print the dynamic symbol table and this is |
| 1203 | executable or dynamic executable, fail. */ |
| 1204 | if (symsec_type == SHT_DYNSYM |
| 1205 | && ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) |
| 1206 | { |
| 1207 | /* XXX Add machine specific object file types. */ |
| 1208 | error (0, 0, gettext ("%s%s%s%s: Invalid operation"), |
| 1209 | prefix ?: "", prefix ? "(" : "", fname, prefix ? ")" : ""); |
| 1210 | result = 1; |
| 1211 | goto out; |
| 1212 | } |
| 1213 | |
| 1214 | /* Create the full name of the file. */ |
| 1215 | if (prefix != NULL) |
| 1216 | cp = mempcpy (cp, prefix, prefix_len); |
| 1217 | cp = mempcpy (cp, fname, fname_len); |
| 1218 | if (suffix != NULL) |
| 1219 | memcpy (cp - 1, suffix, suffix_len + 1); |
| 1220 | |
| 1221 | /* Find the symbol table. |
| 1222 | |
| 1223 | XXX Can there be more than one? Do we print all? Currently we do. */ |
| 1224 | while ((scn = elf_nextscn (elf, scn)) != NULL) |
| 1225 | { |
| 1226 | GElf_Shdr shdr_mem; |
| 1227 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); |
| 1228 | |
| 1229 | if (shdr == NULL) |
| 1230 | INTERNAL_ERROR (fullname); |
| 1231 | |
| 1232 | if (shdr->sh_type == symsec_type) |
| 1233 | { |
| 1234 | Elf_Scn *xndxscn = NULL; |
| 1235 | |
| 1236 | /* We have a symbol table. First make sure we remember this. */ |
| 1237 | any = 1; |
| 1238 | |
| 1239 | /* Look for an extended section index table for this section. */ |
| 1240 | if (symsec_type == SHT_SYMTAB) |
| 1241 | { |
| 1242 | size_t scnndx = elf_ndxscn (scn); |
| 1243 | |
| 1244 | while ((xndxscn = elf_nextscn (elf, xndxscn)) != NULL) |
| 1245 | { |
| 1246 | GElf_Shdr xndxshdr_mem; |
| 1247 | GElf_Shdr *xndxshdr = gelf_getshdr (xndxscn, &xndxshdr_mem); |
| 1248 | |
| 1249 | if (xndxshdr == NULL) |
| 1250 | INTERNAL_ERROR (fullname); |
| 1251 | |
| 1252 | if (xndxshdr->sh_type == SHT_SYMTAB_SHNDX |
| 1253 | && xndxshdr->sh_link == scnndx) |
| 1254 | break; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | show_symbols (ebl, ehdr, scn, xndxscn, shdr, prefix, fname, |
| 1259 | fullname); |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | if (! any) |
| 1264 | { |
| 1265 | error (0, 0, gettext ("%s%s%s: no symbols"), |
| 1266 | prefix ?: "", prefix ? ":" : "", fname); |
| 1267 | result = 1; |
| 1268 | } |
| 1269 | |
| 1270 | out: |
| 1271 | /* Close the ELF backend library descriptor. */ |
| 1272 | ebl_closebackend (ebl); |
| 1273 | |
| 1274 | return result; |
| 1275 | } |