1 /* Print information from ELF file in human-readable form.
2 Copyright (C) 1999-2018 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
37 #include <stdio_ext.h>
48 #include <printversion.h>
49 #include "../libelf/libelfP.h"
50 #include "../libelf/common.h"
51 #include "../libebl/libeblP.h"
52 #include "../libdwelf/libdwelf.h"
53 #include "../libdw/libdwP.h"
54 #include "../libdwfl/libdwflP.h"
55 #include "../libdw/memory-access.h"
57 #include "../libdw/known-dwarf.h"
60 #define CORE_SIGILL SIGILL
61 #define CORE_SIGBUS SIGBUS
62 #define CORE_SIGFPE SIGFPE
63 #define CORE_SIGSEGV SIGSEGV
64 #define CORE_SI_USER SI_USER
66 /* We want the linux version of those as that is what shows up in the core files. */
67 #define CORE_SIGILL 4 /* Illegal instruction (ANSI). */
68 #define CORE_SIGBUS 7 /* BUS error (4.2 BSD). */
69 #define CORE_SIGFPE 8 /* Floating-point exception (ANSI). */
70 #define CORE_SIGSEGV 11 /* Segmentation violation (ANSI). */
71 #define CORE_SI_USER 0 /* Sent by kill, sigsend. */
74 /* Name and version of program. */
75 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
77 /* Bug report address. */
78 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
80 /* argp key value for --elf-section, non-ascii. */
81 #define ELF_INPUT_SECTION 256
83 /* argp key value for --dwarf-skeleton, non-ascii. */
84 #define DWARF_SKELETON 257
86 /* argp key value for --dyn-syms, non-ascii. */
87 #define PRINT_DYNSYM_TABLE 258
89 /* Terrible hack for hooking unrelated skeleton/split compile units,
90 see __libdw_link_skel_split in print_debug. */
91 static bool do_not_close_dwfl = false;
93 /* Definitions of arguments for argp functions. */
94 static const struct argp_option options[] =
96 { NULL, 0, NULL, 0, N_("ELF input selection:"), 0 },
97 { "elf-section", ELF_INPUT_SECTION, "SECTION", OPTION_ARG_OPTIONAL,
98 N_("Use the named SECTION (default .gnu_debugdata) as (compressed) ELF "
100 { "dwarf-skeleton", DWARF_SKELETON, "FILE", 0,
101 N_("Used with -w to find the skeleton Compile Units in FILE associated "
102 "with the Split Compile units in a .dwo input file"), 0 },
103 { NULL, 0, NULL, 0, N_("ELF output selection:"), 0 },
104 { "all", 'a', NULL, 0,
105 N_("All these plus -p .strtab -p .dynstr -p .comment"), 0 },
106 { "dynamic", 'd', NULL, 0, N_("Display the dynamic segment"), 0 },
107 { "file-header", 'h', NULL, 0, N_("Display the ELF file header"), 0 },
108 { "histogram", 'I', NULL, 0,
109 N_("Display histogram of bucket list lengths"), 0 },
110 { "program-headers", 'l', NULL, 0, N_("Display the program headers"), 0 },
111 { "segments", 'l', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
112 { "relocs", 'r', NULL, 0, N_("Display relocations"), 0 },
113 { "section-groups", 'g', NULL, 0, N_("Display the section groups"), 0 },
114 { "section-headers", 'S', NULL, 0, N_("Display the sections' headers"), 0 },
115 { "sections", 'S', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
116 { "symbols", 's', "SECTION", OPTION_ARG_OPTIONAL,
117 N_("Display the symbol table sections"), 0 },
118 { "syms", 's', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
119 { "dyn-syms", PRINT_DYNSYM_TABLE, NULL, 0,
120 N_("Display (only) the dynamic symbol table"), 0 },
121 { "version-info", 'V', NULL, 0, N_("Display versioning information"), 0 },
122 { "notes", 'n', "SECTION", OPTION_ARG_OPTIONAL, N_("Display the ELF notes"), 0 },
123 { "arch-specific", 'A', NULL, 0,
124 N_("Display architecture specific information, if any"), 0 },
125 { "exception", 'e', NULL, 0,
126 N_("Display sections for exception handling"), 0 },
128 { NULL, 0, NULL, 0, N_("Additional output selection:"), 0 },
129 { "debug-dump", 'w', "SECTION", OPTION_ARG_OPTIONAL,
130 N_("Display DWARF section content. SECTION can be one of abbrev, addr, "
131 "aranges, decodedaranges, frame, gdb_index, info, info+, loc, line, "
132 "decodedline, ranges, pubnames, str, macinfo, macro or exception"), 0 },
133 { "hex-dump", 'x', "SECTION", 0,
134 N_("Dump the uninterpreted contents of SECTION, by number or name"), 0 },
135 { "strings", 'p', "SECTION", OPTION_ARG_OPTIONAL,
136 N_("Print string contents of sections"), 0 },
137 { "string-dump", 'p', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
138 { "archive-index", 'c', NULL, 0,
139 N_("Display the symbol index of an archive"), 0 },
140 { "use-dynamic", 'D', NULL, 0,
141 N_("Use the dynamic segment when possible for displaying info"), 0 },
143 { NULL, 0, NULL, 0, N_("Output control:"), 0 },
144 { "numeric-addresses", 'N', NULL, 0,
145 N_("Do not find symbol names for addresses in DWARF data"), 0 },
146 { "unresolved-address-offsets", 'U', NULL, 0,
147 N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 },
148 { "wide", 'W', NULL, 0,
149 N_("Ignored for compatibility (lines always wide)"), 0 },
150 { "decompress", 'z', NULL, 0,
151 N_("Show compression information for compressed sections (when used with -S); decompress section before dumping data (when used with -p or -x)"), 0 },
152 { NULL, 0, NULL, 0, NULL, 0 }
155 /* Short description of program. */
156 static const char doc[] = N_("\
157 Print information from ELF file in human-readable form.");
159 /* Strings for arguments in help texts. */
160 static const char args_doc[] = N_("FILE...");
162 /* Prototype for option handler. */
163 static error_t parse_opt (int key, char *arg, struct argp_state *state);
165 /* Data structure to communicate with argp functions. */
166 static struct argp argp =
168 options, parse_opt, args_doc, doc, NULL, NULL, NULL
171 /* If non-null, the section from which we should read to (compressed) ELF. */
172 static const char *elf_input_section = NULL;
174 /* If non-null, the file that contains the skeleton CUs. */
175 static const char *dwarf_skeleton = NULL;
177 /* Flags set by the option controlling the output. */
179 /* True if dynamic segment should be printed. */
180 static bool print_dynamic_table;
182 /* True if the file header should be printed. */
183 static bool print_file_header;
185 /* True if the program headers should be printed. */
186 static bool print_program_header;
188 /* True if relocations should be printed. */
189 static bool print_relocations;
191 /* True if the section headers should be printed. */
192 static bool print_section_header;
194 /* True if the symbol table should be printed. */
195 static bool print_symbol_table;
197 /* True if (only) the dynsym table should be printed. */
198 static bool print_dynsym_table;
200 /* True if reconstruct dynamic symbol table from the PT_DYNAMIC segment. */
201 static bool use_dynamic_segment;
203 /* A specific section name, or NULL to print all symbol tables. */
204 static char *symbol_table_section;
206 /* A specific section name, or NULL to print all ELF notes. */
207 static char *notes_section;
209 /* True if the version information should be printed. */
210 static bool print_version_info;
212 /* True if section groups should be printed. */
213 static bool print_section_groups;
215 /* True if bucket list length histogram should be printed. */
216 static bool print_histogram;
218 /* True if the architecture specific data should be printed. */
219 static bool print_arch;
221 /* True if note section content should be printed. */
222 static bool print_notes;
224 /* True if SHF_STRINGS section content should be printed. */
225 static bool print_string_sections;
227 /* True if archive index should be printed. */
228 static bool print_archive_index;
230 /* True if any of the control options except print_archive_index is set. */
231 static bool any_control_option;
233 /* True if we should print addresses from DWARF in symbolic form. */
234 static bool print_address_names = true;
236 /* True if we should print raw values instead of relativized addresses. */
237 static bool print_unresolved_addresses = false;
239 /* True if we should print the .debug_aranges section using libdw. */
240 static bool decodedaranges = false;
242 /* True if we should print the .debug_aranges section using libdw. */
243 static bool decodedline = false;
245 /* True if we want to show more information about compressed sections. */
246 static bool print_decompress = false;
248 /* True if we want to show split compile units for debug_info skeletons. */
249 static bool show_split_units = false;
251 /* Select printing of debugging sections. */
252 static enum section_e
254 section_abbrev = 1, /* .debug_abbrev */
255 section_aranges = 2, /* .debug_aranges */
256 section_frame = 4, /* .debug_frame or .eh_frame & al. */
257 section_info = 8, /* .debug_info, (implies .debug_types) */
258 section_line = 16, /* .debug_line */
259 section_loc = 32, /* .debug_loc */
260 section_pubnames = 64, /* .debug_pubnames */
261 section_str = 128, /* .debug_str */
262 section_macinfo = 256, /* .debug_macinfo */
263 section_ranges = 512, /* .debug_ranges */
264 section_exception = 1024, /* .eh_frame & al. */
265 section_gdb_index = 2048, /* .gdb_index */
266 section_macro = 4096, /* .debug_macro */
267 section_addr = 8192, /* .debug_addr */
268 section_types = 16384, /* .debug_types (implied by .debug_info) */
269 section_all = (section_abbrev | section_aranges | section_frame
270 | section_info | section_line | section_loc
271 | section_pubnames | section_str | section_macinfo
272 | section_ranges | section_exception | section_gdb_index
273 | section_macro | section_addr | section_types)
274 } print_debug_sections, implicit_debug_sections;
276 /* Select hex dumping of sections. */
277 static struct section_argument *dump_data_sections;
278 static struct section_argument **dump_data_sections_tail = &dump_data_sections;
280 /* Select string dumping of sections. */
281 static struct section_argument *string_sections;
282 static struct section_argument **string_sections_tail = &string_sections;
284 struct section_argument
286 struct section_argument *next;
291 /* Numbers of sections and program headers in the file. */
296 /* Declarations of local functions. */
297 static void process_file (int fd, const char *fname, bool only_one);
298 static void process_elf_file (Dwfl_Module *dwflmod, int fd);
299 static void print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr);
300 static void print_shdr (Ebl *ebl, GElf_Ehdr *ehdr);
301 static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr);
302 static void print_scngrp (Ebl *ebl);
303 static void print_dynamic (Ebl *ebl);
304 static void print_relocs (Ebl *ebl, GElf_Ehdr *ehdr);
305 static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
307 static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
309 static bool print_symtab (Ebl *ebl, int type);
310 static void handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
311 static void print_verinfo (Ebl *ebl);
312 static void handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
313 static void handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
314 static void handle_versym (Ebl *ebl, Elf_Scn *scn,
316 static void print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr);
317 static void handle_hash (Ebl *ebl);
318 static void handle_notes (Ebl *ebl, GElf_Ehdr *ehdr);
319 static void print_liblist (Ebl *ebl);
320 static void print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr);
321 static void dump_data (Ebl *ebl);
322 static void dump_strings (Ebl *ebl);
323 static void print_strings (Ebl *ebl);
324 static void dump_archive_index (Elf *, const char *);
339 /* Declarations of local functions for use-dynamic. */
340 static Elf_Data *get_dynscn_strtab (Elf *elf, GElf_Phdr *phdr);
341 static void get_dynscn_addrs (Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max]);
342 static void find_offsets (Elf *elf, GElf_Addr main_bias, size_t n,
343 GElf_Addr addrs[n], GElf_Off offs[n]);
345 /* Looked up once with gettext in main. */
346 static char *yes_str;
350 cleanup_list (struct section_argument *list)
354 struct section_argument *a = list;
361 main (int argc, char *argv[])
363 /* We use no threads here which can interfere with handling a stream. */
364 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
367 setlocale (LC_ALL, "");
369 /* Initialize the message catalog. */
370 textdomain (PACKAGE_TARNAME);
376 /* Parse and process arguments. */
378 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
380 /* Before we start tell the ELF library which version we are using. */
381 elf_version (EV_CURRENT);
383 /* Now process all the files given at the command line. */
384 bool only_one = remaining + 1 == argc;
388 int fd = open (argv[remaining], O_RDONLY);
391 error (0, errno, _("cannot open input file '%s'"), argv[remaining]);
395 process_file (fd, argv[remaining], only_one);
399 while (++remaining < argc);
401 cleanup_list (dump_data_sections);
402 cleanup_list (string_sections);
404 return error_message_count != 0;
408 add_dump_section (const char *name,
412 struct section_argument *a = xmalloc (sizeof *a);
415 a->implicit = implicit;
416 struct section_argument ***tailp
417 = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
422 /* Handle program arguments. */
424 parse_opt (int key, char *arg,
425 struct argp_state *state __attribute__ ((unused)))
430 print_file_header = true;
431 print_program_header = true;
432 print_relocations = true;
433 print_section_header = true;
434 print_symbol_table = true;
435 print_version_info = true;
436 print_dynamic_table = true;
437 print_section_groups = true;
438 print_histogram = true;
441 implicit_debug_sections |= section_exception;
442 add_dump_section (".strtab", key, true);
443 add_dump_section (".dynstr", key, true);
444 add_dump_section (".comment", key, true);
445 any_control_option = true;
449 any_control_option = true;
452 print_dynamic_table = true;
453 any_control_option = true;
456 use_dynamic_segment = true;
459 print_debug_sections |= section_exception;
460 any_control_option = true;
463 print_section_groups = true;
464 any_control_option = true;
467 print_file_header = true;
468 any_control_option = true;
471 print_histogram = true;
472 any_control_option = true;
475 print_program_header = true;
476 any_control_option = true;
480 any_control_option = true;
484 print_relocations = true;
485 any_control_option = true;
488 print_section_header = true;
489 any_control_option = true;
492 print_symbol_table = true;
493 any_control_option = true;
494 symbol_table_section = arg;
496 case PRINT_DYNSYM_TABLE:
497 print_dynsym_table = true;
498 any_control_option = true;
501 print_version_info = true;
502 any_control_option = true;
505 print_archive_index = true;
510 print_debug_sections = section_all;
511 implicit_debug_sections = section_info;
512 show_split_units = true;
514 else if (strcmp (arg, "abbrev") == 0)
515 print_debug_sections |= section_abbrev;
516 else if (strcmp (arg, "addr") == 0)
518 print_debug_sections |= section_addr;
519 implicit_debug_sections |= section_info;
521 else if (strcmp (arg, "aranges") == 0)
522 print_debug_sections |= section_aranges;
523 else if (strcmp (arg, "decodedaranges") == 0)
525 print_debug_sections |= section_aranges;
526 decodedaranges = true;
528 else if (strcmp (arg, "ranges") == 0)
530 print_debug_sections |= section_ranges;
531 implicit_debug_sections |= section_info;
533 else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
534 print_debug_sections |= section_frame;
535 else if (strcmp (arg, "info") == 0)
537 print_debug_sections |= section_info;
538 print_debug_sections |= section_types;
540 else if (strcmp (arg, "info+") == 0)
542 print_debug_sections |= section_info;
543 print_debug_sections |= section_types;
544 show_split_units = true;
546 else if (strcmp (arg, "loc") == 0)
548 print_debug_sections |= section_loc;
549 implicit_debug_sections |= section_info;
551 else if (strcmp (arg, "line") == 0)
552 print_debug_sections |= section_line;
553 else if (strcmp (arg, "decodedline") == 0)
555 print_debug_sections |= section_line;
558 else if (strcmp (arg, "pubnames") == 0)
559 print_debug_sections |= section_pubnames;
560 else if (strcmp (arg, "str") == 0)
562 print_debug_sections |= section_str;
563 /* For mapping string offset tables to CUs. */
564 implicit_debug_sections |= section_info;
566 else if (strcmp (arg, "macinfo") == 0)
567 print_debug_sections |= section_macinfo;
568 else if (strcmp (arg, "macro") == 0)
569 print_debug_sections |= section_macro;
570 else if (strcmp (arg, "exception") == 0)
571 print_debug_sections |= section_exception;
572 else if (strcmp (arg, "gdb_index") == 0)
573 print_debug_sections |= section_gdb_index;
576 fprintf (stderr, _("Unknown DWARF debug section `%s'.\n"),
578 argp_help (&argp, stderr, ARGP_HELP_SEE,
579 program_invocation_short_name);
582 any_control_option = true;
585 any_control_option = true;
588 print_string_sections = true;
593 add_dump_section (arg, key, false);
594 any_control_option = true;
597 print_address_names = false;
600 print_unresolved_addresses = true;
602 case ARGP_KEY_NO_ARGS:
603 fputs (_("Missing file name.\n"), stderr);
606 if (! any_control_option && ! print_archive_index)
608 fputs (_("No operation specified.\n"), stderr);
610 argp_help (&argp, stderr, ARGP_HELP_SEE,
611 program_invocation_short_name);
615 case 'W': /* Ignored. */
618 print_decompress = true;
620 case ELF_INPUT_SECTION:
622 elf_input_section = ".gnu_debugdata";
624 elf_input_section = arg;
627 dwarf_skeleton = arg;
630 return ARGP_ERR_UNKNOWN;
636 /* Create a file descriptor to read the data from the
637 elf_input_section given a file descriptor to an ELF file. */
639 open_input_section (int fd)
644 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
647 error (0, 0, _("cannot generate Elf descriptor: %s"),
652 if (elf_getshdrnum (elf, &shnums) < 0)
654 error (0, 0, _("cannot determine number of sections: %s"),
661 if (elf_getshdrstrndx (elf, &shstrndx) < 0)
663 error (0, 0, _("cannot get section header string table index"));
667 for (cnt = 0; cnt < shnums; ++cnt)
669 Elf_Scn *scn = elf_getscn (elf, cnt);
672 error (0, 0, _("cannot get section: %s"),
678 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
679 if (unlikely (shdr == NULL))
681 error (0, 0, _("cannot get section header: %s"),
686 const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
689 error (0, 0, _("cannot get section name"));
693 if (strcmp (sname, elf_input_section) == 0)
695 Elf_Data *data = elf_rawdata (scn, NULL);
698 error (0, 0, _("cannot get %s content: %s"),
699 sname, elf_errmsg (-1));
703 /* Create (and immediately unlink) a temporary file to store
704 section data in to create a file descriptor for it. */
705 const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
706 static const char suffix[] = "/readelfXXXXXX";
707 int tmplen = strlen (tmpdir) + sizeof (suffix);
708 char *tempname = alloca (tmplen);
709 sprintf (tempname, "%s%s", tmpdir, suffix);
711 int sfd = mkstemp (tempname);
714 error (0, 0, _("cannot create temp file '%s'"),
720 ssize_t size = data->d_size;
721 if (write_retry (sfd, data->d_buf, size) != size)
723 error (0, 0, _("cannot write section data"));
727 if (elf_end (elf) != 0)
729 error (0, 0, _("error while closing Elf descriptor: %s"),
734 if (lseek (sfd, 0, SEEK_SET) == -1)
736 error (0, 0, _("error while rewinding file descriptor"));
744 /* Named section not found. */
745 if (elf_end (elf) != 0)
746 error (0, 0, _("error while closing Elf descriptor: %s"),
751 /* Check if the file is an archive, and if so dump its index. */
753 check_archive_index (int fd, const char *fname, bool only_one)
755 /* Create an `Elf' descriptor. */
756 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
758 error (0, 0, _("cannot generate Elf descriptor: %s"),
762 if (elf_kind (elf) == ELF_K_AR)
765 printf ("\n%s:\n\n", fname);
766 dump_archive_index (elf, fname);
770 _("'%s' is not an archive, cannot print archive index"),
773 /* Now we can close the descriptor. */
774 if (elf_end (elf) != 0)
775 error (0, 0, _("error while closing Elf descriptor: %s"),
780 /* Trivial callback used for checking if we opened an archive. */
782 count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
783 void **userdata __attribute__ ((unused)),
784 const char *name __attribute__ ((unused)),
785 Dwarf_Addr base __attribute__ ((unused)),
789 return DWARF_CB_ABORT;
790 *(bool *) arg = true;
794 struct process_dwflmod_args
801 process_dwflmod (Dwfl_Module *dwflmod,
802 void **userdata __attribute__ ((unused)),
803 const char *name __attribute__ ((unused)),
804 Dwarf_Addr base __attribute__ ((unused)),
807 const struct process_dwflmod_args *a = arg;
809 /* Print the file name. */
813 dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
815 printf ("\n%s:\n\n", fname);
818 process_elf_file (dwflmod, a->fd);
823 /* Stub libdwfl callback, only the ELF handle already open is ever used.
824 Only used for finding the alternate debug file if the Dwarf comes from
825 the main file. We are not interested in separate debuginfo. */
827 find_no_debuginfo (Dwfl_Module *mod,
831 const char *file_name,
832 const char *debuglink_file,
833 GElf_Word debuglink_crc,
834 char **debuginfo_file_name)
837 dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
839 /* We are only interested if the Dwarf has been setup on the main
840 elf file but is only missing the alternate debug link. If dwbias
841 hasn't even been setup, this is searching for separate debuginfo
842 for the main elf. We don't care in that case. */
843 if (dwbias == (Dwarf_Addr) -1)
846 return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
847 file_name, debuglink_file,
848 debuglink_crc, debuginfo_file_name);
852 create_dwfl (int fd, const char *fname)
854 /* Duplicate an fd for dwfl_report_offline to swallow. */
855 int dwfl_fd = dup (fd);
856 if (unlikely (dwfl_fd < 0))
857 error_exit (errno, "dup");
859 /* Use libdwfl in a trivial way to open the libdw handle for us.
860 This takes care of applying relocations to DWARF data in ET_REL files. */
861 static const Dwfl_Callbacks callbacks =
863 .section_address = dwfl_offline_section_address,
864 .find_debuginfo = find_no_debuginfo
866 Dwfl *dwfl = dwfl_begin (&callbacks);
867 if (likely (dwfl != NULL))
868 /* Let 0 be the logical address of the file (or first in archive). */
869 dwfl->offline_next_address = 0;
870 if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
873 if (fstat (dwfl_fd, &st) != 0)
874 error (0, errno, _("cannot stat input file"));
875 else if (unlikely (st.st_size == 0))
876 error (0, 0, _("input file is empty"));
878 error (0, 0, _("failed reading '%s': %s"),
879 fname, dwfl_errmsg (-1));
880 close (dwfl_fd); /* Consumed on success, not on failure. */
884 dwfl_report_end (dwfl, NULL, NULL);
889 /* Process one input file. */
891 process_file (int fd, const char *fname, bool only_one)
893 if (print_archive_index)
894 check_archive_index (fd, fname, only_one);
896 if (!any_control_option)
899 if (elf_input_section != NULL)
901 /* Replace fname and fd with section content. */
902 char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
903 sprintf (fnname, "%s:%s", fname, elf_input_section);
904 fd = open_input_section (fd);
907 error (0, 0, _("No such section '%s' in '%s'"),
908 elf_input_section, fname);
914 Dwfl *dwfl = create_dwfl (fd, fname);
919 /* Clear ONLY_ONE if we have multiple modules, from an archive. */
921 only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
924 /* Process the one or more modules gleaned from this file. */
925 struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
926 dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
928 /* Terrible hack for hooking unrelated skeleton/split compile units,
929 see __libdw_link_skel_split in print_debug. */
930 if (! do_not_close_dwfl)
933 /* Need to close the replaced fd if we created it. Caller takes
935 if (elf_input_section != NULL)
939 /* Check whether there are any compressed sections in the ELF file. */
941 elf_contains_chdrs (Elf *elf)
944 while ((scn = elf_nextscn (elf, scn)) != NULL)
947 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
948 if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
954 /* Process one ELF file. */
956 process_elf_file (Dwfl_Module *dwflmod, int fd)
959 Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
962 GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
966 error (0, 0, _("cannot read ELF header: %s"), elf_errmsg (-1));
970 Ebl *ebl = ebl_openbackend (elf);
971 if (unlikely (ebl == NULL))
974 error (0, errno, _("cannot create EBL handle"));
978 /* Determine the number of sections. */
979 if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
980 error_exit (0, _("cannot determine number of sections: %s"),
983 /* Determine the number of phdrs. */
984 if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
985 error_exit (0, _("cannot determine number of program headers: %s"),
988 /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
989 may have applied relocation to some sections. If there are any
990 compressed sections, any pass (or libdw/libdwfl) might have
991 uncompressed them. So we need to get a fresh Elf handle on the
992 file to display those. */
993 bool print_unchanged = ((print_section_header
995 || dump_data_sections != NULL
997 && (ehdr->e_type == ET_REL
998 || elf_contains_chdrs (ebl->elf)));
1000 Elf *pure_elf = NULL;
1001 Ebl *pure_ebl = ebl;
1002 if (print_unchanged)
1004 /* Read the file afresh. */
1005 off_t aroff = elf_getaroff (elf);
1006 pure_elf = dwelf_elf_begin (fd);
1009 /* Archive member. */
1010 (void) elf_rand (pure_elf, aroff);
1011 Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
1015 if (pure_elf == NULL)
1017 error (0, 0, _("cannot read ELF: %s"), elf_errmsg (-1));
1020 pure_ebl = ebl_openbackend (pure_elf);
1021 if (pure_ebl == NULL)
1025 bool symtab_printed = false;
1027 if (print_file_header)
1028 print_ehdr (ebl, ehdr);
1029 if (print_section_header)
1030 print_shdr (pure_ebl, ehdr);
1031 if (print_program_header)
1032 print_phdr (ebl, ehdr);
1033 if (print_section_groups)
1035 if (print_dynamic_table)
1036 print_dynamic (ebl);
1037 if (print_relocations)
1038 print_relocs (pure_ebl, ehdr);
1039 if (print_histogram)
1041 if (print_symbol_table || print_dynsym_table)
1042 symtab_printed |= print_symtab (ebl, SHT_DYNSYM);
1043 if (print_version_info)
1044 print_verinfo (ebl);
1045 if (print_symbol_table)
1046 symtab_printed |= print_symtab (ebl, SHT_SYMTAB);
1048 if ((print_symbol_table || print_dynsym_table)
1049 && !symtab_printed && symbol_table_section != NULL)
1050 printf ("WARNING: %s: '%s'\n", _("cannot find section"),
1051 symbol_table_section);
1054 print_liblist (ebl);
1056 print_attributes (ebl, ehdr);
1057 if (dump_data_sections != NULL)
1058 dump_data (pure_ebl);
1059 if (string_sections != NULL)
1061 if ((print_debug_sections | implicit_debug_sections) != 0)
1062 print_debug (dwflmod, ebl, ehdr);
1064 handle_notes (pure_ebl, ehdr);
1065 if (print_string_sections)
1066 print_strings (ebl);
1068 ebl_closebackend (ebl);
1070 if (pure_ebl != ebl)
1072 ebl_closebackend (pure_ebl);
1078 /* Print file type. */
1080 print_file_type (unsigned short int e_type)
1082 if (likely (e_type <= ET_CORE))
1084 static const char *const knowntypes[] =
1087 N_("REL (Relocatable file)"),
1088 N_("EXEC (Executable file)"),
1089 N_("DYN (Shared object file)"),
1090 N_("CORE (Core file)")
1092 puts (_(knowntypes[e_type]));
1094 else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1095 printf (_("OS Specific: (%x)\n"), e_type);
1096 else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1097 printf (_("Processor Specific: (%x)\n"), e_type);
1103 /* Print ELF header. */
1105 print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1107 fputs_unlocked (_("ELF Header:\n Magic: "), stdout);
1108 for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1109 printf (" %02hhx", ehdr->e_ident[cnt]);
1111 printf (_("\n Class: %s\n"),
1112 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1113 : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1116 printf (_(" Data: %s\n"),
1117 ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1118 ? "2's complement, little endian"
1119 : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1120 ? "2's complement, big endian" : "\?\?\?");
1122 printf (_(" Ident Version: %hhd %s\n"),
1123 ehdr->e_ident[EI_VERSION],
1124 ehdr->e_ident[EI_VERSION] == EV_CURRENT ? _("(current)")
1128 printf (_(" OS/ABI: %s\n"),
1129 ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1131 printf (_(" ABI Version: %hhd\n"),
1132 ehdr->e_ident[EI_ABIVERSION]);
1134 fputs_unlocked (_(" Type: "), stdout);
1135 print_file_type (ehdr->e_type);
1137 const char *machine = dwelf_elf_e_machine_string (ehdr->e_machine);
1138 if (machine != NULL)
1139 printf (_(" Machine: %s\n"), machine);
1141 printf (_(" Machine: <unknown>: 0x%x\n"),
1144 printf (_(" Version: %d %s\n"),
1146 ehdr->e_version == EV_CURRENT ? _("(current)") : "(\?\?\?)");
1148 printf (_(" Entry point address: %#" PRIx64 "\n"),
1151 printf (_(" Start of program headers: %" PRId64 " %s\n"),
1152 ehdr->e_phoff, _("(bytes into file)"));
1154 printf (_(" Start of section headers: %" PRId64 " %s\n"),
1155 ehdr->e_shoff, _("(bytes into file)"));
1157 printf (_(" Flags: %s\n"),
1158 ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1160 printf (_(" Size of this header: %" PRId16 " %s\n"),
1161 ehdr->e_ehsize, _("(bytes)"));
1163 printf (_(" Size of program header entries: %" PRId16 " %s\n"),
1164 ehdr->e_phentsize, _("(bytes)"));
1166 printf (_(" Number of program headers entries: %" PRId16),
1168 if (ehdr->e_phnum == PN_XNUM)
1171 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1173 printf (_(" (%" PRIu32 " in [0].sh_info)"),
1174 (uint32_t) shdr->sh_info);
1176 fputs_unlocked (_(" ([0] not available)"), stdout);
1178 fputc_unlocked ('\n', stdout);
1180 printf (_(" Size of section header entries: %" PRId16 " %s\n"),
1181 ehdr->e_shentsize, _("(bytes)"));
1183 printf (_(" Number of section headers entries: %" PRId16),
1185 if (ehdr->e_shnum == 0)
1188 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1190 printf (_(" (%" PRIu32 " in [0].sh_size)"),
1191 (uint32_t) shdr->sh_size);
1193 fputs_unlocked (_(" ([0] not available)"), stdout);
1195 fputc_unlocked ('\n', stdout);
1197 if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1200 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1202 /* We managed to get the zeroth section. */
1203 snprintf (buf, sizeof (buf), _(" (%" PRIu32 " in [0].sh_link)"),
1204 (uint32_t) shdr->sh_link);
1207 strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1);
1208 buf[sizeof (buf) - 1] = '\0';
1211 printf (_(" Section header string table index: XINDEX%s\n\n"),
1215 printf (_(" Section header string table index: %" PRId16 "\n\n"),
1221 get_visibility_type (int value)
1239 elf_ch_type_name (unsigned int code)
1245 case ELFCOMPRESS_ZLIB:
1247 case ELFCOMPRESS_ZSTD:
1254 /* Print the section headers. */
1256 print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1261 if (! print_file_header)
1264 if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1265 error_exit (0, _("cannot get number of sections: %s"),
1269 There are %zd section headers, starting at offset %#" PRIx64 ":\n\
1271 sections, ehdr->e_shoff);
1274 /* Get the section header string table index. */
1275 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1276 error_exit (0, _("cannot get section header string table index: %s"),
1279 puts (_("Section Headers:"));
1281 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1282 puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1284 puts (_("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1286 if (print_decompress)
1288 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1289 puts (_(" [Compression Size Al]"));
1291 puts (_(" [Compression Size Al]"));
1294 for (cnt = 0; cnt < shnum; ++cnt)
1296 Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1298 if (unlikely (scn == NULL))
1299 error_exit (0, _("cannot get section: %s"),
1302 /* Get the section header. */
1304 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1305 if (unlikely (shdr == NULL))
1306 error_exit (0, _("cannot get section header: %s"),
1311 if (shdr->sh_flags & SHF_WRITE)
1313 if (shdr->sh_flags & SHF_ALLOC)
1315 if (shdr->sh_flags & SHF_EXECINSTR)
1317 if (shdr->sh_flags & SHF_MERGE)
1319 if (shdr->sh_flags & SHF_STRINGS)
1321 if (shdr->sh_flags & SHF_INFO_LINK)
1323 if (shdr->sh_flags & SHF_LINK_ORDER)
1325 if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1327 if (shdr->sh_flags & SHF_GROUP)
1329 if (shdr->sh_flags & SHF_TLS)
1331 if (shdr->sh_flags & SHF_COMPRESSED)
1333 if (shdr->sh_flags & SHF_ORDERED)
1335 if (shdr->sh_flags & SHF_EXCLUDE)
1337 if (shdr->sh_flags & SHF_GNU_RETAIN)
1343 sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1344 printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1345 " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1348 ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
1349 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
1350 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
1351 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
1352 shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
1353 shdr->sh_addralign);
1355 if (print_decompress)
1357 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1360 if (gelf_getchdr (scn, &chdr) != NULL)
1361 printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1363 elf_ch_type_name (chdr.ch_type),
1365 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1366 chdr.ch_size, chdr.ch_addralign);
1369 _("bad compression header for section %zd: %s"),
1370 elf_ndxscn (scn), elf_errmsg (-1));
1372 else if (startswith (sname, ".zdebug"))
1375 if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1376 printf (" [GNU ZLIB %0*zx ]\n",
1377 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1380 _("bad gnu compressed size for section %zd: %s"),
1381 elf_ndxscn (scn), elf_errmsg (-1));
1386 fputc_unlocked ('\n', stdout);
1390 /* Print the program header. */
1392 print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1395 /* No program header, this is OK in relocatable objects. */
1398 puts (_("Program Headers:"));
1399 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1401 Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1404 Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1406 /* Process all program headers. */
1407 bool has_relro = false;
1408 GElf_Addr relro_from = 0;
1409 GElf_Addr relro_to = 0;
1410 for (size_t cnt = 0; cnt < phnum; ++cnt)
1414 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1416 /* If for some reason the header cannot be returned show this. */
1417 if (unlikely (phdr == NULL))
1423 printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
1424 " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
1425 ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1427 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1428 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1431 phdr->p_flags & PF_R ? 'R' : ' ',
1432 phdr->p_flags & PF_W ? 'W' : ' ',
1433 phdr->p_flags & PF_X ? 'E' : ' ',
1436 if (phdr->p_type == PT_INTERP)
1438 /* If we are sure the file offset is valid then we can show
1439 the user the name of the interpreter. We check whether
1440 there is a section at the file offset. Normally there
1441 would be a section called ".interp". But in separate
1442 .debug files it is a NOBITS section (and so doesn't match
1443 with gelf_offscn). Which probably means the offset is
1444 not valid another reason could be because the ELF file
1445 just doesn't contain any section headers, in that case
1446 just play it safe and don't display anything. */
1448 Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1450 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1453 char *filedata = elf_rawfile (ebl->elf, &maxsize);
1455 if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1456 && filedata != NULL && phdr->p_offset < maxsize
1457 && phdr->p_filesz <= maxsize - phdr->p_offset
1458 && memchr (filedata + phdr->p_offset, '\0',
1459 phdr->p_filesz) != NULL)
1460 printf (_("\t[Requesting program interpreter: %s]\n"),
1461 filedata + phdr->p_offset);
1463 else if (phdr->p_type == PT_GNU_RELRO)
1466 relro_from = phdr->p_vaddr;
1467 relro_to = relro_from + phdr->p_memsz;
1472 if (unlikely (elf_getshdrnum (ebl->elf, §ions) < 0))
1473 error_exit (0, _("cannot get number of sections: %s"),
1477 /* No sections in the file. Punt. */
1480 /* Get the section header string table index. */
1482 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1483 error_exit (0, _("cannot get section header string table index"));
1485 puts (_("\n Section to Segment mapping:\n Segment Sections..."));
1487 for (size_t cnt = 0; cnt < phnum; ++cnt)
1489 /* Print the segment number. */
1490 printf (" %2.2zu ", cnt);
1493 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1494 /* This must not happen. */
1495 if (unlikely (phdr == NULL))
1496 error_exit (0, _("cannot get program header: %s"),
1499 /* Iterate over the sections. */
1500 bool in_relro = false;
1502 for (size_t inner = 1; inner < shnum; ++inner)
1504 Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1505 /* This should not happen. */
1506 if (unlikely (scn == NULL))
1507 error_exit (0, _("cannot get section: %s"),
1510 /* Get the section header. */
1512 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1513 if (unlikely (shdr == NULL))
1514 error_exit (0, _("cannot get section header: %s"),
1517 if (shdr->sh_size > 0
1518 /* Compare allocated sections by VMA, unallocated
1519 sections by file offset. */
1520 && (shdr->sh_flags & SHF_ALLOC
1521 ? (shdr->sh_addr >= phdr->p_vaddr
1522 && (shdr->sh_addr + shdr->sh_size
1523 <= phdr->p_vaddr + phdr->p_memsz))
1524 : (shdr->sh_offset >= phdr->p_offset
1525 && (shdr->sh_offset + shdr->sh_size
1526 <= phdr->p_offset + phdr->p_filesz))))
1528 if (has_relro && !in_relro
1529 && shdr->sh_addr >= relro_from
1530 && shdr->sh_addr + shdr->sh_size <= relro_to)
1532 fputs_unlocked (" [RELRO:", stdout);
1535 else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1537 fputs_unlocked ("]", stdout);
1540 else if (has_relro && in_relro
1541 && shdr->sh_addr + shdr->sh_size > relro_to)
1542 fputs_unlocked ("] <RELRO:", stdout);
1543 else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1547 fputs_unlocked (" [RO:", stdout);
1553 /* Determine the segment this section is part of. */
1555 GElf_Phdr phdr2_mem;
1556 GElf_Phdr *phdr2 = NULL;
1557 for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1559 phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1561 if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1562 && shdr->sh_addr >= phdr2->p_vaddr
1563 && (shdr->sh_addr + shdr->sh_size
1564 <= phdr2->p_vaddr + phdr2->p_memsz))
1570 if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1572 fputs_unlocked (" [RO:", stdout);
1575 else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1577 fputs_unlocked ("]", stdout);
1584 elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1586 /* Signal that this section is only partially covered. */
1587 if (has_relro && in_relro
1588 && shdr->sh_addr + shdr->sh_size > relro_to)
1590 fputs_unlocked (">", stdout);
1595 if (in_relro || in_ro)
1596 fputs_unlocked ("]", stdout);
1598 /* Finish the line. */
1599 fputc_unlocked ('\n', stdout);
1605 section_name (Ebl *ebl, GElf_Shdr *shdr)
1608 if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
1610 return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
1615 handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1617 /* Get the data of the section. */
1618 Elf_Data *data = elf_getdata (scn, NULL);
1620 Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1621 GElf_Shdr symshdr_mem;
1622 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1623 Elf_Data *symdata = elf_getdata (symscn, NULL);
1625 if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1629 /* Get the section header string table index. */
1631 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1632 error_exit (0, _("cannot get section header string table index"));
1634 Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1637 GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1639 printf ((grpref[0] & GRP_COMDAT)
1641 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1643 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1644 data->d_size / sizeof (Elf32_Word) - 1)
1646 \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
1647 \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1648 data->d_size / sizeof (Elf32_Word) - 1),
1650 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1652 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1653 ?: _("<INVALID SYMBOL>"),
1654 data->d_size / sizeof (Elf32_Word) - 1);
1656 for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1658 GElf_Shdr grpshdr_mem;
1659 GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1663 printf (" [%2u] %s\n",
1666 && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1667 ? str : _("<INVALID SECTION>"));
1673 print_scngrp (Ebl *ebl)
1675 /* Find all relocation sections and handle them. */
1676 Elf_Scn *scn = NULL;
1678 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1680 /* Handle the section if it is a symbol table. */
1682 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1684 if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1686 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1688 if (elf_compress (scn, 0, 0) < 0)
1689 printf ("WARNING: %s [%zd]\n",
1690 _("Couldn't uncompress section"),
1692 shdr = gelf_getshdr (scn, &shdr_mem);
1693 if (unlikely (shdr == NULL))
1694 error_exit (0, _("cannot get section [%zd] header: %s"),
1698 handle_scngrp (ebl, scn, shdr);
1704 static const struct flags
1710 { DF_ORIGIN, "ORIGIN" },
1711 { DF_SYMBOLIC, "SYMBOLIC" },
1712 { DF_TEXTREL, "TEXTREL" },
1713 { DF_BIND_NOW, "BIND_NOW" },
1714 { DF_STATIC_TLS, "STATIC_TLS" }
1716 static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1718 static const struct flags dt_flags_1[] =
1720 { DF_1_NOW, "NOW" },
1721 { DF_1_GLOBAL, "GLOBAL" },
1722 { DF_1_GROUP, "GROUP" },
1723 { DF_1_NODELETE, "NODELETE" },
1724 { DF_1_LOADFLTR, "LOADFLTR" },
1725 { DF_1_INITFIRST, "INITFIRST" },
1726 { DF_1_NOOPEN, "NOOPEN" },
1727 { DF_1_ORIGIN, "ORIGIN" },
1728 { DF_1_DIRECT, "DIRECT" },
1729 { DF_1_TRANS, "TRANS" },
1730 { DF_1_INTERPOSE, "INTERPOSE" },
1731 { DF_1_NODEFLIB, "NODEFLIB" },
1732 { DF_1_NODUMP, "NODUMP" },
1733 { DF_1_CONFALT, "CONFALT" },
1734 { DF_1_ENDFILTEE, "ENDFILTEE" },
1735 { DF_1_DISPRELDNE, "DISPRELDNE" },
1736 { DF_1_DISPRELPND, "DISPRELPND" },
1738 static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1740 static const struct flags dt_feature_1[] =
1742 { DTF_1_PARINIT, "PARINIT" },
1743 { DTF_1_CONFEXP, "CONFEXP" }
1745 static const int ndt_feature_1 = (sizeof (dt_feature_1)
1746 / sizeof (dt_feature_1[0]));
1748 static const struct flags dt_posflag_1[] =
1750 { DF_P1_LAZYLOAD, "LAZYLOAD" },
1751 { DF_P1_GROUPPERM, "GROUPPERM" }
1753 static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1754 / sizeof (dt_posflag_1[0]));
1758 print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1764 for (cnt = 0; cnt < nflags; ++cnt)
1765 if (d_val & flags[cnt].mask)
1768 putchar_unlocked (' ');
1769 fputs_unlocked (flags[cnt].str, stdout);
1770 d_val &= ~flags[cnt].mask;
1777 putchar_unlocked (' ');
1778 printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1781 putchar_unlocked ('\n');
1786 print_dt_flags (int class, GElf_Xword d_val)
1788 print_flags (class, d_val, dt_flags, ndt_flags);
1793 print_dt_flags_1 (int class, GElf_Xword d_val)
1795 print_flags (class, d_val, dt_flags_1, ndt_flags_1);
1800 print_dt_feature_1 (int class, GElf_Xword d_val)
1802 print_flags (class, d_val, dt_feature_1, ndt_feature_1);
1807 print_dt_posflag_1 (int class, GElf_Xword d_val)
1809 print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
1814 get_dyn_ents (Elf_Data * dyn_data)
1821 dyn = gelf_getdyn(dyn_data, dyn_idx, &dyn_mem);
1825 while (dyn != NULL && dyn->d_tag != DT_NULL);
1832 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
1834 int class = gelf_getclass (ebl->elf);
1835 GElf_Shdr glink_mem;
1842 /* Get the data of the section. */
1843 if (use_dynamic_segment && phdr != NULL)
1844 data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
1845 phdr->p_filesz, ELF_T_DYN);
1847 data = elf_getdata (scn, NULL);
1852 /* Get the dynamic section entry number */
1853 dyn_ents = get_dyn_ents (data);
1855 if (!use_dynamic_segment && shdr != NULL)
1857 /* Get the section header string table index. */
1858 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1859 error_exit (0, _("cannot get section header string table index"));
1861 glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1863 error_exit (0, _("invalid sh_link value in section %zu"),
1866 printf (ngettext ("\
1867 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1869 \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1871 (unsigned long int) dyn_ents,
1872 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1874 (int) shdr->sh_link,
1875 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1877 else if (phdr != NULL)
1879 printf (ngettext ("\
1880 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
1882 \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 "\n",
1884 (unsigned long int) dyn_ents,
1885 class == ELFCLASS32 ? 10 : 18, phdr->p_paddr,
1889 fputs_unlocked (_(" Type Value\n"), stdout);
1891 /* if --use-dynamic option is enabled,
1892 use the string table to get the related library info. */
1893 Elf_Data *strtab_data = NULL;
1894 if (use_dynamic_segment && phdr != NULL)
1896 strtab_data = get_dynscn_strtab(ebl->elf, phdr);
1897 if (strtab_data == NULL)
1898 error_exit (0, _("cannot get string table by using dynamic segment"));
1901 for (cnt = 0; cnt < dyn_ents; ++cnt)
1904 GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
1910 ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
1913 if (dyn->d_tag == DT_NEEDED
1914 || dyn->d_tag == DT_SONAME
1915 || dyn->d_tag == DT_RPATH
1916 || dyn->d_tag == DT_RUNPATH)
1918 if (! use_dynamic_segment && shdr != NULL)
1919 name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
1920 else if (dyn->d_un.d_val < strtab_data->d_size
1921 && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
1922 strtab_data->d_size - 1 - dyn->d_un.d_val) != NULL)
1923 name = ((char *) strtab_data->d_buf) + dyn->d_un.d_val;
1932 /* No further output. */
1933 fputc_unlocked ('\n', stdout);
1937 printf (_("Shared library: [%s]\n"), name);
1941 printf (_("Library soname: [%s]\n"), name);
1945 printf (_("Library rpath: [%s]\n"), name);
1949 printf (_("Library runpath: [%s]\n"), name);
1962 case DT_INIT_ARRAYSZ:
1963 case DT_FINI_ARRAYSZ:
1966 case DT_GNU_CONFLICTSZ:
1967 case DT_GNU_LIBLISTSZ:
1968 printf (_("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
1975 printf ("%" PRId64 "\n", dyn->d_un.d_val);
1979 const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
1981 puts (tagname ?: "???");
1985 print_dt_flags (class, dyn->d_un.d_val);
1989 print_dt_flags_1 (class, dyn->d_un.d_val);
1993 print_dt_feature_1 (class, dyn->d_un.d_val);
1997 print_dt_posflag_1 (class, dyn->d_un.d_val);
2001 printf ("%#0*" PRIx64 "\n",
2002 class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
2009 /* Print the dynamic segment. */
2011 print_dynamic (Ebl *ebl)
2013 for (size_t i = 0; i < phnum; ++i)
2016 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
2018 if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
2020 Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
2022 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2023 if ((use_dynamic_segment && phdr != NULL)
2024 || (shdr != NULL && shdr->sh_type == SHT_DYNAMIC))
2025 handle_dynamic (ebl, scn, shdr, phdr);
2032 /* Print relocations. */
2034 print_relocs (Ebl *ebl, GElf_Ehdr *ehdr)
2036 /* Find all relocation sections and handle them. */
2037 Elf_Scn *scn = NULL;
2039 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2041 /* Handle the section if it is a symbol table. */
2043 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2045 if (likely (shdr != NULL))
2047 if (shdr->sh_type == SHT_REL)
2048 handle_relocs_rel (ebl, ehdr, scn, shdr);
2049 else if (shdr->sh_type == SHT_RELA)
2050 handle_relocs_rela (ebl, ehdr, scn, shdr);
2056 /* Handle a relocation section. */
2058 handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2060 int class = gelf_getclass (ebl->elf);
2061 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
2062 int nentries = shdr->sh_size / sh_entsize;
2064 /* Get the data of the section. */
2065 Elf_Data *data = elf_getdata (scn, NULL);
2069 /* Get the symbol table information. */
2070 Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2071 GElf_Shdr symshdr_mem;
2072 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2073 Elf_Data *symdata = elf_getdata (symscn, NULL);
2075 /* Get the section header of the section the relocations are for. */
2076 GElf_Shdr destshdr_mem;
2077 GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2080 if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2082 printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2087 /* Search for the optional extended section index table. */
2088 Elf_Data *xndxdata = NULL;
2089 int xndxscnidx = elf_scnshndx (scn);
2090 if (unlikely (xndxscnidx > 0))
2091 xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2093 /* Get the section header string table index. */
2095 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2096 error_exit (0, _("cannot get section header string table index"));
2098 if (shdr->sh_info != 0)
2099 printf (ngettext ("\
2100 \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2102 \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2105 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2106 (unsigned int) shdr->sh_info,
2107 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2111 /* The .rel.dyn section does not refer to a specific section but
2112 instead of section index zero. Do not try to print a section
2114 printf (ngettext ("\
2115 \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2117 \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2119 (unsigned int) elf_ndxscn (scn),
2120 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2123 fputs_unlocked (class == ELFCLASS32
2125 Offset Type Value Name\n")
2127 Offset Type Value Name\n"),
2130 int is_statically_linked = 0;
2131 for (int cnt = 0; cnt < nentries; ++cnt)
2134 GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
2135 if (likely (rel != NULL))
2140 GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2141 GELF_R_SYM (rel->r_info),
2143 if (unlikely (sym == NULL))
2145 /* As a special case we have to handle relocations in static
2146 executables. This only happens for IRELATIVE relocations
2147 (so far). There is no symbol table. */
2148 if (is_statically_linked == 0)
2150 /* Find the program header and look for a PT_INTERP entry. */
2151 is_statically_linked = -1;
2152 if (ehdr->e_type == ET_EXEC)
2154 is_statically_linked = 1;
2156 for (size_t inner = 0; inner < phnum; ++inner)
2159 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2161 if (phdr != NULL && phdr->p_type == PT_INTERP)
2163 is_statically_linked = -1;
2170 if (is_statically_linked > 0 && shdr->sh_link == 0)
2172 %#0*" PRIx64 " %-20s %*s %s\n",
2173 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2174 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2175 /* Avoid the leading R_ which isn't carrying any
2177 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2178 buf, sizeof (buf)) + 2
2179 : _("<INVALID RELOC>"),
2180 class == ELFCLASS32 ? 10 : 18, "",
2181 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2183 printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2184 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2185 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2186 /* Avoid the leading R_ which isn't carrying any
2188 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2189 buf, sizeof (buf)) + 2
2190 : _("<INVALID RELOC>"),
2191 _("INVALID SYMBOL"),
2192 (long int) GELF_R_SYM (rel->r_info));
2194 else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2195 printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2196 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2197 likely (ebl_reloc_type_check (ebl,
2198 GELF_R_TYPE (rel->r_info)))
2199 /* Avoid the leading R_ which isn't carrying any
2201 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2202 buf, sizeof (buf)) + 2
2203 : _("<INVALID RELOC>"),
2204 class == ELFCLASS32 ? 10 : 18, sym->st_value,
2205 elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2208 /* This is a relocation against a STT_SECTION symbol. */
2209 GElf_Shdr secshdr_mem;
2211 secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2212 sym->st_shndx == SHN_XINDEX
2213 ? xndx : sym->st_shndx),
2216 if (unlikely (secshdr == NULL))
2217 printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2218 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2219 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2220 /* Avoid the leading R_ which isn't carrying any
2222 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2223 buf, sizeof (buf)) + 2
2224 : _("<INVALID RELOC>"),
2225 _("INVALID SECTION"),
2226 (long int) (sym->st_shndx == SHN_XINDEX
2227 ? xndx : sym->st_shndx));
2229 printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2230 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2231 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2232 /* Avoid the leading R_ which isn't carrying any
2234 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2235 buf, sizeof (buf)) + 2
2236 : _("<INVALID RELOC>"),
2237 class == ELFCLASS32 ? 10 : 18, sym->st_value,
2238 elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2245 /* Handle a relocation section. */
2247 handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2249 int class = gelf_getclass (ebl->elf);
2250 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2251 int nentries = shdr->sh_size / sh_entsize;
2253 /* Get the data of the section. */
2254 Elf_Data *data = elf_getdata (scn, NULL);
2258 /* Get the symbol table information. */
2259 Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2260 GElf_Shdr symshdr_mem;
2261 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2262 Elf_Data *symdata = elf_getdata (symscn, NULL);
2264 /* Get the section header of the section the relocations are for. */
2265 GElf_Shdr destshdr_mem;
2266 GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2269 if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2271 printf (_("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2276 /* Search for the optional extended section index table. */
2277 Elf_Data *xndxdata = NULL;
2278 int xndxscnidx = elf_scnshndx (scn);
2279 if (unlikely (xndxscnidx > 0))
2280 xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2282 /* Get the section header string table index. */
2284 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2285 error_exit (0, _("cannot get section header string table index"));
2287 if (shdr->sh_info != 0)
2288 printf (ngettext ("\
2289 \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2291 \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2294 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2295 (unsigned int) shdr->sh_info,
2296 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2300 /* The .rela.dyn section does not refer to a specific section but
2301 instead of section index zero. Do not try to print a section
2303 printf (ngettext ("\
2304 \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2306 \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2308 (unsigned int) elf_ndxscn (scn),
2309 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2312 fputs_unlocked (class == ELFCLASS32
2314 Offset Type Value Addend Name\n")
2316 Offset Type Value Addend Name\n"),
2319 int is_statically_linked = 0;
2320 for (int cnt = 0; cnt < nentries; ++cnt)
2323 GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2324 if (likely (rel != NULL))
2329 GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2330 GELF_R_SYM (rel->r_info),
2333 if (unlikely (sym == NULL))
2335 /* As a special case we have to handle relocations in static
2336 executables. This only happens for IRELATIVE relocations
2337 (so far). There is no symbol table. */
2338 if (is_statically_linked == 0)
2340 /* Find the program header and look for a PT_INTERP entry. */
2341 is_statically_linked = -1;
2342 if (ehdr->e_type == ET_EXEC)
2344 is_statically_linked = 1;
2346 for (size_t inner = 0; inner < phnum; ++inner)
2349 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2351 if (phdr != NULL && phdr->p_type == PT_INTERP)
2353 is_statically_linked = -1;
2360 if (is_statically_linked > 0 && shdr->sh_link == 0)
2362 %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2363 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2364 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2365 /* Avoid the leading R_ which isn't carrying any
2367 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2368 buf, sizeof (buf)) + 2
2369 : _("<INVALID RELOC>"),
2370 class == ELFCLASS32 ? 10 : 18, "",
2372 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2374 printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2375 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2376 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2377 /* Avoid the leading R_ which isn't carrying any
2379 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2380 buf, sizeof (buf)) + 2
2381 : _("<INVALID RELOC>"),
2382 _("INVALID SYMBOL"),
2383 (long int) GELF_R_SYM (rel->r_info));
2385 else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2387 %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2388 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2389 likely (ebl_reloc_type_check (ebl,
2390 GELF_R_TYPE (rel->r_info)))
2391 /* Avoid the leading R_ which isn't carrying any
2393 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2394 buf, sizeof (buf)) + 2
2395 : _("<INVALID RELOC>"),
2396 class == ELFCLASS32 ? 10 : 18, sym->st_value,
2398 elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2401 /* This is a relocation against a STT_SECTION symbol. */
2402 GElf_Shdr secshdr_mem;
2404 secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2405 sym->st_shndx == SHN_XINDEX
2406 ? xndx : sym->st_shndx),
2409 if (unlikely (secshdr == NULL))
2410 printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2411 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2412 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2413 /* Avoid the leading R_ which isn't carrying any
2415 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2416 buf, sizeof (buf)) + 2
2417 : _("<INVALID RELOC>"),
2418 _("INVALID SECTION"),
2419 (long int) (sym->st_shndx == SHN_XINDEX
2420 ? xndx : sym->st_shndx));
2423 %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2424 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2425 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2426 /* Avoid the leading R_ which isn't carrying any
2428 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2429 buf, sizeof (buf)) + 2
2430 : _("<INVALID RELOC>"),
2431 class == ELFCLASS32 ? 10 : 18, sym->st_value,
2433 elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2440 /* Print the program header. Return true if a symtab is printed,
2443 print_symtab (Ebl *ebl, int type)
2445 /* Find the symbol table(s). For this we have to search through the
2447 Elf_Scn *scn = NULL;
2448 bool symtab_printed = false;
2450 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2452 /* Handle the section if it is a symbol table. */
2454 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2456 if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2458 if (symbol_table_section != NULL)
2460 /* Get the section header string table index. */
2463 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2465 _("cannot get section header string table index"));
2466 sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2467 if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2471 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2473 if (elf_compress (scn, 0, 0) < 0)
2474 printf ("WARNING: %s [%zd]\n",
2475 _("Couldn't uncompress section"),
2477 shdr = gelf_getshdr (scn, &shdr_mem);
2478 if (unlikely (shdr == NULL))
2480 _("cannot get section [%zd] header: %s"),
2481 elf_ndxscn (scn), elf_errmsg (-1));
2483 handle_symtab (ebl, scn, shdr);
2484 symtab_printed = true;
2488 return symtab_printed;
2493 handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2495 Elf_Data *versym_data = NULL;
2496 Elf_Data *verneed_data = NULL;
2497 Elf_Data *verdef_data = NULL;
2498 Elf_Data *xndx_data = NULL;
2499 int class = gelf_getclass (ebl->elf);
2500 Elf32_Word verneed_stridx = 0;
2501 Elf32_Word verdef_stridx = 0;
2503 /* Get the data of the section. */
2504 Elf_Data *data = elf_getdata (scn, NULL);
2508 /* Find out whether we have other sections we might need. */
2509 Elf_Scn *runscn = NULL;
2510 while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2512 GElf_Shdr runshdr_mem;
2513 GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2515 if (likely (runshdr != NULL))
2517 if (runshdr->sh_type == SHT_GNU_versym
2518 && runshdr->sh_link == elf_ndxscn (scn))
2519 /* Bingo, found the version information. Now get the data. */
2520 versym_data = elf_getdata (runscn, NULL);
2521 else if (runshdr->sh_type == SHT_GNU_verneed)
2523 /* This is the information about the needed versions. */
2524 verneed_data = elf_getdata (runscn, NULL);
2525 verneed_stridx = runshdr->sh_link;
2527 else if (runshdr->sh_type == SHT_GNU_verdef)
2529 /* This is the information about the defined versions. */
2530 verdef_data = elf_getdata (runscn, NULL);
2531 verdef_stridx = runshdr->sh_link;
2533 else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
2534 && runshdr->sh_link == elf_ndxscn (scn))
2535 /* Extended section index. */
2536 xndx_data = elf_getdata (runscn, NULL);
2540 /* Get the section header string table index. */
2542 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2543 error_exit (0, _("cannot get section header string table index"));
2545 GElf_Shdr glink_mem;
2546 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2549 error_exit (0, _("invalid sh_link value in section %zu"),
2552 /* Now we can compute the number of entries in the section. */
2553 unsigned int nsyms = data->d_size / (class == ELFCLASS32
2554 ? sizeof (Elf32_Sym)
2555 : sizeof (Elf64_Sym));
2557 printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
2558 "\nSymbol table [%2u] '%s' contains %u entries:\n",
2560 (unsigned int) elf_ndxscn (scn),
2561 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2562 printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2563 " %lu local symbols String table: [%2u] '%s'\n",
2565 (unsigned long int) shdr->sh_info,
2566 (unsigned int) shdr->sh_link,
2567 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2569 fputs_unlocked (class == ELFCLASS32
2571 Num: Value Size Type Bind Vis Ndx Name\n")
2573 Num: Value Size Type Bind Vis Ndx Name\n"),
2576 for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2583 GElf_Sym *sym = gelf_getsymshndx (data, xndx_data, cnt, &sym_mem, &xndx);
2585 if (unlikely (sym == NULL))
2588 /* Determine the real section index. */
2589 if (likely (sym->st_shndx != SHN_XINDEX))
2590 xndx = sym->st_shndx;
2593 %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2595 class == ELFCLASS32 ? 8 : 16,
2598 ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info),
2599 typebuf, sizeof (typebuf)),
2600 ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2601 bindbuf, sizeof (bindbuf)),
2602 get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2603 ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2604 sizeof (scnbuf), NULL, shnum),
2605 elf_strptr (ebl->elf, shdr->sh_link, sym->st_name));
2607 if (versym_data != NULL)
2609 /* Get the version information. */
2610 GElf_Versym versym_mem;
2611 GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2613 if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2615 bool is_nobits = false;
2616 bool check_def = xndx != SHN_UNDEF;
2618 if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2620 GElf_Shdr symshdr_mem;
2621 GElf_Shdr *symshdr =
2622 gelf_getshdr (elf_getscn (ebl->elf, xndx), &symshdr_mem);
2624 is_nobits = (symshdr != NULL
2625 && symshdr->sh_type == SHT_NOBITS);
2628 if (is_nobits || ! check_def)
2630 /* We must test both. */
2631 GElf_Vernaux vernaux_mem;
2632 GElf_Vernaux *vernaux = NULL;
2633 size_t vn_offset = 0;
2635 GElf_Verneed verneed_mem;
2636 GElf_Verneed *verneed = gelf_getverneed (verneed_data, 0,
2638 while (verneed != NULL)
2640 size_t vna_offset = vn_offset;
2642 vernaux = gelf_getvernaux (verneed_data,
2643 vna_offset += verneed->vn_aux,
2645 while (vernaux != NULL
2646 && vernaux->vna_other != *versym
2647 && vernaux->vna_next != 0
2648 && (verneed_data->d_size - vna_offset
2649 >= vernaux->vna_next))
2651 /* Update the offset. */
2652 vna_offset += vernaux->vna_next;
2654 vernaux = (vernaux->vna_next == 0
2656 : gelf_getvernaux (verneed_data,
2661 /* Check whether we found the version. */
2662 if (vernaux != NULL && vernaux->vna_other == *versym)
2666 if (verneed_data->d_size - vn_offset < verneed->vn_next)
2669 vn_offset += verneed->vn_next;
2670 verneed = (verneed->vn_next == 0
2672 : gelf_getverneed (verneed_data, vn_offset,
2676 if (vernaux != NULL && vernaux->vna_other == *versym)
2679 elf_strptr (ebl->elf, verneed_stridx,
2681 (unsigned int) vernaux->vna_other);
2684 else if (unlikely (! is_nobits))
2685 error (0, 0, _("bad dynamic symbol"));
2690 if (check_def && *versym != 0x8001)
2692 /* We must test both. */
2693 size_t vd_offset = 0;
2695 GElf_Verdef verdef_mem;
2696 GElf_Verdef *verdef = gelf_getverdef (verdef_data, 0,
2698 while (verdef != NULL)
2700 if (verdef->vd_ndx == (*versym & 0x7fff))
2701 /* Found the definition. */
2704 if (verdef_data->d_size - vd_offset < verdef->vd_next)
2707 vd_offset += verdef->vd_next;
2708 verdef = (verdef->vd_next == 0
2710 : gelf_getverdef (verdef_data, vd_offset,
2716 GElf_Verdaux verdaux_mem;
2717 GElf_Verdaux *verdaux
2718 = gelf_getverdaux (verdef_data,
2719 vd_offset + verdef->vd_aux,
2722 if (verdaux != NULL)
2723 printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2724 elf_strptr (ebl->elf, verdef_stridx,
2725 verdaux->vda_name));
2731 putchar_unlocked ('\n');
2736 /* Print version information. */
2738 print_verinfo (Ebl *ebl)
2740 /* Find the version information sections. For this we have to
2741 search through the section table. */
2742 Elf_Scn *scn = NULL;
2744 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2746 /* Handle the section if it is part of the versioning handling. */
2748 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2750 if (likely (shdr != NULL))
2752 if (shdr->sh_type == SHT_GNU_verneed)
2753 handle_verneed (ebl, scn, shdr);
2754 else if (shdr->sh_type == SHT_GNU_verdef)
2755 handle_verdef (ebl, scn, shdr);
2756 else if (shdr->sh_type == SHT_GNU_versym)
2757 handle_versym (ebl, scn, shdr);
2764 get_ver_flags (unsigned int flags)
2766 static char buf[32];
2772 if (flags & VER_FLG_BASE)
2773 endp = stpcpy (buf, "BASE ");
2777 if (flags & VER_FLG_WEAK)
2780 endp = stpcpy (endp, "| ");
2782 endp = stpcpy (endp, "WEAK ");
2785 if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
2787 strncpy (endp, _("| <unknown>"), buf + sizeof (buf) - endp);
2788 buf[sizeof (buf) - 1] = '\0';
2796 handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2798 int class = gelf_getclass (ebl->elf);
2800 /* Get the data of the section. */
2801 Elf_Data *data = elf_getdata (scn, NULL);
2805 /* Get the section header string table index. */
2807 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2808 error_exit (0, _("cannot get section header string table index"));
2810 GElf_Shdr glink_mem;
2811 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2814 error_exit (0, _("invalid sh_link value in section %zu"),
2817 printf (ngettext ("\
2818 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2820 \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2822 (unsigned int) elf_ndxscn (scn),
2823 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
2824 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2826 (unsigned int) shdr->sh_link,
2827 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2829 unsigned int offset = 0;
2830 for (int cnt = shdr->sh_info; --cnt >= 0; )
2832 /* Get the data at the next offset. */
2833 GElf_Verneed needmem;
2834 GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
2835 if (unlikely (need == NULL))
2838 printf (_(" %#06x: Version: %hu File: %s Cnt: %hu\n"),
2839 offset, (unsigned short int) need->vn_version,
2840 elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
2841 (unsigned short int) need->vn_cnt);
2843 unsigned int auxoffset = offset + need->vn_aux;
2844 for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
2846 GElf_Vernaux auxmem;
2847 GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
2848 if (unlikely (aux == NULL))
2851 printf (_(" %#06x: Name: %s Flags: %s Version: %hu\n"),
2853 elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
2854 get_ver_flags (aux->vna_flags),
2855 (unsigned short int) aux->vna_other);
2857 if (aux->vna_next == 0)
2860 auxoffset += aux->vna_next;
2863 /* Find the next offset. */
2864 if (need->vn_next == 0)
2867 offset += need->vn_next;
2873 handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2875 /* Get the data of the section. */
2876 Elf_Data *data = elf_getdata (scn, NULL);
2880 /* Get the section header string table index. */
2882 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2883 error_exit (0, _("cannot get section header string table index"));
2885 GElf_Shdr glink_mem;
2886 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2889 error_exit (0, _("invalid sh_link value in section %zu"),
2892 int class = gelf_getclass (ebl->elf);
2893 printf (ngettext ("\
2894 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2896 \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2898 (unsigned int) elf_ndxscn (scn),
2899 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2901 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2903 (unsigned int) shdr->sh_link,
2904 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2906 unsigned int offset = 0;
2907 for (int cnt = shdr->sh_info; --cnt >= 0; )
2909 /* Get the data at the next offset. */
2911 GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
2912 if (unlikely (def == NULL))
2915 unsigned int auxoffset = offset + def->vd_aux;
2916 GElf_Verdaux auxmem;
2917 GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
2918 if (unlikely (aux == NULL))
2922 %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
2923 offset, def->vd_version,
2924 get_ver_flags (def->vd_flags),
2927 elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2929 auxoffset += aux->vda_next;
2930 for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
2932 aux = gelf_getverdaux (data, auxoffset, &auxmem);
2933 if (unlikely (aux == NULL))
2936 printf (_(" %#06x: Parent %d: %s\n"),
2938 elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2940 if (aux->vda_next == 0)
2943 auxoffset += aux->vda_next;
2946 /* Find the next offset. */
2947 if (def->vd_next == 0)
2949 offset += def->vd_next;
2955 handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2957 int class = gelf_getclass (ebl->elf);
2958 const char **vername;
2959 const char **filename;
2961 /* Get the data of the section. */
2962 Elf_Data *data = elf_getdata (scn, NULL);
2966 /* Get the section header string table index. */
2968 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2969 error_exit (0, _("cannot get section header string table index"));
2971 /* We have to find the version definition section and extract the
2973 Elf_Scn *defscn = NULL;
2974 Elf_Scn *needscn = NULL;
2976 Elf_Scn *verscn = NULL;
2977 while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
2979 GElf_Shdr vershdr_mem;
2980 GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
2982 if (likely (vershdr != NULL))
2984 if (vershdr->sh_type == SHT_GNU_verdef)
2986 else if (vershdr->sh_type == SHT_GNU_verneed)
2992 if (defscn != NULL || needscn != NULL)
2994 /* We have a version information (better should have). Now get
2995 the version names. First find the maximum version number. */
2999 /* Run through the version definitions and find the highest
3001 unsigned int offset = 0;
3003 GElf_Shdr defshdrmem;
3006 defdata = elf_getdata (defscn, NULL);
3007 if (unlikely (defdata == NULL))
3010 defshdr = gelf_getshdr (defscn, &defshdrmem);
3011 if (unlikely (defshdr == NULL))
3014 for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3019 /* Get the data at the next offset. */
3020 def = gelf_getverdef (defdata, offset, &defmem);
3021 if (unlikely (def == NULL))
3024 nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
3026 if (def->vd_next == 0)
3028 offset += def->vd_next;
3031 if (needscn != NULL)
3033 unsigned int offset = 0;
3035 GElf_Shdr needshdrmem;
3036 GElf_Shdr *needshdr;
3038 needdata = elf_getdata (needscn, NULL);
3039 if (unlikely (needdata == NULL))
3042 needshdr = gelf_getshdr (needscn, &needshdrmem);
3043 if (unlikely (needshdr == NULL))
3046 for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3048 GElf_Verneed needmem;
3050 unsigned int auxoffset;
3053 /* Get the data at the next offset. */
3054 need = gelf_getverneed (needdata, offset, &needmem);
3055 if (unlikely (need == NULL))
3058 /* Run through the auxiliary entries. */
3059 auxoffset = offset + need->vn_aux;
3060 for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
3062 GElf_Vernaux auxmem;
3065 aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
3066 if (unlikely (aux == NULL))
3069 nvername = MAX (nvername,
3070 (size_t) (aux->vna_other & 0x7fff));
3072 if (aux->vna_next == 0)
3074 auxoffset += aux->vna_next;
3077 if (need->vn_next == 0)
3079 offset += need->vn_next;
3083 /* This is the number of versions we know about. */
3086 /* Allocate the array. */
3087 vername = (const char **) alloca (nvername * sizeof (const char *));
3088 memset(vername, 0, nvername * sizeof (const char *));
3089 filename = (const char **) alloca (nvername * sizeof (const char *));
3090 memset(filename, 0, nvername * sizeof (const char *));
3092 /* Run through the data structures again and collect the strings. */
3095 /* Run through the version definitions and find the highest
3097 unsigned int offset = 0;
3099 GElf_Shdr defshdrmem;
3102 defdata = elf_getdata (defscn, NULL);
3103 if (unlikely (defdata == NULL))
3106 defshdr = gelf_getshdr (defscn, &defshdrmem);
3107 if (unlikely (defshdr == NULL))
3110 for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
3113 /* Get the data at the next offset. */
3115 GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
3116 if (unlikely (def == NULL))
3119 GElf_Verdaux auxmem;
3120 GElf_Verdaux *aux = gelf_getverdaux (defdata,
3121 offset + def->vd_aux,
3123 if (unlikely (aux == NULL))
3126 vername[def->vd_ndx & 0x7fff]
3127 = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
3128 filename[def->vd_ndx & 0x7fff] = NULL;
3130 if (def->vd_next == 0)
3132 offset += def->vd_next;
3135 if (needscn != NULL)
3137 unsigned int offset = 0;
3139 Elf_Data *needdata = elf_getdata (needscn, NULL);
3140 GElf_Shdr needshdrmem;
3141 GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
3142 if (unlikely (needdata == NULL || needshdr == NULL))
3145 for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
3147 /* Get the data at the next offset. */
3148 GElf_Verneed needmem;
3149 GElf_Verneed *need = gelf_getverneed (needdata, offset,
3151 if (unlikely (need == NULL))
3154 /* Run through the auxiliary entries. */
3155 unsigned int auxoffset = offset + need->vn_aux;
3156 for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3158 GElf_Vernaux auxmem;
3159 GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3161 if (unlikely (aux == NULL))
3164 vername[aux->vna_other & 0x7fff]
3165 = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3166 filename[aux->vna_other & 0x7fff]
3167 = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3169 if (aux->vna_next == 0)
3171 auxoffset += aux->vna_next;
3174 if (need->vn_next == 0)
3176 offset += need->vn_next;
3187 GElf_Shdr glink_mem;
3188 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3190 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3192 error_exit (0, _("invalid sh_link value in section %zu"),
3195 /* Print the header. */
3196 printf (ngettext ("\
3197 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3199 \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3200 shdr->sh_size / sh_entsize),
3201 (unsigned int) elf_ndxscn (scn),
3202 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3203 (int) (shdr->sh_size / sh_entsize),
3204 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3206 (unsigned int) shdr->sh_link,
3207 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3209 /* Now we can finally look at the actual contents of this section. */
3210 for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3213 printf ("\n %4d:", cnt);
3216 GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3224 fputs_unlocked (_(" 0 *local* "),
3229 fputs_unlocked (_(" 1 *global* "),
3234 n = printf ("%4d%c%s",
3235 *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3237 && (unsigned int) (*sym & 0x7fff) < nvername)
3238 ? vername[*sym & 0x7fff] : "???");
3239 if ((unsigned int) (*sym & 0x7fff) < nvername
3240 && filename != NULL && filename[*sym & 0x7fff] != NULL)
3241 n += printf ("(%s)", filename[*sym & 0x7fff]);
3242 printf ("%*s", MAX (0, 33 - (int) n), " ");
3246 putchar_unlocked ('\n');
3251 print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3252 uint_fast32_t maxlength, Elf32_Word nbucket,
3253 uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3255 uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
3257 for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3258 ++counts[lengths[cnt]];
3260 GElf_Shdr glink_mem;
3261 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3266 error (0, 0, _("invalid sh_link value in section %zu"),
3271 printf (ngettext ("\
3272 \nHistogram for bucket list length in section [%2u] '%s' (total of %d bucket):\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3274 \nHistogram for bucket list length in section [%2u] '%s' (total of %d buckets):\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
3276 (unsigned int) elf_ndxscn (scn),
3277 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3279 gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3282 (unsigned int) shdr->sh_link,
3283 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3285 if (extrastr != NULL)
3286 fputs (extrastr, stdout);
3288 if (likely (nbucket > 0))
3290 uint64_t success = 0;
3292 /* xgettext:no-c-format */
3293 fputs_unlocked (_("\
3294 Length Number % of total Coverage\n"), stdout);
3295 printf (_(" 0 %6" PRIu32 " %5.1f%%\n"),
3296 counts[0], (counts[0] * 100.0) / nbucket);
3298 uint64_t nzero_counts = 0;
3299 for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3301 nzero_counts += counts[cnt] * cnt;
3303 %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3304 (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3305 (nzero_counts * 100.0) / nsyms);
3309 for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3312 success += counts[cnt] * acc;
3316 Average number of tests: successful lookup: %f\n\
3317 unsuccessful lookup: %f\n"),
3318 (double) success / (double) nzero_counts,
3319 (double) nzero_counts / (double) nbucket);
3326 /* This function handles the traditional System V-style hash table format. */
3328 handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3330 Elf_Data *data = elf_getdata (scn, NULL);
3331 if (unlikely (data == NULL))
3333 error (0, 0, _("cannot get data for section %d: %s"),
3334 (int) elf_ndxscn (scn), elf_errmsg (-1));
3338 if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3341 error (0, 0, _("invalid data in sysv.hash section %d"),
3342 (int) elf_ndxscn (scn));
3346 Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3347 Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3349 uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3350 if (used_buf > data->d_size)
3353 Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3354 Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3356 uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3358 uint_fast32_t maxlength = 0;
3359 uint_fast32_t nsyms = 0;
3360 for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3362 Elf32_Word inner = bucket[cnt];
3363 Elf32_Word chain_len = 0;
3364 while (inner > 0 && inner < nchain)
3368 if (chain_len > nchain)
3370 error (0, 0, _("invalid chain in sysv.hash section %d"),
3371 (int) elf_ndxscn (scn));
3375 if (maxlength < ++lengths[cnt])
3378 inner = chain[inner];
3382 print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3389 /* This function handles the incorrect, System V-style hash table
3390 format some 64-bit architectures use. */
3392 handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3394 Elf_Data *data = elf_getdata (scn, NULL);
3395 if (unlikely (data == NULL))
3397 error (0, 0, _("cannot get data for section %d: %s"),
3398 (int) elf_ndxscn (scn), elf_errmsg (-1));
3402 if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3405 error (0, 0, _("invalid data in sysv.hash64 section %d"),
3406 (int) elf_ndxscn (scn));
3410 Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3411 Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3413 uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3415 || maxwords - 2 < nbucket
3416 || maxwords - 2 - nbucket < nchain)
3419 Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3420 Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3422 uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
3424 uint_fast32_t maxlength = 0;
3425 uint_fast32_t nsyms = 0;
3426 for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3428 Elf64_Xword inner = bucket[cnt];
3429 Elf64_Xword chain_len = 0;
3430 while (inner > 0 && inner < nchain)
3434 if (chain_len > nchain)
3436 error (0, 0, _("invalid chain in sysv.hash64 section %d"),
3437 (int) elf_ndxscn (scn));
3441 if (maxlength < ++lengths[cnt])
3444 inner = chain[inner];
3448 print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3455 /* This function handles the GNU-style hash table format. */
3457 handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3459 uint32_t *lengths = NULL;
3460 Elf_Data *data = elf_getdata (scn, NULL);
3461 if (unlikely (data == NULL))
3463 error (0, 0, _("cannot get data for section %d: %s"),
3464 (int) elf_ndxscn (scn), elf_errmsg (-1));
3468 if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3472 error (0, 0, _("invalid data in gnu.hash section %d"),
3473 (int) elf_ndxscn (scn));
3477 Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3478 Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3480 /* Next comes the size of the bitmap. It's measured in words for
3481 the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3482 64 bit archs. There is always a bloom filter present, so zero is
3483 an invalid value. */
3484 Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3485 if (gelf_getclass (ebl->elf) == ELFCLASS64)
3488 if (bitmask_words == 0)
3491 Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3493 /* Is there still room for the sym chain?
3494 Use uint64_t calculation to prevent 32bit overflow. */
3495 uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3496 uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3497 if (used_buf > data->d_size)
3500 lengths = xcalloc (nbucket, sizeof (uint32_t));
3502 Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3503 Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3504 Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3507 /* Compute distribution of chain lengths. */
3508 uint_fast32_t maxlength = 0;
3509 uint_fast32_t nsyms = 0;
3510 for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3511 if (bucket[cnt] != 0)
3513 Elf32_Word inner = bucket[cnt] - symbias;
3517 if (maxlength < ++lengths[cnt])
3519 if (inner >= max_nsyms)
3522 while ((chain[inner++] & 1) == 0);
3525 /* Count bits in bitmask. */
3526 uint_fast32_t nbits = 0;
3527 for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
3529 uint_fast32_t word = bitmask[cnt];
3531 word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
3532 word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
3533 word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
3534 word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
3535 nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
3538 char *str = xasprintf (_("\
3540 Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
3541 (unsigned int) symbias,
3542 bitmask_words * sizeof (Elf32_Word),
3544 / (uint_fast32_t) (bitmask_words
3545 * sizeof (Elf32_Word) * 8)),
3546 (unsigned int) shift);
3548 print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3556 /* Find the symbol table(s). For this we have to search through the
3559 handle_hash (Ebl *ebl)
3561 /* Get the section header string table index. */
3563 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3564 error_exit (0, _("cannot get section header string table index"));
3566 Elf_Scn *scn = NULL;
3567 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3569 /* Handle the section if it is a symbol table. */
3571 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3573 if (likely (shdr != NULL))
3575 if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
3576 && (shdr->sh_flags & SHF_COMPRESSED) != 0)
3578 if (elf_compress (scn, 0, 0) < 0)
3579 printf ("WARNING: %s [%zd]\n",
3580 _("Couldn't uncompress section"),
3582 shdr = gelf_getshdr (scn, &shdr_mem);
3583 if (unlikely (shdr == NULL))
3584 error_exit (0, _("cannot get section [%zd] header: %s"),
3585 elf_ndxscn (scn), elf_errmsg (-1));
3588 if (shdr->sh_type == SHT_HASH)
3590 if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
3591 handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
3593 handle_sysv_hash (ebl, scn, shdr, shstrndx);
3595 else if (shdr->sh_type == SHT_GNU_HASH)
3596 handle_gnu_hash (ebl, scn, shdr, shstrndx);
3603 print_liblist (Ebl *ebl)
3605 /* Find the library list sections. For this we have to search
3606 through the section table. */
3607 Elf_Scn *scn = NULL;
3609 /* Get the section header string table index. */
3611 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3612 error_exit (0, _("cannot get section header string table index"));
3614 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3617 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3619 if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
3621 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
3622 int nentries = shdr->sh_size / sh_entsize;
3623 printf (ngettext ("\
3624 \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
3626 \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
3629 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3633 Elf_Data *data = elf_getdata (scn, NULL);
3638 Library Time Stamp Checksum Version Flags"));
3640 for (int cnt = 0; cnt < nentries; ++cnt)
3643 GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
3644 if (unlikely (lib == NULL))
3647 time_t t = (time_t) lib->l_time_stamp;
3648 struct tm *tm = gmtime (&t);
3649 if (unlikely (tm == NULL))
3652 printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
3653 cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
3654 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
3655 tm->tm_hour, tm->tm_min, tm->tm_sec,
3656 (unsigned int) lib->l_checksum,
3657 (unsigned int) lib->l_version,
3658 (unsigned int) lib->l_flags);
3664 static inline size_t
3665 left (Elf_Data *data,
3666 const unsigned char *p)
3668 return (const unsigned char *) data->d_buf + data->d_size - p;
3672 print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
3674 /* Find the object attributes sections. For this we have to search
3675 through the section table. */
3676 Elf_Scn *scn = NULL;
3678 /* Get the section header string table index. */
3680 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3681 error_exit (0, _("cannot get section header string table index"));
3683 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3686 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3688 if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
3689 && (shdr->sh_type != SHT_ARM_ATTRIBUTES
3690 || ehdr->e_machine != EM_ARM)
3691 && (shdr->sh_type != SHT_CSKY_ATTRIBUTES
3692 || ehdr->e_machine != EM_CSKY)
3693 && (shdr->sh_type != SHT_RISCV_ATTRIBUTES
3694 || ehdr->e_machine != EM_RISCV)))
3698 \nObject attributes section [%2zu] '%s' of %" PRIu64
3699 " bytes at offset %#0" PRIx64 ":\n"),
3701 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3702 shdr->sh_size, shdr->sh_offset);
3704 Elf_Data *data = elf_rawdata (scn, NULL);
3705 if (unlikely (data == NULL || data->d_size == 0))
3708 const unsigned char *p = data->d_buf;
3710 /* There is only one 'version', A. */
3711 if (unlikely (*p++ != 'A'))
3714 fputs_unlocked (_(" Owner Size\n"), stdout);
3716 /* Loop over the sections. */
3717 while (left (data, p) >= 4)
3719 /* Section length. */
3721 memcpy (&len, p, sizeof len);
3723 if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3726 if (unlikely (len > left (data, p)))
3729 /* Section vendor name. */
3730 const unsigned char *name = p + sizeof len;
3733 unsigned const char *q = memchr (name, '\0', len);
3734 if (unlikely (q == NULL))
3738 printf (_(" %-13s %4" PRIu32 "\n"), name, len);
3740 bool gnu_vendor = (q - name == sizeof "gnu"
3741 && !memcmp (name, "gnu", sizeof "gnu"));
3743 /* Loop over subsections. */
3744 if (shdr->sh_type != SHT_GNU_ATTRIBUTES
3748 const unsigned char *const sub = q;
3750 unsigned int subsection_tag;
3751 get_uleb128 (subsection_tag, q, p);
3752 if (unlikely (q >= p))
3755 uint32_t subsection_len;
3756 if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
3759 memcpy (&subsection_len, q, sizeof subsection_len);
3761 if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3762 CONVERT (subsection_len);
3764 /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
3765 if (unlikely (subsection_len == 0
3766 || subsection_len >= (uint32_t) PTRDIFF_MAX
3767 || p - sub < (ptrdiff_t) subsection_len))
3770 const unsigned char *r = q + sizeof subsection_len;
3771 q = sub + subsection_len;
3773 switch (subsection_tag)
3776 /* Unknown subsection, print and skip. */
3777 printf (_(" %-4u %12" PRIu32 "\n"),
3778 subsection_tag, subsection_len);
3781 case 1: /* Tag_File */
3782 printf (_(" File: %11" PRIu32 "\n"),
3788 get_uleb128 (tag, r, q);
3789 if (unlikely (r >= q))
3792 /* GNU style tags have either a uleb128 value,
3793 when lowest bit is not set, or a string
3794 when the lowest bit is set.
3795 "compatibility" (32) is special. It has
3796 both a string and a uleb128 value. For
3797 non-gnu we assume 6 till 31 only take ints.
3798 XXX see arm backend, do we need a separate
3801 const char *string = NULL;
3802 if (tag == 32 || (tag & 1) == 0
3803 || (! gnu_vendor && (tag > 5 && tag < 32)))
3805 get_uleb128 (value, r, q);
3812 || (! gnu_vendor && tag > 32)))
3813 || (! gnu_vendor && tag > 3 && tag < 6))
3815 string = (const char *) r;
3816 r = memchr (r, '\0', q - r);
3822 const char *tag_name = NULL;
3823 const char *value_name = NULL;
3824 ebl_check_object_attribute (ebl, (const char *) name,
3826 &tag_name, &value_name);
3828 if (tag_name != NULL)
3831 printf (_(" %s: %" PRId64 ", %s\n"),
3832 tag_name, value, string);
3833 else if (string == NULL && value_name == NULL)
3834 printf (_(" %s: %" PRId64 "\n"),
3837 printf (_(" %s: %s\n"),
3838 tag_name, string ?: value_name);
3842 /* For "gnu" vendor 32 "compatibility" has
3843 already been handled above. */
3845 || strcmp ((const char *) name, "gnu"));
3847 printf (_(" %u: %" PRId64 "\n"),
3850 printf (_(" %u: %s\n"),
3862 print_dwarf_addr (Dwfl_Module *dwflmod,
3863 int address_size, Dwarf_Addr address, Dwarf_Addr raw)
3865 /* See if there is a name we can give for this address. */
3868 const char *name = (print_address_names && ! print_unresolved_addresses)
3869 ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
3873 if (print_unresolved_addresses)
3880 /* Relativize the address. */
3881 int n = dwfl_module_relocations (dwflmod);
3882 int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
3884 /* In an ET_REL file there is a section name to refer to. */
3886 : dwfl_module_relocation_info (dwflmod, i, NULL));
3892 ? (address_size == 0
3893 ? printf ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">",
3894 scn, address, name, off)
3895 : printf ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3896 scn, 2 + address_size * 2, address,
3898 : (address_size == 0
3899 ? printf ("%#" PRIx64 " <%s+%#" PRIx64 ">",
3901 : printf ("%#0*" PRIx64 " <%s+%#" PRIx64 ">",
3902 2 + address_size * 2, address,
3905 ? (address_size == 0
3906 ? printf ("%s+%#" PRIx64 " <%s>", scn, address, name)
3907 : printf ("%s+%#0*" PRIx64 " <%s>",
3908 scn, 2 + address_size * 2, address, name))
3909 : (address_size == 0
3910 ? printf ("%#" PRIx64 " <%s>", address, name)
3911 : printf ("%#0*" PRIx64 " <%s>",
3912 2 + address_size * 2, address, name))))
3914 ? (address_size == 0
3915 ? printf ("%s+%#" PRIx64, scn, address)
3916 : printf ("%s+%#0*" PRIx64, scn, 2 + address_size * 2, address))
3917 : (address_size == 0
3918 ? printf ("%#" PRIx64, address)
3919 : printf ("%#0*" PRIx64, 2 + address_size * 2, address)))) < 0)
3920 error_exit (0, _("sprintf failure"));
3925 dwarf_tag_string (unsigned int tag)
3929 #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
3930 DWARF_ALL_KNOWN_DW_TAG
3931 #undef DWARF_ONE_KNOWN_DW_TAG
3939 dwarf_attr_string (unsigned int attrnum)
3943 #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
3944 DWARF_ALL_KNOWN_DW_AT
3945 #undef DWARF_ONE_KNOWN_DW_AT
3953 dwarf_form_string (unsigned int form)
3957 #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
3958 DWARF_ALL_KNOWN_DW_FORM
3959 #undef DWARF_ONE_KNOWN_DW_FORM
3967 dwarf_lang_string (unsigned int lang)
3971 #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
3972 DWARF_ALL_KNOWN_DW_LANG
3973 #undef DWARF_ONE_KNOWN_DW_LANG
3981 dwarf_inline_string (unsigned int code)
3983 static const char *const known[] =
3985 #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
3986 DWARF_ALL_KNOWN_DW_INL
3987 #undef DWARF_ONE_KNOWN_DW_INL
3990 if (likely (code < sizeof (known) / sizeof (known[0])))
3998 dwarf_encoding_string (unsigned int code)
4000 static const char *const known[] =
4002 #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
4003 DWARF_ALL_KNOWN_DW_ATE
4004 #undef DWARF_ONE_KNOWN_DW_ATE
4007 if (likely (code < sizeof (known) / sizeof (known[0])))
4015 dwarf_access_string (unsigned int code)
4017 static const char *const known[] =
4019 #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
4020 DWARF_ALL_KNOWN_DW_ACCESS
4021 #undef DWARF_ONE_KNOWN_DW_ACCESS
4024 if (likely (code < sizeof (known) / sizeof (known[0])))
4032 dwarf_defaulted_string (unsigned int code)
4034 static const char *const known[] =
4036 #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
4037 DWARF_ALL_KNOWN_DW_DEFAULTED
4038 #undef DWARF_ONE_KNOWN_DW_DEFAULTED
4041 if (likely (code < sizeof (known) / sizeof (known[0])))
4049 dwarf_visibility_string (unsigned int code)
4051 static const char *const known[] =
4053 #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
4054 DWARF_ALL_KNOWN_DW_VIS
4055 #undef DWARF_ONE_KNOWN_DW_VIS
4058 if (likely (code < sizeof (known) / sizeof (known[0])))
4066 dwarf_virtuality_string (unsigned int code)
4068 static const char *const known[] =
4070 #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
4071 DWARF_ALL_KNOWN_DW_VIRTUALITY
4072 #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
4075 if (likely (code < sizeof (known) / sizeof (known[0])))
4083 dwarf_identifier_case_string (unsigned int code)
4085 static const char *const known[] =
4087 #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
4088 DWARF_ALL_KNOWN_DW_ID
4089 #undef DWARF_ONE_KNOWN_DW_ID
4092 if (likely (code < sizeof (known) / sizeof (known[0])))
4100 dwarf_calling_convention_string (unsigned int code)
4102 static const char *const known[] =
4104 #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
4105 DWARF_ALL_KNOWN_DW_CC
4106 #undef DWARF_ONE_KNOWN_DW_CC
4109 if (likely (code < sizeof (known) / sizeof (known[0])))
4117 dwarf_ordering_string (unsigned int code)
4119 static const char *const known[] =
4121 #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
4122 DWARF_ALL_KNOWN_DW_ORD
4123 #undef DWARF_ONE_KNOWN_DW_ORD
4126 if (likely (code < sizeof (known) / sizeof (known[0])))
4134 dwarf_discr_list_string (unsigned int code)
4136 static const char *const known[] =
4138 #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
4139 DWARF_ALL_KNOWN_DW_DSC
4140 #undef DWARF_ONE_KNOWN_DW_DSC
4143 if (likely (code < sizeof (known) / sizeof (known[0])))
4151 dwarf_locexpr_opcode_string (unsigned int code)
4153 static const char *const known[] =
4155 /* Normally we can't afford building huge table of 64K entries,
4156 most of them zero, just because there are a couple defined
4157 values at the far end. In case of opcodes, it's OK. */
4158 #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4159 DWARF_ALL_KNOWN_DW_OP
4160 #undef DWARF_ONE_KNOWN_DW_OP
4163 if (likely (code < sizeof (known) / sizeof (known[0])))
4171 dwarf_unit_string (unsigned int type)
4175 #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4176 DWARF_ALL_KNOWN_DW_UT
4177 #undef DWARF_ONE_KNOWN_DW_UT
4185 dwarf_range_list_encoding_string (unsigned int kind)
4189 #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4190 DWARF_ALL_KNOWN_DW_RLE
4191 #undef DWARF_ONE_KNOWN_DW_RLE
4199 dwarf_loc_list_encoding_string (unsigned int kind)
4203 #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4204 DWARF_ALL_KNOWN_DW_LLE
4205 #undef DWARF_ONE_KNOWN_DW_LLE
4206 /* DW_LLE_GNU_view_pair is special/incompatible with default codes. */
4207 case DW_LLE_GNU_view_pair: return "GNU_view_pair";
4215 dwarf_line_content_description_string (unsigned int kind)
4219 #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4220 DWARF_ALL_KNOWN_DW_LNCT
4221 #undef DWARF_ONE_KNOWN_DW_LNCT
4228 /* Used by all dwarf_foo_name functions. */
4230 string_or_unknown (const char *known, unsigned int code,
4231 unsigned int lo_user, unsigned int hi_user,
4232 bool print_unknown_num)
4234 static char unknown_buf[20];
4236 if (likely (known != NULL))
4239 if (lo_user != 0 && code >= lo_user && code <= hi_user)
4241 snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4246 if (print_unknown_num)
4248 snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4257 dwarf_tag_name (unsigned int tag)
4259 const char *ret = dwarf_tag_string (tag);
4260 return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4264 dwarf_attr_name (unsigned int attr)
4266 const char *ret = dwarf_attr_string (attr);
4267 return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4272 dwarf_form_name (unsigned int form)
4274 const char *ret = dwarf_form_string (form);
4275 return string_or_unknown (ret, form, 0, 0, true);
4280 dwarf_lang_name (unsigned int lang)
4282 const char *ret = dwarf_lang_string (lang);
4283 return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4288 dwarf_inline_name (unsigned int code)
4290 const char *ret = dwarf_inline_string (code);
4291 return string_or_unknown (ret, code, 0, 0, false);
4296 dwarf_encoding_name (unsigned int code)
4298 const char *ret = dwarf_encoding_string (code);
4299 return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4304 dwarf_access_name (unsigned int code)
4306 const char *ret = dwarf_access_string (code);
4307 return string_or_unknown (ret, code, 0, 0, false);
4312 dwarf_defaulted_name (unsigned int code)
4314 const char *ret = dwarf_defaulted_string (code);
4315 return string_or_unknown (ret, code, 0, 0, false);
4320 dwarf_visibility_name (unsigned int code)
4322 const char *ret = dwarf_visibility_string (code);
4323 return string_or_unknown (ret, code, 0, 0, false);
4328 dwarf_virtuality_name (unsigned int code)
4330 const char *ret = dwarf_virtuality_string (code);
4331 return string_or_unknown (ret, code, 0, 0, false);
4336 dwarf_identifier_case_name (unsigned int code)
4338 const char *ret = dwarf_identifier_case_string (code);
4339 return string_or_unknown (ret, code, 0, 0, false);
4344 dwarf_calling_convention_name (unsigned int code)
4346 const char *ret = dwarf_calling_convention_string (code);
4347 return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4352 dwarf_ordering_name (unsigned int code)
4354 const char *ret = dwarf_ordering_string (code);
4355 return string_or_unknown (ret, code, 0, 0, false);
4360 dwarf_discr_list_name (unsigned int code)
4362 const char *ret = dwarf_discr_list_string (code);
4363 return string_or_unknown (ret, code, 0, 0, false);
4368 dwarf_unit_name (unsigned int type)
4370 const char *ret = dwarf_unit_string (type);
4371 return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4376 dwarf_range_list_encoding_name (unsigned int kind)
4378 const char *ret = dwarf_range_list_encoding_string (kind);
4379 return string_or_unknown (ret, kind, 0, 0, false);
4384 dwarf_loc_list_encoding_name (unsigned int kind)
4386 const char *ret = dwarf_loc_list_encoding_string (kind);
4387 return string_or_unknown (ret, kind, 0, 0, false);
4392 dwarf_line_content_description_name (unsigned int kind)
4394 const char *ret = dwarf_line_content_description_string (kind);
4395 return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4401 print_block (size_t n, const void *block)
4404 puts (_("empty block"));
4407 printf (_("%zu byte block:"), n);
4408 const unsigned char *data = block;
4410 printf (" %02x", *data++);
4417 print_bytes (size_t n, const unsigned char *bytes)
4421 printf ("%02x", *bytes++);
4428 get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4433 Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4434 if (debug_addr == NULL)
4437 Dwarf_Off base = __libdw_cu_addr_base (cu);
4438 Dwarf_Word off = idx * cu->address_size;
4439 if (base > debug_addr->d_size
4440 || off > debug_addr->d_size - base
4441 || cu->address_size > debug_addr->d_size - base - off)
4444 const unsigned char *addrp = debug_addr->d_buf + base + off;
4445 if (cu->address_size == 4)
4446 *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4448 *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4454 print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4455 unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4456 struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4458 const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4462 printf ("%*s(empty)\n", indent, "");
4466 #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4467 #define CONSUME(n) NEED (n); else len -= (n)
4469 Dwarf_Word offset = 0;
4472 uint_fast8_t op = *data++;
4474 const char *op_name = dwarf_locexpr_opcode_string (op);
4475 if (unlikely (op_name == NULL))
4477 static char buf[20];
4478 if (op >= DW_OP_lo_user)
4479 snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4481 snprintf (buf, sizeof buf, "??? (%#x)", op);
4488 /* Address operand. */
4492 addr = read_4ubyte_unaligned (dbg, data);
4493 else if (addrsize == 8)
4494 addr = read_8ubyte_unaligned (dbg, data);
4500 printf ("%*s[%2" PRIuMAX "] %s ",
4501 indent, "", (uintmax_t) offset, op_name);
4502 print_dwarf_addr (dwflmod, 0, addr, addr);
4505 offset += 1 + addrsize;
4508 case DW_OP_call_ref:
4509 case DW_OP_GNU_variable_value:
4510 /* Offset operand. */
4511 if (ref_size != 4 && ref_size != 8)
4512 goto invalid; /* Cannot be used in CFA. */
4515 addr = read_4ubyte_unaligned (dbg, data);
4517 addr = read_8ubyte_unaligned (dbg, data);
4520 /* addr is a DIE offset, so format it as one. */
4521 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4522 indent, "", (uintmax_t) offset,
4523 op_name, (uintmax_t) addr);
4524 offset += 1 + ref_size;
4527 case DW_OP_deref_size:
4528 case DW_OP_xderef_size:
4531 // XXX value might be modified by relocation
4533 printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4534 indent, "", (uintmax_t) offset,
4535 op_name, *((uint8_t *) data));
4543 // XXX value might be modified by relocation
4544 printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4545 indent, "", (uintmax_t) offset,
4546 op_name, read_2ubyte_unaligned (dbg, data));
4554 // XXX value might be modified by relocation
4555 printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4556 indent, "", (uintmax_t) offset,
4557 op_name, read_4ubyte_unaligned (dbg, data));
4565 // XXX value might be modified by relocation
4566 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4567 indent, "", (uintmax_t) offset,
4568 op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4576 // XXX value might be modified by relocation
4577 printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
4578 indent, "", (uintmax_t) offset,
4579 op_name, *((int8_t *) data));
4587 // XXX value might be modified by relocation
4588 printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
4589 indent, "", (uintmax_t) offset,
4590 op_name, read_2sbyte_unaligned (dbg, data));
4598 // XXX value might be modified by relocation
4599 printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
4600 indent, "", (uintmax_t) offset,
4601 op_name, read_4sbyte_unaligned (dbg, data));
4609 // XXX value might be modified by relocation
4610 printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4611 indent, "", (uintmax_t) offset,
4612 op_name, read_8sbyte_unaligned (dbg, data));
4620 case DW_OP_plus_uconst:
4622 const unsigned char *start = data;
4625 get_uleb128 (uleb, data, data + len);
4626 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4627 indent, "", (uintmax_t) offset, op_name, uleb);
4628 CONSUME (data - start);
4629 offset += 1 + (data - start);
4633 case DW_OP_GNU_addr_index:
4635 case DW_OP_GNU_const_index:;
4638 get_uleb128 (uleb, data, data + len);
4639 printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
4640 indent, "", (uintmax_t) offset, op_name, uleb);
4641 CONSUME (data - start);
4642 offset += 1 + (data - start);
4643 if (get_indexed_addr (cu, uleb, &addr) != 0)
4647 print_dwarf_addr (dwflmod, 0, addr, addr);
4652 case DW_OP_bit_piece:
4656 get_uleb128 (uleb, data, data + len);
4658 get_uleb128 (uleb2, data, data + len);
4659 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
4660 indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4661 CONSUME (data - start);
4662 offset += 1 + (data - start);
4666 case DW_OP_breg0 ... DW_OP_breg31:
4671 get_sleb128 (sleb, data, data + len);
4672 printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4673 indent, "", (uintmax_t) offset, op_name, sleb);
4674 CONSUME (data - start);
4675 offset += 1 + (data - start);
4681 get_uleb128 (uleb, data, data + len);
4683 get_sleb128 (sleb, data, data + len);
4684 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
4685 indent, "", (uintmax_t) offset, op_name, uleb, sleb);
4686 CONSUME (data - start);
4687 offset += 1 + (data - start);
4692 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
4693 indent, "", (uintmax_t) offset, op_name,
4694 read_2ubyte_unaligned (dbg, data));
4702 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
4703 indent, "", (uintmax_t) offset, op_name,
4704 read_4ubyte_unaligned (dbg, data));
4713 printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
4714 indent, "", (uintmax_t) offset, op_name,
4715 (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
4721 case DW_OP_implicit_value:
4724 get_uleb128 (uleb, data, data + len);
4725 printf ("%*s[%2" PRIuMAX "] %s: ",
4726 indent, "", (uintmax_t) offset, op_name);
4728 print_block (uleb, data);
4730 CONSUME (data - start);
4731 offset += 1 + (data - start);
4734 case DW_OP_implicit_pointer:
4735 case DW_OP_GNU_implicit_pointer:
4736 /* DIE offset operand. */
4739 if (ref_size != 4 && ref_size != 8)
4740 goto invalid; /* Cannot be used in CFA. */
4742 addr = read_4ubyte_unaligned (dbg, data);
4744 addr = read_8ubyte_unaligned (dbg, data);
4746 /* Byte offset operand. */
4748 get_sleb128 (sleb, data, data + len);
4750 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
4751 indent, "", (intmax_t) offset,
4752 op_name, (uintmax_t) addr, sleb);
4753 CONSUME (data - start);
4754 offset += 1 + (data - start);
4757 case DW_OP_entry_value:
4758 case DW_OP_GNU_entry_value:
4759 /* Size plus expression block. */
4762 get_uleb128 (uleb, data, data + len);
4763 printf ("%*s[%2" PRIuMAX "] %s:\n",
4764 indent, "", (uintmax_t) offset, op_name);
4766 print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
4767 addrsize, offset_size, cu, uleb, data);
4769 CONSUME (data - start);
4770 offset += 1 + (data - start);
4773 case DW_OP_const_type:
4774 case DW_OP_GNU_const_type:
4775 /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
4776 unsigned size plus block. */
4779 get_uleb128 (uleb, data, data + len);
4780 if (! print_unresolved_addresses && cu != NULL)
4783 uint8_t usize = *(uint8_t *) data++;
4785 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
4786 indent, "", (uintmax_t) offset, op_name, uleb);
4787 print_block (usize, data);
4789 CONSUME (data - start);
4790 offset += 1 + (data - start);
4793 case DW_OP_regval_type:
4794 case DW_OP_GNU_regval_type:
4795 /* uleb128 register number, uleb128 CU relative
4796 DW_TAG_base_type DIE offset. */
4799 get_uleb128 (uleb, data, data + len);
4801 get_uleb128 (uleb2, data, data + len);
4802 if (! print_unresolved_addresses && cu != NULL)
4804 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
4805 indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4806 CONSUME (data - start);
4807 offset += 1 + (data - start);
4810 case DW_OP_deref_type:
4811 case DW_OP_GNU_deref_type:
4812 /* 1-byte unsigned size of value, uleb128 CU relative
4813 DW_TAG_base_type DIE offset. */
4816 usize = *(uint8_t *) data++;
4818 get_uleb128 (uleb, data, data + len);
4819 if (! print_unresolved_addresses && cu != NULL)
4821 printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4822 indent, "", (uintmax_t) offset,
4823 op_name, usize, uleb);
4824 CONSUME (data - start);
4825 offset += 1 + (data - start);
4828 case DW_OP_xderef_type:
4829 /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
4832 usize = *(uint8_t *) data++;
4834 get_uleb128 (uleb, data, data + len);
4835 printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4836 indent, "", (uintmax_t) offset,
4837 op_name, usize, uleb);
4838 CONSUME (data - start);
4839 offset += 1 + (data - start);
4843 case DW_OP_GNU_convert:
4844 case DW_OP_reinterpret:
4845 case DW_OP_GNU_reinterpret:
4846 /* uleb128 CU relative offset to DW_TAG_base_type, or zero
4847 for conversion to untyped. */
4850 get_uleb128 (uleb, data, data + len);
4851 if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
4853 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4854 indent, "", (uintmax_t) offset, op_name, uleb);
4855 CONSUME (data - start);
4856 offset += 1 + (data - start);
4859 case DW_OP_GNU_parameter_ref:
4860 /* 4 byte CU relative reference to the abstract optimized away
4861 DW_TAG_formal_parameter. */
4863 uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
4864 if (! print_unresolved_addresses && cu != NULL)
4865 param_off += cu->start;
4866 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4867 indent, "", (uintmax_t) offset, op_name, param_off);
4875 printf ("%*s[%2" PRIuMAX "] %s\n",
4876 indent, "", (uintmax_t) offset, op_name);
4881 indent = indentrest;
4885 printf (_("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
4886 indent, "", (uintmax_t) offset, op_name);
4892 /* Turn the addresses into file offsets by using the phdrs. */
4894 find_offsets(Elf *elf, GElf_Addr main_bias, size_t n,
4895 GElf_Addr addrs[n], GElf_Off offs[n])
4897 size_t unsolved = n;
4898 for (size_t i = 0; i < phnum; ++i) {
4900 GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
4901 if (phdr != NULL && phdr->p_type == PT_LOAD && phdr->p_memsz > 0)
4902 for (size_t j = 0; j < n; ++j)
4903 if (offs[j] == 0 && addrs[j] >= phdr->p_vaddr + main_bias &&
4904 addrs[j] - (phdr->p_vaddr + main_bias) < phdr->p_filesz) {
4905 offs[j] = addrs[j] - (phdr->p_vaddr + main_bias) + phdr->p_offset;
4906 if (--unsolved == 0)
4912 /* The dynamic segment (type PT_DYNAMIC), contains the .dynamic section.
4913 And .dynamic section contains an array of the dynamic structures.
4914 We use the array to get:
4915 DT_STRTAB: the address of the string table
4916 DT_SYMTAB: the address of the symbol table
4917 DT_STRSZ: the size, in bytes, of the string table
4920 get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
4922 Elf_Data *data = elf_getdata_rawchunk(
4923 elf, phdr->p_offset, phdr->p_filesz, ELF_T_DYN);
4926 for (;; ++dyn_idx) {
4928 GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
4929 /* DT_NULL Marks end of dynamic section. */
4930 if (dyn == NULL || dyn->d_tag == DT_NULL)
4933 switch (dyn->d_tag) {
4935 addrs[i_symtab] = dyn->d_un.d_ptr;
4939 addrs[i_hash] = dyn->d_un.d_ptr;
4943 addrs[i_gnu_hash] = dyn->d_un.d_ptr;
4947 addrs[i_strtab] = dyn->d_un.d_ptr;
4951 addrs[i_versym] = dyn->d_un.d_ptr;
4955 addrs[i_verdef] = dyn->d_un.d_ptr;
4959 addrs[i_verneed] = dyn->d_un.d_ptr;
4963 addrs[i_strsz] = dyn->d_un.d_val;
4970 /* Use dynamic segment to get data for the string table section. */
4972 get_dynscn_strtab(Elf *elf, GElf_Phdr *phdr)
4974 Elf_Data *strtab_data;
4975 GElf_Addr addrs[i_max] = {0,};
4976 GElf_Off offs[i_max] = {0,};
4977 get_dynscn_addrs(elf, phdr, addrs);
4978 find_offsets(elf, 0, i_max, addrs, offs);
4979 strtab_data = elf_getdata_rawchunk(
4980 elf, offs[i_strtab], addrs[i_strsz], ELF_T_BYTE);
4987 Dwarf_Off offset:(64 - 3);
4991 struct Dwarf_CU *cu;
4995 #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
4996 #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
4999 cudie_base (Dwarf_Die *cudie)
5002 /* Find the base address of the compilation unit. It will normally
5003 be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
5004 address could be overridden by DW_AT_entry_pc. It's been
5005 removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
5006 compilation units with discontinuous ranges. */
5007 if (unlikely (dwarf_lowpc (cudie, &base) != 0))
5009 Dwarf_Attribute attr_mem;
5010 if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
5018 listptr_base (struct listptr *p)
5020 Dwarf_Die cu = CUDIE (p->cu);
5021 return cudie_base (&cu);
5024 /* To store the name used in compare_listptr */
5025 static const char *sort_listptr_name;
5028 compare_listptr (const void *a, const void *b)
5030 const char *name = sort_listptr_name;
5031 struct listptr *p1 = (void *) a;
5032 struct listptr *p2 = (void *) b;
5034 if (p1->offset < p2->offset)
5036 if (p1->offset > p2->offset)
5039 if (!p1->warned && !p2->warned)
5041 if (p1->addr64 != p2->addr64)
5043 p1->warned = p2->warned = true;
5045 _("%s %#" PRIx64 " used with different address sizes"),
5046 name, (uint64_t) p1->offset);
5048 if (p1->dwarf64 != p2->dwarf64)
5050 p1->warned = p2->warned = true;
5052 _("%s %#" PRIx64 " used with different offset sizes"),
5053 name, (uint64_t) p1->offset);
5055 if (listptr_base (p1) != listptr_base (p2))
5057 p1->warned = p2->warned = true;
5059 _("%s %#" PRIx64 " used with different base addresses"),
5060 name, (uint64_t) p1->offset);
5062 if (p1->attr != p2 ->attr)
5064 p1->warned = p2->warned = true;
5067 " used with different attribute %s and %s"),
5068 name, (uint64_t) p1->offset, dwarf_attr_name (p1->attr),
5069 dwarf_attr_name (p2->attr));
5076 struct listptr_table
5080 struct listptr *table;
5083 static struct listptr_table known_locsptr;
5084 static struct listptr_table known_loclistsptr;
5085 static struct listptr_table known_rangelistptr;
5086 static struct listptr_table known_rnglistptr;
5087 static struct listptr_table known_addrbases;
5088 static struct listptr_table known_stroffbases;
5091 reset_listptr (struct listptr_table *table)
5093 free (table->table);
5094 table->table = NULL;
5095 table->n = table->alloc = 0;
5098 /* Returns false if offset doesn't fit. See struct listptr. */
5100 notice_listptr (enum section_e section, struct listptr_table *table,
5101 uint_fast8_t address_size, uint_fast8_t offset_size,
5102 struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
5104 if (print_debug_sections & section)
5106 if (table->n == table->alloc)
5108 if (table->alloc == 0)
5112 table->table = xrealloc (table->table,
5113 table->alloc * sizeof table->table[0]);
5116 struct listptr *p = &table->table[table->n++];
5118 *p = (struct listptr)
5120 .addr64 = address_size == 8,
5121 .dwarf64 = offset_size == 8,
5127 if (p->offset != offset)
5137 sort_listptr (struct listptr_table *table, const char *name)
5141 sort_listptr_name = name;
5142 qsort (table->table, table->n, sizeof table->table[0],
5148 skip_listptr_hole (struct listptr_table *table, size_t *idxp,
5149 uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
5150 Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
5151 unsigned char **readp, unsigned char *endp,
5157 while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
5160 struct listptr *p = &table->table[*idxp];
5162 if (*idxp == table->n
5163 || p->offset >= (Dwarf_Off) (endp - *readp + offset))
5166 printf (_(" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
5171 if (p->offset != (Dwarf_Off) offset)
5173 *readp += p->offset - offset;
5174 printf (_(" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
5175 offset, (Dwarf_Off) p->offset - offset);
5179 if (address_sizep != NULL)
5180 *address_sizep = listptr_address_size (p);
5181 if (offset_sizep != NULL)
5182 *offset_sizep = listptr_offset_size (p);
5184 *base = listptr_base (p);
5194 next_listptr_offset (struct listptr_table *table, size_t *idxp, Dwarf_Off off)
5196 /* Note that multiple attributes could in theory point to the same loclist
5197 offset, so make sure we pick one that is bigger than the current one.
5198 The table is sorted on offset. */
5199 if (*idxp < table->n)
5201 while (++*idxp < table->n)
5203 Dwarf_Off next = table->table[*idxp].offset;
5211 /* Returns the listptr associated with the given index, or NULL. */
5212 static struct listptr *
5213 get_listptr (struct listptr_table *table, size_t idx)
5215 if (idx >= table->n)
5217 return &table->table[idx];
5220 /* Returns the next index, base address and CU associated with the
5221 list unit offsets. If there is none false is returned, otherwise
5222 true. Assumes the table has been sorted. */
5224 listptr_cu (struct listptr_table *table, size_t *idxp,
5225 Dwarf_Off start, Dwarf_Off end,
5226 Dwarf_Addr *base, struct Dwarf_CU **cu)
5228 while (*idxp < table->n
5229 && table->table[*idxp].offset < start)
5232 if (*idxp < table->n
5233 && table->table[*idxp].offset >= start
5234 && table->table[*idxp].offset < end)
5236 struct listptr *p = &table->table[*idxp];
5237 *base = listptr_base (p);
5245 /* Returns the next index with the current CU for the given attribute.
5246 If there is none false is returned, otherwise true. Assumes the
5247 table has been sorted. */
5249 listptr_attr (struct listptr_table *table, size_t idxp,
5250 Dwarf_Off offset, unsigned int attr)
5252 struct listptr *listptr;
5255 listptr = get_listptr (table, idxp);
5256 if (listptr == NULL)
5259 if (listptr->offset == offset && listptr->attr == attr)
5264 while (listptr->offset <= offset);
5270 print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5271 Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
5272 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5274 const size_t sh_size = (dbg->sectiondata[IDX_debug_abbrev] ?
5275 dbg->sectiondata[IDX_debug_abbrev]->d_size : 0);
5277 printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5279 elf_ndxscn (scn), section_name (ebl, shdr),
5280 (uint64_t) shdr->sh_offset);
5282 Dwarf_Off offset = 0;
5283 while (offset < sh_size)
5285 printf (_("\nAbbreviation section at offset %" PRIu64 ":\n"),
5291 Dwarf_Abbrev abbrev;
5293 int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5296 if (unlikely (res < 0))
5299 *** error while reading abbreviation: %s\n"),
5304 /* This is the NUL byte at the end of the section. */
5309 /* We know these calls can never fail. */
5310 unsigned int code = dwarf_getabbrevcode (&abbrev);
5311 unsigned int tag = dwarf_getabbrevtag (&abbrev);
5312 int has_children = dwarf_abbrevhaschildren (&abbrev);
5314 printf (_(" [%5u] offset: %" PRId64
5315 ", children: %s, tag: %s\n"),
5316 code, (int64_t) offset,
5317 has_children ? yes_str : no_str,
5318 dwarf_tag_name (tag));
5325 while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5326 &data, &enoffset) == 0)
5328 printf (" attr: %s, form: %s",
5329 dwarf_attr_name (name), dwarf_form_name (form));
5330 if (form == DW_FORM_implicit_const)
5331 printf (" (%" PRId64 ")", data);
5332 printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5343 print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5344 Ebl *ebl, GElf_Ehdr *ehdr,
5345 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5348 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5349 elf_ndxscn (scn), section_name (ebl, shdr),
5350 (uint64_t) shdr->sh_offset);
5352 if (shdr->sh_size == 0)
5355 /* We like to get the section from libdw to make sure they are relocated. */
5356 Elf_Data *data = (dbg->sectiondata[IDX_debug_addr]
5357 ?: elf_rawdata (scn, NULL));
5358 if (unlikely (data == NULL))
5360 error (0, 0, _("cannot get .debug_addr section data: %s"),
5366 sort_listptr (&known_addrbases, "addr_base");
5368 const unsigned char *start = (const unsigned char *) data->d_buf;
5369 const unsigned char *readp = start;
5370 const unsigned char *readendp = ((const unsigned char *) data->d_buf
5373 while (readp < readendp)
5375 /* We cannot really know whether or not there is an header. The
5376 DebugFission extension to DWARF4 doesn't add one. The DWARF5
5377 .debug_addr variant does. Whether or not we have an header,
5378 DW_AT_[GNU_]addr_base points at "index 0". So if the current
5379 offset equals the CU addr_base then we can just start
5380 printing addresses. If there is no CU with an exact match
5381 then we'll try to parse the header first. */
5382 Dwarf_Off off = (Dwarf_Off) (readp
5383 - (const unsigned char *) data->d_buf);
5385 printf ("Table at offset %" PRIx64 " ", off);
5387 struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5388 const unsigned char *next_unitp;
5390 uint64_t unit_length;
5392 uint8_t address_size;
5393 uint8_t segment_size;
5394 if (listptr == NULL)
5396 error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5399 /* We will have to assume it is just addresses to the end... */
5400 address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5401 next_unitp = readendp;
5402 printf ("Unknown CU:\n");
5407 if (dwarf_cu_die (listptr->cu, &cudie,
5408 NULL, NULL, NULL, NULL,
5409 NULL, NULL) == NULL)
5410 printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5412 printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5414 if (listptr->offset == off)
5416 address_size = listptr_address_size (listptr);
5420 /* The addresses start here, but where do they end? */
5421 listptr = get_listptr (&known_addrbases, idx);
5422 if (listptr == NULL)
5423 next_unitp = readendp;
5424 else if (listptr->cu->version < 5)
5426 next_unitp = start + listptr->offset;
5427 if (listptr->offset < off || listptr->offset > data->d_size)
5430 "Warning: Bad address base for next unit at %"
5432 next_unitp = readendp;
5437 /* Tricky, we don't have a header for this unit, but
5438 there is one for the next. We will have to
5439 "guess" how big it is and subtract it from the
5440 offset (because that points after the header). */
5441 unsigned int offset_size = listptr_offset_size (listptr);
5442 Dwarf_Off next_off = (listptr->offset
5443 - (offset_size == 4 ? 4 : 12) /* len */
5445 - 1 /* address size */
5446 - 1); /* segment selector size */
5447 next_unitp = start + next_off;
5448 if (next_off < off || next_off > data->d_size)
5451 "Warning: Couldn't calculate .debug_addr "
5452 " unit length at %" PRIx64, off);
5453 next_unitp = readendp;
5456 unit_length = (uint64_t) (next_unitp - readp);
5458 /* Pretend we have a header. */
5460 printf (_(" Length: %8" PRIu64 "\n"),
5462 printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5463 printf (_(" Address size: %8" PRIu64 "\n"),
5464 (uint64_t) address_size);
5465 printf (_(" Segment size: %8" PRIu64 "\n"),
5466 (uint64_t) segment_size);
5471 /* OK, we have to parse an header first. */
5472 unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5473 if (unlikely (unit_length == 0xffffffff))
5475 if (unlikely (readp > readendp - 8))
5478 error (0, 0, "Invalid data");
5481 unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5484 printf (_(" Length: %8" PRIu64 "\n"),
5487 /* We need at least 2-bytes (version) + 1-byte
5488 (addr_size) + 1-byte (segment_size) = 4 bytes to
5489 complete the header. And this unit cannot go beyond
5490 the section data. */
5491 if (readp > readendp - 4
5493 || unit_length > (uint64_t) (readendp - readp))
5496 next_unitp = readp + unit_length;
5498 version = read_2ubyte_unaligned_inc (dbg, readp);
5499 printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5503 error (0, 0, _("Unknown version"));
5507 address_size = *readp++;
5508 printf (_(" Address size: %8" PRIu64 "\n"),
5509 (uint64_t) address_size);
5511 if (address_size != 4 && address_size != 8)
5513 error (0, 0, _("unsupported address size"));
5517 segment_size = *readp++;
5518 printf (_(" Segment size: %8" PRIu64 "\n"),
5519 (uint64_t) segment_size);
5522 if (segment_size != 0)
5524 error (0, 0, _("unsupported segment size"));
5528 if (listptr->offset != (Dwarf_Off) (readp - start))
5530 error (0, 0, "Address index doesn't start after header");
5537 size_t addresses = (next_unitp - readp) / address_size;
5538 while (addresses >= 10)
5544 unsigned int uidx = 0;
5545 size_t index_offset = readp - (const unsigned char *) data->d_buf;
5546 printf (" Addresses start at offset 0x%zx:\n", index_offset);
5547 while (readp <= next_unitp - address_size)
5549 Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
5551 printf (" [%*u] ", digits, uidx++);
5552 print_dwarf_addr (dwflmod, address_size, addr, addr);
5557 if (readp != next_unitp)
5558 error (0, 0, "extra %zd bytes at end of unit",
5559 (size_t) (next_unitp - readp));
5566 /* Print content of DWARF .debug_aranges section. We fortunately do
5567 not have to know a bit about the structure of the section, libdwarf
5568 takes care of it. */
5570 print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5571 GElf_Shdr *shdr, Dwarf *dbg)
5573 Dwarf_Aranges *aranges;
5575 if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
5577 error (0, 0, _("cannot get .debug_aranges content: %s"),
5582 GElf_Shdr glink_mem;
5584 glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
5587 error (0, 0, _("invalid sh_link value in section %zu"),
5592 printf (ngettext ("\
5593 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
5595 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
5597 elf_ndxscn (scn), section_name (ebl, shdr),
5598 (uint64_t) shdr->sh_offset, cnt);
5600 /* Compute floor(log16(cnt)). */
5609 for (size_t n = 0; n < cnt; ++n)
5611 Dwarf_Arange *runp = dwarf_onearange (aranges, n);
5612 if (unlikely (runp == NULL))
5614 printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
5622 if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
5623 printf (_(" [%*zu] ???\n"), digits, n);
5625 printf (_(" [%*zu] start: %0#*" PRIx64
5626 ", length: %5" PRIu64 ", CU DIE offset: %6"
5628 digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
5629 (uint64_t) start, (uint64_t) length, (int64_t) offset);
5634 /* Print content of DWARF .debug_aranges section. */
5636 print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5637 Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5638 GElf_Shdr *shdr, Dwarf *dbg)
5642 print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
5646 Elf_Data *data = (dbg->sectiondata[IDX_debug_aranges]
5647 ?: elf_rawdata (scn, NULL));
5649 if (unlikely (data == NULL))
5651 error (0, 0, _("cannot get .debug_aranges content: %s"),
5657 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5658 elf_ndxscn (scn), section_name (ebl, shdr),
5659 (uint64_t) shdr->sh_offset);
5661 const unsigned char *readp = data->d_buf;
5662 const unsigned char *readendp = readp + data->d_size;
5664 while (readp < readendp)
5666 const unsigned char *hdrstart = readp;
5667 size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
5669 printf (_("\nTable at offset %zu:\n"), start_offset);
5670 if (readp + 4 > readendp)
5673 error (0, 0, _("invalid data in section [%zu] '%s'"),
5674 elf_ndxscn (scn), section_name (ebl, shdr));
5678 Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
5679 unsigned int length_bytes = 4;
5680 if (length == DWARF3_LENGTH_64_BIT)
5682 if (readp + 8 > readendp)
5684 length = read_8ubyte_unaligned_inc (dbg, readp);
5688 const unsigned char *nexthdr = readp + length;
5689 printf (_("\n Length: %6" PRIu64 "\n"),
5692 if (unlikely (length > (size_t) (readendp - readp)))
5698 if (readp + 2 > readendp)
5700 uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5701 printf (_(" DWARF version: %6" PRIuFAST16 "\n"),
5705 error (0, 0, _("unsupported aranges version"));
5710 if (readp + length_bytes > readendp)
5712 if (length_bytes == 8)
5713 offset = read_8ubyte_unaligned_inc (dbg, readp);
5715 offset = read_4ubyte_unaligned_inc (dbg, readp);
5716 printf (_(" CU offset: %6" PRIx64 "\n"),
5719 if (readp + 1 > readendp)
5721 unsigned int address_size = *readp++;
5722 printf (_(" Address size: %6" PRIu64 "\n"),
5723 (uint64_t) address_size);
5724 if (address_size != 4 && address_size != 8)
5726 error (0, 0, _("unsupported address size"));
5730 if (readp + 1 > readendp)
5732 unsigned int segment_size = *readp++;
5733 printf (_(" Segment size: %6" PRIu64 "\n\n"),
5734 (uint64_t) segment_size);
5735 if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5737 error (0, 0, _("unsupported segment size"));
5741 /* Round the address to the next multiple of 2*address_size. */
5742 readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
5743 % (2 * address_size));
5745 while (readp < nexthdr)
5747 Dwarf_Word range_address;
5748 Dwarf_Word range_length;
5749 Dwarf_Word segment = 0;
5750 if (readp + 2 * address_size + segment_size > readendp)
5752 if (address_size == 4)
5754 range_address = read_4ubyte_unaligned_inc (dbg, readp);
5755 range_length = read_4ubyte_unaligned_inc (dbg, readp);
5759 range_address = read_8ubyte_unaligned_inc (dbg, readp);
5760 range_length = read_8ubyte_unaligned_inc (dbg, readp);
5763 if (segment_size == 4)
5764 segment = read_4ubyte_unaligned_inc (dbg, readp);
5765 else if (segment_size == 8)
5766 segment = read_8ubyte_unaligned_inc (dbg, readp);
5768 if (range_address == 0 && range_length == 0 && segment == 0)
5772 print_dwarf_addr (dwflmod, address_size, range_address,
5775 print_dwarf_addr (dwflmod, address_size,
5776 range_address + range_length - 1,
5778 if (segment_size != 0)
5779 printf (" (%" PRIx64 ")\n", (uint64_t) segment);
5785 if (readp != nexthdr)
5787 size_t padding = nexthdr - readp;
5788 printf (_(" %zu padding bytes\n"), padding);
5795 static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
5797 /* Returns true and sets cu and cu_base if the given Dwarf is a split
5798 DWARF (.dwo) file. */
5800 split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
5803 if (is_split_dwarf (dbg, &id, cu))
5806 if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
5808 *cu_base = cudie_base (&cudie);
5815 /* Print content of DWARF .debug_rnglists section. */
5817 print_debug_rnglists_section (Dwfl_Module *dwflmod,
5819 GElf_Ehdr *ehdr __attribute__ ((unused)),
5820 Elf_Scn *scn, GElf_Shdr *shdr,
5821 Dwarf *dbg __attribute__((unused)))
5824 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5825 elf_ndxscn (scn), section_name (ebl, shdr),
5826 (uint64_t) shdr->sh_offset);
5828 Elf_Data *data =(dbg->sectiondata[IDX_debug_rnglists]
5829 ?: elf_rawdata (scn, NULL));
5830 if (unlikely (data == NULL))
5832 error (0, 0, _("cannot get .debug_rnglists content: %s"),
5837 /* For the listptr to get the base address/CU. */
5838 sort_listptr (&known_rnglistptr, "rnglistptr");
5839 size_t listptr_idx = 0;
5841 const unsigned char *readp = data->d_buf;
5842 const unsigned char *const dataend = ((unsigned char *) data->d_buf
5844 while (readp < dataend)
5846 if (unlikely (readp > dataend - 4))
5849 error (0, 0, _("invalid data in section [%zu] '%s'"),
5850 elf_ndxscn (scn), section_name (ebl, shdr));
5854 ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
5855 printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
5858 uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5859 unsigned int offset_size = 4;
5860 if (unlikely (unit_length == 0xffffffff))
5862 if (unlikely (readp > dataend - 8))
5865 unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5868 printf (_(" Length: %8" PRIu64 "\n"), unit_length);
5870 /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
5871 bytes to complete the header. And this unit cannot go beyond
5872 the section data. */
5873 if (readp > dataend - 8
5875 || unit_length > (uint64_t) (dataend - readp))
5878 const unsigned char *nexthdr = readp + unit_length;
5880 uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5881 printf (_(" DWARF version: %8" PRIu16 "\n"), version);
5885 error (0, 0, _("Unknown version"));
5889 uint8_t address_size = *readp++;
5890 printf (_(" Address size: %8" PRIu64 "\n"),
5891 (uint64_t) address_size);
5893 if (address_size != 4 && address_size != 8)
5895 error (0, 0, _("unsupported address size"));
5899 uint8_t segment_size = *readp++;
5900 printf (_(" Segment size: %8" PRIu64 "\n"),
5901 (uint64_t) segment_size);
5903 if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5905 error (0, 0, _("unsupported segment size"));
5909 uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
5910 printf (_(" Offset entries: %8" PRIu64 "\n"),
5911 (uint64_t) offset_entry_count);
5913 /* We need the CU that uses this unit to get the initial base address. */
5914 Dwarf_Addr cu_base = 0;
5915 struct Dwarf_CU *cu = NULL;
5916 if (listptr_cu (&known_rnglistptr, &listptr_idx,
5918 (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
5920 || split_dwarf_cu_base (dbg, &cu, &cu_base))
5923 if (dwarf_cu_die (cu, &cudie,
5924 NULL, NULL, NULL, NULL,
5925 NULL, NULL) == NULL)
5926 printf (_(" Unknown CU base: "));
5928 printf (_(" CU [%6" PRIx64 "] base: "),
5929 dwarf_dieoffset (&cudie));
5930 print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
5934 printf (_(" Not associated with a CU.\n"));
5938 const unsigned char *offset_array_start = readp;
5939 if (offset_entry_count > 0)
5941 uint64_t max_entries = (unit_length - 8) / offset_size;
5942 if (offset_entry_count > max_entries)
5945 _("too many offset entries for unit length"));
5946 offset_entry_count = max_entries;
5949 printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
5950 (uint64_t) (offset_array_start
5951 - (unsigned char *) data->d_buf));
5952 for (uint32_t idx = 0; idx < offset_entry_count; idx++)
5954 printf (" [%6" PRIu32 "] ", idx);
5955 if (offset_size == 4)
5957 uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
5958 printf ("0x%" PRIx32 "\n", off);
5962 uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
5963 printf ("0x%" PRIx64 "\n", off);
5969 Dwarf_Addr base = cu_base;
5970 bool start_of_list = true;
5971 while (readp < nexthdr)
5973 uint8_t kind = *readp++;
5977 if (start_of_list && kind == DW_RLE_end_of_list)
5983 printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
5984 (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
5985 (uint64_t) (readp - offset_array_start - 1));
5986 start_of_list = false;
5989 printf (" %s", dwarf_range_list_encoding_name (kind));
5992 case DW_RLE_end_of_list:
5993 start_of_list = true;
5997 case DW_RLE_base_addressx:
5998 if ((uint64_t) (nexthdr - readp) < 1)
6001 error (0, 0, _("invalid range list data"));
6004 get_uleb128 (op1, readp, nexthdr);
6005 printf (" %" PRIx64 "\n", op1);
6006 if (! print_unresolved_addresses)
6009 if (get_indexed_addr (cu, op1, &addr) != 0)
6014 print_dwarf_addr (dwflmod, address_size, addr, addr);
6020 case DW_RLE_startx_endx:
6021 if ((uint64_t) (nexthdr - readp) < 1)
6023 get_uleb128 (op1, readp, nexthdr);
6024 if ((uint64_t) (nexthdr - readp) < 1)
6026 get_uleb128 (op2, readp, nexthdr);
6027 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6028 if (! print_unresolved_addresses)
6032 if (get_indexed_addr (cu, op1, &addr1) != 0
6033 || get_indexed_addr (cu, op2, &addr2) != 0)
6035 printf (" ???..\n");
6041 print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6043 print_dwarf_addr (dwflmod, address_size,
6050 case DW_RLE_startx_length:
6051 if ((uint64_t) (nexthdr - readp) < 1)
6053 get_uleb128 (op1, readp, nexthdr);
6054 if ((uint64_t) (nexthdr - readp) < 1)
6056 get_uleb128 (op2, readp, nexthdr);
6057 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6058 if (! print_unresolved_addresses)
6062 if (get_indexed_addr (cu, op1, &addr1) != 0)
6064 printf (" ???..\n");
6069 addr2 = addr1 + op2;
6071 print_dwarf_addr (dwflmod, address_size, addr1, addr1);
6073 print_dwarf_addr (dwflmod, address_size,
6080 case DW_RLE_offset_pair:
6081 if ((uint64_t) (nexthdr - readp) < 1)
6083 get_uleb128 (op1, readp, nexthdr);
6084 if ((uint64_t) (nexthdr - readp) < 1)
6086 get_uleb128 (op2, readp, nexthdr);
6087 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
6088 if (! print_unresolved_addresses)
6093 print_dwarf_addr (dwflmod, address_size, op1, op1);
6095 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6100 case DW_RLE_base_address:
6101 if (address_size == 4)
6103 if ((uint64_t) (nexthdr - readp) < 4)
6105 op1 = read_4ubyte_unaligned_inc (dbg, readp);
6109 if ((uint64_t) (nexthdr - readp) < 8)
6111 op1 = read_8ubyte_unaligned_inc (dbg, readp);
6114 printf (" 0x%" PRIx64 "\n", base);
6115 if (! print_unresolved_addresses)
6118 print_dwarf_addr (dwflmod, address_size, base, base);
6123 case DW_RLE_start_end:
6124 if (address_size == 4)
6126 if ((uint64_t) (nexthdr - readp) < 8)
6128 op1 = read_4ubyte_unaligned_inc (dbg, readp);
6129 op2 = read_4ubyte_unaligned_inc (dbg, readp);
6133 if ((uint64_t) (nexthdr - readp) < 16)
6135 op1 = read_8ubyte_unaligned_inc (dbg, readp);
6136 op2 = read_8ubyte_unaligned_inc (dbg, readp);
6138 printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
6139 if (! print_unresolved_addresses)
6142 print_dwarf_addr (dwflmod, address_size, op1, op1);
6144 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6149 case DW_RLE_start_length:
6150 if (address_size == 4)
6152 if ((uint64_t) (nexthdr - readp) < 4)
6154 op1 = read_4ubyte_unaligned_inc (dbg, readp);
6158 if ((uint64_t) (nexthdr - readp) < 8)
6160 op1 = read_8ubyte_unaligned_inc (dbg, readp);
6162 if ((uint64_t) (nexthdr - readp) < 1)
6164 get_uleb128 (op2, readp, nexthdr);
6165 printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
6166 if (! print_unresolved_addresses)
6170 print_dwarf_addr (dwflmod, address_size, op1, op1);
6172 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
6183 if (readp != nexthdr)
6185 size_t padding = nexthdr - readp;
6186 printf (_(" %zu padding bytes\n\n"), padding);
6192 /* Print content of DWARF .debug_ranges section. */
6194 print_debug_ranges_section (Dwfl_Module *dwflmod,
6195 Ebl *ebl, GElf_Ehdr *ehdr,
6196 Elf_Scn *scn, GElf_Shdr *shdr,
6199 Elf_Data *data = (dbg->sectiondata[IDX_debug_ranges]
6200 ?: elf_rawdata (scn, NULL));
6201 if (unlikely (data == NULL))
6203 error (0, 0, _("cannot get .debug_ranges content: %s"),
6209 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6210 elf_ndxscn (scn), section_name (ebl, shdr),
6211 (uint64_t) shdr->sh_offset);
6213 sort_listptr (&known_rangelistptr, "rangelistptr");
6214 size_t listptr_idx = 0;
6216 uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6219 Dwarf_Addr base = 0;
6220 unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
6221 unsigned char *readp = data->d_buf;
6222 Dwarf_CU *last_cu = NULL;
6223 while (readp < endp)
6225 ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6226 Dwarf_CU *cu = last_cu;
6228 if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
6229 &address_size, NULL, &base, &cu,
6230 offset, &readp, endp, NULL))
6236 if (dwarf_cu_die (cu, &cudie,
6237 NULL, NULL, NULL, NULL,
6238 NULL, NULL) == NULL)
6239 printf (_("\n Unknown CU base: "));
6241 printf (_("\n CU [%6" PRIx64 "] base: "),
6242 dwarf_dieoffset (&cudie));
6243 print_dwarf_addr (dwflmod, address_size, base, base);
6248 if (unlikely (data->d_size - offset < (size_t) address_size * 2))
6250 printf (_(" [%6tx] <INVALID DATA>\n"), offset);
6256 if (address_size == 8)
6258 begin = read_8ubyte_unaligned_inc (dbg, readp);
6259 end = read_8ubyte_unaligned_inc (dbg, readp);
6263 begin = read_4ubyte_unaligned_inc (dbg, readp);
6264 end = read_4ubyte_unaligned_inc (dbg, readp);
6265 if (begin == (Dwarf_Addr) (uint32_t) -1)
6266 begin = (Dwarf_Addr) -1l;
6269 if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6272 printf (" [%6tx] ", offset);
6275 puts (_("base address"));
6277 print_dwarf_addr (dwflmod, address_size, end, end);
6282 else if (begin == 0 && end == 0) /* End of list entry. */
6285 printf (_(" [%6tx] empty list\n"), offset);
6290 /* We have an address range entry. */
6291 if (first) /* First address range entry in a list. */
6292 printf (" [%6tx] ", offset);
6296 printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6297 if (! print_unresolved_addresses)
6300 print_dwarf_addr (dwflmod, address_size, base + begin,
6303 print_dwarf_addr (dwflmod, address_size,
6304 base + end - 1, base + end);
6313 #define REGNAMESZ 16
6315 register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6316 char name[REGNAMESZ], int *bits, int *type)
6321 ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6322 bits ?: &ignore, type ?: &ignore);
6326 snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6328 snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6330 *bits = loc != NULL ? loc->bits : 0;
6332 *type = DW_ATE_unsigned;
6333 set = "??? unrecognized";
6337 if (bits != NULL && *bits <= 0)
6338 *bits = loc != NULL ? loc->bits : 0;
6339 if (type != NULL && *type == DW_ATE_void)
6340 *type = DW_ATE_unsigned;
6346 static const unsigned char *
6347 read_encoded (unsigned int encoding, const unsigned char *readp,
6348 const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6350 if ((encoding & 0xf) == DW_EH_PE_absptr)
6351 encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6352 ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6354 switch (encoding & 0xf)
6356 case DW_EH_PE_uleb128:
6357 get_uleb128 (*res, readp, endp);
6359 case DW_EH_PE_sleb128:
6360 get_sleb128 (*res, readp, endp);
6362 case DW_EH_PE_udata2:
6363 if (readp + 2 > endp)
6365 *res = read_2ubyte_unaligned_inc (dbg, readp);
6367 case DW_EH_PE_udata4:
6368 if (readp + 4 > endp)
6370 *res = read_4ubyte_unaligned_inc (dbg, readp);
6372 case DW_EH_PE_udata8:
6373 if (readp + 8 > endp)
6375 *res = read_8ubyte_unaligned_inc (dbg, readp);
6377 case DW_EH_PE_sdata2:
6378 if (readp + 2 > endp)
6380 *res = read_2sbyte_unaligned_inc (dbg, readp);
6382 case DW_EH_PE_sdata4:
6383 if (readp + 4 > endp)
6385 *res = read_4sbyte_unaligned_inc (dbg, readp);
6387 case DW_EH_PE_sdata8:
6388 if (readp + 8 > endp)
6390 *res = read_8sbyte_unaligned_inc (dbg, readp);
6395 _("invalid encoding"));
6402 regname (Ebl *ebl, unsigned int regno, char *regnamebuf)
6404 register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6410 print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6411 Dwarf_Word vma_base, unsigned int code_align,
6413 unsigned int version, unsigned int ptr_size,
6414 unsigned int encoding,
6415 Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr, Dwarf *dbg)
6417 char regnamebuf[REGNAMESZ];
6419 puts ("\n Program:");
6420 Dwarf_Word pc = vma_base;
6421 while (readp < endp)
6423 unsigned int opcode = *readp++;
6425 if (opcode < DW_CFA_advance_loc)
6426 /* Extended opcode. */
6437 case DW_CFA_set_loc:
6438 if ((uint64_t) (endp - readp) < 1)
6440 readp = read_encoded (encoding, readp, endp, &op1, dbg);
6441 printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6442 op1, pc = vma_base + op1);
6444 case DW_CFA_advance_loc1:
6445 if ((uint64_t) (endp - readp) < 1)
6447 printf (" advance_loc1 %u to %#" PRIx64 "\n",
6448 *readp, pc += *readp * code_align);
6451 case DW_CFA_advance_loc2:
6452 if ((uint64_t) (endp - readp) < 2)
6454 op1 = read_2ubyte_unaligned_inc (dbg, readp);
6455 printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6456 op1, pc += op1 * code_align);
6458 case DW_CFA_advance_loc4:
6459 if ((uint64_t) (endp - readp) < 4)
6461 op1 = read_4ubyte_unaligned_inc (dbg, readp);
6462 printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
6463 op1, pc += op1 * code_align);
6465 case DW_CFA_offset_extended:
6466 if ((uint64_t) (endp - readp) < 1)
6468 get_uleb128 (op1, readp, endp);
6469 if ((uint64_t) (endp - readp) < 1)
6471 get_uleb128 (op2, readp, endp);
6472 printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
6474 op1, regname (ebl, op1, regnamebuf), op2 * data_align);
6476 case DW_CFA_restore_extended:
6477 if ((uint64_t) (endp - readp) < 1)
6479 get_uleb128 (op1, readp, endp);
6480 printf (" restore_extended r%" PRIu64 " (%s)\n",
6481 op1, regname (ebl, op1, regnamebuf));
6483 case DW_CFA_undefined:
6484 if ((uint64_t) (endp - readp) < 1)
6486 get_uleb128 (op1, readp, endp);
6487 printf (" undefined r%" PRIu64 " (%s)\n", op1,
6488 regname (ebl, op1, regnamebuf));
6490 case DW_CFA_same_value:
6491 if ((uint64_t) (endp - readp) < 1)
6493 get_uleb128 (op1, readp, endp);
6494 printf (" same_value r%" PRIu64 " (%s)\n", op1,
6495 regname (ebl, op1, regnamebuf));
6497 case DW_CFA_register:
6498 if ((uint64_t) (endp - readp) < 1)
6500 get_uleb128 (op1, readp, endp);
6501 if ((uint64_t) (endp - readp) < 1)
6503 get_uleb128 (op2, readp, endp);
6504 printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
6505 op1, regname (ebl, op1, regnamebuf), op2,
6506 regname (ebl, op2, regnamebuf));
6508 case DW_CFA_remember_state:
6509 puts (" remember_state");
6511 case DW_CFA_restore_state:
6512 puts (" restore_state");
6514 case DW_CFA_def_cfa:
6515 if ((uint64_t) (endp - readp) < 1)
6517 get_uleb128 (op1, readp, endp);
6518 if ((uint64_t) (endp - readp) < 1)
6520 get_uleb128 (op2, readp, endp);
6521 printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6522 op1, regname (ebl, op1, regnamebuf), op2);
6524 case DW_CFA_def_cfa_register:
6525 if ((uint64_t) (endp - readp) < 1)
6527 get_uleb128 (op1, readp, endp);
6528 printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6529 op1, regname (ebl, op1, regnamebuf));
6531 case DW_CFA_def_cfa_offset:
6532 if ((uint64_t) (endp - readp) < 1)
6534 get_uleb128 (op1, readp, endp);
6535 printf (" def_cfa_offset %" PRIu64 "\n", op1);
6537 case DW_CFA_def_cfa_expression:
6538 if ((uint64_t) (endp - readp) < 1)
6540 get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
6541 printf (" def_cfa_expression %" PRIu64 "\n", op1);
6542 if ((uint64_t) (endp - readp) < op1)
6545 fputs (_(" <INVALID DATA>\n"), stdout);
6548 print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6552 case DW_CFA_expression:
6553 if ((uint64_t) (endp - readp) < 1)
6555 get_uleb128 (op1, readp, endp);
6556 if ((uint64_t) (endp - readp) < 1)
6558 get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6559 printf (" expression r%" PRIu64 " (%s) \n",
6560 op1, regname (ebl, op1, regnamebuf));
6561 if ((uint64_t) (endp - readp) < op2)
6563 print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6567 case DW_CFA_offset_extended_sf:
6568 if ((uint64_t) (endp - readp) < 1)
6570 get_uleb128 (op1, readp, endp);
6571 if ((uint64_t) (endp - readp) < 1)
6573 get_sleb128 (sop2, readp, endp);
6574 printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
6576 op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6578 case DW_CFA_def_cfa_sf:
6579 if ((uint64_t) (endp - readp) < 1)
6581 get_uleb128 (op1, readp, endp);
6582 if ((uint64_t) (endp - readp) < 1)
6584 get_sleb128 (sop2, readp, endp);
6585 printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
6586 op1, regname (ebl, op1, regnamebuf), sop2 * data_align);
6588 case DW_CFA_def_cfa_offset_sf:
6589 if ((uint64_t) (endp - readp) < 1)
6591 get_sleb128 (sop1, readp, endp);
6592 printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
6594 case DW_CFA_val_offset:
6595 if ((uint64_t) (endp - readp) < 1)
6597 get_uleb128 (op1, readp, endp);
6598 if ((uint64_t) (endp - readp) < 1)
6600 get_uleb128 (op2, readp, endp);
6601 printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n",
6602 op1, op2 * data_align);
6604 case DW_CFA_val_offset_sf:
6605 if ((uint64_t) (endp - readp) < 1)
6607 get_uleb128 (op1, readp, endp);
6608 if ((uint64_t) (endp - readp) < 1)
6610 get_sleb128 (sop2, readp, endp);
6611 printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
6612 op1, sop2 * data_align);
6614 case DW_CFA_val_expression:
6615 if ((uint64_t) (endp - readp) < 1)
6617 get_uleb128 (op1, readp, endp);
6618 if ((uint64_t) (endp - readp) < 1)
6620 get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6621 printf (" val_expression r%" PRIu64 " (%s)\n",
6622 op1, regname (ebl, op1, regnamebuf));
6623 if ((uint64_t) (endp - readp) < op2)
6625 print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
6629 case DW_CFA_MIPS_advance_loc8:
6630 if ((uint64_t) (endp - readp) < 8)
6632 op1 = read_8ubyte_unaligned_inc (dbg, readp);
6633 printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
6634 op1, pc += op1 * code_align);
6636 case DW_CFA_GNU_window_save: /* DW_CFA_AARCH64_negate_ra_state */
6637 if (ehdr->e_machine == EM_AARCH64)
6638 puts (" AARCH64_negate_ra_state");
6640 puts (" GNU_window_save");
6642 case DW_CFA_GNU_args_size:
6643 if ((uint64_t) (endp - readp) < 1)
6645 get_uleb128 (op1, readp, endp);
6646 printf (" args_size %" PRIu64 "\n", op1);
6649 printf (" ??? (%u)\n", opcode);
6652 else if (opcode < DW_CFA_offset)
6653 printf (" advance_loc %u to %#" PRIx64 "\n",
6654 opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
6655 else if (opcode < DW_CFA_restore)
6658 if ((uint64_t) (endp - readp) < 1)
6660 get_uleb128 (offset, readp, endp);
6661 printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
6662 opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf),
6663 offset * data_align);
6666 printf (" restore r%u (%s)\n",
6667 opcode & 0x3f, regname (ebl, opcode & 0x3f, regnamebuf));
6673 encoded_ptr_size (int encoding, unsigned int ptr_size)
6675 switch (encoding & 7)
6677 case DW_EH_PE_udata4:
6679 case DW_EH_PE_udata8:
6685 fprintf (stderr, "Unsupported pointer encoding: %#x, "
6686 "assuming pointer size of %d.\n", encoding, ptr_size);
6692 print_encoding (unsigned int val)
6696 case DW_EH_PE_absptr:
6697 fputs ("absptr", stdout);
6699 case DW_EH_PE_uleb128:
6700 fputs ("uleb128", stdout);
6702 case DW_EH_PE_udata2:
6703 fputs ("udata2", stdout);
6705 case DW_EH_PE_udata4:
6706 fputs ("udata4", stdout);
6708 case DW_EH_PE_udata8:
6709 fputs ("udata8", stdout);
6711 case DW_EH_PE_sleb128:
6712 fputs ("sleb128", stdout);
6714 case DW_EH_PE_sdata2:
6715 fputs ("sdata2", stdout);
6717 case DW_EH_PE_sdata4:
6718 fputs ("sdata4", stdout);
6720 case DW_EH_PE_sdata8:
6721 fputs ("sdata8", stdout);
6724 /* We did not use any of the bits after all. */
6733 print_relinfo (unsigned int val)
6737 case DW_EH_PE_pcrel:
6738 fputs ("pcrel", stdout);
6740 case DW_EH_PE_textrel:
6741 fputs ("textrel", stdout);
6743 case DW_EH_PE_datarel:
6744 fputs ("datarel", stdout);
6746 case DW_EH_PE_funcrel:
6747 fputs ("funcrel", stdout);
6749 case DW_EH_PE_aligned:
6750 fputs ("aligned", stdout);
6761 print_encoding_base (const char *pfx, unsigned int fde_encoding)
6763 printf ("(%s", pfx);
6765 if (fde_encoding == DW_EH_PE_omit)
6769 unsigned int w = fde_encoding;
6771 w = print_encoding (w);
6775 if (w != fde_encoding)
6776 fputc_unlocked (' ', stdout);
6778 w = print_relinfo (w);
6782 printf ("%s%x", w != fde_encoding ? " " : "", w);
6790 print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
6791 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
6794 /* We know this call will succeed since it did in the caller. */
6795 (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
6796 const char *scnname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
6798 /* Needed if we find PC-relative addresses. */
6800 if (dwfl_module_getelf (dwflmod, &bias) == NULL)
6802 error (0, 0, _("cannot get ELF: %s"), dwfl_errmsg (-1));
6806 bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
6807 Elf_Data *data = (is_eh_frame
6808 ? elf_rawdata (scn, NULL)
6809 : (dbg->sectiondata[IDX_debug_frame]
6810 ?: elf_rawdata (scn, NULL)));
6812 if (unlikely (data == NULL))
6814 error (0, 0, _("cannot get %s content: %s"),
6815 scnname, elf_errmsg (-1));
6821 \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6822 elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6825 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6826 elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6830 ptrdiff_t cie_offset;
6831 const char *augmentation;
6832 unsigned int code_alignment_factor;
6833 unsigned int data_alignment_factor;
6834 uint8_t address_size;
6835 uint8_t fde_encoding;
6836 uint8_t lsda_encoding;
6837 struct cieinfo *next;
6840 const unsigned char *readp = data->d_buf;
6841 const unsigned char *const dataend = ((unsigned char *) data->d_buf
6843 while (readp < dataend)
6845 if (unlikely (readp + 4 > dataend))
6848 error (0, 0, _("invalid data in section [%zu] '%s'"),
6849 elf_ndxscn (scn), scnname);
6853 /* At the beginning there must be a CIE. There can be multiple,
6854 hence we test tis in a loop. */
6855 ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6857 Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6858 unsigned int length = 4;
6859 if (unlikely (unit_length == 0xffffffff))
6861 if (unlikely (readp + 8 > dataend))
6864 unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6868 if (unlikely (unit_length == 0))
6870 printf (_("\n [%6tx] Zero terminator\n"), offset);
6874 Dwarf_Word maxsize = dataend - readp;
6875 if (unlikely (unit_length > maxsize))
6878 unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6880 ptrdiff_t start = readp - (unsigned char *) data->d_buf;
6881 const unsigned char *const cieend = readp + unit_length;
6882 if (unlikely (cieend > dataend))
6888 if (unlikely (cieend - readp < 4))
6890 cie_id = read_4ubyte_unaligned_inc (dbg, readp);
6891 if (!is_eh_frame && cie_id == DW_CIE_ID_32)
6892 cie_id = DW_CIE_ID_64;
6896 if (unlikely (cieend - readp < 8))
6898 cie_id = read_8ubyte_unaligned_inc (dbg, readp);
6901 uint_fast8_t version = 2;
6902 unsigned int code_alignment_factor;
6903 int data_alignment_factor;
6904 unsigned int fde_encoding = 0;
6905 unsigned int lsda_encoding = 0;
6906 Dwarf_Word initial_location = 0;
6907 Dwarf_Word vma_base = 0;
6909 if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
6911 if (unlikely (cieend - readp < 2))
6914 const char *const augmentation = (const char *) readp;
6915 readp = memchr (readp, '\0', cieend - readp);
6916 if (unlikely (readp == NULL))
6920 uint_fast8_t segment_size = 0;
6923 if (cieend - readp < 5)
6925 ptr_size = *readp++;
6926 segment_size = *readp++;
6929 if (cieend - readp < 1)
6931 get_uleb128 (code_alignment_factor, readp, cieend);
6932 if (cieend - readp < 1)
6934 get_sleb128 (data_alignment_factor, readp, cieend);
6936 /* In some variant for unwind data there is another field. */
6937 if (strcmp (augmentation, "eh") == 0)
6938 readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6940 unsigned int return_address_register;
6941 if (cieend - readp < 1)
6943 if (unlikely (version == 1))
6944 return_address_register = *readp++;
6946 get_uleb128 (return_address_register, readp, cieend);
6948 printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
6949 " CIE_id: %" PRIu64 "\n"
6951 " augmentation: \"%s\"\n",
6952 offset, (uint64_t) unit_length, (uint64_t) cie_id,
6953 version, augmentation);
6955 printf (" address_size: %u\n"
6956 " segment_size: %u\n",
6957 ptr_size, segment_size);
6958 printf (" code_alignment_factor: %u\n"
6959 " data_alignment_factor: %d\n"
6960 " return_address_register: %u\n",
6961 code_alignment_factor,
6962 data_alignment_factor, return_address_register);
6964 if (augmentation[0] == 'z')
6966 unsigned int augmentationlen;
6967 get_uleb128 (augmentationlen, readp, cieend);
6969 if (augmentationlen > (size_t) (cieend - readp))
6971 error (0, 0, _("invalid augmentation length"));
6976 const char *hdr = "Augmentation data:";
6977 const char *cp = augmentation + 1;
6978 while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
6980 printf (" %-26s%#x ", hdr, *readp);
6985 fde_encoding = *readp++;
6986 print_encoding_base (_("FDE address encoding: "),
6989 else if (*cp == 'L')
6991 lsda_encoding = *readp++;
6992 print_encoding_base (_("LSDA pointer encoding: "),
6995 else if (*cp == 'P')
6997 /* Personality. This field usually has a relocation
6998 attached pointing to __gcc_personality_v0. */
6999 const unsigned char *startp = readp;
7000 unsigned int encoding = *readp++;
7002 readp = read_encoded (encoding, readp,
7003 readp - 1 + augmentationlen,
7006 while (++startp < readp)
7007 printf ("%#x ", *startp);
7010 print_encoding (encoding);
7012 switch (encoding & 0xf)
7014 case DW_EH_PE_sleb128:
7015 case DW_EH_PE_sdata2:
7016 case DW_EH_PE_sdata4:
7017 printf ("%" PRId64 ")\n", val);
7020 printf ("%#" PRIx64 ")\n", val);
7025 printf ("(%x)\n", *readp++);
7031 if (likely (ptr_size == 4 || ptr_size == 8))
7033 struct cieinfo *newp = alloca (sizeof (*newp));
7034 newp->cie_offset = offset;
7035 newp->augmentation = augmentation;
7036 newp->fde_encoding = fde_encoding;
7037 newp->lsda_encoding = lsda_encoding;
7038 newp->address_size = ptr_size;
7039 newp->code_alignment_factor = code_alignment_factor;
7040 newp->data_alignment_factor = data_alignment_factor;
7047 struct cieinfo *cie = cies;
7050 ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
7051 : cie_id == (Dwarf_Off) cie->cie_offset)
7055 if (unlikely (cie == NULL))
7057 puts ("invalid CIE reference in FDE");
7061 /* Initialize from CIE data. */
7062 fde_encoding = cie->fde_encoding;
7063 lsda_encoding = cie->lsda_encoding;
7064 ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
7065 code_alignment_factor = cie->code_alignment_factor;
7066 data_alignment_factor = cie->data_alignment_factor;
7068 const unsigned char *base = readp;
7069 // XXX There are sometimes relocations for this value
7070 initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
7071 Dwarf_Word address_range
7072 = read_addr_unaligned_inc (ptr_size, dbg, readp);
7074 /* pcrel for an FDE address is relative to the runtime
7075 address of the start_address field itself. Sign extend
7076 if necessary to make sure the calculation is done on the
7077 full 64 bit address even when initial_location only holds
7078 the lower 32 bits. */
7079 Dwarf_Addr pc_start = initial_location;
7081 pc_start = (uint64_t) (int32_t) pc_start;
7082 if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7083 pc_start += ((uint64_t) shdr->sh_addr
7084 + (base - (const unsigned char *) data->d_buf)
7087 printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
7088 " CIE_pointer: %" PRIu64 "\n"
7089 " initial_location: ",
7090 offset, (uint64_t) unit_length,
7091 cie->cie_offset, (uint64_t) cie_id);
7092 print_dwarf_addr (dwflmod, cie->address_size,
7093 pc_start, initial_location);
7094 if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7096 vma_base = (((uint64_t) shdr->sh_offset
7097 + (base - (const unsigned char *) data->d_buf)
7098 + (uint64_t) initial_location)
7100 ? UINT64_C (0xffffffff)
7101 : UINT64_C (0xffffffffffffffff)));
7102 printf (_(" (offset: %#" PRIx64 ")"),
7103 (uint64_t) vma_base);
7106 printf ("\n address_range: %#" PRIx64,
7107 (uint64_t) address_range);
7108 if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
7109 printf (_(" (end offset: %#" PRIx64 ")"),
7110 ((uint64_t) vma_base + (uint64_t) address_range)
7112 ? UINT64_C (0xffffffff)
7113 : UINT64_C (0xffffffffffffffff)));
7116 if (cie->augmentation[0] == 'z')
7118 unsigned int augmentationlen;
7119 if (cieend - readp < 1)
7121 get_uleb128 (augmentationlen, readp, cieend);
7123 if (augmentationlen > (size_t) (cieend - readp))
7125 error (0, 0, _("invalid augmentation length"));
7130 if (augmentationlen > 0)
7132 const char *hdr = "Augmentation data:";
7133 const char *cp = cie->augmentation + 1;
7136 && cp < cie->augmentation + augmentationlen + 1)
7140 uint64_t lsda_pointer;
7141 const unsigned char *p
7142 = read_encoded (lsda_encoding, &readp[u],
7143 &readp[augmentationlen],
7144 &lsda_pointer, dbg);
7147 %-26sLSDA pointer: %#" PRIx64 "\n"),
7154 while (u < augmentationlen)
7156 printf (" %-26s%#x\n", hdr, readp[u++]);
7161 readp += augmentationlen;
7165 /* Handle the initialization instructions. */
7166 if (ptr_size != 4 && ptr_size !=8)
7167 printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
7169 print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
7170 data_alignment_factor, version, ptr_size,
7171 fde_encoding, dwflmod, ebl, ehdr, dbg);
7177 /* Returns the signedness (or false if it cannot be determined) and
7178 the byte size (or zero if it cannot be gotten) of the given DIE
7179 DW_AT_type attribute. Uses dwarf_peel_type and dwarf_aggregate_size. */
7181 die_type_sign_bytes (Dwarf_Die *die, bool *is_signed, int *bytes)
7183 Dwarf_Attribute attr;
7189 if (dwarf_peel_type (dwarf_formref_die (dwarf_attr_integrate (die,
7195 *is_signed = (dwarf_formudata (dwarf_attr (&type, DW_AT_encoding,
7197 && (val == DW_ATE_signed || val == DW_ATE_signed_char));
7199 if (dwarf_aggregate_size (&type, &val) == 0)
7206 Dwfl_Module *dwflmod;
7212 unsigned int version;
7213 unsigned int addrsize;
7214 unsigned int offset_size;
7215 struct Dwarf_CU *cu;
7220 attr_callback (Dwarf_Attribute *attrp, void *arg)
7222 struct attrcb_args *cbargs = (struct attrcb_args *) arg;
7223 const int level = cbargs->level;
7224 Dwarf_Die *die = &cbargs->dies[level];
7225 bool is_split = cbargs->is_split;
7227 unsigned int attr = dwarf_whatattr (attrp);
7228 if (unlikely (attr == 0))
7230 if (!cbargs->silent)
7231 error (0, 0, _("DIE [%" PRIx64 "] "
7232 "cannot get attribute code: %s"),
7233 dwarf_dieoffset (die), dwarf_errmsg (-1));
7234 return DWARF_CB_ABORT;
7237 unsigned int form = dwarf_whatform (attrp);
7238 if (unlikely (form == 0))
7240 if (!cbargs->silent)
7241 error (0, 0, _("DIE [%" PRIx64 "] "
7242 "cannot get attribute form: %s"),
7243 dwarf_dieoffset (die), dwarf_errmsg (-1));
7244 return DWARF_CB_ABORT;
7251 case DW_FORM_addrx1:
7252 case DW_FORM_addrx2:
7253 case DW_FORM_addrx3:
7254 case DW_FORM_addrx4:
7255 case DW_FORM_GNU_addr_index:
7256 if (!cbargs->silent)
7259 if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
7262 if (!cbargs->silent)
7263 error (0, 0, _("DIE [%" PRIx64 "] "
7264 "cannot get attribute '%s' (%s) value: "
7266 dwarf_dieoffset (die),
7267 dwarf_attr_name (attr),
7268 dwarf_form_name (form),
7270 /* Don't ABORT, it might be other attributes can be resolved. */
7273 if (form != DW_FORM_addr )
7276 if (dwarf_formudata (attrp, &word) != 0)
7278 printf (" %*s%-20s (%s) [%" PRIx64 "] ",
7279 (int) (level * 2), "", dwarf_attr_name (attr),
7280 dwarf_form_name (form), word);
7283 printf (" %*s%-20s (%s) ",
7284 (int) (level * 2), "", dwarf_attr_name (attr),
7285 dwarf_form_name (form));
7286 print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, addr, addr);
7291 case DW_FORM_indirect:
7293 case DW_FORM_line_strp:
7299 case DW_FORM_string:
7300 case DW_FORM_GNU_strp_alt:
7301 case DW_FORM_GNU_str_index:
7304 const char *str = dwarf_formstring (attrp);
7305 if (unlikely (str == NULL))
7307 printf (" %*s%-20s (%s) \"%s\"\n",
7308 (int) (level * 2), "", dwarf_attr_name (attr),
7309 dwarf_form_name (form), str);
7312 case DW_FORM_ref_addr:
7313 case DW_FORM_ref_udata:
7318 case DW_FORM_GNU_ref_alt:
7319 case DW_FORM_ref_sup4:
7320 case DW_FORM_ref_sup8:
7324 if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7327 printf (" %*s%-20s (%s) ",
7328 (int) (level * 2), "", dwarf_attr_name (attr),
7329 dwarf_form_name (form));
7331 printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7333 printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7336 case DW_FORM_ref_sig8:
7339 printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7340 (int) (level * 2), "", dwarf_attr_name (attr),
7341 dwarf_form_name (form),
7342 (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7345 case DW_FORM_sec_offset:
7346 case DW_FORM_rnglistx:
7347 case DW_FORM_loclistx:
7348 case DW_FORM_implicit_const:
7351 case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7354 case DW_FORM_data1:;
7356 if (unlikely (dwarf_formudata (attrp, &num) != 0))
7359 const char *valuestr = NULL;
7360 bool as_hex_id = false;
7363 /* This case can take either a constant or a loclistptr. */
7364 case DW_AT_data_member_location:
7365 if (form != DW_FORM_sec_offset
7366 && (cbargs->version >= 4
7367 || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7369 if (!cbargs->silent)
7370 printf (" %*s%-20s (%s) %" PRIuMAX "\n",
7371 (int) (level * 2), "", dwarf_attr_name (attr),
7372 dwarf_form_name (form), (uintmax_t) num);
7377 /* These cases always take a loclist[ptr] and no constant. */
7378 case DW_AT_location:
7379 case DW_AT_data_location:
7380 case DW_AT_vtable_elem_location:
7381 case DW_AT_string_length:
7382 case DW_AT_use_location:
7383 case DW_AT_frame_base:
7384 case DW_AT_return_addr:
7385 case DW_AT_static_link:
7387 case DW_AT_GNU_call_site_value:
7388 case DW_AT_GNU_call_site_data_value:
7389 case DW_AT_GNU_call_site_target:
7390 case DW_AT_GNU_call_site_target_clobbered:
7391 case DW_AT_GNU_locviews:
7394 if (cbargs->cu->version < 5)
7396 if (! cbargs->is_split)
7398 nlpt = notice_listptr (section_loc, &known_locsptr,
7400 cbargs->offset_size,
7401 cbargs->cu, num, attr);
7408 /* Only register for a real section offset. Otherwise
7409 it is a DW_FORM_loclistx which is just an index
7410 number and we should already have registered the
7411 section offset for the index when we saw the
7412 DW_AT_loclists_base CU attribute. */
7413 if (form == DW_FORM_sec_offset)
7414 nlpt = notice_listptr (section_loc, &known_loclistsptr,
7415 cbargs->addrsize, cbargs->offset_size,
7416 cbargs->cu, num, attr);
7422 if (!cbargs->silent)
7424 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7425 printf (" %*s%-20s (%s) location list [%6"
7427 (int) (level * 2), "", dwarf_attr_name (attr),
7428 dwarf_form_name (form), (uintmax_t) num,
7429 nlpt ? "" : " <WARNING offset too big>");
7431 printf (" %*s%-20s (%s) location index [%6"
7433 (int) (level * 2), "", dwarf_attr_name (attr),
7434 dwarf_form_name (form), (uintmax_t) num);
7439 case DW_AT_loclists_base:
7441 bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7442 cbargs->addrsize, cbargs->offset_size,
7443 cbargs->cu, num, attr);
7445 if (!cbargs->silent)
7446 printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7447 (int) (level * 2), "", dwarf_attr_name (attr),
7448 dwarf_form_name (form), (uintmax_t) num,
7449 nlpt ? "" : " <WARNING offset too big>");
7454 case DW_AT_start_scope:
7457 if (cbargs->cu->version < 5)
7458 nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7459 cbargs->addrsize, cbargs->offset_size,
7460 cbargs->cu, num, attr);
7463 /* Only register for a real section offset. Otherwise
7464 it is a DW_FORM_rangelistx which is just an index
7465 number and we should already have registered the
7466 section offset for the index when we saw the
7467 DW_AT_rnglists_base CU attribute. */
7468 if (form == DW_FORM_sec_offset)
7469 nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7470 cbargs->addrsize, cbargs->offset_size,
7471 cbargs->cu, num, attr);
7476 if (!cbargs->silent)
7478 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7479 printf (" %*s%-20s (%s) range list [%6"
7481 (int) (level * 2), "", dwarf_attr_name (attr),
7482 dwarf_form_name (form), (uintmax_t) num,
7483 nlpt ? "" : " <WARNING offset too big>");
7485 printf (" %*s%-20s (%s) range index [%6"
7487 (int) (level * 2), "", dwarf_attr_name (attr),
7488 dwarf_form_name (form), (uintmax_t) num);
7493 case DW_AT_rnglists_base:
7495 bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7496 cbargs->addrsize, cbargs->offset_size,
7497 cbargs->cu, num, attr);
7498 if (!cbargs->silent)
7499 printf (" %*s%-20s (%s) range list [%6"
7501 (int) (level * 2), "", dwarf_attr_name (attr),
7502 dwarf_form_name (form), (uintmax_t) num,
7503 nlpt ? "" : " <WARNING offset too big>");
7507 case DW_AT_addr_base:
7508 case DW_AT_GNU_addr_base:
7510 bool addrbase = notice_listptr (section_addr, &known_addrbases,
7512 cbargs->offset_size,
7513 cbargs->cu, num, attr);
7514 if (!cbargs->silent)
7515 printf (" %*s%-20s (%s) address base [%6"
7517 (int) (level * 2), "", dwarf_attr_name (attr),
7518 dwarf_form_name (form), (uintmax_t) num,
7519 addrbase ? "" : " <WARNING offset too big>");
7523 case DW_AT_str_offsets_base:
7525 bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7527 cbargs->offset_size,
7528 cbargs->cu, num, attr);
7529 if (!cbargs->silent)
7530 printf (" %*s%-20s (%s) str offsets base [%6"
7532 (int) (level * 2), "", dwarf_attr_name (attr),
7533 dwarf_form_name (form), (uintmax_t) num,
7534 stroffbase ? "" : " <WARNING offset too big>");
7538 case DW_AT_language:
7539 valuestr = dwarf_lang_name (num);
7541 case DW_AT_encoding:
7542 valuestr = dwarf_encoding_name (num);
7544 case DW_AT_accessibility:
7545 valuestr = dwarf_access_name (num);
7547 case DW_AT_defaulted:
7548 valuestr = dwarf_defaulted_name (num);
7550 case DW_AT_visibility:
7551 valuestr = dwarf_visibility_name (num);
7553 case DW_AT_virtuality:
7554 valuestr = dwarf_virtuality_name (num);
7556 case DW_AT_identifier_case:
7557 valuestr = dwarf_identifier_case_name (num);
7559 case DW_AT_calling_convention:
7560 valuestr = dwarf_calling_convention_name (num);
7563 valuestr = dwarf_inline_name (num);
7565 case DW_AT_ordering:
7566 valuestr = dwarf_ordering_name (num);
7568 case DW_AT_decl_file:
7569 case DW_AT_call_file:
7574 /* Try to get the actual file, the current interface only
7575 gives us full paths, but we only want to show the file
7578 if (dwarf_cu_die (cbargs->cu, &cudie,
7579 NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
7583 if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
7585 valuestr = dwarf_filesrc (files, num, NULL, NULL);
7586 if (valuestr != NULL)
7588 char *filename = strrchr (valuestr, '/');
7589 if (filename != NULL)
7590 valuestr = filename + 1;
7593 error (0, 0, _("invalid file (%" PRId64 "): %s"),
7594 num, dwarf_errmsg (-1));
7597 error (0, 0, _("no srcfiles for CU [%" PRIx64 "]"),
7598 dwarf_dieoffset (&cudie));
7601 error (0, 0, _("couldn't get DWARF CU: %s"),
7603 if (valuestr == NULL)
7607 case DW_AT_GNU_dwo_id:
7619 /* When highpc is in constant form it is relative to lowpc.
7620 In that case also show the address. */
7622 if (attr == DW_AT_high_pc && dwarf_highpc (die, &highpc) == 0)
7624 printf (" %*s%-20s (%s) %" PRIuMAX " (",
7625 (int) (level * 2), "", dwarf_attr_name (attr),
7626 dwarf_form_name (form), (uintmax_t) num);
7627 print_dwarf_addr (cbargs->dwflmod, cbargs->addrsize, highpc, highpc);
7634 printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
7635 (int) (level * 2), "", dwarf_attr_name (attr),
7636 dwarf_form_name (form), num);
7640 Dwarf_Sword snum = 0;
7643 if (attr == DW_AT_const_value)
7644 die_type_sign_bytes (die, &is_signed, &bytes);
7646 is_signed = (form == DW_FORM_sdata
7647 || form == DW_FORM_implicit_const);
7650 if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
7653 if (valuestr == NULL)
7655 printf (" %*s%-20s (%s) ",
7656 (int) (level * 2), "", dwarf_attr_name (attr),
7657 dwarf_form_name (form));
7661 printf (" %*s%-20s (%s) %s (",
7662 (int) (level * 2), "", dwarf_attr_name (attr),
7663 dwarf_form_name (form), valuestr);
7670 printf ("%" PRId8, (int8_t) snum);
7672 printf ("%" PRIu8, (uint8_t) num);
7677 printf ("%" PRId16, (int16_t) snum);
7679 printf ("%" PRIu16, (uint16_t) num);
7684 printf ("%" PRId32, (int32_t) snum);
7686 printf ("%" PRIu32, (uint32_t) num);
7691 printf ("%" PRId64, (int64_t) snum);
7693 printf ("%" PRIu64, (uint64_t) num);
7698 printf ("%" PRIdMAX, (intmax_t) snum);
7700 printf ("%" PRIuMAX, (uintmax_t) num);
7704 /* Make clear if we switched from a signed encoding to
7705 an unsigned value. */
7706 if (attr == DW_AT_const_value
7707 && (form == DW_FORM_sdata || form == DW_FORM_implicit_const)
7709 printf (" (%" PRIdMAX ")", (intmax_t) num);
7711 if (valuestr == NULL)
7723 if (unlikely (dwarf_formflag (attrp, &flag) != 0))
7726 printf (" %*s%-20s (%s) %s\n",
7727 (int) (level * 2), "", dwarf_attr_name (attr),
7728 dwarf_form_name (form), flag ? yes_str : no_str);
7731 case DW_FORM_flag_present:
7734 printf (" %*s%-20s (%s) %s\n",
7735 (int) (level * 2), "", dwarf_attr_name (attr),
7736 dwarf_form_name (form), yes_str);
7739 case DW_FORM_exprloc:
7740 case DW_FORM_block4:
7741 case DW_FORM_block2:
7742 case DW_FORM_block1:
7744 case DW_FORM_data16: /* DWARF5 calls this a constant class. */
7748 if (unlikely (dwarf_formblock (attrp, &block) != 0))
7751 printf (" %*s%-20s (%s) ",
7752 (int) (level * 2), "", dwarf_attr_name (attr),
7753 dwarf_form_name (form));
7758 if (form != DW_FORM_exprloc)
7760 print_block (block.length, block.data);
7765 case DW_AT_location:
7766 case DW_AT_data_location:
7767 case DW_AT_data_member_location:
7768 case DW_AT_vtable_elem_location:
7769 case DW_AT_string_length:
7770 case DW_AT_use_location:
7771 case DW_AT_frame_base:
7772 case DW_AT_return_addr:
7773 case DW_AT_static_link:
7774 case DW_AT_allocated:
7775 case DW_AT_associated:
7776 case DW_AT_bit_size:
7777 case DW_AT_bit_offset:
7778 case DW_AT_bit_stride:
7779 case DW_AT_byte_size:
7780 case DW_AT_byte_stride:
7782 case DW_AT_lower_bound:
7783 case DW_AT_upper_bound:
7784 case DW_AT_GNU_call_site_value:
7785 case DW_AT_GNU_call_site_data_value:
7786 case DW_AT_GNU_call_site_target:
7787 case DW_AT_GNU_call_site_target_clobbered:
7788 if (form == DW_FORM_exprloc
7789 || (form != DW_FORM_data16
7790 && attrp->cu->version < 4)) /* blocks were expressions. */
7793 print_ops (cbargs->dwflmod, cbargs->dbg,
7794 12 + level * 2, 12 + level * 2,
7795 cbargs->version, cbargs->addrsize, cbargs->offset_size,
7796 attrp->cu, block.length, block.data);
7799 print_block (block.length, block.data);
7802 case DW_AT_discr_list:
7803 if (block.length == 0)
7805 else if (form != DW_FORM_data16)
7807 const unsigned char *readp = block.data;
7808 const unsigned char *readendp = readp + block.length;
7810 /* See if we are dealing with a signed or unsigned
7811 values. If the parent of this variant DIE is a
7812 variant_part then it will either have a discriminant
7813 which points to the member which type is the
7814 discriminant type. Or the variant_part itself has a
7815 type representing the discriminant. */
7816 bool is_signed = false;
7819 Dwarf_Die *parent = &cbargs->dies[level - 1];
7820 if (dwarf_tag (die) == DW_TAG_variant
7821 && dwarf_tag (parent) == DW_TAG_variant_part)
7824 Dwarf_Attribute discr_attr;
7826 if (dwarf_formref_die (dwarf_attr (parent,
7830 die_type_sign_bytes (&member, &is_signed, &bytes);
7832 die_type_sign_bytes (parent, &is_signed, &bytes);
7835 while (readp < readendp)
7837 int d = (int) *readp++;
7838 printf ("%s ", dwarf_discr_list_name (d));
7839 if (readp >= readendp)
7844 if (d == DW_DSC_label)
7848 get_sleb128 (sval, readp, readendp);
7849 printf ("%" PRId64 "", sval);
7853 get_uleb128 (val, readp, readendp);
7854 printf ("%" PRIu64 "", val);
7857 else if (d == DW_DSC_range)
7861 get_sleb128 (sval, readp, readendp);
7862 printf ("%" PRId64 "..", sval);
7863 if (readp >= readendp)
7865 get_sleb128 (sval, readp, readendp);
7866 printf ("%" PRId64 "", sval);
7870 get_uleb128 (val, readp, readendp);
7871 printf ("%" PRIu64 "..", val);
7872 if (readp >= readendp)
7874 get_uleb128 (val, readp, readendp);
7875 printf ("%" PRIu64 "", val);
7880 print_block (readendp - readp, readp);
7883 if (readp < readendp)
7889 print_block (block.length, block.data);
7897 printf (" %*s%-20s (%s) ???\n",
7898 (int) (level * 2), "", dwarf_attr_name (attr),
7899 dwarf_form_name (form));
7907 print_debug_units (Dwfl_Module *dwflmod,
7908 Ebl *ebl, GElf_Ehdr *ehdr __attribute__ ((unused)),
7909 Elf_Scn *scn, GElf_Shdr *shdr,
7910 Dwarf *dbg, bool debug_types)
7912 const bool silent = !(print_debug_sections & section_info) && !debug_types;
7913 const char *secname = section_name (ebl, shdr);
7917 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
7918 elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
7920 /* If the section is empty we don't have to do anything. */
7921 if (!silent && shdr->sh_size == 0)
7925 Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
7927 /* New compilation unit. */
7931 Dwarf_Off abbroffset;
7935 Dwarf_Off subdie_off;
7943 /* We cheat a little because we want to see only the CUs from .debug_info
7944 or .debug_types. We know the Dwarf_CU struct layout. Set it up at
7945 the end of .debug_info if we want .debug_types only. Check the returned
7946 Dwarf_CU is still in the expected section. */
7950 cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
7951 cu_mem.sec_idx = IDX_debug_info;
7958 unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
7966 error (0, 0, _("cannot get next unit: %s"), dwarf_errmsg (-1));
7970 if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
7973 dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
7974 &unit_id, &subdie_off);
7978 Dwarf_Off offset = cu->start;
7979 if (debug_types && version < 5)
7982 Dwarf_Off dieoffset;
7983 dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, cu->start
7986 printf (_(" Type unit at offset %" PRIu64 ":\n"
7987 " Version: %" PRIu16
7988 ", Abbreviation section offset: %" PRIu64
7989 ", Address size: %" PRIu8
7990 ", Offset size: %" PRIu8
7991 "\n Type signature: %#" PRIx64
7992 ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
7993 (uint64_t) offset, version, abbroffset, addrsize, offsize,
7994 unit_id, (uint64_t) subdie_off, dieoffset);
7998 printf (_(" Compilation unit at offset %" PRIu64 ":\n"
7999 " Version: %" PRIu16
8000 ", Abbreviation section offset: %" PRIu64
8001 ", Address size: %" PRIu8
8002 ", Offset size: %" PRIu8 "\n"),
8003 (uint64_t) offset, version, abbroffset, addrsize, offsize);
8005 if (version >= 5 || (unit_type != DW_UT_compile
8006 && unit_type != DW_UT_partial))
8008 printf (_(" Unit type: %s (%" PRIu8 ")"),
8009 dwarf_unit_name (unit_type), unit_type);
8010 if (unit_type == DW_UT_type
8011 || unit_type == DW_UT_skeleton
8012 || unit_type == DW_UT_split_compile
8013 || unit_type == DW_UT_split_type)
8014 printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8015 if (unit_type == DW_UT_type
8016 || unit_type == DW_UT_split_type)
8019 Dwarf_Off dieoffset;
8020 dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
8022 dieoffset = dwarf_dieoffset (&typedie);
8023 printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
8024 subdie_off, dieoffset);
8031 if (version < 2 || version > 5
8032 || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
8035 error (0, 0, _("unknown version (%d) or unit type (%d)"),
8036 version, unit_type);
8040 struct attrcb_args args =
8045 .addrsize = addrsize,
8046 .offset_size = offsize
8049 bool is_split = false;
8052 args.cu = dies[0].cu;
8054 args.is_split = is_split;
8056 /* We might return here again for the split CU subdie. */
8060 Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
8061 if (unlikely (offset == (Dwarf_Off) -1))
8064 error (0, 0, _("cannot get DIE offset: %s"),
8069 int tag = dwarf_tag (&dies[level]);
8070 if (unlikely (tag == DW_TAG_invalid))
8073 error (0, 0, _("cannot get tag of DIE at offset [%" PRIx64
8074 "] in section '%s': %s"),
8075 (uint64_t) offset, secname, dwarf_errmsg (-1));
8081 unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
8083 printf (" {%6" PRIx64 "} ", (uint64_t) offset);
8085 printf (" [%6" PRIx64 "] ", (uint64_t) offset);
8086 printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
8087 dwarf_tag_name (tag), code);
8090 /* Print the attribute values. */
8093 (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
8095 /* Make room for the next level's DIE. */
8096 if (level + 1 == maxdies)
8097 dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
8099 int res = dwarf_child (&dies[level], &dies[level + 1]);
8102 while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
8106 if (unlikely (res == -1))
8109 error (0, 0, _("cannot get next DIE: %s\n"),
8114 else if (unlikely (res < 0))
8117 error (0, 0, _("cannot get next DIE: %s"),
8126 /* We might want to show the split compile unit if this was a skeleton.
8127 We need to scan it if we are requesting printing .debug_ranges for
8128 DWARF4 since GNU DebugFission uses "offsets" into the main ranges
8130 if (unit_type == DW_UT_skeleton
8131 && ((!silent && show_split_units)
8132 || (version < 5 && (print_debug_sections & section_ranges) != 0)))
8135 if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
8136 || dwarf_tag (&subdie) == DW_TAG_invalid)
8140 Dwarf_Attribute dwo_at;
8141 const char *dwo_name =
8142 (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
8144 ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
8148 "Could not find split unit '%s', id: %" PRIx64 "\n",
8154 Dwarf_CU *split_cu = subdie.cu;
8155 dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
8156 &addrsize, &offsize, &unit_id, &subdie_off);
8157 Dwarf_Off offset = cu->start;
8161 printf (_(" Split compilation unit at offset %"
8163 " Version: %" PRIu16
8164 ", Abbreviation section offset: %" PRIu64
8165 ", Address size: %" PRIu8
8166 ", Offset size: %" PRIu8 "\n"),
8167 (uint64_t) offset, version, abbroffset,
8169 printf (_(" Unit type: %s (%" PRIu8 ")"),
8170 dwarf_unit_name (unit_type), unit_type);
8171 printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
8175 unit_type = DW_UT_split_compile;
8179 args.cu = dies[0].cu;
8180 args.dbg = split_cu->dbg;
8181 args.is_split = is_split;
8194 print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8195 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8197 print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
8201 print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8202 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8204 print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
8209 print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl,
8210 GElf_Ehdr *ehdr __attribute__ ((unused)),
8211 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8214 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
8215 elf_ndxscn (scn), section_name (ebl, shdr),
8216 (uint64_t) shdr->sh_offset);
8219 = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8223 Dwarf_Off off, next_off = 0;
8224 Dwarf_CU *cu = NULL;
8225 while (dwarf_next_lines (dbg, off = next_off, &next_off, &cu, NULL, NULL,
8226 &lines, &nlines) == 0)
8229 if (cu != NULL && dwarf_cu_info (cu, NULL, NULL, &cudie,
8230 NULL, NULL, NULL, NULL) == 0)
8231 printf (" CU [%" PRIx64 "] %s\n",
8232 dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8235 /* DWARF5 lines can be independent of any CU, but they probably
8236 are used by some CU. Determine the CU this block is for. */
8238 Dwarf_Off ncuoffset = 0;
8240 while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8241 NULL, NULL, NULL) == 0)
8243 if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8245 Dwarf_Attribute stmt_list;
8246 if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8249 if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8260 printf (" CU [%" PRIx64 "] %s\n",
8261 dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
8263 printf (" No CU\n");
8266 printf (" line:col SBPE* disc isa op address"
8267 " (Statement Block Prologue Epilogue *End)\n");
8268 const char *last_file = "";
8269 for (size_t n = 0; n < nlines; n++)
8271 Dwarf_Line *line = dwarf_onesrcline (lines, n);
8274 printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
8277 Dwarf_Word mtime, length;
8278 const char *file = dwarf_linesrc (line, &mtime, &length);
8281 printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
8284 else if (strcmp (last_file, file) != 0)
8286 printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
8287 file, mtime, length);
8292 bool statement, endseq, block, prologue_end, epilogue_begin;
8293 unsigned int lineop, isa, disc;
8295 dwarf_lineaddr (line, &address);
8296 dwarf_lineno (line, &lineno);
8297 dwarf_linecol (line, &colno);
8298 dwarf_lineop_index (line, &lineop);
8299 dwarf_linebeginstatement (line, &statement);
8300 dwarf_lineendsequence (line, &endseq);
8301 dwarf_lineblock (line, &block);
8302 dwarf_lineprologueend (line, &prologue_end);
8303 dwarf_lineepiloguebegin (line, &epilogue_begin);
8304 dwarf_lineisa (line, &isa);
8305 dwarf_linediscriminator (line, &disc);
8307 /* End sequence is special, it is one byte past. */
8308 printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d ",
8310 (statement ? 'S' : ' '),
8311 (block ? 'B' : ' '),
8312 (prologue_end ? 'P' : ' '),
8313 (epilogue_begin ? 'E' : ' '),
8314 (endseq ? '*' : ' '),
8316 print_dwarf_addr (dwflmod, address_size,
8317 address - (endseq ? 1 : 0), address);
8327 /* Print the value of a form.
8328 Returns new value of readp, or readendp on failure. */
8329 static const unsigned char *
8330 print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
8331 const unsigned char *readendp, unsigned int offset_len,
8332 Dwarf_Off str_offsets_base)
8335 unsigned char *endp;
8341 if (readendp - readp < 1)
8344 error (0, 0, "invalid data");
8348 printf (" %" PRIx8, (unsigned int) val);
8352 if (readendp - readp < 2)
8354 val = read_2ubyte_unaligned_inc (dbg, readp);
8355 printf(" %" PRIx16, (unsigned int) val);
8359 if (readendp - readp < 4)
8361 val = read_4ubyte_unaligned_inc (dbg, readp);
8362 printf (" %" PRIx32, (unsigned int) val);
8366 if (readendp - readp < 8)
8368 val = read_8ubyte_unaligned_inc (dbg, readp);
8369 printf (" %" PRIx64, val);
8373 if (readendp - readp < 1)
8375 get_sleb128 (val, readp, readendp);
8376 printf (" %" PRIx64, val);
8380 if (readendp - readp < 1)
8382 get_uleb128 (val, readp, readendp);
8383 printf (" %" PRIx64, val);
8387 if (readendp - readp < 1)
8389 get_uleb128 (val, readp, readendp);
8390 if ((size_t) (readendp - readp) < val)
8392 print_bytes (val, readp);
8396 case DW_FORM_block1:
8397 if (readendp - readp < 1)
8400 if ((size_t) (readendp - readp) < val)
8402 print_bytes (val, readp);
8406 case DW_FORM_block2:
8407 if (readendp - readp < 2)
8409 val = read_2ubyte_unaligned_inc (dbg, readp);
8410 if ((size_t) (readendp - readp) < val)
8412 print_bytes (val, readp);
8416 case DW_FORM_block4:
8417 if (readendp - readp < 4)
8419 val = read_4ubyte_unaligned_inc (dbg, readp);
8420 if ((size_t) (readendp - readp) < val)
8422 print_bytes (val, readp);
8426 case DW_FORM_data16:
8427 if (readendp - readp < 16)
8429 print_bytes (16, readp);
8434 if (readendp - readp < 1)
8437 printf ("%s", val != 0 ? yes_str : no_str);
8440 case DW_FORM_string:
8441 endp = memchr (readp, '\0', readendp - readp);
8444 printf ("%s", readp);
8449 case DW_FORM_line_strp:
8450 case DW_FORM_strp_sup:
8451 if ((size_t) (readendp - readp) < offset_len)
8453 if (offset_len == 8)
8454 val = read_8ubyte_unaligned_inc (dbg, readp);
8456 val = read_4ubyte_unaligned_inc (dbg, readp);
8457 if (form == DW_FORM_strp)
8458 data = dbg->sectiondata[IDX_debug_str];
8459 else if (form == DW_FORM_line_strp)
8460 data = dbg->sectiondata[IDX_debug_line_str];
8461 else /* form == DW_FORM_strp_sup */
8463 Dwarf *alt = dwarf_getalt (dbg);
8464 data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
8466 if (data == NULL || val >= data->d_size
8467 || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
8470 str = (char *) data->d_buf + val;
8471 printf ("%s (%" PRIu64 ")", str, val);
8474 case DW_FORM_sec_offset:
8475 if ((size_t) (readendp - readp) < offset_len)
8477 if (offset_len == 8)
8478 val = read_8ubyte_unaligned_inc (dbg, readp);
8480 val = read_4ubyte_unaligned_inc (dbg, readp);
8481 printf ("[%" PRIx64 "]", val);
8485 case DW_FORM_GNU_str_index:
8486 if (readendp - readp < 1)
8488 get_uleb128 (val, readp, readendp);
8490 data = dbg->sectiondata[IDX_debug_str_offsets];
8492 || data->d_size - str_offsets_base < val)
8496 const unsigned char *strreadp = data->d_buf + str_offsets_base + val;
8497 const unsigned char *strreadendp = data->d_buf + data->d_size;
8498 if ((size_t) (strreadendp - strreadp) < offset_len)
8503 if (offset_len == 8)
8504 idx = read_8ubyte_unaligned (dbg, strreadp);
8506 idx = read_4ubyte_unaligned (dbg, strreadp);
8508 data = dbg->sectiondata[IDX_debug_str];
8509 if (data == NULL || idx >= data->d_size
8510 || memchr (data->d_buf + idx, '\0',
8511 data->d_size - idx) == NULL)
8514 str = (char *) data->d_buf + idx;
8517 printf ("%s (%" PRIu64 ")", str, val);
8521 if (readendp - readp < 1)
8527 if (readendp - readp < 2)
8529 val = read_2ubyte_unaligned_inc (dbg, readp);
8533 if (readendp - readp < 3)
8535 val = read_3ubyte_unaligned_inc (dbg, readp);
8539 if (readendp - readp < 4)
8541 val = read_4ubyte_unaligned_inc (dbg, readp);
8545 error (0, 0, _("unknown form: %s"), dwarf_form_name (form));
8552 /* Only used via run_advance_pc() macro */
8554 run_advance_pc (unsigned int op_advance,
8555 unsigned int minimum_instr_len,
8556 unsigned int max_ops_per_instr,
8557 unsigned int *op_addr_advance,
8558 Dwarf_Word *address,
8559 unsigned int *op_index)
8561 const unsigned int advanced_op_index = (*op_index) + op_advance;
8563 *op_addr_advance = minimum_instr_len * (advanced_op_index
8564 / max_ops_per_instr);
8565 *address = *address + *op_addr_advance;
8566 *op_index = advanced_op_index % max_ops_per_instr;
8570 print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8571 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8575 print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
8580 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8581 elf_ndxscn (scn), section_name (ebl, shdr),
8582 (uint64_t) shdr->sh_offset);
8584 if (shdr->sh_size == 0)
8587 /* There is no functionality in libdw to read the information in the
8588 way it is represented here. Hardcode the decoder. */
8589 Elf_Data *data = (dbg->sectiondata[IDX_debug_line]
8590 ?: elf_rawdata (scn, NULL));
8591 if (unlikely (data == NULL))
8593 error (0, 0, _("cannot get line data section data: %s"),
8598 const unsigned char *linep = (const unsigned char *) data->d_buf;
8599 const unsigned char *lineendp;
8602 < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
8604 size_t start_offset = linep - (const unsigned char *) data->d_buf;
8606 printf (_("\nTable at offset %zu:\n"), start_offset);
8608 if (unlikely (linep + 4 > lineendp))
8610 Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
8611 unsigned int length = 4;
8612 if (unlikely (unit_length == 0xffffffff))
8614 if (unlikely (linep + 8 > lineendp))
8617 error (0, 0, _("invalid data in section [%zu] '%s'"),
8618 elf_ndxscn (scn), section_name (ebl, shdr));
8621 unit_length = read_8ubyte_unaligned_inc (dbg, linep);
8625 /* Check whether we have enough room in the section. */
8626 if (unlikely (unit_length > (size_t) (lineendp - linep)))
8628 lineendp = linep + unit_length;
8630 /* The next element of the header is the version identifier. */
8631 if ((size_t) (lineendp - linep) < 2)
8633 uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
8636 = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8637 unsigned char segment_selector_size = 0;
8640 if ((size_t) (lineendp - linep) < 2)
8642 address_size = *linep++;
8643 segment_selector_size = *linep++;
8646 /* Next comes the header length. */
8647 Dwarf_Word header_length;
8650 if ((size_t) (lineendp - linep) < 4)
8652 header_length = read_4ubyte_unaligned_inc (dbg, linep);
8656 if ((size_t) (lineendp - linep) < 8)
8658 header_length = read_8ubyte_unaligned_inc (dbg, linep);
8661 const unsigned char *header_start = linep;
8663 /* Next the minimum instruction length. */
8664 if ((size_t) (lineendp - linep) < 1)
8666 uint_fast8_t minimum_instr_len = *linep++;
8668 /* Next the maximum operations per instruction, in version 4 format. */
8669 uint_fast8_t max_ops_per_instr;
8671 max_ops_per_instr = 1;
8674 if ((size_t) (lineendp - linep) < 1)
8676 max_ops_per_instr = *linep++;
8679 /* We need at least 4 more bytes. */
8680 if ((size_t) (lineendp - linep) < 4)
8683 /* Then the flag determining the default value of the is_stmt
8685 uint_fast8_t default_is_stmt = *linep++;
8687 /* Now the line base. */
8688 int_fast8_t line_base = *linep++;
8690 /* And the line range. */
8691 uint_fast8_t line_range = *linep++;
8693 /* The opcode base. */
8694 uint_fast8_t opcode_base = *linep++;
8696 /* Print what we got so far. */
8698 " Length: %" PRIu64 "\n"
8699 " DWARF version: %" PRIuFAST16 "\n"
8700 " Prologue length: %" PRIu64 "\n"
8701 " Address size: %zd\n"
8702 " Segment selector size: %zd\n"
8703 " Min instruction length: %" PRIuFAST8 "\n"
8704 " Max operations per instruction: %" PRIuFAST8 "\n"
8705 " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
8706 " Line base: %" PRIdFAST8 "\n"
8707 " Line range: %" PRIuFAST8 "\n"
8708 " Opcode base: %" PRIuFAST8 "\n"
8711 (uint64_t) unit_length, version, (uint64_t) header_length,
8712 address_size, (size_t) segment_selector_size,
8713 minimum_instr_len, max_ops_per_instr,
8714 default_is_stmt, line_base,
8715 line_range, opcode_base);
8717 if (version < 2 || version > 5)
8719 error (0, 0, _("cannot handle .debug_line version: %u\n"),
8720 (unsigned int) version);
8725 if (address_size != 4 && address_size != 8)
8727 error (0, 0, _("cannot handle address size: %u\n"),
8728 (unsigned int) address_size);
8733 if (segment_selector_size != 0)
8735 error (0, 0, _("cannot handle segment selector size: %u\n"),
8736 (unsigned int) segment_selector_size);
8741 if (unlikely (linep + opcode_base - 1 >= lineendp))
8745 _("invalid data at offset %tu in section [%zu] '%s'"),
8746 linep - (const unsigned char *) data->d_buf,
8747 elf_ndxscn (scn), section_name (ebl, shdr));
8751 int opcode_base_l10 = 1;
8752 unsigned int tmp = opcode_base;
8758 const uint8_t *standard_opcode_lengths = linep - 1;
8759 for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
8760 printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
8761 " [%*" PRIuFAST8 "] %hhu arguments\n",
8762 (int) linep[cnt - 1]),
8763 opcode_base_l10, cnt, linep[cnt - 1]);
8764 linep += opcode_base - 1;
8766 if (unlikely (linep >= lineendp))
8769 Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
8771 puts (_("\nDirectory table:"));
8774 struct encpair { uint16_t desc; uint16_t form; };
8775 struct encpair enc[256];
8778 if ((size_t) (lineendp - linep) < 1)
8780 unsigned char directory_entry_format_count = *linep++;
8781 for (int i = 0; i < directory_entry_format_count; i++)
8783 uint16_t desc, form;
8784 if ((size_t) (lineendp - linep) < 1)
8786 get_uleb128 (desc, linep, lineendp);
8787 if ((size_t) (lineendp - linep) < 1)
8789 get_uleb128 (form, linep, lineendp);
8795 dwarf_line_content_description_name (desc),
8796 dwarf_form_name (form));
8797 if (i + 1 < directory_entry_format_count)
8802 uint64_t directories_count;
8803 if ((size_t) (lineendp - linep) < 1)
8805 get_uleb128 (directories_count, linep, lineendp);
8807 if (directory_entry_format_count == 0
8808 && directories_count != 0)
8811 for (uint64_t i = 0; i < directories_count; i++)
8813 printf (" %-5" PRIu64 " ", i);
8814 for (int j = 0; j < directory_entry_format_count; j++)
8816 linep = print_form_data (dbg, enc[j].form,
8817 linep, lineendp, length,
8819 if (j + 1 < directory_entry_format_count)
8823 if (linep >= lineendp)
8829 while (linep < lineendp && *linep != 0)
8831 unsigned char *endp = memchr (linep, '\0', lineendp - linep);
8832 if (unlikely (endp == NULL))
8835 printf (" %s\n", (char *) linep);
8839 if (linep >= lineendp || *linep != 0)
8841 /* Skip the final NUL byte. */
8845 if (unlikely (linep >= lineendp))
8848 puts (_("\nFile name table:"));
8851 struct encpair { uint16_t desc; uint16_t form; };
8852 struct encpair enc[256];
8855 if ((size_t) (lineendp - linep) < 1)
8857 unsigned char file_name_format_count = *linep++;
8858 for (int i = 0; i < file_name_format_count; i++)
8860 uint64_t desc, form;
8861 if ((size_t) (lineendp - linep) < 1)
8863 get_uleb128 (desc, linep, lineendp);
8864 if ((size_t) (lineendp - linep) < 1)
8866 get_uleb128 (form, linep, lineendp);
8868 if (! libdw_valid_user_form (form))
8875 dwarf_line_content_description_name (desc),
8876 dwarf_form_name (form));
8877 if (i + 1 < file_name_format_count)
8882 uint64_t file_name_count;
8883 if ((size_t) (lineendp - linep) < 1)
8885 get_uleb128 (file_name_count, linep, lineendp);
8887 if (file_name_format_count == 0
8888 && file_name_count != 0)
8891 for (uint64_t i = 0; i < file_name_count; i++)
8893 printf (" %-5" PRIu64 " ", i);
8894 for (int j = 0; j < file_name_format_count; j++)
8896 linep = print_form_data (dbg, enc[j].form,
8897 linep, lineendp, length,
8899 if (j + 1 < file_name_format_count)
8903 if (linep > lineendp)
8909 puts (_(" Entry Dir Time Size Name"));
8910 for (unsigned int cnt = 1; linep < lineendp && *linep != 0; ++cnt)
8912 /* First comes the file name. */
8913 char *fname = (char *) linep;
8914 unsigned char *endp = memchr (fname, '\0', lineendp - linep);
8915 if (unlikely (endp == NULL))
8919 /* Then the index. */
8920 unsigned int diridx;
8921 if (lineendp - linep < 1)
8923 get_uleb128 (diridx, linep, lineendp);
8925 /* Next comes the modification time. */
8927 if (lineendp - linep < 1)
8929 get_uleb128 (mtime, linep, lineendp);
8931 /* Finally the length of the file. */
8933 if (lineendp - linep < 1)
8935 get_uleb128 (fsize, linep, lineendp);
8937 printf (" %-5u %-5u %-9u %-9u %s\n",
8938 cnt, diridx, mtime, fsize, fname);
8940 if (linep >= lineendp || *linep != '\0')
8942 /* Skip the final NUL byte. */
8946 unsigned int debug_str_offset = 0;
8947 if (unlikely (linep == header_start + header_length - 4))
8949 /* CUBINs contain an unsigned 4-byte offset */
8950 debug_str_offset = read_4ubyte_unaligned_inc (dbg, linep);
8953 if (linep == lineendp)
8955 puts (_("\nNo line number statements."));
8959 puts (_("\nLine number statements:"));
8960 Dwarf_Word address = 0;
8961 unsigned int op_index = 0;
8963 uint_fast8_t is_stmt = default_is_stmt;
8965 /* Apply the "operation advance" from a special opcode
8966 or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
8967 unsigned int op_addr_advance;
8968 #define advance_pc(op_advance) run_advance_pc(op_advance, minimum_instr_len, \
8969 max_ops_per_instr, &op_addr_advance, &address, &op_index)
8971 if (max_ops_per_instr == 0)
8974 _("invalid maximum operations per instruction is zero"));
8979 while (linep < lineendp)
8981 size_t offset = linep - (const unsigned char *) data->d_buf;
8985 /* Read the opcode. */
8986 unsigned int opcode = *linep++;
8988 printf (" [%6" PRIx64 "]", (uint64_t)offset);
8989 /* Is this a special opcode? */
8990 if (likely (opcode >= opcode_base))
8992 if (unlikely (line_range == 0))
8995 /* Yes. Handling this is quite easy since the opcode value
8998 opcode = (desired line increment - line_base)
8999 + (line_range * address advance) + opcode_base
9001 int line_increment = (line_base
9002 + (opcode - opcode_base) % line_range);
9004 /* Perform the increments. */
9005 line += line_increment;
9006 advance_pc ((opcode - opcode_base) / line_range);
9008 printf (_(" special opcode %u: address+%u = "),
9009 opcode, op_addr_advance);
9010 print_dwarf_addr (dwflmod, 0, address, address);
9012 printf (_(", op_index = %u, line%+d = %zu\n"),
9013 op_index, line_increment, line);
9015 printf (_(", line%+d = %zu\n"),
9016 line_increment, line);
9018 else if (opcode == 0)
9020 /* This an extended opcode. */
9021 if (unlikely (linep + 2 > lineendp))
9025 unsigned int len = *linep++;
9027 if (unlikely (linep + len > lineendp))
9030 /* The sub-opcode. */
9033 printf (_(" extended opcode %u: "), opcode);
9037 case DW_LNE_end_sequence:
9038 puts (_(" end of sequence"));
9040 /* Reset the registers we care about. */
9044 is_stmt = default_is_stmt;
9047 case DW_LNE_set_address:
9049 if (unlikely ((size_t) (lineendp - linep) < address_size))
9051 if (address_size == 4)
9052 address = read_4ubyte_unaligned_inc (dbg, linep);
9054 address = read_8ubyte_unaligned_inc (dbg, linep);
9056 printf (_(" set address to "));
9057 print_dwarf_addr (dwflmod, 0, address, address);
9062 case DW_LNE_define_file:
9064 char *fname = (char *) linep;
9065 unsigned char *endp = memchr (linep, '\0',
9067 if (unlikely (endp == NULL))
9071 unsigned int diridx;
9072 if (lineendp - linep < 1)
9074 get_uleb128 (diridx, linep, lineendp);
9076 if (lineendp - linep < 1)
9078 get_uleb128 (mtime, linep, lineendp);
9079 Dwarf_Word filelength;
9080 if (lineendp - linep < 1)
9082 get_uleb128 (filelength, linep, lineendp);
9085 define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
9086 diridx, (uint64_t) mtime, (uint64_t) filelength,
9091 case DW_LNE_set_discriminator:
9092 /* Takes one ULEB128 parameter, the discriminator. */
9093 if (unlikely (standard_opcode_lengths[opcode] != 1
9094 || lineendp - linep < 1))
9097 get_uleb128 (u128, linep, lineendp);
9098 printf (_(" set discriminator to %u\n"), u128);
9101 case DW_LNE_NVIDIA_inlined_call:
9103 if (unlikely (linep >= lineendp))
9106 unsigned int context;
9107 get_uleb128 (context, linep, lineendp);
9109 if (unlikely (linep >= lineendp))
9112 unsigned int function_name;
9113 get_uleb128 (function_name, linep, lineendp);
9114 function_name += debug_str_offset;
9116 Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9118 if (str_data == NULL || function_name >= str_data->d_size
9119 || memchr (str_data->d_buf + function_name, '\0',
9120 str_data->d_size - function_name) == NULL)
9121 function_str = "???";
9123 function_str = (char *) str_data->d_buf + function_name;
9125 printf (_(" set inlined context %u,"
9126 " function name %s (0x%x)\n"),
9127 context, function_str, function_name);
9131 case DW_LNE_NVIDIA_set_function_name:
9133 if (unlikely (linep >= lineendp))
9136 unsigned int function_name;
9137 get_uleb128 (function_name, linep, lineendp);
9138 function_name += debug_str_offset;
9140 Elf_Data *str_data = dbg->sectiondata[IDX_debug_str];
9142 if (str_data == NULL || function_name >= str_data->d_size
9143 || memchr (str_data->d_buf + function_name, '\0',
9144 str_data->d_size - function_name) == NULL)
9145 function_str = "???";
9147 function_str = (char *) str_data->d_buf + function_name;
9149 printf (_(" set function name %s (0x%x)\n"),
9150 function_str, function_name);
9155 /* Unknown, ignore it. */
9156 puts (_(" unknown opcode"));
9161 else if (opcode <= DW_LNS_set_isa)
9163 /* This is a known standard opcode. */
9167 /* Takes no argument. */
9171 case DW_LNS_advance_pc:
9172 /* Takes one uleb128 parameter which is added to the
9174 if (lineendp - linep < 1)
9176 get_uleb128 (u128, linep, lineendp);
9179 printf (_(" advance address by %u to "),
9181 print_dwarf_addr (dwflmod, 0, address, address);
9183 printf (_(", op_index to %u"), op_index);
9188 case DW_LNS_advance_line:
9189 /* Takes one sleb128 parameter which is added to the
9191 if (lineendp - linep < 1)
9193 get_sleb128 (s128, linep, lineendp);
9196 advance line by constant %d to %" PRId64 "\n"),
9197 s128, (int64_t) line);
9200 case DW_LNS_set_file:
9201 /* Takes one uleb128 parameter which is stored in file. */
9202 if (lineendp - linep < 1)
9204 get_uleb128 (u128, linep, lineendp);
9205 printf (_(" set file to %" PRIu64 "\n"),
9209 case DW_LNS_set_column:
9210 /* Takes one uleb128 parameter which is stored in column. */
9211 if (unlikely (standard_opcode_lengths[opcode] != 1
9212 || lineendp - linep < 1))
9215 get_uleb128 (u128, linep, lineendp);
9216 printf (_(" set column to %" PRIu64 "\n"),
9220 case DW_LNS_negate_stmt:
9221 /* Takes no argument. */
9222 is_stmt = 1 - is_stmt;
9223 printf (_(" set '%s' to %" PRIuFAST8 "\n"),
9224 "is_stmt", is_stmt);
9227 case DW_LNS_set_basic_block:
9228 /* Takes no argument. */
9229 puts (_(" set basic block flag"));
9232 case DW_LNS_const_add_pc:
9233 /* Takes no argument. */
9235 if (unlikely (line_range == 0))
9238 advance_pc ((255 - opcode_base) / line_range);
9240 printf (_(" advance address by constant %u to "),
9242 print_dwarf_addr (dwflmod, 0, address, address);
9244 printf (_(", op_index to %u"), op_index);
9249 case DW_LNS_fixed_advance_pc:
9250 /* Takes one 16 bit parameter which is added to the
9252 if (unlikely (standard_opcode_lengths[opcode] != 1
9253 || lineendp - linep < 2))
9256 u128 = read_2ubyte_unaligned_inc (dbg, linep);
9261 advance address by fixed value %u to \n"),
9263 print_dwarf_addr (dwflmod, 0, address, address);
9268 case DW_LNS_set_prologue_end:
9269 /* Takes no argument. */
9270 puts (_(" set prologue end flag"));
9273 case DW_LNS_set_epilogue_begin:
9274 /* Takes no argument. */
9275 puts (_(" set epilogue begin flag"));
9278 case DW_LNS_set_isa:
9279 /* Takes one uleb128 parameter which is stored in isa. */
9280 if (unlikely (standard_opcode_lengths[opcode] != 1
9281 || lineendp - linep < 1))
9284 get_uleb128 (u128, linep, lineendp);
9285 printf (_(" set isa to %u\n"), u128);
9291 /* This is a new opcode the generator but not we know about.
9292 Read the parameters associated with it but then discard
9293 everything. Read all the parameters for this opcode. */
9294 printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
9295 " unknown opcode with %" PRIu8 " parameters:",
9296 standard_opcode_lengths[opcode]),
9297 standard_opcode_lengths[opcode]);
9298 for (int n = standard_opcode_lengths[opcode];
9299 n > 0 && linep < lineendp; --n)
9301 get_uleb128 (u128, linep, lineendp);
9302 if (n != standard_opcode_lengths[opcode])
9303 putc_unlocked (',', stdout);
9304 printf (" %u", u128);
9307 /* Next round, ignore this opcode. */
9313 /* There must only be one data block. */
9314 assert (elf_getdata (scn, data) == NULL);
9319 print_debug_loclists_section (Dwfl_Module *dwflmod,
9321 GElf_Ehdr *ehdr __attribute__ ((unused)),
9322 Elf_Scn *scn, GElf_Shdr *shdr,
9326 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9327 elf_ndxscn (scn), section_name (ebl, shdr),
9328 (uint64_t) shdr->sh_offset);
9330 Elf_Data *data = (dbg->sectiondata[IDX_debug_loclists]
9331 ?: elf_rawdata (scn, NULL));
9332 if (unlikely (data == NULL))
9334 error (0, 0, _("cannot get .debug_loclists content: %s"),
9339 /* For the listptr to get the base address/CU. */
9340 sort_listptr (&known_loclistsptr, "loclistsptr");
9341 size_t listptr_idx = 0;
9343 const unsigned char *readp = data->d_buf;
9344 const unsigned char *const dataend = ((unsigned char *) data->d_buf
9346 while (readp < dataend)
9348 if (unlikely (readp > dataend - 4))
9351 error (0, 0, _("invalid data in section [%zu] '%s'"),
9352 elf_ndxscn (scn), section_name (ebl, shdr));
9356 ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9357 printf (_("Table at Offset 0x%" PRIx64 ":\n\n"),
9360 uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
9361 unsigned int offset_size = 4;
9362 if (unlikely (unit_length == 0xffffffff))
9364 if (unlikely (readp > dataend - 8))
9367 unit_length = read_8ubyte_unaligned_inc (dbg, readp);
9370 printf (_(" Length: %8" PRIu64 "\n"), unit_length);
9372 /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
9373 bytes to complete the header. And this unit cannot go beyond
9374 the section data. */
9375 if (readp > dataend - 8
9377 || unit_length > (uint64_t) (dataend - readp))
9380 const unsigned char *nexthdr = readp + unit_length;
9382 uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
9383 printf (_(" DWARF version: %8" PRIu16 "\n"), version);
9387 error (0, 0, _("Unknown version"));
9391 uint8_t address_size = *readp++;
9392 printf (_(" Address size: %8" PRIu64 "\n"),
9393 (uint64_t) address_size);
9395 if (address_size != 4 && address_size != 8)
9397 error (0, 0, _("unsupported address size"));
9401 uint8_t segment_size = *readp++;
9402 printf (_(" Segment size: %8" PRIu64 "\n"),
9403 (uint64_t) segment_size);
9405 if (segment_size != 0)
9407 error (0, 0, _("unsupported segment size"));
9411 uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
9412 printf (_(" Offset entries: %8" PRIu64 "\n"),
9413 (uint64_t) offset_entry_count);
9415 /* We need the CU that uses this unit to get the initial base address. */
9416 Dwarf_Addr cu_base = 0;
9417 struct Dwarf_CU *cu = NULL;
9418 if (listptr_cu (&known_loclistsptr, &listptr_idx,
9420 (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
9422 || split_dwarf_cu_base (dbg, &cu, &cu_base))
9425 if (dwarf_cu_die (cu, &cudie,
9426 NULL, NULL, NULL, NULL,
9427 NULL, NULL) == NULL)
9428 printf (_(" Unknown CU base: "));
9430 printf (_(" CU [%6" PRIx64 "] base: "),
9431 dwarf_dieoffset (&cudie));
9432 print_dwarf_addr (dwflmod, address_size, cu_base, cu_base);
9436 printf (_(" Not associated with a CU.\n"));
9440 const unsigned char *offset_array_start = readp;
9441 if (offset_entry_count > 0)
9443 uint64_t max_entries = (unit_length - 8) / offset_size;
9444 if (offset_entry_count > max_entries)
9447 _("too many offset entries for unit length"));
9448 offset_entry_count = max_entries;
9451 printf (_(" Offsets starting at 0x%" PRIx64 ":\n"),
9452 (uint64_t) (offset_array_start
9453 - (unsigned char *) data->d_buf));
9454 for (uint32_t idx = 0; idx < offset_entry_count; idx++)
9456 printf (" [%6" PRIu32 "] ", idx);
9457 if (offset_size == 4)
9459 uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
9460 printf ("0x%" PRIx32 "\n", off);
9464 uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
9465 printf ("0x%" PRIx64 "\n", off);
9471 Dwarf_Addr base = cu_base;
9472 bool start_of_list = true;
9473 while (readp < nexthdr)
9475 Dwarf_Off off = (Dwarf_Off) (readp - (unsigned char *) data->d_buf);
9476 if (listptr_attr (&known_loclistsptr, listptr_idx, off,
9477 DW_AT_GNU_locviews))
9479 Dwarf_Off next_off = next_listptr_offset (&known_loclistsptr,
9481 const unsigned char *locp = readp;
9482 const unsigned char *locendp;
9484 || next_off > (size_t) (nexthdr - ((const unsigned char *)
9488 locendp = (const unsigned char *) data->d_buf + next_off;
9490 printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9491 (uint64_t) (readp - (unsigned char *) data->d_buf),
9492 (uint64_t) (readp - offset_array_start));
9494 while (locp < locendp)
9497 get_uleb128 (v1, locp, locendp);
9498 if (locp >= locendp)
9500 printf (_(" <INVALID DATA>\n"));
9503 get_uleb128 (v2, locp, locendp);
9504 printf (" view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9508 readp = (unsigned char *) locendp;
9512 uint8_t kind = *readp++;
9513 uint64_t op1, op2, len;
9516 if (start_of_list && kind == DW_LLE_end_of_list)
9522 printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
9523 (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
9524 (uint64_t) (readp - offset_array_start - 1));
9525 start_of_list = false;
9528 printf (" %s", dwarf_loc_list_encoding_name (kind));
9531 case DW_LLE_end_of_list:
9532 start_of_list = true;
9536 case DW_LLE_base_addressx:
9537 if ((uint64_t) (nexthdr - readp) < 1)
9540 error (0, 0, _("invalid loclists data"));
9543 get_uleb128 (op1, readp, nexthdr);
9544 printf (" %" PRIx64 "\n", op1);
9545 if (! print_unresolved_addresses)
9548 if (get_indexed_addr (cu, op1, &addr) != 0)
9553 print_dwarf_addr (dwflmod, address_size, addr, addr);
9559 case DW_LLE_startx_endx:
9560 if ((uint64_t) (nexthdr - readp) < 1)
9562 get_uleb128 (op1, readp, nexthdr);
9563 if ((uint64_t) (nexthdr - readp) < 1)
9565 get_uleb128 (op2, readp, nexthdr);
9566 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9567 if (! print_unresolved_addresses)
9571 if (get_indexed_addr (cu, op1, &addr1) != 0
9572 || get_indexed_addr (cu, op2, &addr2) != 0)
9574 printf (" ???..\n");
9580 print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9582 print_dwarf_addr (dwflmod, address_size,
9587 if ((uint64_t) (nexthdr - readp) < 1)
9589 get_uleb128 (len, readp, nexthdr);
9590 if ((uint64_t) (nexthdr - readp) < len)
9592 print_ops (dwflmod, dbg, 8, 8, version,
9593 address_size, offset_size, cu, len, readp);
9597 case DW_LLE_startx_length:
9598 if ((uint64_t) (nexthdr - readp) < 1)
9600 get_uleb128 (op1, readp, nexthdr);
9601 if ((uint64_t) (nexthdr - readp) < 1)
9603 get_uleb128 (op2, readp, nexthdr);
9604 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9605 if (! print_unresolved_addresses)
9609 if (get_indexed_addr (cu, op1, &addr1) != 0)
9611 printf (" ???..\n");
9616 addr2 = addr1 + op2;
9618 print_dwarf_addr (dwflmod, address_size, addr1, addr1);
9620 print_dwarf_addr (dwflmod, address_size,
9625 if ((uint64_t) (nexthdr - readp) < 1)
9627 get_uleb128 (len, readp, nexthdr);
9628 if ((uint64_t) (nexthdr - readp) < len)
9630 print_ops (dwflmod, dbg, 8, 8, version,
9631 address_size, offset_size, cu, len, readp);
9635 case DW_LLE_offset_pair:
9636 if ((uint64_t) (nexthdr - readp) < 1)
9638 get_uleb128 (op1, readp, nexthdr);
9639 if ((uint64_t) (nexthdr - readp) < 1)
9641 get_uleb128 (op2, readp, nexthdr);
9642 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9643 if (! print_unresolved_addresses)
9648 print_dwarf_addr (dwflmod, address_size, op1, op1);
9650 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9653 if ((uint64_t) (nexthdr - readp) < 1)
9655 get_uleb128 (len, readp, nexthdr);
9656 if ((uint64_t) (nexthdr - readp) < len)
9658 print_ops (dwflmod, dbg, 8, 8, version,
9659 address_size, offset_size, cu, len, readp);
9663 case DW_LLE_default_location:
9664 if ((uint64_t) (nexthdr - readp) < 1)
9666 get_uleb128 (len, readp, nexthdr);
9667 if ((uint64_t) (nexthdr - readp) < len)
9669 print_ops (dwflmod, dbg, 8, 8, version,
9670 address_size, offset_size, cu, len, readp);
9674 case DW_LLE_base_address:
9675 if (address_size == 4)
9677 if ((uint64_t) (nexthdr - readp) < 4)
9679 op1 = read_4ubyte_unaligned_inc (dbg, readp);
9683 if ((uint64_t) (nexthdr - readp) < 8)
9685 op1 = read_8ubyte_unaligned_inc (dbg, readp);
9688 printf (" 0x%" PRIx64 "\n", base);
9689 if (! print_unresolved_addresses)
9692 print_dwarf_addr (dwflmod, address_size, base, base);
9697 case DW_LLE_start_end:
9698 if (address_size == 4)
9700 if ((uint64_t) (nexthdr - readp) < 8)
9702 op1 = read_4ubyte_unaligned_inc (dbg, readp);
9703 op2 = read_4ubyte_unaligned_inc (dbg, readp);
9707 if ((uint64_t) (nexthdr - readp) < 16)
9709 op1 = read_8ubyte_unaligned_inc (dbg, readp);
9710 op2 = read_8ubyte_unaligned_inc (dbg, readp);
9712 printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
9713 if (! print_unresolved_addresses)
9716 print_dwarf_addr (dwflmod, address_size, op1, op1);
9718 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9721 if ((uint64_t) (nexthdr - readp) < 1)
9723 get_uleb128 (len, readp, nexthdr);
9724 if ((uint64_t) (nexthdr - readp) < len)
9726 print_ops (dwflmod, dbg, 8, 8, version,
9727 address_size, offset_size, cu, len, readp);
9731 case DW_LLE_start_length:
9732 if (address_size == 4)
9734 if ((uint64_t) (nexthdr - readp) < 4)
9736 op1 = read_4ubyte_unaligned_inc (dbg, readp);
9740 if ((uint64_t) (nexthdr - readp) < 8)
9742 op1 = read_8ubyte_unaligned_inc (dbg, readp);
9744 if ((uint64_t) (nexthdr - readp) < 1)
9746 get_uleb128 (op2, readp, nexthdr);
9747 printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
9748 if (! print_unresolved_addresses)
9752 print_dwarf_addr (dwflmod, address_size, op1, op1);
9754 print_dwarf_addr (dwflmod, address_size, op2 - 1, op2);
9757 if ((uint64_t) (nexthdr - readp) < 1)
9759 get_uleb128 (len, readp, nexthdr);
9760 if ((uint64_t) (nexthdr - readp) < len)
9762 print_ops (dwflmod, dbg, 8, 8, version,
9763 address_size, offset_size, cu, len, readp);
9767 case DW_LLE_GNU_view_pair:
9768 if ((uint64_t) (nexthdr - readp) < 1)
9770 get_uleb128 (op1, readp, nexthdr);
9771 if ((uint64_t) (nexthdr - readp) < 1)
9773 get_uleb128 (op2, readp, nexthdr);
9774 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9783 if (readp != nexthdr)
9785 size_t padding = nexthdr - readp;
9786 printf (_(" %zu padding bytes\n\n"), padding);
9794 print_debug_loc_section (Dwfl_Module *dwflmod,
9795 Ebl *ebl, GElf_Ehdr *ehdr,
9796 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9798 Elf_Data *data = (dbg->sectiondata[IDX_debug_loc]
9799 ?: elf_rawdata (scn, NULL));
9801 if (unlikely (data == NULL))
9803 error (0, 0, _("cannot get .debug_loc content: %s"),
9809 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9810 elf_ndxscn (scn), section_name (ebl, shdr),
9811 (uint64_t) shdr->sh_offset);
9813 sort_listptr (&known_locsptr, "loclistptr");
9814 size_t listptr_idx = 0;
9816 uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9817 uint_fast8_t offset_size = 4;
9820 Dwarf_Addr base = 0;
9821 unsigned char *readp = data->d_buf;
9822 unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
9823 Dwarf_CU *last_cu = NULL;
9824 while (readp < endp)
9826 ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9827 Dwarf_CU *cu = last_cu;
9828 unsigned int attr = 0;
9830 if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
9831 &address_size, &offset_size, &base,
9832 &cu, offset, &readp, endp, &attr))
9838 if (dwarf_cu_die (cu, &cudie,
9839 NULL, NULL, NULL, NULL,
9840 NULL, NULL) == NULL)
9841 printf (_("\n Unknown CU base: "));
9843 printf (_("\n CU [%6" PRIx64 "] base: "),
9844 dwarf_dieoffset (&cudie));
9845 print_dwarf_addr (dwflmod, address_size, base, base);
9850 if (attr == DW_AT_GNU_locviews)
9852 Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
9853 &listptr_idx, offset);
9854 const unsigned char *locp = readp;
9855 const unsigned char *locendp;
9857 || next_off > (size_t) (endp
9858 - (const unsigned char *) data->d_buf))
9861 locendp = (const unsigned char *) data->d_buf + next_off;
9863 while (locp < locendp)
9866 get_uleb128 (v1, locp, locendp);
9867 if (locp >= locendp)
9869 printf (_(" [%6tx] <INVALID DATA>\n"), offset);
9872 get_uleb128 (v2, locp, locendp);
9873 if (first) /* First view pair in a list. */
9874 printf (" [%6tx] ", offset);
9877 printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9882 readp = (unsigned char *) locendp;
9886 /* GNU DebugFission encoded addresses as addrx. */
9887 bool is_debugfission = ((cu != NULL
9888 || split_dwarf_cu_base (dbg, &cu, &base))
9890 && cu->unit_type == DW_UT_split_compile));
9891 if (!is_debugfission
9892 && unlikely (data->d_size - offset < (size_t) address_size * 2))
9895 printf (_(" [%6tx] <INVALID DATA>\n"), offset);
9901 bool use_base = true;
9902 if (is_debugfission)
9904 const unsigned char *locp = readp;
9905 const unsigned char *locendp = readp + data->d_size;
9906 if (locp >= locendp)
9910 unsigned char code = *locp++;
9913 case DW_LLE_GNU_end_of_list_entry:
9918 case DW_LLE_GNU_base_address_selection_entry:
9919 if (locp >= locendp)
9921 begin = (Dwarf_Addr) -1;
9922 get_uleb128 (idx, locp, locendp);
9923 if (get_indexed_addr (cu, idx, &end) != 0)
9924 end = idx; /* ... */
9927 case DW_LLE_GNU_start_end_entry:
9928 if (locp >= locendp)
9930 get_uleb128 (idx, locp, locendp);
9931 if (get_indexed_addr (cu, idx, &begin) != 0)
9932 begin = idx; /* ... */
9933 if (locp >= locendp)
9935 get_uleb128 (idx, locp, locendp);
9936 if (get_indexed_addr (cu, idx, &end) != 0)
9937 end = idx; /* ... */
9941 case DW_LLE_GNU_start_length_entry:
9942 if (locp >= locendp)
9944 get_uleb128 (idx, locp, locendp);
9945 if (get_indexed_addr (cu, idx, &begin) != 0)
9946 begin = idx; /* ... */
9947 if (locendp - locp < 4)
9949 end = read_4ubyte_unaligned_inc (dbg, locp);
9958 readp = (unsigned char *) locp;
9960 else if (address_size == 8)
9962 begin = read_8ubyte_unaligned_inc (dbg, readp);
9963 end = read_8ubyte_unaligned_inc (dbg, readp);
9967 begin = read_4ubyte_unaligned_inc (dbg, readp);
9968 end = read_4ubyte_unaligned_inc (dbg, readp);
9969 if (begin == (Dwarf_Addr) (uint32_t) -1)
9970 begin = (Dwarf_Addr) -1l;
9973 if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
9976 printf (" [%6tx] ", offset);
9979 puts (_("base address"));
9981 print_dwarf_addr (dwflmod, address_size, end, end);
9986 else if (begin == 0 && end == 0) /* End of list entry. */
9989 printf (_(" [%6tx] empty list\n"), offset);
9994 /* We have a location expression entry. */
9995 uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
9997 if (first) /* First entry in a list. */
9998 printf (" [%6tx] ", offset);
10002 printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
10003 if (! print_unresolved_addresses)
10005 Dwarf_Addr dab = use_base ? base + begin : begin;
10006 Dwarf_Addr dae = use_base ? base + end : end;
10008 print_dwarf_addr (dwflmod, address_size, dab, dab);
10010 print_dwarf_addr (dwflmod, address_size, dae - 1, dae);
10014 if (endp - readp <= (ptrdiff_t) len)
10016 fputs (_(" <INVALID DATA>\n"), stdout);
10020 print_ops (dwflmod, dbg, 11, 11,
10021 cu != NULL ? cu->version : 3,
10022 address_size, offset_size, cu, len, readp);
10034 Dwarf_Files *files;
10035 struct mac_culist *next;
10040 mac_compare (const void *p1, const void *p2)
10042 struct mac_culist *m1 = (struct mac_culist *) p1;
10043 struct mac_culist *m2 = (struct mac_culist *) p2;
10045 if (m1->offset < m2->offset)
10047 if (m1->offset > m2->offset)
10054 print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10056 GElf_Ehdr *ehdr __attribute__ ((unused)),
10057 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10060 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10061 elf_ndxscn (scn), section_name (ebl, shdr),
10062 (uint64_t) shdr->sh_offset);
10063 putc_unlocked ('\n', stdout);
10065 /* There is no function in libdw to iterate over the raw content of
10066 the section but it is easy enough to do. */
10067 Elf_Data *data = (dbg->sectiondata[IDX_debug_macinfo]
10068 ?: elf_rawdata (scn, NULL));
10069 if (unlikely (data == NULL))
10071 error (0, 0, _("cannot get macro information section data: %s"),
10076 /* Get the source file information for all CUs. */
10080 struct mac_culist *culist = NULL;
10081 size_t nculist = 0;
10082 while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10085 if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10088 Dwarf_Attribute attr;
10089 if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
10093 if (dwarf_formudata (&attr, &macoff) != 0)
10096 struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10098 newp->offset = macoff;
10099 newp->files = NULL;
10100 newp->next = culist;
10105 /* Convert the list into an array for easier consumption. */
10106 struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
10108 /* Add sentinel. */
10109 cus[nculist].offset = data->d_size;
10110 cus[nculist].files = (Dwarf_Files *) -1l;
10113 for (size_t cnt = nculist - 1; culist != NULL; --cnt)
10115 assert (cnt < nculist);
10116 cus[cnt] = *culist;
10117 culist = culist->next;
10120 /* Sort the array according to the offset in the .debug_macinfo
10121 section. Note we keep the sentinel at the end. */
10122 qsort (cus, nculist, sizeof (*cus), mac_compare);
10125 const unsigned char *readp = (const unsigned char *) data->d_buf;
10126 const unsigned char *readendp = readp + data->d_size;
10129 while (readp < readendp)
10131 unsigned int opcode = *readp++;
10133 unsigned int u128_2;
10134 const unsigned char *endp;
10138 case DW_MACINFO_define:
10139 case DW_MACINFO_undef:
10140 case DW_MACINFO_vendor_ext:
10141 /* For the first two opcodes the parameters are
10145 We can treat these cases together. */
10146 get_uleb128 (u128, readp, readendp);
10148 endp = memchr (readp, '\0', readendp - readp);
10149 if (unlikely (endp == NULL))
10152 %*s*** non-terminated string at end of section"),
10157 if (opcode == DW_MACINFO_define)
10158 printf ("%*s#define %s, line %u\n",
10159 level, "", (char *) readp, u128);
10160 else if (opcode == DW_MACINFO_undef)
10161 printf ("%*s#undef %s, line %u\n",
10162 level, "", (char *) readp, u128);
10164 printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
10169 case DW_MACINFO_start_file:
10170 /* The two parameters are line and file index, in this order. */
10171 get_uleb128 (u128, readp, readendp);
10172 if (readendp - readp < 1)
10175 %*s*** missing DW_MACINFO_start_file argument at end of section"),
10179 get_uleb128 (u128_2, readp, readendp);
10181 /* Find the CU DIE for this file. */
10182 size_t macoff = readp - (const unsigned char *) data->d_buf;
10183 const char *fname = "???";
10184 if (macoff >= cus[0].offset && cus[0].offset != data->d_size)
10186 while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
10189 if (cus[0].files == NULL
10190 && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
10191 cus[0].files = (Dwarf_Files *) -1l;
10193 if (cus[0].files != (Dwarf_Files *) -1l)
10194 fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
10198 printf ("%*sstart_file %u, [%u] %s\n",
10199 level, "", u128, u128_2, fname);
10203 case DW_MACINFO_end_file:
10205 printf ("%*send_file\n", level, "");
10206 /* Nothing more to do. */
10210 // XXX gcc seems to generate files with a trailing zero.
10211 if (unlikely (opcode != 0 || readp != readendp))
10212 printf ("%*s*** invalid opcode %u\n", level, "", opcode);
10220 print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10222 GElf_Ehdr *ehdr __attribute__ ((unused)),
10223 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10226 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10227 elf_ndxscn (scn), section_name (ebl, shdr),
10228 (uint64_t) shdr->sh_offset);
10229 putc_unlocked ('\n', stdout);
10231 Elf_Data *data = elf_getdata (scn, NULL);
10232 if (unlikely (data == NULL))
10234 error (0, 0, _("cannot get macro information section data: %s"),
10239 /* Get the source file information for all CUs. Uses same
10240 datastructure as macinfo. But uses offset field to directly
10241 match .debug_line offset. And just stored in a list. */
10245 struct mac_culist *culist = NULL;
10246 size_t nculist = 0;
10247 while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
10250 if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
10253 Dwarf_Attribute attr;
10254 if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
10257 Dwarf_Word lineoff;
10258 if (dwarf_formudata (&attr, &lineoff) != 0)
10261 struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
10263 newp->offset = lineoff;
10264 newp->files = NULL;
10265 newp->next = culist;
10270 const unsigned char *readp = (const unsigned char *) data->d_buf;
10271 const unsigned char *readendp = readp + data->d_size;
10273 while (readp < readendp)
10275 printf (_(" Offset: 0x%" PRIx64 "\n"),
10276 (uint64_t) (readp - (const unsigned char *) data->d_buf));
10278 // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
10279 // optional vendor extension macro entry table.
10280 if (readp + 2 > readendp)
10283 error (0, 0, _("invalid data"));
10286 const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
10287 printf (_(" Version: %" PRIu16 "\n"), vers);
10289 // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
10290 // 5 when it gets standardized.
10291 if (vers != 4 && vers != 5)
10293 printf (_(" unknown version, cannot parse section\n"));
10297 if (readp + 1 > readendp)
10299 const unsigned char flag = *readp++;
10300 printf (_(" Flag: 0x%" PRIx8), flag);
10304 if ((flag & 0x01) != 0)
10306 printf ("offset_size");
10307 if ((flag & 0xFE) != 0)
10310 if ((flag & 0x02) != 0)
10312 printf ("debug_line_offset");
10313 if ((flag & 0xFC) != 0)
10316 if ((flag & 0x04) != 0)
10318 printf ("operands_table");
10319 if ((flag & 0xF8) != 0)
10322 if ((flag & 0xF8) != 0)
10323 printf ("unknown");
10328 unsigned int offset_len = (flag & 0x01) ? 8 : 4;
10329 printf (_(" Offset length: %" PRIu8 "\n"), offset_len);
10330 Dwarf_Off line_offset = -1;
10333 if (offset_len == 8)
10334 line_offset = read_8ubyte_unaligned_inc (dbg, readp);
10336 line_offset = read_4ubyte_unaligned_inc (dbg, readp);
10337 printf (_(" .debug_line offset: 0x%" PRIx64 "\n"),
10341 struct mac_culist *cu = NULL;
10342 if (line_offset != (Dwarf_Off) -1)
10345 while (cu != NULL && line_offset != cu->offset)
10349 Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
10353 const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user + 1];
10354 memset (vendor, 0, sizeof vendor);
10357 // 1 byte length, for each item, 1 byte opcode, uleb128 number
10358 // of arguments, for each argument 1 byte form code.
10359 if (readp + 1 > readendp)
10361 unsigned int tlen = *readp++;
10362 printf (_(" extension opcode table, %" PRIu8 " items:\n"),
10364 for (unsigned int i = 0; i < tlen; i++)
10366 if (readp + 1 > readendp)
10368 unsigned int opcode = *readp++;
10369 printf (_(" [%" PRIx8 "]"), opcode);
10370 if (opcode < DW_MACRO_lo_user
10371 || opcode > DW_MACRO_hi_user)
10373 // Record the start of description for this vendor opcode.
10374 // uleb128 nr args, 1 byte per arg form.
10375 vendor[opcode - DW_MACRO_lo_user] = readp;
10376 if (readp + 1 > readendp)
10378 unsigned int args = *readp++;
10381 printf (_(" %" PRIu8 " arguments:"), args);
10384 if (readp + 1 > readendp)
10386 unsigned int form = *readp++;
10387 printf (" %s", dwarf_form_name (form));
10388 if (! libdw_valid_user_form (form))
10392 putchar_unlocked (',');
10396 printf (_(" no arguments."));
10397 putchar_unlocked ('\n');
10400 putchar_unlocked ('\n');
10403 if (readp + 1 > readendp)
10405 unsigned int opcode = *readp++;
10406 while (opcode != 0)
10409 unsigned int u128_2;
10410 const unsigned char *endp;
10415 case DW_MACRO_start_file:
10416 get_uleb128 (u128, readp, readendp);
10417 if (readp >= readendp)
10419 get_uleb128 (u128_2, readp, readendp);
10421 /* Find the CU DIE that matches this line offset. */
10422 const char *fname = "???";
10425 if (cu->files == NULL
10426 && dwarf_getsrcfiles (&cu->die, &cu->files,
10428 cu->files = (Dwarf_Files *) -1l;
10430 if (cu->files != (Dwarf_Files *) -1l)
10431 fname = (dwarf_filesrc (cu->files, u128_2,
10432 NULL, NULL) ?: "???");
10434 printf ("%*sstart_file %u, [%u] %s\n",
10435 level, "", u128, u128_2, fname);
10439 case DW_MACRO_end_file:
10441 printf ("%*send_file\n", level, "");
10444 case DW_MACRO_define:
10445 get_uleb128 (u128, readp, readendp);
10446 endp = memchr (readp, '\0', readendp - readp);
10449 printf ("%*s#define %s, line %u\n",
10450 level, "", readp, u128);
10454 case DW_MACRO_undef:
10455 get_uleb128 (u128, readp, readendp);
10456 endp = memchr (readp, '\0', readendp - readp);
10459 printf ("%*s#undef %s, line %u\n",
10460 level, "", readp, u128);
10464 case DW_MACRO_define_strp:
10465 get_uleb128 (u128, readp, readendp);
10466 if (readp + offset_len > readendp)
10468 if (offset_len == 8)
10469 off = read_8ubyte_unaligned_inc (dbg, readp);
10471 off = read_4ubyte_unaligned_inc (dbg, readp);
10472 printf ("%*s#define %s, line %u (indirect)\n",
10473 level, "", dwarf_getstring (dbg, off, NULL), u128);
10476 case DW_MACRO_undef_strp:
10477 get_uleb128 (u128, readp, readendp);
10478 if (readp + offset_len > readendp)
10480 if (offset_len == 8)
10481 off = read_8ubyte_unaligned_inc (dbg, readp);
10483 off = read_4ubyte_unaligned_inc (dbg, readp);
10484 printf ("%*s#undef %s, line %u (indirect)\n",
10485 level, "", dwarf_getstring (dbg, off, NULL), u128);
10488 case DW_MACRO_import:
10489 if (readp + offset_len > readendp)
10491 if (offset_len == 8)
10492 off = read_8ubyte_unaligned_inc (dbg, readp);
10494 off = read_4ubyte_unaligned_inc (dbg, readp);
10495 printf ("%*s#include offset 0x%" PRIx64 "\n",
10499 case DW_MACRO_define_sup:
10500 get_uleb128 (u128, readp, readendp);
10501 if (readp + offset_len > readendp)
10503 printf ("%*s#define ", level, "");
10504 readp = print_form_data (dbg, DW_FORM_strp_sup,
10505 readp, readendp, offset_len,
10507 printf (", line %u (sup)\n", u128);
10510 case DW_MACRO_undef_sup:
10511 get_uleb128 (u128, readp, readendp);
10512 if (readp + offset_len > readendp)
10514 printf ("%*s#undef ", level, "");
10515 readp = print_form_data (dbg, DW_FORM_strp_sup,
10516 readp, readendp, offset_len,
10518 printf (", line %u (sup)\n", u128);
10521 case DW_MACRO_import_sup:
10522 if (readp + offset_len > readendp)
10524 if (offset_len == 8)
10525 off = read_8ubyte_unaligned_inc (dbg, readp);
10527 off = read_4ubyte_unaligned_inc (dbg, readp);
10528 // XXX Needs support for reading from supplementary object file.
10529 printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
10533 case DW_MACRO_define_strx:
10534 get_uleb128 (u128, readp, readendp);
10535 if (readp + offset_len > readendp)
10537 printf ("%*s#define ", level, "");
10538 readp = print_form_data (dbg, DW_FORM_strx,
10539 readp, readendp, offset_len,
10541 printf (", line %u (strx)\n", u128);
10544 case DW_MACRO_undef_strx:
10545 get_uleb128 (u128, readp, readendp);
10546 if (readp + offset_len > readendp)
10548 printf ("%*s#undef ", level, "");
10549 readp = print_form_data (dbg, DW_FORM_strx,
10550 readp, readendp, offset_len,
10552 printf (", line %u (strx)\n", u128);
10556 printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
10557 if (opcode < DW_MACRO_lo_user
10558 || opcode > DW_MACRO_lo_user
10559 || vendor[opcode - DW_MACRO_lo_user] == NULL)
10562 const unsigned char *op_desc;
10563 op_desc = vendor[opcode - DW_MACRO_lo_user];
10565 // Just skip the arguments, we cannot really interpret them,
10566 // but print as much as we can.
10567 unsigned int args = *op_desc++;
10568 while (args > 0 && readp < readendp)
10570 unsigned int form = *op_desc++;
10571 readp = print_form_data (dbg, form, readp, readendp,
10572 offset_len, str_offsets_base);
10577 putchar_unlocked ('\n');
10580 if (readp + 1 > readendp)
10584 putchar_unlocked ('\n');
10590 /* Callback for printing global names. */
10592 print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
10595 int *np = (int *) arg;
10597 printf (_(" [%5d] DIE offset: %6" PRId64
10598 ", CU DIE offset: %6" PRId64 ", name: %s\n"),
10599 (*np)++, global->die_offset, global->cu_offset, global->name);
10605 /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
10607 print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10609 GElf_Ehdr *ehdr __attribute__ ((unused)),
10610 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10612 printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10613 elf_ndxscn (scn), section_name (ebl, shdr),
10614 (uint64_t) shdr->sh_offset);
10617 (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
10620 /* Print the content of the DWARF string section '.debug_str'. */
10622 print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10624 GElf_Ehdr *ehdr __attribute__ ((unused)),
10625 Elf_Scn *scn, GElf_Shdr *shdr,
10626 Dwarf *dbg __attribute__ ((unused)))
10628 Elf_Data *data = elf_rawdata (scn, NULL);
10629 const size_t sh_size = data ? data->d_size : 0;
10631 /* Compute floor(log16(shdr->sh_size)). */
10632 GElf_Addr tmp = sh_size;
10639 digits = MAX (4, digits);
10641 printf (_("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
10644 section_name (ebl, shdr), (uint64_t) shdr->sh_offset,
10645 /* TRANS: the debugstr| prefix makes the string unique. */
10646 digits + 2, sgettext ("debugstr|Offset"));
10648 Dwarf_Off offset = 0;
10649 while (offset < sh_size)
10652 const char *str = (const char *) data->d_buf + offset;
10653 const char *endp = memchr (str, '\0', sh_size - offset);
10654 if (unlikely (endp == NULL))
10656 printf (_(" *** error, missing string terminator\n"));
10660 printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
10667 print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10669 GElf_Ehdr *ehdr __attribute__ ((unused)),
10670 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10673 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10674 elf_ndxscn (scn), section_name (ebl, shdr),
10675 (uint64_t) shdr->sh_offset);
10677 if (shdr->sh_size == 0)
10680 /* We like to get the section from libdw to make sure they are relocated. */
10681 Elf_Data *data = (dbg->sectiondata[IDX_debug_str_offsets]
10682 ?: elf_rawdata (scn, NULL));
10683 if (unlikely (data == NULL))
10685 error (0, 0, _("cannot get .debug_str_offsets section data: %s"),
10691 sort_listptr (&known_stroffbases, "str_offsets");
10693 const unsigned char *start = (const unsigned char *) data->d_buf;
10694 const unsigned char *readp = start;
10695 const unsigned char *readendp = ((const unsigned char *) data->d_buf
10698 while (readp < readendp)
10700 /* Most string offset tables will have a header. For split
10701 dwarf unit GNU DebugFission didn't add one. But they were
10702 also only defined for split units (main or skeleton units
10703 didn't have indirect strings). So if we don't have a
10704 DW_AT_str_offsets_base at all and this is offset zero, then
10705 just start printing offsets immediately, if this is a .dwo
10707 Dwarf_Off off = (Dwarf_Off) (readp
10708 - (const unsigned char *) data->d_buf);
10710 printf ("Table at offset %" PRIx64 " ", off);
10712 struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
10713 const unsigned char *next_unitp = readendp;
10714 uint8_t offset_size;
10716 if (listptr == NULL)
10718 /* This can happen for .dwo files. There is only an header
10719 in the case this is a version 5 split DWARF file. */
10722 if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
10725 error (0, 0, "Warning: Cannot find any DWARF unit.");
10726 /* Just guess some values. */
10727 has_header = false;
10731 && (unit_type == DW_UT_split_type
10732 || unit_type == DW_UT_split_compile))
10734 has_header = cu->version > 4;
10735 offset_size = cu->offset_size;
10740 "Warning: No CU references .debug_str_offsets after %"
10742 has_header = cu->version > 4;
10743 offset_size = cu->offset_size;
10749 /* This must be DWARF5, since GNU DebugFission didn't define
10750 DW_AT_str_offsets_base. */
10754 if (dwarf_cu_die (listptr->cu, &cudie,
10755 NULL, NULL, NULL, NULL,
10756 NULL, NULL) == NULL)
10757 printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
10759 printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
10764 uint64_t unit_length;
10768 unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10769 if (unlikely (unit_length == 0xffffffff))
10771 if (unlikely (readp > readendp - 8))
10774 error (0, 0, "Invalid data");
10777 unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10784 printf (_(" Length: %8" PRIu64 "\n"),
10786 printf (_(" Offset size: %8" PRIu8 "\n"),
10789 /* We need at least 2-bytes (version) + 2-bytes (padding) =
10790 4 bytes to complete the header. And this unit cannot go
10791 beyond the section data. */
10792 if (readp > readendp - 4
10794 || unit_length > (uint64_t) (readendp - readp))
10797 next_unitp = readp + unit_length;
10799 version = read_2ubyte_unaligned_inc (dbg, readp);
10800 printf (_(" DWARF version: %8" PRIu16 "\n"), version);
10804 error (0, 0, _("Unknown version"));
10808 padding = read_2ubyte_unaligned_inc (dbg, readp);
10809 printf (_(" Padding: %8" PRIx16 "\n"), padding);
10811 if (listptr != NULL
10812 && listptr->offset != (Dwarf_Off) (readp - start))
10814 error (0, 0, "String offsets index doesn't start after header");
10822 size_t offsets = (next_unitp - readp) / offset_size;
10823 while (offsets >= 10)
10829 unsigned int uidx = 0;
10830 size_t index_offset = readp - (const unsigned char *) data->d_buf;
10831 printf (" Offsets start at 0x%zx:\n", index_offset);
10832 while (readp <= next_unitp - offset_size)
10835 if (offset_size == 4)
10836 offset = read_4ubyte_unaligned_inc (dbg, readp);
10838 offset = read_8ubyte_unaligned_inc (dbg, readp);
10839 const char *str = dwarf_getstring (dbg, offset, NULL);
10840 printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
10841 digits, uidx++, (int) offset_size * 2, offset, str ?: "???");
10845 if (readp != next_unitp)
10846 error (0, 0, "extra %zd bytes at end of unit",
10847 (size_t) (next_unitp - readp));
10850 readp = next_unitp;
10855 /* Print the content of the call frame search table section
10856 '.eh_frame_hdr'. */
10858 print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10859 Ebl *ebl __attribute__ ((unused)),
10860 GElf_Ehdr *ehdr __attribute__ ((unused)),
10861 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10864 \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
10867 Elf_Data *data = elf_rawdata (scn, NULL);
10869 if (unlikely (data == NULL))
10871 error (0, 0, _("cannot get %s content: %s"),
10872 ".eh_frame_hdr", elf_errmsg (-1));
10876 const unsigned char *readp = data->d_buf;
10877 const unsigned char *const dataend = ((unsigned char *) data->d_buf
10880 if (unlikely (readp + 4 > dataend))
10883 error (0, 0, _("invalid data"));
10887 unsigned int version = *readp++;
10888 unsigned int eh_frame_ptr_enc = *readp++;
10889 unsigned int fde_count_enc = *readp++;
10890 unsigned int table_enc = *readp++;
10892 printf (" version: %u\n"
10893 " eh_frame_ptr_enc: %#x ",
10894 version, eh_frame_ptr_enc);
10895 print_encoding_base ("", eh_frame_ptr_enc);
10896 printf (" fde_count_enc: %#x ", fde_count_enc);
10897 print_encoding_base ("", fde_count_enc);
10898 printf (" table_enc: %#x ", table_enc);
10899 print_encoding_base ("", table_enc);
10901 uint64_t eh_frame_ptr = 0;
10902 if (eh_frame_ptr_enc != DW_EH_PE_omit)
10904 readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
10906 if (unlikely (readp == NULL))
10909 printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
10910 if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
10911 printf (" (offset: %#" PRIx64 ")",
10912 /* +4 because of the 4 byte header of the section. */
10913 (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
10915 putchar_unlocked ('\n');
10918 uint64_t fde_count = 0;
10919 if (fde_count_enc != DW_EH_PE_omit)
10921 readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
10922 if (unlikely (readp == NULL))
10925 printf (" fde_count: %" PRIu64 "\n", fde_count);
10928 if (fde_count == 0 || table_enc == DW_EH_PE_omit)
10933 /* Optimize for the most common case. */
10934 if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
10935 while (fde_count > 0 && readp + 8 <= dataend)
10937 int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
10938 uint64_t initial_offset = ((uint64_t) shdr->sh_offset
10939 + (int64_t) initial_location);
10940 int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
10941 // XXX Possibly print symbol name or section offset for initial_offset
10942 printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
10943 " fde=[%6" PRIx64 "]\n",
10944 initial_location, initial_offset,
10945 address, address - (eh_frame_ptr + 4));
10948 while (0 && readp < dataend)
10955 /* Print the content of the exception handling table section
10956 '.eh_frame_hdr'. */
10958 print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
10959 Ebl *ebl __attribute__ ((unused)),
10960 GElf_Ehdr *ehdr __attribute__ ((unused)),
10962 GElf_Shdr *shdr __attribute__ ((unused)),
10963 Dwarf *dbg __attribute__ ((unused)))
10966 \nException handling table section [%2zu] '.gcc_except_table':\n"),
10969 Elf_Data *data = elf_rawdata (scn, NULL);
10971 if (unlikely (data == NULL))
10973 error (0, 0, _("cannot get %s content: %s"),
10974 ".gcc_except_table", elf_errmsg (-1));
10978 const unsigned char *readp = data->d_buf;
10979 const unsigned char *const dataend = readp + data->d_size;
10981 if (unlikely (readp + 1 > dataend))
10984 error (0, 0, _("invalid data"));
10987 unsigned int lpstart_encoding = *readp++;
10988 printf (_(" LPStart encoding: %#x "), lpstart_encoding);
10989 print_encoding_base ("", lpstart_encoding);
10990 if (lpstart_encoding != DW_EH_PE_omit)
10993 readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
10994 printf (" LPStart: %#" PRIx64 "\n", lpstart);
10997 if (unlikely (readp + 1 > dataend))
10999 unsigned int ttype_encoding = *readp++;
11000 printf (_(" TType encoding: %#x "), ttype_encoding);
11001 print_encoding_base ("", ttype_encoding);
11002 const unsigned char *ttype_base = NULL;
11003 if (ttype_encoding != DW_EH_PE_omit)
11005 unsigned int ttype_base_offset;
11006 get_uleb128 (ttype_base_offset, readp, dataend);
11007 printf (" TType base offset: %#x\n", ttype_base_offset);
11008 if ((size_t) (dataend - readp) > ttype_base_offset)
11009 ttype_base = readp + ttype_base_offset;
11012 if (unlikely (readp + 1 > dataend))
11014 unsigned int call_site_encoding = *readp++;
11015 printf (_(" Call site encoding: %#x "), call_site_encoding);
11016 print_encoding_base ("", call_site_encoding);
11017 unsigned int call_site_table_len;
11018 get_uleb128 (call_site_table_len, readp, dataend);
11020 const unsigned char *const action_table = readp + call_site_table_len;
11021 if (unlikely (action_table > dataend))
11023 unsigned int u = 0;
11024 unsigned int max_action = 0;
11025 while (readp < action_table)
11028 puts (_("\n Call site table:"));
11030 uint64_t call_site_start;
11031 readp = read_encoded (call_site_encoding, readp, dataend,
11032 &call_site_start, dbg);
11033 uint64_t call_site_length;
11034 readp = read_encoded (call_site_encoding, readp, dataend,
11035 &call_site_length, dbg);
11036 uint64_t landing_pad;
11037 readp = read_encoded (call_site_encoding, readp, dataend,
11038 &landing_pad, dbg);
11039 unsigned int action;
11040 get_uleb128 (action, readp, dataend);
11041 max_action = MAX (action, max_action);
11042 printf (_(" [%4u] Call site start: %#" PRIx64 "\n"
11043 " Call site length: %" PRIu64 "\n"
11044 " Landing pad: %#" PRIx64 "\n"
11046 u++, call_site_start, call_site_length, landing_pad, action);
11048 if (readp != action_table)
11051 unsigned int max_ar_filter = 0;
11052 if (max_action > 0)
11054 puts ("\n Action table:");
11056 size_t maxdata = (size_t) (dataend - action_table);
11057 if (max_action > maxdata || maxdata - max_action < 1)
11059 invalid_action_table:
11060 fputs (_(" <INVALID DATA>\n"), stdout);
11064 const unsigned char *const action_table_end
11065 = action_table + max_action + 1;
11071 get_sleb128 (ar_filter, readp, action_table_end);
11072 if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
11073 max_ar_filter = ar_filter;
11075 if (readp >= action_table_end)
11076 goto invalid_action_table;
11077 get_sleb128 (ar_disp, readp, action_table_end);
11079 printf (" [%4u] ar_filter: % d\n"
11081 u, ar_filter, ar_disp);
11082 if (abs (ar_disp) & 1)
11083 printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
11084 else if (ar_disp != 0)
11087 putchar_unlocked ('\n');
11090 while (readp < action_table_end);
11093 if (max_ar_filter > 0 && ttype_base != NULL)
11095 unsigned char dsize;
11096 puts ("\n TType table:");
11098 // XXX Not *4, size of encoding;
11099 switch (ttype_encoding & 7)
11101 case DW_EH_PE_udata2:
11102 case DW_EH_PE_sdata2:
11105 case DW_EH_PE_udata4:
11106 case DW_EH_PE_sdata4:
11109 case DW_EH_PE_udata8:
11110 case DW_EH_PE_sdata8:
11115 error (1, 0, _("invalid TType encoding"));
11119 > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
11122 readp = ttype_base - max_ar_filter * dsize;
11126 readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
11128 printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
11130 while (readp < ttype_base);
11134 /* Print the content of the '.gdb_index' section.
11135 http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
11138 print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl,
11139 GElf_Ehdr *ehdr __attribute__ ((unused)),
11140 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
11142 printf (_("\nGDB section [%2zu] '%s' at offset %#" PRIx64
11143 " contains %" PRId64 " bytes :\n"),
11144 elf_ndxscn (scn), section_name (ebl, shdr),
11145 (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
11147 Elf_Data *data = elf_rawdata (scn, NULL);
11149 if (unlikely (data == NULL))
11151 error (0, 0, _("cannot get %s content: %s"),
11152 ".gdb_index", elf_errmsg (-1));
11156 // .gdb_index is always in little endian.
11157 Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
11160 const unsigned char *readp = data->d_buf;
11161 const unsigned char *const dataend = readp + data->d_size;
11163 if (unlikely (readp + 4 > dataend))
11166 error (0, 0, _("invalid data"));
11170 int32_t vers = read_4ubyte_unaligned (dbg, readp);
11171 printf (_(" Version: %" PRId32 "\n"), vers);
11173 // The only difference between version 4 and version 5 is the
11174 // hash used for generating the table. Version 6 contains symbols
11175 // for inlined functions, older versions didn't. Version 7 adds
11176 // symbol kinds. Version 8 just indicates that it correctly includes
11177 // TUs for symbols.
11178 if (vers < 4 || vers > 8)
11180 printf (_(" unknown version, cannot parse section\n"));
11185 if (unlikely (readp + 4 > dataend))
11188 uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
11189 printf (_(" CU offset: %#" PRIx32 "\n"), cu_off);
11192 if (unlikely (readp + 4 > dataend))
11195 uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
11196 printf (_(" TU offset: %#" PRIx32 "\n"), tu_off);
11199 if (unlikely (readp + 4 > dataend))
11202 uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
11203 printf (_(" address offset: %#" PRIx32 "\n"), addr_off);
11206 if (unlikely (readp + 4 > dataend))
11209 uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
11210 printf (_(" symbol offset: %#" PRIx32 "\n"), sym_off);
11213 if (unlikely (readp + 4 > dataend))
11216 uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
11217 printf (_(" constant offset: %#" PRIx32 "\n"), const_off);
11219 if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
11223 readp = data->d_buf + cu_off;
11225 const unsigned char *nextp = data->d_buf + tu_off;
11226 if (tu_off >= data->d_size)
11229 size_t cu_nr = (nextp - readp) / 16;
11231 printf (_("\n CU list at offset %#" PRIx32
11232 " contains %zu entries:\n"),
11236 while (dataend - readp >= 16 && n < cu_nr)
11238 uint64_t off = read_8ubyte_unaligned (dbg, readp);
11241 uint64_t len = read_8ubyte_unaligned (dbg, readp);
11244 printf (" [%4zu] start: %0#8" PRIx64
11245 ", length: %5" PRIu64 "\n", n, off, len);
11249 readp = data->d_buf + tu_off;
11250 nextp = data->d_buf + addr_off;
11251 if (addr_off >= data->d_size)
11254 size_t tu_nr = (nextp - readp) / 24;
11256 printf (_("\n TU list at offset %#" PRIx32
11257 " contains %zu entries:\n"),
11261 while (dataend - readp >= 24 && n < tu_nr)
11263 uint64_t off = read_8ubyte_unaligned (dbg, readp);
11266 uint64_t type = read_8ubyte_unaligned (dbg, readp);
11269 uint64_t sig = read_8ubyte_unaligned (dbg, readp);
11272 printf (" [%4zu] CU offset: %5" PRId64
11273 ", type offset: %5" PRId64
11274 ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
11278 readp = data->d_buf + addr_off;
11279 nextp = data->d_buf + sym_off;
11280 if (sym_off >= data->d_size)
11283 size_t addr_nr = (nextp - readp) / 20;
11285 printf (_("\n Address list at offset %#" PRIx32
11286 " contains %zu entries:\n"),
11287 addr_off, addr_nr);
11290 while (dataend - readp >= 20 && n < addr_nr)
11292 uint64_t low = read_8ubyte_unaligned (dbg, readp);
11295 uint64_t high = read_8ubyte_unaligned (dbg, readp);
11298 uint32_t idx = read_4ubyte_unaligned (dbg, readp);
11301 printf (" [%4zu] ", n);
11302 print_dwarf_addr (dwflmod, 8, low, low);
11304 print_dwarf_addr (dwflmod, 8, high - 1, high);
11305 printf (", CU index: %5" PRId32 "\n", idx);
11309 const unsigned char *const_start = data->d_buf + const_off;
11310 if (const_off >= data->d_size)
11313 readp = data->d_buf + sym_off;
11314 nextp = const_start;
11315 size_t sym_nr = (nextp - readp) / 8;
11317 printf (_("\n Symbol table at offset %#" PRIx32
11318 " contains %zu slots:\n"),
11322 while (dataend - readp >= 8 && n < sym_nr)
11324 uint32_t name = read_4ubyte_unaligned (dbg, readp);
11327 uint32_t vector = read_4ubyte_unaligned (dbg, readp);
11330 if (name != 0 || vector != 0)
11332 const unsigned char *sym = const_start + name;
11333 if (unlikely ((size_t) (dataend - const_start) < name
11334 || memchr (sym, '\0', dataend - sym) == NULL))
11337 printf (" [%4zu] symbol: %s, CUs: ", n, sym);
11339 const unsigned char *readcus = const_start + vector;
11340 if (unlikely ((size_t) (dataend - const_start) < vector))
11342 uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
11345 uint32_t cu_kind, cu, kind;
11348 if (unlikely (readcus + 4 > dataend))
11350 cu_kind = read_4ubyte_unaligned (dbg, readcus);
11351 cu = cu_kind & ((1 << 24) - 1);
11352 kind = (cu_kind >> 28) & 7;
11353 is_static = cu_kind & (1U << 31);
11354 if (cu > cu_nr - 1)
11355 printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
11357 printf ("%" PRId32, cu);
11376 printf ("unknown-0x%" PRIx32, kind);
11379 printf (":%c)", (is_static ? 'S' : 'G'));
11390 /* Returns true and sets split DWARF CU id if there is a split compile
11391 unit in the given Dwarf, and no non-split units are found (before it). */
11393 is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
11395 Dwarf_CU *cu = NULL;
11396 while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
11399 if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11400 id, NULL, NULL) != 0)
11403 if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
11406 /* We really only care about the split compile unit, the types
11407 should be fine and self sufficient. Also they don't have an
11408 id that we can match with a skeleton unit. */
11409 if (unit_type == DW_UT_split_compile)
11419 /* Check that there is one and only one Dwfl_Module, return in arg. */
11421 getone_dwflmod (Dwfl_Module *dwflmod,
11422 void **userdata __attribute__ ((unused)),
11423 const char *name __attribute__ ((unused)),
11424 Dwarf_Addr base __attribute__ ((unused)),
11427 Dwfl_Module **m = (Dwfl_Module **) arg;
11429 return DWARF_CB_ABORT;
11431 return DWARF_CB_OK;
11435 print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
11437 /* Used for skeleton file, if necessary for split DWARF. */
11438 Dwfl *skel_dwfl = NULL;
11439 Dwfl_Module *skel_mod = NULL;
11440 char *skel_name = NULL;
11441 Dwarf *split_dbg = NULL;
11442 Dwarf_CU *split_cu = NULL;
11444 /* Before we start the real work get a debug context descriptor. */
11446 Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
11450 .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
11454 if ((print_debug_sections & ~(section_exception|section_frame)) != 0)
11455 error (0, 0, _("cannot get debug context descriptor: %s"),
11461 /* If we are asked about a split dwarf (.dwo) file, use the user
11462 provided, or find the corresponding skeleton file. If we got
11463 a skeleton file, replace the given dwflmod and dbg, with one
11464 derived from the skeleton file to provide enough context. */
11466 if (is_split_dwarf (dbg, &split_id, &split_cu))
11468 if (dwarf_skeleton != NULL)
11469 skel_name = strdup (dwarf_skeleton);
11472 /* Replace file.dwo with file.o and see if that matches. */
11474 dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
11478 size_t flen = strlen (fname);
11479 if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
11481 skel_name = strdup (fname);
11482 if (skel_name != NULL)
11484 skel_name[flen - 3] = 'o';
11485 skel_name[flen - 2] = '\0';
11491 if (skel_name != NULL)
11493 int skel_fd = open (skel_name, O_RDONLY);
11495 fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
11496 " '%s'\n", skel_name);
11498 skel_dwfl = create_dwfl (skel_fd, skel_name);
11500 if (skel_dwfl != NULL)
11502 if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
11503 &skel_mod, 0) != 0)
11505 fprintf (stderr, "Warning: Bad DWARF skeleton,"
11506 " multiple modules '%s'\n", skel_name);
11507 dwfl_end (skel_dwfl);
11511 else if (skel_fd != -1)
11512 fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
11513 " '%s': %s\n", skel_name, dwfl_errmsg (-1));
11515 if (skel_mod != NULL)
11517 Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
11518 if (skel_dbg != NULL)
11520 /* First check the skeleton CU DIE, only fetch
11521 the split DIE if we know the id matches to
11522 not unnecessary search for any split DIEs we
11524 Dwarf_CU *cu = NULL;
11525 while (dwarf_get_units (skel_dbg, cu, &cu,
11526 NULL, NULL, NULL, NULL) == 0)
11530 if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
11531 &skel_id, NULL, NULL) == 0
11532 && unit_type == DW_UT_skeleton
11533 && split_id == skel_id)
11536 if (dwarf_cu_info (cu, NULL, NULL, NULL,
11538 NULL, NULL, NULL) == 0
11539 && dwarf_tag (&subdie) != DW_TAG_invalid)
11541 split_dbg = dwarf_cu_getdwarf (subdie.cu);
11542 if (split_dbg == NULL)
11544 "Warning: Couldn't get split_dbg:"
11545 " %s\n", dwarf_errmsg (-1));
11550 /* Everything matches up, but not
11551 according to libdw. Which means
11552 the user knew better. So...
11553 Terrible hack... We can never
11554 destroy the underlying dwfl
11555 because it would free the wrong
11556 Dwarfs... So we leak memory...*/
11557 if (cu->split == NULL
11558 && dwarf_skeleton != NULL)
11560 do_not_close_dwfl = true;
11561 __libdw_link_skel_split (cu, split_cu);
11562 split_dbg = dwarf_cu_getdwarf (split_cu);
11566 fprintf (stderr, "Warning: Couldn't get"
11567 " skeleton subdie: %s\n",
11568 dwarf_errmsg (-1));
11572 if (split_dbg == NULL)
11573 fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
11576 fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
11577 " %s\n", dwfl_errmsg (-1));
11581 if (split_dbg != NULL)
11584 dwflmod = skel_mod;
11586 else if (skel_name == NULL)
11588 "Warning: split DWARF file, but no skeleton found.\n");
11590 else if (dwarf_skeleton != NULL)
11591 fprintf (stderr, "Warning: DWARF skeleton given,"
11592 " but not a split DWARF file\n");
11595 /* Get the section header string table index. */
11597 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
11598 error_exit (0, _("cannot get section header string table index"));
11600 /* If the .debug_info section is listed as implicitly required then
11601 we must make sure to handle it before handling any other debug
11602 section. Various other sections depend on the CU DIEs being
11603 scanned (silently) first. */
11604 bool implicit_info = (implicit_debug_sections & section_info) != 0;
11605 bool explicit_info = (print_debug_sections & section_info) != 0;
11608 Elf_Scn *scn = NULL;
11609 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11611 GElf_Shdr shdr_mem;
11612 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11614 if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11616 const char *name = elf_strptr (ebl->elf, shstrndx,
11621 if (strcmp (name, ".debug_info") == 0
11622 || strcmp (name, ".debug_info.dwo") == 0
11623 || strcmp (name, ".zdebug_info") == 0
11624 || strcmp (name, ".zdebug_info.dwo") == 0
11625 || strcmp (name, ".gnu.debuglto_.debug_info") == 0)
11627 print_debug_info_section (dwflmod, ebl, ehdr,
11633 print_debug_sections &= ~section_info;
11634 implicit_debug_sections &= ~section_info;
11637 /* Look through all the sections for the debugging sections to print. */
11638 Elf_Scn *scn = NULL;
11639 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11641 GElf_Shdr shdr_mem;
11642 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11644 if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11646 static const struct
11649 enum section_e bitmask;
11650 void (*fp) (Dwfl_Module *, Ebl *,
11651 GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
11652 } debug_sections[] =
11654 #define NEW_SECTION(name) \
11655 { ".debug_" #name, section_##name, print_debug_##name##_section }
11656 NEW_SECTION (abbrev),
11657 NEW_SECTION (addr),
11658 NEW_SECTION (aranges),
11659 NEW_SECTION (frame),
11660 NEW_SECTION (info),
11661 NEW_SECTION (types),
11662 NEW_SECTION (line),
11664 /* loclists is loc for DWARF5. */
11665 { ".debug_loclists", section_loc,
11666 print_debug_loclists_section },
11667 NEW_SECTION (pubnames),
11669 /* A DWARF5 specialised debug string section. */
11670 { ".debug_line_str", section_str,
11671 print_debug_str_section },
11672 /* DWARF5 string offsets table. */
11673 { ".debug_str_offsets", section_str,
11674 print_debug_str_offsets_section },
11675 NEW_SECTION (macinfo),
11676 NEW_SECTION (macro),
11677 NEW_SECTION (ranges),
11678 /* rnglists is ranges for DWARF5. */
11679 { ".debug_rnglists", section_ranges,
11680 print_debug_rnglists_section },
11681 { ".eh_frame", section_frame | section_exception,
11682 print_debug_frame_section },
11683 { ".eh_frame_hdr", section_frame | section_exception,
11684 print_debug_frame_hdr_section },
11685 { ".gcc_except_table", section_frame | section_exception,
11686 print_debug_exception_table },
11687 { ".gdb_index", section_gdb_index, print_gdb_index_section }
11689 const int ndebug_sections = (sizeof (debug_sections)
11690 / sizeof (debug_sections[0]));
11691 const char *name = elf_strptr (ebl->elf, shstrndx,
11697 for (n = 0; n < ndebug_sections; ++n)
11699 size_t dbglen = strlen (debug_sections[n].name);
11700 size_t scnlen = strlen (name);
11701 if ((strncmp (name, debug_sections[n].name, dbglen) == 0
11702 && (dbglen == scnlen
11703 || (scnlen == dbglen + 4
11704 && strstr (name, ".dwo") == name + dbglen)))
11705 || (name[0] == '.' && name[1] == 'z'
11706 && debug_sections[n].name[1] == 'd'
11707 && strncmp (&name[2], &debug_sections[n].name[1],
11709 && (scnlen == dbglen + 1
11710 || (scnlen == dbglen + 5
11711 && strstr (name, ".dwo") == name + dbglen + 1)))
11712 || (scnlen > 14 /* .gnu.debuglto_ prefix. */
11713 && startswith (name, ".gnu.debuglto_")
11714 && strcmp (&name[14], debug_sections[n].name) == 0)
11717 if ((print_debug_sections | implicit_debug_sections)
11718 & debug_sections[n].bitmask)
11719 debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
11726 dwfl_end (skel_dwfl);
11729 /* Turn implicit and/or explicit back on in case we go over another file. */
11731 implicit_debug_sections |= section_info;
11733 print_debug_sections |= section_info;
11735 reset_listptr (&known_locsptr);
11736 reset_listptr (&known_loclistsptr);
11737 reset_listptr (&known_rangelistptr);
11738 reset_listptr (&known_rnglistptr);
11739 reset_listptr (&known_addrbases);
11740 reset_listptr (&known_stroffbases);
11744 #define ITEM_INDENT 4
11745 #define WRAP_COLUMN 75
11747 /* Print "NAME: FORMAT", wrapping when output text would make the line
11748 exceed WRAP_COLUMN. Unpadded numbers look better for the core items
11749 but this function is also used for registers which should be printed
11750 aligned. Fortunately registers output uses fixed fields width (such
11751 as %11d) for the alignment.
11753 Line breaks should not depend on the particular values although that
11754 may happen in some cases of the core items. */
11756 static unsigned int
11757 __attribute__ ((format (printf, 6, 7)))
11758 print_core_item (unsigned int colno, char sep, unsigned int wrap,
11759 size_t name_width, const char *name, const char *format, ...)
11761 size_t len = strlen (name);
11762 if (name_width < len)
11767 va_start (ap, format);
11768 int out_len = vasprintf (&out, format, ap);
11771 error_exit (0, _("memory exhausted"));
11773 size_t n = name_width + sizeof ": " - 1 + out_len;
11777 printf ("%*s", ITEM_INDENT, "");
11778 colno = ITEM_INDENT + n;
11780 else if (colno + 2 + n < wrap)
11782 printf ("%c ", sep);
11787 printf ("\n%*s", ITEM_INDENT, "");
11788 colno = ITEM_INDENT + n;
11791 printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
11798 static const void *
11799 convert (Elf *core, Elf_Type type, uint_fast16_t count,
11800 void *value, const void *data, size_t size)
11802 Elf_Data valuedata =
11806 .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
11807 .d_version = EV_CURRENT,
11812 .d_buf = (void *) data,
11813 .d_size = valuedata.d_size,
11814 .d_version = EV_CURRENT,
11817 Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
11818 ? elf32_xlatetom : elf64_xlatetom)
11819 (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
11821 error_exit (0, _("cannot convert core note data: %s"),
11824 return data + indata.d_size;
11827 typedef uint8_t GElf_Byte;
11829 static unsigned int
11830 handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
11831 unsigned int colno, size_t *repeated_size)
11833 uint_fast16_t count = item->count ?: 1;
11834 /* Ebl_Core_Item count is always a small number.
11835 Make sure the backend didn't put in some large bogus value. */
11836 assert (count < 128);
11839 DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
11840 DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
11841 DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
11842 DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
11843 DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
11844 DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
11846 #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
11847 typedef union { TYPES; } value_t;
11848 void *data = alloca (count * sizeof (value_t));
11851 #define DO_TYPE(NAME, Name, hex, dec) \
11852 GElf_##Name *value_##Name __attribute__((unused)) = data
11856 size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
11857 size_t convsize = size;
11858 if (repeated_size != NULL)
11860 if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
11862 data = alloca (*repeated_size);
11863 count *= *repeated_size / size;
11864 convsize = count * size;
11865 *repeated_size -= convsize;
11867 else if (item->count != 0 || item->format != '\n')
11868 *repeated_size -= size;
11871 convert (core, item->type, count, data, desc + item->offset, convsize);
11873 Elf_Type type = item->type;
11874 if (type == ELF_T_ADDR)
11875 type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
11877 switch (item->format)
11880 assert (count == 1);
11883 #define DO_TYPE(NAME, Name, hex, dec) \
11884 case ELF_T_##NAME: \
11885 colno = print_core_item (colno, ',', WRAP_COLUMN, \
11886 0, item->name, dec, value_##Name[0]); \
11896 assert (count == 1);
11899 #define DO_TYPE(NAME, Name, hex, dec) \
11900 case ELF_T_##NAME: \
11901 colno = print_core_item (colno, ',', WRAP_COLUMN, \
11902 0, item->name, hex, value_##Name[0]); \
11913 assert (size % sizeof (unsigned int) == 0);
11914 unsigned int nbits = count * size * 8;
11915 unsigned int pop = 0;
11916 for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
11917 pop += __builtin_popcount (*i);
11918 bool negate = pop > nbits / 2;
11919 const unsigned int bias = item->format == 'b';
11922 char printed[(negate ? nbits - pop : pop) * 16 + 1];
11926 if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
11928 assert (size == sizeof (unsigned int) * 2);
11929 for (unsigned int *i = data;
11930 (void *) i < data + count * size; i += 2)
11932 unsigned int w = i[1];
11938 unsigned int lastbit = 0;
11939 unsigned int run = 0;
11940 for (const unsigned int *i = data;
11941 (void *) i < data + count * size; ++i)
11943 unsigned int bit = ((void *) i - data) * 8;
11944 unsigned int w = negate ? ~*i : *i;
11947 /* Note that a right shift equal to (or greater than)
11948 the number of bits of w is undefined behaviour. In
11949 particular when the least significant bit is bit 32
11950 (w = 0x8000000) then w >>= n is undefined. So
11951 explicitly handle that case separately. */
11952 unsigned int n = ffs (w);
11953 if (n < sizeof (w) * 8)
11959 if (lastbit != 0 && lastbit + 1 == bit)
11964 p += sprintf (p, "%u", bit - bias);
11966 p += sprintf (p, ",%u", bit - bias);
11968 p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
11975 if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
11976 p += sprintf (p, "-%u", lastbit - bias);
11978 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11979 negate ? "~<%s>" : "<%s>", printed);
11984 case (char) ('T'|0x80):
11985 assert (count == 2);
11990 #define DO_TYPE(NAME, Name, hex, dec) \
11991 case ELF_T_##NAME: \
11992 sec = value_##Name[0]; \
11993 usec = value_##Name[1]; \
12000 if (unlikely (item->format == (char) ('T'|0x80)))
12002 /* This is a hack for an ill-considered 64-bit ABI where
12003 tv_usec is actually a 32-bit field with 32 bits of padding
12004 rounding out struct timeval. We've already converted it as
12005 a 64-bit field. For little-endian, this just means the
12006 high half is the padding; it's presumably zero, but should
12007 be ignored anyway. For big-endian, it means the 32-bit
12008 field went into the high half of USEC. */
12009 GElf_Ehdr ehdr_mem;
12010 GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
12011 if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
12014 usec &= UINT32_MAX;
12016 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12017 "%" PRIu64 ".%.6" PRIu64, sec, usec);
12021 assert (count == 1);
12022 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12023 "%c", value_Byte[0]);
12027 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
12028 "%.*s", (int) count, value_Byte);
12032 /* This is a list of strings separated by '\n'. */
12033 assert (item->count == 0);
12034 assert (repeated_size != NULL);
12035 assert (item->name == NULL);
12036 if (unlikely (item->offset >= *repeated_size))
12039 const char *s = desc + item->offset;
12040 size = *repeated_size - item->offset;
12041 *repeated_size = 0;
12044 const char *eol = memchr (s, '\n', size);
12048 printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
12051 size -= eol + 1 - s;
12055 colno = WRAP_COLUMN;
12062 error (0, 0, "XXX not handling format '%c' for %s",
12063 item->format, item->name);
12073 /* Sort items by group, and by layout offset within each group. */
12075 compare_core_items (const void *a, const void *b)
12077 const Ebl_Core_Item *const *p1 = a;
12078 const Ebl_Core_Item *const *p2 = b;
12079 const Ebl_Core_Item *item1 = *p1;
12080 const Ebl_Core_Item *item2 = *p2;
12082 return ((item1->group == item2->group ? 0
12083 : strcmp (item1->group, item2->group))
12084 ?: (int) item1->offset - (int) item2->offset);
12087 /* Sort item groups by layout offset of the first item in the group. */
12089 compare_core_item_groups (const void *a, const void *b)
12091 const Ebl_Core_Item *const *const *p1 = a;
12092 const Ebl_Core_Item *const *const *p2 = b;
12093 const Ebl_Core_Item *const *group1 = *p1;
12094 const Ebl_Core_Item *const *group2 = *p2;
12095 const Ebl_Core_Item *item1 = *group1;
12096 const Ebl_Core_Item *item2 = *group2;
12098 return (int) item1->offset - (int) item2->offset;
12101 static unsigned int
12102 handle_core_items (Elf *core, const void *desc, size_t descsz,
12103 const Ebl_Core_Item *items, size_t nitems)
12107 unsigned int colno = 0;
12109 /* FORMAT '\n' makes sense to be present only as a single item as it
12110 processes all the data of a note. FORMATs 'b' and 'B' have a special case
12111 if present as a single item but they can be also processed with other
12113 if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
12114 || items[0].format == 'B'))
12116 assert (items[0].offset == 0);
12117 size_t size = descsz;
12118 colno = handle_core_item (core, items, desc, colno, &size);
12119 /* If SIZE is not zero here there is some remaining data. But we do not
12120 know how to process it anyway. */
12123 for (size_t i = 0; i < nitems; ++i)
12124 assert (items[i].format != '\n');
12126 /* Sort to collect the groups together. */
12127 const Ebl_Core_Item *sorted_items[nitems];
12128 for (size_t i = 0; i < nitems; ++i)
12129 sorted_items[i] = &items[i];
12130 qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
12132 /* Collect the unique groups and sort them. */
12133 const Ebl_Core_Item **groups[nitems];
12134 groups[0] = &sorted_items[0];
12135 size_t ngroups = 1;
12136 for (size_t i = 1; i < nitems; ++i)
12137 if (sorted_items[i]->group != sorted_items[i - 1]->group
12138 && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
12139 groups[ngroups++] = &sorted_items[i];
12140 qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
12142 /* Write out all the groups. */
12143 const void *last = desc;
12146 for (size_t i = 0; i < ngroups; ++i)
12148 for (const Ebl_Core_Item **item = groups[i];
12149 (item < &sorted_items[nitems]
12150 && ((*item)->group == groups[i][0]->group
12151 || !strcmp ((*item)->group, groups[i][0]->group)));
12153 colno = handle_core_item (core, *item, desc, colno, NULL);
12155 /* Force a line break at the end of the group. */
12156 colno = WRAP_COLUMN;
12162 /* This set of items consumed a certain amount of the note's data.
12163 If there is more data there, we have another unit of the same size.
12164 Loop to print that out too. */
12165 const Ebl_Core_Item *item = &items[nitems - 1];
12166 size_t eltsz = item->offset + gelf_fsize (core, item->type,
12167 item->count ?: 1, EV_CURRENT);
12176 while (descsz >= eltsz && !memcmp (desc, last, eltsz));
12180 /* For just one repeat, print it unabridged twice. */
12185 printf (_("\n%*s... <repeats %u more times> ..."),
12186 ITEM_INDENT, "", reps);
12190 while (descsz > 0);
12195 static unsigned int
12196 handle_bit_registers (const Ebl_Register_Location *regloc, const void *desc,
12197 unsigned int colno)
12199 desc += regloc->offset;
12201 abort (); /* XXX */
12206 static unsigned int
12207 handle_core_register (Ebl *ebl, Elf *core, int maxregname,
12208 const Ebl_Register_Location *regloc, const void *desc,
12209 unsigned int colno)
12211 if (regloc->bits % 8 != 0)
12212 return handle_bit_registers (regloc, desc, colno);
12214 desc += regloc->offset;
12216 for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
12218 char name[REGNAMESZ];
12221 register_info (ebl, reg, regloc, name, &bits, &type);
12224 BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
12225 BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
12226 BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
12227 BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
12229 #define BITS(bits, xtype, sfmt, ufmt) \
12230 uint##bits##_t b##bits; int##bits##_t b##bits##s
12231 union { TYPES; uint64_t b128[2]; } value;
12236 case DW_ATE_unsigned:
12237 case DW_ATE_signed:
12238 case DW_ATE_address:
12241 #define BITS(bits, xtype, sfmt, ufmt) \
12243 desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
12244 if (type == DW_ATE_signed) \
12245 colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12246 maxregname, name, \
12247 sfmt, value.b##bits##s); \
12249 colno = print_core_item (colno, ' ', WRAP_COLUMN, \
12250 maxregname, name, \
12251 ufmt, value.b##bits); \
12257 assert (type == DW_ATE_unsigned);
12258 desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
12259 int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
12260 colno = print_core_item (colno, ' ', WRAP_COLUMN,
12262 "0x%.16" PRIx64 "%.16" PRIx64,
12263 value.b128[!be], value.b128[be]);
12273 /* Print each byte in hex, the whole thing in native byte order. */
12274 assert (bits % 8 == 0);
12275 const uint8_t *bytes = desc;
12277 char hex[bits / 4 + 1];
12278 hex[bits / 4] = '\0';
12280 if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
12282 bytes += bits / 8 - 1;
12286 for (char *h = hex; bits > 0; bits -= 8, idx += incr)
12288 *h++ = "0123456789abcdef"[bytes[idx] >> 4];
12289 *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
12291 colno = print_core_item (colno, ' ', WRAP_COLUMN,
12292 maxregname, name, "0x%s", hex);
12295 desc += regloc->pad;
12304 struct register_info
12306 const Ebl_Register_Location *regloc;
12308 char name[REGNAMESZ];
12315 register_bitpos (const struct register_info *r)
12317 return (r->regloc->offset * 8
12318 + ((r->regno - r->regloc->regno)
12319 * (r->regloc->bits + r->regloc->pad * 8)));
12323 compare_sets_by_info (const struct register_info *r1,
12324 const struct register_info *r2)
12326 return ((int) r2->bits - (int) r1->bits
12327 ?: register_bitpos (r1) - register_bitpos (r2));
12330 /* Sort registers by set, and by size and layout offset within each set. */
12332 compare_registers (const void *a, const void *b)
12334 const struct register_info *r1 = a;
12335 const struct register_info *r2 = b;
12337 /* Unused elements sort last. */
12338 if (r1->regloc == NULL)
12339 return r2->regloc == NULL ? 0 : 1;
12340 if (r2->regloc == NULL)
12343 return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
12344 ?: compare_sets_by_info (r1, r2));
12347 /* Sort register sets by layout offset of the first register in the set. */
12349 compare_register_sets (const void *a, const void *b)
12351 const struct register_info *const *p1 = a;
12352 const struct register_info *const *p2 = b;
12353 return compare_sets_by_info (*p1, *p2);
12357 same_set (const struct register_info *a,
12358 const struct register_info *b,
12359 const struct register_info *regs,
12362 return (a < ®s[maxnreg] && a->regloc != NULL
12363 && b < ®s[maxnreg] && b->regloc != NULL
12364 && a->bits == b->bits
12365 && (a->set == b->set || !strcmp (a->set, b->set)));
12368 static unsigned int
12369 handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
12370 const Ebl_Register_Location *reglocs, size_t nregloc)
12375 ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
12378 for (size_t i = 0; i < nregloc; ++i)
12379 if (maxnreg < reglocs[i].regno + reglocs[i].count)
12380 maxnreg = reglocs[i].regno + reglocs[i].count;
12381 assert (maxnreg > 0);
12384 struct register_info regs[maxnreg];
12385 memset (regs, 0, sizeof regs);
12387 /* Sort to collect the sets together. */
12389 for (size_t i = 0; i < nregloc; ++i)
12390 for (int reg = reglocs[i].regno;
12391 reg < reglocs[i].regno + reglocs[i].count;
12394 assert (reg < maxnreg);
12397 struct register_info *info = ®s[reg];
12398 info->regloc = ®locs[i];
12400 info->set = register_info (ebl, reg, ®locs[i],
12401 info->name, &info->bits, &info->type);
12403 qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
12405 /* Collect the unique sets and sort them. */
12406 struct register_info *sets[maxreg + 1];
12407 sets[0] = ®s[0];
12409 for (int i = 1; i <= maxreg; ++i)
12410 if (regs[i].regloc != NULL
12411 && !same_set (®s[i], ®s[i - 1], regs, maxnreg))
12412 sets[nsets++] = ®s[i];
12413 qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
12415 /* Write out all the sets. */
12416 unsigned int colno = 0;
12417 for (size_t i = 0; i < nsets; ++i)
12419 /* Find the longest name of a register in this set. */
12420 size_t maxname = 0;
12421 const struct register_info *end;
12422 for (end = sets[i]; same_set (sets[i], end, regs, maxnreg); ++end)
12424 size_t len = strlen (end->name);
12429 for (const struct register_info *reg = sets[i];
12431 reg += reg->regloc->count ?: 1)
12432 colno = handle_core_register (ebl, core, maxname,
12433 reg->regloc, desc, colno);
12435 /* Force a line break at the end of the group. */
12436 colno = WRAP_COLUMN;
12443 handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12445 Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
12448 error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12450 const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
12451 for (size_t i = 0; i < nauxv; ++i)
12453 GElf_auxv_t av_mem;
12454 GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
12460 if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
12462 /* Unknown type. */
12463 if (av->a_un.a_val == 0)
12464 printf (" %" PRIu64 "\n", av->a_type);
12466 printf (" %" PRIu64 ": %#" PRIx64 "\n",
12467 av->a_type, av->a_un.a_val);
12472 case '\0': /* Normally zero. */
12473 if (av->a_un.a_val == 0)
12475 printf (" %s\n", name);
12479 case 'x': /* hex */
12480 case 'p': /* address */
12481 case 's': /* address of string */
12482 printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
12485 printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
12488 printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
12492 printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
12493 GElf_Xword bit = 1;
12494 const char *pfx = "<";
12495 for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
12497 if (av->a_un.a_val & bit)
12499 printf ("%s%s", pfx, p);
12514 buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
12516 return ptr < end && (size_t) (end - ptr) >= sz;
12520 buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12523 if (! buf_has_data (*ptrp, end, 4))
12526 *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
12531 buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
12534 size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12535 if (! buf_has_data (*ptrp, end, sz))
12544 *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
12554 handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12556 Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
12558 error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12560 unsigned char const *ptr = data->d_buf;
12561 unsigned char const *const end = data->d_buf + data->d_size;
12563 /* Siginfo head is three ints: signal number, error number, origin
12565 int si_signo, si_errno, si_code;
12566 if (! buf_read_int (core, &ptr, end, &si_signo)
12567 || ! buf_read_int (core, &ptr, end, &si_errno)
12568 || ! buf_read_int (core, &ptr, end, &si_code))
12571 printf (" Not enough data in NT_SIGINFO note.\n");
12575 /* Next is a pointer-aligned union of structures. On 64-bit
12576 machines, that implies a word of padding. */
12577 if (gelf_getclass (core) == ELFCLASS64)
12580 printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
12581 si_signo, si_errno, si_code);
12592 if (! buf_read_ulong (core, &ptr, end, &addr))
12594 printf (" fault address: %#" PRIx64 "\n", addr);
12600 else if (si_code == CORE_SI_USER)
12603 if (! buf_read_int (core, &ptr, end, &pid)
12604 || ! buf_read_int (core, &ptr, end, &uid))
12606 printf (" sender PID: %d, sender UID: %d\n", pid, uid);
12611 handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12613 Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
12615 error_exit (0, _("cannot convert core note data: %s"), elf_errmsg (-1));
12617 unsigned char const *ptr = data->d_buf;
12618 unsigned char const *const end = data->d_buf + data->d_size;
12620 uint64_t count, page_size;
12621 if (! buf_read_ulong (core, &ptr, end, &count)
12622 || ! buf_read_ulong (core, &ptr, end, &page_size))
12625 printf (" Not enough data in NT_FILE note.\n");
12629 size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12630 uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
12631 if (count > maxcount)
12634 /* Where file names are stored. */
12635 unsigned char const *const fstart = ptr + 3 * count * addrsize;
12636 char const *fptr = (char *) fstart;
12638 printf (" %" PRId64 " files:\n", count);
12639 for (uint64_t i = 0; i < count; ++i)
12641 uint64_t mstart, mend, moffset;
12642 if (! buf_read_ulong (core, &ptr, fstart, &mstart)
12643 || ! buf_read_ulong (core, &ptr, fstart, &mend)
12644 || ! buf_read_ulong (core, &ptr, fstart, &moffset))
12647 const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
12651 int ct = printf (" %08" PRIx64 "-%08" PRIx64
12652 " %08" PRIx64 " %" PRId64,
12653 mstart, mend, moffset * page_size, mend - mstart);
12654 printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
12661 handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
12662 const char *name, const void *desc)
12664 GElf_Word regs_offset;
12666 const Ebl_Register_Location *reglocs;
12668 const Ebl_Core_Item *items;
12670 if (! ebl_core_note (ebl, nhdr, name, desc,
12671 ®s_offset, &nregloc, ®locs, &nitems, &items))
12674 /* Pass 0 for DESCSZ when there are registers in the note,
12675 so that the ITEMS array does not describe the whole thing.
12676 For non-register notes, the actual descsz might be a multiple
12677 of the unit size, not just exactly the unit size. */
12678 unsigned int colno = handle_core_items (ebl->elf, desc,
12679 nregloc == 0 ? nhdr->n_descsz : 0,
12682 putchar_unlocked ('\n');
12684 colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
12687 putchar_unlocked ('\n');
12691 handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
12692 GElf_Off start, Elf_Data *data)
12694 fputs_unlocked (_(" Owner Data size Type\n"), stdout);
12701 size_t name_offset;
12702 size_t desc_offset;
12703 while (offset < data->d_size
12704 && (offset = gelf_getnote (data, offset,
12705 &nhdr, &name_offset, &desc_offset)) > 0)
12707 const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
12708 const char *desc = data->d_buf + desc_offset;
12710 /* GNU Build Attributes are weird, they store most of their data
12711 into the owner name field. Extract just the owner name
12712 prefix here, then use the rest later as data. */
12713 bool is_gnu_build_attr
12714 = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
12715 const char *print_name = (is_gnu_build_attr
12716 ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
12717 size_t print_namesz = (is_gnu_build_attr
12718 ? strlen (print_name) : nhdr.n_namesz);
12722 printf (_(" %-13.*s %9" PRId32 " %s\n"),
12723 (int) print_namesz, print_name, nhdr.n_descsz,
12724 ehdr->e_type == ET_CORE
12725 ? ebl_core_note_type_name (ebl, nhdr.n_type,
12727 : ebl_object_note_type_name (ebl, name, nhdr.n_type,
12729 buf2, sizeof (buf2)));
12731 /* Filter out invalid entries. */
12732 if (memchr (name, '\0', nhdr.n_namesz) != NULL
12733 /* XXX For now help broken Linux kernels. */
12736 if (ehdr->e_type == ET_CORE)
12738 if (nhdr.n_type == NT_AUXV
12739 && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
12740 || (nhdr.n_namesz == 5 && name[4] == '\0'))
12741 && !memcmp (name, "CORE", 4))
12742 handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
12743 start + desc_offset);
12744 else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
12745 switch (nhdr.n_type)
12748 handle_siginfo_note (ebl->elf, nhdr.n_descsz,
12749 start + desc_offset);
12753 handle_file_note (ebl->elf, nhdr.n_descsz,
12754 start + desc_offset);
12758 handle_core_note (ebl, &nhdr, name, desc);
12761 handle_core_note (ebl, &nhdr, name, desc);
12764 ebl_object_note (ebl, nhdr.n_namesz, name, nhdr.n_type,
12765 nhdr.n_descsz, desc);
12769 if (offset == data->d_size)
12774 _("cannot get content of note: %s"),
12775 data != NULL ? "garbage data" : elf_errmsg (-1));
12779 handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
12781 /* If we have section headers, just look for SHT_NOTE sections.
12782 In a debuginfo file, the program headers are not reliable. */
12785 /* Get the section header string table index. */
12787 if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
12788 error_exit (0, _("cannot get section header string table index"));
12790 Elf_Scn *scn = NULL;
12791 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12793 GElf_Shdr shdr_mem;
12794 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12796 if (shdr == NULL || shdr->sh_type != SHT_NOTE)
12797 /* Not what we are looking for. */
12800 if (notes_section != NULL)
12802 char *sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
12803 if (sname == NULL || strcmp (sname, notes_section) != 0)
12808 \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12810 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
12811 shdr->sh_size, shdr->sh_offset);
12813 handle_notes_data (ebl, ehdr, shdr->sh_offset,
12814 elf_getdata (scn, NULL));
12819 /* We have to look through the program header to find the note
12820 sections. There can be more than one. */
12821 for (size_t cnt = 0; cnt < phnum; ++cnt)
12824 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
12826 if (phdr == NULL || phdr->p_type != PT_NOTE)
12827 /* Not what we are looking for. */
12831 \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12832 phdr->p_filesz, phdr->p_offset);
12834 handle_notes_data (ebl, ehdr, phdr->p_offset,
12835 elf_getdata_rawchunk (ebl->elf,
12836 phdr->p_offset, phdr->p_filesz,
12837 (phdr->p_align == 8
12838 ? ELF_T_NHDR8 : ELF_T_NHDR)));
12844 hex_dump (const uint8_t *data, size_t len)
12849 printf (" 0x%08zx ", pos);
12851 const size_t chunk = MIN (len - pos, 16);
12853 for (size_t i = 0; i < chunk; ++i)
12855 printf ("%02x ", data[pos + i]);
12857 printf ("%02x", data[pos + i]);
12860 printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
12862 for (size_t i = 0; i < chunk; ++i)
12864 unsigned char b = data[pos + i];
12865 printf ("%c", isprint (b) ? b : '.');
12874 dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12876 if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12877 printf (_("\nSection [%zu] '%s' has no data to dump.\n"),
12878 elf_ndxscn (scn), name);
12881 if (print_decompress)
12883 /* We try to decompress the section, but keep the old shdr around
12884 so we can show both the original shdr size and the uncompressed
12886 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12888 if (elf_compress (scn, 0, 0) < 0)
12889 printf ("WARNING: %s [%zd]\n",
12890 _("Couldn't uncompress section"),
12893 else if (startswith (name, ".zdebug"))
12895 if (elf_compress_gnu (scn, 0, 0) < 0)
12896 printf ("WARNING: %s [%zd]\n",
12897 _("Couldn't uncompress section"),
12902 Elf_Data *data = elf_rawdata (scn, NULL);
12904 error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
12905 elf_ndxscn (scn), name, elf_errmsg (-1));
12908 if (data->d_size == shdr->sh_size)
12909 printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
12910 " bytes at offset %#0" PRIx64 ":\n"),
12911 elf_ndxscn (scn), name,
12912 shdr->sh_size, shdr->sh_offset);
12914 printf (_("\nHex dump of section [%zu] '%s', %" PRIu64
12915 " bytes (%zd uncompressed) at offset %#0"
12917 elf_ndxscn (scn), name,
12918 shdr->sh_size, data->d_size, shdr->sh_offset);
12919 hex_dump (data->d_buf, data->d_size);
12925 print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12927 if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12928 printf (_("\nSection [%zu] '%s' has no strings to dump.\n"),
12929 elf_ndxscn (scn), name);
12932 if (print_decompress)
12934 /* We try to decompress the section, but keep the old shdr around
12935 so we can show both the original shdr size and the uncompressed
12937 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12939 if (elf_compress (scn, 0, 0) < 0)
12940 printf ("WARNING: %s [%zd]\n",
12941 _("Couldn't uncompress section"),
12944 else if (startswith (name, ".zdebug"))
12946 if (elf_compress_gnu (scn, 0, 0) < 0)
12947 printf ("WARNING: %s [%zd]\n",
12948 _("Couldn't uncompress section"),
12953 Elf_Data *data = elf_rawdata (scn, NULL);
12955 error (0, 0, _("cannot get data for section [%zu] '%s': %s"),
12956 elf_ndxscn (scn), name, elf_errmsg (-1));
12959 if (data->d_size == shdr->sh_size)
12960 printf (_("\nString section [%zu] '%s' contains %" PRIu64
12961 " bytes at offset %#0" PRIx64 ":\n"),
12962 elf_ndxscn (scn), name,
12963 shdr->sh_size, shdr->sh_offset);
12965 printf (_("\nString section [%zu] '%s' contains %" PRIu64
12966 " bytes (%zd uncompressed) at offset %#0"
12968 elf_ndxscn (scn), name,
12969 shdr->sh_size, data->d_size, shdr->sh_offset);
12971 const char *start = data->d_buf;
12972 const char *const limit = start + data->d_size;
12975 const char *end = memchr (start, '\0', limit - start);
12976 const size_t pos = start - (const char *) data->d_buf;
12977 if (unlikely (end == NULL))
12979 printf (" [%6zx]- %.*s\n",
12980 pos, (int) (limit - start), start);
12983 printf (" [%6zx] %s\n", pos, start);
12985 } while (start < limit);
12991 for_each_section_argument (Elf *elf, const struct section_argument *list,
12992 void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
12995 /* Get the section header string table index. */
12997 if (elf_getshdrstrndx (elf, &shstrndx) < 0)
12998 error_exit (0, _("cannot get section header string table index"));
13000 for (const struct section_argument *a = list; a != NULL; a = a->next)
13003 GElf_Shdr shdr_mem;
13004 const char *name = NULL;
13007 unsigned long int shndx = strtoul (a->arg, &endp, 0);
13008 if (endp != a->arg && *endp == '\0')
13010 scn = elf_getscn (elf, shndx);
13013 error (0, 0, _("\nsection [%lu] does not exist"), shndx);
13017 if (gelf_getshdr (scn, &shdr_mem) == NULL)
13018 error_exit (0, _("cannot get section header: %s"),
13020 name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13021 (*dump) (scn, &shdr_mem, name);
13025 /* Need to look up the section by name. */
13027 bool found = false;
13028 while ((scn = elf_nextscn (elf, scn)) != NULL)
13030 if (gelf_getshdr (scn, &shdr_mem) == NULL)
13032 name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
13035 if (!strcmp (name, a->arg))
13038 (*dump) (scn, &shdr_mem, name);
13042 if (unlikely (!found) && !a->implicit)
13043 error (0, 0, _("\nsection '%s' does not exist"), a->arg);
13049 dump_data (Ebl *ebl)
13051 for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
13055 dump_strings (Ebl *ebl)
13057 for_each_section_argument (ebl->elf, string_sections, &print_string_section);
13061 print_strings (Ebl *ebl)
13063 /* Get the section header string table index. */
13065 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
13066 error_exit (0, _("cannot get section header string table index"));
13069 GElf_Shdr shdr_mem;
13072 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
13074 if (gelf_getshdr (scn, &shdr_mem) == NULL)
13077 if (shdr_mem.sh_type != SHT_PROGBITS
13078 || !(shdr_mem.sh_flags & SHF_STRINGS))
13081 name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
13085 print_string_section (scn, &shdr_mem, name);
13090 dump_archive_index (Elf *elf, const char *fname)
13093 const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
13096 int result = elf_errno ();
13097 if (unlikely (result != ELF_E_NO_INDEX))
13098 error_exit (0, _("cannot get symbol index of archive '%s': %s"),
13099 fname, elf_errmsg (result));
13101 printf (_("\nArchive '%s' has no symbol index\n"), fname);
13105 printf (_("\nIndex of archive '%s' has %zu entries:\n"),
13109 for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
13111 if (s->as_off != as_off)
13113 as_off = s->as_off;
13115 Elf *subelf = NULL;
13116 if (unlikely (elf_rand (elf, as_off) == 0)
13117 || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
13119 #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
13123 _("cannot extract member at offset %zu in '%s': %s"),
13124 as_off, fname, elf_errmsg (-1));
13126 const Elf_Arhdr *h = elf_getarhdr (subelf);
13128 printf (_("Archive member '%s' contains:\n"), h->ar_name);
13133 printf ("\t%s\n", s->as_name);
13137 #include "debugpred.h"