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/>. */
39 #include <stdio_ext.h>
50 #include <printversion.h>
51 #include "../libelf/libelfP.h"
52 #include "../libelf/common.h"
53 #include "../libebl/libeblP.h"
54 #include "../libdwelf/libdwelf.h"
55 #include "../libdw/libdwP.h"
56 #include "../libdwfl/libdwflP.h"
57 #include "../libdw/memory-access.h"
59 #include "../libdw/known-dwarf.h"
62 #define CORE_SIGILL SIGILL
63 #define CORE_SIGBUS SIGBUS
64 #define CORE_SIGFPE SIGFPE
65 #define CORE_SIGSEGV SIGSEGV
66 #define CORE_SI_USER SI_USER
68 /* We want the linux version of those as that is what shows up in the core files. */
69 #define CORE_SIGILL 4 /* Illegal instruction (ANSI). */
70 #define CORE_SIGBUS 7 /* BUS error (4.2 BSD). */
71 #define CORE_SIGFPE 8 /* Floating-point exception (ANSI). */
72 #define CORE_SIGSEGV 11 /* Segmentation violation (ANSI). */
73 #define CORE_SI_USER 0 /* Sent by kill, sigsend. */
76 /* Name and version of program. */
77 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
79 /* Bug report address. */
80 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
82 /* argp key value for --elf-section, non-ascii. */
83 #define ELF_INPUT_SECTION 256
85 /* argp key value for --dwarf-skeleton, non-ascii. */
86 #define DWARF_SKELETON 257
88 /* Terrible hack for hooking unrelated skeleton/split compile units,
89 see __libdw_link_skel_split in print_debug. */
90 static bool do_not_close_dwfl = false;
92 /* Definitions of arguments for argp functions. */
93 static const struct argp_option options[] =
95 { NULL, 0, NULL, 0, N_("ELF input selection:"), 0 },
96 { "elf-section", ELF_INPUT_SECTION, "SECTION", OPTION_ARG_OPTIONAL,
97 N_("Use the named SECTION (default .gnu_debugdata) as (compressed) ELF "
99 { "dwarf-skeleton", DWARF_SKELETON, "FILE", 0,
100 N_("Used with -w to find the skeleton Compile Units in FILE associated "
101 "with the Split Compile units in a .dwo input file"), 0 },
102 { NULL, 0, NULL, 0, N_("ELF output selection:"), 0 },
103 { "all", 'a', NULL, 0,
104 N_("All these plus -p .strtab -p .dynstr -p .comment"), 0 },
105 { "dynamic", 'd', NULL, 0, N_("Display the dynamic segment"), 0 },
106 { "file-header", 'h', NULL, 0, N_("Display the ELF file header"), 0 },
107 { "histogram", 'I', NULL, 0,
108 N_("Display histogram of bucket list lengths"), 0 },
109 { "program-headers", 'l', NULL, 0, N_("Display the program headers"), 0 },
110 { "segments", 'l', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
111 { "relocs", 'r', NULL, 0, N_("Display relocations"), 0 },
112 { "section-groups", 'g', NULL, 0, N_("Display the section groups"), 0 },
113 { "section-headers", 'S', NULL, 0, N_("Display the sections' headers"), 0 },
114 { "sections", 'S', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
115 { "symbols", 's', "SECTION", OPTION_ARG_OPTIONAL,
116 N_("Display the symbol table sections"), 0 },
117 { "version-info", 'V', NULL, 0, N_("Display versioning information"), 0 },
118 { "notes", 'n', NULL, 0, N_("Display the ELF notes"), 0 },
119 { "arch-specific", 'A', NULL, 0,
120 N_("Display architecture specific information, if any"), 0 },
121 { "exception", 'e', NULL, 0,
122 N_("Display sections for exception handling"), 0 },
124 { NULL, 0, NULL, 0, N_("Additional output selection:"), 0 },
125 { "debug-dump", 'w', "SECTION", OPTION_ARG_OPTIONAL,
126 N_("Display DWARF section content. SECTION can be one of abbrev, addr, "
127 "aranges, decodedaranges, frame, gdb_index, info, info+, loc, line, "
128 "decodedline, ranges, pubnames, str, macinfo, macro or exception"), 0 },
129 { "hex-dump", 'x', "SECTION", 0,
130 N_("Dump the uninterpreted contents of SECTION, by number or name"), 0 },
131 { "strings", 'p', "SECTION", OPTION_ARG_OPTIONAL,
132 N_("Print string contents of sections"), 0 },
133 { "string-dump", 'p', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
134 { "archive-index", 'c', NULL, 0,
135 N_("Display the symbol index of an archive"), 0 },
137 { NULL, 0, NULL, 0, N_("Output control:"), 0 },
138 { "numeric-addresses", 'N', NULL, 0,
139 N_("Do not find symbol names for addresses in DWARF data"), 0 },
140 { "unresolved-address-offsets", 'U', NULL, 0,
141 N_("Display just offsets instead of resolving values to addresses in DWARF data"), 0 },
142 { "wide", 'W', NULL, 0,
143 N_("Ignored for compatibility (lines always wide)"), 0 },
144 { "decompress", 'z', NULL, 0,
145 N_("Show compression information for compressed sections (when used with -S); decompress section before dumping data (when used with -p or -x)"), 0 },
146 { NULL, 0, NULL, 0, NULL, 0 }
149 /* Short description of program. */
150 static const char doc[] = N_("\
151 Print information from ELF file in human-readable form.");
153 /* Strings for arguments in help texts. */
154 static const char args_doc[] = N_("FILE...");
156 /* Prototype for option handler. */
157 static error_t parse_opt (int key, char *arg, struct argp_state *state);
159 /* Data structure to communicate with argp functions. */
160 static struct argp argp =
162 options, parse_opt, args_doc, doc, NULL, NULL, NULL
165 /* If non-null, the section from which we should read to (compressed) ELF. */
166 static const char *elf_input_section = NULL;
168 /* If non-null, the file that contains the skeleton CUs. */
169 static const char *dwarf_skeleton = NULL;
171 /* Flags set by the option controlling the output. */
173 /* True if dynamic segment should be printed. */
174 static bool print_dynamic_table;
176 /* True if the file header should be printed. */
177 static bool print_file_header;
179 /* True if the program headers should be printed. */
180 static bool print_program_header;
182 /* True if relocations should be printed. */
183 static bool print_relocations;
185 /* True if the section headers should be printed. */
186 static bool print_section_header;
188 /* True if the symbol table should be printed. */
189 static bool print_symbol_table;
191 /* A specific section name, or NULL to print all symbol tables. */
192 static char *symbol_table_section;
194 /* True if the version information should be printed. */
195 static bool print_version_info;
197 /* True if section groups should be printed. */
198 static bool print_section_groups;
200 /* True if bucket list length histogram should be printed. */
201 static bool print_histogram;
203 /* True if the architecture specific data should be printed. */
204 static bool print_arch;
206 /* True if note section content should be printed. */
207 static bool print_notes;
209 /* True if SHF_STRINGS section content should be printed. */
210 static bool print_string_sections;
212 /* True if archive index should be printed. */
213 static bool print_archive_index;
215 /* True if any of the control options except print_archive_index is set. */
216 static bool any_control_option;
218 /* True if we should print addresses from DWARF in symbolic form. */
219 static bool print_address_names = true;
221 /* True if we should print raw values instead of relativized addresses. */
222 static bool print_unresolved_addresses = false;
224 /* True if we should print the .debug_aranges section using libdw. */
225 static bool decodedaranges = false;
227 /* True if we should print the .debug_aranges section using libdw. */
228 static bool decodedline = false;
230 /* True if we want to show more information about compressed sections. */
231 static bool print_decompress = false;
233 /* True if we want to show split compile units for debug_info skeletons. */
234 static bool show_split_units = false;
236 /* Select printing of debugging sections. */
237 static enum section_e
239 section_abbrev = 1, /* .debug_abbrev */
240 section_aranges = 2, /* .debug_aranges */
241 section_frame = 4, /* .debug_frame or .eh_frame & al. */
242 section_info = 8, /* .debug_info, (implies .debug_types) */
243 section_line = 16, /* .debug_line */
244 section_loc = 32, /* .debug_loc */
245 section_pubnames = 64, /* .debug_pubnames */
246 section_str = 128, /* .debug_str */
247 section_macinfo = 256, /* .debug_macinfo */
248 section_ranges = 512, /* .debug_ranges */
249 section_exception = 1024, /* .eh_frame & al. */
250 section_gdb_index = 2048, /* .gdb_index */
251 section_macro = 4096, /* .debug_macro */
252 section_addr = 8192, /* .debug_addr */
253 section_types = 16384, /* .debug_types (implied by .debug_info) */
254 section_all = (section_abbrev | section_aranges | section_frame
255 | section_info | section_line | section_loc
256 | section_pubnames | section_str | section_macinfo
257 | section_ranges | section_exception | section_gdb_index
258 | section_macro | section_addr | section_types)
259 } print_debug_sections, implicit_debug_sections;
261 /* Select hex dumping of sections. */
262 static struct section_argument *dump_data_sections;
263 static struct section_argument **dump_data_sections_tail = &dump_data_sections;
265 /* Select string dumping of sections. */
266 static struct section_argument *string_sections;
267 static struct section_argument **string_sections_tail = &string_sections;
269 struct section_argument
271 struct section_argument *next;
276 /* Numbers of sections and program headers in the file. */
281 /* Declarations of local functions. */
282 static void process_file (int fd, const char *fname, bool only_one);
283 static void process_elf_file (Dwfl_Module *dwflmod, int fd);
284 static void print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr);
285 static void print_shdr (Ebl *ebl, GElf_Ehdr *ehdr);
286 static void print_phdr (Ebl *ebl, GElf_Ehdr *ehdr);
287 static void print_scngrp (Ebl *ebl);
288 static void print_dynamic (Ebl *ebl);
289 static void print_relocs (Ebl *ebl, GElf_Ehdr *ehdr);
290 static void handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
292 static void handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
294 static void print_symtab (Ebl *ebl, int type);
295 static void handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
296 static void print_verinfo (Ebl *ebl);
297 static void handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
298 static void handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr);
299 static void handle_versym (Ebl *ebl, Elf_Scn *scn,
301 static void print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr);
302 static void handle_hash (Ebl *ebl);
303 static void handle_notes (Ebl *ebl, GElf_Ehdr *ehdr);
304 static void print_liblist (Ebl *ebl);
305 static void print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr);
306 static void dump_data (Ebl *ebl);
307 static void dump_strings (Ebl *ebl);
308 static void print_strings (Ebl *ebl);
309 static void dump_archive_index (Elf *, const char *);
312 /* Looked up once with gettext in main. */
313 static char *yes_str;
317 main (int argc, char *argv[])
319 /* We use no threads here which can interfere with handling a stream. */
320 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
323 setlocale (LC_ALL, "");
325 /* Initialize the message catalog. */
326 textdomain (PACKAGE_TARNAME);
329 yes_str = gettext ("yes");
330 no_str = gettext ("yes");
332 /* Parse and process arguments. */
334 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
336 /* Before we start tell the ELF library which version we are using. */
337 elf_version (EV_CURRENT);
339 /* Now process all the files given at the command line. */
340 bool only_one = remaining + 1 == argc;
344 int fd = open (argv[remaining], O_RDONLY);
347 error (0, errno, gettext ("cannot open input file"));
351 process_file (fd, argv[remaining], only_one);
355 while (++remaining < argc);
357 return error_message_count != 0;
361 /* Handle program arguments. */
363 parse_opt (int key, char *arg,
364 struct argp_state *state __attribute__ ((unused)))
366 void add_dump_section (const char *name, bool implicit)
368 struct section_argument *a = xmalloc (sizeof *a);
371 a->implicit = implicit;
372 struct section_argument ***tailp
373 = key == 'x' ? &dump_data_sections_tail : &string_sections_tail;
381 print_file_header = true;
382 print_program_header = true;
383 print_relocations = true;
384 print_section_header = true;
385 print_symbol_table = true;
386 print_version_info = true;
387 print_dynamic_table = true;
388 print_section_groups = true;
389 print_histogram = true;
392 implicit_debug_sections |= section_exception;
393 add_dump_section (".strtab", true);
394 add_dump_section (".dynstr", true);
395 add_dump_section (".comment", true);
396 any_control_option = true;
400 any_control_option = true;
403 print_dynamic_table = true;
404 any_control_option = true;
407 print_debug_sections |= section_exception;
408 any_control_option = true;
411 print_section_groups = true;
412 any_control_option = true;
415 print_file_header = true;
416 any_control_option = true;
419 print_histogram = true;
420 any_control_option = true;
423 print_program_header = true;
424 any_control_option = true;
428 any_control_option = true;
431 print_relocations = true;
432 any_control_option = true;
435 print_section_header = true;
436 any_control_option = true;
439 print_symbol_table = true;
440 any_control_option = true;
441 symbol_table_section = arg;
444 print_version_info = true;
445 any_control_option = true;
448 print_archive_index = true;
453 print_debug_sections = section_all;
454 implicit_debug_sections = section_info;
455 show_split_units = true;
457 else if (strcmp (arg, "abbrev") == 0)
458 print_debug_sections |= section_abbrev;
459 else if (strcmp (arg, "addr") == 0)
461 print_debug_sections |= section_addr;
462 implicit_debug_sections |= section_info;
464 else if (strcmp (arg, "aranges") == 0)
465 print_debug_sections |= section_aranges;
466 else if (strcmp (arg, "decodedaranges") == 0)
468 print_debug_sections |= section_aranges;
469 decodedaranges = true;
471 else if (strcmp (arg, "ranges") == 0)
473 print_debug_sections |= section_ranges;
474 implicit_debug_sections |= section_info;
476 else if (strcmp (arg, "frame") == 0 || strcmp (arg, "frames") == 0)
477 print_debug_sections |= section_frame;
478 else if (strcmp (arg, "info") == 0)
480 print_debug_sections |= section_info;
481 print_debug_sections |= section_types;
483 else if (strcmp (arg, "info+") == 0)
485 print_debug_sections |= section_info;
486 print_debug_sections |= section_types;
487 show_split_units = true;
489 else if (strcmp (arg, "loc") == 0)
491 print_debug_sections |= section_loc;
492 implicit_debug_sections |= section_info;
494 else if (strcmp (arg, "line") == 0)
495 print_debug_sections |= section_line;
496 else if (strcmp (arg, "decodedline") == 0)
498 print_debug_sections |= section_line;
501 else if (strcmp (arg, "pubnames") == 0)
502 print_debug_sections |= section_pubnames;
503 else if (strcmp (arg, "str") == 0)
505 print_debug_sections |= section_str;
506 /* For mapping string offset tables to CUs. */
507 implicit_debug_sections |= section_info;
509 else if (strcmp (arg, "macinfo") == 0)
510 print_debug_sections |= section_macinfo;
511 else if (strcmp (arg, "macro") == 0)
512 print_debug_sections |= section_macro;
513 else if (strcmp (arg, "exception") == 0)
514 print_debug_sections |= section_exception;
515 else if (strcmp (arg, "gdb_index") == 0)
516 print_debug_sections |= section_gdb_index;
519 fprintf (stderr, gettext ("Unknown DWARF debug section `%s'.\n"),
521 argp_help (&argp, stderr, ARGP_HELP_SEE,
522 program_invocation_short_name);
525 any_control_option = true;
528 any_control_option = true;
531 print_string_sections = true;
536 add_dump_section (arg, false);
537 any_control_option = true;
540 print_address_names = false;
543 print_unresolved_addresses = true;
545 case ARGP_KEY_NO_ARGS:
546 fputs (gettext ("Missing file name.\n"), stderr);
549 if (! any_control_option && ! print_archive_index)
551 fputs (gettext ("No operation specified.\n"), stderr);
553 argp_help (&argp, stderr, ARGP_HELP_SEE,
554 program_invocation_short_name);
558 case 'W': /* Ignored. */
561 print_decompress = true;
563 case ELF_INPUT_SECTION:
565 elf_input_section = ".gnu_debugdata";
567 elf_input_section = arg;
570 dwarf_skeleton = arg;
573 return ARGP_ERR_UNKNOWN;
579 /* Create a file descriptor to read the data from the
580 elf_input_section given a file descriptor to an ELF file. */
582 open_input_section (int fd)
587 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
590 error (0, 0, gettext ("cannot generate Elf descriptor: %s"),
595 if (elf_getshdrnum (elf, &shnums) < 0)
597 error (0, 0, gettext ("cannot determine number of sections: %s"),
604 if (elf_getshdrstrndx (elf, &shstrndx) < 0)
606 error (0, 0, gettext ("cannot get section header string table index"));
610 for (cnt = 0; cnt < shnums; ++cnt)
612 Elf_Scn *scn = elf_getscn (elf, cnt);
615 error (0, 0, gettext ("cannot get section: %s"),
621 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
622 if (unlikely (shdr == NULL))
624 error (0, 0, gettext ("cannot get section header: %s"),
629 const char *sname = elf_strptr (elf, shstrndx, shdr->sh_name);
632 error (0, 0, gettext ("cannot get section name"));
636 if (strcmp (sname, elf_input_section) == 0)
638 Elf_Data *data = elf_rawdata (scn, NULL);
641 error (0, 0, gettext ("cannot get %s content: %s"),
642 sname, elf_errmsg (-1));
646 /* Create (and immediately unlink) a temporary file to store
647 section data in to create a file descriptor for it. */
648 const char *tmpdir = getenv ("TMPDIR") ?: P_tmpdir;
649 static const char suffix[] = "/readelfXXXXXX";
650 int tmplen = strlen (tmpdir) + sizeof (suffix);
651 char *tempname = alloca (tmplen);
652 sprintf (tempname, "%s%s", tmpdir, suffix);
654 int sfd = mkstemp (tempname);
657 error (0, 0, gettext ("cannot create temp file '%s'"),
663 ssize_t size = data->d_size;
664 if (write_retry (sfd, data->d_buf, size) != size)
666 error (0, 0, gettext ("cannot write section data"));
670 if (elf_end (elf) != 0)
672 error (0, 0, gettext ("error while closing Elf descriptor: %s"),
677 if (lseek (sfd, 0, SEEK_SET) == -1)
679 error (0, 0, gettext ("error while rewinding file descriptor"));
687 /* Named section not found. */
688 if (elf_end (elf) != 0)
689 error (0, 0, gettext ("error while closing Elf descriptor: %s"),
694 /* Check if the file is an archive, and if so dump its index. */
696 check_archive_index (int fd, const char *fname, bool only_one)
698 /* Create an `Elf' descriptor. */
699 Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
701 error (0, 0, gettext ("cannot generate Elf descriptor: %s"),
705 if (elf_kind (elf) == ELF_K_AR)
708 printf ("\n%s:\n\n", fname);
709 dump_archive_index (elf, fname);
713 gettext ("'%s' is not an archive, cannot print archive index"),
716 /* Now we can close the descriptor. */
717 if (elf_end (elf) != 0)
718 error (0, 0, gettext ("error while closing Elf descriptor: %s"),
723 /* Trivial callback used for checking if we opened an archive. */
725 count_dwflmod (Dwfl_Module *dwflmod __attribute__ ((unused)),
726 void **userdata __attribute__ ((unused)),
727 const char *name __attribute__ ((unused)),
728 Dwarf_Addr base __attribute__ ((unused)),
732 return DWARF_CB_ABORT;
733 *(bool *) arg = true;
737 struct process_dwflmod_args
744 process_dwflmod (Dwfl_Module *dwflmod,
745 void **userdata __attribute__ ((unused)),
746 const char *name __attribute__ ((unused)),
747 Dwarf_Addr base __attribute__ ((unused)),
750 const struct process_dwflmod_args *a = arg;
752 /* Print the file name. */
756 dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL, &fname, NULL);
758 printf ("\n%s:\n\n", fname);
761 process_elf_file (dwflmod, a->fd);
766 /* Stub libdwfl callback, only the ELF handle already open is ever used.
767 Only used for finding the alternate debug file if the Dwarf comes from
768 the main file. We are not interested in separate debuginfo. */
770 find_no_debuginfo (Dwfl_Module *mod,
774 const char *file_name,
775 const char *debuglink_file,
776 GElf_Word debuglink_crc,
777 char **debuginfo_file_name)
780 dwfl_module_info (mod, NULL, NULL, NULL, &dwbias, NULL, NULL, NULL);
782 /* We are only interested if the Dwarf has been setup on the main
783 elf file but is only missing the alternate debug link. If dwbias
784 hasn't even been setup, this is searching for separate debuginfo
785 for the main elf. We don't care in that case. */
786 if (dwbias == (Dwarf_Addr) -1)
789 return dwfl_standard_find_debuginfo (mod, userdata, modname, base,
790 file_name, debuglink_file,
791 debuglink_crc, debuginfo_file_name);
795 create_dwfl (int fd, const char *fname)
797 /* Duplicate an fd for dwfl_report_offline to swallow. */
798 int dwfl_fd = dup (fd);
799 if (unlikely (dwfl_fd < 0))
800 error (EXIT_FAILURE, errno, "dup");
802 /* Use libdwfl in a trivial way to open the libdw handle for us.
803 This takes care of applying relocations to DWARF data in ET_REL files. */
804 static const Dwfl_Callbacks callbacks =
806 .section_address = dwfl_offline_section_address,
807 .find_debuginfo = find_no_debuginfo
809 Dwfl *dwfl = dwfl_begin (&callbacks);
810 if (likely (dwfl != NULL))
811 /* Let 0 be the logical address of the file (or first in archive). */
812 dwfl->offline_next_address = 0;
813 if (dwfl_report_offline (dwfl, fname, fname, dwfl_fd) == NULL)
816 if (fstat (dwfl_fd, &st) != 0)
817 error (0, errno, gettext ("cannot stat input file"));
818 else if (unlikely (st.st_size == 0))
819 error (0, 0, gettext ("input file is empty"));
821 error (0, 0, gettext ("failed reading '%s': %s"),
822 fname, dwfl_errmsg (-1));
823 close (dwfl_fd); /* Consumed on success, not on failure. */
827 dwfl_report_end (dwfl, NULL, NULL);
832 /* Process one input file. */
834 process_file (int fd, const char *fname, bool only_one)
836 if (print_archive_index)
837 check_archive_index (fd, fname, only_one);
839 if (!any_control_option)
842 if (elf_input_section != NULL)
844 /* Replace fname and fd with section content. */
845 char *fnname = alloca (strlen (fname) + strlen (elf_input_section) + 2);
846 sprintf (fnname, "%s:%s", fname, elf_input_section);
847 fd = open_input_section (fd);
850 error (0, 0, gettext ("No such section '%s' in '%s'"),
851 elf_input_section, fname);
857 Dwfl *dwfl = create_dwfl (fd, fname);
862 /* Clear ONLY_ONE if we have multiple modules, from an archive. */
864 only_one = dwfl_getmodules (dwfl, &count_dwflmod, &seen, 0) == 0;
867 /* Process the one or more modules gleaned from this file. */
868 struct process_dwflmod_args a = { .fd = fd, .only_one = only_one };
869 dwfl_getmodules (dwfl, &process_dwflmod, &a, 0);
871 /* Terrible hack for hooking unrelated skeleton/split compile units,
872 see __libdw_link_skel_split in print_debug. */
873 if (! do_not_close_dwfl)
876 /* Need to close the replaced fd if we created it. Caller takes
878 if (elf_input_section != NULL)
882 /* Check whether there are any compressed sections in the ELF file. */
884 elf_contains_chdrs (Elf *elf)
887 while ((scn = elf_nextscn (elf, scn)) != NULL)
890 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
891 if (shdr != NULL && (shdr->sh_flags & SHF_COMPRESSED) != 0)
897 /* Process one ELF file. */
899 process_elf_file (Dwfl_Module *dwflmod, int fd)
902 Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
905 GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
910 error (0, 0, gettext ("cannot read ELF header: %s"), elf_errmsg (-1));
914 Ebl *ebl = ebl_openbackend (elf);
915 if (unlikely (ebl == NULL))
918 error (0, errno, gettext ("cannot create EBL handle"));
922 /* Determine the number of sections. */
923 if (unlikely (elf_getshdrnum (ebl->elf, &shnum) < 0))
924 error (EXIT_FAILURE, 0,
925 gettext ("cannot determine number of sections: %s"),
928 /* Determine the number of phdrs. */
929 if (unlikely (elf_getphdrnum (ebl->elf, &phnum) < 0))
930 error (EXIT_FAILURE, 0,
931 gettext ("cannot determine number of program headers: %s"),
934 /* For an ET_REL file, libdwfl has adjusted the in-core shdrs and
935 may have applied relocation to some sections. If there are any
936 compressed sections, any pass (or libdw/libdwfl) might have
937 uncompressed them. So we need to get a fresh Elf handle on the
938 file to display those. */
939 bool print_unchanged = ((print_section_header
941 || dump_data_sections != NULL
943 && (ehdr->e_type == ET_REL
944 || elf_contains_chdrs (ebl->elf)));
946 Elf *pure_elf = NULL;
950 /* Read the file afresh. */
951 off_t aroff = elf_getaroff (elf);
952 pure_elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
955 /* Archive member. */
956 (void) elf_rand (pure_elf, aroff);
957 Elf *armem = elf_begin (-1, ELF_C_READ_MMAP, pure_elf);
961 if (pure_elf == NULL)
963 pure_ebl = ebl_openbackend (pure_elf);
964 if (pure_ebl == NULL)
968 if (print_file_header)
969 print_ehdr (ebl, ehdr);
970 if (print_section_header)
971 print_shdr (pure_ebl, ehdr);
972 if (print_program_header)
973 print_phdr (ebl, ehdr);
974 if (print_section_groups)
976 if (print_dynamic_table)
978 if (print_relocations)
979 print_relocs (pure_ebl, ehdr);
982 if (print_symbol_table)
983 print_symtab (ebl, SHT_DYNSYM);
984 if (print_version_info)
986 if (print_symbol_table)
987 print_symtab (ebl, SHT_SYMTAB);
991 print_attributes (ebl, ehdr);
992 if (dump_data_sections != NULL)
993 dump_data (pure_ebl);
994 if (string_sections != NULL)
996 if ((print_debug_sections | implicit_debug_sections) != 0)
997 print_debug (dwflmod, ebl, ehdr);
999 handle_notes (pure_ebl, ehdr);
1000 if (print_string_sections)
1001 print_strings (ebl);
1003 ebl_closebackend (ebl);
1005 if (pure_ebl != ebl)
1007 ebl_closebackend (pure_ebl);
1013 /* Print file type. */
1015 print_file_type (unsigned short int e_type)
1017 if (likely (e_type <= ET_CORE))
1019 static const char *const knowntypes[] =
1022 N_("REL (Relocatable file)"),
1023 N_("EXEC (Executable file)"),
1024 N_("DYN (Shared object file)"),
1025 N_("CORE (Core file)")
1027 puts (gettext (knowntypes[e_type]));
1029 else if (e_type >= ET_LOOS && e_type <= ET_HIOS)
1030 printf (gettext ("OS Specific: (%x)\n"), e_type);
1031 else if (e_type >= ET_LOPROC /* && e_type <= ET_HIPROC always true */)
1032 printf (gettext ("Processor Specific: (%x)\n"), e_type);
1038 /* Print ELF header. */
1040 print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
1042 fputs_unlocked (gettext ("ELF Header:\n Magic: "), stdout);
1043 for (size_t cnt = 0; cnt < EI_NIDENT; ++cnt)
1044 printf (" %02hhx", ehdr->e_ident[cnt]);
1046 printf (gettext ("\n Class: %s\n"),
1047 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? "ELF32"
1048 : ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? "ELF64"
1051 printf (gettext (" Data: %s\n"),
1052 ehdr->e_ident[EI_DATA] == ELFDATA2LSB
1053 ? "2's complement, little endian"
1054 : ehdr->e_ident[EI_DATA] == ELFDATA2MSB
1055 ? "2's complement, big endian" : "\?\?\?");
1057 printf (gettext (" Ident Version: %hhd %s\n"),
1058 ehdr->e_ident[EI_VERSION],
1059 ehdr->e_ident[EI_VERSION] == EV_CURRENT ? gettext ("(current)")
1063 printf (gettext (" OS/ABI: %s\n"),
1064 ebl_osabi_name (ebl, ehdr->e_ident[EI_OSABI], buf, sizeof (buf)));
1066 printf (gettext (" ABI Version: %hhd\n"),
1067 ehdr->e_ident[EI_ABIVERSION]);
1069 fputs_unlocked (gettext (" Type: "), stdout);
1070 print_file_type (ehdr->e_type);
1072 printf (gettext (" Machine: %s\n"), ebl->name);
1074 printf (gettext (" Version: %d %s\n"),
1076 ehdr->e_version == EV_CURRENT ? gettext ("(current)") : "(\?\?\?)");
1078 printf (gettext (" Entry point address: %#" PRIx64 "\n"),
1081 printf (gettext (" Start of program headers: %" PRId64 " %s\n"),
1082 ehdr->e_phoff, gettext ("(bytes into file)"));
1084 printf (gettext (" Start of section headers: %" PRId64 " %s\n"),
1085 ehdr->e_shoff, gettext ("(bytes into file)"));
1087 printf (gettext (" Flags: %s\n"),
1088 ebl_machine_flag_name (ebl, ehdr->e_flags, buf, sizeof (buf)));
1090 printf (gettext (" Size of this header: %" PRId16 " %s\n"),
1091 ehdr->e_ehsize, gettext ("(bytes)"));
1093 printf (gettext (" Size of program header entries: %" PRId16 " %s\n"),
1094 ehdr->e_phentsize, gettext ("(bytes)"));
1096 printf (gettext (" Number of program headers entries: %" PRId16),
1098 if (ehdr->e_phnum == PN_XNUM)
1101 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1103 printf (gettext (" (%" PRIu32 " in [0].sh_info)"),
1104 (uint32_t) shdr->sh_info);
1106 fputs_unlocked (gettext (" ([0] not available)"), stdout);
1108 fputc_unlocked ('\n', stdout);
1110 printf (gettext (" Size of section header entries: %" PRId16 " %s\n"),
1111 ehdr->e_shentsize, gettext ("(bytes)"));
1113 printf (gettext (" Number of section headers entries: %" PRId16),
1115 if (ehdr->e_shnum == 0)
1118 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1120 printf (gettext (" (%" PRIu32 " in [0].sh_size)"),
1121 (uint32_t) shdr->sh_size);
1123 fputs_unlocked (gettext (" ([0] not available)"), stdout);
1125 fputc_unlocked ('\n', stdout);
1127 if (unlikely (ehdr->e_shstrndx == SHN_XINDEX))
1130 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (ebl->elf, 0), &shdr_mem);
1132 /* We managed to get the zeroth section. */
1133 snprintf (buf, sizeof (buf), gettext (" (%" PRIu32 " in [0].sh_link)"),
1134 (uint32_t) shdr->sh_link);
1137 strncpy (buf, gettext (" ([0] not available)"), sizeof (buf));
1138 buf[sizeof (buf) - 1] = '\0';
1141 printf (gettext (" Section header string table index: XINDEX%s\n\n"),
1145 printf (gettext (" Section header string table index: %" PRId16 "\n\n"),
1151 get_visibility_type (int value)
1169 elf_ch_type_name (unsigned int code)
1174 if (code == ELFCOMPRESS_ZLIB)
1180 /* Print the section headers. */
1182 print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
1187 if (! print_file_header)
1189 There are %d section headers, starting at offset %#" PRIx64 ":\n\
1191 ehdr->e_shnum, ehdr->e_shoff);
1193 /* Get the section header string table index. */
1194 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1195 error (EXIT_FAILURE, 0,
1196 gettext ("cannot get section header string table index"));
1198 puts (gettext ("Section Headers:"));
1200 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1201 puts (gettext ("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1203 puts (gettext ("[Nr] Name Type Addr Off Size ES Flags Lk Inf Al"));
1205 if (print_decompress)
1207 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1208 puts (gettext (" [Compression Size Al]"));
1210 puts (gettext (" [Compression Size Al]"));
1213 for (cnt = 0; cnt < shnum; ++cnt)
1215 Elf_Scn *scn = elf_getscn (ebl->elf, cnt);
1217 if (unlikely (scn == NULL))
1218 error (EXIT_FAILURE, 0, gettext ("cannot get section: %s"),
1221 /* Get the section header. */
1223 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1224 if (unlikely (shdr == NULL))
1225 error (EXIT_FAILURE, 0, gettext ("cannot get section header: %s"),
1230 if (shdr->sh_flags & SHF_WRITE)
1232 if (shdr->sh_flags & SHF_ALLOC)
1234 if (shdr->sh_flags & SHF_EXECINSTR)
1236 if (shdr->sh_flags & SHF_MERGE)
1238 if (shdr->sh_flags & SHF_STRINGS)
1240 if (shdr->sh_flags & SHF_INFO_LINK)
1242 if (shdr->sh_flags & SHF_LINK_ORDER)
1244 if (shdr->sh_flags & SHF_OS_NONCONFORMING)
1246 if (shdr->sh_flags & SHF_GROUP)
1248 if (shdr->sh_flags & SHF_TLS)
1250 if (shdr->sh_flags & SHF_COMPRESSED)
1252 if (shdr->sh_flags & SHF_ORDERED)
1254 if (shdr->sh_flags & SHF_EXCLUDE)
1260 sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "<corrupt>";
1261 printf ("[%2zu] %-20s %-12s %0*" PRIx64 " %0*" PRIx64 " %0*" PRIx64
1262 " %2" PRId64 " %-5s %2" PRId32 " %3" PRId32
1265 ebl_section_type_name (ebl, shdr->sh_type, buf, sizeof (buf)),
1266 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, shdr->sh_addr,
1267 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_offset,
1268 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, shdr->sh_size,
1269 shdr->sh_entsize, flagbuf, shdr->sh_link, shdr->sh_info,
1270 shdr->sh_addralign);
1272 if (print_decompress)
1274 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1277 if (gelf_getchdr (scn, &chdr) != NULL)
1278 printf (" [ELF %s (%" PRId32 ") %0*" PRIx64
1280 elf_ch_type_name (chdr.ch_type),
1282 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8,
1283 chdr.ch_size, chdr.ch_addralign);
1286 gettext ("bad compression header for section %zd: %s"),
1287 elf_ndxscn (scn), elf_errmsg (-1));
1289 else if (strncmp(".zdebug", sname, strlen (".zdebug")) == 0)
1292 if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
1293 printf (" [GNU ZLIB %0*zx ]\n",
1294 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 6 : 8, size);
1297 gettext ("bad gnu compressed size for section %zd: %s"),
1298 elf_ndxscn (scn), elf_errmsg (-1));
1303 fputc_unlocked ('\n', stdout);
1307 /* Print the program header. */
1309 print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
1312 /* No program header, this is OK in relocatable objects. */
1315 puts (gettext ("Program Headers:"));
1316 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1318 Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1321 Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align"));
1323 /* Process all program headers. */
1324 bool has_relro = false;
1325 GElf_Addr relro_from = 0;
1326 GElf_Addr relro_to = 0;
1327 for (size_t cnt = 0; cnt < phnum; ++cnt)
1331 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
1333 /* If for some reason the header cannot be returned show this. */
1334 if (unlikely (phdr == NULL))
1340 printf (" %-14s 0x%06" PRIx64 " 0x%0*" PRIx64 " 0x%0*" PRIx64
1341 " 0x%06" PRIx64 " 0x%06" PRIx64 " %c%c%c 0x%" PRIx64 "\n",
1342 ebl_segment_type_name (ebl, phdr->p_type, buf, sizeof (buf)),
1344 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_vaddr,
1345 ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16, phdr->p_paddr,
1348 phdr->p_flags & PF_R ? 'R' : ' ',
1349 phdr->p_flags & PF_W ? 'W' : ' ',
1350 phdr->p_flags & PF_X ? 'E' : ' ',
1353 if (phdr->p_type == PT_INTERP)
1355 /* If we are sure the file offset is valid then we can show
1356 the user the name of the interpreter. We check whether
1357 there is a section at the file offset. Normally there
1358 would be a section called ".interp". But in separate
1359 .debug files it is a NOBITS section (and so doesn't match
1360 with gelf_offscn). Which probably means the offset is
1361 not valid another reason could be because the ELF file
1362 just doesn't contain any section headers, in that case
1363 just play it safe and don't display anything. */
1365 Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1367 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1370 char *filedata = elf_rawfile (ebl->elf, &maxsize);
1372 if (shdr != NULL && shdr->sh_type == SHT_PROGBITS
1373 && filedata != NULL && phdr->p_offset < maxsize
1374 && phdr->p_filesz <= maxsize - phdr->p_offset
1375 && memchr (filedata + phdr->p_offset, '\0',
1376 phdr->p_filesz) != NULL)
1377 printf (gettext ("\t[Requesting program interpreter: %s]\n"),
1378 filedata + phdr->p_offset);
1380 else if (phdr->p_type == PT_GNU_RELRO)
1383 relro_from = phdr->p_vaddr;
1384 relro_to = relro_from + phdr->p_memsz;
1388 if (ehdr->e_shnum == 0)
1389 /* No sections in the file. Punt. */
1392 /* Get the section header string table index. */
1394 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1395 error (EXIT_FAILURE, 0,
1396 gettext ("cannot get section header string table index"));
1398 puts (gettext ("\n Section to Segment mapping:\n Segment Sections..."));
1400 for (size_t cnt = 0; cnt < phnum; ++cnt)
1402 /* Print the segment number. */
1403 printf (" %2.2zu ", cnt);
1406 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &phdr_mem);
1407 /* This must not happen. */
1408 if (unlikely (phdr == NULL))
1409 error (EXIT_FAILURE, 0, gettext ("cannot get program header: %s"),
1412 /* Iterate over the sections. */
1413 bool in_relro = false;
1415 for (size_t inner = 1; inner < shnum; ++inner)
1417 Elf_Scn *scn = elf_getscn (ebl->elf, inner);
1418 /* This should not happen. */
1419 if (unlikely (scn == NULL))
1420 error (EXIT_FAILURE, 0, gettext ("cannot get section: %s"),
1423 /* Get the section header. */
1425 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1426 if (unlikely (shdr == NULL))
1427 error (EXIT_FAILURE, 0,
1428 gettext ("cannot get section header: %s"),
1431 if (shdr->sh_size > 0
1432 /* Compare allocated sections by VMA, unallocated
1433 sections by file offset. */
1434 && (shdr->sh_flags & SHF_ALLOC
1435 ? (shdr->sh_addr >= phdr->p_vaddr
1436 && (shdr->sh_addr + shdr->sh_size
1437 <= phdr->p_vaddr + phdr->p_memsz))
1438 : (shdr->sh_offset >= phdr->p_offset
1439 && (shdr->sh_offset + shdr->sh_size
1440 <= phdr->p_offset + phdr->p_filesz))))
1442 if (has_relro && !in_relro
1443 && shdr->sh_addr >= relro_from
1444 && shdr->sh_addr + shdr->sh_size <= relro_to)
1446 fputs_unlocked (" [RELRO:", stdout);
1449 else if (has_relro && in_relro && shdr->sh_addr >= relro_to)
1451 fputs_unlocked ("]", stdout);
1454 else if (has_relro && in_relro
1455 && shdr->sh_addr + shdr->sh_size > relro_to)
1456 fputs_unlocked ("] <RELRO:", stdout);
1457 else if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_W) == 0)
1461 fputs_unlocked (" [RO:", stdout);
1467 /* Determine the segment this section is part of. */
1469 GElf_Phdr phdr2_mem;
1470 GElf_Phdr *phdr2 = NULL;
1471 for (cnt2 = 0; cnt2 < phnum; ++cnt2)
1473 phdr2 = gelf_getphdr (ebl->elf, cnt2, &phdr2_mem);
1475 if (phdr2 != NULL && phdr2->p_type == PT_LOAD
1476 && shdr->sh_addr >= phdr2->p_vaddr
1477 && (shdr->sh_addr + shdr->sh_size
1478 <= phdr2->p_vaddr + phdr2->p_memsz))
1484 if ((phdr2->p_flags & PF_W) == 0 && !in_ro)
1486 fputs_unlocked (" [RO:", stdout);
1489 else if ((phdr2->p_flags & PF_W) != 0 && in_ro)
1491 fputs_unlocked ("]", stdout);
1498 elf_strptr (ebl->elf, shstrndx, shdr->sh_name));
1500 /* Signal that this sectin is only partially covered. */
1501 if (has_relro && in_relro
1502 && shdr->sh_addr + shdr->sh_size > relro_to)
1504 fputs_unlocked (">", stdout);
1509 if (in_relro || in_ro)
1510 fputs_unlocked ("]", stdout);
1512 /* Finish the line. */
1513 fputc_unlocked ('\n', stdout);
1519 section_name (Ebl *ebl, GElf_Ehdr *ehdr, GElf_Shdr *shdr)
1521 return elf_strptr (ebl->elf, ehdr->e_shstrndx, shdr->sh_name) ?: "???";
1526 handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1528 /* Get the data of the section. */
1529 Elf_Data *data = elf_getdata (scn, NULL);
1531 Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1532 GElf_Shdr symshdr_mem;
1533 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1534 Elf_Data *symdata = elf_getdata (symscn, NULL);
1536 if (data == NULL || data->d_size < sizeof (Elf32_Word) || symshdr == NULL
1540 /* Get the section header string table index. */
1542 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1543 error (EXIT_FAILURE, 0,
1544 gettext ("cannot get section header string table index"));
1546 Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
1549 GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1551 printf ((grpref[0] & GRP_COMDAT)
1553 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
1555 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1556 data->d_size / sizeof (Elf32_Word) - 1)
1558 \nSection group [%2zu] '%s' with signature '%s' contains %zu entry:\n", "\
1559 \nSection group [%2zu] '%s' with signature '%s' contains %zu entries:\n",
1560 data->d_size / sizeof (Elf32_Word) - 1),
1562 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1564 : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
1565 ?: gettext ("<INVALID SYMBOL>"),
1566 data->d_size / sizeof (Elf32_Word) - 1);
1568 for (size_t cnt = 1; cnt < data->d_size / sizeof (Elf32_Word); ++cnt)
1570 GElf_Shdr grpshdr_mem;
1571 GElf_Shdr *grpshdr = gelf_getshdr (elf_getscn (ebl->elf, grpref[cnt]),
1575 printf (" [%2u] %s\n",
1578 && (str = elf_strptr (ebl->elf, shstrndx, grpshdr->sh_name))
1579 ? str : gettext ("<INVALID SECTION>"));
1585 print_scngrp (Ebl *ebl)
1587 /* Find all relocation sections and handle them. */
1588 Elf_Scn *scn = NULL;
1590 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1592 /* Handle the section if it is a symbol table. */
1594 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1596 if (shdr != NULL && shdr->sh_type == SHT_GROUP)
1598 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
1600 if (elf_compress (scn, 0, 0) < 0)
1601 printf ("WARNING: %s [%zd]\n",
1602 gettext ("Couldn't uncompress section"),
1604 shdr = gelf_getshdr (scn, &shdr_mem);
1605 if (unlikely (shdr == NULL))
1606 error (EXIT_FAILURE, 0,
1607 gettext ("cannot get section [%zd] header: %s"),
1611 handle_scngrp (ebl, scn, shdr);
1617 static const struct flags
1623 { DF_ORIGIN, "ORIGIN" },
1624 { DF_SYMBOLIC, "SYMBOLIC" },
1625 { DF_TEXTREL, "TEXTREL" },
1626 { DF_BIND_NOW, "BIND_NOW" },
1627 { DF_STATIC_TLS, "STATIC_TLS" }
1629 static const int ndt_flags = sizeof (dt_flags) / sizeof (dt_flags[0]);
1631 static const struct flags dt_flags_1[] =
1633 { DF_1_NOW, "NOW" },
1634 { DF_1_GLOBAL, "GLOBAL" },
1635 { DF_1_GROUP, "GROUP" },
1636 { DF_1_NODELETE, "NODELETE" },
1637 { DF_1_LOADFLTR, "LOADFLTR" },
1638 { DF_1_INITFIRST, "INITFIRST" },
1639 { DF_1_NOOPEN, "NOOPEN" },
1640 { DF_1_ORIGIN, "ORIGIN" },
1641 { DF_1_DIRECT, "DIRECT" },
1642 { DF_1_TRANS, "TRANS" },
1643 { DF_1_INTERPOSE, "INTERPOSE" },
1644 { DF_1_NODEFLIB, "NODEFLIB" },
1645 { DF_1_NODUMP, "NODUMP" },
1646 { DF_1_CONFALT, "CONFALT" },
1647 { DF_1_ENDFILTEE, "ENDFILTEE" },
1648 { DF_1_DISPRELDNE, "DISPRELDNE" },
1649 { DF_1_DISPRELPND, "DISPRELPND" },
1651 static const int ndt_flags_1 = sizeof (dt_flags_1) / sizeof (dt_flags_1[0]);
1653 static const struct flags dt_feature_1[] =
1655 { DTF_1_PARINIT, "PARINIT" },
1656 { DTF_1_CONFEXP, "CONFEXP" }
1658 static const int ndt_feature_1 = (sizeof (dt_feature_1)
1659 / sizeof (dt_feature_1[0]));
1661 static const struct flags dt_posflag_1[] =
1663 { DF_P1_LAZYLOAD, "LAZYLOAD" },
1664 { DF_P1_GROUPPERM, "GROUPPERM" }
1666 static const int ndt_posflag_1 = (sizeof (dt_posflag_1)
1667 / sizeof (dt_posflag_1[0]));
1671 print_flags (int class, GElf_Xword d_val, const struct flags *flags,
1677 for (cnt = 0; cnt < nflags; ++cnt)
1678 if (d_val & flags[cnt].mask)
1681 putchar_unlocked (' ');
1682 fputs_unlocked (flags[cnt].str, stdout);
1683 d_val &= ~flags[cnt].mask;
1690 putchar_unlocked (' ');
1691 printf ("%#0*" PRIx64, class == ELFCLASS32 ? 10 : 18, d_val);
1694 putchar_unlocked ('\n');
1699 print_dt_flags (int class, GElf_Xword d_val)
1701 print_flags (class, d_val, dt_flags, ndt_flags);
1706 print_dt_flags_1 (int class, GElf_Xword d_val)
1708 print_flags (class, d_val, dt_flags_1, ndt_flags_1);
1713 print_dt_feature_1 (int class, GElf_Xword d_val)
1715 print_flags (class, d_val, dt_feature_1, ndt_feature_1);
1720 print_dt_posflag_1 (int class, GElf_Xword d_val)
1722 print_flags (class, d_val, dt_posflag_1, ndt_posflag_1);
1727 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
1729 int class = gelf_getclass (ebl->elf);
1730 GElf_Shdr glink_mem;
1737 /* Get the data of the section. */
1738 data = elf_getdata (scn, NULL);
1742 /* Get the section header string table index. */
1743 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1744 error (EXIT_FAILURE, 0,
1745 gettext ("cannot get section header string table index"));
1747 sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
1749 glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
1751 error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
1754 printf (ngettext ("\
1755 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1757 \nDynamic segment contains %lu entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
1758 shdr->sh_size / sh_entsize),
1759 (unsigned long int) (shdr->sh_size / sh_entsize),
1760 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
1762 (int) shdr->sh_link,
1763 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
1764 fputs_unlocked (gettext (" Type Value\n"), stdout);
1766 for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
1769 GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
1775 ebl_dynamic_tag_name (ebl, dyn->d_tag, buf, sizeof (buf)));
1783 /* No further output. */
1784 fputc_unlocked ('\n', stdout);
1788 printf (gettext ("Shared library: [%s]\n"),
1789 elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1793 printf (gettext ("Library soname: [%s]\n"),
1794 elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1798 printf (gettext ("Library rpath: [%s]\n"),
1799 elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1803 printf (gettext ("Library runpath: [%s]\n"),
1804 elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val));
1817 case DT_INIT_ARRAYSZ:
1818 case DT_FINI_ARRAYSZ:
1821 case DT_GNU_CONFLICTSZ:
1822 case DT_GNU_LIBLISTSZ:
1823 printf (gettext ("%" PRId64 " (bytes)\n"), dyn->d_un.d_val);
1830 printf ("%" PRId64 "\n", dyn->d_un.d_val);
1834 const char *tagname = ebl_dynamic_tag_name (ebl, dyn->d_un.d_val,
1836 puts (tagname ?: "???");
1840 print_dt_flags (class, dyn->d_un.d_val);
1844 print_dt_flags_1 (class, dyn->d_un.d_val);
1848 print_dt_feature_1 (class, dyn->d_un.d_val);
1852 print_dt_posflag_1 (class, dyn->d_un.d_val);
1856 printf ("%#0*" PRIx64 "\n",
1857 class == ELFCLASS32 ? 10 : 18, dyn->d_un.d_val);
1864 /* Print the dynamic segment. */
1866 print_dynamic (Ebl *ebl)
1868 for (size_t i = 0; i < phnum; ++i)
1871 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, i, &phdr_mem);
1873 if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
1875 Elf_Scn *scn = gelf_offscn (ebl->elf, phdr->p_offset);
1877 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1878 if (shdr != NULL && shdr->sh_type == SHT_DYNAMIC)
1879 handle_dynamic (ebl, scn, shdr);
1886 /* Print relocations. */
1888 print_relocs (Ebl *ebl, GElf_Ehdr *ehdr)
1890 /* Find all relocation sections and handle them. */
1891 Elf_Scn *scn = NULL;
1893 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
1895 /* Handle the section if it is a symbol table. */
1897 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1899 if (likely (shdr != NULL))
1901 if (shdr->sh_type == SHT_REL)
1902 handle_relocs_rel (ebl, ehdr, scn, shdr);
1903 else if (shdr->sh_type == SHT_RELA)
1904 handle_relocs_rela (ebl, ehdr, scn, shdr);
1910 /* Handle a relocation section. */
1912 handle_relocs_rel (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
1914 int class = gelf_getclass (ebl->elf);
1915 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
1916 int nentries = shdr->sh_size / sh_entsize;
1918 /* Get the data of the section. */
1919 Elf_Data *data = elf_getdata (scn, NULL);
1923 /* Get the symbol table information. */
1924 Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
1925 GElf_Shdr symshdr_mem;
1926 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1927 Elf_Data *symdata = elf_getdata (symscn, NULL);
1929 /* Get the section header of the section the relocations are for. */
1930 GElf_Shdr destshdr_mem;
1931 GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
1934 if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
1936 printf (gettext ("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
1941 /* Search for the optional extended section index table. */
1942 Elf_Data *xndxdata = NULL;
1943 int xndxscnidx = elf_scnshndx (scn);
1944 if (unlikely (xndxscnidx > 0))
1945 xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
1947 /* Get the section header string table index. */
1949 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
1950 error (EXIT_FAILURE, 0,
1951 gettext ("cannot get section header string table index"));
1953 if (shdr->sh_info != 0)
1954 printf (ngettext ("\
1955 \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
1957 \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
1960 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1961 (unsigned int) shdr->sh_info,
1962 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
1966 /* The .rel.dyn section does not refer to a specific section but
1967 instead of section index zero. Do not try to print a section
1969 printf (ngettext ("\
1970 \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
1972 \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
1974 (unsigned int) elf_ndxscn (scn),
1975 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
1978 fputs_unlocked (class == ELFCLASS32
1980 Offset Type Value Name\n")
1982 Offset Type Value Name\n"),
1985 int is_statically_linked = 0;
1986 for (int cnt = 0; cnt < nentries; ++cnt)
1989 GElf_Rel *rel = gelf_getrel (data, cnt, &relmem);
1990 if (likely (rel != NULL))
1995 GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
1996 GELF_R_SYM (rel->r_info),
1998 if (unlikely (sym == NULL))
2000 /* As a special case we have to handle relocations in static
2001 executables. This only happens for IRELATIVE relocations
2002 (so far). There is no symbol table. */
2003 if (is_statically_linked == 0)
2005 /* Find the program header and look for a PT_INTERP entry. */
2006 is_statically_linked = -1;
2007 if (ehdr->e_type == ET_EXEC)
2009 is_statically_linked = 1;
2011 for (size_t inner = 0; inner < phnum; ++inner)
2014 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2016 if (phdr != NULL && phdr->p_type == PT_INTERP)
2018 is_statically_linked = -1;
2025 if (is_statically_linked > 0 && shdr->sh_link == 0)
2027 %#0*" PRIx64 " %-20s %*s %s\n",
2028 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2029 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2030 /* Avoid the leading R_ which isn't carrying any
2032 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2033 buf, sizeof (buf)) + 2
2034 : gettext ("<INVALID RELOC>"),
2035 class == ELFCLASS32 ? 10 : 18, "",
2036 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2038 printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2039 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2040 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2041 /* Avoid the leading R_ which isn't carrying any
2043 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2044 buf, sizeof (buf)) + 2
2045 : gettext ("<INVALID RELOC>"),
2046 gettext ("INVALID SYMBOL"),
2047 (long int) GELF_R_SYM (rel->r_info));
2049 else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2050 printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2051 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2052 likely (ebl_reloc_type_check (ebl,
2053 GELF_R_TYPE (rel->r_info)))
2054 /* Avoid the leading R_ which isn't carrying any
2056 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2057 buf, sizeof (buf)) + 2
2058 : gettext ("<INVALID RELOC>"),
2059 class == ELFCLASS32 ? 10 : 18, sym->st_value,
2060 elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2063 /* This is a relocation against a STT_SECTION symbol. */
2064 GElf_Shdr secshdr_mem;
2066 secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2067 sym->st_shndx == SHN_XINDEX
2068 ? xndx : sym->st_shndx),
2071 if (unlikely (secshdr == NULL))
2072 printf (" %#0*" PRIx64 " %-20s <%s %ld>\n",
2073 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2074 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2075 /* Avoid the leading R_ which isn't carrying any
2077 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2078 buf, sizeof (buf)) + 2
2079 : gettext ("<INVALID RELOC>"),
2080 gettext ("INVALID SECTION"),
2081 (long int) (sym->st_shndx == SHN_XINDEX
2082 ? xndx : sym->st_shndx));
2084 printf (" %#0*" PRIx64 " %-20s %#0*" PRIx64 " %s\n",
2085 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2086 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2087 /* Avoid the leading R_ which isn't carrying any
2089 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2090 buf, sizeof (buf)) + 2
2091 : gettext ("<INVALID RELOC>"),
2092 class == ELFCLASS32 ? 10 : 18, sym->st_value,
2093 elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2100 /* Handle a relocation section. */
2102 handle_relocs_rela (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, GElf_Shdr *shdr)
2104 int class = gelf_getclass (ebl->elf);
2105 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
2106 int nentries = shdr->sh_size / sh_entsize;
2108 /* Get the data of the section. */
2109 Elf_Data *data = elf_getdata (scn, NULL);
2113 /* Get the symbol table information. */
2114 Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link);
2115 GElf_Shdr symshdr_mem;
2116 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
2117 Elf_Data *symdata = elf_getdata (symscn, NULL);
2119 /* Get the section header of the section the relocations are for. */
2120 GElf_Shdr destshdr_mem;
2121 GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_info),
2124 if (unlikely (symshdr == NULL || symdata == NULL || destshdr == NULL))
2126 printf (gettext ("\nInvalid symbol table at offset %#0" PRIx64 "\n"),
2131 /* Search for the optional extended section index table. */
2132 Elf_Data *xndxdata = NULL;
2133 int xndxscnidx = elf_scnshndx (scn);
2134 if (unlikely (xndxscnidx > 0))
2135 xndxdata = elf_getdata (elf_getscn (ebl->elf, xndxscnidx), NULL);
2137 /* Get the section header string table index. */
2139 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2140 error (EXIT_FAILURE, 0,
2141 gettext ("cannot get section header string table index"));
2143 if (shdr->sh_info != 0)
2144 printf (ngettext ("\
2145 \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2147 \nRelocation section [%2zu] '%s' for section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2150 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2151 (unsigned int) shdr->sh_info,
2152 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name),
2156 /* The .rela.dyn section does not refer to a specific section but
2157 instead of section index zero. Do not try to print a section
2159 printf (ngettext ("\
2160 \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
2162 \nRelocation section [%2u] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
2164 (unsigned int) elf_ndxscn (scn),
2165 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2168 fputs_unlocked (class == ELFCLASS32
2170 Offset Type Value Addend Name\n")
2172 Offset Type Value Addend Name\n"),
2175 int is_statically_linked = 0;
2176 for (int cnt = 0; cnt < nentries; ++cnt)
2179 GElf_Rela *rel = gelf_getrela (data, cnt, &relmem);
2180 if (likely (rel != NULL))
2185 GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
2186 GELF_R_SYM (rel->r_info),
2189 if (unlikely (sym == NULL))
2191 /* As a special case we have to handle relocations in static
2192 executables. This only happens for IRELATIVE relocations
2193 (so far). There is no symbol table. */
2194 if (is_statically_linked == 0)
2196 /* Find the program header and look for a PT_INTERP entry. */
2197 is_statically_linked = -1;
2198 if (ehdr->e_type == ET_EXEC)
2200 is_statically_linked = 1;
2202 for (size_t inner = 0; inner < phnum; ++inner)
2205 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, inner,
2207 if (phdr != NULL && phdr->p_type == PT_INTERP)
2209 is_statically_linked = -1;
2216 if (is_statically_linked > 0 && shdr->sh_link == 0)
2218 %#0*" PRIx64 " %-15s %*s %#6" PRIx64 " %s\n",
2219 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2220 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2221 /* Avoid the leading R_ which isn't carrying any
2223 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2224 buf, sizeof (buf)) + 2
2225 : gettext ("<INVALID RELOC>"),
2226 class == ELFCLASS32 ? 10 : 18, "",
2228 elf_strptr (ebl->elf, shstrndx, destshdr->sh_name));
2230 printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2231 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2232 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2233 /* Avoid the leading R_ which isn't carrying any
2235 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2236 buf, sizeof (buf)) + 2
2237 : gettext ("<INVALID RELOC>"),
2238 gettext ("INVALID SYMBOL"),
2239 (long int) GELF_R_SYM (rel->r_info));
2241 else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION)
2243 %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2244 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2245 likely (ebl_reloc_type_check (ebl,
2246 GELF_R_TYPE (rel->r_info)))
2247 /* Avoid the leading R_ which isn't carrying any
2249 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2250 buf, sizeof (buf)) + 2
2251 : gettext ("<INVALID RELOC>"),
2252 class == ELFCLASS32 ? 10 : 18, sym->st_value,
2254 elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name));
2257 /* This is a relocation against a STT_SECTION symbol. */
2258 GElf_Shdr secshdr_mem;
2260 secshdr = gelf_getshdr (elf_getscn (ebl->elf,
2261 sym->st_shndx == SHN_XINDEX
2262 ? xndx : sym->st_shndx),
2265 if (unlikely (secshdr == NULL))
2266 printf (" %#0*" PRIx64 " %-15s <%s %ld>\n",
2267 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2268 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2269 /* Avoid the leading R_ which isn't carrying any
2271 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2272 buf, sizeof (buf)) + 2
2273 : gettext ("<INVALID RELOC>"),
2274 gettext ("INVALID SECTION"),
2275 (long int) (sym->st_shndx == SHN_XINDEX
2276 ? xndx : sym->st_shndx));
2279 %#0*" PRIx64 " %-15s %#0*" PRIx64 " %+6" PRId64 " %s\n",
2280 class == ELFCLASS32 ? 10 : 18, rel->r_offset,
2281 ebl_reloc_type_check (ebl, GELF_R_TYPE (rel->r_info))
2282 /* Avoid the leading R_ which isn't carrying any
2284 ? ebl_reloc_type_name (ebl, GELF_R_TYPE (rel->r_info),
2285 buf, sizeof (buf)) + 2
2286 : gettext ("<INVALID RELOC>"),
2287 class == ELFCLASS32 ? 10 : 18, sym->st_value,
2289 elf_strptr (ebl->elf, shstrndx, secshdr->sh_name));
2296 /* Print the program header. */
2298 print_symtab (Ebl *ebl, int type)
2300 /* Find the symbol table(s). For this we have to search through the
2302 Elf_Scn *scn = NULL;
2304 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2306 /* Handle the section if it is a symbol table. */
2308 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2310 if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
2312 if (symbol_table_section != NULL)
2314 /* Get the section header string table index. */
2317 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2318 error (EXIT_FAILURE, 0,
2319 gettext ("cannot get section header string table index"));
2320 sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2321 if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2325 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
2327 if (elf_compress (scn, 0, 0) < 0)
2328 printf ("WARNING: %s [%zd]\n",
2329 gettext ("Couldn't uncompress section"),
2331 shdr = gelf_getshdr (scn, &shdr_mem);
2332 if (unlikely (shdr == NULL))
2333 error (EXIT_FAILURE, 0,
2334 gettext ("cannot get section [%zd] header: %s"),
2335 elf_ndxscn (scn), elf_errmsg (-1));
2337 handle_symtab (ebl, scn, shdr);
2344 handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2346 Elf_Data *versym_data = NULL;
2347 Elf_Data *verneed_data = NULL;
2348 Elf_Data *verdef_data = NULL;
2349 Elf_Data *xndx_data = NULL;
2350 int class = gelf_getclass (ebl->elf);
2351 Elf32_Word verneed_stridx = 0;
2352 Elf32_Word verdef_stridx = 0;
2354 /* Get the data of the section. */
2355 Elf_Data *data = elf_getdata (scn, NULL);
2359 /* Find out whether we have other sections we might need. */
2360 Elf_Scn *runscn = NULL;
2361 while ((runscn = elf_nextscn (ebl->elf, runscn)) != NULL)
2363 GElf_Shdr runshdr_mem;
2364 GElf_Shdr *runshdr = gelf_getshdr (runscn, &runshdr_mem);
2366 if (likely (runshdr != NULL))
2368 if (runshdr->sh_type == SHT_GNU_versym
2369 && runshdr->sh_link == elf_ndxscn (scn))
2370 /* Bingo, found the version information. Now get the data. */
2371 versym_data = elf_getdata (runscn, NULL);
2372 else if (runshdr->sh_type == SHT_GNU_verneed)
2374 /* This is the information about the needed versions. */
2375 verneed_data = elf_getdata (runscn, NULL);
2376 verneed_stridx = runshdr->sh_link;
2378 else if (runshdr->sh_type == SHT_GNU_verdef)
2380 /* This is the information about the defined versions. */
2381 verdef_data = elf_getdata (runscn, NULL);
2382 verdef_stridx = runshdr->sh_link;
2384 else if (runshdr->sh_type == SHT_SYMTAB_SHNDX
2385 && runshdr->sh_link == elf_ndxscn (scn))
2386 /* Extended section index. */
2387 xndx_data = elf_getdata (runscn, NULL);
2391 /* Get the section header string table index. */
2393 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2394 error (EXIT_FAILURE, 0,
2395 gettext ("cannot get section header string table index"));
2397 GElf_Shdr glink_mem;
2398 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2401 error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
2404 /* Now we can compute the number of entries in the section. */
2405 unsigned int nsyms = data->d_size / (class == ELFCLASS32
2406 ? sizeof (Elf32_Sym)
2407 : sizeof (Elf64_Sym));
2409 printf (ngettext ("\nSymbol table [%2u] '%s' contains %u entry:\n",
2410 "\nSymbol table [%2u] '%s' contains %u entries:\n",
2412 (unsigned int) elf_ndxscn (scn),
2413 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2414 printf (ngettext (" %lu local symbol String table: [%2u] '%s'\n",
2415 " %lu local symbols String table: [%2u] '%s'\n",
2417 (unsigned long int) shdr->sh_info,
2418 (unsigned int) shdr->sh_link,
2419 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2421 fputs_unlocked (class == ELFCLASS32
2423 Num: Value Size Type Bind Vis Ndx Name\n")
2425 Num: Value Size Type Bind Vis Ndx Name\n"),
2428 for (unsigned int cnt = 0; cnt < nsyms; ++cnt)
2435 GElf_Sym *sym = gelf_getsymshndx (data, xndx_data, cnt, &sym_mem, &xndx);
2437 if (unlikely (sym == NULL))
2440 /* Determine the real section index. */
2441 if (likely (sym->st_shndx != SHN_XINDEX))
2442 xndx = sym->st_shndx;
2445 %5u: %0*" PRIx64 " %6" PRId64 " %-7s %-6s %-9s %6s %s"),
2447 class == ELFCLASS32 ? 8 : 16,
2450 ebl_symbol_type_name (ebl, GELF_ST_TYPE (sym->st_info),
2451 typebuf, sizeof (typebuf)),
2452 ebl_symbol_binding_name (ebl, GELF_ST_BIND (sym->st_info),
2453 bindbuf, sizeof (bindbuf)),
2454 get_visibility_type (GELF_ST_VISIBILITY (sym->st_other)),
2455 ebl_section_name (ebl, sym->st_shndx, xndx, scnbuf,
2456 sizeof (scnbuf), NULL, shnum),
2457 elf_strptr (ebl->elf, shdr->sh_link, sym->st_name));
2459 if (versym_data != NULL)
2461 /* Get the version information. */
2462 GElf_Versym versym_mem;
2463 GElf_Versym *versym = gelf_getversym (versym_data, cnt, &versym_mem);
2465 if (versym != NULL && ((*versym & 0x8000) != 0 || *versym > 1))
2467 bool is_nobits = false;
2468 bool check_def = xndx != SHN_UNDEF;
2470 if (xndx < SHN_LORESERVE || sym->st_shndx == SHN_XINDEX)
2472 GElf_Shdr symshdr_mem;
2473 GElf_Shdr *symshdr =
2474 gelf_getshdr (elf_getscn (ebl->elf, xndx), &symshdr_mem);
2476 is_nobits = (symshdr != NULL
2477 && symshdr->sh_type == SHT_NOBITS);
2480 if (is_nobits || ! check_def)
2482 /* We must test both. */
2483 GElf_Vernaux vernaux_mem;
2484 GElf_Vernaux *vernaux = NULL;
2485 size_t vn_offset = 0;
2487 GElf_Verneed verneed_mem;
2488 GElf_Verneed *verneed = gelf_getverneed (verneed_data, 0,
2490 while (verneed != NULL)
2492 size_t vna_offset = vn_offset;
2494 vernaux = gelf_getvernaux (verneed_data,
2495 vna_offset += verneed->vn_aux,
2497 while (vernaux != NULL
2498 && vernaux->vna_other != *versym
2499 && vernaux->vna_next != 0)
2501 /* Update the offset. */
2502 vna_offset += vernaux->vna_next;
2504 vernaux = (vernaux->vna_next == 0
2506 : gelf_getvernaux (verneed_data,
2511 /* Check whether we found the version. */
2512 if (vernaux != NULL && vernaux->vna_other == *versym)
2516 vn_offset += verneed->vn_next;
2517 verneed = (verneed->vn_next == 0
2519 : gelf_getverneed (verneed_data, vn_offset,
2523 if (vernaux != NULL && vernaux->vna_other == *versym)
2526 elf_strptr (ebl->elf, verneed_stridx,
2528 (unsigned int) vernaux->vna_other);
2531 else if (unlikely (! is_nobits))
2532 error (0, 0, gettext ("bad dynamic symbol"));
2537 if (check_def && *versym != 0x8001)
2539 /* We must test both. */
2540 size_t vd_offset = 0;
2542 GElf_Verdef verdef_mem;
2543 GElf_Verdef *verdef = gelf_getverdef (verdef_data, 0,
2545 while (verdef != NULL)
2547 if (verdef->vd_ndx == (*versym & 0x7fff))
2548 /* Found the definition. */
2551 vd_offset += verdef->vd_next;
2552 verdef = (verdef->vd_next == 0
2554 : gelf_getverdef (verdef_data, vd_offset,
2560 GElf_Verdaux verdaux_mem;
2561 GElf_Verdaux *verdaux
2562 = gelf_getverdaux (verdef_data,
2563 vd_offset + verdef->vd_aux,
2566 if (verdaux != NULL)
2567 printf ((*versym & 0x8000) ? "@%s" : "@@%s",
2568 elf_strptr (ebl->elf, verdef_stridx,
2569 verdaux->vda_name));
2575 putchar_unlocked ('\n');
2580 /* Print version information. */
2582 print_verinfo (Ebl *ebl)
2584 /* Find the version information sections. For this we have to
2585 search through the section table. */
2586 Elf_Scn *scn = NULL;
2588 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
2590 /* Handle the section if it is part of the versioning handling. */
2592 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2594 if (likely (shdr != NULL))
2596 if (shdr->sh_type == SHT_GNU_verneed)
2597 handle_verneed (ebl, scn, shdr);
2598 else if (shdr->sh_type == SHT_GNU_verdef)
2599 handle_verdef (ebl, scn, shdr);
2600 else if (shdr->sh_type == SHT_GNU_versym)
2601 handle_versym (ebl, scn, shdr);
2608 get_ver_flags (unsigned int flags)
2610 static char buf[32];
2614 return gettext ("none");
2616 if (flags & VER_FLG_BASE)
2617 endp = stpcpy (buf, "BASE ");
2621 if (flags & VER_FLG_WEAK)
2624 endp = stpcpy (endp, "| ");
2626 endp = stpcpy (endp, "WEAK ");
2629 if (unlikely (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)))
2631 strncpy (endp, gettext ("| <unknown>"), buf + sizeof (buf) - endp);
2632 buf[sizeof (buf) - 1] = '\0';
2640 handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2642 int class = gelf_getclass (ebl->elf);
2644 /* Get the data of the section. */
2645 Elf_Data *data = elf_getdata (scn, NULL);
2649 /* Get the section header string table index. */
2651 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2652 error (EXIT_FAILURE, 0,
2653 gettext ("cannot get section header string table index"));
2655 GElf_Shdr glink_mem;
2656 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2659 error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
2662 printf (ngettext ("\
2663 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2665 \nVersion needs section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2667 (unsigned int) elf_ndxscn (scn),
2668 elf_strptr (ebl->elf, shstrndx, shdr->sh_name), shdr->sh_info,
2669 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2671 (unsigned int) shdr->sh_link,
2672 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2674 unsigned int offset = 0;
2675 for (int cnt = shdr->sh_info; --cnt >= 0; )
2677 /* Get the data at the next offset. */
2678 GElf_Verneed needmem;
2679 GElf_Verneed *need = gelf_getverneed (data, offset, &needmem);
2680 if (unlikely (need == NULL))
2683 printf (gettext (" %#06x: Version: %hu File: %s Cnt: %hu\n"),
2684 offset, (unsigned short int) need->vn_version,
2685 elf_strptr (ebl->elf, shdr->sh_link, need->vn_file),
2686 (unsigned short int) need->vn_cnt);
2688 unsigned int auxoffset = offset + need->vn_aux;
2689 for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
2691 GElf_Vernaux auxmem;
2692 GElf_Vernaux *aux = gelf_getvernaux (data, auxoffset, &auxmem);
2693 if (unlikely (aux == NULL))
2696 printf (gettext (" %#06x: Name: %s Flags: %s Version: %hu\n"),
2698 elf_strptr (ebl->elf, shdr->sh_link, aux->vna_name),
2699 get_ver_flags (aux->vna_flags),
2700 (unsigned short int) aux->vna_other);
2702 if (aux->vna_next == 0)
2705 auxoffset += aux->vna_next;
2708 /* Find the next offset. */
2709 if (need->vn_next == 0)
2712 offset += need->vn_next;
2718 handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2720 /* Get the data of the section. */
2721 Elf_Data *data = elf_getdata (scn, NULL);
2725 /* Get the section header string table index. */
2727 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2728 error (EXIT_FAILURE, 0,
2729 gettext ("cannot get section header string table index"));
2731 GElf_Shdr glink_mem;
2732 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2735 error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
2738 int class = gelf_getclass (ebl->elf);
2739 printf (ngettext ("\
2740 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2742 \nVersion definition section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'\n",
2744 (unsigned int) elf_ndxscn (scn),
2745 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2747 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2749 (unsigned int) shdr->sh_link,
2750 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2752 unsigned int offset = 0;
2753 for (int cnt = shdr->sh_info; --cnt >= 0; )
2755 /* Get the data at the next offset. */
2757 GElf_Verdef *def = gelf_getverdef (data, offset, &defmem);
2758 if (unlikely (def == NULL))
2761 unsigned int auxoffset = offset + def->vd_aux;
2762 GElf_Verdaux auxmem;
2763 GElf_Verdaux *aux = gelf_getverdaux (data, auxoffset, &auxmem);
2764 if (unlikely (aux == NULL))
2768 %#06x: Version: %hd Flags: %s Index: %hd Cnt: %hd Name: %s\n"),
2769 offset, def->vd_version,
2770 get_ver_flags (def->vd_flags),
2773 elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2775 auxoffset += aux->vda_next;
2776 for (int cnt2 = 1; cnt2 < def->vd_cnt; ++cnt2)
2778 aux = gelf_getverdaux (data, auxoffset, &auxmem);
2779 if (unlikely (aux == NULL))
2782 printf (gettext (" %#06x: Parent %d: %s\n"),
2784 elf_strptr (ebl->elf, shdr->sh_link, aux->vda_name));
2786 if (aux->vda_next == 0)
2789 auxoffset += aux->vda_next;
2792 /* Find the next offset. */
2793 if (def->vd_next == 0)
2795 offset += def->vd_next;
2801 handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2803 int class = gelf_getclass (ebl->elf);
2804 const char **vername;
2805 const char **filename;
2807 /* Get the data of the section. */
2808 Elf_Data *data = elf_getdata (scn, NULL);
2812 /* Get the section header string table index. */
2814 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2815 error (EXIT_FAILURE, 0,
2816 gettext ("cannot get section header string table index"));
2818 /* We have to find the version definition section and extract the
2820 Elf_Scn *defscn = NULL;
2821 Elf_Scn *needscn = NULL;
2823 Elf_Scn *verscn = NULL;
2824 while ((verscn = elf_nextscn (ebl->elf, verscn)) != NULL)
2826 GElf_Shdr vershdr_mem;
2827 GElf_Shdr *vershdr = gelf_getshdr (verscn, &vershdr_mem);
2829 if (likely (vershdr != NULL))
2831 if (vershdr->sh_type == SHT_GNU_verdef)
2833 else if (vershdr->sh_type == SHT_GNU_verneed)
2839 if (defscn != NULL || needscn != NULL)
2841 /* We have a version information (better should have). Now get
2842 the version names. First find the maximum version number. */
2846 /* Run through the version definitions and find the highest
2848 unsigned int offset = 0;
2850 GElf_Shdr defshdrmem;
2853 defdata = elf_getdata (defscn, NULL);
2854 if (unlikely (defdata == NULL))
2857 defshdr = gelf_getshdr (defscn, &defshdrmem);
2858 if (unlikely (defshdr == NULL))
2861 for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
2866 /* Get the data at the next offset. */
2867 def = gelf_getverdef (defdata, offset, &defmem);
2868 if (unlikely (def == NULL))
2871 nvername = MAX (nvername, (size_t) (def->vd_ndx & 0x7fff));
2873 if (def->vd_next == 0)
2875 offset += def->vd_next;
2878 if (needscn != NULL)
2880 unsigned int offset = 0;
2882 GElf_Shdr needshdrmem;
2883 GElf_Shdr *needshdr;
2885 needdata = elf_getdata (needscn, NULL);
2886 if (unlikely (needdata == NULL))
2889 needshdr = gelf_getshdr (needscn, &needshdrmem);
2890 if (unlikely (needshdr == NULL))
2893 for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
2895 GElf_Verneed needmem;
2897 unsigned int auxoffset;
2900 /* Get the data at the next offset. */
2901 need = gelf_getverneed (needdata, offset, &needmem);
2902 if (unlikely (need == NULL))
2905 /* Run through the auxiliary entries. */
2906 auxoffset = offset + need->vn_aux;
2907 for (cnt2 = need->vn_cnt; --cnt2 >= 0; )
2909 GElf_Vernaux auxmem;
2912 aux = gelf_getvernaux (needdata, auxoffset, &auxmem);
2913 if (unlikely (aux == NULL))
2916 nvername = MAX (nvername,
2917 (size_t) (aux->vna_other & 0x7fff));
2919 if (aux->vna_next == 0)
2921 auxoffset += aux->vna_next;
2924 if (need->vn_next == 0)
2926 offset += need->vn_next;
2930 /* This is the number of versions we know about. */
2933 /* Allocate the array. */
2934 vername = (const char **) alloca (nvername * sizeof (const char *));
2935 memset(vername, 0, nvername * sizeof (const char *));
2936 filename = (const char **) alloca (nvername * sizeof (const char *));
2937 memset(filename, 0, nvername * sizeof (const char *));
2939 /* Run through the data structures again and collect the strings. */
2942 /* Run through the version definitions and find the highest
2944 unsigned int offset = 0;
2946 GElf_Shdr defshdrmem;
2949 defdata = elf_getdata (defscn, NULL);
2950 if (unlikely (defdata == NULL))
2953 defshdr = gelf_getshdr (defscn, &defshdrmem);
2954 if (unlikely (defshdr == NULL))
2957 for (unsigned int cnt = 0; cnt < defshdr->sh_info; ++cnt)
2960 /* Get the data at the next offset. */
2962 GElf_Verdef *def = gelf_getverdef (defdata, offset, &defmem);
2963 if (unlikely (def == NULL))
2966 GElf_Verdaux auxmem;
2967 GElf_Verdaux *aux = gelf_getverdaux (defdata,
2968 offset + def->vd_aux,
2970 if (unlikely (aux == NULL))
2973 vername[def->vd_ndx & 0x7fff]
2974 = elf_strptr (ebl->elf, defshdr->sh_link, aux->vda_name);
2975 filename[def->vd_ndx & 0x7fff] = NULL;
2977 if (def->vd_next == 0)
2979 offset += def->vd_next;
2982 if (needscn != NULL)
2984 unsigned int offset = 0;
2986 Elf_Data *needdata = elf_getdata (needscn, NULL);
2987 GElf_Shdr needshdrmem;
2988 GElf_Shdr *needshdr = gelf_getshdr (needscn, &needshdrmem);
2989 if (unlikely (needdata == NULL || needshdr == NULL))
2992 for (unsigned int cnt = 0; cnt < needshdr->sh_info; ++cnt)
2994 /* Get the data at the next offset. */
2995 GElf_Verneed needmem;
2996 GElf_Verneed *need = gelf_getverneed (needdata, offset,
2998 if (unlikely (need == NULL))
3001 /* Run through the auxiliary entries. */
3002 unsigned int auxoffset = offset + need->vn_aux;
3003 for (int cnt2 = need->vn_cnt; --cnt2 >= 0; )
3005 GElf_Vernaux auxmem;
3006 GElf_Vernaux *aux = gelf_getvernaux (needdata, auxoffset,
3008 if (unlikely (aux == NULL))
3011 vername[aux->vna_other & 0x7fff]
3012 = elf_strptr (ebl->elf, needshdr->sh_link, aux->vna_name);
3013 filename[aux->vna_other & 0x7fff]
3014 = elf_strptr (ebl->elf, needshdr->sh_link, need->vn_file);
3016 if (aux->vna_next == 0)
3018 auxoffset += aux->vna_next;
3021 if (need->vn_next == 0)
3023 offset += need->vn_next;
3034 GElf_Shdr glink_mem;
3035 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
3037 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_HALF, 1, EV_CURRENT);
3039 error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %zu"),
3042 /* Print the header. */
3043 printf (ngettext ("\
3044 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3046 \nVersion symbols section [%2u] '%s' contains %d entries:\n Addr: %#0*" PRIx64 " Offset: %#08" PRIx64 " Link to section: [%2u] '%s'",
3047 shdr->sh_size / sh_entsize),
3048 (unsigned int) elf_ndxscn (scn),
3049 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3050 (int) (shdr->sh_size / sh_entsize),
3051 class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
3053 (unsigned int) shdr->sh_link,
3054 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3056 /* Now we can finally look at the actual contents of this section. */
3057 for (unsigned int cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
3060 printf ("\n %4d:", cnt);
3063 GElf_Versym *sym = gelf_getversym (data, cnt, &symmem);
3071 fputs_unlocked (gettext (" 0 *local* "),
3076 fputs_unlocked (gettext (" 1 *global* "),
3081 n = printf ("%4d%c%s",
3082 *sym & 0x7fff, *sym & 0x8000 ? 'h' : ' ',
3084 && (unsigned int) (*sym & 0x7fff) < nvername)
3085 ? vername[*sym & 0x7fff] : "???");
3086 if ((unsigned int) (*sym & 0x7fff) < nvername
3087 && filename != NULL && filename[*sym & 0x7fff] != NULL)
3088 n += printf ("(%s)", filename[*sym & 0x7fff]);
3089 printf ("%*s", MAX (0, 33 - (int) n), " ");
3093 putchar_unlocked ('\n');
3098 print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
3099 uint_fast32_t maxlength, Elf32_Word nbucket,
3100 uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
3102 uint32_t *counts = (uint32_t *) xcalloc (maxlength + 1, sizeof (uint32_t));
3104 for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3105 ++counts[lengths[cnt]];
3107 GElf_Shdr glink_mem;
3108 GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
3113 error (0, 0, gettext ("invalid sh_link value in section %zu"),
3118 printf (ngettext ("\
3119 \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",
3121 \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",
3123 (unsigned int) elf_ndxscn (scn),
3124 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3126 gelf_getclass (ebl->elf) == ELFCLASS32 ? 10 : 18,
3129 (unsigned int) shdr->sh_link,
3130 elf_strptr (ebl->elf, shstrndx, glink->sh_name));
3132 if (extrastr != NULL)
3133 fputs (extrastr, stdout);
3135 if (likely (nbucket > 0))
3137 uint64_t success = 0;
3139 /* xgettext:no-c-format */
3140 fputs_unlocked (gettext ("\
3141 Length Number % of total Coverage\n"), stdout);
3142 printf (gettext (" 0 %6" PRIu32 " %5.1f%%\n"),
3143 counts[0], (counts[0] * 100.0) / nbucket);
3145 uint64_t nzero_counts = 0;
3146 for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3148 nzero_counts += counts[cnt] * cnt;
3150 %7d %6" PRIu32 " %5.1f%% %5.1f%%\n"),
3151 (int) cnt, counts[cnt], (counts[cnt] * 100.0) / nbucket,
3152 (nzero_counts * 100.0) / nsyms);
3156 for (Elf32_Word cnt = 1; cnt <= maxlength; ++cnt)
3159 success += counts[cnt] * acc;
3163 Average number of tests: successful lookup: %f\n\
3164 unsuccessful lookup: %f\n"),
3165 (double) success / (double) nzero_counts,
3166 (double) nzero_counts / (double) nbucket);
3173 /* This function handles the traditional System V-style hash table format. */
3175 handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3177 Elf_Data *data = elf_getdata (scn, NULL);
3178 if (unlikely (data == NULL))
3180 error (0, 0, gettext ("cannot get data for section %d: %s"),
3181 (int) elf_ndxscn (scn), elf_errmsg (-1));
3185 if (unlikely (data->d_size < 2 * sizeof (Elf32_Word)))
3188 error (0, 0, gettext ("invalid data in sysv.hash section %d"),
3189 (int) elf_ndxscn (scn));
3193 Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3194 Elf32_Word nchain = ((Elf32_Word *) data->d_buf)[1];
3196 uint64_t used_buf = (2ULL + nchain + nbucket) * sizeof (Elf32_Word);
3197 if (used_buf > data->d_size)
3200 Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
3201 Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
3203 uint32_t *lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
3205 uint_fast32_t maxlength = 0;
3206 uint_fast32_t nsyms = 0;
3207 for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3209 Elf32_Word inner = bucket[cnt];
3210 Elf32_Word chain_len = 0;
3211 while (inner > 0 && inner < nchain)
3215 if (chain_len > nchain)
3217 if (maxlength < ++lengths[cnt])
3220 inner = chain[inner];
3224 print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3231 /* This function handles the incorrect, System V-style hash table
3232 format some 64-bit architectures use. */
3234 handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3236 Elf_Data *data = elf_getdata (scn, NULL);
3237 if (unlikely (data == NULL))
3239 error (0, 0, gettext ("cannot get data for section %d: %s"),
3240 (int) elf_ndxscn (scn), elf_errmsg (-1));
3244 if (unlikely (data->d_size < 2 * sizeof (Elf64_Xword)))
3247 error (0, 0, gettext ("invalid data in sysv.hash64 section %d"),
3248 (int) elf_ndxscn (scn));
3252 Elf64_Xword nbucket = ((Elf64_Xword *) data->d_buf)[0];
3253 Elf64_Xword nchain = ((Elf64_Xword *) data->d_buf)[1];
3255 uint64_t maxwords = data->d_size / sizeof (Elf64_Xword);
3257 || maxwords - 2 < nbucket
3258 || maxwords - 2 - nbucket < nchain)
3261 Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
3262 Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
3264 uint32_t *lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
3266 uint_fast32_t maxlength = 0;
3267 uint_fast32_t nsyms = 0;
3268 for (Elf64_Xword cnt = 0; cnt < nbucket; ++cnt)
3270 Elf64_Xword inner = bucket[cnt];
3271 Elf64_Xword chain_len = 0;
3272 while (inner > 0 && inner < nchain)
3276 if (chain_len > nchain)
3278 if (maxlength < ++lengths[cnt])
3281 inner = chain[inner];
3285 print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3292 /* This function handles the GNU-style hash table format. */
3294 handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
3296 uint32_t *lengths = NULL;
3297 Elf_Data *data = elf_getdata (scn, NULL);
3298 if (unlikely (data == NULL))
3300 error (0, 0, gettext ("cannot get data for section %d: %s"),
3301 (int) elf_ndxscn (scn), elf_errmsg (-1));
3305 if (unlikely (data->d_size < 4 * sizeof (Elf32_Word)))
3309 error (0, 0, gettext ("invalid data in gnu.hash section %d"),
3310 (int) elf_ndxscn (scn));
3314 Elf32_Word nbucket = ((Elf32_Word *) data->d_buf)[0];
3315 Elf32_Word symbias = ((Elf32_Word *) data->d_buf)[1];
3317 /* Next comes the size of the bitmap. It's measured in words for
3318 the architecture. It's 32 bits for 32 bit archs, and 64 bits for
3319 64 bit archs. There is always a bloom filter present, so zero is
3320 an invalid value. */
3321 Elf32_Word bitmask_words = ((Elf32_Word *) data->d_buf)[2];
3322 if (gelf_getclass (ebl->elf) == ELFCLASS64)
3325 if (bitmask_words == 0)
3328 Elf32_Word shift = ((Elf32_Word *) data->d_buf)[3];
3330 /* Is there still room for the sym chain?
3331 Use uint64_t calculation to prevent 32bit overlow. */
3332 uint64_t used_buf = (4ULL + bitmask_words + nbucket) * sizeof (Elf32_Word);
3333 uint32_t max_nsyms = (data->d_size - used_buf) / sizeof (Elf32_Word);
3334 if (used_buf > data->d_size)
3337 lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
3339 Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
3340 Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
3341 Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[4 + bitmask_words
3344 /* Compute distribution of chain lengths. */
3345 uint_fast32_t maxlength = 0;
3346 uint_fast32_t nsyms = 0;
3347 for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
3348 if (bucket[cnt] != 0)
3350 Elf32_Word inner = bucket[cnt] - symbias;
3354 if (maxlength < ++lengths[cnt])
3356 if (inner >= max_nsyms)
3359 while ((chain[inner++] & 1) == 0);
3362 /* Count bits in bitmask. */
3363 uint_fast32_t nbits = 0;
3364 for (Elf32_Word cnt = 0; cnt < bitmask_words; ++cnt)
3366 uint_fast32_t word = bitmask[cnt];
3368 word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
3369 word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
3370 word = (word & 0x0f0f0f0f) + ((word >> 4) & 0x0f0f0f0f);
3371 word = (word & 0x00ff00ff) + ((word >> 8) & 0x00ff00ff);
3372 nbits += (word & 0x0000ffff) + ((word >> 16) & 0x0000ffff);
3376 if (unlikely (asprintf (&str, gettext ("\
3378 Bitmask Size: %zu bytes %" PRIuFAST32 "%% bits set 2nd hash shift: %u\n"),
3379 (unsigned int) symbias,
3380 bitmask_words * sizeof (Elf32_Word),
3382 / (uint_fast32_t) (bitmask_words
3383 * sizeof (Elf32_Word) * 8)),
3384 (unsigned int) shift) == -1))
3385 error (EXIT_FAILURE, 0, gettext ("memory exhausted"));
3387 print_hash_info (ebl, scn, shdr, shstrndx, maxlength, nbucket, nsyms,
3395 /* Find the symbol table(s). For this we have to search through the
3398 handle_hash (Ebl *ebl)
3400 /* Get the section header string table index. */
3402 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3403 error (EXIT_FAILURE, 0,
3404 gettext ("cannot get section header string table index"));
3406 Elf_Scn *scn = NULL;
3407 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3409 /* Handle the section if it is a symbol table. */
3411 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3413 if (likely (shdr != NULL))
3415 if ((shdr->sh_type == SHT_HASH || shdr->sh_type == SHT_GNU_HASH)
3416 && (shdr->sh_flags & SHF_COMPRESSED) != 0)
3418 if (elf_compress (scn, 0, 0) < 0)
3419 printf ("WARNING: %s [%zd]\n",
3420 gettext ("Couldn't uncompress section"),
3422 shdr = gelf_getshdr (scn, &shdr_mem);
3423 if (unlikely (shdr == NULL))
3424 error (EXIT_FAILURE, 0,
3425 gettext ("cannot get section [%zd] header: %s"),
3426 elf_ndxscn (scn), elf_errmsg (-1));
3429 if (shdr->sh_type == SHT_HASH)
3431 if (ebl_sysvhash_entrysize (ebl) == sizeof (Elf64_Xword))
3432 handle_sysv_hash64 (ebl, scn, shdr, shstrndx);
3434 handle_sysv_hash (ebl, scn, shdr, shstrndx);
3436 else if (shdr->sh_type == SHT_GNU_HASH)
3437 handle_gnu_hash (ebl, scn, shdr, shstrndx);
3444 print_liblist (Ebl *ebl)
3446 /* Find the library list sections. For this we have to search
3447 through the section table. */
3448 Elf_Scn *scn = NULL;
3450 /* Get the section header string table index. */
3452 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3453 error (EXIT_FAILURE, 0,
3454 gettext ("cannot get section header string table index"));
3456 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3459 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3461 if (shdr != NULL && shdr->sh_type == SHT_GNU_LIBLIST)
3463 size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_LIB, 1, EV_CURRENT);
3464 int nentries = shdr->sh_size / sh_entsize;
3465 printf (ngettext ("\
3466 \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entry:\n",
3468 \nLibrary list section [%2zu] '%s' at offset %#0" PRIx64 " contains %d entries:\n",
3471 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3475 Elf_Data *data = elf_getdata (scn, NULL);
3480 Library Time Stamp Checksum Version Flags"));
3482 for (int cnt = 0; cnt < nentries; ++cnt)
3485 GElf_Lib *lib = gelf_getlib (data, cnt, &lib_mem);
3486 if (unlikely (lib == NULL))
3489 time_t t = (time_t) lib->l_time_stamp;
3490 struct tm *tm = gmtime (&t);
3491 if (unlikely (tm == NULL))
3494 printf (" [%2d] %-29s %04u-%02u-%02uT%02u:%02u:%02u %08x %-7u %u\n",
3495 cnt, elf_strptr (ebl->elf, shdr->sh_link, lib->l_name),
3496 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
3497 tm->tm_hour, tm->tm_min, tm->tm_sec,
3498 (unsigned int) lib->l_checksum,
3499 (unsigned int) lib->l_version,
3500 (unsigned int) lib->l_flags);
3507 print_attributes (Ebl *ebl, const GElf_Ehdr *ehdr)
3509 /* Find the object attributes sections. For this we have to search
3510 through the section table. */
3511 Elf_Scn *scn = NULL;
3513 /* Get the section header string table index. */
3515 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
3516 error (EXIT_FAILURE, 0,
3517 gettext ("cannot get section header string table index"));
3519 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
3522 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3524 if (shdr == NULL || (shdr->sh_type != SHT_GNU_ATTRIBUTES
3525 && (shdr->sh_type != SHT_ARM_ATTRIBUTES
3526 || ehdr->e_machine != EM_ARM)))
3530 \nObject attributes section [%2zu] '%s' of %" PRIu64
3531 " bytes at offset %#0" PRIx64 ":\n"),
3533 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
3534 shdr->sh_size, shdr->sh_offset);
3536 Elf_Data *data = elf_rawdata (scn, NULL);
3537 if (unlikely (data == NULL || data->d_size == 0))
3540 const unsigned char *p = data->d_buf;
3542 /* There is only one 'version', A. */
3543 if (unlikely (*p++ != 'A'))
3546 fputs_unlocked (gettext (" Owner Size\n"), stdout);
3548 inline size_t left (void)
3550 return (const unsigned char *) data->d_buf + data->d_size - p;
3553 /* Loop over the sections. */
3554 while (left () >= 4)
3556 /* Section length. */
3558 memcpy (&len, p, sizeof len);
3560 if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3563 if (unlikely (len > left ()))
3566 /* Section vendor name. */
3567 const unsigned char *name = p + sizeof len;
3570 unsigned const char *q = memchr (name, '\0', len);
3571 if (unlikely (q == NULL))
3575 printf (gettext (" %-13s %4" PRIu32 "\n"), name, len);
3577 bool gnu_vendor = (q - name == sizeof "gnu"
3578 && !memcmp (name, "gnu", sizeof "gnu"));
3580 /* Loop over subsections. */
3581 if (shdr->sh_type != SHT_GNU_ATTRIBUTES
3585 const unsigned char *const sub = q;
3587 unsigned int subsection_tag;
3588 get_uleb128 (subsection_tag, q, p);
3589 if (unlikely (q >= p))
3592 uint32_t subsection_len;
3593 if (unlikely (p - sub < (ptrdiff_t) sizeof subsection_len))
3596 memcpy (&subsection_len, q, sizeof subsection_len);
3598 if (MY_ELFDATA != ehdr->e_ident[EI_DATA])
3599 CONVERT (subsection_len);
3601 /* Don't overflow, ptrdiff_t might be 32bits, but signed. */
3602 if (unlikely (subsection_len == 0
3603 || subsection_len >= (uint32_t) PTRDIFF_MAX
3604 || p - sub < (ptrdiff_t) subsection_len))
3607 const unsigned char *r = q + sizeof subsection_len;
3608 q = sub + subsection_len;
3610 switch (subsection_tag)
3613 /* Unknown subsection, print and skip. */
3614 printf (gettext (" %-4u %12" PRIu32 "\n"),
3615 subsection_tag, subsection_len);
3618 case 1: /* Tag_File */
3619 printf (gettext (" File: %11" PRIu32 "\n"),
3625 get_uleb128 (tag, r, q);
3626 if (unlikely (r >= q))
3629 /* GNU style tags have either a uleb128 value,
3630 when lowest bit is not set, or a string
3631 when the lowest bit is set.
3632 "compatibility" (32) is special. It has
3633 both a string and a uleb128 value. For
3634 non-gnu we assume 6 till 31 only take ints.
3635 XXX see arm backend, do we need a separate
3638 const char *string = NULL;
3639 if (tag == 32 || (tag & 1) == 0
3640 || (! gnu_vendor && (tag > 5 && tag < 32)))
3642 get_uleb128 (value, r, q);
3649 || (! gnu_vendor && tag > 32)))
3650 || (! gnu_vendor && tag > 3 && tag < 6))
3652 string = (const char *) r;
3653 r = memchr (r, '\0', q - r);
3659 const char *tag_name = NULL;
3660 const char *value_name = NULL;
3661 ebl_check_object_attribute (ebl, (const char *) name,
3663 &tag_name, &value_name);
3665 if (tag_name != NULL)
3668 printf (gettext (" %s: %" PRId64 ", %s\n"),
3669 tag_name, value, string);
3670 else if (string == NULL && value_name == NULL)
3671 printf (gettext (" %s: %" PRId64 "\n"),
3674 printf (gettext (" %s: %s\n"),
3675 tag_name, string ?: value_name);
3679 /* For "gnu" vendor 32 "compatibility" has
3680 already been handled above. */
3682 || strcmp ((const char *) name, "gnu"));
3684 printf (gettext (" %u: %" PRId64 "\n"),
3687 printf (gettext (" %u: %s\n"),
3699 format_dwarf_addr (Dwfl_Module *dwflmod,
3700 int address_size, Dwarf_Addr address, Dwarf_Addr raw)
3702 /* See if there is a name we can give for this address. */
3705 const char *name = (print_address_names && ! print_unresolved_addresses)
3706 ? dwfl_module_addrinfo (dwflmod, address, &off, &sym, NULL, NULL, NULL)
3710 if (print_unresolved_addresses)
3717 /* Relativize the address. */
3718 int n = dwfl_module_relocations (dwflmod);
3719 int i = n < 1 ? -1 : dwfl_module_relocate_address (dwflmod, &address);
3721 /* In an ET_REL file there is a section name to refer to. */
3723 : dwfl_module_relocation_info (dwflmod, i, NULL));
3730 ? (address_size == 0
3731 ? asprintf (&result,
3732 gettext ("%s+%#" PRIx64 " <%s+%#" PRIx64 ">"),
3733 scn, address, name, off)
3734 : asprintf (&result,
3735 gettext ("%s+%#0*" PRIx64 " <%s+%#" PRIx64 ">"),
3736 scn, 2 + address_size * 2, address,
3738 : (address_size == 0
3739 ? asprintf (&result,
3740 gettext ("%#" PRIx64 " <%s+%#" PRIx64 ">"),
3742 : asprintf (&result,
3743 gettext ("%#0*" PRIx64 " <%s+%#" PRIx64 ">"),
3744 2 + address_size * 2, address,
3747 ? (address_size == 0
3748 ? asprintf (&result,
3749 gettext ("%s+%#" PRIx64 " <%s>"),
3751 : asprintf (&result,
3752 gettext ("%s+%#0*" PRIx64 " <%s>"),
3753 scn, 2 + address_size * 2, address, name))
3754 : (address_size == 0
3755 ? asprintf (&result,
3756 gettext ("%#" PRIx64 " <%s>"),
3758 : asprintf (&result,
3759 gettext ("%#0*" PRIx64 " <%s>"),
3760 2 + address_size * 2, address, name))))
3762 ? (address_size == 0
3763 ? asprintf (&result,
3764 gettext ("%s+%#" PRIx64),
3766 : asprintf (&result,
3767 gettext ("%s+%#0*" PRIx64),
3768 scn, 2 + address_size * 2, address))
3769 : (address_size == 0
3770 ? asprintf (&result,
3773 : asprintf (&result,
3775 2 + address_size * 2, address)))) < 0)
3776 error (EXIT_FAILURE, 0, _("memory exhausted"));
3782 dwarf_tag_string (unsigned int tag)
3786 #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
3787 DWARF_ALL_KNOWN_DW_TAG
3788 #undef DWARF_ONE_KNOWN_DW_TAG
3796 dwarf_attr_string (unsigned int attrnum)
3800 #define DWARF_ONE_KNOWN_DW_AT(NAME, CODE) case CODE: return #NAME;
3801 DWARF_ALL_KNOWN_DW_AT
3802 #undef DWARF_ONE_KNOWN_DW_AT
3810 dwarf_form_string (unsigned int form)
3814 #define DWARF_ONE_KNOWN_DW_FORM(NAME, CODE) case CODE: return #NAME;
3815 DWARF_ALL_KNOWN_DW_FORM
3816 #undef DWARF_ONE_KNOWN_DW_FORM
3824 dwarf_lang_string (unsigned int lang)
3828 #define DWARF_ONE_KNOWN_DW_LANG(NAME, CODE) case CODE: return #NAME;
3829 DWARF_ALL_KNOWN_DW_LANG
3830 #undef DWARF_ONE_KNOWN_DW_LANG
3838 dwarf_inline_string (unsigned int code)
3840 static const char *const known[] =
3842 #define DWARF_ONE_KNOWN_DW_INL(NAME, CODE) [CODE] = #NAME,
3843 DWARF_ALL_KNOWN_DW_INL
3844 #undef DWARF_ONE_KNOWN_DW_INL
3847 if (likely (code < sizeof (known) / sizeof (known[0])))
3855 dwarf_encoding_string (unsigned int code)
3857 static const char *const known[] =
3859 #define DWARF_ONE_KNOWN_DW_ATE(NAME, CODE) [CODE] = #NAME,
3860 DWARF_ALL_KNOWN_DW_ATE
3861 #undef DWARF_ONE_KNOWN_DW_ATE
3864 if (likely (code < sizeof (known) / sizeof (known[0])))
3872 dwarf_access_string (unsigned int code)
3874 static const char *const known[] =
3876 #define DWARF_ONE_KNOWN_DW_ACCESS(NAME, CODE) [CODE] = #NAME,
3877 DWARF_ALL_KNOWN_DW_ACCESS
3878 #undef DWARF_ONE_KNOWN_DW_ACCESS
3881 if (likely (code < sizeof (known) / sizeof (known[0])))
3889 dwarf_defaulted_string (unsigned int code)
3891 static const char *const known[] =
3893 #define DWARF_ONE_KNOWN_DW_DEFAULTED(NAME, CODE) [CODE] = #NAME,
3894 DWARF_ALL_KNOWN_DW_DEFAULTED
3895 #undef DWARF_ONE_KNOWN_DW_DEFAULTED
3898 if (likely (code < sizeof (known) / sizeof (known[0])))
3906 dwarf_visibility_string (unsigned int code)
3908 static const char *const known[] =
3910 #define DWARF_ONE_KNOWN_DW_VIS(NAME, CODE) [CODE] = #NAME,
3911 DWARF_ALL_KNOWN_DW_VIS
3912 #undef DWARF_ONE_KNOWN_DW_VIS
3915 if (likely (code < sizeof (known) / sizeof (known[0])))
3923 dwarf_virtuality_string (unsigned int code)
3925 static const char *const known[] =
3927 #define DWARF_ONE_KNOWN_DW_VIRTUALITY(NAME, CODE) [CODE] = #NAME,
3928 DWARF_ALL_KNOWN_DW_VIRTUALITY
3929 #undef DWARF_ONE_KNOWN_DW_VIRTUALITY
3932 if (likely (code < sizeof (known) / sizeof (known[0])))
3940 dwarf_identifier_case_string (unsigned int code)
3942 static const char *const known[] =
3944 #define DWARF_ONE_KNOWN_DW_ID(NAME, CODE) [CODE] = #NAME,
3945 DWARF_ALL_KNOWN_DW_ID
3946 #undef DWARF_ONE_KNOWN_DW_ID
3949 if (likely (code < sizeof (known) / sizeof (known[0])))
3957 dwarf_calling_convention_string (unsigned int code)
3959 static const char *const known[] =
3961 #define DWARF_ONE_KNOWN_DW_CC(NAME, CODE) [CODE] = #NAME,
3962 DWARF_ALL_KNOWN_DW_CC
3963 #undef DWARF_ONE_KNOWN_DW_CC
3966 if (likely (code < sizeof (known) / sizeof (known[0])))
3974 dwarf_ordering_string (unsigned int code)
3976 static const char *const known[] =
3978 #define DWARF_ONE_KNOWN_DW_ORD(NAME, CODE) [CODE] = #NAME,
3979 DWARF_ALL_KNOWN_DW_ORD
3980 #undef DWARF_ONE_KNOWN_DW_ORD
3983 if (likely (code < sizeof (known) / sizeof (known[0])))
3991 dwarf_discr_list_string (unsigned int code)
3993 static const char *const known[] =
3995 #define DWARF_ONE_KNOWN_DW_DSC(NAME, CODE) [CODE] = #NAME,
3996 DWARF_ALL_KNOWN_DW_DSC
3997 #undef DWARF_ONE_KNOWN_DW_DSC
4000 if (likely (code < sizeof (known) / sizeof (known[0])))
4008 dwarf_locexpr_opcode_string (unsigned int code)
4010 static const char *const known[] =
4012 /* Normally we can't affort building huge table of 64K entries,
4013 most of them zero, just because there are a couple defined
4014 values at the far end. In case of opcodes, it's OK. */
4015 #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
4016 DWARF_ALL_KNOWN_DW_OP
4017 #undef DWARF_ONE_KNOWN_DW_OP
4020 if (likely (code < sizeof (known) / sizeof (known[0])))
4028 dwarf_unit_string (unsigned int type)
4032 #define DWARF_ONE_KNOWN_DW_UT(NAME, CODE) case CODE: return #NAME;
4033 DWARF_ALL_KNOWN_DW_UT
4034 #undef DWARF_ONE_KNOWN_DW_UT
4042 dwarf_range_list_encoding_string (unsigned int kind)
4046 #define DWARF_ONE_KNOWN_DW_RLE(NAME, CODE) case CODE: return #NAME;
4047 DWARF_ALL_KNOWN_DW_RLE
4048 #undef DWARF_ONE_KNOWN_DW_RLE
4056 dwarf_loc_list_encoding_string (unsigned int kind)
4060 #define DWARF_ONE_KNOWN_DW_LLE(NAME, CODE) case CODE: return #NAME;
4061 DWARF_ALL_KNOWN_DW_LLE
4062 #undef DWARF_ONE_KNOWN_DW_LLE
4070 dwarf_line_content_description_string (unsigned int kind)
4074 #define DWARF_ONE_KNOWN_DW_LNCT(NAME, CODE) case CODE: return #NAME;
4075 DWARF_ALL_KNOWN_DW_LNCT
4076 #undef DWARF_ONE_KNOWN_DW_LNCT
4083 /* Used by all dwarf_foo_name functions. */
4085 string_or_unknown (const char *known, unsigned int code,
4086 unsigned int lo_user, unsigned int hi_user,
4087 bool print_unknown_num)
4089 static char unknown_buf[20];
4091 if (likely (known != NULL))
4094 if (lo_user != 0 && code >= lo_user && code <= hi_user)
4096 snprintf (unknown_buf, sizeof unknown_buf, "lo_user+%#x",
4101 if (print_unknown_num)
4103 snprintf (unknown_buf, sizeof unknown_buf, "??? (%#x)", code);
4112 dwarf_tag_name (unsigned int tag)
4114 const char *ret = dwarf_tag_string (tag);
4115 return string_or_unknown (ret, tag, DW_TAG_lo_user, DW_TAG_hi_user, true);
4119 dwarf_attr_name (unsigned int attr)
4121 const char *ret = dwarf_attr_string (attr);
4122 return string_or_unknown (ret, attr, DW_AT_lo_user, DW_AT_hi_user, true);
4127 dwarf_form_name (unsigned int form)
4129 const char *ret = dwarf_form_string (form);
4130 return string_or_unknown (ret, form, 0, 0, true);
4135 dwarf_lang_name (unsigned int lang)
4137 const char *ret = dwarf_lang_string (lang);
4138 return string_or_unknown (ret, lang, DW_LANG_lo_user, DW_LANG_hi_user, false);
4143 dwarf_inline_name (unsigned int code)
4145 const char *ret = dwarf_inline_string (code);
4146 return string_or_unknown (ret, code, 0, 0, false);
4151 dwarf_encoding_name (unsigned int code)
4153 const char *ret = dwarf_encoding_string (code);
4154 return string_or_unknown (ret, code, DW_ATE_lo_user, DW_ATE_hi_user, false);
4159 dwarf_access_name (unsigned int code)
4161 const char *ret = dwarf_access_string (code);
4162 return string_or_unknown (ret, code, 0, 0, false);
4167 dwarf_defaulted_name (unsigned int code)
4169 const char *ret = dwarf_defaulted_string (code);
4170 return string_or_unknown (ret, code, 0, 0, false);
4175 dwarf_visibility_name (unsigned int code)
4177 const char *ret = dwarf_visibility_string (code);
4178 return string_or_unknown (ret, code, 0, 0, false);
4183 dwarf_virtuality_name (unsigned int code)
4185 const char *ret = dwarf_virtuality_string (code);
4186 return string_or_unknown (ret, code, 0, 0, false);
4191 dwarf_identifier_case_name (unsigned int code)
4193 const char *ret = dwarf_identifier_case_string (code);
4194 return string_or_unknown (ret, code, 0, 0, false);
4199 dwarf_calling_convention_name (unsigned int code)
4201 const char *ret = dwarf_calling_convention_string (code);
4202 return string_or_unknown (ret, code, DW_CC_lo_user, DW_CC_hi_user, false);
4207 dwarf_ordering_name (unsigned int code)
4209 const char *ret = dwarf_ordering_string (code);
4210 return string_or_unknown (ret, code, 0, 0, false);
4215 dwarf_discr_list_name (unsigned int code)
4217 const char *ret = dwarf_discr_list_string (code);
4218 return string_or_unknown (ret, code, 0, 0, false);
4223 dwarf_unit_name (unsigned int type)
4225 const char *ret = dwarf_unit_string (type);
4226 return string_or_unknown (ret, type, DW_UT_lo_user, DW_UT_hi_user, true);
4231 dwarf_range_list_encoding_name (unsigned int kind)
4233 const char *ret = dwarf_range_list_encoding_string (kind);
4234 return string_or_unknown (ret, kind, 0, 0, false);
4239 dwarf_loc_list_encoding_name (unsigned int kind)
4241 const char *ret = dwarf_loc_list_encoding_string (kind);
4242 return string_or_unknown (ret, kind, 0, 0, false);
4247 dwarf_line_content_description_name (unsigned int kind)
4249 const char *ret = dwarf_line_content_description_string (kind);
4250 return string_or_unknown (ret, kind, DW_LNCT_lo_user, DW_LNCT_hi_user,
4256 print_block (size_t n, const void *block)
4259 puts (_("empty block"));
4262 printf (_("%zu byte block:"), n);
4263 const unsigned char *data = block;
4265 printf (" %02x", *data++);
4272 print_bytes (size_t n, const unsigned char *bytes)
4276 printf ("%02x", *bytes++);
4283 get_indexed_addr (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr)
4288 Elf_Data *debug_addr = cu->dbg->sectiondata[IDX_debug_addr];
4289 if (debug_addr == NULL)
4292 Dwarf_Off base = __libdw_cu_addr_base (cu);
4293 Dwarf_Word off = idx * cu->address_size;
4294 if (base > debug_addr->d_size
4295 || off > debug_addr->d_size - base
4296 || cu->address_size > debug_addr->d_size - base - off)
4299 const unsigned char *addrp = debug_addr->d_buf + base + off;
4300 if (cu->address_size == 4)
4301 *addr = read_4ubyte_unaligned (cu->dbg, addrp);
4303 *addr = read_8ubyte_unaligned (cu->dbg, addrp);
4309 print_ops (Dwfl_Module *dwflmod, Dwarf *dbg, int indent, int indentrest,
4310 unsigned int vers, unsigned int addrsize, unsigned int offset_size,
4311 struct Dwarf_CU *cu, Dwarf_Word len, const unsigned char *data)
4313 const unsigned int ref_size = vers < 3 ? addrsize : offset_size;
4317 printf ("%*s(empty)\n", indent, "");
4321 #define NEED(n) if (len < (Dwarf_Word) (n)) goto invalid
4322 #define CONSUME(n) NEED (n); else len -= (n)
4324 Dwarf_Word offset = 0;
4327 uint_fast8_t op = *data++;
4329 const char *op_name = dwarf_locexpr_opcode_string (op);
4330 if (unlikely (op_name == NULL))
4332 static char buf[20];
4333 if (op >= DW_OP_lo_user)
4334 snprintf (buf, sizeof buf, "lo_user+%#x", op - DW_OP_lo_user);
4336 snprintf (buf, sizeof buf, "??? (%#x)", op);
4343 /* Address operand. */
4347 addr = read_4ubyte_unaligned (dbg, data);
4348 else if (addrsize == 8)
4349 addr = read_8ubyte_unaligned (dbg, data);
4355 char *a = format_dwarf_addr (dwflmod, 0, addr, addr);
4356 printf ("%*s[%2" PRIuMAX "] %s %s\n",
4357 indent, "", (uintmax_t) offset, op_name, a);
4360 offset += 1 + addrsize;
4363 case DW_OP_call_ref:
4364 case DW_OP_GNU_variable_value:
4365 /* Offset operand. */
4366 if (ref_size != 4 && ref_size != 8)
4367 goto invalid; /* Cannot be used in CFA. */
4370 addr = read_4ubyte_unaligned (dbg, data);
4372 addr = read_8ubyte_unaligned (dbg, data);
4375 /* addr is a DIE offset, so format it as one. */
4376 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4377 indent, "", (uintmax_t) offset,
4378 op_name, (uintmax_t) addr);
4379 offset += 1 + ref_size;
4382 case DW_OP_deref_size:
4383 case DW_OP_xderef_size:
4386 // XXX value might be modified by relocation
4388 printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 "\n",
4389 indent, "", (uintmax_t) offset,
4390 op_name, *((uint8_t *) data));
4398 // XXX value might be modified by relocation
4399 printf ("%*s[%2" PRIuMAX "] %s %" PRIu16 "\n",
4400 indent, "", (uintmax_t) offset,
4401 op_name, read_2ubyte_unaligned (dbg, data));
4409 // XXX value might be modified by relocation
4410 printf ("%*s[%2" PRIuMAX "] %s %" PRIu32 "\n",
4411 indent, "", (uintmax_t) offset,
4412 op_name, read_4ubyte_unaligned (dbg, data));
4420 // XXX value might be modified by relocation
4421 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4422 indent, "", (uintmax_t) offset,
4423 op_name, (uint64_t) read_8ubyte_unaligned (dbg, data));
4431 // XXX value might be modified by relocation
4432 printf ("%*s[%2" PRIuMAX "] %s %" PRId8 "\n",
4433 indent, "", (uintmax_t) offset,
4434 op_name, *((int8_t *) data));
4442 // XXX value might be modified by relocation
4443 printf ("%*s[%2" PRIuMAX "] %s %" PRId16 "\n",
4444 indent, "", (uintmax_t) offset,
4445 op_name, read_2sbyte_unaligned (dbg, data));
4453 // XXX value might be modified by relocation
4454 printf ("%*s[%2" PRIuMAX "] %s %" PRId32 "\n",
4455 indent, "", (uintmax_t) offset,
4456 op_name, read_4sbyte_unaligned (dbg, data));
4464 // XXX value might be modified by relocation
4465 printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4466 indent, "", (uintmax_t) offset,
4467 op_name, read_8sbyte_unaligned (dbg, data));
4475 case DW_OP_plus_uconst:
4477 const unsigned char *start = data;
4480 get_uleb128 (uleb, data, data + len);
4481 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 "\n",
4482 indent, "", (uintmax_t) offset, op_name, uleb);
4483 CONSUME (data - start);
4484 offset += 1 + (data - start);
4488 case DW_OP_GNU_addr_index:
4490 case DW_OP_GNU_const_index:;
4493 get_uleb128 (uleb, data, data + len);
4494 printf ("%*s[%2" PRIuMAX "] %s [%" PRIu64 "] ",
4495 indent, "", (uintmax_t) offset, op_name, uleb);
4496 CONSUME (data - start);
4497 offset += 1 + (data - start);
4498 if (get_indexed_addr (cu, uleb, &addr) != 0)
4502 a = format_dwarf_addr (dwflmod, 0, addr, addr);
4508 case DW_OP_bit_piece:
4512 get_uleb128 (uleb, data, data + len);
4514 get_uleb128 (uleb2, data, data + len);
4515 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 ", %" PRIu64 "\n",
4516 indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4517 CONSUME (data - start);
4518 offset += 1 + (data - start);
4522 case DW_OP_breg0 ... DW_OP_breg31:
4527 get_sleb128 (sleb, data, data + len);
4528 printf ("%*s[%2" PRIuMAX "] %s %" PRId64 "\n",
4529 indent, "", (uintmax_t) offset, op_name, sleb);
4530 CONSUME (data - start);
4531 offset += 1 + (data - start);
4537 get_uleb128 (uleb, data, data + len);
4539 get_sleb128 (sleb, data, data + len);
4540 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " %" PRId64 "\n",
4541 indent, "", (uintmax_t) offset, op_name, uleb, sleb);
4542 CONSUME (data - start);
4543 offset += 1 + (data - start);
4548 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx16 "]\n",
4549 indent, "", (uintmax_t) offset, op_name,
4550 read_2ubyte_unaligned (dbg, data));
4558 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIx32 "]\n",
4559 indent, "", (uintmax_t) offset, op_name,
4560 read_4ubyte_unaligned (dbg, data));
4569 printf ("%*s[%2" PRIuMAX "] %s %" PRIuMAX "\n",
4570 indent, "", (uintmax_t) offset, op_name,
4571 (uintmax_t) (offset + read_2sbyte_unaligned (dbg, data) + 3));
4577 case DW_OP_implicit_value:
4580 get_uleb128 (uleb, data, data + len);
4581 printf ("%*s[%2" PRIuMAX "] %s: ",
4582 indent, "", (uintmax_t) offset, op_name);
4584 print_block (uleb, data);
4586 CONSUME (data - start);
4587 offset += 1 + (data - start);
4590 case DW_OP_implicit_pointer:
4591 case DW_OP_GNU_implicit_pointer:
4592 /* DIE offset operand. */
4595 if (ref_size != 4 && ref_size != 8)
4596 goto invalid; /* Cannot be used in CFA. */
4598 addr = read_4ubyte_unaligned (dbg, data);
4600 addr = read_8ubyte_unaligned (dbg, data);
4602 /* Byte offset operand. */
4604 get_sleb128 (sleb, data, data + len);
4606 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] %+" PRId64 "\n",
4607 indent, "", (intmax_t) offset,
4608 op_name, (uintmax_t) addr, sleb);
4609 CONSUME (data - start);
4610 offset += 1 + (data - start);
4613 case DW_OP_entry_value:
4614 case DW_OP_GNU_entry_value:
4615 /* Size plus expression block. */
4618 get_uleb128 (uleb, data, data + len);
4619 printf ("%*s[%2" PRIuMAX "] %s:\n",
4620 indent, "", (uintmax_t) offset, op_name);
4622 print_ops (dwflmod, dbg, indent + 5, indent + 5, vers,
4623 addrsize, offset_size, cu, uleb, data);
4625 CONSUME (data - start);
4626 offset += 1 + (data - start);
4629 case DW_OP_const_type:
4630 case DW_OP_GNU_const_type:
4631 /* uleb128 CU relative DW_TAG_base_type DIE offset, 1-byte
4632 unsigned size plus block. */
4635 get_uleb128 (uleb, data, data + len);
4636 if (! print_unresolved_addresses && cu != NULL)
4639 uint8_t usize = *(uint8_t *) data++;
4641 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "] ",
4642 indent, "", (uintmax_t) offset, op_name, uleb);
4643 print_block (usize, data);
4645 CONSUME (data - start);
4646 offset += 1 + (data - start);
4649 case DW_OP_regval_type:
4650 case DW_OP_GNU_regval_type:
4651 /* uleb128 register number, uleb128 CU relative
4652 DW_TAG_base_type DIE offset. */
4655 get_uleb128 (uleb, data, data + len);
4657 get_uleb128 (uleb2, data, data + len);
4658 if (! print_unresolved_addresses && cu != NULL)
4660 printf ("%*s[%2" PRIuMAX "] %s %" PRIu64 " [%6" PRIx64 "]\n",
4661 indent, "", (uintmax_t) offset, op_name, uleb, uleb2);
4662 CONSUME (data - start);
4663 offset += 1 + (data - start);
4666 case DW_OP_deref_type:
4667 case DW_OP_GNU_deref_type:
4668 /* 1-byte unsigned size of value, uleb128 CU relative
4669 DW_TAG_base_type DIE offset. */
4672 usize = *(uint8_t *) data++;
4674 get_uleb128 (uleb, data, data + len);
4675 if (! print_unresolved_addresses && cu != NULL)
4677 printf ("%*s[%2" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4678 indent, "", (uintmax_t) offset,
4679 op_name, usize, uleb);
4680 CONSUME (data - start);
4681 offset += 1 + (data - start);
4684 case DW_OP_xderef_type:
4685 /* 1-byte unsigned size of value, uleb128 base_type DIE offset. */
4688 usize = *(uint8_t *) data++;
4690 get_uleb128 (uleb, data, data + len);
4691 printf ("%*s[%4" PRIuMAX "] %s %" PRIu8 " [%6" PRIxMAX "]\n",
4692 indent, "", (uintmax_t) offset,
4693 op_name, usize, uleb);
4694 CONSUME (data - start);
4695 offset += 1 + (data - start);
4699 case DW_OP_GNU_convert:
4700 case DW_OP_reinterpret:
4701 case DW_OP_GNU_reinterpret:
4702 /* uleb128 CU relative offset to DW_TAG_base_type, or zero
4703 for conversion to untyped. */
4706 get_uleb128 (uleb, data, data + len);
4707 if (uleb != 0 && ! print_unresolved_addresses && cu != NULL)
4709 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4710 indent, "", (uintmax_t) offset, op_name, uleb);
4711 CONSUME (data - start);
4712 offset += 1 + (data - start);
4715 case DW_OP_GNU_parameter_ref:
4716 /* 4 byte CU relative reference to the abstract optimized away
4717 DW_TAG_formal_parameter. */
4719 uintmax_t param_off = (uintmax_t) read_4ubyte_unaligned (dbg, data);
4720 if (! print_unresolved_addresses && cu != NULL)
4721 param_off += cu->start;
4722 printf ("%*s[%2" PRIuMAX "] %s [%6" PRIxMAX "]\n",
4723 indent, "", (uintmax_t) offset, op_name, param_off);
4731 printf ("%*s[%2" PRIuMAX "] %s\n",
4732 indent, "", (uintmax_t) offset, op_name);
4737 indent = indentrest;
4741 printf (gettext ("%*s[%2" PRIuMAX "] %s <TRUNCATED>\n"),
4742 indent, "", (uintmax_t) offset, op_name);
4750 Dwarf_Off offset:(64 - 3);
4754 struct Dwarf_CU *cu;
4758 #define listptr_offset_size(p) ((p)->dwarf64 ? 8 : 4)
4759 #define listptr_address_size(p) ((p)->addr64 ? 8 : 4)
4762 cudie_base (Dwarf_Die *cudie)
4765 /* Find the base address of the compilation unit. It will normally
4766 be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base
4767 address could be overridden by DW_AT_entry_pc. It's been
4768 removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for
4769 compilation units with discontinuous ranges. */
4770 if (unlikely (dwarf_lowpc (cudie, &base) != 0))
4772 Dwarf_Attribute attr_mem;
4773 if (dwarf_formaddr (dwarf_attr (cudie, DW_AT_entry_pc, &attr_mem),
4781 listptr_base (struct listptr *p)
4783 Dwarf_Die cu = CUDIE (p->cu);
4784 return cudie_base (&cu);
4788 compare_listptr (const void *a, const void *b, void *arg)
4790 const char *name = arg;
4791 struct listptr *p1 = (void *) a;
4792 struct listptr *p2 = (void *) b;
4794 if (p1->offset < p2->offset)
4796 if (p1->offset > p2->offset)
4799 if (!p1->warned && !p2->warned)
4801 if (p1->addr64 != p2->addr64)
4803 p1->warned = p2->warned = true;
4805 gettext ("%s %#" PRIx64 " used with different address sizes"),
4806 name, (uint64_t) p1->offset);
4808 if (p1->dwarf64 != p2->dwarf64)
4810 p1->warned = p2->warned = true;
4812 gettext ("%s %#" PRIx64 " used with different offset sizes"),
4813 name, (uint64_t) p1->offset);
4815 if (listptr_base (p1) != listptr_base (p2))
4817 p1->warned = p2->warned = true;
4819 gettext ("%s %#" PRIx64 " used with different base addresses"),
4820 name, (uint64_t) p1->offset);
4822 if (p1->attr != p2 ->attr)
4824 p1->warned = p2->warned = true;
4826 gettext ("%s %#" PRIx64
4827 " used with different attribute %s and %s"),
4828 name, (uint64_t) p1->offset, dwarf_attr_name (p2->attr),
4829 dwarf_attr_name (p2->attr));
4836 struct listptr_table
4840 struct listptr *table;
4843 static struct listptr_table known_locsptr;
4844 static struct listptr_table known_loclistsptr;
4845 static struct listptr_table known_rangelistptr;
4846 static struct listptr_table known_rnglistptr;
4847 static struct listptr_table known_addrbases;
4848 static struct listptr_table known_stroffbases;
4851 reset_listptr (struct listptr_table *table)
4853 free (table->table);
4854 table->table = NULL;
4855 table->n = table->alloc = 0;
4858 /* Returns false if offset doesn't fit. See struct listptr. */
4860 notice_listptr (enum section_e section, struct listptr_table *table,
4861 uint_fast8_t address_size, uint_fast8_t offset_size,
4862 struct Dwarf_CU *cu, Dwarf_Off offset, unsigned int attr)
4864 if (print_debug_sections & section)
4866 if (table->n == table->alloc)
4868 if (table->alloc == 0)
4872 table->table = xrealloc (table->table,
4873 table->alloc * sizeof table->table[0]);
4876 struct listptr *p = &table->table[table->n++];
4878 *p = (struct listptr)
4880 .addr64 = address_size == 8,
4881 .dwarf64 = offset_size == 8,
4887 if (p->offset != offset)
4897 sort_listptr (struct listptr_table *table, const char *name)
4900 qsort_r (table->table, table->n, sizeof table->table[0],
4901 &compare_listptr, (void *) name);
4905 skip_listptr_hole (struct listptr_table *table, size_t *idxp,
4906 uint_fast8_t *address_sizep, uint_fast8_t *offset_sizep,
4907 Dwarf_Addr *base, struct Dwarf_CU **cu, ptrdiff_t offset,
4908 unsigned char **readp, unsigned char *endp,
4914 while (*idxp < table->n && table->table[*idxp].offset < (Dwarf_Off) offset)
4917 struct listptr *p = &table->table[*idxp];
4919 if (*idxp == table->n
4920 || p->offset >= (Dwarf_Off) (endp - *readp + offset))
4923 printf (gettext (" [%6tx] <UNUSED GARBAGE IN REST OF SECTION>\n"),
4928 if (p->offset != (Dwarf_Off) offset)
4930 *readp += p->offset - offset;
4931 printf (gettext (" [%6tx] <UNUSED GARBAGE> ... %" PRIu64 " bytes ...\n"),
4932 offset, (Dwarf_Off) p->offset - offset);
4936 if (address_sizep != NULL)
4937 *address_sizep = listptr_address_size (p);
4938 if (offset_sizep != NULL)
4939 *offset_sizep = listptr_offset_size (p);
4941 *base = listptr_base (p);
4951 next_listptr_offset (struct listptr_table *table, size_t idx)
4953 /* Note that multiple attributes could in theory point to the same loclist
4954 offset, so make sure we pick one that is bigger than the current one.
4955 The table is sorted on offset. */
4956 Dwarf_Off offset = table->table[idx].offset;
4957 while (++idx < table->n)
4959 Dwarf_Off next = table->table[idx].offset;
4966 /* Returns the listptr associated with the given index, or NULL. */
4967 static struct listptr *
4968 get_listptr (struct listptr_table *table, size_t idx)
4970 if (idx >= table->n)
4972 return &table->table[idx];
4975 /* Returns the next index, base address and CU associated with the
4976 list unit offsets. If there is none false is returned, otherwise
4977 true. Assumes the table has been sorted. */
4979 listptr_cu (struct listptr_table *table, size_t *idxp,
4980 Dwarf_Off start, Dwarf_Off end,
4981 Dwarf_Addr *base, struct Dwarf_CU **cu)
4983 while (*idxp < table->n
4984 && table->table[*idxp].offset < start)
4987 if (*idxp < table->n
4988 && table->table[*idxp].offset >= start
4989 && table->table[*idxp].offset < end)
4991 struct listptr *p = &table->table[*idxp];
4992 *base = listptr_base (p);
5002 print_debug_abbrev_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5003 Ebl *ebl, GElf_Ehdr *ehdr,
5004 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5006 const size_t sh_size = (dbg->sectiondata[IDX_debug_abbrev] ?
5007 dbg->sectiondata[IDX_debug_abbrev]->d_size : 0);
5009 printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
5011 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5012 (uint64_t) shdr->sh_offset);
5014 Dwarf_Off offset = 0;
5015 while (offset < sh_size)
5017 printf (gettext ("\nAbbreviation section at offset %" PRIu64 ":\n"),
5023 Dwarf_Abbrev abbrev;
5025 int res = dwarf_offabbrev (dbg, offset, &length, &abbrev);
5028 if (unlikely (res < 0))
5031 *** error while reading abbreviation: %s\n"),
5036 /* This is the NUL byte at the end of the section. */
5041 /* We know these calls can never fail. */
5042 unsigned int code = dwarf_getabbrevcode (&abbrev);
5043 unsigned int tag = dwarf_getabbrevtag (&abbrev);
5044 int has_children = dwarf_abbrevhaschildren (&abbrev);
5046 printf (gettext (" [%5u] offset: %" PRId64
5047 ", children: %s, tag: %s\n"),
5048 code, (int64_t) offset,
5049 has_children ? gettext ("yes") : gettext ("no"),
5050 dwarf_tag_name (tag));
5057 while (dwarf_getabbrevattr_data (&abbrev, cnt, &name, &form,
5058 &data, &enoffset) == 0)
5060 printf (" attr: %s, form: %s",
5061 dwarf_attr_name (name), dwarf_form_name (form));
5062 if (form == DW_FORM_implicit_const)
5063 printf (" (%" PRId64 ")", data);
5064 printf (", offset: %#" PRIx64 "\n", (uint64_t) enoffset);
5075 print_debug_addr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5076 Ebl *ebl, GElf_Ehdr *ehdr,
5077 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
5080 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5081 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5082 (uint64_t) shdr->sh_offset);
5084 if (shdr->sh_size == 0)
5087 /* We like to get the section from libdw to make sure they are relocated. */
5088 Elf_Data *data = (dbg->sectiondata[IDX_debug_addr]
5089 ?: elf_rawdata (scn, NULL));
5090 if (unlikely (data == NULL))
5092 error (0, 0, gettext ("cannot get .debug_addr section data: %s"),
5098 sort_listptr (&known_addrbases, "addr_base");
5100 const unsigned char *start = (const unsigned char *) data->d_buf;
5101 const unsigned char *readp = start;
5102 const unsigned char *readendp = ((const unsigned char *) data->d_buf
5105 while (readp < readendp)
5107 /* We cannot really know whether or not there is an header. The
5108 DebugFission extension to DWARF4 doesn't add one. The DWARF5
5109 .debug_addr variant does. Whether or not we have an header,
5110 DW_AT_[GNU_]addr_base points at "index 0". So if the current
5111 offset equals the CU addr_base then we can just start
5112 printing addresses. If there is no CU with an exact match
5113 then we'll try to parse the header first. */
5114 Dwarf_Off off = (Dwarf_Off) (readp
5115 - (const unsigned char *) data->d_buf);
5117 printf ("Table at offset %" PRIx64 " ", off);
5119 struct listptr *listptr = get_listptr (&known_addrbases, idx++);
5120 const unsigned char *next_unitp;
5122 uint64_t unit_length;
5124 uint8_t address_size;
5125 uint8_t segment_size;
5126 if (listptr == NULL)
5128 error (0, 0, "Warning: No CU references .debug_addr after %" PRIx64,
5131 /* We will have to assume it is just addresses to the end... */
5132 address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5133 next_unitp = readendp;
5134 printf ("Unknown CU:\n");
5139 if (dwarf_cu_die (listptr->cu, &cudie,
5140 NULL, NULL, NULL, NULL,
5141 NULL, NULL) == NULL)
5142 printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
5144 printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
5146 if (listptr->offset == off)
5148 address_size = listptr_address_size (listptr);
5152 /* The addresses start here, but where do they end? */
5153 listptr = get_listptr (&known_addrbases, idx);
5154 if (listptr == NULL)
5156 next_unitp = readendp;
5157 unit_length = (uint64_t) (next_unitp - readp);
5159 else if (listptr->cu->version < 5)
5161 next_unitp = start + listptr->offset;
5162 if (listptr->offset < off || listptr->offset > data->d_size)
5165 "Warning: Bad address base for next unit at %"
5167 next_unitp = readendp;
5169 unit_length = (uint64_t) (next_unitp - readp);
5173 /* Tricky, we don't have a header for this unit, but
5174 there is one for the next. We will have to
5175 "guess" how big it is and subtract it from the
5176 offset (because that points after the header). */
5177 unsigned int offset_size = listptr_offset_size (listptr);
5178 Dwarf_Off next_off = (listptr->offset
5179 - (offset_size == 4 ? 4 : 12) /* len */
5181 - 1 /* address size */
5182 - 1); /* segment selector size */
5183 next_unitp = start + next_off;
5184 if (next_off < off || next_off > data->d_size)
5187 "Warning: Couldn't calculate .debug_addr "
5188 " unit lenght at %" PRIx64, off);
5189 next_unitp = readendp;
5193 /* Pretend we have a header. */
5195 printf (gettext (" Length: %8" PRIu64 "\n"),
5197 printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
5198 printf (gettext (" Address size: %8" PRIu64 "\n"),
5199 (uint64_t) address_size);
5200 printf (gettext (" Segment size: %8" PRIu64 "\n"),
5201 (uint64_t) segment_size);
5206 /* OK, we have to parse an header first. */
5207 unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5208 if (unlikely (unit_length == 0xffffffff))
5210 if (unlikely (readp > readendp - 8))
5213 error (0, 0, "Invalid data");
5216 unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5219 printf (gettext (" Length: %8" PRIu64 "\n"),
5222 /* We need at least 2-bytes (version) + 1-byte
5223 (addr_size) + 1-byte (segment_size) = 4 bytes to
5224 complete the header. And this unit cannot go beyond
5225 the section data. */
5226 if (readp > readendp - 4
5228 || unit_length > (uint64_t) (readendp - readp))
5231 next_unitp = readp + unit_length;
5233 version = read_2ubyte_unaligned_inc (dbg, readp);
5234 printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
5238 error (0, 0, gettext ("Unknown version"));
5242 address_size = *readp++;
5243 printf (gettext (" Address size: %8" PRIu64 "\n"),
5244 (uint64_t) address_size);
5246 if (address_size != 4 && address_size != 8)
5248 error (0, 0, gettext ("unsupported address size"));
5252 segment_size = *readp++;
5253 printf (gettext (" Segment size: %8" PRIu64 "\n"),
5254 (uint64_t) segment_size);
5257 if (segment_size != 0)
5259 error (0, 0, gettext ("unsupported segment size"));
5263 if (listptr->offset != (Dwarf_Off) (readp - start))
5265 error (0, 0, "Address index doesn't start after header");
5272 size_t addresses = (next_unitp - readp) / address_size;
5273 while (addresses >= 10)
5279 unsigned int index = 0;
5280 size_t index_offset = readp - (const unsigned char *) data->d_buf;
5281 printf (" Addresses start at offset 0x%zx:\n", index_offset);
5282 while (readp <= next_unitp - address_size)
5284 Dwarf_Addr addr = read_addr_unaligned_inc (address_size, dbg,
5286 printf (" [%*u] ", digits, index++);
5287 char *a = format_dwarf_addr (dwflmod, address_size,
5294 if (readp != next_unitp)
5295 error (0, 0, "extra %zd bytes at end of unit",
5296 (size_t) (next_unitp - readp));
5303 /* Print content of DWARF .debug_aranges section. We fortunately do
5304 not have to know a bit about the structure of the section, libdwarf
5305 takes care of it. */
5307 print_decoded_aranges_section (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5308 GElf_Shdr *shdr, Dwarf *dbg)
5310 Dwarf_Aranges *aranges;
5312 if (unlikely (dwarf_getaranges (dbg, &aranges, &cnt) != 0))
5314 error (0, 0, gettext ("cannot get .debug_aranges content: %s"),
5319 GElf_Shdr glink_mem;
5321 glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
5324 error (0, 0, gettext ("invalid sh_link value in section %zu"),
5329 printf (ngettext ("\
5330 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entry:\n",
5332 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 " contains %zu entries:\n",
5334 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5335 (uint64_t) shdr->sh_offset, cnt);
5337 /* Compute floor(log16(cnt)). */
5346 for (size_t n = 0; n < cnt; ++n)
5348 Dwarf_Arange *runp = dwarf_onearange (aranges, n);
5349 if (unlikely (runp == NULL))
5351 printf ("cannot get arange %zu: %s\n", n, dwarf_errmsg (-1));
5359 if (unlikely (dwarf_getarangeinfo (runp, &start, &length, &offset) != 0))
5360 printf (gettext (" [%*zu] ???\n"), digits, n);
5362 printf (gettext (" [%*zu] start: %0#*" PRIx64
5363 ", length: %5" PRIu64 ", CU DIE offset: %6"
5365 digits, n, ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 10 : 18,
5366 (uint64_t) start, (uint64_t) length, (int64_t) offset);
5371 /* Print content of DWARF .debug_aranges section. */
5373 print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
5374 Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn,
5375 GElf_Shdr *shdr, Dwarf *dbg)
5379 print_decoded_aranges_section (ebl, ehdr, scn, shdr, dbg);
5383 Elf_Data *data = (dbg->sectiondata[IDX_debug_aranges]
5384 ?: elf_rawdata (scn, NULL));
5386 if (unlikely (data == NULL))
5388 error (0, 0, gettext ("cannot get .debug_aranges content: %s"),
5394 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5395 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5396 (uint64_t) shdr->sh_offset);
5398 const unsigned char *readp = data->d_buf;
5399 const unsigned char *readendp = readp + data->d_size;
5401 while (readp < readendp)
5403 const unsigned char *hdrstart = readp;
5404 size_t start_offset = hdrstart - (const unsigned char *) data->d_buf;
5406 printf (gettext ("\nTable at offset %zu:\n"), start_offset);
5407 if (readp + 4 > readendp)
5410 error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
5411 elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
5415 Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
5416 unsigned int length_bytes = 4;
5417 if (length == DWARF3_LENGTH_64_BIT)
5419 if (readp + 8 > readendp)
5421 length = read_8ubyte_unaligned_inc (dbg, readp);
5425 const unsigned char *nexthdr = readp + length;
5426 printf (gettext ("\n Length: %6" PRIu64 "\n"),
5429 if (unlikely (length > (size_t) (readendp - readp)))
5435 if (readp + 2 > readendp)
5437 uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5438 printf (gettext (" DWARF version: %6" PRIuFAST16 "\n"),
5442 error (0, 0, gettext ("unsupported aranges version"));
5447 if (readp + length_bytes > readendp)
5449 if (length_bytes == 8)
5450 offset = read_8ubyte_unaligned_inc (dbg, readp);
5452 offset = read_4ubyte_unaligned_inc (dbg, readp);
5453 printf (gettext (" CU offset: %6" PRIx64 "\n"),
5456 if (readp + 1 > readendp)
5458 unsigned int address_size = *readp++;
5459 printf (gettext (" Address size: %6" PRIu64 "\n"),
5460 (uint64_t) address_size);
5461 if (address_size != 4 && address_size != 8)
5463 error (0, 0, gettext ("unsupported address size"));
5467 unsigned int segment_size = *readp++;
5468 printf (gettext (" Segment size: %6" PRIu64 "\n\n"),
5469 (uint64_t) segment_size);
5470 if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5472 error (0, 0, gettext ("unsupported segment size"));
5476 /* Round the address to the next multiple of 2*address_size. */
5477 readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
5478 % (2 * address_size));
5480 while (readp < nexthdr)
5482 Dwarf_Word range_address;
5483 Dwarf_Word range_length;
5484 Dwarf_Word segment = 0;
5485 if (readp + 2 * address_size + segment_size > readendp)
5487 if (address_size == 4)
5489 range_address = read_4ubyte_unaligned_inc (dbg, readp);
5490 range_length = read_4ubyte_unaligned_inc (dbg, readp);
5494 range_address = read_8ubyte_unaligned_inc (dbg, readp);
5495 range_length = read_8ubyte_unaligned_inc (dbg, readp);
5498 if (segment_size == 4)
5499 segment = read_4ubyte_unaligned_inc (dbg, readp);
5500 else if (segment_size == 8)
5501 segment = read_8ubyte_unaligned_inc (dbg, readp);
5503 if (range_address == 0 && range_length == 0 && segment == 0)
5506 char *b = format_dwarf_addr (dwflmod, address_size, range_address,
5508 char *e = format_dwarf_addr (dwflmod, address_size,
5509 range_address + range_length - 1,
5511 if (segment_size != 0)
5512 printf (gettext (" %s..%s (%" PRIx64 ")\n"), b, e,
5513 (uint64_t) segment);
5515 printf (gettext (" %s..%s\n"), b, e);
5521 if (readp != nexthdr)
5523 size_t padding = nexthdr - readp;
5524 printf (gettext (" %zu padding bytes\n"), padding);
5531 static bool is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu);
5533 /* Returns true and sets cu and cu_base if the given Dwarf is a split
5534 DWARF (.dwo) file. */
5536 split_dwarf_cu_base (Dwarf *dbg, Dwarf_CU **cu, Dwarf_Addr *cu_base)
5539 if (is_split_dwarf (dbg, &id, cu))
5542 if (dwarf_cu_info (*cu, NULL, NULL, &cudie, NULL, NULL, NULL, NULL) == 0)
5544 *cu_base = cudie_base (&cudie);
5551 /* Print content of DWARF .debug_rnglists section. */
5553 print_debug_rnglists_section (Dwfl_Module *dwflmod,
5554 Ebl *ebl, GElf_Ehdr *ehdr,
5555 Elf_Scn *scn, GElf_Shdr *shdr,
5556 Dwarf *dbg __attribute__((unused)))
5559 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5560 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5561 (uint64_t) shdr->sh_offset);
5563 Elf_Data *data =(dbg->sectiondata[IDX_debug_rnglists]
5564 ?: elf_rawdata (scn, NULL));
5565 if (unlikely (data == NULL))
5567 error (0, 0, gettext ("cannot get .debug_rnglists content: %s"),
5572 /* For the listptr to get the base address/CU. */
5573 sort_listptr (&known_rnglistptr, "rnglistptr");
5574 size_t listptr_idx = 0;
5576 const unsigned char *readp = data->d_buf;
5577 const unsigned char *const dataend = ((unsigned char *) data->d_buf
5579 while (readp < dataend)
5581 if (unlikely (readp > dataend - 4))
5584 error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
5585 elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
5589 ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
5590 printf (gettext ("Table at Offset 0x%" PRIx64 ":\n\n"),
5593 uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
5594 unsigned int offset_size = 4;
5595 if (unlikely (unit_length == 0xffffffff))
5597 if (unlikely (readp > dataend - 8))
5600 unit_length = read_8ubyte_unaligned_inc (dbg, readp);
5603 printf (gettext (" Length: %8" PRIu64 "\n"), unit_length);
5605 /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
5606 bytes to complete the header. And this unit cannot go beyond
5607 the section data. */
5608 if (readp > dataend - 8
5610 || unit_length > (uint64_t) (dataend - readp))
5613 const unsigned char *nexthdr = readp + unit_length;
5615 uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
5616 printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
5620 error (0, 0, gettext ("Unknown version"));
5624 uint8_t address_size = *readp++;
5625 printf (gettext (" Address size: %8" PRIu64 "\n"),
5626 (uint64_t) address_size);
5628 if (address_size != 4 && address_size != 8)
5630 error (0, 0, gettext ("unsupported address size"));
5634 uint8_t segment_size = *readp++;
5635 printf (gettext (" Segment size: %8" PRIu64 "\n"),
5636 (uint64_t) segment_size);
5638 if (segment_size != 0 && segment_size != 4 && segment_size != 8)
5640 error (0, 0, gettext ("unsupported segment size"));
5644 uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
5645 printf (gettext (" Offset entries: %8" PRIu64 "\n"),
5646 (uint64_t) offset_entry_count);
5648 /* We need the CU that uses this unit to get the initial base address. */
5649 Dwarf_Addr cu_base = 0;
5650 struct Dwarf_CU *cu = NULL;
5651 if (listptr_cu (&known_rnglistptr, &listptr_idx,
5653 (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
5655 || split_dwarf_cu_base (dbg, &cu, &cu_base))
5657 char *basestr = format_dwarf_addr (dwflmod, address_size,
5660 if (dwarf_cu_die (cu, &cudie,
5661 NULL, NULL, NULL, NULL,
5662 NULL, NULL) == NULL)
5663 printf (gettext (" Unknown CU base: %s\n"), basestr);
5665 printf (gettext (" CU [%6" PRIx64 "] base: %s\n"),
5666 dwarf_dieoffset (&cudie), basestr);
5670 printf (gettext (" Not associated with a CU.\n"));
5674 const unsigned char *offset_array_start = readp;
5675 if (offset_entry_count > 0)
5677 uint64_t needed = offset_entry_count * offset_size;
5678 if (unit_length - 8 < needed)
5681 gettext ("too many offset entries for unit length"));
5685 printf (gettext (" Offsets starting at 0x%" PRIx64 ":\n"),
5686 (uint64_t) (offset_array_start
5687 - (unsigned char *) data->d_buf));
5688 for (uint32_t idx = 0; idx < offset_entry_count; idx++)
5690 printf (" [%6" PRIu32 "] ", idx);
5691 if (offset_size == 4)
5693 uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
5694 printf ("0x%" PRIx32 "\n", off);
5698 uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
5699 printf ("0x%" PRIx64 "\n", off);
5705 Dwarf_Addr base = cu_base;
5706 bool start_of_list = true;
5707 while (readp < nexthdr)
5709 uint8_t kind = *readp++;
5714 if (start_of_list && kind == DW_RLE_end_of_list)
5720 printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
5721 (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
5722 (uint64_t) (readp - offset_array_start - 1));
5723 start_of_list = false;
5726 printf (" %s", dwarf_range_list_encoding_name (kind));
5729 case DW_RLE_end_of_list:
5730 start_of_list = true;
5734 case DW_RLE_base_addressx:
5735 if ((uint64_t) (nexthdr - readp) < 1)
5738 error (0, 0, gettext ("invalid range list data"));
5741 get_uleb128 (op1, readp, nexthdr);
5742 printf (" %" PRIx64 "\n", op1);
5743 if (! print_unresolved_addresses)
5746 if (get_indexed_addr (cu, op1, &addr) != 0)
5750 a1 = format_dwarf_addr (dwflmod, address_size,
5752 printf (" %s\n", a1);
5758 case DW_RLE_startx_endx:
5759 if ((uint64_t) (nexthdr - readp) < 1)
5761 get_uleb128 (op1, readp, nexthdr);
5762 if ((uint64_t) (nexthdr - readp) < 1)
5764 get_uleb128 (op2, readp, nexthdr);
5765 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5766 if (! print_unresolved_addresses)
5770 if (get_indexed_addr (cu, op1, &addr1) != 0
5771 || get_indexed_addr (cu, op2, &addr2) != 0)
5773 printf (" ???..\n");
5778 a1 = format_dwarf_addr (dwflmod, address_size,
5780 a2 = format_dwarf_addr (dwflmod, address_size,
5782 printf (" %s..\n", a1);
5783 printf (" %s\n", a2);
5790 case DW_RLE_startx_length:
5791 if ((uint64_t) (nexthdr - readp) < 1)
5793 get_uleb128 (op1, readp, nexthdr);
5794 if ((uint64_t) (nexthdr - readp) < 1)
5796 get_uleb128 (op2, readp, nexthdr);
5797 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5798 if (! print_unresolved_addresses)
5802 if (get_indexed_addr (cu, op1, &addr1) != 0)
5804 printf (" ???..\n");
5809 addr2 = addr1 + op2;
5810 a1 = format_dwarf_addr (dwflmod, address_size,
5812 a2 = format_dwarf_addr (dwflmod, address_size,
5814 printf (" %s..\n", a1);
5815 printf (" %s..\n", a2);
5822 case DW_RLE_offset_pair:
5823 if ((uint64_t) (nexthdr - readp) < 1)
5825 get_uleb128 (op1, readp, nexthdr);
5826 if ((uint64_t) (nexthdr - readp) < 1)
5828 get_uleb128 (op2, readp, nexthdr);
5829 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
5830 if (! print_unresolved_addresses)
5834 a1 = format_dwarf_addr (dwflmod, address_size, op1, op1);
5835 a2 = format_dwarf_addr (dwflmod, address_size,
5837 printf (" %s..\n", a1);
5838 printf (" %s\n", a2);
5844 case DW_RLE_base_address:
5845 if (address_size == 4)
5847 if ((uint64_t) (nexthdr - readp) < 4)
5849 op1 = read_4ubyte_unaligned_inc (dbg, readp);
5853 if ((uint64_t) (nexthdr - readp) < 8)
5855 op1 = read_8ubyte_unaligned_inc (dbg, readp);
5858 printf (" 0x%" PRIx64 "\n", base);
5859 if (! print_unresolved_addresses)
5861 a1 = format_dwarf_addr (dwflmod, address_size, base, base);
5862 printf (" %s\n", a1);
5867 case DW_RLE_start_end:
5868 if (address_size == 4)
5870 if ((uint64_t) (nexthdr - readp) < 8)
5872 op1 = read_4ubyte_unaligned_inc (dbg, readp);
5873 op2 = read_4ubyte_unaligned_inc (dbg, readp);
5877 if ((uint64_t) (nexthdr - readp) < 16)
5879 op1 = read_8ubyte_unaligned_inc (dbg, readp);
5880 op2 = read_8ubyte_unaligned_inc (dbg, readp);
5882 printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
5883 if (! print_unresolved_addresses)
5885 a1 = format_dwarf_addr (dwflmod, address_size, op1, op1);
5886 a2 = format_dwarf_addr (dwflmod, address_size,
5888 printf (" %s..\n", a1);
5889 printf (" %s\n", a2);
5895 case DW_RLE_start_length:
5896 if (address_size == 4)
5898 if ((uint64_t) (nexthdr - readp) < 4)
5900 op1 = read_4ubyte_unaligned_inc (dbg, readp);
5904 if ((uint64_t) (nexthdr - readp) < 8)
5906 op1 = read_8ubyte_unaligned_inc (dbg, readp);
5908 if ((uint64_t) (nexthdr - readp) < 1)
5910 get_uleb128 (op2, readp, nexthdr);
5911 printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
5912 if (! print_unresolved_addresses)
5914 a1 = format_dwarf_addr (dwflmod, address_size, op1, op1);
5916 a2 = format_dwarf_addr (dwflmod, address_size,
5918 printf (" %s..\n", a1);
5919 printf (" %s\n", a2);
5931 if (readp != nexthdr)
5933 size_t padding = nexthdr - readp;
5934 printf (gettext (" %zu padding bytes\n\n"), padding);
5940 /* Print content of DWARF .debug_ranges section. */
5942 print_debug_ranges_section (Dwfl_Module *dwflmod,
5943 Ebl *ebl, GElf_Ehdr *ehdr,
5944 Elf_Scn *scn, GElf_Shdr *shdr,
5947 Elf_Data *data = (dbg->sectiondata[IDX_debug_ranges]
5948 ?: elf_rawdata (scn, NULL));
5949 if (unlikely (data == NULL))
5951 error (0, 0, gettext ("cannot get .debug_ranges content: %s"),
5957 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
5958 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
5959 (uint64_t) shdr->sh_offset);
5961 sort_listptr (&known_rangelistptr, "rangelistptr");
5962 size_t listptr_idx = 0;
5964 uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
5967 Dwarf_Addr base = 0;
5968 unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
5969 unsigned char *readp = data->d_buf;
5970 Dwarf_CU *last_cu = NULL;
5971 while (readp < endp)
5973 ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
5974 Dwarf_CU *cu = last_cu;
5976 if (first && skip_listptr_hole (&known_rangelistptr, &listptr_idx,
5977 &address_size, NULL, &base, &cu,
5978 offset, &readp, endp, NULL))
5983 char *basestr = format_dwarf_addr (dwflmod, address_size,
5986 if (dwarf_cu_die (cu, &cudie,
5987 NULL, NULL, NULL, NULL,
5988 NULL, NULL) == NULL)
5989 printf (gettext ("\n Unknown CU base: %s\n"), basestr);
5991 printf (gettext ("\n CU [%6" PRIx64 "] base: %s\n"),
5992 dwarf_dieoffset (&cudie), basestr);
5997 if (unlikely (data->d_size - offset < (size_t) address_size * 2))
5999 printf (gettext (" [%6tx] <INVALID DATA>\n"), offset);
6005 if (address_size == 8)
6007 begin = read_8ubyte_unaligned_inc (dbg, readp);
6008 end = read_8ubyte_unaligned_inc (dbg, readp);
6012 begin = read_4ubyte_unaligned_inc (dbg, readp);
6013 end = read_4ubyte_unaligned_inc (dbg, readp);
6014 if (begin == (Dwarf_Addr) (uint32_t) -1)
6015 begin = (Dwarf_Addr) -1l;
6018 if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
6020 char *b = format_dwarf_addr (dwflmod, address_size, end, end);
6021 printf (gettext (" [%6tx] base address\n %s\n"), offset, b);
6025 else if (begin == 0 && end == 0) /* End of list entry. */
6028 printf (gettext (" [%6tx] empty list\n"), offset);
6033 /* We have an address range entry. */
6034 if (first) /* First address range entry in a list. */
6035 printf (" [%6tx] ", offset);
6039 printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
6040 if (! print_unresolved_addresses)
6042 char *b = format_dwarf_addr (dwflmod, address_size, base + begin,
6044 char *e = format_dwarf_addr (dwflmod, address_size,
6045 base + end - 1, base + end);
6046 printf (" %s..\n", b);
6047 printf (" %s\n", e);
6057 #define REGNAMESZ 16
6059 register_info (Ebl *ebl, unsigned int regno, const Ebl_Register_Location *loc,
6060 char name[REGNAMESZ], int *bits, int *type)
6065 ssize_t n = ebl_register_info (ebl, regno, name, REGNAMESZ, &pfx, &set,
6066 bits ?: &ignore, type ?: &ignore);
6070 snprintf (name, REGNAMESZ, "reg%u", loc->regno);
6072 snprintf (name, REGNAMESZ, "??? 0x%x", regno);
6074 *bits = loc != NULL ? loc->bits : 0;
6076 *type = DW_ATE_unsigned;
6077 set = "??? unrecognized";
6081 if (bits != NULL && *bits <= 0)
6082 *bits = loc != NULL ? loc->bits : 0;
6083 if (type != NULL && *type == DW_ATE_void)
6084 *type = DW_ATE_unsigned;
6090 static const unsigned char *
6091 read_encoded (unsigned int encoding, const unsigned char *readp,
6092 const unsigned char *const endp, uint64_t *res, Dwarf *dbg)
6094 if ((encoding & 0xf) == DW_EH_PE_absptr)
6095 encoding = gelf_getclass (dbg->elf) == ELFCLASS32
6096 ? DW_EH_PE_udata4 : DW_EH_PE_udata8;
6098 switch (encoding & 0xf)
6100 case DW_EH_PE_uleb128:
6101 get_uleb128 (*res, readp, endp);
6103 case DW_EH_PE_sleb128:
6104 get_sleb128 (*res, readp, endp);
6106 case DW_EH_PE_udata2:
6107 if (readp + 2 > endp)
6109 *res = read_2ubyte_unaligned_inc (dbg, readp);
6111 case DW_EH_PE_udata4:
6112 if (readp + 4 > endp)
6114 *res = read_4ubyte_unaligned_inc (dbg, readp);
6116 case DW_EH_PE_udata8:
6117 if (readp + 8 > endp)
6119 *res = read_8ubyte_unaligned_inc (dbg, readp);
6121 case DW_EH_PE_sdata2:
6122 if (readp + 2 > endp)
6124 *res = read_2sbyte_unaligned_inc (dbg, readp);
6126 case DW_EH_PE_sdata4:
6127 if (readp + 4 > endp)
6129 *res = read_4sbyte_unaligned_inc (dbg, readp);
6131 case DW_EH_PE_sdata8:
6132 if (readp + 8 > endp)
6134 *res = read_8sbyte_unaligned_inc (dbg, readp);
6139 gettext ("invalid encoding"));
6147 print_cfa_program (const unsigned char *readp, const unsigned char *const endp,
6148 Dwarf_Word vma_base, unsigned int code_align,
6150 unsigned int version, unsigned int ptr_size,
6151 unsigned int encoding,
6152 Dwfl_Module *dwflmod, Ebl *ebl, Dwarf *dbg)
6154 char regnamebuf[REGNAMESZ];
6155 const char *regname (unsigned int regno)
6157 register_info (ebl, regno, NULL, regnamebuf, NULL, NULL);
6161 puts ("\n Program:");
6162 Dwarf_Word pc = vma_base;
6163 while (readp < endp)
6165 unsigned int opcode = *readp++;
6167 if (opcode < DW_CFA_advance_loc)
6168 /* Extended opcode. */
6179 case DW_CFA_set_loc:
6180 if ((uint64_t) (endp - readp) < 1)
6182 readp = read_encoded (encoding, readp, endp, &op1, dbg);
6183 printf (" set_loc %#" PRIx64 " to %#" PRIx64 "\n",
6184 op1, pc = vma_base + op1);
6186 case DW_CFA_advance_loc1:
6187 if ((uint64_t) (endp - readp) < 1)
6189 printf (" advance_loc1 %u to %#" PRIx64 "\n",
6190 *readp, pc += *readp * code_align);
6193 case DW_CFA_advance_loc2:
6194 if ((uint64_t) (endp - readp) < 2)
6196 op1 = read_2ubyte_unaligned_inc (dbg, readp);
6197 printf (" advance_loc2 %" PRIu64 " to %#" PRIx64 "\n",
6198 op1, pc += op1 * code_align);
6200 case DW_CFA_advance_loc4:
6201 if ((uint64_t) (endp - readp) < 4)
6203 op1 = read_4ubyte_unaligned_inc (dbg, readp);
6204 printf (" advance_loc4 %" PRIu64 " to %#" PRIx64 "\n",
6205 op1, pc += op1 * code_align);
6207 case DW_CFA_offset_extended:
6208 if ((uint64_t) (endp - readp) < 1)
6210 get_uleb128 (op1, readp, endp);
6211 if ((uint64_t) (endp - readp) < 1)
6213 get_uleb128 (op2, readp, endp);
6214 printf (" offset_extended r%" PRIu64 " (%s) at cfa%+" PRId64
6216 op1, regname (op1), op2 * data_align);
6218 case DW_CFA_restore_extended:
6219 if ((uint64_t) (endp - readp) < 1)
6221 get_uleb128 (op1, readp, endp);
6222 printf (" restore_extended r%" PRIu64 " (%s)\n",
6223 op1, regname (op1));
6225 case DW_CFA_undefined:
6226 if ((uint64_t) (endp - readp) < 1)
6228 get_uleb128 (op1, readp, endp);
6229 printf (" undefined r%" PRIu64 " (%s)\n", op1, regname (op1));
6231 case DW_CFA_same_value:
6232 if ((uint64_t) (endp - readp) < 1)
6234 get_uleb128 (op1, readp, endp);
6235 printf (" same_value r%" PRIu64 " (%s)\n", op1, regname (op1));
6237 case DW_CFA_register:
6238 if ((uint64_t) (endp - readp) < 1)
6240 get_uleb128 (op1, readp, endp);
6241 if ((uint64_t) (endp - readp) < 1)
6243 get_uleb128 (op2, readp, endp);
6244 printf (" register r%" PRIu64 " (%s) in r%" PRIu64 " (%s)\n",
6245 op1, regname (op1), op2, regname (op2));
6247 case DW_CFA_remember_state:
6248 puts (" remember_state");
6250 case DW_CFA_restore_state:
6251 puts (" restore_state");
6253 case DW_CFA_def_cfa:
6254 if ((uint64_t) (endp - readp) < 1)
6256 get_uleb128 (op1, readp, endp);
6257 if ((uint64_t) (endp - readp) < 1)
6259 get_uleb128 (op2, readp, endp);
6260 printf (" def_cfa r%" PRIu64 " (%s) at offset %" PRIu64 "\n",
6261 op1, regname (op1), op2);
6263 case DW_CFA_def_cfa_register:
6264 if ((uint64_t) (endp - readp) < 1)
6266 get_uleb128 (op1, readp, endp);
6267 printf (" def_cfa_register r%" PRIu64 " (%s)\n",
6268 op1, regname (op1));
6270 case DW_CFA_def_cfa_offset:
6271 if ((uint64_t) (endp - readp) < 1)
6273 get_uleb128 (op1, readp, endp);
6274 printf (" def_cfa_offset %" PRIu64 "\n", op1);
6276 case DW_CFA_def_cfa_expression:
6277 if ((uint64_t) (endp - readp) < 1)
6279 get_uleb128 (op1, readp, endp); /* Length of DW_FORM_block. */
6280 printf (" def_cfa_expression %" PRIu64 "\n", op1);
6281 if ((uint64_t) (endp - readp) < op1)
6284 fputs (gettext (" <INVALID DATA>\n"), stdout);
6287 print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6291 case DW_CFA_expression:
6292 if ((uint64_t) (endp - readp) < 1)
6294 get_uleb128 (op1, readp, endp);
6295 if ((uint64_t) (endp - readp) < 1)
6297 get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6298 printf (" expression r%" PRIu64 " (%s) \n",
6299 op1, regname (op1));
6300 if ((uint64_t) (endp - readp) < op2)
6302 print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0, NULL,
6306 case DW_CFA_offset_extended_sf:
6307 if ((uint64_t) (endp - readp) < 1)
6309 get_uleb128 (op1, readp, endp);
6310 if ((uint64_t) (endp - readp) < 1)
6312 get_sleb128 (sop2, readp, endp);
6313 printf (" offset_extended_sf r%" PRIu64 " (%s) at cfa%+"
6315 op1, regname (op1), sop2 * data_align);
6317 case DW_CFA_def_cfa_sf:
6318 if ((uint64_t) (endp - readp) < 1)
6320 get_uleb128 (op1, readp, endp);
6321 if ((uint64_t) (endp - readp) < 1)
6323 get_sleb128 (sop2, readp, endp);
6324 printf (" def_cfa_sf r%" PRIu64 " (%s) at offset %" PRId64 "\n",
6325 op1, regname (op1), sop2 * data_align);
6327 case DW_CFA_def_cfa_offset_sf:
6328 if ((uint64_t) (endp - readp) < 1)
6330 get_sleb128 (sop1, readp, endp);
6331 printf (" def_cfa_offset_sf %" PRId64 "\n", sop1 * data_align);
6333 case DW_CFA_val_offset:
6334 if ((uint64_t) (endp - readp) < 1)
6336 get_uleb128 (op1, readp, endp);
6337 if ((uint64_t) (endp - readp) < 1)
6339 get_uleb128 (op2, readp, endp);
6340 printf (" val_offset %" PRIu64 " at offset %" PRIu64 "\n",
6341 op1, op2 * data_align);
6343 case DW_CFA_val_offset_sf:
6344 if ((uint64_t) (endp - readp) < 1)
6346 get_uleb128 (op1, readp, endp);
6347 if ((uint64_t) (endp - readp) < 1)
6349 get_sleb128 (sop2, readp, endp);
6350 printf (" val_offset_sf %" PRIu64 " at offset %" PRId64 "\n",
6351 op1, sop2 * data_align);
6353 case DW_CFA_val_expression:
6354 if ((uint64_t) (endp - readp) < 1)
6356 get_uleb128 (op1, readp, endp);
6357 if ((uint64_t) (endp - readp) < 1)
6359 get_uleb128 (op2, readp, endp); /* Length of DW_FORM_block. */
6360 printf (" val_expression r%" PRIu64 " (%s)\n",
6361 op1, regname (op1));
6362 if ((uint64_t) (endp - readp) < op2)
6364 print_ops (dwflmod, dbg, 10, 10, version, ptr_size, 0,
6368 case DW_CFA_MIPS_advance_loc8:
6369 if ((uint64_t) (endp - readp) < 8)
6371 op1 = read_8ubyte_unaligned_inc (dbg, readp);
6372 printf (" MIPS_advance_loc8 %" PRIu64 " to %#" PRIx64 "\n",
6373 op1, pc += op1 * code_align);
6375 case DW_CFA_GNU_window_save:
6376 puts (" GNU_window_save");
6378 case DW_CFA_GNU_args_size:
6379 if ((uint64_t) (endp - readp) < 1)
6381 get_uleb128 (op1, readp, endp);
6382 printf (" args_size %" PRIu64 "\n", op1);
6385 printf (" ??? (%u)\n", opcode);
6388 else if (opcode < DW_CFA_offset)
6389 printf (" advance_loc %u to %#" PRIx64 "\n",
6390 opcode & 0x3f, pc += (opcode & 0x3f) * code_align);
6391 else if (opcode < DW_CFA_restore)
6394 if ((uint64_t) (endp - readp) < 1)
6396 get_uleb128 (offset, readp, endp);
6397 printf (" offset r%u (%s) at cfa%+" PRId64 "\n",
6398 opcode & 0x3f, regname (opcode & 0x3f), offset * data_align);
6401 printf (" restore r%u (%s)\n",
6402 opcode & 0x3f, regname (opcode & 0x3f));
6408 encoded_ptr_size (int encoding, unsigned int ptr_size)
6410 switch (encoding & 7)
6412 case DW_EH_PE_udata4:
6414 case DW_EH_PE_udata8:
6420 fprintf (stderr, "Unsupported pointer encoding: %#x, "
6421 "assuming pointer size of %d.\n", encoding, ptr_size);
6427 print_encoding (unsigned int val)
6431 case DW_EH_PE_absptr:
6432 fputs ("absptr", stdout);
6434 case DW_EH_PE_uleb128:
6435 fputs ("uleb128", stdout);
6437 case DW_EH_PE_udata2:
6438 fputs ("udata2", stdout);
6440 case DW_EH_PE_udata4:
6441 fputs ("udata4", stdout);
6443 case DW_EH_PE_udata8:
6444 fputs ("udata8", stdout);
6446 case DW_EH_PE_sleb128:
6447 fputs ("sleb128", stdout);
6449 case DW_EH_PE_sdata2:
6450 fputs ("sdata2", stdout);
6452 case DW_EH_PE_sdata4:
6453 fputs ("sdata4", stdout);
6455 case DW_EH_PE_sdata8:
6456 fputs ("sdata8", stdout);
6459 /* We did not use any of the bits after all. */
6468 print_relinfo (unsigned int val)
6472 case DW_EH_PE_pcrel:
6473 fputs ("pcrel", stdout);
6475 case DW_EH_PE_textrel:
6476 fputs ("textrel", stdout);
6478 case DW_EH_PE_datarel:
6479 fputs ("datarel", stdout);
6481 case DW_EH_PE_funcrel:
6482 fputs ("funcrel", stdout);
6484 case DW_EH_PE_aligned:
6485 fputs ("aligned", stdout);
6496 print_encoding_base (const char *pfx, unsigned int fde_encoding)
6498 printf ("(%s", pfx);
6500 if (fde_encoding == DW_EH_PE_omit)
6504 unsigned int w = fde_encoding;
6506 w = print_encoding (w);
6510 if (w != fde_encoding)
6511 fputc_unlocked (' ', stdout);
6513 w = print_relinfo (w);
6517 printf ("%s%x", w != fde_encoding ? " " : "", w);
6525 print_debug_frame_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
6526 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
6529 /* We know this call will succeed since it did in the caller. */
6530 (void) elf_getshdrstrndx (ebl->elf, &shstrndx);
6531 const char *scnname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
6533 /* Needed if we find PC-relative addresses. */
6535 if (dwfl_module_getelf (dwflmod, &bias) == NULL)
6537 error (0, 0, gettext ("cannot get ELF: %s"), dwfl_errmsg (-1));
6541 bool is_eh_frame = strcmp (scnname, ".eh_frame") == 0;
6542 Elf_Data *data = (is_eh_frame
6543 ? elf_rawdata (scn, NULL)
6544 : (dbg->sectiondata[IDX_debug_frame]
6545 ?: elf_rawdata (scn, NULL)));
6547 if (unlikely (data == NULL))
6549 error (0, 0, gettext ("cannot get %s content: %s"),
6550 scnname, elf_errmsg (-1));
6556 \nCall frame information section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6557 elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6560 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
6561 elf_ndxscn (scn), scnname, (uint64_t) shdr->sh_offset);
6565 ptrdiff_t cie_offset;
6566 const char *augmentation;
6567 unsigned int code_alignment_factor;
6568 unsigned int data_alignment_factor;
6569 uint8_t address_size;
6570 uint8_t fde_encoding;
6571 uint8_t lsda_encoding;
6572 struct cieinfo *next;
6575 const unsigned char *readp = data->d_buf;
6576 const unsigned char *const dataend = ((unsigned char *) data->d_buf
6578 while (readp < dataend)
6580 if (unlikely (readp + 4 > dataend))
6583 error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
6584 elf_ndxscn (scn), scnname);
6588 /* At the beginning there must be a CIE. There can be multiple,
6589 hence we test tis in a loop. */
6590 ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
6592 Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, readp);
6593 unsigned int length = 4;
6594 if (unlikely (unit_length == 0xffffffff))
6596 if (unlikely (readp + 8 > dataend))
6599 unit_length = read_8ubyte_unaligned_inc (dbg, readp);
6603 if (unlikely (unit_length == 0))
6605 printf (gettext ("\n [%6tx] Zero terminator\n"), offset);
6609 Dwarf_Word maxsize = dataend - readp;
6610 if (unlikely (unit_length > maxsize))
6613 unsigned int ptr_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6615 ptrdiff_t start = readp - (unsigned char *) data->d_buf;
6616 const unsigned char *const cieend = readp + unit_length;
6617 if (unlikely (cieend > dataend || readp + 8 > dataend))
6623 cie_id = read_4ubyte_unaligned_inc (dbg, readp);
6624 if (!is_eh_frame && cie_id == DW_CIE_ID_32)
6625 cie_id = DW_CIE_ID_64;
6628 cie_id = read_8ubyte_unaligned_inc (dbg, readp);
6630 uint_fast8_t version = 2;
6631 unsigned int code_alignment_factor;
6632 int data_alignment_factor;
6633 unsigned int fde_encoding = 0;
6634 unsigned int lsda_encoding = 0;
6635 Dwarf_Word initial_location = 0;
6636 Dwarf_Word vma_base = 0;
6638 if (cie_id == (is_eh_frame ? 0 : DW_CIE_ID_64))
6641 const char *const augmentation = (const char *) readp;
6642 readp = memchr (readp, '\0', cieend - readp);
6643 if (unlikely (readp == NULL))
6647 uint_fast8_t segment_size = 0;
6650 if (cieend - readp < 5)
6652 ptr_size = *readp++;
6653 segment_size = *readp++;
6656 if (cieend - readp < 1)
6658 get_uleb128 (code_alignment_factor, readp, cieend);
6659 if (cieend - readp < 1)
6661 get_sleb128 (data_alignment_factor, readp, cieend);
6663 /* In some variant for unwind data there is another field. */
6664 if (strcmp (augmentation, "eh") == 0)
6665 readp += ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
6667 unsigned int return_address_register;
6668 if (cieend - readp < 1)
6670 if (unlikely (version == 1))
6671 return_address_register = *readp++;
6673 get_uleb128 (return_address_register, readp, cieend);
6675 printf ("\n [%6tx] CIE length=%" PRIu64 "\n"
6676 " CIE_id: %" PRIu64 "\n"
6678 " augmentation: \"%s\"\n",
6679 offset, (uint64_t) unit_length, (uint64_t) cie_id,
6680 version, augmentation);
6682 printf (" address_size: %u\n"
6683 " segment_size: %u\n",
6684 ptr_size, segment_size);
6685 printf (" code_alignment_factor: %u\n"
6686 " data_alignment_factor: %d\n"
6687 " return_address_register: %u\n",
6688 code_alignment_factor,
6689 data_alignment_factor, return_address_register);
6691 if (augmentation[0] == 'z')
6693 unsigned int augmentationlen;
6694 get_uleb128 (augmentationlen, readp, cieend);
6696 if (augmentationlen > (size_t) (cieend - readp))
6698 error (0, 0, gettext ("invalid augmentation length"));
6703 const char *hdr = "Augmentation data:";
6704 const char *cp = augmentation + 1;
6705 while (*cp != '\0' && cp < augmentation + augmentationlen + 1)
6707 printf (" %-26s%#x ", hdr, *readp);
6712 fde_encoding = *readp++;
6713 print_encoding_base (gettext ("FDE address encoding: "),
6716 else if (*cp == 'L')
6718 lsda_encoding = *readp++;
6719 print_encoding_base (gettext ("LSDA pointer encoding: "),
6722 else if (*cp == 'P')
6724 /* Personality. This field usually has a relocation
6725 attached pointing to __gcc_personality_v0. */
6726 const unsigned char *startp = readp;
6727 unsigned int encoding = *readp++;
6729 readp = read_encoded (encoding, readp,
6730 readp - 1 + augmentationlen,
6733 while (++startp < readp)
6734 printf ("%#x ", *startp);
6737 print_encoding (encoding);
6739 switch (encoding & 0xf)
6741 case DW_EH_PE_sleb128:
6742 case DW_EH_PE_sdata2:
6743 case DW_EH_PE_sdata4:
6744 printf ("%" PRId64 ")\n", val);
6747 printf ("%#" PRIx64 ")\n", val);
6752 printf ("(%x)\n", *readp++);
6758 if (likely (ptr_size == 4 || ptr_size == 8))
6760 struct cieinfo *newp = alloca (sizeof (*newp));
6761 newp->cie_offset = offset;
6762 newp->augmentation = augmentation;
6763 newp->fde_encoding = fde_encoding;
6764 newp->lsda_encoding = lsda_encoding;
6765 newp->address_size = ptr_size;
6766 newp->code_alignment_factor = code_alignment_factor;
6767 newp->data_alignment_factor = data_alignment_factor;
6774 struct cieinfo *cie = cies;
6777 ? ((Dwarf_Off) start - cie_id) == (Dwarf_Off) cie->cie_offset
6778 : cie_id == (Dwarf_Off) cie->cie_offset)
6782 if (unlikely (cie == NULL))
6784 puts ("invalid CIE reference in FDE");
6788 /* Initialize from CIE data. */
6789 fde_encoding = cie->fde_encoding;
6790 lsda_encoding = cie->lsda_encoding;
6791 ptr_size = encoded_ptr_size (fde_encoding, cie->address_size);
6792 code_alignment_factor = cie->code_alignment_factor;
6793 data_alignment_factor = cie->data_alignment_factor;
6795 const unsigned char *base = readp;
6796 // XXX There are sometimes relocations for this value
6797 initial_location = read_addr_unaligned_inc (ptr_size, dbg, readp);
6798 Dwarf_Word address_range
6799 = read_addr_unaligned_inc (ptr_size, dbg, readp);
6801 /* pcrel for an FDE address is relative to the runtime
6802 address of the start_address field itself. Sign extend
6803 if necessary to make sure the calculation is done on the
6804 full 64 bit address even when initial_location only holds
6805 the lower 32 bits. */
6806 Dwarf_Addr pc_start = initial_location;
6808 pc_start = (uint64_t) (int32_t) pc_start;
6809 if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6810 pc_start += ((uint64_t) shdr->sh_addr
6811 + (base - (const unsigned char *) data->d_buf)
6814 char *a = format_dwarf_addr (dwflmod, cie->address_size,
6815 pc_start, initial_location);
6816 printf ("\n [%6tx] FDE length=%" PRIu64 " cie=[%6tx]\n"
6817 " CIE_pointer: %" PRIu64 "\n"
6818 " initial_location: %s",
6819 offset, (uint64_t) unit_length,
6820 cie->cie_offset, (uint64_t) cie_id, a);
6822 if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6824 vma_base = (((uint64_t) shdr->sh_offset
6825 + (base - (const unsigned char *) data->d_buf)
6826 + (uint64_t) initial_location)
6828 ? UINT64_C (0xffffffff)
6829 : UINT64_C (0xffffffffffffffff)));
6830 printf (gettext (" (offset: %#" PRIx64 ")"),
6831 (uint64_t) vma_base);
6834 printf ("\n address_range: %#" PRIx64,
6835 (uint64_t) address_range);
6836 if ((fde_encoding & 0x70) == DW_EH_PE_pcrel)
6837 printf (gettext (" (end offset: %#" PRIx64 ")"),
6838 ((uint64_t) vma_base + (uint64_t) address_range)
6840 ? UINT64_C (0xffffffff)
6841 : UINT64_C (0xffffffffffffffff)));
6844 if (cie->augmentation[0] == 'z')
6846 unsigned int augmentationlen;
6847 if (cieend - readp < 1)
6849 get_uleb128 (augmentationlen, readp, cieend);
6851 if (augmentationlen > (size_t) (cieend - readp))
6853 error (0, 0, gettext ("invalid augmentation length"));
6858 if (augmentationlen > 0)
6860 const char *hdr = "Augmentation data:";
6861 const char *cp = cie->augmentation + 1;
6864 && cp < cie->augmentation + augmentationlen + 1)
6868 uint64_t lsda_pointer;
6869 const unsigned char *p
6870 = read_encoded (lsda_encoding, &readp[u],
6871 &readp[augmentationlen],
6872 &lsda_pointer, dbg);
6875 %-26sLSDA pointer: %#" PRIx64 "\n"),
6882 while (u < augmentationlen)
6884 printf (" %-26s%#x\n", hdr, readp[u++]);
6889 readp += augmentationlen;
6893 /* Handle the initialization instructions. */
6894 if (ptr_size != 4 && ptr_size !=8)
6895 printf ("invalid CIE pointer size (%u), must be 4 or 8.\n", ptr_size);
6897 print_cfa_program (readp, cieend, vma_base, code_alignment_factor,
6898 data_alignment_factor, version, ptr_size,
6899 fde_encoding, dwflmod, ebl, dbg);
6907 Dwfl_Module *dwflmod;
6913 unsigned int version;
6914 unsigned int addrsize;
6915 unsigned int offset_size;
6916 struct Dwarf_CU *cu;
6921 attr_callback (Dwarf_Attribute *attrp, void *arg)
6923 struct attrcb_args *cbargs = (struct attrcb_args *) arg;
6924 const int level = cbargs->level;
6925 Dwarf_Die *die = cbargs->die;
6926 bool is_split = cbargs->is_split;
6928 unsigned int attr = dwarf_whatattr (attrp);
6929 if (unlikely (attr == 0))
6931 if (!cbargs->silent)
6932 error (0, 0, gettext ("DIE [%" PRIx64 "] "
6933 "cannot get attribute code: %s"),
6934 dwarf_dieoffset (die), dwarf_errmsg (-1));
6935 return DWARF_CB_ABORT;
6938 unsigned int form = dwarf_whatform (attrp);
6939 if (unlikely (form == 0))
6941 if (!cbargs->silent)
6942 error (0, 0, gettext ("DIE [%" PRIx64 "] "
6943 "cannot get attribute form: %s"),
6944 dwarf_dieoffset (die), dwarf_errmsg (-1));
6945 return DWARF_CB_ABORT;
6952 case DW_FORM_addrx1:
6953 case DW_FORM_addrx2:
6954 case DW_FORM_addrx3:
6955 case DW_FORM_addrx4:
6956 case DW_FORM_GNU_addr_index:
6957 if (!cbargs->silent)
6960 if (unlikely (dwarf_formaddr (attrp, &addr) != 0))
6963 if (!cbargs->silent)
6964 error (0, 0, gettext ("DIE [%" PRIx64 "] "
6965 "cannot get attribute '%s' (%s) value: "
6967 dwarf_dieoffset (die),
6968 dwarf_attr_name (attr),
6969 dwarf_form_name (form),
6971 /* Don't ABORT, it might be other attributes can be resolved. */
6974 char *a = format_dwarf_addr (cbargs->dwflmod, cbargs->addrsize,
6976 if (form != DW_FORM_addr )
6979 if (dwarf_formudata (attrp, &index) != 0)
6981 printf (" %*s%-20s (%s) [%" PRIx64 "] %s\n",
6982 (int) (level * 2), "", dwarf_attr_name (attr),
6983 dwarf_form_name (form), index, a);
6986 printf (" %*s%-20s (%s) %s\n",
6987 (int) (level * 2), "", dwarf_attr_name (attr),
6988 dwarf_form_name (form), a);
6993 case DW_FORM_indirect:
6995 case DW_FORM_line_strp:
7001 case DW_FORM_string:
7002 case DW_FORM_GNU_strp_alt:
7003 case DW_FORM_GNU_str_index:
7006 const char *str = dwarf_formstring (attrp);
7007 if (unlikely (str == NULL))
7009 printf (" %*s%-20s (%s) \"%s\"\n",
7010 (int) (level * 2), "", dwarf_attr_name (attr),
7011 dwarf_form_name (form), str);
7014 case DW_FORM_ref_addr:
7015 case DW_FORM_ref_udata:
7020 case DW_FORM_GNU_ref_alt:
7021 case DW_FORM_ref_sup4:
7022 case DW_FORM_ref_sup8:
7026 if (unlikely (dwarf_formref_die (attrp, &ref) == NULL))
7029 printf (" %*s%-20s (%s) ",
7030 (int) (level * 2), "", dwarf_attr_name (attr),
7031 dwarf_form_name (form));
7033 printf ("{%6" PRIxMAX "}\n", (uintmax_t) dwarf_dieoffset (&ref));
7035 printf ("[%6" PRIxMAX "]\n", (uintmax_t) dwarf_dieoffset (&ref));
7038 case DW_FORM_ref_sig8:
7041 printf (" %*s%-20s (%s) {%6" PRIx64 "}\n",
7042 (int) (level * 2), "", dwarf_attr_name (attr),
7043 dwarf_form_name (form),
7044 (uint64_t) read_8ubyte_unaligned (attrp->cu->dbg, attrp->valp));
7047 case DW_FORM_sec_offset:
7048 case DW_FORM_rnglistx:
7049 case DW_FORM_loclistx:
7050 case DW_FORM_implicit_const:
7053 case DW_FORM_data8: /* Note no data16 here, we see that as block. */
7056 case DW_FORM_data1:;
7058 if (unlikely (dwarf_formudata (attrp, &num) != 0))
7061 const char *valuestr = NULL;
7062 bool as_hex_id = false;
7065 /* This case can take either a constant or a loclistptr. */
7066 case DW_AT_data_member_location:
7067 if (form != DW_FORM_sec_offset
7068 && (cbargs->version >= 4
7069 || (form != DW_FORM_data4 && form != DW_FORM_data8)))
7071 if (!cbargs->silent)
7072 printf (" %*s%-20s (%s) %" PRIxMAX "\n",
7073 (int) (level * 2), "", dwarf_attr_name (attr),
7074 dwarf_form_name (form), (uintmax_t) num);
7079 /* These cases always take a loclist[ptr] and no constant. */
7080 case DW_AT_location:
7081 case DW_AT_data_location:
7082 case DW_AT_vtable_elem_location:
7083 case DW_AT_string_length:
7084 case DW_AT_use_location:
7085 case DW_AT_frame_base:
7086 case DW_AT_return_addr:
7087 case DW_AT_static_link:
7089 case DW_AT_GNU_call_site_value:
7090 case DW_AT_GNU_call_site_data_value:
7091 case DW_AT_GNU_call_site_target:
7092 case DW_AT_GNU_call_site_target_clobbered:
7093 case DW_AT_GNU_locviews:
7096 if (cbargs->cu->version < 5)
7098 if (! cbargs->is_split)
7100 nlpt = notice_listptr (section_loc, &known_locsptr,
7102 cbargs->offset_size,
7103 cbargs->cu, num, attr);
7110 /* Only register for a real section offset. Otherwise
7111 it is a DW_FORM_loclistx which is just an index
7112 number and we should already have registered the
7113 section offset for the index when we saw the
7114 DW_AT_loclists_base CU attribute. */
7115 if (form == DW_FORM_sec_offset)
7116 nlpt = notice_listptr (section_loc, &known_loclistsptr,
7117 cbargs->addrsize, cbargs->offset_size,
7118 cbargs->cu, num, attr);
7124 if (!cbargs->silent)
7126 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7127 printf (" %*s%-20s (%s) location list [%6"
7129 (int) (level * 2), "", dwarf_attr_name (attr),
7130 dwarf_form_name (form), (uintmax_t) num,
7131 nlpt ? "" : " <WARNING offset too big>");
7133 printf (" %*s%-20s (%s) location index [%6"
7135 (int) (level * 2), "", dwarf_attr_name (attr),
7136 dwarf_form_name (form), (uintmax_t) num);
7141 case DW_AT_loclists_base:
7143 bool nlpt = notice_listptr (section_loc, &known_loclistsptr,
7144 cbargs->addrsize, cbargs->offset_size,
7145 cbargs->cu, num, attr);
7147 if (!cbargs->silent)
7148 printf (" %*s%-20s (%s) location list [%6" PRIxMAX "]%s\n",
7149 (int) (level * 2), "", dwarf_attr_name (attr),
7150 dwarf_form_name (form), (uintmax_t) num,
7151 nlpt ? "" : " <WARNING offset too big>");
7156 case DW_AT_start_scope:
7159 if (cbargs->cu->version < 5)
7160 nlpt = notice_listptr (section_ranges, &known_rangelistptr,
7161 cbargs->addrsize, cbargs->offset_size,
7162 cbargs->cu, num, attr);
7165 /* Only register for a real section offset. Otherwise
7166 it is a DW_FORM_rangelistx which is just an index
7167 number and we should already have registered the
7168 section offset for the index when we saw the
7169 DW_AT_rnglists_base CU attribute. */
7170 if (form == DW_FORM_sec_offset)
7171 nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7172 cbargs->addrsize, cbargs->offset_size,
7173 cbargs->cu, num, attr);
7178 if (!cbargs->silent)
7180 if (cbargs->cu->version < 5 || form == DW_FORM_sec_offset)
7181 printf (" %*s%-20s (%s) range list [%6"
7183 (int) (level * 2), "", dwarf_attr_name (attr),
7184 dwarf_form_name (form), (uintmax_t) num,
7185 nlpt ? "" : " <WARNING offset too big>");
7187 printf (" %*s%-20s (%s) range index [%6"
7189 (int) (level * 2), "", dwarf_attr_name (attr),
7190 dwarf_form_name (form), (uintmax_t) num);
7195 case DW_AT_rnglists_base:
7197 bool nlpt = notice_listptr (section_ranges, &known_rnglistptr,
7198 cbargs->addrsize, cbargs->offset_size,
7199 cbargs->cu, num, attr);
7200 if (!cbargs->silent)
7201 printf (" %*s%-20s (%s) range list [%6"
7203 (int) (level * 2), "", dwarf_attr_name (attr),
7204 dwarf_form_name (form), (uintmax_t) num,
7205 nlpt ? "" : " <WARNING offset too big>");
7209 case DW_AT_addr_base:
7210 case DW_AT_GNU_addr_base:
7212 bool addrbase = notice_listptr (section_addr, &known_addrbases,
7214 cbargs->offset_size,
7215 cbargs->cu, num, attr);
7216 if (!cbargs->silent)
7217 printf (" %*s%-20s (%s) address base [%6"
7219 (int) (level * 2), "", dwarf_attr_name (attr),
7220 dwarf_form_name (form), (uintmax_t) num,
7221 addrbase ? "" : " <WARNING offset too big>");
7225 case DW_AT_str_offsets_base:
7227 bool stroffbase = notice_listptr (section_str, &known_stroffbases,
7229 cbargs->offset_size,
7230 cbargs->cu, num, attr);
7231 if (!cbargs->silent)
7232 printf (" %*s%-20s (%s) str offsets base [%6"
7234 (int) (level * 2), "", dwarf_attr_name (attr),
7235 dwarf_form_name (form), (uintmax_t) num,
7236 stroffbase ? "" : " <WARNING offset too big>");
7240 case DW_AT_language:
7241 valuestr = dwarf_lang_name (num);
7243 case DW_AT_encoding:
7244 valuestr = dwarf_encoding_name (num);
7246 case DW_AT_accessibility:
7247 valuestr = dwarf_access_name (num);
7249 case DW_AT_defaulted:
7250 valuestr = dwarf_defaulted_name (num);
7252 case DW_AT_visibility:
7253 valuestr = dwarf_visibility_name (num);
7255 case DW_AT_virtuality:
7256 valuestr = dwarf_virtuality_name (num);
7258 case DW_AT_identifier_case:
7259 valuestr = dwarf_identifier_case_name (num);
7261 case DW_AT_calling_convention:
7262 valuestr = dwarf_calling_convention_name (num);
7265 valuestr = dwarf_inline_name (num);
7267 case DW_AT_ordering:
7268 valuestr = dwarf_ordering_name (num);
7270 case DW_AT_discr_list:
7271 valuestr = dwarf_discr_list_name (num);
7273 case DW_AT_decl_file:
7274 case DW_AT_call_file:
7279 /* Try to get the actual file, the current interface only
7280 gives us full paths, but we only want to show the file
7283 if (dwarf_cu_die (cbargs->cu, &cudie,
7284 NULL, NULL, NULL, NULL, NULL, NULL) != NULL)
7288 if (dwarf_getsrcfiles (&cudie, &files, &nfiles) == 0)
7290 valuestr = dwarf_filesrc (files, num, NULL, NULL);
7291 if (valuestr != NULL)
7293 char *filename = strrchr (valuestr, '/');
7294 if (filename != NULL)
7295 valuestr = filename + 1;
7298 error (0, 0, gettext ("invalid file (%" PRId64 "): %s"),
7299 num, dwarf_errmsg (-1));
7302 error (0, 0, gettext ("no srcfiles for CU [%" PRIx64 "]"),
7303 dwarf_dieoffset (&cudie));
7306 error (0, 0, gettext ("couldn't get DWARF CU: %s"),
7308 if (valuestr == NULL)
7312 case DW_AT_GNU_dwo_id:
7324 /* When highpc is in constant form it is relative to lowpc.
7325 In that case also show the address. */
7327 if (attr == DW_AT_high_pc && dwarf_highpc (cbargs->die, &highpc) == 0)
7329 char *a = format_dwarf_addr (cbargs->dwflmod, cbargs->addrsize,
7331 printf (" %*s%-20s (%s) %" PRIuMAX " (%s)\n",
7332 (int) (level * 2), "", dwarf_attr_name (attr),
7333 dwarf_form_name (form), (uintmax_t) num, a);
7338 Dwarf_Sword snum = 0;
7339 if (form == DW_FORM_sdata)
7340 if (unlikely (dwarf_formsdata (attrp, &snum) != 0))
7345 printf (" %*s%-20s (%s) 0x%.16" PRIx64 "\n",
7346 (int) (level * 2), "", dwarf_attr_name (attr),
7347 dwarf_form_name (form), num);
7349 else if (valuestr == NULL)
7351 printf (" %*s%-20s (%s)",
7352 (int) (level * 2), "", dwarf_attr_name (attr),
7353 dwarf_form_name (form));
7354 if (form == DW_FORM_sdata)
7355 printf (" %" PRIdMAX "\n", (intmax_t) snum);
7357 printf (" %" PRIuMAX "\n", (uintmax_t) num);
7361 printf (" %*s%-20s (%s) %s",
7362 (int) (level * 2), "", dwarf_attr_name (attr),
7363 dwarf_form_name (form), valuestr);
7364 if (form == DW_FORM_sdata)
7365 printf (" (%" PRIdMAX ")\n", (intmax_t) snum);
7367 printf (" (%" PRIuMAX ")\n", (uintmax_t) num);
7376 if (unlikely (dwarf_formflag (attrp, &flag) != 0))
7379 printf (" %*s%-20s (%s) %s\n",
7380 (int) (level * 2), "", dwarf_attr_name (attr),
7381 dwarf_form_name (form), flag ? yes_str : no_str);
7384 case DW_FORM_flag_present:
7387 printf (" %*s%-20s (%s) %s\n",
7388 (int) (level * 2), "", dwarf_attr_name (attr),
7389 dwarf_form_name (form), yes_str);
7392 case DW_FORM_exprloc:
7393 case DW_FORM_block4:
7394 case DW_FORM_block2:
7395 case DW_FORM_block1:
7397 case DW_FORM_data16: /* DWARF5 calls this a constant class. */
7401 if (unlikely (dwarf_formblock (attrp, &block) != 0))
7404 printf (" %*s%-20s (%s) ",
7405 (int) (level * 2), "", dwarf_attr_name (attr),
7406 dwarf_form_name (form));
7411 if (form != DW_FORM_exprloc)
7413 print_block (block.length, block.data);
7418 case DW_AT_location:
7419 case DW_AT_data_location:
7420 case DW_AT_data_member_location:
7421 case DW_AT_vtable_elem_location:
7422 case DW_AT_string_length:
7423 case DW_AT_use_location:
7424 case DW_AT_frame_base:
7425 case DW_AT_return_addr:
7426 case DW_AT_static_link:
7427 case DW_AT_allocated:
7428 case DW_AT_associated:
7429 case DW_AT_bit_size:
7430 case DW_AT_bit_offset:
7431 case DW_AT_bit_stride:
7432 case DW_AT_byte_size:
7433 case DW_AT_byte_stride:
7435 case DW_AT_lower_bound:
7436 case DW_AT_upper_bound:
7437 case DW_AT_GNU_call_site_value:
7438 case DW_AT_GNU_call_site_data_value:
7439 case DW_AT_GNU_call_site_target:
7440 case DW_AT_GNU_call_site_target_clobbered:
7442 print_ops (cbargs->dwflmod, cbargs->dbg,
7443 12 + level * 2, 12 + level * 2,
7444 cbargs->version, cbargs->addrsize, cbargs->offset_size,
7445 attrp->cu, block.length, block.data);
7453 printf (" %*s%-20s (%s) ???\n",
7454 (int) (level * 2), "", dwarf_attr_name (attr),
7455 dwarf_form_name (form));
7463 print_debug_units (Dwfl_Module *dwflmod,
7464 Ebl *ebl, GElf_Ehdr *ehdr,
7465 Elf_Scn *scn, GElf_Shdr *shdr,
7466 Dwarf *dbg, bool debug_types)
7468 const bool silent = !(print_debug_sections & section_info) && !debug_types;
7469 const char *secname = section_name (ebl, ehdr, shdr);
7473 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n [Offset]\n"),
7474 elf_ndxscn (scn), secname, (uint64_t) shdr->sh_offset);
7476 /* If the section is empty we don't have to do anything. */
7477 if (!silent && shdr->sh_size == 0)
7481 Dwarf_Die *dies = (Dwarf_Die *) xmalloc (maxdies * sizeof (Dwarf_Die));
7483 /* New compilation unit. */
7487 Dwarf_Off abbroffset;
7491 Dwarf_Off subdie_off;
7499 /* We cheat a little because we want to see only the CUs from .debug_info
7500 or .debug_types. We know the Dwarf_CU struct layout. Set it up at
7501 the end of .debug_info if we want .debug_types only. Check the returned
7502 Dwarf_CU is still in the expected section. */
7506 cu_mem.end = dbg->sectiondata[IDX_debug_info]->d_size;
7507 cu_mem.sec_idx = IDX_debug_info;
7514 unit_res = dwarf_get_units (dbg, cu, &cu, &version, &unit_type,
7522 error (0, 0, gettext ("cannot get next unit: %s"), dwarf_errmsg (-1));
7526 if (cu->sec_idx != (size_t) (debug_types ? IDX_debug_types : IDX_debug_info))
7529 dwarf_cu_die (cu, &result, NULL, &abbroffset, &addrsize, &offsize,
7530 &unit_id, &subdie_off);
7534 Dwarf_Off offset = cu->start;
7535 if (debug_types && version < 5)
7538 Dwarf_Off dieoffset;
7539 dieoffset = dwarf_dieoffset (dwarf_offdie_types (dbg, subdie_off,
7541 printf (gettext (" Type unit at offset %" PRIu64 ":\n"
7542 " Version: %" PRIu16
7543 ", Abbreviation section offset: %" PRIu64
7544 ", Address size: %" PRIu8
7545 ", Offset size: %" PRIu8
7546 "\n Type signature: %#" PRIx64
7547 ", Type offset: %#" PRIx64 " [%" PRIx64 "]\n"),
7548 (uint64_t) offset, version, abbroffset, addrsize, offsize,
7549 unit_id, (uint64_t) subdie_off, dieoffset);
7553 printf (gettext (" Compilation unit at offset %" PRIu64 ":\n"
7554 " Version: %" PRIu16
7555 ", Abbreviation section offset: %" PRIu64
7556 ", Address size: %" PRIu8
7557 ", Offset size: %" PRIu8 "\n"),
7558 (uint64_t) offset, version, abbroffset, addrsize, offsize);
7560 if (version >= 5 || (unit_type != DW_UT_compile
7561 && unit_type != DW_UT_partial))
7563 printf (gettext (" Unit type: %s (%" PRIu8 ")"),
7564 dwarf_unit_name (unit_type), unit_type);
7565 if (unit_type == DW_UT_type
7566 || unit_type == DW_UT_skeleton
7567 || unit_type == DW_UT_split_compile
7568 || unit_type == DW_UT_split_type)
7569 printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7570 if (unit_type == DW_UT_type
7571 || unit_type == DW_UT_split_type)
7574 Dwarf_Off dieoffset;
7575 dwarf_cu_info (cu, NULL, NULL, NULL, &typedie,
7577 dieoffset = dwarf_dieoffset (&typedie);
7578 printf (", Unit DIE off: %#" PRIx64 " [%" PRIx64 "]",
7579 subdie_off, dieoffset);
7586 if (version < 2 || version > 5
7587 || unit_type < DW_UT_compile || unit_type > DW_UT_split_type)
7590 error (0, 0, gettext ("unknown version (%d) or unit type (%d)"),
7591 version, unit_type);
7595 struct attrcb_args args =
7600 .addrsize = addrsize,
7601 .offset_size = offsize
7604 bool is_split = false;
7607 args.cu = dies[0].cu;
7609 args.is_split = is_split;
7611 /* We might return here again for the split CU subdie. */
7615 Dwarf_Off offset = dwarf_dieoffset (&dies[level]);
7616 if (unlikely (offset == (Dwarf_Off) -1))
7619 error (0, 0, gettext ("cannot get DIE offset: %s"),
7624 int tag = dwarf_tag (&dies[level]);
7625 if (unlikely (tag == DW_TAG_invalid))
7628 error (0, 0, gettext ("cannot get tag of DIE at offset [%" PRIx64
7629 "] in section '%s': %s"),
7630 (uint64_t) offset, secname, dwarf_errmsg (-1));
7636 unsigned int code = dwarf_getabbrevcode (dies[level].abbrev);
7638 printf (" {%6" PRIx64 "} ", (uint64_t) offset);
7640 printf (" [%6" PRIx64 "] ", (uint64_t) offset);
7641 printf ("%*s%-20s abbrev: %u\n", (int) (level * 2), "",
7642 dwarf_tag_name (tag), code);
7645 /* Print the attribute values. */
7647 args.die = &dies[level];
7648 (void) dwarf_getattrs (&dies[level], attr_callback, &args, 0);
7650 /* Make room for the next level's DIE. */
7651 if (level + 1 == maxdies)
7652 dies = (Dwarf_Die *) xrealloc (dies,
7654 * sizeof (Dwarf_Die));
7656 int res = dwarf_child (&dies[level], &dies[level + 1]);
7659 while ((res = dwarf_siblingof (&dies[level], &dies[level])) == 1)
7663 if (unlikely (res == -1))
7666 error (0, 0, gettext ("cannot get next DIE: %s\n"),
7671 else if (unlikely (res < 0))
7674 error (0, 0, gettext ("cannot get next DIE: %s"),
7683 /* We might want to show the split compile unit if this was a skeleton.
7684 We need to scan it if we are requesting printing .debug_ranges for
7685 DWARF4 since GNU DebugFission uses "offsets" into the main ranges
7687 if (unit_type == DW_UT_skeleton
7688 && ((!silent && show_split_units)
7689 || (version < 5 && (print_debug_sections & section_ranges) != 0)))
7692 if (dwarf_cu_info (cu, NULL, NULL, NULL, &subdie, NULL, NULL, NULL) != 0
7693 || dwarf_tag (&subdie) == DW_TAG_invalid)
7697 Dwarf_Attribute dwo_at;
7698 const char *dwo_name =
7699 (dwarf_formstring (dwarf_attr (&cudie, DW_AT_dwo_name,
7701 ?: (dwarf_formstring (dwarf_attr (&cudie, DW_AT_GNU_dwo_name,
7705 "Could not find split unit '%s', id: %" PRIx64 "\n",
7711 Dwarf_CU *split_cu = subdie.cu;
7712 dwarf_cu_die (split_cu, &result, NULL, &abbroffset,
7713 &addrsize, &offsize, &unit_id, &subdie_off);
7714 Dwarf_Off offset = cu->start;
7718 printf (gettext (" Split compilation unit at offset %"
7720 " Version: %" PRIu16
7721 ", Abbreviation section offset: %" PRIu64
7722 ", Address size: %" PRIu8
7723 ", Offset size: %" PRIu8 "\n"),
7724 (uint64_t) offset, version, abbroffset,
7726 printf (gettext (" Unit type: %s (%" PRIu8 ")"),
7727 dwarf_unit_name (unit_type), unit_type);
7728 printf (", Unit id: 0x%.16" PRIx64 "", unit_id);
7732 unit_type = DW_UT_split_compile;
7736 args.cu = dies[0].cu;
7737 args.dbg = split_cu->dbg;
7738 args.is_split = is_split;
7751 print_debug_info_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7752 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7754 print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, false);
7758 print_debug_types_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7759 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7761 print_debug_units (dwflmod, ebl, ehdr, scn, shdr, dbg, true);
7766 print_decoded_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
7767 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
7770 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n\n"),
7771 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
7772 (uint64_t) shdr->sh_offset);
7775 = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
7778 Dwarf_Off ncuoffset = 0;
7780 while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
7781 NULL, NULL, NULL) == 0)
7784 if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
7789 if (dwarf_getsrclines (&cudie, &lines, &nlines) != 0)
7792 printf (" CU [%" PRIx64 "] %s\n",
7793 dwarf_dieoffset (&cudie), dwarf_diename (&cudie));
7794 printf (" line:col SBPE* disc isa op address"
7795 " (Statement Block Prologue Epilogue *End)\n");
7796 const char *last_file = "";
7797 for (size_t n = 0; n < nlines; n++)
7799 Dwarf_Line *line = dwarf_onesrcline (lines, n);
7802 printf (" dwarf_onesrcline: %s\n", dwarf_errmsg (-1));
7805 Dwarf_Word mtime, length;
7806 const char *file = dwarf_linesrc (line, &mtime, &length);
7809 printf (" <%s> (mtime: ?, length: ?)\n", dwarf_errmsg (-1));
7812 else if (strcmp (last_file, file) != 0)
7814 printf (" %s (mtime: %" PRIu64 ", length: %" PRIu64 ")\n",
7815 file, mtime, length);
7820 bool statement, endseq, block, prologue_end, epilogue_begin;
7821 unsigned int lineop, isa, disc;
7823 dwarf_lineaddr (line, &address);
7824 dwarf_lineno (line, &lineno);
7825 dwarf_linecol (line, &colno);
7826 dwarf_lineop_index (line, &lineop);
7827 dwarf_linebeginstatement (line, &statement);
7828 dwarf_lineendsequence (line, &endseq);
7829 dwarf_lineblock (line, &block);
7830 dwarf_lineprologueend (line, &prologue_end);
7831 dwarf_lineepiloguebegin (line, &epilogue_begin);
7832 dwarf_lineisa (line, &isa);
7833 dwarf_linediscriminator (line, &disc);
7835 /* End sequence is special, it is one byte past. */
7836 char *a = format_dwarf_addr (dwflmod, address_size,
7837 address - (endseq ? 1 : 0), address);
7838 printf (" %4d:%-3d %c%c%c%c%c %4d %3d %2d %s\n",
7840 (statement ? 'S' : ' '),
7841 (block ? 'B' : ' '),
7842 (prologue_end ? 'P' : ' '),
7843 (epilogue_begin ? 'E' : ' '),
7844 (endseq ? '*' : ' '),
7845 disc, isa, lineop, a);
7855 /* Print the value of a form.
7856 Returns new value of readp, or readendp on failure. */
7857 static const unsigned char *
7858 print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
7859 const unsigned char *readendp, unsigned int offset_len,
7860 Dwarf_Off str_offsets_base)
7863 unsigned char *endp;
7869 if (readendp - readp < 1)
7872 error (0, 0, "invalid data");
7876 printf (" %" PRIx8, (unsigned int) val);
7880 if (readendp - readp < 2)
7882 val = read_2ubyte_unaligned_inc (dbg, readp);
7883 printf(" %" PRIx16, (unsigned int) val);
7887 if (readendp - readp < 4)
7889 val = read_4ubyte_unaligned_inc (dbg, readp);
7890 printf (" %" PRIx32, (unsigned int) val);
7894 if (readendp - readp < 8)
7896 val = read_8ubyte_unaligned_inc (dbg, readp);
7897 printf (" %" PRIx64, val);
7901 if (readendp - readp < 1)
7903 get_sleb128 (val, readp, readendp);
7904 printf (" %" PRIx64, val);
7908 if (readendp - readp < 1)
7910 get_uleb128 (val, readp, readendp);
7911 printf (" %" PRIx64, val);
7915 if (readendp - readp < 1)
7917 get_uleb128 (val, readp, readendp);
7918 if (readendp - readp < (ptrdiff_t) val)
7920 print_bytes (val, readp);
7924 case DW_FORM_block1:
7925 if (readendp - readp < 1)
7928 if (readendp - readp < (ptrdiff_t) val)
7930 print_bytes (val, readp);
7934 case DW_FORM_block2:
7935 if (readendp - readp < 2)
7937 val = read_2ubyte_unaligned_inc (dbg, readp);
7938 if (readendp - readp < (ptrdiff_t) val)
7940 print_bytes (val, readp);
7944 case DW_FORM_block4:
7945 if (readendp - readp < 2)
7947 val = read_4ubyte_unaligned_inc (dbg, readp);
7948 if (readendp - readp < (ptrdiff_t) val)
7950 print_bytes (val, readp);
7954 case DW_FORM_data16:
7955 if (readendp - readp < 16)
7957 print_bytes (16, readp);
7962 if (readendp - readp < 1)
7965 printf ("%s", val != 0 ? gettext ("yes") : gettext ("no"));
7968 case DW_FORM_string:
7969 endp = memchr (readp, '\0', readendp - readp);
7972 printf ("%s", readp);
7977 case DW_FORM_line_strp:
7978 case DW_FORM_strp_sup:
7979 if (readendp - readp < (ptrdiff_t) offset_len)
7981 if (offset_len == 8)
7982 val = read_8ubyte_unaligned_inc (dbg, readp);
7984 val = read_4ubyte_unaligned_inc (dbg, readp);
7985 if (form == DW_FORM_strp)
7986 data = dbg->sectiondata[IDX_debug_str];
7987 else if (form == DW_FORM_line_strp)
7988 data = dbg->sectiondata[IDX_debug_line_str];
7989 else /* form == DW_FORM_strp_sup */
7991 Dwarf *alt = dwarf_getalt (dbg);
7992 data = alt != NULL ? alt->sectiondata[IDX_debug_str] : NULL;
7994 if (data == NULL || val >= data->d_size
7995 || memchr (data->d_buf + val, '\0', data->d_size - val) == NULL)
7998 str = (char *) data->d_buf + val;
7999 printf ("%s (%" PRIu64 ")", str, val);
8002 case DW_FORM_sec_offset:
8003 if (readendp - readp < (ptrdiff_t) offset_len)
8005 if (offset_len == 8)
8006 val = read_8ubyte_unaligned_inc (dbg, readp);
8008 val = read_4ubyte_unaligned_inc (dbg, readp);
8009 printf ("[%" PRIx64 "]", val);
8013 case DW_FORM_GNU_str_index:
8014 if (readendp - readp < 1)
8016 get_uleb128 (val, readp, readendp);
8018 data = dbg->sectiondata[IDX_debug_str_offsets];
8020 || data->d_size - str_offsets_base < val)
8024 readp = data->d_buf + str_offsets_base + val;
8025 readendp = data->d_buf + data->d_size;
8026 if (readendp - readp < (ptrdiff_t) offset_len)
8031 if (offset_len == 8)
8032 idx = read_8ubyte_unaligned_inc (dbg, readp);
8034 idx = read_4ubyte_unaligned_inc (dbg, readp);
8036 data = dbg->sectiondata[IDX_debug_str];
8037 if (data == NULL || idx >= data->d_size
8038 || memchr (data->d_buf + idx, '\0',
8039 data->d_size - idx) == NULL)
8042 str = (char *) data->d_buf + idx;
8045 printf ("%s (%" PRIu64 ")", str, val);
8049 if (readendp - readp < 1)
8055 if (readendp - readp < 2)
8057 val = read_2ubyte_unaligned (dbg, readp);
8061 if (readendp - readp < 3)
8063 val = read_3ubyte_unaligned (dbg, readp);
8067 if (readendp - readp < 4)
8069 val = read_4ubyte_unaligned (dbg, readp);
8073 error (0, 0, gettext ("unknown form: %s"), dwarf_form_name (form));
8081 print_debug_line_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
8082 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
8086 print_decoded_line_section (dwflmod, ebl, ehdr, scn, shdr, dbg);
8091 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8092 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
8093 (uint64_t) shdr->sh_offset);
8095 if (shdr->sh_size == 0)
8098 /* There is no functionality in libdw to read the information in the
8099 way it is represented here. Hardcode the decoder. */
8100 Elf_Data *data = (dbg->sectiondata[IDX_debug_line]
8101 ?: elf_rawdata (scn, NULL));
8102 if (unlikely (data == NULL))
8104 error (0, 0, gettext ("cannot get line data section data: %s"),
8109 const unsigned char *linep = (const unsigned char *) data->d_buf;
8110 const unsigned char *lineendp;
8113 < (lineendp = (const unsigned char *) data->d_buf + data->d_size))
8115 size_t start_offset = linep - (const unsigned char *) data->d_buf;
8117 printf (gettext ("\nTable at offset %zu:\n"), start_offset);
8119 if (unlikely (linep + 4 > lineendp))
8121 Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
8122 unsigned int length = 4;
8123 if (unlikely (unit_length == 0xffffffff))
8125 if (unlikely (linep + 8 > lineendp))
8128 error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
8129 elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
8132 unit_length = read_8ubyte_unaligned_inc (dbg, linep);
8136 /* Check whether we have enough room in the section. */
8137 if (unlikely (unit_length > (size_t) (lineendp - linep)))
8139 lineendp = linep + unit_length;
8141 /* The next element of the header is the version identifier. */
8142 if ((size_t) (lineendp - linep) < 2)
8144 uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
8147 = elf_getident (ebl->elf, NULL)[EI_CLASS] == ELFCLASS32 ? 4 : 8;
8148 unsigned char segment_selector_size = 0;
8151 if ((size_t) (lineendp - linep) < 2)
8153 address_size = *linep++;
8154 segment_selector_size = *linep++;
8157 /* Next comes the header length. */
8158 Dwarf_Word header_length;
8161 if ((size_t) (lineendp - linep) < 4)
8163 header_length = read_4ubyte_unaligned_inc (dbg, linep);
8167 if ((size_t) (lineendp - linep) < 8)
8169 header_length = read_8ubyte_unaligned_inc (dbg, linep);
8172 /* Next the minimum instruction length. */
8173 if ((size_t) (lineendp - linep) < 1)
8175 uint_fast8_t minimum_instr_len = *linep++;
8177 /* Next the maximum operations per instruction, in version 4 format. */
8178 uint_fast8_t max_ops_per_instr;
8180 max_ops_per_instr = 1;
8183 if ((size_t) (lineendp - linep) < 1)
8185 max_ops_per_instr = *linep++;
8188 /* We need at least 4 more bytes. */
8189 if ((size_t) (lineendp - linep) < 4)
8192 /* Then the flag determining the default value of the is_stmt
8194 uint_fast8_t default_is_stmt = *linep++;
8196 /* Now the line base. */
8197 int_fast8_t line_base = *linep++;
8199 /* And the line range. */
8200 uint_fast8_t line_range = *linep++;
8202 /* The opcode base. */
8203 uint_fast8_t opcode_base = *linep++;
8205 /* Print what we got so far. */
8206 printf (gettext ("\n"
8207 " Length: %" PRIu64 "\n"
8208 " DWARF version: %" PRIuFAST16 "\n"
8209 " Prologue length: %" PRIu64 "\n"
8210 " Address size: %zd\n"
8211 " Segment selector size: %zd\n"
8212 " Min instruction length: %" PRIuFAST8 "\n"
8213 " Max operations per instruction: %" PRIuFAST8 "\n"
8214 " Initial value if 'is_stmt': %" PRIuFAST8 "\n"
8215 " Line base: %" PRIdFAST8 "\n"
8216 " Line range: %" PRIuFAST8 "\n"
8217 " Opcode base: %" PRIuFAST8 "\n"
8220 (uint64_t) unit_length, version, (uint64_t) header_length,
8221 address_size, (size_t) segment_selector_size,
8222 minimum_instr_len, max_ops_per_instr,
8223 default_is_stmt, line_base,
8224 line_range, opcode_base);
8226 if (version < 2 || version > 5)
8228 error (0, 0, gettext ("cannot handle .debug_line version: %u\n"),
8229 (unsigned int) version);
8234 if (address_size != 4 && address_size != 8)
8236 error (0, 0, gettext ("cannot handle address size: %u\n"),
8237 (unsigned int) address_size);
8242 if (segment_selector_size != 0)
8244 error (0, 0, gettext ("cannot handle segment selector size: %u\n"),
8245 (unsigned int) segment_selector_size);
8250 if (unlikely (linep + opcode_base - 1 >= lineendp))
8254 gettext ("invalid data at offset %tu in section [%zu] '%s'"),
8255 linep - (const unsigned char *) data->d_buf,
8256 elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
8260 int opcode_base_l10 = 1;
8261 unsigned int tmp = opcode_base;
8267 const uint8_t *standard_opcode_lengths = linep - 1;
8268 for (uint_fast8_t cnt = 1; cnt < opcode_base; ++cnt)
8269 printf (ngettext (" [%*" PRIuFAST8 "] %hhu argument\n",
8270 " [%*" PRIuFAST8 "] %hhu arguments\n",
8271 (int) linep[cnt - 1]),
8272 opcode_base_l10, cnt, linep[cnt - 1]);
8273 linep += opcode_base - 1;
8275 if (unlikely (linep >= lineendp))
8278 Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, NULL);
8280 puts (gettext ("\nDirectory table:"));
8283 struct encpair { uint16_t desc; uint16_t form; };
8284 struct encpair enc[256];
8286 printf (gettext (" ["));
8287 if ((size_t) (lineendp - linep) < 1)
8289 unsigned char directory_entry_format_count = *linep++;
8290 for (int i = 0; i < directory_entry_format_count; i++)
8292 uint16_t desc, form;
8293 if ((size_t) (lineendp - linep) < 1)
8295 get_uleb128 (desc, linep, lineendp);
8296 if ((size_t) (lineendp - linep) < 1)
8298 get_uleb128 (form, linep, lineendp);
8304 dwarf_line_content_description_name (desc),
8305 dwarf_form_name (form));
8306 if (i + 1 < directory_entry_format_count)
8311 uint64_t directories_count;
8312 if ((size_t) (lineendp - linep) < 1)
8314 get_uleb128 (directories_count, linep, lineendp);
8316 if (directory_entry_format_count == 0
8317 && directories_count != 0)
8320 for (uint64_t i = 0; i < directories_count; i++)
8322 printf (" %-5" PRIu64 " ", i);
8323 for (int j = 0; j < directory_entry_format_count; j++)
8325 linep = print_form_data (dbg, enc[j].form,
8326 linep, lineendp, length,
8328 if (j + 1 < directory_entry_format_count)
8338 unsigned char *endp = memchr (linep, '\0', lineendp - linep);
8339 if (unlikely (endp == NULL))
8342 printf (" %s\n", (char *) linep);
8346 /* Skip the final NUL byte. */
8350 if (unlikely (linep >= lineendp))
8353 puts (gettext ("\nFile name table:"));
8356 struct encpair { uint16_t desc; uint16_t form; };
8357 struct encpair enc[256];
8359 printf (gettext (" ["));
8360 if ((size_t) (lineendp - linep) < 1)
8362 unsigned char file_name_format_count = *linep++;
8363 for (int i = 0; i < file_name_format_count; i++)
8365 uint64_t desc, form;
8366 if ((size_t) (lineendp - linep) < 1)
8368 get_uleb128 (desc, linep, lineendp);
8369 if ((size_t) (lineendp - linep) < 1)
8371 get_uleb128 (form, linep, lineendp);
8373 if (! libdw_valid_user_form (form))
8380 dwarf_line_content_description_name (desc),
8381 dwarf_form_name (form));
8382 if (i + 1 < file_name_format_count)
8387 uint64_t file_name_count;
8388 if ((size_t) (lineendp - linep) < 1)
8390 get_uleb128 (file_name_count, linep, lineendp);
8392 if (file_name_format_count == 0
8393 && file_name_count != 0)
8396 for (uint64_t i = 0; i < file_name_count; i++)
8398 printf (" %-5" PRIu64 " ", i);
8399 for (int j = 0; j < file_name_format_count; j++)
8401 linep = print_form_data (dbg, enc[j].form,
8402 linep, lineendp, length,
8404 if (j + 1 < file_name_format_count)
8412 puts (gettext (" Entry Dir Time Size Name"));
8413 for (unsigned int cnt = 1; *linep != 0; ++cnt)
8415 /* First comes the file name. */
8416 char *fname = (char *) linep;
8417 unsigned char *endp = memchr (fname, '\0', lineendp - linep);
8418 if (unlikely (endp == NULL))
8422 /* Then the index. */
8423 unsigned int diridx;
8424 if (lineendp - linep < 1)
8426 get_uleb128 (diridx, linep, lineendp);
8428 /* Next comes the modification time. */
8430 if (lineendp - linep < 1)
8432 get_uleb128 (mtime, linep, lineendp);
8434 /* Finally the length of the file. */
8436 if (lineendp - linep < 1)
8438 get_uleb128 (fsize, linep, lineendp);
8440 printf (" %-5u %-5u %-9u %-9u %s\n",
8441 cnt, diridx, mtime, fsize, fname);
8443 /* Skip the final NUL byte. */
8447 puts (gettext ("\nLine number statements:"));
8448 Dwarf_Word address = 0;
8449 unsigned int op_index = 0;
8451 uint_fast8_t is_stmt = default_is_stmt;
8453 /* Determine the CU this block is for. */
8455 Dwarf_Off ncuoffset = 0;
8457 while (dwarf_nextcu (dbg, cuoffset = ncuoffset, &ncuoffset, &hsize,
8458 NULL, NULL, NULL) == 0)
8461 if (dwarf_offdie (dbg, cuoffset + hsize, &cudie) == NULL)
8463 Dwarf_Attribute stmt_list;
8464 if (dwarf_attr (&cudie, DW_AT_stmt_list, &stmt_list) == NULL)
8467 if (dwarf_formudata (&stmt_list, &lineoff) != 0)
8469 if (lineoff == start_offset)
8472 address_size = cudie.cu->address_size;
8477 /* Apply the "operation advance" from a special opcode
8478 or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */
8479 unsigned int op_addr_advance;
8481 inline void advance_pc (unsigned int op_advance)
8483 op_addr_advance = minimum_instr_len * ((op_index + op_advance)
8484 / max_ops_per_instr);
8485 address += op_addr_advance;
8486 show_op_index = (op_index > 0 ||
8487 (op_index + op_advance) % max_ops_per_instr > 0);
8488 op_index = (op_index + op_advance) % max_ops_per_instr;
8491 if (max_ops_per_instr == 0)
8494 gettext ("invalid maximum operations per instruction is zero"));
8499 while (linep < lineendp)
8501 size_t offset = linep - (const unsigned char *) data->d_buf;
8505 /* Read the opcode. */
8506 unsigned int opcode = *linep++;
8508 printf (" [%6" PRIx64 "]", (uint64_t)offset);
8509 /* Is this a special opcode? */
8510 if (likely (opcode >= opcode_base))
8512 if (unlikely (line_range == 0))
8515 /* Yes. Handling this is quite easy since the opcode value
8518 opcode = (desired line increment - line_base)
8519 + (line_range * address advance) + opcode_base
8521 int line_increment = (line_base
8522 + (opcode - opcode_base) % line_range);
8524 /* Perform the increments. */
8525 line += line_increment;
8526 advance_pc ((opcode - opcode_base) / line_range);
8528 char *a = format_dwarf_addr (dwflmod, 0, address, address);
8531 special opcode %u: address+%u = %s, op_index = %u, line%+d = %zu\n"),
8532 opcode, op_addr_advance, a, op_index,
8533 line_increment, line);
8536 special opcode %u: address+%u = %s, line%+d = %zu\n"),
8537 opcode, op_addr_advance, a, line_increment, line);
8540 else if (opcode == 0)
8542 /* This an extended opcode. */
8543 if (unlikely (linep + 2 > lineendp))
8547 unsigned int len = *linep++;
8549 if (unlikely (linep + len > lineendp))
8552 /* The sub-opcode. */
8555 printf (gettext (" extended opcode %u: "), opcode);
8559 case DW_LNE_end_sequence:
8560 puts (gettext (" end of sequence"));
8562 /* Reset the registers we care about. */
8566 is_stmt = default_is_stmt;
8569 case DW_LNE_set_address:
8571 if (unlikely ((size_t) (lineendp - linep) < address_size))
8573 if (address_size == 4)
8574 address = read_4ubyte_unaligned_inc (dbg, linep);
8576 address = read_8ubyte_unaligned_inc (dbg, linep);
8578 char *a = format_dwarf_addr (dwflmod, 0, address, address);
8579 printf (gettext (" set address to %s\n"), a);
8584 case DW_LNE_define_file:
8586 char *fname = (char *) linep;
8587 unsigned char *endp = memchr (linep, '\0',
8589 if (unlikely (endp == NULL))
8593 unsigned int diridx;
8594 if (lineendp - linep < 1)
8596 get_uleb128 (diridx, linep, lineendp);
8598 if (lineendp - linep < 1)
8600 get_uleb128 (mtime, linep, lineendp);
8601 Dwarf_Word filelength;
8602 if (lineendp - linep < 1)
8604 get_uleb128 (filelength, linep, lineendp);
8607 define new file: dir=%u, mtime=%" PRIu64 ", length=%" PRIu64 ", name=%s\n"),
8608 diridx, (uint64_t) mtime, (uint64_t) filelength,
8613 case DW_LNE_set_discriminator:
8614 /* Takes one ULEB128 parameter, the discriminator. */
8615 if (unlikely (standard_opcode_lengths[opcode] != 1))
8618 get_uleb128 (u128, linep, lineendp);
8619 printf (gettext (" set discriminator to %u\n"), u128);
8623 /* Unknown, ignore it. */
8624 puts (gettext (" unknown opcode"));
8629 else if (opcode <= DW_LNS_set_isa)
8631 /* This is a known standard opcode. */
8635 /* Takes no argument. */
8636 puts (gettext (" copy"));
8639 case DW_LNS_advance_pc:
8640 /* Takes one uleb128 parameter which is added to the
8642 get_uleb128 (u128, linep, lineendp);
8645 char *a = format_dwarf_addr (dwflmod, 0, address, address);
8648 advance address by %u to %s, op_index to %u\n"),
8649 op_addr_advance, a, op_index);
8651 printf (gettext (" advance address by %u to %s\n"),
8652 op_addr_advance, a);
8657 case DW_LNS_advance_line:
8658 /* Takes one sleb128 parameter which is added to the
8660 get_sleb128 (s128, linep, lineendp);
8663 advance line by constant %d to %" PRId64 "\n"),
8664 s128, (int64_t) line);
8667 case DW_LNS_set_file:
8668 /* Takes one uleb128 parameter which is stored in file. */
8669 get_uleb128 (u128, linep, lineendp);
8670 printf (gettext (" set file to %" PRIu64 "\n"),
8674 case DW_LNS_set_column:
8675 /* Takes one uleb128 parameter which is stored in column. */
8676 if (unlikely (standard_opcode_lengths[opcode] != 1))
8679 get_uleb128 (u128, linep, lineendp);
8680 printf (gettext (" set column to %" PRIu64 "\n"),
8684 case DW_LNS_negate_stmt:
8685 /* Takes no argument. */
8686 is_stmt = 1 - is_stmt;
8687 printf (gettext (" set '%s' to %" PRIuFAST8 "\n"),
8688 "is_stmt", is_stmt);
8691 case DW_LNS_set_basic_block:
8692 /* Takes no argument. */
8693 puts (gettext (" set basic block flag"));
8696 case DW_LNS_const_add_pc:
8697 /* Takes no argument. */
8699 if (unlikely (line_range == 0))
8702 advance_pc ((255 - opcode_base) / line_range);
8704 char *a = format_dwarf_addr (dwflmod, 0, address, address);
8707 advance address by constant %u to %s, op_index to %u\n"),
8708 op_addr_advance, a, op_index);
8711 advance address by constant %u to %s\n"),
8712 op_addr_advance, a);
8717 case DW_LNS_fixed_advance_pc:
8718 /* Takes one 16 bit parameter which is added to the
8720 if (unlikely (standard_opcode_lengths[opcode] != 1))
8723 u128 = read_2ubyte_unaligned_inc (dbg, linep);
8727 char *a = format_dwarf_addr (dwflmod, 0, address, address);
8729 advance address by fixed value %u to %s\n"),
8735 case DW_LNS_set_prologue_end:
8736 /* Takes no argument. */
8737 puts (gettext (" set prologue end flag"));
8740 case DW_LNS_set_epilogue_begin:
8741 /* Takes no argument. */
8742 puts (gettext (" set epilogue begin flag"));
8745 case DW_LNS_set_isa:
8746 /* Takes one uleb128 parameter which is stored in isa. */
8747 if (unlikely (standard_opcode_lengths[opcode] != 1))
8750 get_uleb128 (u128, linep, lineendp);
8751 printf (gettext (" set isa to %u\n"), u128);
8757 /* This is a new opcode the generator but not we know about.
8758 Read the parameters associated with it but then discard
8759 everything. Read all the parameters for this opcode. */
8760 printf (ngettext (" unknown opcode with %" PRIu8 " parameter:",
8761 " unknown opcode with %" PRIu8 " parameters:",
8762 standard_opcode_lengths[opcode]),
8763 standard_opcode_lengths[opcode]);
8764 for (int n = standard_opcode_lengths[opcode]; n > 0; --n)
8766 get_uleb128 (u128, linep, lineendp);
8767 if (n != standard_opcode_lengths[opcode])
8768 putc_unlocked (',', stdout);
8769 printf (" %u", u128);
8772 /* Next round, ignore this opcode. */
8778 /* There must only be one data block. */
8779 assert (elf_getdata (scn, data) == NULL);
8784 print_debug_loclists_section (Dwfl_Module *dwflmod,
8785 Ebl *ebl, GElf_Ehdr *ehdr,
8786 Elf_Scn *scn, GElf_Shdr *shdr,
8790 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
8791 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
8792 (uint64_t) shdr->sh_offset);
8794 Elf_Data *data = (dbg->sectiondata[IDX_debug_loclists]
8795 ?: elf_rawdata (scn, NULL));
8796 if (unlikely (data == NULL))
8798 error (0, 0, gettext ("cannot get .debug_loclists content: %s"),
8803 /* For the listptr to get the base address/CU. */
8804 sort_listptr (&known_loclistsptr, "loclistsptr");
8805 size_t listptr_idx = 0;
8807 const unsigned char *readp = data->d_buf;
8808 const unsigned char *const dataend = ((unsigned char *) data->d_buf
8810 while (readp < dataend)
8812 if (unlikely (readp > dataend - 4))
8815 error (0, 0, gettext ("invalid data in section [%zu] '%s'"),
8816 elf_ndxscn (scn), section_name (ebl, ehdr, shdr));
8820 ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
8821 printf (gettext ("Table at Offset 0x%" PRIx64 ":\n\n"),
8824 uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
8825 unsigned int offset_size = 4;
8826 if (unlikely (unit_length == 0xffffffff))
8828 if (unlikely (readp > dataend - 8))
8831 unit_length = read_8ubyte_unaligned_inc (dbg, readp);
8834 printf (gettext (" Length: %8" PRIu64 "\n"), unit_length);
8836 /* We need at least 2-bytes + 1-byte + 1-byte + 4-bytes = 8
8837 bytes to complete the header. And this unit cannot go beyond
8838 the section data. */
8839 if (readp > dataend - 8
8841 || unit_length > (uint64_t) (dataend - readp))
8844 const unsigned char *nexthdr = readp + unit_length;
8846 uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
8847 printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
8851 error (0, 0, gettext ("Unknown version"));
8855 uint8_t address_size = *readp++;
8856 printf (gettext (" Address size: %8" PRIu64 "\n"),
8857 (uint64_t) address_size);
8859 if (address_size != 4 && address_size != 8)
8861 error (0, 0, gettext ("unsupported address size"));
8865 uint8_t segment_size = *readp++;
8866 printf (gettext (" Segment size: %8" PRIu64 "\n"),
8867 (uint64_t) segment_size);
8869 if (segment_size != 0)
8871 error (0, 0, gettext ("unsupported segment size"));
8875 uint32_t offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
8876 printf (gettext (" Offset entries: %8" PRIu64 "\n"),
8877 (uint64_t) offset_entry_count);
8879 /* We need the CU that uses this unit to get the initial base address. */
8880 Dwarf_Addr cu_base = 0;
8881 struct Dwarf_CU *cu = NULL;
8882 if (listptr_cu (&known_loclistsptr, &listptr_idx,
8884 (Dwarf_Off) (nexthdr - (unsigned char *) data->d_buf),
8886 || split_dwarf_cu_base (dbg, &cu, &cu_base))
8888 char *basestr = format_dwarf_addr (dwflmod, address_size,
8891 if (dwarf_cu_die (cu, &cudie,
8892 NULL, NULL, NULL, NULL,
8893 NULL, NULL) == NULL)
8894 printf (gettext (" Unknown CU base: %s\n"), basestr);
8896 printf (gettext (" CU [%6" PRIx64 "] base: %s\n"),
8897 dwarf_dieoffset (&cudie), basestr);
8901 printf (gettext (" Not associated with a CU.\n"));
8905 const unsigned char *offset_array_start = readp;
8906 if (offset_entry_count > 0)
8908 uint64_t needed = offset_entry_count * offset_size;
8909 if (unit_length - 8 < needed)
8912 gettext ("too many offset entries for unit length"));
8916 printf (gettext (" Offsets starting at 0x%" PRIx64 ":\n"),
8917 (uint64_t) (offset_array_start
8918 - (unsigned char *) data->d_buf));
8919 for (uint32_t idx = 0; idx < offset_entry_count; idx++)
8921 printf (" [%6" PRIu32 "] ", idx);
8922 if (offset_size == 4)
8924 uint32_t off = read_4ubyte_unaligned_inc (dbg, readp);
8925 printf ("0x%" PRIx32 "\n", off);
8929 uint64_t off = read_8ubyte_unaligned_inc (dbg, readp);
8930 printf ("0x%" PRIx64 "\n", off);
8936 Dwarf_Addr base = cu_base;
8937 bool start_of_list = true;
8938 while (readp < nexthdr)
8940 uint8_t kind = *readp++;
8941 uint64_t op1, op2, len;
8945 if (start_of_list && kind == DW_LLE_end_of_list)
8951 printf (" Offset: %" PRIx64 ", Index: %" PRIx64 "\n",
8952 (uint64_t) (readp - (unsigned char *) data->d_buf - 1),
8953 (uint64_t) (readp - offset_array_start - 1));
8954 start_of_list = false;
8957 printf (" %s", dwarf_loc_list_encoding_name (kind));
8960 case DW_LLE_end_of_list:
8961 start_of_list = true;
8965 case DW_LLE_base_addressx:
8966 if ((uint64_t) (nexthdr - readp) < 1)
8969 error (0, 0, gettext ("invalid loclists data"));
8972 get_uleb128 (op1, readp, nexthdr);
8973 printf (" %" PRIx64 "\n", op1);
8974 if (! print_unresolved_addresses)
8977 if (get_indexed_addr (cu, op1, &addr) != 0)
8981 a1 = format_dwarf_addr (dwflmod, address_size,
8983 printf (" %s\n", a1);
8989 case DW_LLE_startx_endx:
8990 if ((uint64_t) (nexthdr - readp) < 1)
8992 get_uleb128 (op1, readp, nexthdr);
8993 if ((uint64_t) (nexthdr - readp) < 1)
8995 get_uleb128 (op2, readp, nexthdr);
8996 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
8997 if (! print_unresolved_addresses)
9001 if (get_indexed_addr (cu, op1, &addr1) != 0
9002 || get_indexed_addr (cu, op2, &addr2) != 0)
9004 printf (" ???..\n");
9009 a1 = format_dwarf_addr (dwflmod, address_size,
9011 a2 = format_dwarf_addr (dwflmod, address_size,
9013 printf (" %s..\n", a1);
9014 printf (" %s\n", a2);
9019 if ((uint64_t) (nexthdr - readp) < 1)
9021 get_uleb128 (len, readp, nexthdr);
9022 if ((uint64_t) (nexthdr - readp) < len)
9024 print_ops (dwflmod, dbg, 8, 8, version,
9025 address_size, offset_size, cu, len, readp);
9029 case DW_LLE_startx_length:
9030 if ((uint64_t) (nexthdr - readp) < 1)
9032 get_uleb128 (op1, readp, nexthdr);
9033 if ((uint64_t) (nexthdr - readp) < 1)
9035 get_uleb128 (op2, readp, nexthdr);
9036 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9037 if (! print_unresolved_addresses)
9041 if (get_indexed_addr (cu, op1, &addr1) != 0)
9043 printf (" ???..\n");
9048 addr2 = addr1 + op2;
9049 a1 = format_dwarf_addr (dwflmod, address_size,
9051 a2 = format_dwarf_addr (dwflmod, address_size,
9053 printf (" %s..\n", a1);
9054 printf (" %s..\n", a2);
9059 if ((uint64_t) (nexthdr - readp) < 1)
9061 get_uleb128 (len, readp, nexthdr);
9062 if ((uint64_t) (nexthdr - readp) < len)
9064 print_ops (dwflmod, dbg, 8, 8, version,
9065 address_size, offset_size, cu, len, readp);
9069 case DW_LLE_offset_pair:
9070 if ((uint64_t) (nexthdr - readp) < 1)
9072 get_uleb128 (op1, readp, nexthdr);
9073 if ((uint64_t) (nexthdr - readp) < 1)
9075 get_uleb128 (op2, readp, nexthdr);
9076 printf (" %" PRIx64 ", %" PRIx64 "\n", op1, op2);
9077 if (! print_unresolved_addresses)
9081 a1 = format_dwarf_addr (dwflmod, address_size, op1, op1);
9082 a2 = format_dwarf_addr (dwflmod, address_size,
9084 printf (" %s..\n", a1);
9085 printf (" %s\n", a2);
9089 if ((uint64_t) (nexthdr - readp) < 1)
9091 get_uleb128 (len, readp, nexthdr);
9092 if ((uint64_t) (nexthdr - readp) < len)
9094 print_ops (dwflmod, dbg, 8, 8, version,
9095 address_size, offset_size, cu, len, readp);
9099 case DW_LLE_default_location:
9100 if ((uint64_t) (nexthdr - readp) < 1)
9102 get_uleb128 (len, readp, nexthdr);
9103 if ((uint64_t) (nexthdr - readp) < len)
9105 print_ops (dwflmod, dbg, 8, 8, version,
9106 address_size, offset_size, cu, len, readp);
9110 case DW_LLE_base_address:
9111 if (address_size == 4)
9113 if ((uint64_t) (nexthdr - readp) < 4)
9115 op1 = read_4ubyte_unaligned_inc (dbg, readp);
9119 if ((uint64_t) (nexthdr - readp) < 8)
9121 op1 = read_8ubyte_unaligned_inc (dbg, readp);
9124 printf (" 0x%" PRIx64 "\n", base);
9125 if (! print_unresolved_addresses)
9127 a1 = format_dwarf_addr (dwflmod, address_size, base, base);
9128 printf (" %s\n", a1);
9133 case DW_LLE_start_end:
9134 if (address_size == 4)
9136 if ((uint64_t) (nexthdr - readp) < 8)
9138 op1 = read_4ubyte_unaligned_inc (dbg, readp);
9139 op2 = read_4ubyte_unaligned_inc (dbg, readp);
9143 if ((uint64_t) (nexthdr - readp) < 16)
9145 op1 = read_8ubyte_unaligned_inc (dbg, readp);
9146 op2 = read_8ubyte_unaligned_inc (dbg, readp);
9148 printf (" 0x%" PRIx64 "..0x%" PRIx64 "\n", op1, op2);
9149 if (! print_unresolved_addresses)
9151 a1 = format_dwarf_addr (dwflmod, address_size, op1, op1);
9152 a2 = format_dwarf_addr (dwflmod, address_size,
9154 printf (" %s..\n", a1);
9155 printf (" %s\n", a2);
9159 if ((uint64_t) (nexthdr - readp) < 1)
9161 get_uleb128 (len, readp, nexthdr);
9162 if ((uint64_t) (nexthdr - readp) < len)
9164 print_ops (dwflmod, dbg, 8, 8, version,
9165 address_size, offset_size, cu, len, readp);
9169 case DW_LLE_start_length:
9170 if (address_size == 4)
9172 if ((uint64_t) (nexthdr - readp) < 4)
9174 op1 = read_4ubyte_unaligned_inc (dbg, readp);
9178 if ((uint64_t) (nexthdr - readp) < 8)
9180 op1 = read_8ubyte_unaligned_inc (dbg, readp);
9182 if ((uint64_t) (nexthdr - readp) < 1)
9184 get_uleb128 (op2, readp, nexthdr);
9185 printf (" 0x%" PRIx64 ", %" PRIx64 "\n", op1, op2);
9186 if (! print_unresolved_addresses)
9188 a1 = format_dwarf_addr (dwflmod, address_size, op1, op1);
9190 a2 = format_dwarf_addr (dwflmod, address_size,
9192 printf (" %s..\n", a1);
9193 printf (" %s\n", a2);
9197 if ((uint64_t) (nexthdr - readp) < 1)
9199 get_uleb128 (len, readp, nexthdr);
9200 if ((uint64_t) (nexthdr - readp) < len)
9202 print_ops (dwflmod, dbg, 8, 8, version,
9203 address_size, offset_size, cu, len, readp);
9213 if (readp != nexthdr)
9215 size_t padding = nexthdr - readp;
9216 printf (gettext (" %zu padding bytes\n\n"), padding);
9224 print_debug_loc_section (Dwfl_Module *dwflmod,
9225 Ebl *ebl, GElf_Ehdr *ehdr,
9226 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9228 Elf_Data *data = (dbg->sectiondata[IDX_debug_loc]
9229 ?: elf_rawdata (scn, NULL));
9231 if (unlikely (data == NULL))
9233 error (0, 0, gettext ("cannot get .debug_loc content: %s"),
9239 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9240 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
9241 (uint64_t) shdr->sh_offset);
9243 sort_listptr (&known_locsptr, "loclistptr");
9244 size_t listptr_idx = 0;
9246 uint_fast8_t address_size = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
9247 uint_fast8_t offset_size = 4;
9250 Dwarf_Addr base = 0;
9251 unsigned char *readp = data->d_buf;
9252 unsigned char *const endp = (unsigned char *) data->d_buf + data->d_size;
9253 Dwarf_CU *last_cu = NULL;
9254 while (readp < endp)
9256 ptrdiff_t offset = readp - (unsigned char *) data->d_buf;
9257 Dwarf_CU *cu = last_cu;
9258 unsigned int attr = 0;
9260 if (first && skip_listptr_hole (&known_locsptr, &listptr_idx,
9261 &address_size, &offset_size, &base,
9262 &cu, offset, &readp, endp, &attr))
9267 char *basestr = format_dwarf_addr (dwflmod, address_size,
9270 if (dwarf_cu_die (cu, &cudie,
9271 NULL, NULL, NULL, NULL,
9272 NULL, NULL) == NULL)
9273 printf (gettext ("\n Unknown CU base: %s\n"), basestr);
9275 printf (gettext ("\n CU [%6" PRIx64 "] base: %s\n"),
9276 dwarf_dieoffset (&cudie), basestr);
9281 if (attr == DW_AT_GNU_locviews)
9283 Dwarf_Off next_off = next_listptr_offset (&known_locsptr,
9285 const unsigned char *locp = readp;
9286 const unsigned char *locendp;
9290 locendp = (const unsigned char *) data->d_buf + next_off;
9292 while (locp < locendp)
9295 get_uleb128 (v1, locp, locendp);
9296 if (locp >= locendp)
9298 printf (gettext (" [%6tx] <INVALID DATA>\n"), offset);
9301 get_uleb128 (v2, locp, locendp);
9302 if (first) /* First view pair in a list. */
9303 printf (" [%6tx] ", offset);
9306 printf ("view pair %" PRId64 ", %" PRId64 "\n", v1, v2);
9311 readp = (unsigned char *) locendp;
9315 /* GNU DebugFission encoded addresses as addrx. */
9316 bool is_debugfission = ((cu != NULL
9317 || split_dwarf_cu_base (dbg, &cu, &base))
9319 && cu->unit_type == DW_UT_split_compile));
9320 if (!is_debugfission
9321 && unlikely (data->d_size - offset < (size_t) address_size * 2))
9324 printf (gettext (" [%6tx] <INVALID DATA>\n"), offset);
9330 bool use_base = true;
9331 if (is_debugfission)
9333 const unsigned char *locp = readp;
9334 const unsigned char *locendp = readp + data->d_size;
9335 if (locp >= locendp)
9339 unsigned char code = *locp++;
9342 case DW_LLE_GNU_end_of_list_entry:
9347 case DW_LLE_GNU_base_address_selection_entry:
9348 if (locp >= locendp)
9350 begin = (Dwarf_Addr) -1;
9351 get_uleb128 (idx, locp, locendp);
9352 if (get_indexed_addr (cu, idx, &end) != 0)
9353 end = idx; /* ... */
9356 case DW_LLE_GNU_start_end_entry:
9357 if (locp >= locendp)
9359 get_uleb128 (idx, locp, locendp);
9360 if (get_indexed_addr (cu, idx, &begin) != 0)
9361 end = idx; /* ... */
9362 if (locp >= locendp)
9364 get_uleb128 (idx, locp, locendp);
9365 if (get_indexed_addr (cu, idx, &end) != 0)
9366 end = idx; /* ... */
9370 case DW_LLE_GNU_start_length_entry:
9371 if (locp >= locendp)
9373 get_uleb128 (idx, locp, locendp);
9374 if (get_indexed_addr (cu, idx, &begin) != 0)
9375 begin = idx; /* ... */
9376 if (locendp - locp < 4)
9378 end = read_4ubyte_unaligned_inc (dbg, locp);
9387 readp = (unsigned char *) locp;
9389 else if (address_size == 8)
9391 begin = read_8ubyte_unaligned_inc (dbg, readp);
9392 end = read_8ubyte_unaligned_inc (dbg, readp);
9396 begin = read_4ubyte_unaligned_inc (dbg, readp);
9397 end = read_4ubyte_unaligned_inc (dbg, readp);
9398 if (begin == (Dwarf_Addr) (uint32_t) -1)
9399 begin = (Dwarf_Addr) -1l;
9402 if (begin == (Dwarf_Addr) -1l) /* Base address entry. */
9404 char *b = format_dwarf_addr (dwflmod, address_size, end, end);
9405 printf (gettext (" [%6tx] base address\n %s\n"), offset, b);
9409 else if (begin == 0 && end == 0) /* End of list entry. */
9412 printf (gettext (" [%6tx] empty list\n"), offset);
9417 /* We have a location expression entry. */
9418 uint_fast16_t len = read_2ubyte_unaligned_inc (dbg, readp);
9420 if (first) /* First entry in a list. */
9421 printf (" [%6tx] ", offset);
9425 printf ("range %" PRIx64 ", %" PRIx64 "\n", begin, end);
9426 if (! print_unresolved_addresses)
9428 Dwarf_Addr dab = use_base ? base + begin : begin;
9429 Dwarf_Addr dae = use_base ? base + end : end;
9430 char *b = format_dwarf_addr (dwflmod, address_size,
9432 char *e = format_dwarf_addr (dwflmod, address_size,
9434 printf (" %s..\n", b);
9435 printf (" %s\n", e);
9440 if (endp - readp <= (ptrdiff_t) len)
9442 fputs (gettext (" <INVALID DATA>\n"), stdout);
9446 print_ops (dwflmod, dbg, 11, 11,
9447 cu != NULL ? cu->version : 3,
9448 address_size, offset_size, cu, len, readp);
9461 struct mac_culist *next;
9466 mac_compare (const void *p1, const void *p2)
9468 struct mac_culist *m1 = (struct mac_culist *) p1;
9469 struct mac_culist *m2 = (struct mac_culist *) p2;
9471 if (m1->offset < m2->offset)
9473 if (m1->offset > m2->offset)
9480 print_debug_macinfo_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9481 Ebl *ebl, GElf_Ehdr *ehdr,
9482 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9485 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9486 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
9487 (uint64_t) shdr->sh_offset);
9488 putc_unlocked ('\n', stdout);
9490 /* There is no function in libdw to iterate over the raw content of
9491 the section but it is easy enough to do. */
9492 Elf_Data *data = (dbg->sectiondata[IDX_debug_macinfo]
9493 ?: elf_rawdata (scn, NULL));
9494 if (unlikely (data == NULL))
9496 error (0, 0, gettext ("cannot get macro information section data: %s"),
9501 /* Get the source file information for all CUs. */
9505 struct mac_culist *culist = NULL;
9507 while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
9510 if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
9513 Dwarf_Attribute attr;
9514 if (dwarf_attr (&cudie, DW_AT_macro_info, &attr) == NULL)
9518 if (dwarf_formudata (&attr, &macoff) != 0)
9521 struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
9523 newp->offset = macoff;
9525 newp->next = culist;
9530 /* Convert the list into an array for easier consumption. */
9531 struct mac_culist *cus = (struct mac_culist *) alloca ((nculist + 1)
9534 cus[nculist].offset = data->d_size;
9535 cus[nculist].files = (Dwarf_Files *) -1l;
9538 for (size_t cnt = nculist - 1; culist != NULL; --cnt)
9540 assert (cnt < nculist);
9542 culist = culist->next;
9545 /* Sort the array according to the offset in the .debug_macinfo
9546 section. Note we keep the sentinel at the end. */
9547 qsort (cus, nculist, sizeof (*cus), mac_compare);
9550 const unsigned char *readp = (const unsigned char *) data->d_buf;
9551 const unsigned char *readendp = readp + data->d_size;
9554 while (readp < readendp)
9556 unsigned int opcode = *readp++;
9558 unsigned int u128_2;
9559 const unsigned char *endp;
9563 case DW_MACINFO_define:
9564 case DW_MACINFO_undef:
9565 case DW_MACINFO_vendor_ext:
9566 /* For the first two opcodes the parameters are
9570 We can treat these cases together. */
9571 get_uleb128 (u128, readp, readendp);
9573 endp = memchr (readp, '\0', readendp - readp);
9574 if (unlikely (endp == NULL))
9577 %*s*** non-terminated string at end of section"),
9582 if (opcode == DW_MACINFO_define)
9583 printf ("%*s#define %s, line %u\n",
9584 level, "", (char *) readp, u128);
9585 else if (opcode == DW_MACINFO_undef)
9586 printf ("%*s#undef %s, line %u\n",
9587 level, "", (char *) readp, u128);
9589 printf (" #vendor-ext %s, number %u\n", (char *) readp, u128);
9594 case DW_MACINFO_start_file:
9595 /* The two parameters are line and file index, in this order. */
9596 get_uleb128 (u128, readp, readendp);
9597 if (readendp - readp < 1)
9600 %*s*** missing DW_MACINFO_start_file argument at end of section"),
9604 get_uleb128 (u128_2, readp, readendp);
9606 /* Find the CU DIE for this file. */
9607 size_t macoff = readp - (const unsigned char *) data->d_buf;
9608 const char *fname = "???";
9609 if (macoff >= cus[0].offset)
9611 while (macoff >= cus[1].offset && cus[1].offset != data->d_size)
9614 if (cus[0].files == NULL
9615 && dwarf_getsrcfiles (&cus[0].die, &cus[0].files, NULL) != 0)
9616 cus[0].files = (Dwarf_Files *) -1l;
9618 if (cus[0].files != (Dwarf_Files *) -1l)
9619 fname = (dwarf_filesrc (cus[0].files, u128_2, NULL, NULL)
9623 printf ("%*sstart_file %u, [%u] %s\n",
9624 level, "", u128, u128_2, fname);
9628 case DW_MACINFO_end_file:
9630 printf ("%*send_file\n", level, "");
9631 /* Nothing more to do. */
9635 // XXX gcc seems to generate files with a trailing zero.
9636 if (unlikely (opcode != 0 || readp != readendp))
9637 printf ("%*s*** invalid opcode %u\n", level, "", opcode);
9645 print_debug_macro_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
9646 Ebl *ebl, GElf_Ehdr *ehdr,
9647 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
9650 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
9651 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
9652 (uint64_t) shdr->sh_offset);
9653 putc_unlocked ('\n', stdout);
9655 Elf_Data *data = (dbg->sectiondata[IDX_debug_macro]
9656 ?: elf_rawdata (scn, NULL));
9657 if (unlikely (data == NULL))
9659 error (0, 0, gettext ("cannot get macro information section data: %s"),
9664 /* Get the source file information for all CUs. Uses same
9665 datastructure as macinfo. But uses offset field to directly
9666 match .debug_line offset. And just stored in a list. */
9670 struct mac_culist *culist = NULL;
9672 while (dwarf_nextcu (dbg, offset = ncu, &ncu, &hsize, NULL, NULL, NULL) == 0)
9675 if (dwarf_offdie (dbg, offset + hsize, &cudie) == NULL)
9678 Dwarf_Attribute attr;
9679 if (dwarf_attr (&cudie, DW_AT_stmt_list, &attr) == NULL)
9683 if (dwarf_formudata (&attr, &lineoff) != 0)
9686 struct mac_culist *newp = (struct mac_culist *) alloca (sizeof (*newp));
9688 newp->offset = lineoff;
9690 newp->next = culist;
9695 const unsigned char *readp = (const unsigned char *) data->d_buf;
9696 const unsigned char *readendp = readp + data->d_size;
9698 while (readp < readendp)
9700 printf (gettext (" Offset: 0x%" PRIx64 "\n"),
9701 (uint64_t) (readp - (const unsigned char *) data->d_buf));
9703 // Header, 2 byte version, 1 byte flag, optional .debug_line offset,
9704 // optional vendor extension macro entry table.
9705 if (readp + 2 > readendp)
9708 error (0, 0, gettext ("invalid data"));
9711 const uint16_t vers = read_2ubyte_unaligned_inc (dbg, readp);
9712 printf (gettext (" Version: %" PRIu16 "\n"), vers);
9714 // Version 4 is the GNU extension for DWARF4. DWARF5 will use version
9715 // 5 when it gets standardized.
9716 if (vers != 4 && vers != 5)
9718 printf (gettext (" unknown version, cannot parse section\n"));
9722 if (readp + 1 > readendp)
9724 const unsigned char flag = *readp++;
9725 printf (gettext (" Flag: 0x%" PRIx8 "\n"), flag);
9727 unsigned int offset_len = (flag & 0x01) ? 8 : 4;
9728 printf (gettext (" Offset length: %" PRIu8 "\n"), offset_len);
9729 Dwarf_Off line_offset = -1;
9732 if (offset_len == 8)
9733 line_offset = read_8ubyte_unaligned_inc (dbg, readp);
9735 line_offset = read_4ubyte_unaligned_inc (dbg, readp);
9736 printf (gettext (" .debug_line offset: 0x%" PRIx64 "\n"),
9740 struct mac_culist *cu = NULL;
9741 if (line_offset != (Dwarf_Off) -1)
9744 while (cu != NULL && line_offset != cu->offset)
9748 Dwarf_Off str_offsets_base = str_offsets_base_off (dbg, (cu != NULL
9752 const unsigned char *vendor[DW_MACRO_hi_user - DW_MACRO_lo_user];
9753 memset (vendor, 0, sizeof vendor);
9756 // 1 byte length, for each item, 1 byte opcode, uleb128 number
9757 // of arguments, for each argument 1 byte form code.
9758 if (readp + 1 > readendp)
9760 unsigned int tlen = *readp++;
9761 printf (gettext (" extension opcode table, %" PRIu8 " items:\n"),
9763 for (unsigned int i = 0; i < tlen; i++)
9765 if (readp + 1 > readendp)
9767 unsigned int opcode = *readp++;
9768 printf (gettext (" [%" PRIx8 "]"), opcode);
9769 if (opcode < DW_MACRO_lo_user
9770 || opcode > DW_MACRO_hi_user)
9772 // Record the start of description for this vendor opcode.
9773 // uleb128 nr args, 1 byte per arg form.
9774 vendor[opcode - DW_MACRO_lo_user] = readp;
9775 if (readp + 1 > readendp)
9777 unsigned int args = *readp++;
9780 printf (gettext (" %" PRIu8 " arguments:"), args);
9783 if (readp + 1 > readendp)
9785 unsigned int form = *readp++;
9786 printf (" %s", dwarf_form_name (form));
9787 if (! libdw_valid_user_form (form))
9791 putchar_unlocked (',');
9795 printf (gettext (" no arguments."));
9796 putchar_unlocked ('\n');
9799 putchar_unlocked ('\n');
9802 if (readp + 1 > readendp)
9804 unsigned int opcode = *readp++;
9808 unsigned int u128_2;
9809 const unsigned char *endp;
9814 case DW_MACRO_start_file:
9815 get_uleb128 (u128, readp, readendp);
9816 if (readp >= readendp)
9818 get_uleb128 (u128_2, readp, readendp);
9820 /* Find the CU DIE that matches this line offset. */
9821 const char *fname = "???";
9824 if (cu->files == NULL
9825 && dwarf_getsrcfiles (&cu->die, &cu->files,
9827 cu->files = (Dwarf_Files *) -1l;
9829 if (cu->files != (Dwarf_Files *) -1l)
9830 fname = (dwarf_filesrc (cu->files, u128_2,
9831 NULL, NULL) ?: "???");
9833 printf ("%*sstart_file %u, [%u] %s\n",
9834 level, "", u128, u128_2, fname);
9838 case DW_MACRO_end_file:
9840 printf ("%*send_file\n", level, "");
9843 case DW_MACRO_define:
9844 get_uleb128 (u128, readp, readendp);
9845 endp = memchr (readp, '\0', readendp - readp);
9848 printf ("%*s#define %s, line %u\n",
9849 level, "", readp, u128);
9853 case DW_MACRO_undef:
9854 get_uleb128 (u128, readp, readendp);
9855 endp = memchr (readp, '\0', readendp - readp);
9858 printf ("%*s#undef %s, line %u\n",
9859 level, "", readp, u128);
9863 case DW_MACRO_define_strp:
9864 get_uleb128 (u128, readp, readendp);
9865 if (readp + offset_len > readendp)
9867 if (offset_len == 8)
9868 off = read_8ubyte_unaligned_inc (dbg, readp);
9870 off = read_4ubyte_unaligned_inc (dbg, readp);
9871 printf ("%*s#define %s, line %u (indirect)\n",
9872 level, "", dwarf_getstring (dbg, off, NULL), u128);
9875 case DW_MACRO_undef_strp:
9876 get_uleb128 (u128, readp, readendp);
9877 if (readp + offset_len > readendp)
9879 if (offset_len == 8)
9880 off = read_8ubyte_unaligned_inc (dbg, readp);
9882 off = read_4ubyte_unaligned_inc (dbg, readp);
9883 printf ("%*s#undef %s, line %u (indirect)\n",
9884 level, "", dwarf_getstring (dbg, off, NULL), u128);
9887 case DW_MACRO_import:
9888 if (readp + offset_len > readendp)
9890 if (offset_len == 8)
9891 off = read_8ubyte_unaligned_inc (dbg, readp);
9893 off = read_4ubyte_unaligned_inc (dbg, readp);
9894 printf ("%*s#include offset 0x%" PRIx64 "\n",
9898 case DW_MACRO_define_sup:
9899 get_uleb128 (u128, readp, readendp);
9900 if (readp + offset_len > readendp)
9902 printf ("%*s#define ", level, "");
9903 readp = print_form_data (dbg, DW_FORM_strp_sup,
9904 readp, readendp, offset_len,
9906 printf (", line %u (sup)\n", u128);
9909 case DW_MACRO_undef_sup:
9910 get_uleb128 (u128, readp, readendp);
9911 if (readp + offset_len > readendp)
9913 printf ("%*s#undef ", level, "");
9914 readp = print_form_data (dbg, DW_FORM_strp_sup,
9915 readp, readendp, offset_len,
9917 printf (", line %u (sup)\n", u128);
9920 case DW_MACRO_import_sup:
9921 if (readp + offset_len > readendp)
9923 if (offset_len == 8)
9924 off = read_8ubyte_unaligned_inc (dbg, readp);
9926 off = read_4ubyte_unaligned_inc (dbg, readp);
9927 // XXX Needs support for reading from supplementary object file.
9928 printf ("%*s#include offset 0x%" PRIx64 " (sup)\n",
9932 case DW_MACRO_define_strx:
9933 get_uleb128 (u128, readp, readendp);
9934 if (readp + offset_len > readendp)
9936 printf ("%*s#define ", level, "");
9937 readp = print_form_data (dbg, DW_FORM_strx,
9938 readp, readendp, offset_len,
9940 printf (", line %u (strx)\n", u128);
9943 case DW_MACRO_undef_strx:
9944 get_uleb128 (u128, readp, readendp);
9945 if (readp + offset_len > readendp)
9947 printf ("%*s#undef ", level, "");
9948 readp = print_form_data (dbg, DW_FORM_strx,
9949 readp, readendp, offset_len,
9951 printf (", line %u (strx)\n", u128);
9955 printf ("%*svendor opcode 0x%" PRIx8, level, "", opcode);
9956 if (opcode < DW_MACRO_lo_user
9957 || opcode > DW_MACRO_lo_user
9958 || vendor[opcode - DW_MACRO_lo_user] == NULL)
9961 const unsigned char *op_desc;
9962 op_desc = vendor[opcode - DW_MACRO_lo_user];
9964 // Just skip the arguments, we cannot really interpret them,
9965 // but print as much as we can.
9966 unsigned int args = *op_desc++;
9967 while (args > 0 && readp < readendp)
9969 unsigned int form = *op_desc++;
9970 readp = print_form_data (dbg, form, readp, readendp,
9971 offset_len, str_offsets_base);
9976 putchar_unlocked ('\n');
9979 if (readp + 1 > readendp)
9983 putchar_unlocked ('\n');
9989 /* Callback for printing global names. */
9991 print_pubnames (Dwarf *dbg __attribute__ ((unused)), Dwarf_Global *global,
9994 int *np = (int *) arg;
9996 printf (gettext (" [%5d] DIE offset: %6" PRId64
9997 ", CU DIE offset: %6" PRId64 ", name: %s\n"),
9998 (*np)++, global->die_offset, global->cu_offset, global->name);
10004 /* Print the known exported symbols in the DWARF section '.debug_pubnames'. */
10006 print_debug_pubnames_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10007 Ebl *ebl, GElf_Ehdr *ehdr,
10008 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10010 printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10011 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
10012 (uint64_t) shdr->sh_offset);
10015 (void) dwarf_getpubnames (dbg, print_pubnames, &n, 0);
10018 /* Print the content of the DWARF string section '.debug_str'. */
10020 print_debug_str_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10021 Ebl *ebl, GElf_Ehdr *ehdr,
10022 Elf_Scn *scn, GElf_Shdr *shdr,
10023 Dwarf *dbg __attribute__ ((unused)))
10025 Elf_Data *data = elf_rawdata (scn, NULL);
10026 const size_t sh_size = data ? data->d_size : 0;
10028 /* Compute floor(log16(shdr->sh_size)). */
10029 GElf_Addr tmp = sh_size;
10036 digits = MAX (4, digits);
10038 printf (gettext ("\nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"
10041 section_name (ebl, ehdr, shdr), (uint64_t) shdr->sh_offset,
10042 /* TRANS: the debugstr| prefix makes the string unique. */
10043 digits + 2, sgettext ("debugstr|Offset"));
10045 Dwarf_Off offset = 0;
10046 while (offset < sh_size)
10049 const char *str = (const char *) data->d_buf + offset;
10050 const char *endp = memchr (str, '\0', sh_size - offset);
10051 if (unlikely (endp == NULL))
10053 printf (gettext (" *** error, missing string terminator\n"));
10057 printf (" [%*" PRIx64 "] \"%s\"\n", digits, (uint64_t) offset, str);
10064 print_debug_str_offsets_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10065 Ebl *ebl, GElf_Ehdr *ehdr,
10066 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10068 printf (gettext ("\
10069 \nDWARF section [%2zu] '%s' at offset %#" PRIx64 ":\n"),
10070 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
10071 (uint64_t) shdr->sh_offset);
10073 if (shdr->sh_size == 0)
10076 /* We like to get the section from libdw to make sure they are relocated. */
10077 Elf_Data *data = (dbg->sectiondata[IDX_debug_str_offsets]
10078 ?: elf_rawdata (scn, NULL));
10079 if (unlikely (data == NULL))
10081 error (0, 0, gettext ("cannot get .debug_str_offsets section data: %s"),
10087 sort_listptr (&known_stroffbases, "str_offsets");
10089 const unsigned char *start = (const unsigned char *) data->d_buf;
10090 const unsigned char *readp = start;
10091 const unsigned char *readendp = ((const unsigned char *) data->d_buf
10094 while (readp < readendp)
10096 /* Most string offset tables will have a header. For split
10097 dwarf unit GNU DebugFission didn't add one. But they were
10098 also only defined for split units (main or skeleton units
10099 didn't have indirect strings). So if we don't have a
10100 DW_AT_str_offsets_base at all and this is offset zero, then
10101 just start printing offsets immediately, if this is a .dwo
10103 Dwarf_Off off = (Dwarf_Off) (readp
10104 - (const unsigned char *) data->d_buf);
10106 printf ("Table at offset %" PRIx64 " ", off);
10108 struct listptr *listptr = get_listptr (&known_stroffbases, idx++);
10109 const unsigned char *next_unitp = readendp;
10110 uint8_t offset_size;
10112 if (listptr == NULL)
10114 /* This can happen for .dwo files. There is only an header
10115 in the case this is a version 5 split DWARF file. */
10118 if (dwarf_get_units (dbg, NULL, &cu, NULL, &unit_type,
10121 error (0, 0, "Warning: Cannot find any DWARF unit.");
10122 /* Just guess some values. */
10123 has_header = false;
10127 && (unit_type == DW_UT_split_type
10128 || unit_type == DW_UT_split_compile))
10130 has_header = cu->version > 4;
10131 offset_size = cu->offset_size;
10136 "Warning: No CU references .debug_str_offsets after %"
10138 has_header = cu->version > 4;
10139 offset_size = cu->offset_size;
10145 /* This must be DWARF5, since GNU DebugFission didn't define
10146 DW_AT_str_offsets_base. */
10150 if (dwarf_cu_die (listptr->cu, &cudie,
10151 NULL, NULL, NULL, NULL,
10152 NULL, NULL) == NULL)
10153 printf ("Unknown CU (%s):\n", dwarf_errmsg (-1));
10155 printf ("for CU [%6" PRIx64 "]:\n", dwarf_dieoffset (&cudie));
10160 uint64_t unit_length;
10164 unit_length = read_4ubyte_unaligned_inc (dbg, readp);
10165 if (unlikely (unit_length == 0xffffffff))
10167 if (unlikely (readp > readendp - 8))
10170 error (0, 0, "Invalid data");
10173 unit_length = read_8ubyte_unaligned_inc (dbg, readp);
10180 printf (gettext (" Length: %8" PRIu64 "\n"),
10182 printf (gettext (" Offset size: %8" PRIu8 "\n"),
10185 /* We need at least 2-bytes (version) + 2-bytes (padding) =
10186 4 bytes to complete the header. And this unit cannot go
10187 beyond the section data. */
10188 if (readp > readendp - 4
10190 || unit_length > (uint64_t) (readendp - readp))
10193 next_unitp = readp + unit_length;
10195 version = read_2ubyte_unaligned_inc (dbg, readp);
10196 printf (gettext (" DWARF version: %8" PRIu16 "\n"), version);
10200 error (0, 0, gettext ("Unknown version"));
10204 padding = read_2ubyte_unaligned_inc (dbg, readp);
10205 printf (gettext (" Padding: %8" PRIx16 "\n"), padding);
10207 if (listptr != NULL
10208 && listptr->offset != (Dwarf_Off) (readp - start))
10210 error (0, 0, "String offsets index doesn't start after header");
10218 size_t offsets = (next_unitp - readp) / offset_size;
10219 while (offsets >= 10)
10225 unsigned int index = 0;
10226 size_t index_offset = readp - (const unsigned char *) data->d_buf;
10227 printf (" Offsets start at 0x%zx:\n", index_offset);
10228 while (readp <= next_unitp - offset_size)
10231 if (offset_size == 4)
10232 offset = read_4ubyte_unaligned_inc (dbg, readp);
10234 offset = read_8ubyte_unaligned_inc (dbg, readp);
10235 const char *str = dwarf_getstring (dbg, offset, NULL);
10236 printf (" [%*u] [%*" PRIx64 "] \"%s\"\n",
10237 digits, index++, (int) offset_size * 2, offset, str ?: "???");
10241 if (readp != next_unitp)
10242 error (0, 0, "extra %zd bytes at end of unit",
10243 (size_t) (next_unitp - readp));
10246 readp = next_unitp;
10251 /* Print the content of the call frame search table section
10252 '.eh_frame_hdr'. */
10254 print_debug_frame_hdr_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
10255 Ebl *ebl __attribute__ ((unused)),
10256 GElf_Ehdr *ehdr __attribute__ ((unused)),
10257 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10259 printf (gettext ("\
10260 \nCall frame search table section [%2zu] '.eh_frame_hdr':\n"),
10263 Elf_Data *data = elf_rawdata (scn, NULL);
10265 if (unlikely (data == NULL))
10267 error (0, 0, gettext ("cannot get %s content: %s"),
10268 ".eh_frame_hdr", elf_errmsg (-1));
10272 const unsigned char *readp = data->d_buf;
10273 const unsigned char *const dataend = ((unsigned char *) data->d_buf
10276 if (unlikely (readp + 4 > dataend))
10279 error (0, 0, gettext ("invalid data"));
10283 unsigned int version = *readp++;
10284 unsigned int eh_frame_ptr_enc = *readp++;
10285 unsigned int fde_count_enc = *readp++;
10286 unsigned int table_enc = *readp++;
10288 printf (" version: %u\n"
10289 " eh_frame_ptr_enc: %#x ",
10290 version, eh_frame_ptr_enc);
10291 print_encoding_base ("", eh_frame_ptr_enc);
10292 printf (" fde_count_enc: %#x ", fde_count_enc);
10293 print_encoding_base ("", fde_count_enc);
10294 printf (" table_enc: %#x ", table_enc);
10295 print_encoding_base ("", table_enc);
10297 uint64_t eh_frame_ptr = 0;
10298 if (eh_frame_ptr_enc != DW_EH_PE_omit)
10300 readp = read_encoded (eh_frame_ptr_enc, readp, dataend, &eh_frame_ptr,
10302 if (unlikely (readp == NULL))
10305 printf (" eh_frame_ptr: %#" PRIx64, eh_frame_ptr);
10306 if ((eh_frame_ptr_enc & 0x70) == DW_EH_PE_pcrel)
10307 printf (" (offset: %#" PRIx64 ")",
10308 /* +4 because of the 4 byte header of the section. */
10309 (uint64_t) shdr->sh_offset + 4 + eh_frame_ptr);
10311 putchar_unlocked ('\n');
10314 uint64_t fde_count = 0;
10315 if (fde_count_enc != DW_EH_PE_omit)
10317 readp = read_encoded (fde_count_enc, readp, dataend, &fde_count, dbg);
10318 if (unlikely (readp == NULL))
10321 printf (" fde_count: %" PRIu64 "\n", fde_count);
10324 if (fde_count == 0 || table_enc == DW_EH_PE_omit)
10329 /* Optimize for the most common case. */
10330 if (table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
10331 while (fde_count > 0 && readp + 8 <= dataend)
10333 int32_t initial_location = read_4sbyte_unaligned_inc (dbg, readp);
10334 uint64_t initial_offset = ((uint64_t) shdr->sh_offset
10335 + (int64_t) initial_location);
10336 int32_t address = read_4sbyte_unaligned_inc (dbg, readp);
10337 // XXX Possibly print symbol name or section offset for initial_offset
10338 printf (" %#" PRIx32 " (offset: %#6" PRIx64 ") -> %#" PRIx32
10339 " fde=[%6" PRIx64 "]\n",
10340 initial_location, initial_offset,
10341 address, address - (eh_frame_ptr + 4));
10344 while (0 && readp < dataend)
10351 /* Print the content of the exception handling table section
10352 '.eh_frame_hdr'. */
10354 print_debug_exception_table (Dwfl_Module *dwflmod __attribute__ ((unused)),
10355 Ebl *ebl __attribute__ ((unused)),
10356 GElf_Ehdr *ehdr __attribute__ ((unused)),
10358 GElf_Shdr *shdr __attribute__ ((unused)),
10359 Dwarf *dbg __attribute__ ((unused)))
10361 printf (gettext ("\
10362 \nException handling table section [%2zu] '.gcc_except_table':\n"),
10365 Elf_Data *data = elf_rawdata (scn, NULL);
10367 if (unlikely (data == NULL))
10369 error (0, 0, gettext ("cannot get %s content: %s"),
10370 ".gcc_except_table", elf_errmsg (-1));
10374 const unsigned char *readp = data->d_buf;
10375 const unsigned char *const dataend = readp + data->d_size;
10377 if (unlikely (readp + 1 > dataend))
10380 error (0, 0, gettext ("invalid data"));
10383 unsigned int lpstart_encoding = *readp++;
10384 printf (gettext (" LPStart encoding: %#x "), lpstart_encoding);
10385 print_encoding_base ("", lpstart_encoding);
10386 if (lpstart_encoding != DW_EH_PE_omit)
10389 readp = read_encoded (lpstart_encoding, readp, dataend, &lpstart, dbg);
10390 printf (" LPStart: %#" PRIx64 "\n", lpstart);
10393 if (unlikely (readp + 1 > dataend))
10395 unsigned int ttype_encoding = *readp++;
10396 printf (gettext (" TType encoding: %#x "), ttype_encoding);
10397 print_encoding_base ("", ttype_encoding);
10398 const unsigned char *ttype_base = NULL;
10399 if (ttype_encoding != DW_EH_PE_omit)
10401 unsigned int ttype_base_offset;
10402 get_uleb128 (ttype_base_offset, readp, dataend);
10403 printf (" TType base offset: %#x\n", ttype_base_offset);
10404 if ((size_t) (dataend - readp) > ttype_base_offset)
10405 ttype_base = readp + ttype_base_offset;
10408 if (unlikely (readp + 1 > dataend))
10410 unsigned int call_site_encoding = *readp++;
10411 printf (gettext (" Call site encoding: %#x "), call_site_encoding);
10412 print_encoding_base ("", call_site_encoding);
10413 unsigned int call_site_table_len;
10414 get_uleb128 (call_site_table_len, readp, dataend);
10416 const unsigned char *const action_table = readp + call_site_table_len;
10417 if (unlikely (action_table > dataend))
10419 unsigned int u = 0;
10420 unsigned int max_action = 0;
10421 while (readp < action_table)
10424 puts (gettext ("\n Call site table:"));
10426 uint64_t call_site_start;
10427 readp = read_encoded (call_site_encoding, readp, dataend,
10428 &call_site_start, dbg);
10429 uint64_t call_site_length;
10430 readp = read_encoded (call_site_encoding, readp, dataend,
10431 &call_site_length, dbg);
10432 uint64_t landing_pad;
10433 readp = read_encoded (call_site_encoding, readp, dataend,
10434 &landing_pad, dbg);
10435 unsigned int action;
10436 get_uleb128 (action, readp, dataend);
10437 max_action = MAX (action, max_action);
10438 printf (gettext (" [%4u] Call site start: %#" PRIx64 "\n"
10439 " Call site length: %" PRIu64 "\n"
10440 " Landing pad: %#" PRIx64 "\n"
10442 u++, call_site_start, call_site_length, landing_pad, action);
10444 if (readp != action_table)
10447 unsigned int max_ar_filter = 0;
10448 if (max_action > 0)
10450 puts ("\n Action table:");
10452 size_t maxdata = (size_t) (dataend - action_table);
10453 if (max_action > maxdata || maxdata - max_action < 1)
10455 invalid_action_table:
10456 fputs (gettext (" <INVALID DATA>\n"), stdout);
10460 const unsigned char *const action_table_end
10461 = action_table + max_action + 1;
10467 get_sleb128 (ar_filter, readp, action_table_end);
10468 if (ar_filter > 0 && (unsigned int) ar_filter > max_ar_filter)
10469 max_ar_filter = ar_filter;
10471 if (readp >= action_table_end)
10472 goto invalid_action_table;
10473 get_sleb128 (ar_disp, readp, action_table_end);
10475 printf (" [%4u] ar_filter: % d\n"
10477 u, ar_filter, ar_disp);
10478 if (abs (ar_disp) & 1)
10479 printf (" -> [%4u]\n", u + (ar_disp + 1) / 2);
10480 else if (ar_disp != 0)
10483 putchar_unlocked ('\n');
10486 while (readp < action_table_end);
10489 if (max_ar_filter > 0 && ttype_base != NULL)
10491 unsigned char dsize;
10492 puts ("\n TType table:");
10494 // XXX Not *4, size of encoding;
10495 switch (ttype_encoding & 7)
10497 case DW_EH_PE_udata2:
10498 case DW_EH_PE_sdata2:
10501 case DW_EH_PE_udata4:
10502 case DW_EH_PE_sdata4:
10505 case DW_EH_PE_udata8:
10506 case DW_EH_PE_sdata8:
10511 error (1, 0, gettext ("invalid TType encoding"));
10515 > (size_t) (ttype_base - (const unsigned char *) data->d_buf) / dsize)
10518 readp = ttype_base - max_ar_filter * dsize;
10522 readp = read_encoded (ttype_encoding, readp, ttype_base, &ttype,
10524 printf (" [%4u] %#" PRIx64 "\n", max_ar_filter--, ttype);
10526 while (readp < ttype_base);
10530 /* Print the content of the '.gdb_index' section.
10531 http://sourceware.org/gdb/current/onlinedocs/gdb/Index-Section-Format.html
10534 print_gdb_index_section (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr,
10535 Elf_Scn *scn, GElf_Shdr *shdr, Dwarf *dbg)
10537 printf (gettext ("\nGDB section [%2zu] '%s' at offset %#" PRIx64
10538 " contains %" PRId64 " bytes :\n"),
10539 elf_ndxscn (scn), section_name (ebl, ehdr, shdr),
10540 (uint64_t) shdr->sh_offset, (uint64_t) shdr->sh_size);
10542 Elf_Data *data = elf_rawdata (scn, NULL);
10544 if (unlikely (data == NULL))
10546 error (0, 0, gettext ("cannot get %s content: %s"),
10547 ".gdb_index", elf_errmsg (-1));
10551 // .gdb_index is always in little endian.
10552 Dwarf dummy_dbg = { .other_byte_order = MY_ELFDATA != ELFDATA2LSB };
10555 const unsigned char *readp = data->d_buf;
10556 const unsigned char *const dataend = readp + data->d_size;
10558 if (unlikely (readp + 4 > dataend))
10561 error (0, 0, gettext ("invalid data"));
10565 int32_t vers = read_4ubyte_unaligned (dbg, readp);
10566 printf (gettext (" Version: %" PRId32 "\n"), vers);
10568 // The only difference between version 4 and version 5 is the
10569 // hash used for generating the table. Version 6 contains symbols
10570 // for inlined functions, older versions didn't. Version 7 adds
10571 // symbol kinds. Version 8 just indicates that it correctly includes
10572 // TUs for symbols.
10573 if (vers < 4 || vers > 8)
10575 printf (gettext (" unknown version, cannot parse section\n"));
10580 if (unlikely (readp + 4 > dataend))
10583 uint32_t cu_off = read_4ubyte_unaligned (dbg, readp);
10584 printf (gettext (" CU offset: %#" PRIx32 "\n"), cu_off);
10587 if (unlikely (readp + 4 > dataend))
10590 uint32_t tu_off = read_4ubyte_unaligned (dbg, readp);
10591 printf (gettext (" TU offset: %#" PRIx32 "\n"), tu_off);
10594 if (unlikely (readp + 4 > dataend))
10597 uint32_t addr_off = read_4ubyte_unaligned (dbg, readp);
10598 printf (gettext (" address offset: %#" PRIx32 "\n"), addr_off);
10601 if (unlikely (readp + 4 > dataend))
10604 uint32_t sym_off = read_4ubyte_unaligned (dbg, readp);
10605 printf (gettext (" symbol offset: %#" PRIx32 "\n"), sym_off);
10608 if (unlikely (readp + 4 > dataend))
10611 uint32_t const_off = read_4ubyte_unaligned (dbg, readp);
10612 printf (gettext (" constant offset: %#" PRIx32 "\n"), const_off);
10614 if (unlikely ((size_t) (dataend - (const unsigned char *) data->d_buf)
10618 readp = data->d_buf + cu_off;
10620 const unsigned char *nextp = data->d_buf + tu_off;
10621 if (tu_off >= data->d_size)
10624 size_t cu_nr = (nextp - readp) / 16;
10626 printf (gettext ("\n CU list at offset %#" PRIx32
10627 " contains %zu entries:\n"),
10631 while (dataend - readp >= 16 && n < cu_nr)
10633 uint64_t off = read_8ubyte_unaligned (dbg, readp);
10636 uint64_t len = read_8ubyte_unaligned (dbg, readp);
10639 printf (" [%4zu] start: %0#8" PRIx64
10640 ", length: %5" PRIu64 "\n", n, off, len);
10644 readp = data->d_buf + tu_off;
10645 nextp = data->d_buf + addr_off;
10646 if (addr_off >= data->d_size)
10649 size_t tu_nr = (nextp - readp) / 24;
10651 printf (gettext ("\n TU list at offset %#" PRIx32
10652 " contains %zu entries:\n"),
10656 while (dataend - readp >= 24 && n < tu_nr)
10658 uint64_t off = read_8ubyte_unaligned (dbg, readp);
10661 uint64_t type = read_8ubyte_unaligned (dbg, readp);
10664 uint64_t sig = read_8ubyte_unaligned (dbg, readp);
10667 printf (" [%4zu] CU offset: %5" PRId64
10668 ", type offset: %5" PRId64
10669 ", signature: %0#8" PRIx64 "\n", n, off, type, sig);
10673 readp = data->d_buf + addr_off;
10674 nextp = data->d_buf + sym_off;
10675 if (sym_off >= data->d_size)
10678 size_t addr_nr = (nextp - readp) / 20;
10680 printf (gettext ("\n Address list at offset %#" PRIx32
10681 " contains %zu entries:\n"),
10682 addr_off, addr_nr);
10685 while (dataend - readp >= 20 && n < addr_nr)
10687 uint64_t low = read_8ubyte_unaligned (dbg, readp);
10690 uint64_t high = read_8ubyte_unaligned (dbg, readp);
10693 uint32_t idx = read_4ubyte_unaligned (dbg, readp);
10696 char *l = format_dwarf_addr (dwflmod, 8, low, low);
10697 char *h = format_dwarf_addr (dwflmod, 8, high - 1, high);
10698 printf (" [%4zu] %s..%s, CU index: %5" PRId32 "\n",
10705 const unsigned char *const_start = data->d_buf + const_off;
10706 if (const_off >= data->d_size)
10709 readp = data->d_buf + sym_off;
10710 nextp = const_start;
10711 size_t sym_nr = (nextp - readp) / 8;
10713 printf (gettext ("\n Symbol table at offset %#" PRIx32
10714 " contains %zu slots:\n"),
10718 while (dataend - readp >= 8 && n < sym_nr)
10720 uint32_t name = read_4ubyte_unaligned (dbg, readp);
10723 uint32_t vector = read_4ubyte_unaligned (dbg, readp);
10726 if (name != 0 || vector != 0)
10728 const unsigned char *sym = const_start + name;
10729 if (unlikely ((size_t) (dataend - const_start) < name
10730 || memchr (sym, '\0', dataend - sym) == NULL))
10733 printf (" [%4zu] symbol: %s, CUs: ", n, sym);
10735 const unsigned char *readcus = const_start + vector;
10736 if (unlikely ((size_t) (dataend - const_start) < vector))
10738 uint32_t cus = read_4ubyte_unaligned (dbg, readcus);
10741 uint32_t cu_kind, cu, kind;
10744 if (unlikely (readcus + 4 > dataend))
10746 cu_kind = read_4ubyte_unaligned (dbg, readcus);
10747 cu = cu_kind & ((1 << 24) - 1);
10748 kind = (cu_kind >> 28) & 7;
10749 is_static = cu_kind & (1U << 31);
10750 if (cu > cu_nr - 1)
10751 printf ("%" PRId32 "T", cu - (uint32_t) cu_nr);
10753 printf ("%" PRId32, cu);
10772 printf ("unknown-0x%" PRIx32, kind);
10775 printf (":%c)", (is_static ? 'S' : 'G'));
10786 /* Returns true and sets split DWARF CU id if there is a split compile
10787 unit in the given Dwarf, and no non-split units are found (before it). */
10789 is_split_dwarf (Dwarf *dbg, uint64_t *id, Dwarf_CU **split_cu)
10791 Dwarf_CU *cu = NULL;
10792 while (dwarf_get_units (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
10795 if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
10796 id, NULL, NULL) != 0)
10799 if (unit_type != DW_UT_split_compile && unit_type != DW_UT_split_type)
10802 /* We really only care about the split compile unit, the types
10803 should be fine and self sufficient. Also they don't have an
10804 id that we can match with a skeleton unit. */
10805 if (unit_type == DW_UT_split_compile)
10815 /* Check that there is one and only one Dwfl_Module, return in arg. */
10817 getone_dwflmod (Dwfl_Module *dwflmod,
10818 void **userdata __attribute__ ((unused)),
10819 const char *name __attribute__ ((unused)),
10820 Dwarf_Addr base __attribute__ ((unused)),
10823 Dwfl_Module **m = (Dwfl_Module **) arg;
10825 return DWARF_CB_ABORT;
10827 return DWARF_CB_OK;
10831 print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
10833 /* Used for skeleton file, if necessary for split DWARF. */
10834 Dwfl *skel_dwfl = NULL;
10835 Dwfl_Module *skel_mod = NULL;
10836 char *skel_name = NULL;
10837 Dwarf *split_dbg = NULL;
10838 Dwarf_CU *split_cu = NULL;
10840 /* Before we start the real work get a debug context descriptor. */
10842 Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
10846 .other_byte_order = MY_ELFDATA != ehdr->e_ident[EI_DATA]
10850 if ((print_debug_sections & ~section_exception) != 0)
10851 error (0, 0, gettext ("cannot get debug context descriptor: %s"),
10857 /* If we are asked about a split dwarf (.dwo) file, use the user
10858 provided, or find the corresponding skeleton file. If we got
10859 a skeleton file, replace the given dwflmod and dbg, with one
10860 derived from the skeleton file to provide enough context. */
10862 if (is_split_dwarf (dbg, &split_id, &split_cu))
10864 if (dwarf_skeleton != NULL)
10865 skel_name = strdup (dwarf_skeleton);
10868 /* Replace file.dwo with file.o and see if that matches. */
10870 dwfl_module_info (dwflmod, NULL, NULL, NULL, NULL, NULL,
10874 size_t flen = strlen (fname);
10875 if (flen > 4 && strcmp (".dwo", fname + flen - 4) == 0)
10877 skel_name = strdup (fname);
10878 if (skel_name != NULL)
10880 skel_name[flen - 3] = 'o';
10881 skel_name[flen - 2] = '\0';
10887 if (skel_name != NULL)
10889 int skel_fd = open (skel_name, O_RDONLY);
10891 fprintf (stderr, "Warning: Couldn't open DWARF skeleton file"
10892 " '%s'\n", skel_name);
10894 skel_dwfl = create_dwfl (skel_fd, skel_name);
10896 if (skel_dwfl != NULL)
10898 if (dwfl_getmodules (skel_dwfl, &getone_dwflmod,
10899 &skel_mod, 0) != 0)
10901 fprintf (stderr, "Warning: Bad DWARF skeleton,"
10902 " multiple modules '%s'\n", skel_name);
10903 dwfl_end (skel_dwfl);
10907 else if (skel_fd != -1)
10908 fprintf (stderr, "Warning: Couldn't create skeleton dwfl for"
10909 " '%s': %s\n", skel_name, dwfl_errmsg (-1));
10911 if (skel_mod != NULL)
10913 Dwarf *skel_dbg = dwfl_module_getdwarf (skel_mod, &dwbias);
10914 if (skel_dbg != NULL)
10916 /* First check the skeleton CU DIE, only fetch
10917 the split DIE if we know the id matches to
10918 not unnecessary search for any split DIEs we
10920 Dwarf_CU *cu = NULL;
10921 while (dwarf_get_units (skel_dbg, cu, &cu,
10922 NULL, NULL, NULL, NULL) == 0)
10926 if (dwarf_cu_info (cu, NULL, &unit_type, NULL, NULL,
10927 &skel_id, NULL, NULL) == 0
10928 && unit_type == DW_UT_skeleton
10929 && split_id == skel_id)
10932 if (dwarf_cu_info (cu, NULL, NULL, NULL,
10934 NULL, NULL, NULL) == 0
10935 && dwarf_tag (&subdie) != DW_TAG_invalid)
10937 split_dbg = dwarf_cu_getdwarf (subdie.cu);
10938 if (split_dbg == NULL)
10940 "Warning: Couldn't get split_dbg:"
10941 " %s\n", dwarf_errmsg (-1));
10946 /* Everything matches up, but not
10947 according to libdw. Which means
10948 the user knew better. So...
10949 Terrible hack... We can never
10950 destroy the underlying dwfl
10951 because it would free the wrong
10952 Dwarfs... So we leak memory...*/
10953 if (cu->split == NULL
10954 && dwarf_skeleton != NULL)
10956 do_not_close_dwfl = true;
10957 __libdw_link_skel_split (cu, split_cu);
10958 split_dbg = dwarf_cu_getdwarf (split_cu);
10962 fprintf (stderr, "Warning: Couldn't get"
10963 " skeleton subdie: %s\n",
10964 dwarf_errmsg (-1));
10968 if (split_dbg == NULL)
10969 fprintf (stderr, "Warning: '%s' didn't contain a skeleton for split id %" PRIx64 "\n", skel_name, split_id);
10972 fprintf (stderr, "Warning: Couldn't get skeleton DWARF:"
10973 " %s\n", dwfl_errmsg (-1));
10977 if (split_dbg != NULL)
10980 dwflmod = skel_mod;
10982 else if (skel_name == NULL)
10984 "Warning: split DWARF file, but no skeleton found.\n");
10986 else if (dwarf_skeleton != NULL)
10987 fprintf (stderr, "Warning: DWARF skeleton given,"
10988 " but not a split DWARF file\n");
10991 /* Get the section header string table index. */
10993 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
10994 error (EXIT_FAILURE, 0,
10995 gettext ("cannot get section header string table index"));
10997 /* If the .debug_info section is listed as implicitly required then
10998 we must make sure to handle it before handling any other debug
10999 section. Various other sections depend on the CU DIEs being
11000 scanned (silently) first. */
11001 bool implicit_info = (implicit_debug_sections & section_info) != 0;
11002 bool explicit_info = (print_debug_sections & section_info) != 0;
11005 Elf_Scn *scn = NULL;
11006 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11008 GElf_Shdr shdr_mem;
11009 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11011 if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11013 const char *name = elf_strptr (ebl->elf, shstrndx,
11018 if (strcmp (name, ".debug_info") == 0
11019 || strcmp (name, ".debug_info.dwo") == 0
11020 || strcmp (name, ".zdebug_info") == 0
11021 || strcmp (name, ".zdebug_info.dwo") == 0)
11023 print_debug_info_section (dwflmod, ebl, ehdr,
11029 print_debug_sections &= ~section_info;
11030 implicit_debug_sections &= ~section_info;
11033 /* Look through all the sections for the debugging sections to print. */
11034 Elf_Scn *scn = NULL;
11035 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
11037 GElf_Shdr shdr_mem;
11038 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
11040 if (shdr != NULL && shdr->sh_type == SHT_PROGBITS)
11042 static const struct
11045 enum section_e bitmask;
11046 void (*fp) (Dwfl_Module *, Ebl *,
11047 GElf_Ehdr *, Elf_Scn *, GElf_Shdr *, Dwarf *);
11048 } debug_sections[] =
11050 #define NEW_SECTION(name) \
11051 { ".debug_" #name, section_##name, print_debug_##name##_section }
11052 NEW_SECTION (abbrev),
11053 NEW_SECTION (addr),
11054 NEW_SECTION (aranges),
11055 NEW_SECTION (frame),
11056 NEW_SECTION (info),
11057 NEW_SECTION (types),
11058 NEW_SECTION (line),
11060 /* loclists is loc for DWARF5. */
11061 { ".debug_loclists", section_loc,
11062 print_debug_loclists_section },
11063 NEW_SECTION (pubnames),
11065 /* A DWARF5 specialised debug string section. */
11066 { ".debug_line_str", section_str,
11067 print_debug_str_section },
11068 /* DWARF5 string offsets table. */
11069 { ".debug_str_offsets", section_str,
11070 print_debug_str_offsets_section },
11071 NEW_SECTION (macinfo),
11072 NEW_SECTION (macro),
11073 NEW_SECTION (ranges),
11074 /* rnglists is ranges for DWARF5. */
11075 { ".debug_rnglists", section_ranges,
11076 print_debug_rnglists_section },
11077 { ".eh_frame", section_frame | section_exception,
11078 print_debug_frame_section },
11079 { ".eh_frame_hdr", section_frame | section_exception,
11080 print_debug_frame_hdr_section },
11081 { ".gcc_except_table", section_frame | section_exception,
11082 print_debug_exception_table },
11083 { ".gdb_index", section_gdb_index, print_gdb_index_section }
11085 const int ndebug_sections = (sizeof (debug_sections)
11086 / sizeof (debug_sections[0]));
11087 const char *name = elf_strptr (ebl->elf, shstrndx,
11093 for (n = 0; n < ndebug_sections; ++n)
11095 size_t dbglen = strlen (debug_sections[n].name);
11096 size_t scnlen = strlen (name);
11097 if ((strncmp (name, debug_sections[n].name, dbglen) == 0
11098 && (dbglen == scnlen
11099 || (scnlen == dbglen + 4
11100 && strstr (name, ".dwo") == name + dbglen)))
11101 || (name[0] == '.' && name[1] == 'z'
11102 && debug_sections[n].name[1] == 'd'
11103 && strncmp (&name[2], &debug_sections[n].name[1],
11105 && (scnlen == dbglen + 1
11106 || (scnlen == dbglen + 5
11107 && strstr (name, ".dwo") == name + dbglen + 1))))
11109 if ((print_debug_sections | implicit_debug_sections)
11110 & debug_sections[n].bitmask)
11111 debug_sections[n].fp (dwflmod, ebl, ehdr, scn, shdr, dbg);
11118 dwfl_end (skel_dwfl);
11121 /* Turn implicit and/or explicit back on in case we go over another file. */
11123 implicit_debug_sections |= section_info;
11125 print_debug_sections |= section_info;
11127 reset_listptr (&known_locsptr);
11128 reset_listptr (&known_loclistsptr);
11129 reset_listptr (&known_rangelistptr);
11130 reset_listptr (&known_rnglistptr);
11131 reset_listptr (&known_addrbases);
11132 reset_listptr (&known_stroffbases);
11136 #define ITEM_INDENT 4
11137 #define WRAP_COLUMN 75
11139 /* Print "NAME: FORMAT", wrapping when output text would make the line
11140 exceed WRAP_COLUMN. Unpadded numbers look better for the core items
11141 but this function is also used for registers which should be printed
11142 aligned. Fortunately registers output uses fixed fields width (such
11143 as %11d) for the alignment.
11145 Line breaks should not depend on the particular values although that
11146 may happen in some cases of the core items. */
11148 static unsigned int
11149 __attribute__ ((format (printf, 6, 7)))
11150 print_core_item (unsigned int colno, char sep, unsigned int wrap,
11151 size_t name_width, const char *name, const char *format, ...)
11153 size_t len = strlen (name);
11154 if (name_width < len)
11159 va_start (ap, format);
11160 int out_len = vasprintf (&out, format, ap);
11163 error (EXIT_FAILURE, 0, _("memory exhausted"));
11165 size_t n = name_width + sizeof ": " - 1 + out_len;
11169 printf ("%*s", ITEM_INDENT, "");
11170 colno = ITEM_INDENT + n;
11172 else if (colno + 2 + n < wrap)
11174 printf ("%c ", sep);
11179 printf ("\n%*s", ITEM_INDENT, "");
11180 colno = ITEM_INDENT + n;
11183 printf ("%s: %*s%s", name, (int) (name_width - len), "", out);
11190 static const void *
11191 convert (Elf *core, Elf_Type type, uint_fast16_t count,
11192 void *value, const void *data, size_t size)
11194 Elf_Data valuedata =
11198 .d_size = size ?: gelf_fsize (core, type, count, EV_CURRENT),
11199 .d_version = EV_CURRENT,
11204 .d_buf = (void *) data,
11205 .d_size = valuedata.d_size,
11206 .d_version = EV_CURRENT,
11209 Elf_Data *d = (gelf_getclass (core) == ELFCLASS32
11210 ? elf32_xlatetom : elf64_xlatetom)
11211 (&valuedata, &indata, elf_getident (core, NULL)[EI_DATA]);
11213 error (EXIT_FAILURE, 0,
11214 gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
11216 return data + indata.d_size;
11219 typedef uint8_t GElf_Byte;
11221 static unsigned int
11222 handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
11223 unsigned int colno, size_t *repeated_size)
11225 uint_fast16_t count = item->count ?: 1;
11226 /* Ebl_Core_Item count is always a small number.
11227 Make sure the backend didn't put in some large bogus value. */
11228 assert (count < 128);
11231 DO_TYPE (BYTE, Byte, "0x%.2" PRIx8, "%" PRId8); \
11232 DO_TYPE (HALF, Half, "0x%.4" PRIx16, "%" PRId16); \
11233 DO_TYPE (WORD, Word, "0x%.8" PRIx32, "%" PRId32); \
11234 DO_TYPE (SWORD, Sword, "%" PRId32, "%" PRId32); \
11235 DO_TYPE (XWORD, Xword, "0x%.16" PRIx64, "%" PRId64); \
11236 DO_TYPE (SXWORD, Sxword, "%" PRId64, "%" PRId64)
11238 #define DO_TYPE(NAME, Name, hex, dec) GElf_##Name Name
11239 typedef union { TYPES; } value_t;
11240 void *data = alloca (count * sizeof (value_t));
11243 #define DO_TYPE(NAME, Name, hex, dec) \
11244 GElf_##Name *value_##Name __attribute__((unused)) = data
11248 size_t size = gelf_fsize (core, item->type, count, EV_CURRENT);
11249 size_t convsize = size;
11250 if (repeated_size != NULL)
11252 if (*repeated_size > size && (item->format == 'b' || item->format == 'B'))
11254 data = alloca (*repeated_size);
11255 count *= *repeated_size / size;
11256 convsize = count * size;
11257 *repeated_size -= convsize;
11259 else if (item->count != 0 || item->format != '\n')
11260 *repeated_size -= size;
11263 convert (core, item->type, count, data, desc + item->offset, convsize);
11265 Elf_Type type = item->type;
11266 if (type == ELF_T_ADDR)
11267 type = gelf_getclass (core) == ELFCLASS32 ? ELF_T_WORD : ELF_T_XWORD;
11269 switch (item->format)
11272 assert (count == 1);
11275 #define DO_TYPE(NAME, Name, hex, dec) \
11276 case ELF_T_##NAME: \
11277 colno = print_core_item (colno, ',', WRAP_COLUMN, \
11278 0, item->name, dec, value_##Name[0]); \
11288 assert (count == 1);
11291 #define DO_TYPE(NAME, Name, hex, dec) \
11292 case ELF_T_##NAME: \
11293 colno = print_core_item (colno, ',', WRAP_COLUMN, \
11294 0, item->name, hex, value_##Name[0]); \
11305 assert (size % sizeof (unsigned int) == 0);
11306 unsigned int nbits = count * size * 8;
11307 unsigned int pop = 0;
11308 for (const unsigned int *i = data; (void *) i < data + count * size; ++i)
11309 pop += __builtin_popcount (*i);
11310 bool negate = pop > nbits / 2;
11311 const unsigned int bias = item->format == 'b';
11314 char printed[(negate ? nbits - pop : pop) * 16 + 1];
11318 if (BYTE_ORDER != LITTLE_ENDIAN && size > sizeof (unsigned int))
11320 assert (size == sizeof (unsigned int) * 2);
11321 for (unsigned int *i = data;
11322 (void *) i < data + count * size; i += 2)
11324 unsigned int w = i[1];
11330 unsigned int lastbit = 0;
11331 unsigned int run = 0;
11332 for (const unsigned int *i = data;
11333 (void *) i < data + count * size; ++i)
11335 unsigned int bit = ((void *) i - data) * 8;
11336 unsigned int w = negate ? ~*i : *i;
11339 /* Note that a right shift equal to (or greater than)
11340 the number of bits of w is undefined behaviour. In
11341 particular when the least significant bit is bit 32
11342 (w = 0x8000000) then w >>= n is undefined. So
11343 explicitly handle that case separately. */
11344 unsigned int n = ffs (w);
11345 if (n < sizeof (w) * 8)
11351 if (lastbit != 0 && lastbit + 1 == bit)
11356 p += sprintf (p, "%u", bit - bias);
11358 p += sprintf (p, ",%u", bit - bias);
11360 p += sprintf (p, "-%u,%u", lastbit - bias, bit - bias);
11367 if (lastbit > 0 && run > 0 && lastbit + 1 != nbits)
11368 p += sprintf (p, "-%u", lastbit - bias);
11370 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11371 negate ? "~<%s>" : "<%s>", printed);
11376 case (char) ('T'|0x80):
11377 assert (count == 2);
11382 #define DO_TYPE(NAME, Name, hex, dec) \
11383 case ELF_T_##NAME: \
11384 sec = value_##Name[0]; \
11385 usec = value_##Name[1]; \
11392 if (unlikely (item->format == (char) ('T'|0x80)))
11394 /* This is a hack for an ill-considered 64-bit ABI where
11395 tv_usec is actually a 32-bit field with 32 bits of padding
11396 rounding out struct timeval. We've already converted it as
11397 a 64-bit field. For little-endian, this just means the
11398 high half is the padding; it's presumably zero, but should
11399 be ignored anyway. For big-endian, it means the 32-bit
11400 field went into the high half of USEC. */
11401 GElf_Ehdr ehdr_mem;
11402 GElf_Ehdr *ehdr = gelf_getehdr (core, &ehdr_mem);
11403 if (likely (ehdr->e_ident[EI_DATA] == ELFDATA2MSB))
11406 usec &= UINT32_MAX;
11408 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11409 "%" PRIu64 ".%.6" PRIu64, sec, usec);
11413 assert (count == 1);
11414 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11415 "%c", value_Byte[0]);
11419 colno = print_core_item (colno, ',', WRAP_COLUMN, 0, item->name,
11420 "%.*s", (int) count, value_Byte);
11424 /* This is a list of strings separated by '\n'. */
11425 assert (item->count == 0);
11426 assert (repeated_size != NULL);
11427 assert (item->name == NULL);
11428 if (unlikely (item->offset >= *repeated_size))
11431 const char *s = desc + item->offset;
11432 size = *repeated_size - item->offset;
11433 *repeated_size = 0;
11436 const char *eol = memchr (s, '\n', size);
11440 printf ("%*s%.*s\n", ITEM_INDENT, "", len, s);
11443 size -= eol + 1 - s;
11447 colno = WRAP_COLUMN;
11454 error (0, 0, "XXX not handling format '%c' for %s",
11455 item->format, item->name);
11465 /* Sort items by group, and by layout offset within each group. */
11467 compare_core_items (const void *a, const void *b)
11469 const Ebl_Core_Item *const *p1 = a;
11470 const Ebl_Core_Item *const *p2 = b;
11471 const Ebl_Core_Item *item1 = *p1;
11472 const Ebl_Core_Item *item2 = *p2;
11474 return ((item1->group == item2->group ? 0
11475 : strcmp (item1->group, item2->group))
11476 ?: (int) item1->offset - (int) item2->offset);
11479 /* Sort item groups by layout offset of the first item in the group. */
11481 compare_core_item_groups (const void *a, const void *b)
11483 const Ebl_Core_Item *const *const *p1 = a;
11484 const Ebl_Core_Item *const *const *p2 = b;
11485 const Ebl_Core_Item *const *group1 = *p1;
11486 const Ebl_Core_Item *const *group2 = *p2;
11487 const Ebl_Core_Item *item1 = *group1;
11488 const Ebl_Core_Item *item2 = *group2;
11490 return (int) item1->offset - (int) item2->offset;
11493 static unsigned int
11494 handle_core_items (Elf *core, const void *desc, size_t descsz,
11495 const Ebl_Core_Item *items, size_t nitems)
11499 unsigned int colno = 0;
11501 /* FORMAT '\n' makes sense to be present only as a single item as it
11502 processes all the data of a note. FORMATs 'b' and 'B' have a special case
11503 if present as a single item but they can be also processed with other
11505 if (nitems == 1 && (items[0].format == '\n' || items[0].format == 'b'
11506 || items[0].format == 'B'))
11508 assert (items[0].offset == 0);
11509 size_t size = descsz;
11510 colno = handle_core_item (core, items, desc, colno, &size);
11511 /* If SIZE is not zero here there is some remaining data. But we do not
11512 know how to process it anyway. */
11515 for (size_t i = 0; i < nitems; ++i)
11516 assert (items[i].format != '\n');
11518 /* Sort to collect the groups together. */
11519 const Ebl_Core_Item *sorted_items[nitems];
11520 for (size_t i = 0; i < nitems; ++i)
11521 sorted_items[i] = &items[i];
11522 qsort (sorted_items, nitems, sizeof sorted_items[0], &compare_core_items);
11524 /* Collect the unique groups and sort them. */
11525 const Ebl_Core_Item **groups[nitems];
11526 groups[0] = &sorted_items[0];
11527 size_t ngroups = 1;
11528 for (size_t i = 1; i < nitems; ++i)
11529 if (sorted_items[i]->group != sorted_items[i - 1]->group
11530 && strcmp (sorted_items[i]->group, sorted_items[i - 1]->group))
11531 groups[ngroups++] = &sorted_items[i];
11532 qsort (groups, ngroups, sizeof groups[0], &compare_core_item_groups);
11534 /* Write out all the groups. */
11535 const void *last = desc;
11538 for (size_t i = 0; i < ngroups; ++i)
11540 for (const Ebl_Core_Item **item = groups[i];
11541 (item < &sorted_items[nitems]
11542 && ((*item)->group == groups[i][0]->group
11543 || !strcmp ((*item)->group, groups[i][0]->group)));
11545 colno = handle_core_item (core, *item, desc, colno, NULL);
11547 /* Force a line break at the end of the group. */
11548 colno = WRAP_COLUMN;
11554 /* This set of items consumed a certain amount of the note's data.
11555 If there is more data there, we have another unit of the same size.
11556 Loop to print that out too. */
11557 const Ebl_Core_Item *item = &items[nitems - 1];
11558 size_t eltsz = item->offset + gelf_fsize (core, item->type,
11559 item->count ?: 1, EV_CURRENT);
11568 while (descsz >= eltsz && !memcmp (desc, last, eltsz));
11572 /* For just one repeat, print it unabridged twice. */
11577 printf (gettext ("\n%*s... <repeats %u more times> ..."),
11578 ITEM_INDENT, "", reps);
11582 while (descsz > 0);
11587 static unsigned int
11588 handle_bit_registers (const Ebl_Register_Location *regloc, const void *desc,
11589 unsigned int colno)
11591 desc += regloc->offset;
11593 abort (); /* XXX */
11598 static unsigned int
11599 handle_core_register (Ebl *ebl, Elf *core, int maxregname,
11600 const Ebl_Register_Location *regloc, const void *desc,
11601 unsigned int colno)
11603 if (regloc->bits % 8 != 0)
11604 return handle_bit_registers (regloc, desc, colno);
11606 desc += regloc->offset;
11608 for (int reg = regloc->regno; reg < regloc->regno + regloc->count; ++reg)
11610 char name[REGNAMESZ];
11613 register_info (ebl, reg, regloc, name, &bits, &type);
11616 BITS (8, BYTE, "%4" PRId8, "0x%.2" PRIx8); \
11617 BITS (16, HALF, "%6" PRId16, "0x%.4" PRIx16); \
11618 BITS (32, WORD, "%11" PRId32, " 0x%.8" PRIx32); \
11619 BITS (64, XWORD, "%20" PRId64, " 0x%.16" PRIx64)
11621 #define BITS(bits, xtype, sfmt, ufmt) \
11622 uint##bits##_t b##bits; int##bits##_t b##bits##s
11623 union { TYPES; uint64_t b128[2]; } value;
11628 case DW_ATE_unsigned:
11629 case DW_ATE_signed:
11630 case DW_ATE_address:
11633 #define BITS(bits, xtype, sfmt, ufmt) \
11635 desc = convert (core, ELF_T_##xtype, 1, &value, desc, 0); \
11636 if (type == DW_ATE_signed) \
11637 colno = print_core_item (colno, ' ', WRAP_COLUMN, \
11638 maxregname, name, \
11639 sfmt, value.b##bits##s); \
11641 colno = print_core_item (colno, ' ', WRAP_COLUMN, \
11642 maxregname, name, \
11643 ufmt, value.b##bits); \
11649 assert (type == DW_ATE_unsigned);
11650 desc = convert (core, ELF_T_XWORD, 2, &value, desc, 0);
11651 int be = elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB;
11652 colno = print_core_item (colno, ' ', WRAP_COLUMN,
11654 "0x%.16" PRIx64 "%.16" PRIx64,
11655 value.b128[!be], value.b128[be]);
11665 /* Print each byte in hex, the whole thing in native byte order. */
11666 assert (bits % 8 == 0);
11667 const uint8_t *bytes = desc;
11669 char hex[bits / 4 + 1];
11670 hex[bits / 4] = '\0';
11672 if (elf_getident (core, NULL)[EI_DATA] == ELFDATA2LSB)
11674 bytes += bits / 8 - 1;
11678 for (char *h = hex; bits > 0; bits -= 8, idx += incr)
11680 *h++ = "0123456789abcdef"[bytes[idx] >> 4];
11681 *h++ = "0123456789abcdef"[bytes[idx] & 0xf];
11683 colno = print_core_item (colno, ' ', WRAP_COLUMN,
11684 maxregname, name, "0x%s", hex);
11687 desc += regloc->pad;
11696 struct register_info
11698 const Ebl_Register_Location *regloc;
11700 char name[REGNAMESZ];
11707 register_bitpos (const struct register_info *r)
11709 return (r->regloc->offset * 8
11710 + ((r->regno - r->regloc->regno)
11711 * (r->regloc->bits + r->regloc->pad * 8)));
11715 compare_sets_by_info (const struct register_info *r1,
11716 const struct register_info *r2)
11718 return ((int) r2->bits - (int) r1->bits
11719 ?: register_bitpos (r1) - register_bitpos (r2));
11722 /* Sort registers by set, and by size and layout offset within each set. */
11724 compare_registers (const void *a, const void *b)
11726 const struct register_info *r1 = a;
11727 const struct register_info *r2 = b;
11729 /* Unused elements sort last. */
11730 if (r1->regloc == NULL)
11731 return r2->regloc == NULL ? 0 : 1;
11732 if (r2->regloc == NULL)
11735 return ((r1->set == r2->set ? 0 : strcmp (r1->set, r2->set))
11736 ?: compare_sets_by_info (r1, r2));
11739 /* Sort register sets by layout offset of the first register in the set. */
11741 compare_register_sets (const void *a, const void *b)
11743 const struct register_info *const *p1 = a;
11744 const struct register_info *const *p2 = b;
11745 return compare_sets_by_info (*p1, *p2);
11748 static unsigned int
11749 handle_core_registers (Ebl *ebl, Elf *core, const void *desc,
11750 const Ebl_Register_Location *reglocs, size_t nregloc)
11755 ssize_t maxnreg = ebl_register_info (ebl, 0, NULL, 0, NULL, NULL, NULL, NULL);
11758 for (size_t i = 0; i < nregloc; ++i)
11759 if (maxnreg < reglocs[i].regno + reglocs[i].count)
11760 maxnreg = reglocs[i].regno + reglocs[i].count;
11761 assert (maxnreg > 0);
11764 struct register_info regs[maxnreg];
11765 memset (regs, 0, sizeof regs);
11767 /* Sort to collect the sets together. */
11769 for (size_t i = 0; i < nregloc; ++i)
11770 for (int reg = reglocs[i].regno;
11771 reg < reglocs[i].regno + reglocs[i].count;
11774 assert (reg < maxnreg);
11777 struct register_info *info = ®s[reg];
11778 info->regloc = ®locs[i];
11780 info->set = register_info (ebl, reg, ®locs[i],
11781 info->name, &info->bits, &info->type);
11783 qsort (regs, maxreg + 1, sizeof regs[0], &compare_registers);
11785 /* Collect the unique sets and sort them. */
11786 inline bool same_set (const struct register_info *a,
11787 const struct register_info *b)
11789 return (a < ®s[maxnreg] && a->regloc != NULL
11790 && b < ®s[maxnreg] && b->regloc != NULL
11791 && a->bits == b->bits
11792 && (a->set == b->set || !strcmp (a->set, b->set)));
11794 struct register_info *sets[maxreg + 1];
11795 sets[0] = ®s[0];
11797 for (int i = 1; i <= maxreg; ++i)
11798 if (regs[i].regloc != NULL && !same_set (®s[i], ®s[i - 1]))
11799 sets[nsets++] = ®s[i];
11800 qsort (sets, nsets, sizeof sets[0], &compare_register_sets);
11802 /* Write out all the sets. */
11803 unsigned int colno = 0;
11804 for (size_t i = 0; i < nsets; ++i)
11806 /* Find the longest name of a register in this set. */
11807 size_t maxname = 0;
11808 const struct register_info *end;
11809 for (end = sets[i]; same_set (sets[i], end); ++end)
11811 size_t len = strlen (end->name);
11816 for (const struct register_info *reg = sets[i];
11818 reg += reg->regloc->count ?: 1)
11819 colno = handle_core_register (ebl, core, maxname,
11820 reg->regloc, desc, colno);
11822 /* Force a line break at the end of the group. */
11823 colno = WRAP_COLUMN;
11830 handle_auxv_note (Ebl *ebl, Elf *core, GElf_Word descsz, GElf_Off desc_pos)
11832 Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_AUXV);
11835 error (EXIT_FAILURE, 0,
11836 gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
11838 const size_t nauxv = descsz / gelf_fsize (core, ELF_T_AUXV, 1, EV_CURRENT);
11839 for (size_t i = 0; i < nauxv; ++i)
11841 GElf_auxv_t av_mem;
11842 GElf_auxv_t *av = gelf_getauxv (data, i, &av_mem);
11848 if (ebl_auxv_info (ebl, av->a_type, &name, &fmt) == 0)
11850 /* Unknown type. */
11851 if (av->a_un.a_val == 0)
11852 printf (" %" PRIu64 "\n", av->a_type);
11854 printf (" %" PRIu64 ": %#" PRIx64 "\n",
11855 av->a_type, av->a_un.a_val);
11860 case '\0': /* Normally zero. */
11861 if (av->a_un.a_val == 0)
11863 printf (" %s\n", name);
11867 case 'x': /* hex */
11868 case 'p': /* address */
11869 case 's': /* address of string */
11870 printf (" %s: %#" PRIx64 "\n", name, av->a_un.a_val);
11873 printf (" %s: %" PRIu64 "\n", name, av->a_un.a_val);
11876 printf (" %s: %" PRId64 "\n", name, av->a_un.a_val);
11880 printf (" %s: %#" PRIx64 " ", name, av->a_un.a_val);
11881 GElf_Xword bit = 1;
11882 const char *pfx = "<";
11883 for (const char *p = fmt + 1; *p != 0; p = strchr (p, '\0') + 1)
11885 if (av->a_un.a_val & bit)
11887 printf ("%s%s", pfx, p);
11902 buf_has_data (unsigned char const *ptr, unsigned char const *end, size_t sz)
11904 return ptr < end && (size_t) (end - ptr) >= sz;
11908 buf_read_int (Elf *core, unsigned char const **ptrp, unsigned char const *end,
11911 if (! buf_has_data (*ptrp, end, 4))
11914 *ptrp = convert (core, ELF_T_WORD, 1, retp, *ptrp, 4);
11919 buf_read_ulong (Elf *core, unsigned char const **ptrp, unsigned char const *end,
11922 size_t sz = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
11923 if (! buf_has_data (*ptrp, end, sz))
11932 *ptrp = convert (core, ELF_T_ADDR, 1, &u, *ptrp, sz);
11942 handle_siginfo_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
11944 Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
11946 error (EXIT_FAILURE, 0,
11947 gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
11949 unsigned char const *ptr = data->d_buf;
11950 unsigned char const *const end = data->d_buf + data->d_size;
11952 /* Siginfo head is three ints: signal number, error number, origin
11954 int si_signo, si_errno, si_code;
11955 if (! buf_read_int (core, &ptr, end, &si_signo)
11956 || ! buf_read_int (core, &ptr, end, &si_errno)
11957 || ! buf_read_int (core, &ptr, end, &si_code))
11960 printf (" Not enough data in NT_SIGINFO note.\n");
11964 /* Next is a pointer-aligned union of structures. On 64-bit
11965 machines, that implies a word of padding. */
11966 if (gelf_getclass (core) == ELFCLASS64)
11969 printf (" si_signo: %d, si_errno: %d, si_code: %d\n",
11970 si_signo, si_errno, si_code);
11981 if (! buf_read_ulong (core, &ptr, end, &addr))
11983 printf (" fault address: %#" PRIx64 "\n", addr);
11989 else if (si_code == CORE_SI_USER)
11992 if (! buf_read_int (core, &ptr, end, &pid)
11993 || ! buf_read_int (core, &ptr, end, &uid))
11995 printf (" sender PID: %d, sender UID: %d\n", pid, uid);
12000 handle_file_note (Elf *core, GElf_Word descsz, GElf_Off desc_pos)
12002 Elf_Data *data = elf_getdata_rawchunk (core, desc_pos, descsz, ELF_T_BYTE);
12004 error (EXIT_FAILURE, 0,
12005 gettext ("cannot convert core note data: %s"), elf_errmsg (-1));
12007 unsigned char const *ptr = data->d_buf;
12008 unsigned char const *const end = data->d_buf + data->d_size;
12010 uint64_t count, page_size;
12011 if (! buf_read_ulong (core, &ptr, end, &count)
12012 || ! buf_read_ulong (core, &ptr, end, &page_size))
12015 printf (" Not enough data in NT_FILE note.\n");
12019 size_t addrsize = gelf_fsize (core, ELF_T_ADDR, 1, EV_CURRENT);
12020 uint64_t maxcount = (size_t) (end - ptr) / (3 * addrsize);
12021 if (count > maxcount)
12024 /* Where file names are stored. */
12025 unsigned char const *const fstart = ptr + 3 * count * addrsize;
12026 char const *fptr = (char *) fstart;
12028 printf (" %" PRId64 " files:\n", count);
12029 for (uint64_t i = 0; i < count; ++i)
12031 uint64_t mstart, mend, moffset;
12032 if (! buf_read_ulong (core, &ptr, fstart, &mstart)
12033 || ! buf_read_ulong (core, &ptr, fstart, &mend)
12034 || ! buf_read_ulong (core, &ptr, fstart, &moffset))
12037 const char *fnext = memchr (fptr, '\0', (char *) end - fptr);
12041 int ct = printf (" %08" PRIx64 "-%08" PRIx64
12042 " %08" PRIx64 " %" PRId64,
12043 mstart, mend, moffset * page_size, mend - mstart);
12044 printf ("%*s%s\n", ct > 50 ? 3 : 53 - ct, "", fptr);
12051 handle_core_note (Ebl *ebl, const GElf_Nhdr *nhdr,
12052 const char *name, const void *desc)
12054 GElf_Word regs_offset;
12056 const Ebl_Register_Location *reglocs;
12058 const Ebl_Core_Item *items;
12060 if (! ebl_core_note (ebl, nhdr, name,
12061 ®s_offset, &nregloc, ®locs, &nitems, &items))
12064 /* Pass 0 for DESCSZ when there are registers in the note,
12065 so that the ITEMS array does not describe the whole thing.
12066 For non-register notes, the actual descsz might be a multiple
12067 of the unit size, not just exactly the unit size. */
12068 unsigned int colno = handle_core_items (ebl->elf, desc,
12069 nregloc == 0 ? nhdr->n_descsz : 0,
12072 putchar_unlocked ('\n');
12074 colno = handle_core_registers (ebl, ebl->elf, desc + regs_offset,
12077 putchar_unlocked ('\n');
12081 handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
12082 GElf_Off start, Elf_Data *data)
12084 fputs_unlocked (gettext (" Owner Data size Type\n"), stdout);
12091 size_t name_offset;
12092 size_t desc_offset;
12093 while (offset < data->d_size
12094 && (offset = gelf_getnote (data, offset,
12095 &nhdr, &name_offset, &desc_offset)) > 0)
12097 const char *name = nhdr.n_namesz == 0 ? "" : data->d_buf + name_offset;
12098 const char *desc = data->d_buf + desc_offset;
12102 printf (gettext (" %-13.*s %9" PRId32 " %s\n"),
12103 (int) nhdr.n_namesz, name, nhdr.n_descsz,
12104 ehdr->e_type == ET_CORE
12105 ? ebl_core_note_type_name (ebl, nhdr.n_type,
12107 : ebl_object_note_type_name (ebl, name, nhdr.n_type,
12108 buf2, sizeof (buf2)));
12110 /* Filter out invalid entries. */
12111 if (memchr (name, '\0', nhdr.n_namesz) != NULL
12112 /* XXX For now help broken Linux kernels. */
12115 if (ehdr->e_type == ET_CORE)
12117 if (nhdr.n_type == NT_AUXV
12118 && (nhdr.n_namesz == 4 /* Broken old Linux kernels. */
12119 || (nhdr.n_namesz == 5 && name[4] == '\0'))
12120 && !memcmp (name, "CORE", 4))
12121 handle_auxv_note (ebl, ebl->elf, nhdr.n_descsz,
12122 start + desc_offset);
12123 else if (nhdr.n_namesz == 5 && strcmp (name, "CORE") == 0)
12124 switch (nhdr.n_type)
12127 handle_siginfo_note (ebl->elf, nhdr.n_descsz,
12128 start + desc_offset);
12132 handle_file_note (ebl->elf, nhdr.n_descsz,
12133 start + desc_offset);
12137 handle_core_note (ebl, &nhdr, name, desc);
12140 handle_core_note (ebl, &nhdr, name, desc);
12143 ebl_object_note (ebl, name, nhdr.n_type, nhdr.n_descsz, desc);
12147 if (offset == data->d_size)
12152 gettext ("cannot get content of note: %s"),
12153 data != NULL ? "garbage data" : elf_errmsg (-1));
12157 handle_notes (Ebl *ebl, GElf_Ehdr *ehdr)
12159 /* If we have section headers, just look for SHT_NOTE sections.
12160 In a debuginfo file, the program headers are not reliable. */
12163 /* Get the section header string table index. */
12165 if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
12166 error (EXIT_FAILURE, 0,
12167 gettext ("cannot get section header string table index"));
12169 Elf_Scn *scn = NULL;
12170 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12172 GElf_Shdr shdr_mem;
12173 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
12175 if (shdr == NULL || shdr->sh_type != SHT_NOTE)
12176 /* Not what we are looking for. */
12179 printf (gettext ("\
12180 \nNote section [%2zu] '%s' of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12182 elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
12183 shdr->sh_size, shdr->sh_offset);
12185 handle_notes_data (ebl, ehdr, shdr->sh_offset,
12186 elf_getdata (scn, NULL));
12191 /* We have to look through the program header to find the note
12192 sections. There can be more than one. */
12193 for (size_t cnt = 0; cnt < phnum; ++cnt)
12196 GElf_Phdr *phdr = gelf_getphdr (ebl->elf, cnt, &mem);
12198 if (phdr == NULL || phdr->p_type != PT_NOTE)
12199 /* Not what we are looking for. */
12202 printf (gettext ("\
12203 \nNote segment of %" PRIu64 " bytes at offset %#0" PRIx64 ":\n"),
12204 phdr->p_filesz, phdr->p_offset);
12206 handle_notes_data (ebl, ehdr, phdr->p_offset,
12207 elf_getdata_rawchunk (ebl->elf,
12208 phdr->p_offset, phdr->p_filesz,
12215 hex_dump (const uint8_t *data, size_t len)
12220 printf (" 0x%08zx ", pos);
12222 const size_t chunk = MIN (len - pos, 16);
12224 for (size_t i = 0; i < chunk; ++i)
12226 printf ("%02x ", data[pos + i]);
12228 printf ("%02x", data[pos + i]);
12231 printf ("%*s", (int) ((16 - chunk) * 2 + (16 - chunk + 3) / 4), "");
12233 for (size_t i = 0; i < chunk; ++i)
12235 unsigned char b = data[pos + i];
12236 printf ("%c", isprint (b) ? b : '.');
12245 dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12247 if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12248 printf (gettext ("\nSection [%zu] '%s' has no data to dump.\n"),
12249 elf_ndxscn (scn), name);
12252 if (print_decompress)
12254 /* We try to decompress the section, but keep the old shdr around
12255 so we can show both the original shdr size and the uncompressed
12257 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12259 if (elf_compress (scn, 0, 0) < 0)
12260 printf ("WARNING: %s [%zd]\n",
12261 gettext ("Couldn't uncompress section"),
12264 else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
12266 if (elf_compress_gnu (scn, 0, 0) < 0)
12267 printf ("WARNING: %s [%zd]\n",
12268 gettext ("Couldn't uncompress section"),
12273 Elf_Data *data = elf_rawdata (scn, NULL);
12275 error (0, 0, gettext ("cannot get data for section [%zu] '%s': %s"),
12276 elf_ndxscn (scn), name, elf_errmsg (-1));
12279 if (data->d_size == shdr->sh_size)
12280 printf (gettext ("\nHex dump of section [%zu] '%s', %" PRIu64
12281 " bytes at offset %#0" PRIx64 ":\n"),
12282 elf_ndxscn (scn), name,
12283 shdr->sh_size, shdr->sh_offset);
12285 printf (gettext ("\nHex dump of section [%zu] '%s', %" PRIu64
12286 " bytes (%zd uncompressed) at offset %#0"
12288 elf_ndxscn (scn), name,
12289 shdr->sh_size, data->d_size, shdr->sh_offset);
12290 hex_dump (data->d_buf, data->d_size);
12296 print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
12298 if (shdr->sh_size == 0 || shdr->sh_type == SHT_NOBITS)
12299 printf (gettext ("\nSection [%zu] '%s' has no strings to dump.\n"),
12300 elf_ndxscn (scn), name);
12303 if (print_decompress)
12305 /* We try to decompress the section, but keep the old shdr around
12306 so we can show both the original shdr size and the uncompressed
12308 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
12310 if (elf_compress (scn, 0, 0) < 0)
12311 printf ("WARNING: %s [%zd]\n",
12312 gettext ("Couldn't uncompress section"),
12315 else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
12317 if (elf_compress_gnu (scn, 0, 0) < 0)
12318 printf ("WARNING: %s [%zd]\n",
12319 gettext ("Couldn't uncompress section"),
12324 Elf_Data *data = elf_rawdata (scn, NULL);
12326 error (0, 0, gettext ("cannot get data for section [%zu] '%s': %s"),
12327 elf_ndxscn (scn), name, elf_errmsg (-1));
12330 if (data->d_size == shdr->sh_size)
12331 printf (gettext ("\nString section [%zu] '%s' contains %" PRIu64
12332 " bytes at offset %#0" PRIx64 ":\n"),
12333 elf_ndxscn (scn), name,
12334 shdr->sh_size, shdr->sh_offset);
12336 printf (gettext ("\nString section [%zu] '%s' contains %" PRIu64
12337 " bytes (%zd uncompressed) at offset %#0"
12339 elf_ndxscn (scn), name,
12340 shdr->sh_size, data->d_size, shdr->sh_offset);
12342 const char *start = data->d_buf;
12343 const char *const limit = start + data->d_size;
12346 const char *end = memchr (start, '\0', limit - start);
12347 const size_t pos = start - (const char *) data->d_buf;
12348 if (unlikely (end == NULL))
12350 printf (" [%6zx]- %.*s\n",
12351 pos, (int) (limit - start), start);
12354 printf (" [%6zx] %s\n", pos, start);
12356 } while (start < limit);
12362 for_each_section_argument (Elf *elf, const struct section_argument *list,
12363 void (*dump) (Elf_Scn *scn, const GElf_Shdr *shdr,
12366 /* Get the section header string table index. */
12368 if (elf_getshdrstrndx (elf, &shstrndx) < 0)
12369 error (EXIT_FAILURE, 0,
12370 gettext ("cannot get section header string table index"));
12372 for (const struct section_argument *a = list; a != NULL; a = a->next)
12375 GElf_Shdr shdr_mem;
12376 const char *name = NULL;
12379 unsigned long int shndx = strtoul (a->arg, &endp, 0);
12380 if (endp != a->arg && *endp == '\0')
12382 scn = elf_getscn (elf, shndx);
12385 error (0, 0, gettext ("\nsection [%lu] does not exist"), shndx);
12389 if (gelf_getshdr (scn, &shdr_mem) == NULL)
12390 error (EXIT_FAILURE, 0, gettext ("cannot get section header: %s"),
12392 name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12396 /* Need to look up the section by name. */
12398 bool found = false;
12399 while ((scn = elf_nextscn (elf, scn)) != NULL)
12401 if (gelf_getshdr (scn, &shdr_mem) == NULL)
12403 name = elf_strptr (elf, shstrndx, shdr_mem.sh_name);
12406 if (!strcmp (name, a->arg))
12409 (*dump) (scn, &shdr_mem, name);
12413 if (unlikely (!found) && !a->implicit)
12414 error (0, 0, gettext ("\nsection '%s' does not exist"), a->arg);
12420 dump_data (Ebl *ebl)
12422 for_each_section_argument (ebl->elf, dump_data_sections, &dump_data_section);
12426 dump_strings (Ebl *ebl)
12428 for_each_section_argument (ebl->elf, string_sections, &print_string_section);
12432 print_strings (Ebl *ebl)
12434 /* Get the section header string table index. */
12436 if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
12437 error (EXIT_FAILURE, 0,
12438 gettext ("cannot get section header string table index"));
12441 GElf_Shdr shdr_mem;
12444 while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
12446 if (gelf_getshdr (scn, &shdr_mem) == NULL)
12449 if (shdr_mem.sh_type != SHT_PROGBITS
12450 || !(shdr_mem.sh_flags & SHF_STRINGS))
12453 name = elf_strptr (ebl->elf, shstrndx, shdr_mem.sh_name);
12457 print_string_section (scn, &shdr_mem, name);
12462 dump_archive_index (Elf *elf, const char *fname)
12465 const Elf_Arsym *arsym = elf_getarsym (elf, &narsym);
12468 int result = elf_errno ();
12469 if (unlikely (result != ELF_E_NO_INDEX))
12470 error (EXIT_FAILURE, 0,
12471 gettext ("cannot get symbol index of archive '%s': %s"),
12472 fname, elf_errmsg (result));
12474 printf (gettext ("\nArchive '%s' has no symbol index\n"), fname);
12478 printf (gettext ("\nIndex of archive '%s' has %zu entries:\n"),
12482 for (const Elf_Arsym *s = arsym; s < &arsym[narsym - 1]; ++s)
12484 if (s->as_off != as_off)
12486 as_off = s->as_off;
12488 Elf *subelf = NULL;
12489 if (unlikely (elf_rand (elf, as_off) == 0)
12490 || unlikely ((subelf = elf_begin (-1, ELF_C_READ_MMAP, elf))
12492 #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 7)
12495 error (EXIT_FAILURE, 0,
12496 gettext ("cannot extract member at offset %zu in '%s': %s"),
12497 as_off, fname, elf_errmsg (-1));
12499 const Elf_Arhdr *h = elf_getarhdr (subelf);
12501 printf (gettext ("Archive member '%s' contains:\n"), h->ar_name);
12506 printf ("\t%s\n", s->as_name);
12510 #include "debugpred.h"