Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Print symbol information from ELF file in human-readable form. |
Mark Wielaard | 587c4b3 | 2015-04-14 11:59:36 +0200 | [diff] [blame] | 2 | Copyright (C) 2000-2008, 2009, 2011, 2012, 2014, 2015 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2000. |
| 5 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 6 | 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 10 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 11 | elfutils is distributed in the hope that it will be useful, but |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 15 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 16 | 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 18 | |
| 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 36 | #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. */ |
| 51 | static void print_version (FILE *stream, struct argp_state *state); |
Ulrich Drepper | fdc93e1 | 2009-01-17 11:47:10 -0800 | [diff] [blame] | 52 | ARGP_PROGRAM_VERSION_HOOK_DEF = print_version; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 53 | |
| 54 | /* Bug report address. */ |
Ulrich Drepper | fdc93e1 | 2009-01-17 11:47:10 -0800 | [diff] [blame] | 55 | ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | /* Values for the parameters which have no short form. */ |
Ulrich Drepper | 2356ba0 | 2011-10-03 07:23:07 -0400 | [diff] [blame] | 59 | #define OPT_DEFINED 0x100 |
| 60 | #define OPT_MARK_SPECIAL 0x101 |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 61 | |
| 62 | /* Definitions of arguments for argp functions. */ |
| 63 | static 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 Drepper | 2356ba0 | 2011-10-03 07:23:07 -0400 | [diff] [blame] | 86 | { "mark-special", OPT_MARK_SPECIAL, NULL, 0, N_("Mark special symbols"), 0 }, |
| 87 | { "mark-weak", OPT_MARK_SPECIAL, NULL, OPTION_HIDDEN, "", 0 }, |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 88 | { "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 Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 95 | #ifdef USE_DEMANGLE |
| 96 | { "demangle", 'C', NULL, 0, |
| 97 | N_("Decode low-level symbol names into source code names"), 0 }, |
| 98 | #endif |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 99 | { NULL, 0, NULL, 0, N_("Miscellaneous:"), 0 }, |
| 100 | { NULL, 0, NULL, 0, NULL, 0 } |
| 101 | }; |
| 102 | |
| 103 | /* Short description of program. */ |
| 104 | static const char doc[] = N_("List symbols from FILEs (a.out by default)."); |
| 105 | |
| 106 | /* Strings for arguments in help texts. */ |
| 107 | static const char args_doc[] = N_("[FILE...]"); |
| 108 | |
| 109 | /* Prototype for option handler. */ |
| 110 | static error_t parse_opt (int key, char *arg, struct argp_state *state); |
| 111 | |
Ulrich Drepper | c6b3d0c | 2012-01-21 18:14:39 -0500 | [diff] [blame] | 112 | /* Parser children. */ |
| 113 | static struct argp_child argp_children[] = |
| 114 | { |
| 115 | { &color_argp, 0, N_("Output formatting"), 2 }, |
| 116 | { NULL, 0, NULL, 0} |
| 117 | }; |
| 118 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 119 | /* Data structure to communicate with argp functions. */ |
| 120 | static struct argp argp = |
| 121 | { |
Ulrich Drepper | c6b3d0c | 2012-01-21 18:14:39 -0500 | [diff] [blame] | 122 | options, parse_opt, args_doc, doc, argp_children, NULL, NULL |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | |
| 126 | /* Print symbols in file named FNAME. */ |
| 127 | static int process_file (const char *fname, bool more_than_one); |
| 128 | |
| 129 | /* Handle content of archive. */ |
| 130 | static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname, |
| 131 | const char *suffix); |
| 132 | |
| 133 | /* Handle ELF file. */ |
| 134 | static int handle_elf (Elf *elf, const char *prefix, const char *fname, |
| 135 | const char *suffix); |
| 136 | |
| 137 | |
| 138 | #define INTERNAL_ERROR(fname) \ |
Mark Wielaard | 587c4b3 | 2015-04-14 11:59:36 +0200 | [diff] [blame] | 139 | error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s): %s"), \ |
| 140 | fname, __LINE__, PACKAGE_VERSION, elf_errmsg (-1)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 141 | |
| 142 | |
| 143 | /* Internal representation of symbols. */ |
| 144 | typedef 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. */ |
| 155 | static enum |
| 156 | { |
| 157 | format_sysv = 0, |
| 158 | format_bsd, |
| 159 | format_posix |
| 160 | } format; |
| 161 | |
| 162 | /* Print defined, undefined, or both? */ |
| 163 | static bool hide_undefined; |
| 164 | static bool hide_defined; |
| 165 | |
| 166 | /* Print local symbols also? */ |
| 167 | static bool hide_local; |
| 168 | |
| 169 | /* Nonzero if full filename should precede every symbol. */ |
| 170 | static bool print_file_name; |
| 171 | |
| 172 | /* If true print size of defined symbols in BSD format. */ |
| 173 | static bool print_size; |
| 174 | |
| 175 | /* If true print archive index. */ |
| 176 | static bool print_armap; |
| 177 | |
| 178 | /* If true reverse sorting. */ |
| 179 | static bool reverse_sort; |
| 180 | |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 181 | #ifdef USE_DEMANGLE |
| 182 | /* If true demangle symbols. */ |
| 183 | static bool demangle; |
| 184 | #endif |
| 185 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 186 | /* Type of the section we are printing. */ |
| 187 | static GElf_Word symsec_type = SHT_SYMTAB; |
| 188 | |
| 189 | /* Sorting selection. */ |
| 190 | static enum |
| 191 | { |
| 192 | sort_name = 0, |
| 193 | sort_numeric, |
| 194 | sort_nosort |
| 195 | } sort; |
| 196 | |
| 197 | /* Radix for printed numbers. */ |
| 198 | static enum |
| 199 | { |
| 200 | radix_hex = 0, |
| 201 | radix_decimal, |
| 202 | radix_octal |
| 203 | } radix; |
| 204 | |
Ulrich Drepper | 2356ba0 | 2011-10-03 07:23:07 -0400 | [diff] [blame] | 205 | /* 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. */ |
| 210 | static bool mark_special; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | int |
| 214 | main (int argc, char *argv[]) |
| 215 | { |
| 216 | int remaining; |
| 217 | int result = 0; |
| 218 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 219 | /* 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 Drepper | b024386 | 2007-06-06 00:09:36 +0000 | [diff] [blame] | 228 | (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 229 | |
| 230 | /* Initialize the message catalog. */ |
Ulrich Drepper | b024386 | 2007-06-06 00:09:36 +0000 | [diff] [blame] | 231 | (void) textdomain (PACKAGE_TARNAME); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 232 | |
| 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. */ |
| 257 | static void |
| 258 | print_version (FILE *stream, struct argp_state *state __attribute__ ((unused))) |
| 259 | { |
Ulrich Drepper | b024386 | 2007-06-06 00:09:36 +0000 | [diff] [blame] | 260 | fprintf (stream, "nm (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 261 | fprintf (stream, gettext ("\ |
| 262 | Copyright (C) %s Red Hat, Inc.\n\ |
| 263 | This is free software; see the source for copying conditions. There is NO\n\ |
| 264 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ |
Ulrich Drepper | 3a64a30 | 2012-01-21 18:19:24 -0500 | [diff] [blame] | 265 | "), "2012"); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 266 | fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper"); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | /* Handle program arguments. */ |
| 271 | static error_t |
| 272 | parse_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 Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 281 | #ifdef USE_DEMANGLE |
| 282 | case 'C': |
| 283 | demangle = true; |
| 284 | break; |
| 285 | #endif |
| 286 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 287 | 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 Drepper | 2356ba0 | 2011-10-03 07:23:07 -0400 | [diff] [blame] | 346 | case OPT_MARK_SPECIAL: |
| 347 | mark_special = true; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 348 | 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. */ |
| 370 | static int |
| 371 | process_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 Drepper | d112ef8 | 2005-09-03 21:31:27 +0000 | [diff] [blame] | 394 | error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 395 | |
| 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 Drepper | d112ef8 | 2005-09-03 21:31:27 +0000 | [diff] [blame] | 406 | error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 407 | |
| 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 | |
| 422 | static int |
| 423 | handle_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 Drepper | c6b3d0c | 2012-01-21 18:14:39 -0500 | [diff] [blame] | 456 | fputs_unlocked (gettext("\nArchive index:\n"), stdout); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 457 | |
| 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 Wielaard | 0b799ad | 2014-12-26 16:12:52 +0100 | [diff] [blame] | 467 | break; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 468 | } |
| 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 Wielaard | 8fb260f | 2014-12-26 16:20:39 +0100 | [diff] [blame] | 492 | && strcmp (arhdr->ar_name, "//") != 0 |
| 493 | && strcmp (arhdr->ar_name, "/SYM64/") != 0) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 494 | { |
| 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. */ |
| 520 | static 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 537 | static int |
| 538 | global_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 | |
| 547 | static void *global_root; |
| 548 | |
| 549 | |
| 550 | static int |
| 551 | get_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 | |
| 562 | struct 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 | |
| 572 | static int |
| 573 | local_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 | |
| 604 | static int |
| 605 | get_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 McGrath | 6724c90 | 2005-10-28 07:07:19 +0000 | [diff] [blame] | 612 | Dwarf_Op *loc; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 613 | size_t nloc; |
Roland McGrath | 6724c90 | 2005-10-28 07:07:19 +0000 | [diff] [blame] | 614 | if (dwarf_getlocation (locattr, &loc, &nloc) != 0) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 615 | 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 | |
| 630 | static void *local_root; |
| 631 | |
| 632 | |
| 633 | static void |
| 634 | get_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 McGrath | 468fe4d | 2008-12-11 21:00:12 -0800 | [diff] [blame] | 721 | /* Do elf_strptr, but return a backup string and never NULL. */ |
| 722 | static const char * |
| 723 | sym_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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 733 | |
| 734 | /* Show symbols in SysV format. */ |
| 735 | static void |
Marek Polacek | c8920de | 2011-05-12 12:08:21 +0200 | [diff] [blame] | 736 | show_symbols_sysv (Ebl *ebl, GElf_Word strndx, const char *fullname, |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 737 | GElf_SymX *syms, size_t nsyms, int longest_name, |
| 738 | int longest_where) |
| 739 | { |
| 740 | size_t shnum; |
Ulrich Drepper | f189493 | 2009-06-13 15:55:42 -0700 | [diff] [blame] | 741 | if (elf_getshdrnum (ebl->elf, &shnum) < 0) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 742 | 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 Drepper | f189493 | 2009-06-13 15:55:42 -0700 | [diff] [blame] | 752 | if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 753 | 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 Wielaard | 03d76f4 | 2013-11-09 16:45:22 +0100 | [diff] [blame] | 763 | assert (elf_ndxscn (scn) == cnt); |
| 764 | cnt++; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 765 | |
Roland McGrath | 468fe4d | 2008-12-11 21:00:12 -0800 | [diff] [blame] | 766 | char *name = elf_strptr (ebl->elf, shstrndx, |
| 767 | gelf_getshdr (scn, &shdr_mem)->sh_name); |
| 768 | if (unlikely (name == NULL)) |
| 769 | { |
Mark Wielaard | 7df3d2c | 2012-12-11 22:27:05 +0100 | [diff] [blame] | 770 | const size_t bufsz = sizeof "[invalid sh_name 0x12345678]"; |
Roland McGrath | 57bd66c | 2012-12-11 09:42:07 -0800 | [diff] [blame] | 771 | name = alloca (bufsz); |
| 772 | snprintf (name, bufsz, "[invalid sh_name %#" PRIx32 "]", |
Roland McGrath | 468fe4d | 2008-12-11 21:00:12 -0800 | [diff] [blame] | 773 | gelf_getshdr (scn, &shdr_mem)->sh_name); |
| 774 | } |
| 775 | scnnames[elf_ndxscn (scn)] = name; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | int digits = length_map[gelf_getclass (ebl->elf) - 1][radix]; |
| 779 | |
| 780 | /* We always print this prolog. */ |
Marek Polacek | c8920de | 2011-05-12 12:08:21 +0200 | [diff] [blame] | 781 | printf (gettext ("\n\nSymbols from %s:\n\n"), fullname); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 782 | |
| 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 Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 794 | #ifdef USE_DEMANGLE |
| 795 | size_t demangle_buffer_len = 0; |
| 796 | char *demangle_buffer = NULL; |
| 797 | #endif |
| 798 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 799 | /* Iterate over all symbols. */ |
Ulrich Drepper | 66f4c37 | 2011-10-03 15:53:12 -0400 | [diff] [blame] | 800 | for (cnt = 1; cnt < nsyms; ++cnt) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 801 | { |
Ulrich Drepper | 66f4c37 | 2011-10-03 15:53:12 -0400 | [diff] [blame] | 802 | /* In this format SECTION entries are not printed. */ |
| 803 | if (GELF_ST_TYPE (syms[cnt].sym.st_info) == STT_SECTION) |
| 804 | continue; |
| 805 | |
Roland McGrath | 468fe4d | 2008-12-11 21:00:12 -0800 | [diff] [blame] | 806 | char symstrbuf[50]; |
| 807 | const char *symstr = sym_name (ebl->elf, strndx, syms[cnt].sym.st_name, |
| 808 | symstrbuf, sizeof symstrbuf); |
| 809 | |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 810 | #ifdef USE_DEMANGLE |
Jan Kratochvil | 7c6e785 | 2014-01-15 21:16:57 +0100 | [diff] [blame] | 811 | /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */ |
| 812 | if (demangle && symstr[0] == '_' && symstr[1] == 'Z') |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 813 | { |
| 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 823 | char symbindbuf[50]; |
| 824 | char symtypebuf[50]; |
| 825 | char secnamebuf[1024]; |
Ulrich Drepper | 66f4c37 | 2011-10-03 15:53:12 -0400 | [diff] [blame] | 826 | char addressbuf[(64 + 2) / 3 + 1]; |
| 827 | char sizebuf[(64 + 2) / 3 + 1]; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 828 | |
| 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 Drepper | 66f4c37 | 2011-10-03 15:53:12 -0400 | [diff] [blame] | 836 | /* Covert the address. */ |
| 837 | if (syms[cnt].sym.st_shndx == SHN_UNDEF) |
| 838 | addressbuf[0] = sizebuf[0] = '\0'; |
| 839 | else |
| 840 | { |
Mark Wielaard | f48eb6b | 2014-01-23 00:56:41 +0100 | [diff] [blame] | 841 | snprintf (addressbuf, sizeof (addressbuf), |
| 842 | (radix == radix_hex ? "%0*" PRIx64 |
| 843 | : (radix == radix_decimal ? "%0*" PRId64 |
| 844 | : "%0*" PRIo64)), |
Ulrich Drepper | 66f4c37 | 2011-10-03 15:53:12 -0400 | [diff] [blame] | 845 | digits, syms[cnt].sym.st_value); |
Mark Wielaard | f48eb6b | 2014-01-23 00:56:41 +0100 | [diff] [blame] | 846 | snprintf (sizebuf, sizeof (sizebuf), |
| 847 | (radix == radix_hex ? "%0*" PRIx64 |
| 848 | : (radix == radix_decimal ? "%0*" PRId64 |
| 849 | : "%0*" PRIo64)), |
Ulrich Drepper | 66f4c37 | 2011-10-03 15:53:12 -0400 | [diff] [blame] | 850 | digits, syms[cnt].sym.st_size); |
| 851 | } |
| 852 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 853 | /* Print the actual string. */ |
Ulrich Drepper | 66f4c37 | 2011-10-03 15:53:12 -0400 | [diff] [blame] | 854 | printf ("%-*s|%s|%-6s|%-8s|%s|%*s|%s\n", |
| 855 | longest_name, symstr, addressbuf, |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 856 | 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 Drepper | 66f4c37 | 2011-10-03 15:53:12 -0400 | [diff] [blame] | 861 | sizebuf, longest_where, syms[cnt].where, |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 862 | ebl_section_name (ebl, syms[cnt].sym.st_shndx, syms[cnt].xndx, |
| 863 | secnamebuf, sizeof (secnamebuf), scnnames, |
| 864 | shnum)); |
| 865 | } |
| 866 | |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 867 | #ifdef USE_DEMANGLE |
| 868 | free (demangle_buffer); |
| 869 | #endif |
| 870 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 871 | if (scnnames_malloced) |
| 872 | free (scnnames); |
| 873 | } |
| 874 | |
| 875 | |
| 876 | static char |
Ulrich Drepper | 2356ba0 | 2011-10-03 07:23:07 -0400 | [diff] [blame] | 877 | class_type_char (Elf *elf, const GElf_Ehdr *ehdr, GElf_Sym *sym) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 878 | { |
| 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 Drepper | 2356ba0 | 2011-10-03 07:23:07 -0400 | [diff] [blame] | 889 | 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 911 | |
| 912 | return local_p ? tolower (result) : result; |
| 913 | } |
| 914 | |
| 915 | |
| 916 | static void |
Ulrich Drepper | 2356ba0 | 2011-10-03 07:23:07 -0400 | [diff] [blame] | 917 | show_symbols_bsd (Elf *elf, const GElf_Ehdr *ehdr, GElf_Word strndx, |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 918 | 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 Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 926 | #ifdef USE_DEMANGLE |
| 927 | size_t demangle_buffer_len = 0; |
| 928 | char *demangle_buffer = NULL; |
| 929 | #endif |
| 930 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 931 | /* Iterate over all symbols. */ |
| 932 | for (size_t cnt = 0; cnt < nsyms; ++cnt) |
| 933 | { |
Roland McGrath | 468fe4d | 2008-12-11 21:00:12 -0800 | [diff] [blame] | 934 | char symstrbuf[50]; |
| 935 | const char *symstr = sym_name (elf, strndx, syms[cnt].sym.st_name, |
| 936 | symstrbuf, sizeof symstrbuf); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 937 | |
| 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 Drepper | c6b3d0c | 2012-01-21 18:14:39 -0500 | [diff] [blame] | 944 | /* We do not print the entries for files. */ |
| 945 | if (GELF_ST_TYPE (syms[cnt].sym.st_info) == STT_FILE) |
| 946 | continue; |
| 947 | |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 948 | #ifdef USE_DEMANGLE |
Jan Kratochvil | 7c6e785 | 2014-01-15 21:16:57 +0100 | [diff] [blame] | 949 | /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */ |
| 950 | if (demangle && symstr[0] == '_' && symstr[1] == 'Z') |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 951 | { |
| 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 961 | /* 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 Drepper | c6b3d0c | 2012-01-21 18:14:39 -0500 | [diff] [blame] | 968 | 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 973 | if (syms[cnt].sym.st_shndx == SHN_UNDEF) |
Ulrich Drepper | c6b3d0c | 2012-01-21 18:14:39 -0500 | [diff] [blame] | 974 | { |
| 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 988 | else |
Ulrich Drepper | c6b3d0c | 2012-01-21 18:14:39 -0500 | [diff] [blame] | 989 | { |
| 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 Wielaard | f48eb6b | 2014-01-23 00:56:41 +0100 | [diff] [blame] | 1000 | 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 Drepper | c6b3d0c | 2012-01-21 18:14:39 -0500 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | if (color_mode) |
| 1038 | fputs_unlocked (color_off, stdout); |
| 1039 | putchar_unlocked ('\n'); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1040 | } |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 1041 | |
| 1042 | #ifdef USE_DEMANGLE |
| 1043 | free (demangle_buffer); |
| 1044 | #endif |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | |
| 1048 | static void |
Ulrich Drepper | 2356ba0 | 2011-10-03 07:23:07 -0400 | [diff] [blame] | 1049 | show_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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1052 | { |
| 1053 | if (prefix != NULL && ! print_file_name) |
| 1054 | printf ("%s:\n", fullname); |
| 1055 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1056 | int digits = length_map[gelf_getclass (elf) - 1][radix]; |
| 1057 | |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 1058 | #ifdef USE_DEMANGLE |
| 1059 | size_t demangle_buffer_len = 0; |
| 1060 | char *demangle_buffer = NULL; |
| 1061 | #endif |
| 1062 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1063 | /* Iterate over all symbols. */ |
| 1064 | for (size_t cnt = 0; cnt < nsyms; ++cnt) |
| 1065 | { |
Roland McGrath | 468fe4d | 2008-12-11 21:00:12 -0800 | [diff] [blame] | 1066 | char symstrbuf[50]; |
| 1067 | const char *symstr = sym_name (elf, strndx, syms[cnt].sym.st_name, |
| 1068 | symstrbuf, sizeof symstrbuf); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1069 | |
| 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 Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 1076 | #ifdef USE_DEMANGLE |
Jan Kratochvil | 7c6e785 | 2014-01-15 21:16:57 +0100 | [diff] [blame] | 1077 | /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */ |
| 1078 | if (demangle && symstr[0] == '_' && symstr[1] == 'Z') |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 1079 | { |
| 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1089 | /* 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 Wielaard | f48eb6b | 2014-01-23 00:56:41 +0100 | [diff] [blame] | 1097 | 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1102 | symstr, |
Ulrich Drepper | 2356ba0 | 2011-10-03 07:23:07 -0400 | [diff] [blame] | 1103 | 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1109 | : "", |
| 1110 | digits, syms[cnt].sym.st_value, |
| 1111 | digits, syms[cnt].sym.st_size); |
| 1112 | } |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 1113 | |
| 1114 | #ifdef USE_DEMANGLE |
| 1115 | free (demangle_buffer); |
| 1116 | #endif |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | |
| 1120 | /* Maximum size of memory we allocate on the stack. */ |
| 1121 | #define MAX_STACK_ALLOC 65536 |
| 1122 | |
Roland McGrath | cb6d865 | 2007-08-23 08:10:54 +0000 | [diff] [blame] | 1123 | static int |
| 1124 | sort_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 | |
| 1135 | static Elf_Data *sort_by_name_strtab; |
| 1136 | |
| 1137 | static int |
| 1138 | sort_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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1151 | static void |
| 1152 | show_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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1156 | /* Get the section header string table index. */ |
| 1157 | size_t shstrndx; |
Ulrich Drepper | f189493 | 2009-06-13 15:55:42 -0700 | [diff] [blame] | 1158 | if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1159 | 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 Wielaard | 309e712 | 2014-12-26 19:36:31 +0100 | [diff] [blame] | 1168 | if (entsize == 0 |
| 1169 | || entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, ehdr->e_version)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1170 | error (0, 0, |
Mark Wielaard | 309e712 | 2014-12-26 19:36:31 +0100 | [diff] [blame] | 1171 | 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1174 | else if (size % entsize != 0) |
| 1175 | error (0, 0, |
Mark Wielaard | 309e712 | 2014-12-26 19:36:31 +0100 | [diff] [blame] | 1176 | 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1179 | |
| 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 | |
| 1203 | /* Allocate the memory. |
| 1204 | |
| 1205 | XXX We can use a dirty trick here. Since GElf_Sym == Elf64_Sym we |
| 1206 | can use the data memory instead of copying again if what we read |
| 1207 | is a 64 bit file. */ |
| 1208 | GElf_SymX *sym_mem; |
| 1209 | if (nentries * sizeof (GElf_SymX) < MAX_STACK_ALLOC) |
| 1210 | sym_mem = (GElf_SymX *) alloca (nentries * sizeof (GElf_SymX)); |
| 1211 | else |
| 1212 | sym_mem = (GElf_SymX *) xmalloc (nentries * sizeof (GElf_SymX)); |
| 1213 | |
| 1214 | /* Get the data of the section. */ |
| 1215 | Elf_Data *data = elf_getdata (scn, NULL); |
| 1216 | Elf_Data *xndxdata = elf_getdata (xndxscn, NULL); |
| 1217 | if (data == NULL || (xndxscn != NULL && xndxdata == NULL)) |
| 1218 | INTERNAL_ERROR (fullname); |
| 1219 | |
| 1220 | /* Iterate over all symbols. */ |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 1221 | #ifdef USE_DEMANGLE |
| 1222 | size_t demangle_buffer_len = 0; |
| 1223 | char *demangle_buffer = NULL; |
| 1224 | #endif |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1225 | 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 Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 1237 | deselected by the user with command line options. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1238 | 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 McGrath | 468fe4d | 2008-12-11 21:00:12 -0800 | [diff] [blame] | 1248 | if (symstr == NULL) |
| 1249 | continue; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1250 | |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 1251 | #ifdef USE_DEMANGLE |
Jan Kratochvil | 7c6e785 | 2014-01-15 21:16:57 +0100 | [diff] [blame] | 1252 | /* Demangle if necessary. Require GNU v3 ABI by the "_Z" prefix. */ |
| 1253 | if (demangle && symstr[0] == '_' && symstr[1] == 'Z') |
Ulrich Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 1254 | { |
| 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1264 | 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 Wielaard | a3344c7 | 2015-05-06 12:45:49 +0200 | [diff] [blame] | 1300 | const char *file = dwarf_linesrc (line, NULL, NULL); |
| 1301 | file = (file != NULL) ? basename (file) : "???"; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1302 | int n; |
Mark Wielaard | a3344c7 | 2015-05-06 12:45:49 +0200 | [diff] [blame] | 1303 | n = obstack_printf (&whereob, "%s:%d%c", file, |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1304 | 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 Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 1317 | /* Try to find the symbol among the local symbols. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1318 | 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 Drepper | b4a16cf | 2011-10-02 08:33:19 -0400 | [diff] [blame] | 1348 | #ifdef USE_DEMANGLE |
| 1349 | free (demangle_buffer); |
| 1350 | #endif |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1351 | /* 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 McGrath | cb6d865 | 2007-08-23 08:10:54 +0000 | [diff] [blame] | 1356 | { |
| 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1361 | 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 Polacek | c8920de | 2011-05-12 12:08:21 +0200 | [diff] [blame] | 1368 | show_symbols_sysv (ebl, shdr->sh_link, fullname, sym_mem, nentries, |
| 1369 | longest_name, longest_where); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1370 | break; |
| 1371 | |
| 1372 | case format_bsd: |
Ulrich Drepper | 2356ba0 | 2011-10-03 07:23:07 -0400 | [diff] [blame] | 1373 | show_symbols_bsd (ebl->elf, ehdr, shdr->sh_link, prefix, fname, fullname, |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1374 | sym_mem, nentries); |
| 1375 | break; |
| 1376 | |
| 1377 | case format_posix: |
| 1378 | default: |
| 1379 | assert (format == format_posix); |
Ulrich Drepper | 2356ba0 | 2011-10-03 07:23:07 -0400 | [diff] [blame] | 1380 | show_symbols_posix (ebl->elf, ehdr, shdr->sh_link, prefix, fullname, |
| 1381 | sym_mem, nentries); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1382 | break; |
| 1383 | } |
| 1384 | |
| 1385 | /* Free all memory. */ |
| 1386 | if (nentries * sizeof (GElf_Sym) >= MAX_STACK_ALLOC) |
| 1387 | 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 | |
| 1404 | static int |
| 1405 | handle_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 Drepper | 3cbdd38 | 2008-01-02 17:44:39 +0000 | [diff] [blame] | 1502 | |
| 1503 | |
| 1504 | #include "debugpred.h" |